bsd.obj.mk revision 95306
1# $FreeBSD: head/share/mk/bsd.obj.mk 95306 2002-04-23 09:03:56Z ru $
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 *enviroment* variable
14#		and works properly only if set as an enviroment 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 *enviroment* variable
21#		and works properly only if set as an enviroment variable,
22#		not as a global or command line variable!
23#
24#		E.g. use `env MAKEOBJDIRPREFIX=/somewhere/obj make'
25#
26# NOOBJ		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.else
48CANONICALOBJDIR:=/usr/obj${.CURDIR}
49.endif
50
51#
52# Warn of unorthodox object directory.
53#
54# The following directories are tried in order for ${.OBJDIR}:
55#
56# 1.  ${MAKEOBJDIRPREFIX}/`pwd`
57# 2.  ${MAKEOBJDIR}
58# 3.  obj.${MACHINE}
59# 4.  obj
60# 5.  /usr/obj/`pwd`
61# 6.  ${.CURDIR}
62#
63# If ${.OBJDIR} is constructed using canonical cases 1 or 5, or
64# case 2 (using MAKEOBJDIR), don't issue a warning.  Otherwise,
65# issue a warning differentiating between cases 6 and (3 or 4).
66#
67objwarn:
68.if !defined(NOOBJ) && ${.OBJDIR} != ${CANONICALOBJDIR} && \
69    !(defined(MAKEOBJDIRPREFIX) && exists(${CANONICALOBJDIR}/)) && \
70    !(defined(MAKEOBJDIR) && exists(${MAKEOBJDIR}/))
71.if ${.OBJDIR} == ${.CURDIR}
72	@${ECHO} "Warning: Object directory not changed from original ${.CURDIR}"
73.elif exists(${.CURDIR}/obj.${MACHINE}/) || exists(${.CURDIR}/obj/)
74	@${ECHO} "Warning: Using ${.OBJDIR} as object directory instead of\
75		canonical ${CANONICALOBJDIR}"
76.endif
77.endif
78
79.if !target(obj) && !defined(NOOBJ)
80obj:
81	@if ! test -d ${CANONICALOBJDIR}/; then \
82		mkdir -p ${CANONICALOBJDIR}; \
83		if ! test -d ${CANONICALOBJDIR}/; then \
84			${ECHO} "Unable to create ${CANONICALOBJDIR}."; \
85			exit 1; \
86		fi; \
87		${ECHO} "${CANONICALOBJDIR} created for ${.CURDIR}"; \
88	fi
89.endif
90
91.if !target(objlink)
92objlink:
93	@if test -d ${CANONICALOBJDIR}/; then \
94		rm -f ${.CURDIR}/obj; \
95		ln -s ${CANONICALOBJDIR} ${.CURDIR}/obj; \
96	else \
97		echo "No ${CANONICALOBJDIR} to link to - do a make obj."; \
98	fi
99.endif
100
101#
102# where would that obj directory be?
103#
104.if !target(whereobj)
105whereobj:
106	@echo ${.OBJDIR}
107.endif
108
109cleanobj:
110.if ${CANONICALOBJDIR} != ${.CURDIR} && exists(${CANONICALOBJDIR}/)
111	@rm -rf ${CANONICALOBJDIR}
112.else
113	@cd ${.CURDIR} && ${MAKE} clean cleandepend
114.endif
115	@if [ -h ${.CURDIR}/obj ]; then rm -f ${.CURDIR}/obj; fi
116
117.if !target(clean)
118clean:
119.if defined(CLEANFILES) && !empty(CLEANFILES)
120	rm -f ${CLEANFILES} 
121.endif
122.if defined(CLEANDIRS) && !empty(CLEANDIRS)
123	rm -rf ${CLEANDIRS}
124.endif
125.endif
126
127.if !target(checkdpadd)
128checkdpadd:
129.if (defined(DPADD) || defined(LDADD))
130checkdpadd:
131.if ${OBJFORMAT} != aout
132	@ldadd=`echo \`for lib in ${DPADD} ; do \
133		echo $$lib | sed 's;^/usr/lib/lib\(.*\)\.a;-l\1;' ; \
134	done \`` ; \
135	ldadd1=`echo ${LDADD}` ; \
136	if [ "$$ldadd" != "$$ldadd1" ] ; then \
137		echo ${.CURDIR} ; \
138		echo "DPADD -> $$ldadd" ; \
139		echo "LDADD -> $$ldadd1" ; \
140	fi
141.else
142	@dpadd=`echo \`ld -Bstatic -f ${LDADD}\`` ; \
143	if [ "$$dpadd" != "${DPADD}" ] ; then \
144		echo ${.CURDIR} ; \
145		echo "LDADD -> $$dpadd" ; \
146		echo "DPADD =  ${DPADD}" ; \
147	fi
148.endif
149.endif
150.endif
151
152cleandir: cleanobj
153
154.include <bsd.subdir.mk>
155
156.endif !target(__<bsd.obj.mk>__)
157