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}
49OBJTOP?= ${MAKEOBJDIR}
50.else
51CANONICALOBJDIR:=/usr/obj${.CURDIR}
52.endif
53
54OBJTOP?= ${.OBJDIR:S,${.CURDIR},,}${SRCTOP}
55
56#
57# Warn of unorthodox object directory.
58#
59# The following directories are tried in order for ${.OBJDIR}:
60#
61# 1.  ${MAKEOBJDIRPREFIX}/`pwd`
62# 2.  ${MAKEOBJDIR}
63# 3.  obj.${MACHINE}
64# 4.  obj
65# 5.  /usr/obj/`pwd`
66# 6.  ${.CURDIR}
67#
68# If ${.OBJDIR} is constructed using canonical cases 1 or 5, or
69# case 2 (using MAKEOBJDIR), don't issue a warning.  Otherwise,
70# issue a warning differentiating between cases 6 and (3 or 4).
71#
72objwarn:
73.if !defined(NO_OBJ) && ${.OBJDIR} != ${CANONICALOBJDIR} && \
74    !(defined(MAKEOBJDIRPREFIX) && exists(${CANONICALOBJDIR}/)) && \
75    !(defined(MAKEOBJDIR) && exists(${MAKEOBJDIR}/))
76.if ${.OBJDIR} == ${.CURDIR}
77	@${ECHO} "Warning: Object directory not changed from original ${.CURDIR}"
78.elif exists(${.CURDIR}/obj.${MACHINE}/) || exists(${.CURDIR}/obj/)
79	@${ECHO} "Warning: Using ${.OBJDIR} as object directory instead of\
80		canonical ${CANONICALOBJDIR}"
81.endif
82.endif
83
84.if !defined(NO_OBJ)
85.if !target(obj)
86obj: .PHONY
87	@if ! test -d ${CANONICALOBJDIR}/; then \
88		mkdir -p ${CANONICALOBJDIR}; \
89		if ! test -d ${CANONICALOBJDIR}/; then \
90			${ECHO} "Unable to create ${CANONICALOBJDIR}."; \
91			exit 1; \
92		fi; \
93		${ECHO} "${CANONICALOBJDIR} created for ${.CURDIR}"; \
94	fi
95.for dir in ${SRCS:H:O:u}
96	@if ! test -d ${CANONICALOBJDIR}/${dir}/; then \
97		mkdir -p ${CANONICALOBJDIR}/${dir}; \
98		if ! test -d ${CANONICALOBJDIR}/${dir}/; then \
99			${ECHO} "Unable to create ${CANONICALOBJDIR}/${dir}."; \
100			exit 1; \
101		fi; \
102		${ECHO} "${CANONICALOBJDIR}/${dir} created for ${.CURDIR}"; \
103	fi
104.endfor
105.endif
106
107.if !target(objlink)
108objlink:
109	@if test -d ${CANONICALOBJDIR}/; then \
110		rm -f ${.CURDIR}/obj; \
111		ln -s ${CANONICALOBJDIR} ${.CURDIR}/obj; \
112	else \
113		echo "No ${CANONICALOBJDIR} to link to - do a make obj."; \
114	fi
115.endif
116.endif # !defined(NO_OBJ)
117
118#
119# where would that obj directory be?
120#
121.if !target(whereobj)
122whereobj:
123	@echo ${.OBJDIR}
124.endif
125
126.if ${CANONICALOBJDIR} != ${.CURDIR} && exists(${CANONICALOBJDIR}/)
127cleanobj:
128	@rm -rf ${CANONICALOBJDIR}
129.else
130cleanobj: clean cleandepend
131.endif
132	@if [ -L ${.CURDIR}/obj ]; then rm -f ${.CURDIR}/obj; fi
133
134# Tell bmake not to look for generated files via .PATH
135.if !empty(CLEANFILES)
136.NOPATH: ${CLEANFILES}
137.endif
138
139.if !target(clean)
140clean:
141.if defined(CLEANFILES) && !empty(CLEANFILES)
142	rm -f ${CLEANFILES}
143.endif
144.if defined(CLEANDIRS) && !empty(CLEANDIRS)
145	rm -rf ${CLEANDIRS}
146.endif
147.endif
148
149cleandir: cleanobj
150
151.include <bsd.subdir.mk>
152
153.endif # !target(__<bsd.obj.mk>__)
154