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