xref: /freebsd/share/mk/bsd.subdir.mk (revision 069ac184)
1# The include file <bsd.subdir.mk> contains the default targets
2# for building subdirectories.
3#
4# For all of the directories listed in the variable SUBDIRS, the
5# specified directory will be visited and the target made. There is
6# also a default target which allows the command "make subdir" where
7# subdir is any directory listed in the variable SUBDIRS.
8#
9#
10# +++ variables +++
11#
12# DISTRIBUTION	Name of distribution. [base]
13#
14# SUBDIR	A list of subdirectories that should be built as well.
15#		Each of the targets will execute the same target in the
16#		subdirectories. SUBDIR.yes and SUBDIR.yes.yes are
17#		automatically appended to this list.
18#
19# +++ targets +++
20#
21#	distribute:
22# 		This is a variant of install, which will
23# 		put the stuff into the right "distribution".
24#
25# 	See SUBDIR_TARGETS for list of targets that will recurse.
26#
27# 	Targets defined in STANDALONE_SUBDIR_TARGETS will always be ran
28# 	with SUBDIR_PARALLEL and will not respect .WAIT or SUBDIR_DEPEND_
29# 	values.
30#
31# 	SUBDIR_TARGETS and STANDALONE_SUBDIR_TARGETS can be appended to
32# 	via make.conf or src.conf.
33#
34
35.if !target(__<bsd.subdir.mk>__)
36__<bsd.subdir.mk>__:
37
38.if ${MK_AUTO_OBJ} == "no"
39_obj=	obj
40.endif
41
42SUBDIR_TARGETS+= \
43		all all-man analyze buildconfig buildfiles buildincludes \
44		checkdpadd clean cleandepend cleandir cleanilinks \
45		cleanobj depend distribute files includes installconfig \
46		installdirs \
47		installfiles installincludes print-dir realinstall \
48		maninstall manlint ${_obj} objlink tags \
49
50# Described above.
51STANDALONE_SUBDIR_TARGETS+= \
52		all-man buildconfig buildfiles buildincludes check checkdpadd \
53		clean cleandepend cleandir cleanilinks cleanobj files includes \
54		installconfig installdirs installincludes installfiles print-dir \
55		maninstall manlint obj objlink
56
57# It is safe to install in parallel when staging.
58.if defined(NO_ROOT) || !empty(SYSROOT)
59STANDALONE_SUBDIR_TARGETS+= realinstall
60.endif
61
62.include <bsd.init.mk>
63
64.if ${MK_META_MODE} == "yes"
65.MAKE.JOB.PREFIX=
66ECHODIR=	:
67.endif
68
69.if make(print-dir)
70NEED_SUBDIR=	1
71ECHODIR=	:
72.SILENT:
73.if ${RELDIR:U.} != "."
74print-dir:	.PHONY
75	@echo ${RELDIR}
76.endif
77.endif
78
79.if ${MK_AUTO_OBJ} == "yes" && !target(obj)
80obj: .PHONY
81.endif
82
83.if !defined(NEED_SUBDIR)
84# .MAKE.DEPENDFILE==/dev/null is set by bsd.dep.mk to avoid reading
85# Makefile.depend
86.if ${.MAKE.LEVEL} == 0 && ${MK_DIRDEPS_BUILD} == "yes" && !empty(SUBDIR) && \
87    ${.MAKE.DEPENDFILE} != "/dev/null"
88.include <meta.subdir.mk>
89# ignore this
90_SUBDIR:
91.endif
92.endif
93
94DISTRIBUTION?=	base
95.if !target(distribute)
96distribute: .MAKE
97.for dist in ${DISTRIBUTION}
98	${_+_}cd ${.CURDIR}; \
99	    ${MAKE} install installconfig -DNO_SUBDIR DISTBASE=/${dist} DESTDIR=${DISTDIR}/${dist} SHARED=copies
100.endfor
101.endif
102# Convenience targets to run 'build${target}' and 'install${target}' when
103# calling 'make ${target}'.
104.for __target in files includes
105.if !target(${__target})
106${__target}:	build${__target} install${__target}
107.ORDER:		build${__target} install${__target}
108.endif
109.endfor
110
111# Make 'install' supports a before and after target.  Actual install
112# hooks are placed in 'realinstall'.
113.if !target(install)
114.for __stage in before real after
115.if !target(${__stage}install)
116${__stage}install:
117.endif
118.endfor
119install:	beforeinstall realinstall afterinstall
120.ORDER:		beforeinstall realinstall afterinstall
121.endif
122.ORDER: all install
123
124# SUBDIR recursing may be disabled for MK_DIRDEPS_BUILD
125.if !target(_SUBDIR)
126
127.if defined(SUBDIR) || defined(SUBDIR.yes) || defined(SUBDIR.yes.yes)
128SUBDIR:=${SUBDIR} ${SUBDIR.yes} ${SUBDIR.yes.yes}
129SUBDIR:=${SUBDIR:u}
130.endif
131
132.if defined(SUBDIR.)
133.error ${.CURDIR}: Found variable SUBDIR. with value "${SUBDIR.}". This was \
134        probably caused by using SUBDIR.$${MK_FOO} without including \
135        <src.opts.mk> or by using an invalid $${MK_FOO} option.
136.endif
137
138# Subdir code shared among 'make <subdir>', 'make <target>' and SUBDIR_PARALLEL.
139_SUBDIR_SH=	\
140		if test -d ${.CURDIR}/$${dir}.${MACHINE_ARCH}; then \
141			dir=$${dir}.${MACHINE_ARCH}; \
142		fi; \
143		${ECHODIR} "===> ${DIRPRFX}$${dir} ($${target})"; \
144		cd ${.CURDIR}/$${dir}; \
145		${MAKE} $${target} DIRPRFX=${DIRPRFX}$${dir}/
146
147# This is kept for compatibility only.  The normal handling of attaching to
148# SUBDIR_TARGETS will create a target for each directory.
149_SUBDIR: .USEBEFORE
150.if defined(SUBDIR) && !empty(SUBDIR) && !defined(NO_SUBDIR)
151	@${_+_}target=${.TARGET:realinstall=install}; \
152	    for dir in ${SUBDIR:N.WAIT}; do ( ${_SUBDIR_SH} ); done
153.endif
154
155# Create 'make subdir' targets to run the real 'all' target.
156.for __dir in ${SUBDIR:N.WAIT}
157${__dir}: all_subdir_${DIRPRFX}${__dir} .PHONY
158.endfor
159
160.for __target in ${SUBDIR_TARGETS}
161# Can ordering be skipped for this and SUBDIR_PARALLEL forced?
162.if ${STANDALONE_SUBDIR_TARGETS:M${__target}}
163_is_standalone_target=	1
164_subdir_filter=	N.WAIT
165.else
166_is_standalone_target=	0
167_subdir_filter=
168.endif
169__subdir_targets=
170.for __dir in ${SUBDIR:${_subdir_filter}}
171.if ${__dir} == .WAIT
172__subdir_targets+= .WAIT
173.else
174__deps=
175.if ${_is_standalone_target} == 0
176.if defined(SUBDIR_PARALLEL)
177# Apply SUBDIR_DEPEND dependencies for SUBDIR_PARALLEL.
178.for __dep in ${SUBDIR_DEPEND_${__dir}}
179__deps+= ${__target}_subdir_${DIRPRFX}${__dep}
180.endfor
181.else
182# For non-parallel builds, directories depend on all targets before them.
183__deps:= ${__subdir_targets}
184.endif	# defined(SUBDIR_PARALLEL)
185.endif	# ${_is_standalone_target} == 0
186${__target}_subdir_${DIRPRFX}${__dir}: .PHONY .MAKE .SILENT ${__deps}
187	@${_+_}target=${__target:realinstall=install}; \
188	    dir=${__dir}; \
189	    ${_SUBDIR_SH};
190__subdir_targets+= ${__target}_subdir_${DIRPRFX}${__dir}
191.endif	# ${__dir} == .WAIT
192.endfor	# __dir in ${SUBDIR}
193
194# Attach the subdir targets to the real target.
195# Only recurse on directly-called targets.  I.e., don't recurse on dependencies
196# such as 'install' becoming {before,real,after}install, just recurse
197# 'install'.  Despite that, 'realinstall' is special due to ordering issues
198# with 'afterinstall'.
199.if !defined(NO_SUBDIR) && (make(${__target}) || \
200    (${__target} == realinstall && make(install)))
201${__target}: ${__subdir_targets} .PHONY
202.endif	# make(${__target})
203.endfor	# __target in ${SUBDIR_TARGETS}
204
205.endif	# !target(_SUBDIR)
206
207# Ensure all targets exist
208.for __target in ${SUBDIR_TARGETS}
209.if !target(${__target})
210${__target}:
211.endif
212.endfor
213
214.endif
215