xref: /freebsd/share/mk/bsd.subdir.mk (revision b571bcea5495327fd210378109b0b2aed08bebc3)
1f5d738c8SWolfram Schneider# The include file <bsd.subdir.mk> contains the default targets
2400f992eSWolfram Schneider# for building subdirectories.
3f5d738c8SWolfram Schneider#
4f5d738c8SWolfram Schneider# For all of the directories listed in the variable SUBDIRS, the
5f5d738c8SWolfram Schneider# specified directory will be visited and the target made. There is
6f5d738c8SWolfram Schneider# also a default target which allows the command "make subdir" where
7f5d738c8SWolfram Schneider# subdir is any directory listed in the variable SUBDIRS.
8f5d738c8SWolfram Schneider#
9f5d738c8SWolfram Schneider#
10f5d738c8SWolfram Schneider# +++ variables +++
11f5d738c8SWolfram Schneider#
1201df2ec3SDavid E. O'Brien# DISTRIBUTION	Name of distribution. [base]
13f5d738c8SWolfram Schneider#
14f5d738c8SWolfram Schneider# SUBDIR	A list of subdirectories that should be built as well.
15f5d738c8SWolfram Schneider#		Each of the targets will execute the same target in the
162e1dfb34SBrooks Davis#		subdirectories. SUBDIR.yes and SUBDIR.yes.yes are
172e1dfb34SBrooks Davis#		automatically appended to this list.
18f5d738c8SWolfram Schneider#
19f5d738c8SWolfram Schneider# +++ targets +++
20f5d738c8SWolfram Schneider#
21f5d738c8SWolfram Schneider#	distribute:
22f5d738c8SWolfram Schneider# 		This is a variant of install, which will
23f5d738c8SWolfram Schneider# 		put the stuff into the right "distribution".
24f5d738c8SWolfram Schneider#
25b69c69e0SBryan Drewery# 	See SUBDIR_TARGETS for list of targets that will recurse.
26f5d738c8SWolfram Schneider#
27ccfb12d6SBryan Drewery# 	Targets defined in STANDALONE_SUBDIR_TARGETS will always be ran
28ccfb12d6SBryan Drewery# 	with SUBDIR_PARALLEL and will not respect .WAIT or SUBDIR_DEPEND_
29ccfb12d6SBryan Drewery# 	values.
30ccfb12d6SBryan Drewery#
31b69c69e0SBryan Drewery# 	SUBDIR_TARGETS and STANDALONE_SUBDIR_TARGETS can be appended to
32b69c69e0SBryan Drewery# 	via make.conf or src.conf.
33b69c69e0SBryan Drewery#
34f5d738c8SWolfram Schneider
3550c5d27bSSimon J. Gerraty.if !target(__<bsd.subdir.mk>__)
3650c5d27bSSimon J. Gerraty__<bsd.subdir.mk>__:
3750c5d27bSSimon J. Gerraty
38f8b15066SBryan Drewery.if ${MK_AUTO_OBJ} == "no"
39f8b15066SBryan Drewery_obj=	obj
40f8b15066SBryan Drewery.endif
41f8b15066SBryan Drewery
42b69c69e0SBryan DrewerySUBDIR_TARGETS+= \
43aae63957SBryan Drewery		all all-man analyze buildconfig buildfiles buildincludes \
4403173d2fSBryan Drewery		checkdpadd clean cleandepend cleandir cleanilinks \
4503173d2fSBryan Drewery		cleanobj depend distribute files includes installconfig \
46ebd2a662SBryan Drewery		installdirs \
471cbb5888SWarner Losh		installfiles installincludes print-dir realinstall \
48f8b15066SBryan Drewery		maninstall manlint ${_obj} objlink tags \
490b2580beSBryan Drewery
50ccfb12d6SBryan Drewery# Described above.
51b69c69e0SBryan DrewerySTANDALONE_SUBDIR_TARGETS+= \
52b9c6f316SBryan Drewery		all-man buildconfig buildfiles buildincludes check checkdpadd \
53b9c6f316SBryan Drewery		clean cleandepend cleandir cleanilinks cleanobj files includes \
54ebd2a662SBryan Drewery		installconfig installdirs installincludes installfiles print-dir \
559e2f435fSBryan Drewery		maninstall manlint obj objlink
56b9c6f316SBryan Drewery
572bdaf7e8SRuslan Ermilov.include <bsd.init.mk>
58afe61c15SRodney W. Grimes
59c07be2c5SWarner Losh.if ${MK_META_MODE} == "yes"
60c07be2c5SWarner Losh.MAKE.JOB.PREFIX=
61c07be2c5SWarner LoshECHODIR=	:
62c07be2c5SWarner Losh.endif
63c07be2c5SWarner Losh
649e2f435fSBryan Drewery.if make(print-dir)
659e2f435fSBryan DreweryNEED_SUBDIR=	1
669e2f435fSBryan DreweryECHODIR=	:
679e2f435fSBryan Drewery.SILENT:
689e2f435fSBryan Drewery.if ${RELDIR:U.} != "."
699e2f435fSBryan Dreweryprint-dir:	.PHONY
709e2f435fSBryan Drewery	@echo ${RELDIR}
719e2f435fSBryan Drewery.endif
729e2f435fSBryan Drewery.endif
739e2f435fSBryan Drewery
74f8b15066SBryan Drewery.if ${MK_AUTO_OBJ} == "yes" && !target(obj)
75f8b15066SBryan Dreweryobj: .PHONY
76f8b15066SBryan Drewery.endif
77f8b15066SBryan Drewery
78f5374544SSimon J. Gerraty.if !defined(NEED_SUBDIR)
7934d39d2cSBryan Drewery# .MAKE.DEPENDFILE==/dev/null is set by bsd.dep.mk to avoid reading
8034d39d2cSBryan Drewery# Makefile.depend
8134d39d2cSBryan Drewery.if ${.MAKE.LEVEL} == 0 && ${MK_DIRDEPS_BUILD} == "yes" && !empty(SUBDIR) && \
8234d39d2cSBryan Drewery    ${.MAKE.DEPENDFILE} != "/dev/null"
837750ad47SMarcel Moolenaar.include <meta.subdir.mk>
847750ad47SMarcel Moolenaar# ignore this
857750ad47SMarcel Moolenaar_SUBDIR:
867750ad47SMarcel Moolenaar.endif
877750ad47SMarcel Moolenaar.endif
88e0be2e9bSWarner Losh
898f463ff4SRuslan ErmilovDISTRIBUTION?=	base
908f463ff4SRuslan Ermilov.if !target(distribute)
91a7e08b46SSimon J. Gerratydistribute: .MAKE
928f463ff4SRuslan Ermilov.for dist in ${DISTRIBUTION}
93acabf29aSHartmut Brandt	${_+_}cd ${.CURDIR}; \
94b58ea3e1SJessica Clarke	    ${MAKE} install installconfig -DNO_SUBDIR DISTBASE=/${dist} DESTDIR=${DISTDIR}/${dist} SHARED=copies
958f463ff4SRuslan Ermilov.endfor
968f463ff4SRuslan Ermilov.endif
9703173d2fSBryan Drewery# Convenience targets to run 'build${target}' and 'install${target}' when
9803173d2fSBryan Drewery# calling 'make ${target}'.
9903173d2fSBryan Drewery.for __target in files includes
10003173d2fSBryan Drewery.if !target(${__target})
10103173d2fSBryan Drewery${__target}:	build${__target} install${__target}
10203173d2fSBryan Drewery.ORDER:		build${__target} install${__target}
10303173d2fSBryan Drewery.endif
10403173d2fSBryan Drewery.endfor
10503173d2fSBryan Drewery
10603173d2fSBryan Drewery# Make 'install' supports a before and after target.  Actual install
10703173d2fSBryan Drewery# hooks are placed in 'realinstall'.
10803173d2fSBryan Drewery.if !target(install)
10903173d2fSBryan Drewery.for __stage in before real after
11003173d2fSBryan Drewery.if !target(${__stage}install)
11103173d2fSBryan Drewery${__stage}install:
11203173d2fSBryan Drewery.endif
11303173d2fSBryan Drewery.endfor
11403173d2fSBryan Dreweryinstall:	beforeinstall realinstall afterinstall
11503173d2fSBryan Drewery.ORDER:		beforeinstall realinstall afterinstall
11603173d2fSBryan Drewery.endif
11758857dfcSBryan Drewery.ORDER: all install
11803173d2fSBryan Drewery
1192231cd21SBryan Drewery# SUBDIR recursing may be disabled for MK_DIRDEPS_BUILD
1202231cd21SBryan Drewery.if !target(_SUBDIR)
1212231cd21SBryan Drewery
1222e1dfb34SBrooks Davis.if defined(SUBDIR) || defined(SUBDIR.yes) || defined(SUBDIR.yes.yes)
1232e1dfb34SBrooks DavisSUBDIR:=${SUBDIR} ${SUBDIR.yes} ${SUBDIR.yes.yes}
1242231cd21SBryan DrewerySUBDIR:=${SUBDIR:u}
1252231cd21SBryan Drewery.endif
1262231cd21SBryan Drewery
12702e65672SBrooks Davis.if defined(SUBDIR.)
128*b571bceaSEnji Cooper.error ${.CURDIR}: Found variable 'SUBDIR.' with value "${SUBDIR.}". This was\
12902e65672SBrooks Davis        probably caused by using SUBDIR.$${MK_FOO} without including\
13002e65672SBrooks Davis        <src.opts.mk> or by using an invalid $${MK_FOO} option.
13102e65672SBrooks Davis.endif
13202e65672SBrooks Davis
13365d88005SBryan Drewery# Subdir code shared among 'make <subdir>', 'make <target>' and SUBDIR_PARALLEL.
13465d88005SBryan Drewery_SUBDIR_SH=	\
13565d88005SBryan Drewery		if test -d ${.CURDIR}/$${dir}.${MACHINE_ARCH}; then \
13665d88005SBryan Drewery			dir=$${dir}.${MACHINE_ARCH}; \
13765d88005SBryan Drewery		fi; \
13865d88005SBryan Drewery		${ECHODIR} "===> ${DIRPRFX}$${dir} ($${target})"; \
13965d88005SBryan Drewery		cd ${.CURDIR}/$${dir}; \
14065d88005SBryan Drewery		${MAKE} $${target} DIRPRFX=${DIRPRFX}$${dir}/
14165d88005SBryan Drewery
142e54caebeSBryan Drewery# This is kept for compatibility only.  The normal handling of attaching to
143e54caebeSBryan Drewery# SUBDIR_TARGETS will create a target for each directory.
1447f5f4b44SBryan Drewery_SUBDIR: .USEBEFORE
1458f463ff4SRuslan Ermilov.if defined(SUBDIR) && !empty(SUBDIR) && !defined(NO_SUBDIR)
14685874b56SBryan Drewery	@${_+_}target=${.TARGET:realinstall=install}; \
1470edd2832SBryan Drewery	    for dir in ${SUBDIR:N.WAIT}; do ( ${_SUBDIR_SH} ); done
148b25a566dSRuslan Ermilov.endif
149afe61c15SRodney W. Grimes
15034ce63e5SBryan Drewery# Create 'make subdir' targets to run the real 'all' target.
15134ce63e5SBryan Drewery.for __dir in ${SUBDIR:N.WAIT}
15234ce63e5SBryan Drewery${__dir}: all_subdir_${DIRPRFX}${__dir} .PHONY
15334ce63e5SBryan Drewery.endfor
154afe61c15SRodney W. Grimes
155b69c69e0SBryan Drewery.for __target in ${SUBDIR_TARGETS}
156ccfb12d6SBryan Drewery# Can ordering be skipped for this and SUBDIR_PARALLEL forced?
157b4f9ba38SBryan Drewery.if ${STANDALONE_SUBDIR_TARGETS:M${__target}}
158ccfb12d6SBryan Drewery_is_standalone_target=	1
15961018287SBryan Drewery_subdir_filter=	N.WAIT
160ccfb12d6SBryan Drewery.else
161ccfb12d6SBryan Drewery_is_standalone_target=	0
16261018287SBryan Drewery_subdir_filter=
163ccfb12d6SBryan Drewery.endif
164b4bb98edSIan Lepore__subdir_targets=
16561018287SBryan Drewery.for __dir in ${SUBDIR:${_subdir_filter}}
1660368d219SBryan Drewery.if ${__dir} == .WAIT
167b4bb98edSIan Lepore__subdir_targets+= .WAIT
168b4bb98edSIan Lepore.else
169a1c70d69SIan Lepore__deps=
170ccfb12d6SBryan Drewery.if ${_is_standalone_target} == 0
171e54caebeSBryan Drewery.if defined(SUBDIR_PARALLEL)
172e54caebeSBryan Drewery# Apply SUBDIR_DEPEND dependencies for SUBDIR_PARALLEL.
173a1c70d69SIan Lepore.for __dep in ${SUBDIR_DEPEND_${__dir}}
174a540ec75SBryan Drewery__deps+= ${__target}_subdir_${DIRPRFX}${__dep}
175a1c70d69SIan Lepore.endfor
176e54caebeSBryan Drewery.else
177e54caebeSBryan Drewery# For non-parallel builds, directories depend on all targets before them.
178e54caebeSBryan Drewery__deps:= ${__subdir_targets}
179e54caebeSBryan Drewery.endif	# defined(SUBDIR_PARALLEL)
180e54caebeSBryan Drewery.endif	# ${_is_standalone_target} == 0
18157b5a844SBryan Drewery${__target}_subdir_${DIRPRFX}${__dir}: .PHONY .MAKE .SILENT ${__deps}
18285874b56SBryan Drewery	@${_+_}target=${__target:realinstall=install}; \
18365d88005SBryan Drewery	    dir=${__dir}; \
18465d88005SBryan Drewery	    ${_SUBDIR_SH};
185e54caebeSBryan Drewery__subdir_targets+= ${__target}_subdir_${DIRPRFX}${__dir}
186e54caebeSBryan Drewery.endif	# ${__dir} == .WAIT
187ccfb12d6SBryan Drewery.endfor	# __dir in ${SUBDIR}
188d9bb4215SBryan Drewery
189d9bb4215SBryan Drewery# Attach the subdir targets to the real target.
190d9bb4215SBryan Drewery# Only recurse on directly-called targets.  I.e., don't recurse on dependencies
191d9bb4215SBryan Drewery# such as 'install' becoming {before,real,after}install, just recurse
192d9bb4215SBryan Drewery# 'install'.  Despite that, 'realinstall' is special due to ordering issues
193d9bb4215SBryan Drewery# with 'afterinstall'.
194d9bb4215SBryan Drewery.if !defined(NO_SUBDIR) && (make(${__target}) || \
195d9bb4215SBryan Drewery    (${__target} == realinstall && make(install)))
196997ffab7SBryan Drewery${__target}: ${__subdir_targets} .PHONY
19703173d2fSBryan Drewery.endif	# make(${__target})
198b69c69e0SBryan Drewery.endfor	# __target in ${SUBDIR_TARGETS}
19955ff8fb1SPoul-Henning Kamp
20003173d2fSBryan Drewery.endif	# !target(_SUBDIR)
20150c5d27bSSimon J. Gerraty
2022231cd21SBryan Drewery# Ensure all targets exist
203b69c69e0SBryan Drewery.for __target in ${SUBDIR_TARGETS}
2042231cd21SBryan Drewery.if !target(${__target})
2052231cd21SBryan Drewery${__target}:
2062231cd21SBryan Drewery.endif
2072231cd21SBryan Drewery.endfor
2082231cd21SBryan Drewery
20950c5d27bSSimon J. Gerraty.endif
210