xref: /freebsd/share/mk/bsd.subdir.mk (revision 38069501)
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 appended
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 SUBDIR_TARGETS for list of targets that will recurse.
29#
30# 	Targets defined in STANDALONE_SUBDIR_TARGETS will always be ran
31# 	with SUBDIR_PARALLEL and will not respect .WAIT or SUBDIR_DEPEND_
32# 	values.
33#
34# 	SUBDIR_TARGETS and STANDALONE_SUBDIR_TARGETS can be appended to
35# 	via make.conf or src.conf.
36#
37
38.if !target(__<bsd.subdir.mk>__)
39__<bsd.subdir.mk>__:
40
41SUBDIR_TARGETS+= \
42		all all-man analyze buildconfig buildfiles buildincludes \
43		checkdpadd clean cleandepend cleandir cleanilinks \
44		cleanobj depend distribute files includes installconfig \
45		installfiles installincludes print-dir realinstall lint \
46		maninstall manlint obj objlink tags \
47
48# Described above.
49STANDALONE_SUBDIR_TARGETS+= \
50		all-man buildconfig buildfiles buildincludes check checkdpadd \
51		clean cleandepend cleandir cleanilinks cleanobj files includes \
52		installconfig installincludes installfiles print-dir \
53		maninstall manlint obj objlink
54
55# It is safe to install in parallel when staging.
56.if defined(NO_ROOT) || !empty(SYSROOT)
57STANDALONE_SUBDIR_TARGETS+= realinstall
58.endif
59
60.include <bsd.init.mk>
61
62.if make(print-dir)
63NEED_SUBDIR=	1
64ECHODIR=	:
65.SILENT:
66.if ${RELDIR:U.} != "."
67print-dir:	.PHONY
68	@echo ${RELDIR}
69.endif
70.endif
71
72.if !defined(NEED_SUBDIR)
73# .MAKE.DEPENDFILE==/dev/null is set by bsd.dep.mk to avoid reading
74# Makefile.depend
75.if ${.MAKE.LEVEL} == 0 && ${MK_DIRDEPS_BUILD} == "yes" && !empty(SUBDIR) && \
76    ${.MAKE.DEPENDFILE} != "/dev/null"
77.include <meta.subdir.mk>
78# ignore this
79_SUBDIR:
80.endif
81.endif
82
83DISTRIBUTION?=	base
84.if !target(distribute)
85distribute: .MAKE
86.for dist in ${DISTRIBUTION}
87	${_+_}cd ${.CURDIR}; \
88	    ${MAKE} install installconfig -DNO_SUBDIR DESTDIR=${DISTDIR}/${dist} SHARED=copies
89.endfor
90.endif
91# Convenience targets to run 'build${target}' and 'install${target}' when
92# calling 'make ${target}'.
93.for __target in files includes
94.if !target(${__target})
95${__target}:	build${__target} install${__target}
96.ORDER:		build${__target} install${__target}
97.endif
98.endfor
99
100# Make 'install' supports a before and after target.  Actual install
101# hooks are placed in 'realinstall'.
102.if !target(install)
103.for __stage in before real after
104.if !target(${__stage}install)
105${__stage}install:
106.endif
107.endfor
108install:	beforeinstall realinstall afterinstall
109.ORDER:		beforeinstall realinstall afterinstall
110.endif
111.ORDER: all install
112
113# SUBDIR recursing may be disabled for MK_DIRDEPS_BUILD
114.if !target(_SUBDIR)
115
116.if defined(SUBDIR) || defined(SUBDIR.yes)
117SUBDIR:=${SUBDIR} ${SUBDIR.yes}
118SUBDIR:=${SUBDIR:u}
119.endif
120
121# Subdir code shared among 'make <subdir>', 'make <target>' and SUBDIR_PARALLEL.
122_SUBDIR_SH=	\
123		if test -d ${.CURDIR}/$${dir}.${MACHINE_ARCH}; then \
124			dir=$${dir}.${MACHINE_ARCH}; \
125		fi; \
126		${ECHODIR} "===> ${DIRPRFX}$${dir} ($${target})"; \
127		cd ${.CURDIR}/$${dir}; \
128		${MAKE} $${target} DIRPRFX=${DIRPRFX}$${dir}/
129
130# This is kept for compatibility only.  The normal handling of attaching to
131# SUBDIR_TARGETS will create a target for each directory.
132_SUBDIR: .USEBEFORE
133.if defined(SUBDIR) && !empty(SUBDIR) && !defined(NO_SUBDIR)
134	@${_+_}target=${.TARGET:realinstall=install}; \
135	    for dir in ${SUBDIR:N.WAIT}; do ( ${_SUBDIR_SH} ); done
136.endif
137
138# Create 'make subdir' targets to run the real 'all' target.
139.for __dir in ${SUBDIR:N.WAIT}
140${__dir}: all_subdir_${DIRPRFX}${__dir} .PHONY
141.endfor
142
143.for __target in ${SUBDIR_TARGETS}
144# Can ordering be skipped for this and SUBDIR_PARALLEL forced?
145.if ${STANDALONE_SUBDIR_TARGETS:M${__target}}
146_is_standalone_target=	1
147_subdir_filter=	N.WAIT
148.else
149_is_standalone_target=	0
150_subdir_filter=
151.endif
152__subdir_targets=
153.for __dir in ${SUBDIR:${_subdir_filter}}
154.if ${__dir} == .WAIT
155__subdir_targets+= .WAIT
156.else
157__deps=
158.if ${_is_standalone_target} == 0
159.if defined(SUBDIR_PARALLEL)
160# Apply SUBDIR_DEPEND dependencies for SUBDIR_PARALLEL.
161.for __dep in ${SUBDIR_DEPEND_${__dir}}
162__deps+= ${__target}_subdir_${DIRPRFX}${__dep}
163.endfor
164.else
165# For non-parallel builds, directories depend on all targets before them.
166__deps:= ${__subdir_targets}
167.endif	# defined(SUBDIR_PARALLEL)
168.endif	# ${_is_standalone_target} == 0
169${__target}_subdir_${DIRPRFX}${__dir}: .PHONY .MAKE .SILENT ${__deps}
170	@${_+_}target=${__target:realinstall=install}; \
171	    dir=${__dir}; \
172	    ${_SUBDIR_SH};
173__subdir_targets+= ${__target}_subdir_${DIRPRFX}${__dir}
174.endif	# ${__dir} == .WAIT
175.endfor	# __dir in ${SUBDIR}
176
177# Attach the subdir targets to the real target.
178# Only recurse on directly-called targets.  I.e., don't recurse on dependencies
179# such as 'install' becoming {before,real,after}install, just recurse
180# 'install'.  Despite that, 'realinstall' is special due to ordering issues
181# with 'afterinstall'.
182.if !defined(NO_SUBDIR) && (make(${__target}) || \
183    (${__target} == realinstall && make(install)))
184${__target}: ${__subdir_targets} .PHONY
185.endif	# make(${__target})
186.endfor	# __target in ${SUBDIR_TARGETS}
187
188.endif	# !target(_SUBDIR)
189
190# Ensure all targets exist
191.for __target in ${SUBDIR_TARGETS}
192.if !target(${__target})
193${__target}:
194.endif
195.endfor
196
197.endif
198