xref: /freebsd/share/mk/bsd.obj.mk (revision ef36b3f75658d201edb495068db5e1be49593de5)
1# $FreeBSD$
2#
3# The include file <bsd.obj.mk> handles creating the 'obj' directory
4# and cleaning up object files, etc.
5#
6# +++ variables +++
7#
8# CLEANDIRS	Additional directories to remove for the clean target.
9#
10# CLEANFILES	Additional files to remove for the clean target.
11#
12# MAKEOBJDIR 	A pathname for the directory where the targets
13#		are built.  Note: MAKEOBJDIR is an *environment* variable
14#		and works properly only if set as an environment variable,
15#		not as a global or command line variable!
16#
17#		E.g. use `env MAKEOBJDIR=temp-obj make'
18#
19# MAKEOBJDIRPREFIX  Specifies somewhere other than /usr/obj to root the object
20#		tree.  Note: MAKEOBJDIRPREFIX is an *environment* variable
21#		and works properly only if set as an environment variable,
22#		not as a global or command line variable!
23#
24#		E.g. use `env MAKEOBJDIRPREFIX=/somewhere/obj make'
25#
26# NO_OBJ	Do not create object directories.  This should not be set
27#		if anything is built.
28#
29# +++ targets +++
30#
31#	clean:
32#		remove ${CLEANFILES}; remove ${CLEANDIRS} and all contents.
33#
34#	cleandir:
35#		remove the build directory (and all its contents) created by obj
36#
37#	obj:
38#		create build directory.
39#
40
41.if !target(__<bsd.obj.mk>__)
42__<bsd.obj.mk>__:
43.include <bsd.own.mk>
44
45.if ${MK_AUTO_OBJ} == "yes"
46# it is done by now
47objwarn:
48obj:
49CANONICALOBJDIR= ${.OBJDIR}
50.if defined(NO_OBJ)
51# but this makefile does not want it!
52.OBJDIR: ${.CURDIR}
53.endif
54# Handle special case where SRCS is full-pathed and requires
55# nested objdirs.  This duplicates some auto.obj.mk logic.
56.if (!empty(SRCS:M*/*) || !empty(DPSRCS:M*/*)) && \
57    (${.TARGETS} == "" || ${.TARGETS:Nclean*:N*clean:Ndestroy*} != "")
58_wantdirs=	${SRCS:M*/*:H} ${DPSRCS:M*/*:H}
59.if !empty(_wantdirs)
60_wantdirs:=	${_wantdirs:O:u}
61_needdirs=
62.for _dir in ${_wantdirs}
63.if !exists(${.OBJDIR}/${_dir}/)
64_needdirs+=	${_dir}
65.endif
66.endfor
67.endif
68.if !empty(_needdirs)
69#_mkneededdirs!=	umask ${OBJDIR_UMASK:U002}; ${Mkdirs} ${_needdirs}
70__objdir_made != umask ${OBJDIR_UMASK:U002}; ${Mkdirs}; \
71	for dir in ${_needdirs}; do \
72	  dir=${.OBJDIR}/$${dir}; \
73	  ${ECHO_TRACE} "[Creating nested objdir $${dir}...]" >&2; \
74          Mkdirs $${dir}; \
75	done
76.endif
77.endif	# !empty(SRCS:M*/*) || !empty(DPSRCS:M*/*)
78.elif defined(MAKEOBJDIRPREFIX)
79CANONICALOBJDIR:=${MAKEOBJDIRPREFIX}${.CURDIR}
80.elif defined(MAKEOBJDIR) && ${MAKEOBJDIR:M/*} != ""
81CANONICALOBJDIR:=${MAKEOBJDIR}
82OBJTOP?= ${MAKEOBJDIR}
83.else
84CANONICALOBJDIR:=/usr/obj${.CURDIR}
85.endif
86
87OBJTOP?= ${.OBJDIR:S,${.CURDIR},,}${SRCTOP}
88
89#
90# Warn of unorthodox object directory.
91#
92# The following directories are tried in order for ${.OBJDIR}:
93#
94# 1.  ${MAKEOBJDIRPREFIX}/`pwd`
95# 2.  ${MAKEOBJDIR}
96# 3.  obj.${MACHINE}
97# 4.  obj
98# 5.  /usr/obj/`pwd`
99# 6.  ${.CURDIR}
100#
101# If ${.OBJDIR} is constructed using canonical cases 1 or 5, or
102# case 2 (using MAKEOBJDIR), don't issue a warning.  Otherwise,
103# issue a warning differentiating between cases 6 and (3 or 4).
104#
105objwarn: .PHONY
106.if !defined(NO_OBJ) && ${.OBJDIR} != ${CANONICALOBJDIR} && \
107    !(defined(MAKEOBJDIRPREFIX) && exists(${CANONICALOBJDIR}/)) && \
108    !(defined(MAKEOBJDIR) && exists(${MAKEOBJDIR}/))
109.if ${.OBJDIR} == ${.CURDIR}
110	@${ECHO} "Warning: Object directory not changed from original ${.CURDIR}"
111.elif exists(${.CURDIR}/obj.${MACHINE}/) || exists(${.CURDIR}/obj/)
112	@${ECHO} "Warning: Using ${.OBJDIR} as object directory instead of\
113		canonical ${CANONICALOBJDIR}"
114.endif
115.endif
116beforebuild: objwarn
117
118.if !defined(NO_OBJ)
119.if !target(obj)
120obj: .PHONY
121	@if ! test -d ${CANONICALOBJDIR}/; then \
122		mkdir -p ${CANONICALOBJDIR}; \
123		if ! test -d ${CANONICALOBJDIR}/; then \
124			${ECHO} "Unable to create ${CANONICALOBJDIR}."; \
125			exit 1; \
126		fi; \
127		${ECHO} "${CANONICALOBJDIR} created for ${.CURDIR}"; \
128	fi
129.for dir in ${SRCS:H:O:u} ${DPSRCS:H:O:u}
130	@if ! test -d ${CANONICALOBJDIR}/${dir}/; then \
131		mkdir -p ${CANONICALOBJDIR}/${dir}; \
132		if ! test -d ${CANONICALOBJDIR}/${dir}/; then \
133			${ECHO} "Unable to create ${CANONICALOBJDIR}/${dir}."; \
134			exit 1; \
135		fi; \
136		${ECHO} "${CANONICALOBJDIR}/${dir} created for ${.CURDIR}"; \
137	fi
138.endfor
139.endif
140
141.if !target(objlink)
142objlink:
143	@if test -d ${CANONICALOBJDIR}/; then \
144		rm -f ${.CURDIR}/obj; \
145		ln -s ${CANONICALOBJDIR} ${.CURDIR}/obj; \
146	else \
147		echo "No ${CANONICALOBJDIR} to link to - do a make obj."; \
148	fi
149.endif
150.endif # !defined(NO_OBJ)
151
152#
153# where would that obj directory be?
154#
155.if !target(whereobj)
156whereobj:
157	@echo ${.OBJDIR}
158.endif
159
160# Same check in bsd.progs.mk
161.if ${CANONICALOBJDIR} != ${.CURDIR} && exists(${CANONICALOBJDIR}/)
162cleanobj:
163	-rm -rf ${CANONICALOBJDIR}
164.else
165cleanobj: clean cleandepend
166.endif
167	@if [ -L ${.CURDIR}/obj ]; then rm -f ${.CURDIR}/obj; fi
168
169# Tell bmake not to look for generated files via .PATH
170NOPATH_FILES+=	${CLEANFILES}
171.if !empty(NOPATH_FILES)
172.NOPATH: ${NOPATH_FILES}
173.endif
174
175.if !target(clean)
176clean:
177.if defined(CLEANFILES) && !empty(CLEANFILES)
178	rm -f ${CLEANFILES}
179.endif
180.if defined(CLEANDIRS) && !empty(CLEANDIRS)
181	-rm -rf ${CLEANDIRS}
182.endif
183.endif
184.ORDER: clean all
185
186.include <bsd.subdir.mk>
187
188cleandir: .WAIT cleanobj
189
190.if make(destroy*) && defined(OBJROOT)
191# this (rm -rf objdir) is much faster and more reliable than cleaning.
192
193# just in case we are playing games with these...
194_OBJDIR?= ${.OBJDIR}
195_CURDIR?= ${.CURDIR}
196
197# destroy almost everything
198destroy: destroy-all
199destroy-all:
200
201# just remove our objdir
202destroy-arch: .NOMETA
203.if ${_OBJDIR} != ${_CURDIR}
204	cd ${_CURDIR} && rm -rf ${_OBJDIR}
205.endif
206
207.if defined(HOST_OBJTOP)
208destroy-host: destroy.host
209destroy.host: .NOMETA
210	cd ${_CURDIR} && rm -rf ${HOST_OBJTOP}/${RELDIR:N.}
211.endif
212
213.if make(destroy-all) && ${RELDIR} == "."
214destroy-all: destroy-stage
215.endif
216
217# remove the stage tree
218destroy-stage: .NOMETA
219.if defined(STAGE_ROOT)
220	cd ${_CURDIR} && rm -rf ${STAGE_ROOT}
221.endif
222
223# allow parallel destruction
224_destroy_machine_list = common host ${ALL_MACHINE_LIST}
225.for m in ${_destroy_machine_list:O:u}
226destroy-all: destroy.$m
227.if !target(destroy.$m)
228destroy.$m: .NOMETA
229.if ${_OBJDIR} != ${_CURDIR}
230	cd ${_CURDIR} && rm -rf ${OBJROOT}$m*/${RELDIR:N.}
231.endif
232.endif
233.endfor
234
235.endif
236
237.endif # !target(__<bsd.obj.mk>__)
238