xref: /freebsd/share/mk/bsd.subdir.mk (revision 895f86f15fbf6540071feb9328c3c50ed1f027b8)
1#	from: @(#)bsd.subdir.mk	5.9 (Berkeley) 2/1/91
2# $FreeBSD$
3#
4# The include file <bsd.subdir.mk> contains the default targets
5# for building subdirectories.
6#
7# For all of the directories listed in the variable SUBDIRS, the
8# specified directory will be visited and the target made. There is
9# also a default target which allows the command "make subdir" where
10# subdir is any directory listed in the variable SUBDIRS.
11#
12#
13# +++ variables +++
14#
15# DISTRIBUTION	Name of distribution. [base]
16#
17# SUBDIR	A list of subdirectories that should be built as well.
18#		Each of the targets will execute the same target in the
19#		subdirectories. SUBDIR.yes is automatically appeneded
20#		to this list.
21#
22# +++ targets +++
23#
24#	distribute:
25# 		This is a variant of install, which will
26# 		put the stuff into the right "distribution".
27#
28# 	See ALL_SUBDIR_TARGETS for list of targets that will recurse.
29# 	Custom targets can be added to SUBDIR_TARGETS in src.conf.
30#
31# 	Targets defined in STANDALONE_SUBDIR_TARGETS will always be ran
32# 	with SUBDIR_PARALLEL and will not respect .WAIT or SUBDIR_DEPEND_
33# 	values.
34#
35
36.if !target(__<bsd.subdir.mk>__)
37__<bsd.subdir.mk>__:
38
39ALL_SUBDIR_TARGETS= all all-man buildconfig buildfiles buildincludes \
40		    checkdpadd clean cleandepend cleandir cleanilinks \
41		    cleanobj depend distribute files includes installconfig \
42		    installfiles installincludes install lint maninstall \
43		    manlint obj objlink regress tags \
44		    ${SUBDIR_TARGETS}
45
46# Described above.
47STANDALONE_SUBDIR_TARGETS?= obj checkdpadd clean cleandepend cleandir \
48			    cleanilinks cleanobj
49
50.include <bsd.init.mk>
51
52.if !defined(NEED_SUBDIR)
53.if ${.MAKE.LEVEL} == 0 && ${MK_DIRDEPS_BUILD} == "yes" && !empty(SUBDIR) && !(make(clean*) || make(destroy*))
54.include <meta.subdir.mk>
55# ignore this
56_SUBDIR:
57.endif
58.endif
59.if !target(_SUBDIR)
60
61.if defined(SUBDIR)
62SUBDIR:=${SUBDIR} ${SUBDIR.yes}
63SUBDIR:=${SUBDIR:u}
64.endif
65
66DISTRIBUTION?=	base
67.if !target(distribute)
68distribute: .MAKE
69.for dist in ${DISTRIBUTION}
70	${_+_}cd ${.CURDIR}; \
71	    ${MAKE} install -DNO_SUBDIR DESTDIR=${DISTDIR}/${dist} SHARED=copies
72.endfor
73.endif
74
75# Convenience targets to run 'build${target}' and 'install${target}' when
76# calling 'make ${target}'.
77.for __target in files includes
78.if !target(${__target})
79${__target}:	build${__target} install${__target}
80.ORDER:		build${__target} install${__target}
81.endif
82.endfor
83
84# Make 'install' supports a before and after target.  Actual install
85# hooks are placed in 'realinstall'.
86.if !target(install)
87.for __stage in before real after
88.if !target(${__stage}install)
89${__stage}install:
90.endif
91.endfor
92install:	beforeinstall realinstall afterinstall
93.ORDER:		beforeinstall realinstall afterinstall
94.endif
95
96# Subdir code shared among 'make <subdir>', 'make <target>' and SUBDIR_PARALLEL.
97_SUBDIR_SH=	\
98		if test -d ${.CURDIR}/$${dir}.${MACHINE_ARCH}; then \
99			dir=$${dir}.${MACHINE_ARCH}; \
100		fi; \
101		${ECHODIR} "===> ${DIRPRFX}$${dir} ($${target})"; \
102		cd ${.CURDIR}/$${dir}; \
103		${MAKE} $${target} DIRPRFX=${DIRPRFX}$${dir}/
104
105_SUBDIR: .USEBEFORE
106.if defined(SUBDIR) && !empty(SUBDIR) && !defined(NO_SUBDIR)
107	@${_+_}target=${.TARGET}; \
108	    for dir in ${SUBDIR:N.WAIT}; do ( ${_SUBDIR_SH} ); done
109.endif
110
111${SUBDIR:N.WAIT}: .PHONY .MAKE
112	${_+_}@target=all; \
113	    dir=${.TARGET}; \
114	    ${_SUBDIR_SH};
115
116# Work around parsing of .if nested in .for by putting .WAIT string into a var.
117__wait= .WAIT
118.for __target in ${ALL_SUBDIR_TARGETS}
119# Can ordering be skipped for this and SUBDIR_PARALLEL forced?
120.if make(${__target}) && ${STANDALONE_SUBDIR_TARGETS:M${__target}}
121_is_standalone_target=	1
122SUBDIR:=	${SUBDIR:N.WAIT}
123.else
124_is_standalone_target=	0
125.endif
126# Only recurse on directly-called targets.  I.e., don't recurse on dependencies
127# such as 'install' becoming {before,real,after}install, just recurse
128# 'install'.
129.if make(${__target})
130.if defined(SUBDIR_PARALLEL) || ${_is_standalone_target} == 1
131__subdir_targets=
132.for __dir in ${SUBDIR}
133.if ${__wait} == ${__dir}
134__subdir_targets+= .WAIT
135.else
136__subdir_targets+= ${__target}_subdir_${__dir}
137__deps=
138.if ${_is_standalone_target} == 0
139.for __dep in ${SUBDIR_DEPEND_${__dir}}
140__deps+= ${__target}_subdir_${__dep}
141.endfor
142.endif
143${__target}_subdir_${__dir}: .PHONY .MAKE ${__deps}
144.if !defined(NO_SUBDIR)
145	@${_+_}target=${__target}; \
146	    dir=${__dir}; \
147	    ${_SUBDIR_SH};
148.endif
149.endif
150.endfor	# __dir in ${SUBDIR}
151${__target}: ${__subdir_targets}
152.else
153${__target}: _SUBDIR
154.endif	# SUBDIR_PARALLEL || _is_standalone_target
155.elif !target(${__target})
156${__target}:
157.endif	# make(${__target})
158.endfor	# __target in ${ALL_SUBDIR_TARGETS}
159
160.endif	# !target(_SUBDIR)
161
162.endif
163