xref: /freebsd/share/mk/bsd.obj.mk (revision 42c159fe388a3765f69860c84183700af37aca8a)
1# $FreeBSD$
2#
3# The include file <bsd.obj.mk> handles creating the 'obj' directory
4# and cleaning up object files, etc.
5#
6# Under construction: it also contains the _SUBDIR target (which is used
7# by most `mk' files to recurse into subdirectories) and defaults for the
8# cleandepend, depend and tags targets.  It may eventually be merged with
9# with bsd.subdir.mk.
10#
11# +++ variables +++
12#
13# CLEANDIRS	Additional directories to remove for the clean target.
14#
15# CLEANFILES	Additional files to remove for the clean target.
16#
17# MAKEOBJDIR 	A pathname for the directory where the targets
18#		are built.  Note: MAKEOBJDIR is an *enviroment* variable
19#		and works properly only if set as an enviroment variable,
20#		not as a global or command line variable!
21#
22#		E.g. use `env MAKEOBJDIR=temp-obj make'
23#
24# MAKEOBJDIRPREFIX  Specifies somewhere other than /usr/obj to root the object
25#		tree.  Note: MAKEOBJDIRPREFIX is an *enviroment* variable
26#		and works properly only if set as an enviroment variable,
27#		not as a global or command line variable!
28#
29#		E.g. use `env MAKEOBJDIRPREFIX=/somewhere/obj make'
30#
31# NOOBJ		Do not create object directories.  This should not be set
32#		if anything is built.
33#
34# +++ targets +++
35#
36#	clean:
37#		remove ${CLEANFILES}; remove ${CLEANDIRS} and all contents.
38#
39#	cleandir:
40#		remove the build directory (and all its contents) created by obj
41#
42#	obj:
43#		create build directory.
44#
45
46.if defined(MAKEOBJDIRPREFIX)
47CANONICALOBJDIR:=${MAKEOBJDIRPREFIX}${.CURDIR}
48.else
49CANONICALOBJDIR:=/usr/obj${.CURDIR}
50.endif
51
52#
53# Warn of unorthodox object directory.
54#
55# The following directories are tried in order for ${.OBJDIR}:
56#
57# 1.  ${MAKEOBJDIRPREFIX}/`pwd`
58# 2.  ${MAKEOBJDIR}
59# 3.  obj.${MACHINE}
60# 4.  obj
61# 5.  /usr/obj/`pwd`
62# 6.  ${.CURDIR}
63#
64# If ${.OBJDIR} is constructed using canonical cases 1 or 5, or
65# case 2 (using MAKEOBJDIR), don't issue a warning.  Otherwise,
66# issue a warning differentiating between cases 6 and (3 or 4).
67#
68objwarn:
69.if !defined(NOOBJ) && ${.OBJDIR} != ${CANONICALOBJDIR} && \
70    !(defined(MAKEOBJDIRPREFIX) && exists(${CANONICALOBJDIR}/)) && \
71    !(defined(MAKEOBJDIR) && exists(${MAKEOBJDIR}/))
72.if ${.OBJDIR} == ${.CURDIR}
73	@${ECHO} "Warning: Object directory not changed from original ${.CURDIR}"
74.elif exists(${.CURDIR}/obj.${MACHINE}/) || exists(${.CURDIR}/obj/)
75	@${ECHO} "Warning: Using ${.OBJDIR} as object directory instead of\
76		canonical ${CANONICALOBJDIR}"
77.endif
78.endif
79
80.if !target(obj)
81.if defined(NOOBJ)
82obj:
83.else
84obj:	_SUBDIR
85	@if ! test -d ${CANONICALOBJDIR}/; then \
86		mkdir -p ${CANONICALOBJDIR}; \
87		if ! test -d ${CANONICALOBJDIR}/; then \
88			${ECHO} "Unable to create ${CANONICALOBJDIR}."; \
89			exit 1; \
90		fi; \
91		${ECHO} "${CANONICALOBJDIR} created for ${.CURDIR}"; \
92	fi
93.endif
94.endif
95
96.if !target(objlink)
97objlink: _SUBDIR
98	@if test -d ${CANONICALOBJDIR}/; then \
99		rm -f ${.CURDIR}/obj; \
100		ln -s ${CANONICALOBJDIR} ${.CURDIR}/obj; \
101	else \
102		echo "No ${CANONICALOBJDIR} to link to - do a make obj."; \
103	fi
104.endif
105
106#
107# where would that obj directory be?
108#
109.if !target(whereobj)
110whereobj:
111	@echo ${.OBJDIR}
112.endif
113
114cleanobj:
115.if ${CANONICALOBJDIR} != ${.CURDIR} && exists(${CANONICALOBJDIR}/)
116	@rm -rf ${CANONICALOBJDIR}
117.else
118	@cd ${.CURDIR} && ${MAKE} clean cleandepend
119.endif
120	@if [ -h ${.CURDIR}/obj ]; then rm -f ${.CURDIR}/obj; fi
121
122.if !target(clean)
123clean: _SUBDIR
124.if defined(CLEANFILES) && !empty(CLEANFILES)
125	rm -f ${CLEANFILES}
126.endif
127.if defined(CLEANDIRS) && !empty(CLEANDIRS)
128	rm -rf ${CLEANDIRS}
129.endif
130.endif
131
132.if !target(checkdpadd)
133checkdpadd: _SUBDIR
134.if (defined(DPADD) || defined(LDADD))
135checkdpadd:
136.if ${OBJFORMAT} != aout
137	@ldadd=`echo \`for lib in ${DPADD} ; do \
138		echo $$lib | sed 's;^/usr/lib/lib\(.*\)\.a;-l\1;' ; \
139	done \`` ; \
140	ldadd1=`echo ${LDADD}` ; \
141	if [ "$$ldadd" != "$$ldadd1" ] ; then \
142		echo ${.CURDIR} ; \
143		echo "DPADD -> $$ldadd" ; \
144		echo "LDADD -> $$ldadd1" ; \
145	fi
146.else
147	@dpadd=`echo \`ld -Bstatic -f ${LDDESTDIR} ${LDADD}\`` ; \
148	if [ "$$dpadd" != "${DPADD}" ] ; then \
149		echo ${.CURDIR} ; \
150		echo "LDADD -> $$dpadd" ; \
151		echo "DPADD =  ${DPADD}" ; \
152	fi
153.endif
154.endif
155.endif
156
157cleandir: cleanobj _SUBDIR
158
159.for __target in cleandepend depend tags
160.if !target(${__target})
161${__target}: _SUBDIR
162.endif
163.endfor
164
165_SUBDIR: .USE
166.if defined(SUBDIR) && !empty(SUBDIR)
167	@for entry in ${SUBDIR}; do \
168		(${ECHODIR} "===> ${DIRPRFX}$$entry"; \
169		if test -d ${.CURDIR}/$${entry}.${MACHINE_ARCH}; then \
170			cd ${.CURDIR}/$${entry}.${MACHINE_ARCH}; \
171		else \
172			cd ${.CURDIR}/$${entry}; \
173		fi; \
174		${MAKE} ${.TARGET:S/realinstall/install/} DIRPRFX=${DIRPRFX}$$entry/); \
175	done
176.endif
177