bsd.obj.mk revision 44922
1#	$Id: bsd.obj.mk,v 1.27 1999/03/14 20:41:13 markm 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# +++ 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#
55objwarn:
56.if !defined(NOOBJ) && ${.OBJDIR} != ${CANONICALOBJDIR}
57.if ${.OBJDIR} == ${.CURDIR}
58	@${ECHO} "Warning: Object directory not changed from original ${.CURDIR}"
59.elif !defined(MAKEOBJDIR) && !defined(MAKEOBJDIRPREFIX)
60	@${ECHO} "Warning: Using ${.OBJDIR} as object directory instead of\
61		canonical ${CANONICALOBJDIR}"
62.endif
63.endif
64
65.if !target(obj)
66.if defined(NOOBJ)
67obj:
68.else
69obj:	_SUBDIR
70	@if ! test -d ${CANONICALOBJDIR}/; then \
71		mkdir -p ${CANONICALOBJDIR}; \
72		if ! test -d ${CANONICALOBJDIR}/; then \
73			${ECHO} "Unable to create ${CANONICALOBJDIR}."; \
74			exit 1; \
75		fi; \
76		${ECHO} "${CANONICALOBJDIR} created for ${.CURDIR}"; \
77	fi
78.endif
79.endif
80
81.if !target(objlink)
82objlink: _SUBDIR
83	@if test -d ${CANONICALOBJDIR}/; then \
84		rm -f ${.CURDIR}/obj; \
85		ln -s ${CANONICALOBJDIR} ${.CURDIR}/obj; \
86	else \
87		echo "No ${CANONICALOBJDIR} to link to - do a make obj."; \
88	fi
89.endif
90
91#
92# where would that obj directory be?
93#
94.if !target(whereobj)
95whereobj:
96	@echo ${.OBJDIR}
97.endif
98
99cleanobj:
100	@if [ -d ${CANONICALOBJDIR}/ ]; then \
101		rm -rf ${CANONICALOBJDIR}; \
102	else \
103		cd ${.CURDIR} && ${MAKE} clean cleandepend; \
104	fi
105	@if [ -h ${.CURDIR}/obj ]; then rm -f ${.CURDIR}/obj; fi
106
107.if !target(clean)
108clean: _SUBDIR
109.if defined(CLEANFILES) && !empty(CLEANFILES)
110	rm -f ${CLEANFILES} 
111.endif
112.if defined(CLEANDIRS) && !empty(CLEANDIRS)
113	rm -rf ${CLEANDIRS}
114.endif
115.endif
116
117.if !target(checkdpadd)
118checkdpadd: _SUBDIR
119.if (defined(DPADD) || defined(LDADD))
120checkdpadd:
121.if ${OBJFORMAT} != aout
122	@ldadd=`echo \`for lib in ${DPADD} ; do \
123		echo $$lib | sed 's;^/usr/lib/lib\(.*\)\.a;-l\1;' ; \
124	done \`` ; \
125	ldadd1=`echo ${LDADD}` ; \
126	if [ "$$ldadd" != "$$ldadd1" ] ; then \
127		echo ${.CURDIR} ; \
128		echo "DPADD -> $$ldadd" ; \
129		echo "LDADD -> $$ldadd1" ; \
130	fi
131.else
132	@dpadd=`echo \`ld -Bstatic -f ${LDDESTDIR} ${LDADD}\`` ; \
133	if [ "$$dpadd" != "${DPADD}" ] ; then \
134		echo ${.CURDIR} ; \
135		echo "LDADD -> $$dpadd" ; \
136		echo "DPADD =  ${DPADD}" ; \
137	fi
138.endif
139.endif
140.endif
141
142cleandir: cleanobj _SUBDIR
143
144.for __target in cleandepend depend tags
145.if !target(${__target})
146${__target}: _SUBDIR
147.endif
148.endfor
149
150_SUBDIR: .USE
151.if defined(SUBDIR) && !empty(SUBDIR)
152	@for entry in ${SUBDIR}; do \
153		(${ECHODIR} "===> ${DIRPRFX}$$entry"; \
154		if test -d ${.CURDIR}/$${entry}.${MACHINE}; then \
155			cd ${.CURDIR}/$${entry}.${MACHINE}; \
156		else \
157			cd ${.CURDIR}/$${entry}; \
158		fi; \
159		${MAKE} ${.TARGET:S/realinstall/install/:S/.depend/depend/} DIRPRFX=${DIRPRFX}$$entry/); \
160	done
161.endif
162