150476Speter# $FreeBSD: releng/11.0/share/mk/bsd.obj.mk 295641 2016-02-16 02:09:48Z bdrewery $
215353Swosch#
318566Sbde# The include file <bsd.obj.mk> handles creating the 'obj' directory
431859Sbde# and cleaning up object files, etc.
515353Swosch#
615353Swosch# +++ variables +++
715353Swosch#
833815Sbde# CLEANDIRS	Additional directories to remove for the clean target.
915948Swosch#
1033815Sbde# CLEANFILES	Additional files to remove for the clean target.
1133815Sbde#
12139761Skrion# MAKEOBJDIR 	A pathname for the directory where the targets
1399202Sru#		are built.  Note: MAKEOBJDIR is an *environment* variable
1499202Sru#		and works properly only if set as an environment variable,
1518566Sbde#		not as a global or command line variable!
1615353Swosch#
1718566Sbde#		E.g. use `env MAKEOBJDIR=temp-obj make'
1818566Sbde#
1918566Sbde# MAKEOBJDIRPREFIX  Specifies somewhere other than /usr/obj to root the object
2099202Sru#		tree.  Note: MAKEOBJDIRPREFIX is an *environment* variable
2199202Sru#		and works properly only if set as an environment variable,
2218566Sbde#		not as a global or command line variable!
2318566Sbde#
2418340Sswallace#		E.g. use `env MAKEOBJDIRPREFIX=/somewhere/obj make'
2515948Swosch#
26139103Sru# NO_OBJ	Do not create object directories.  This should not be set
2718566Sbde#		if anything is built.
2815353Swosch#
2915353Swosch# +++ targets +++
3015353Swosch#
3115353Swosch#	clean:
3233815Sbde#		remove ${CLEANFILES}; remove ${CLEANDIRS} and all contents.
3315353Swosch#
3415353Swosch#	cleandir:
3516663Sjkh#		remove the build directory (and all its contents) created by obj
3615353Swosch#
3715353Swosch#	obj:
3816663Sjkh#		create build directory.
3915353Swosch#
4014801Swosch
4194940Sru.if !target(__<bsd.obj.mk>__)
4294940Sru__<bsd.obj.mk>__:
4394940Sru.include <bsd.own.mk>
4494940Sru
45284345Ssjg.if ${MK_AUTO_OBJ} == "yes"
46284345Ssjg# it is done by now
47284345Ssjgobjwarn:
48284345Ssjgobj:
49284345SsjgCANONICALOBJDIR= ${.OBJDIR}
50284345Ssjg.if defined(NO_OBJ)
51284345Ssjg# but this makefile does not want it!
52284345Ssjg.OBJDIR: ${.CURDIR}
53284345Ssjg.endif
54284345Ssjg.elif defined(MAKEOBJDIRPREFIX)
5518340SswallaceCANONICALOBJDIR:=${MAKEOBJDIRPREFIX}${.CURDIR}
56241298Smarcel.elif defined(MAKEOBJDIR) && ${MAKEOBJDIR:M/*} != ""
57241298SmarcelCANONICALOBJDIR:=${MAKEOBJDIR}
58291349SbdreweryOBJTOP?= ${MAKEOBJDIR}
5918340Sswallace.else
6018340SswallaceCANONICALOBJDIR:=/usr/obj${.CURDIR}
6118340Sswallace.endif
6214801Swosch
63291349SbdreweryOBJTOP?= ${.OBJDIR:S,${.CURDIR},,}${SRCTOP}
64291349Sbdrewery
6518340Sswallace#
6676896Sru# Warn of unorthodox object directory.
6718340Sswallace#
6876896Sru# The following directories are tried in order for ${.OBJDIR}:
6976896Sru#
7076896Sru# 1.  ${MAKEOBJDIRPREFIX}/`pwd`
7176896Sru# 2.  ${MAKEOBJDIR}
7276896Sru# 3.  obj.${MACHINE}
7376896Sru# 4.  obj
7476896Sru# 5.  /usr/obj/`pwd`
7576896Sru# 6.  ${.CURDIR}
7676896Sru#
7776896Sru# If ${.OBJDIR} is constructed using canonical cases 1 or 5, or
7876896Sru# case 2 (using MAKEOBJDIR), don't issue a warning.  Otherwise,
7976896Sru# issue a warning differentiating between cases 6 and (3 or 4).
8076896Sru#
8118340Sswallaceobjwarn:
82139103Sru.if !defined(NO_OBJ) && ${.OBJDIR} != ${CANONICALOBJDIR} && \
8376896Sru    !(defined(MAKEOBJDIRPREFIX) && exists(${CANONICALOBJDIR}/)) && \
8476896Sru    !(defined(MAKEOBJDIR) && exists(${MAKEOBJDIR}/))
8518340Sswallace.if ${.OBJDIR} == ${.CURDIR}
8618373Speter	@${ECHO} "Warning: Object directory not changed from original ${.CURDIR}"
8776896Sru.elif exists(${.CURDIR}/obj.${MACHINE}/) || exists(${.CURDIR}/obj/)
8818373Speter	@${ECHO} "Warning: Using ${.OBJDIR} as object directory instead of\
8918340Sswallace		canonical ${CANONICALOBJDIR}"
9018340Sswallace.endif
9118340Sswallace.endif
92295641Sbdrewerybeforebuild: objwarn
9318340Sswallace
94139103Sru.if !defined(NO_OBJ)
9598546Sru.if !target(obj)
96128188Sruobj: .PHONY
9725316Sbde	@if ! test -d ${CANONICALOBJDIR}/; then \
9818340Sswallace		mkdir -p ${CANONICALOBJDIR}; \
9925316Sbde		if ! test -d ${CANONICALOBJDIR}/; then \
10018340Sswallace			${ECHO} "Unable to create ${CANONICALOBJDIR}."; \
10116663Sjkh			exit 1; \
10216663Sjkh		fi; \
10318340Sswallace		${ECHO} "${CANONICALOBJDIR} created for ${.CURDIR}"; \
10416663Sjkh	fi
105279980Sdim.for dir in ${SRCS:H:O:u}
106279980Sdim	@if ! test -d ${CANONICALOBJDIR}/${dir}/; then \
107279980Sdim		mkdir -p ${CANONICALOBJDIR}/${dir}; \
108279980Sdim		if ! test -d ${CANONICALOBJDIR}/${dir}/; then \
109279980Sdim			${ECHO} "Unable to create ${CANONICALOBJDIR}/${dir}."; \
110279980Sdim			exit 1; \
111279980Sdim		fi; \
112279980Sdim		${ECHO} "${CANONICALOBJDIR}/${dir} created for ${.CURDIR}"; \
113279980Sdim	fi
114279980Sdim.endfor
11514801Swosch.endif
11614801Swosch
11744922Sbde.if !target(objlink)
11895306Sruobjlink:
11944922Sbde	@if test -d ${CANONICALOBJDIR}/; then \
12044922Sbde		rm -f ${.CURDIR}/obj; \
12144922Sbde		ln -s ${CANONICALOBJDIR} ${.CURDIR}/obj; \
12244922Sbde	else \
12344922Sbde		echo "No ${CANONICALOBJDIR} to link to - do a make obj."; \
12444922Sbde	fi
12544922Sbde.endif
126144893Sharti.endif # !defined(NO_OBJ)
12744922Sbde
12815166Swosch#
12917116Spst# where would that obj directory be?
13017116Spst#
13117116Spst.if !target(whereobj)
13217116Spstwhereobj:
13336641Speter	@echo ${.OBJDIR}
13417116Spst.endif
13517116Spst
136124061Sgreen.if ${CANONICALOBJDIR} != ${.CURDIR} && exists(${CANONICALOBJDIR}/)
13716663Sjkhcleanobj:
138274186Simp	@-rm -rf ${CANONICALOBJDIR}
13976021Sru.else
140124061Sgreencleanobj: clean cleandepend
14176021Sru.endif
142102660Skris	@if [ -L ${.CURDIR}/obj ]; then rm -f ${.CURDIR}/obj; fi
14315166Swosch
144241298Smarcel# Tell bmake not to look for generated files via .PATH
145268970SsjgNOPATH_FILES+=	${CLEANFILES}
146268970Ssjg.if !empty(NOPATH_FILES)
147268970Ssjg.NOPATH: ${NOPATH_FILES}
148241298Smarcel.endif
149241298Smarcel
15031859Sbde.if !target(clean)
15195306Sruclean:
15231859Sbde.if defined(CLEANFILES) && !empty(CLEANFILES)
153139761Skrion	rm -f ${CLEANFILES}
15416663Sjkh.endif
15533815Sbde.if defined(CLEANDIRS) && !empty(CLEANDIRS)
156274186Simp	-rm -rf ${CLEANDIRS}
15715166Swosch.endif
15833815Sbde.endif
15915166Swosch
16095306Srucleandir: cleanobj
16135789Sbde
16295306Sru.include <bsd.subdir.mk>
16335789Sbde
164284345Ssjg.if make(destroy*) && defined(OBJROOT)
165284345Ssjg# this (rm -rf objdir) is much faster and more reliable than cleaning.
166284345Ssjg
167284345Ssjg# just in case we are playing games with these...
168284345Ssjg_OBJDIR?= ${.OBJDIR}
169284345Ssjg_CURDIR?= ${.CURDIR}
170284345Ssjg
171284345Ssjg# destroy almost everything
172284345Ssjgdestroy: destroy-all
173284345Ssjgdestroy-all:
174284345Ssjg
175284345Ssjg# just remove our objdir
176284345Ssjgdestroy-arch: .NOMETA
177284345Ssjg.if ${_OBJDIR} != ${_CURDIR}
178284345Ssjg	cd ${_CURDIR} && rm -rf ${_OBJDIR}
179284345Ssjg.endif
180284345Ssjg
181284345Ssjg.if defined(HOST_OBJTOP)
182284345Ssjgdestroy-host: destroy.host
183284345Ssjgdestroy.host: .NOMETA
184284345Ssjg	cd ${_CURDIR} && rm -rf ${HOST_OBJTOP}/${RELDIR:N.}
185284345Ssjg.endif
186284345Ssjg
187284345Ssjg.if make(destroy-all) && ${RELDIR} == "."
188284345Ssjgdestroy-all: destroy-stage
189284345Ssjg.endif
190284345Ssjg
191284345Ssjg# remove the stage tree
192284345Ssjgdestroy-stage: .NOMETA
193284345Ssjg.if defined(STAGE_ROOT)
194284345Ssjg	cd ${_CURDIR} && rm -rf ${STAGE_ROOT}
195284345Ssjg.endif
196284345Ssjg
197284345Ssjg# allow parallel destruction
198284345Ssjg_destroy_machine_list = common host ${ALL_MACHINE_LIST}
199284345Ssjg.for m in ${_destroy_machine_list:O:u}
200284345Ssjgdestroy-all: destroy.$m
201284345Ssjg.if !target(destroy.$m)
202284345Ssjgdestroy.$m: .NOMETA
203284345Ssjg.if ${_OBJDIR} != ${_CURDIR}
204284345Ssjg	cd ${_CURDIR} && rm -rf ${OBJROOT}$m*/${RELDIR:N.}
205284345Ssjg.endif
206284345Ssjg.endif
207284345Ssjg.endfor
208284345Ssjg
209284345Ssjg.endif
210284345Ssjg
211144893Sharti.endif # !target(__<bsd.obj.mk>__)
212