xref: /freebsd/share/mk/bsd.obj.mk (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
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
92
93.if !defined(NO_OBJ)
94.if !target(obj)
95obj: .PHONY
96	@if ! test -d ${CANONICALOBJDIR}/; then \
97		mkdir -p ${CANONICALOBJDIR}; \
98		if ! test -d ${CANONICALOBJDIR}/; then \
99			${ECHO} "Unable to create ${CANONICALOBJDIR}."; \
100			exit 1; \
101		fi; \
102		${ECHO} "${CANONICALOBJDIR} created for ${.CURDIR}"; \
103	fi
104.for dir in ${SRCS:H:O:u}
105	@if ! test -d ${CANONICALOBJDIR}/${dir}/; then \
106		mkdir -p ${CANONICALOBJDIR}/${dir}; \
107		if ! test -d ${CANONICALOBJDIR}/${dir}/; then \
108			${ECHO} "Unable to create ${CANONICALOBJDIR}/${dir}."; \
109			exit 1; \
110		fi; \
111		${ECHO} "${CANONICALOBJDIR}/${dir} created for ${.CURDIR}"; \
112	fi
113.endfor
114.endif
115
116.if !target(objlink)
117objlink:
118	@if test -d ${CANONICALOBJDIR}/; then \
119		rm -f ${.CURDIR}/obj; \
120		ln -s ${CANONICALOBJDIR} ${.CURDIR}/obj; \
121	else \
122		echo "No ${CANONICALOBJDIR} to link to - do a make obj."; \
123	fi
124.endif
125.endif # !defined(NO_OBJ)
126
127#
128# where would that obj directory be?
129#
130.if !target(whereobj)
131whereobj:
132	@echo ${.OBJDIR}
133.endif
134
135.if ${CANONICALOBJDIR} != ${.CURDIR} && exists(${CANONICALOBJDIR}/)
136cleanobj:
137	@-rm -rf ${CANONICALOBJDIR}
138.else
139cleanobj: clean cleandepend
140.endif
141	@if [ -L ${.CURDIR}/obj ]; then rm -f ${.CURDIR}/obj; fi
142
143# Tell bmake not to look for generated files via .PATH
144NOPATH_FILES+=	${CLEANFILES}
145.if !empty(NOPATH_FILES)
146.NOPATH: ${NOPATH_FILES}
147.endif
148
149.if !target(clean)
150clean:
151.if defined(CLEANFILES) && !empty(CLEANFILES)
152	rm -f ${CLEANFILES}
153.endif
154.if defined(CLEANDIRS) && !empty(CLEANDIRS)
155	-rm -rf ${CLEANDIRS}
156.endif
157.endif
158
159cleandir: cleanobj
160
161.include <bsd.subdir.mk>
162
163.if make(destroy*) && defined(OBJROOT)
164# this (rm -rf objdir) is much faster and more reliable than cleaning.
165
166# just in case we are playing games with these...
167_OBJDIR?= ${.OBJDIR}
168_CURDIR?= ${.CURDIR}
169
170# destroy almost everything
171destroy: destroy-all
172destroy-all:
173
174# just remove our objdir
175destroy-arch: .NOMETA
176.if ${_OBJDIR} != ${_CURDIR}
177	cd ${_CURDIR} && rm -rf ${_OBJDIR}
178.endif
179
180.if defined(HOST_OBJTOP)
181destroy-host: destroy.host
182destroy.host: .NOMETA
183	cd ${_CURDIR} && rm -rf ${HOST_OBJTOP}/${RELDIR:N.}
184.endif
185
186.if make(destroy-all) && ${RELDIR} == "."
187destroy-all: destroy-stage
188.endif
189
190# remove the stage tree
191destroy-stage: .NOMETA
192.if defined(STAGE_ROOT)
193	cd ${_CURDIR} && rm -rf ${STAGE_ROOT}
194.endif
195
196# allow parallel destruction
197_destroy_machine_list = common host ${ALL_MACHINE_LIST}
198.for m in ${_destroy_machine_list:O:u}
199destroy-all: destroy.$m
200.if !target(destroy.$m)
201destroy.$m: .NOMETA
202.if ${_OBJDIR} != ${_CURDIR}
203	cd ${_CURDIR} && rm -rf ${OBJROOT}$m*/${RELDIR:N.}
204.endif
205.endif
206.endfor
207
208.endif
209
210.endif # !target(__<bsd.obj.mk>__)
211