xref: /freebsd/share/mk/bsd.obj.mk (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
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.elif defined(MAKEOBJDIRPREFIX)
55CANONICALOBJDIR:=${MAKEOBJDIRPREFIX}${.CURDIR}
56.elif defined(MAKEOBJDIR) && ${MAKEOBJDIR:M/*} != ""
57CANONICALOBJDIR:=${MAKEOBJDIR}
58.else
59CANONICALOBJDIR:=/usr/obj${.CURDIR}
60.endif
61
62#
63# Warn of unorthodox object directory.
64#
65# The following directories are tried in order for ${.OBJDIR}:
66#
67# 1.  ${MAKEOBJDIRPREFIX}/`pwd`
68# 2.  ${MAKEOBJDIR}
69# 3.  obj.${MACHINE}
70# 4.  obj
71# 5.  /usr/obj/`pwd`
72# 6.  ${.CURDIR}
73#
74# If ${.OBJDIR} is constructed using canonical cases 1 or 5, or
75# case 2 (using MAKEOBJDIR), don't issue a warning.  Otherwise,
76# issue a warning differentiating between cases 6 and (3 or 4).
77#
78objwarn:
79.if !defined(NO_OBJ) && ${.OBJDIR} != ${CANONICALOBJDIR} && \
80    !(defined(MAKEOBJDIRPREFIX) && exists(${CANONICALOBJDIR}/)) && \
81    !(defined(MAKEOBJDIR) && exists(${MAKEOBJDIR}/))
82.if ${.OBJDIR} == ${.CURDIR}
83	@${ECHO} "Warning: Object directory not changed from original ${.CURDIR}"
84.elif exists(${.CURDIR}/obj.${MACHINE}/) || exists(${.CURDIR}/obj/)
85	@${ECHO} "Warning: Using ${.OBJDIR} as object directory instead of\
86		canonical ${CANONICALOBJDIR}"
87.endif
88.endif
89
90.if !defined(NO_OBJ)
91.if !target(obj)
92obj: .PHONY
93	@if ! test -d ${CANONICALOBJDIR}/; then \
94		mkdir -p ${CANONICALOBJDIR}; \
95		if ! test -d ${CANONICALOBJDIR}/; then \
96			${ECHO} "Unable to create ${CANONICALOBJDIR}."; \
97			exit 1; \
98		fi; \
99		${ECHO} "${CANONICALOBJDIR} created for ${.CURDIR}"; \
100	fi
101.for dir in ${SRCS:H:O:u}
102	@if ! test -d ${CANONICALOBJDIR}/${dir}/; then \
103		mkdir -p ${CANONICALOBJDIR}/${dir}; \
104		if ! test -d ${CANONICALOBJDIR}/${dir}/; then \
105			${ECHO} "Unable to create ${CANONICALOBJDIR}/${dir}."; \
106			exit 1; \
107		fi; \
108		${ECHO} "${CANONICALOBJDIR}/${dir} created for ${.CURDIR}"; \
109	fi
110.endfor
111.endif
112
113.if !target(objlink)
114objlink:
115	@if test -d ${CANONICALOBJDIR}/; then \
116		rm -f ${.CURDIR}/obj; \
117		ln -s ${CANONICALOBJDIR} ${.CURDIR}/obj; \
118	else \
119		echo "No ${CANONICALOBJDIR} to link to - do a make obj."; \
120	fi
121.endif
122.endif # !defined(NO_OBJ)
123
124#
125# where would that obj directory be?
126#
127.if !target(whereobj)
128whereobj:
129	@echo ${.OBJDIR}
130.endif
131
132.if ${CANONICALOBJDIR} != ${.CURDIR} && exists(${CANONICALOBJDIR}/)
133cleanobj:
134	@-rm -rf ${CANONICALOBJDIR}
135.else
136cleanobj: clean cleandepend
137.endif
138	@if [ -L ${.CURDIR}/obj ]; then rm -f ${.CURDIR}/obj; fi
139
140# Tell bmake not to look for generated files via .PATH
141NOPATH_FILES+=	${CLEANFILES}
142.if !empty(NOPATH_FILES)
143.NOPATH: ${NOPATH_FILES}
144.endif
145
146.if !target(clean)
147clean:
148.if defined(CLEANFILES) && !empty(CLEANFILES)
149	rm -f ${CLEANFILES}
150.endif
151.if defined(CLEANDIRS) && !empty(CLEANDIRS)
152	-rm -rf ${CLEANDIRS}
153.endif
154.endif
155
156cleandir: cleanobj
157
158.include <bsd.subdir.mk>
159
160.if make(destroy*) && defined(OBJROOT)
161# this (rm -rf objdir) is much faster and more reliable than cleaning.
162
163# just in case we are playing games with these...
164_OBJDIR?= ${.OBJDIR}
165_CURDIR?= ${.CURDIR}
166
167# destroy almost everything
168destroy: destroy-all
169destroy-all:
170
171# just remove our objdir
172destroy-arch: .NOMETA
173.if ${_OBJDIR} != ${_CURDIR}
174	cd ${_CURDIR} && rm -rf ${_OBJDIR}
175.endif
176
177.if defined(HOST_OBJTOP)
178destroy-host: destroy.host
179destroy.host: .NOMETA
180	cd ${_CURDIR} && rm -rf ${HOST_OBJTOP}/${RELDIR:N.}
181.endif
182
183.if make(destroy-all) && ${RELDIR} == "."
184destroy-all: destroy-stage
185.endif
186
187# remove the stage tree
188destroy-stage: .NOMETA
189.if defined(STAGE_ROOT)
190	cd ${_CURDIR} && rm -rf ${STAGE_ROOT}
191.endif
192
193# allow parallel destruction
194_destroy_machine_list = common host ${ALL_MACHINE_LIST}
195.for m in ${_destroy_machine_list:O:u}
196destroy-all: destroy.$m
197.if !target(destroy.$m)
198destroy.$m: .NOMETA
199.if ${_OBJDIR} != ${_CURDIR}
200	cd ${_CURDIR} && rm -rf ${OBJROOT}$m*/${RELDIR:N.}
201.endif
202.endif
203.endfor
204
205.endif
206
207.endif # !target(__<bsd.obj.mk>__)
208