xref: /freebsd/share/mk/bsd.obj.mk (revision 7750ad47a9a7dbc83f87158464170c8640723293)
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 defined(MAKEOBJDIRPREFIX)
46CANONICALOBJDIR:=${MAKEOBJDIRPREFIX}${.CURDIR}
47.elif defined(MAKEOBJDIR) && ${MAKEOBJDIR:M/*} != ""
48CANONICALOBJDIR:=${MAKEOBJDIR}
49.else
50CANONICALOBJDIR:=/usr/obj${.CURDIR}
51.endif
52
53.if defined(.PARSEDIR) && !defined(NO_OBJ) && !defined(NO_AUTO_OBJ)
54.if ${MK_AUTO_OBJ} == "yes"
55__objdir?= ${CANONICALOBJDIR}
56# this is what auto.obj.mk wants to see
57MKOBJDIRS=auto
58.include "auto.obj.mk"
59.endif
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.endif
102
103.if !target(objlink)
104objlink:
105	@if test -d ${CANONICALOBJDIR}/; then \
106		rm -f ${.CURDIR}/obj; \
107		ln -s ${CANONICALOBJDIR} ${.CURDIR}/obj; \
108	else \
109		echo "No ${CANONICALOBJDIR} to link to - do a make obj."; \
110	fi
111.endif
112.endif # !defined(NO_OBJ)
113
114#
115# where would that obj directory be?
116#
117.if !target(whereobj)
118whereobj:
119	@echo ${.OBJDIR}
120.endif
121
122.if ${CANONICALOBJDIR} != ${.CURDIR} && exists(${CANONICALOBJDIR}/)
123cleanobj:
124	@rm -rf ${CANONICALOBJDIR}
125.else
126cleanobj: clean cleandepend
127.endif
128	@if [ -L ${.CURDIR}/obj ]; then rm -f ${.CURDIR}/obj; fi
129
130# Tell bmake not to look for generated files via .PATH
131.if !empty(CLEANFILES)
132.NOPATH: ${CLEANFILES}
133.endif
134
135.if !target(clean)
136clean:
137.if defined(CLEANFILES) && !empty(CLEANFILES)
138	rm -f ${CLEANFILES}
139.endif
140.if defined(CLEANDIRS) && !empty(CLEANDIRS)
141	rm -rf ${CLEANDIRS}
142.endif
143.endif
144
145cleandir: cleanobj
146
147.include <bsd.subdir.mk>
148
149.endif # !target(__<bsd.obj.mk>__)
150