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