xref: /freebsd/share/mk/bsd.obj.mk (revision 380a989b3223d455375b4fae70fd0b9bdd43bafb)
1#	$Id: bsd.obj.mk,v 1.25 1998/06/04 15:31:55 peter Exp $
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# OBJLINK	Create a symbolic link from ${.CURDIR}/obj to
35#		${CANONICALOBJDIR}.  Note: this BREAKS the read-only source
36#		tree rule!
37#
38# +++ targets +++
39#
40#	clean:
41#		remove ${CLEANFILES}; remove ${CLEANDIRS} and all contents.
42#
43#	cleandir:
44#		remove the build directory (and all its contents) created by obj
45#
46#	obj:
47#		create build directory.
48#
49
50.if defined(MAKEOBJDIRPREFIX)
51CANONICALOBJDIR:=${MAKEOBJDIRPREFIX}${.CURDIR}
52.else
53CANONICALOBJDIR:=/usr/obj${.CURDIR}
54.endif
55
56#
57# Warn of unorthodox object directory
58#
59objwarn:
60.if !defined(NOOBJ) && ${.OBJDIR} != ${CANONICALOBJDIR}
61.if ${.OBJDIR} == ${.CURDIR}
62	@${ECHO} "Warning: Object directory not changed from original ${.CURDIR}"
63.elif !defined(MAKEOBJDIR) && !defined(MAKEOBJDIRPREFIX) && !defined(OBJLINK)
64	@${ECHO} "Warning: Using ${.OBJDIR} as object directory instead of\
65		canonical ${CANONICALOBJDIR}"
66.endif
67.endif
68
69.if !target(obj)
70.if defined(NOOBJ)
71obj:
72.else
73.if !defined(OBJLINK)
74obj:	_SUBDIR
75	@if ! test -d ${CANONICALOBJDIR}/; then \
76		mkdir -p ${CANONICALOBJDIR}; \
77		if ! test -d ${CANONICALOBJDIR}/; then \
78			${ECHO} "Unable to create ${CANONICALOBJDIR}."; \
79			exit 1; \
80		fi; \
81		${ECHO} "${CANONICALOBJDIR} created for ${.CURDIR}"; \
82	fi
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		rm -f ${.CURDIR}/obj; \
92		ln -s ${CANONICALOBJDIR} ${.CURDIR}/obj; \
93		${ECHO} "${.CURDIR} -> ${CANONICALOBJDIR}"; \
94	fi
95.endif
96.endif
97.endif
98
99.if !target(objlink)
100objlink: _SUBDIR
101	@if test -d ${CANONICALOBJDIR}/; then \
102		rm -f ${.CURDIR}/obj; \
103		ln -s ${CANONICALOBJDIR} ${.CURDIR}/obj; \
104	else \
105		echo "No ${CANONICALOBJDIR} to link to - do a make obj."; \
106	fi
107.endif
108
109#
110# where would that obj directory be?
111#
112.if !target(whereobj)
113whereobj:
114	@echo ${.OBJDIR}
115.endif
116
117cleanobj:
118	@if [ -d ${CANONICALOBJDIR}/ ]; then \
119		rm -rf ${CANONICALOBJDIR}; \
120	else \
121		cd ${.CURDIR} && ${MAKE} clean cleandepend; \
122	fi
123	@if [ -h ${.CURDIR}/obj ]; then rm -f ${.CURDIR}/obj; fi
124
125.if !target(clean)
126clean: _SUBDIR
127.if defined(CLEANFILES) && !empty(CLEANFILES)
128	rm -f ${CLEANFILES}
129.endif
130.if defined(CLEANDIRS) && !empty(CLEANDIRS)
131	rm -rf ${CLEANDIRS}
132.endif
133.endif
134
135.if !target(checkdpadd)
136checkdpadd: _SUBDIR
137.if (defined(DPADD) || defined(LDADD))
138checkdpadd:
139.if ${OBJFORMAT} != aout
140	@ldadd=`echo \`for lib in ${DPADD} ; do \
141		echo $$lib | sed 's;^/usr/lib/lib\(.*\)\.a;-l\1;' ; \
142	done \`` ; \
143	ldadd1=`echo ${LDADD}` ; \
144	if [ "$$ldadd" != "$$ldadd1" ] ; then \
145		echo ${.CURDIR} ; \
146		echo "DPADD -> $$ldadd" ; \
147		echo "LDADD -> $$ldadd1" ; \
148	fi
149.else
150	@dpadd=`echo \`ld -Bstatic -f ${LDDESTDIR} ${LDADD}\`` ; \
151	if [ "$$dpadd" != "${DPADD}" ] ; then \
152		echo ${.CURDIR} ; \
153		echo "LDADD -> $$dpadd" ; \
154		echo "DPADD =  ${DPADD}" ; \
155	fi
156.endif
157.endif
158.endif
159
160cleandir: cleanobj _SUBDIR
161
162.for __target in cleandepend depend tags
163.if !target(${__target})
164${__target}: _SUBDIR
165.endif
166.endfor
167
168_SUBDIR: .USE
169.if defined(SUBDIR) && !empty(SUBDIR)
170	@for entry in ${SUBDIR}; do \
171		(${ECHODIR} "===> ${DIRPRFX}$$entry"; \
172		if test -d ${.CURDIR}/$${entry}.${MACHINE}; then \
173			cd ${.CURDIR}/$${entry}.${MACHINE}; \
174		else \
175			cd ${.CURDIR}/$${entry}; \
176		fi; \
177		${MAKE} ${.TARGET:S/realinstall/install/:S/.depend/depend/} DIRPRFX=${DIRPRFX}$$entry/); \
178	done
179.endif
180