xref: /freebsd/contrib/bmake/mk/subdir.mk (revision 6a7405f5a6b639682cacf01e35d561411ff556aa)
1c59c3bf3SSimon J. Gerraty# SPDX-License-Identifier: BSD-2-Clause
2c59c3bf3SSimon J. Gerraty#
3*6a7405f5SSimon J. Gerraty#	$Id: subdir.mk,v 1.27 2024/09/01 05:02:43 sjg Exp $
4c59c3bf3SSimon J. Gerraty#
5c59c3bf3SSimon J. Gerraty#	@(#) Copyright (c) 2002-2024, Simon J. Gerraty
6c59c3bf3SSimon J. Gerraty#
7c59c3bf3SSimon J. Gerraty#	This file is provided in the hope that it will
8c59c3bf3SSimon J. Gerraty#	be of use.  There is absolutely NO WARRANTY.
9c59c3bf3SSimon J. Gerraty#	Permission to copy, redistribute or otherwise
10c59c3bf3SSimon J. Gerraty#	use this file is hereby granted provided that
11c59c3bf3SSimon J. Gerraty#	the above copyright notice and this notice are
12c59c3bf3SSimon J. Gerraty#	left intact.
13c59c3bf3SSimon J. Gerraty#
14c59c3bf3SSimon J. Gerraty#	Please send copies of changes and bug-fixes to:
15c59c3bf3SSimon J. Gerraty#	sjg@crufty.net
163cbdda60SSimon J. Gerraty
17c59c3bf3SSimon J. Gerraty#	if SUBDIR=@auto replace that with each subdir that has
18c59c3bf3SSimon J. Gerraty#	a [Mm]akefile.
19c59c3bf3SSimon J. Gerraty#
20c59c3bf3SSimon J. Gerraty#	Unless SUBDIR_MUST_EXIST is defined, missing subdirs
21c59c3bf3SSimon J. Gerraty#	are ignored (to allow for sparse checkout).
22c59c3bf3SSimon J. Gerraty#
23c59c3bf3SSimon J. Gerraty#	If you use _SUBDIRUSE for a target you may need to add it to
24c59c3bf3SSimon J. Gerraty#	SUBDIR_TARGETS.
253cbdda60SSimon J. Gerraty
26c59c3bf3SSimon J. Gerraty# should be set properly in sys.mk
27c59c3bf3SSimon J. Gerraty_this ?= ${.PARSEFILE:S,bsd.,,}
28c59c3bf3SSimon J. Gerraty
29c59c3bf3SSimon J. Gerraty.if !target(__${_this}__)
30c59c3bf3SSimon J. Gerraty__${_this}__: .NOTMAIN
31c59c3bf3SSimon J. Gerraty
32*6a7405f5SSimon J. Gerraty.if defined(SUBDIR) || defined(SUBDIR.yes)
33c59c3bf3SSimon J. Gerraty
34c59c3bf3SSimon J. Gerraty.if ${.MAKE.LEVEL} == 0 && ${MK_DIRDEPS_BUILD:Uno} == "yes"
353cbdda60SSimon J. Gerraty.include <meta.subdir.mk>
363cbdda60SSimon J. Gerraty# keep everyone happy
373cbdda60SSimon J. Gerraty_SUBDIRUSE:
383cbdda60SSimon J. Gerraty.elif !commands(_SUBDIRUSE) && !defined(NO_SUBDIR) && !defined(NOSUBDIR)
39c59c3bf3SSimon J. Gerraty.-include <local.subdir.mk>
403cbdda60SSimon J. Gerraty.if !target(.MAIN)
413cbdda60SSimon J. Gerraty.MAIN: all
423cbdda60SSimon J. Gerraty.endif
433cbdda60SSimon J. Gerraty
443cbdda60SSimon J. GerratyECHO_DIR ?= echo
453cbdda60SSimon J. Gerraty.ifdef SUBDIR_MUST_EXIST
46c59c3bf3SSimon J. GerratyMISSING_DIR=echo "Missing ===> ${.CURDIR}/$$_dir"; exit 1
473cbdda60SSimon J. Gerraty.else
48c59c3bf3SSimon J. GerratyMISSING_DIR=echo "Skipping ===> ${.CURDIR}/$$_dir"; exit 0
493cbdda60SSimon J. Gerraty.endif
503cbdda60SSimon J. Gerraty
51c59c3bf3SSimon J. Gerraty# the actual implementation
52c59c3bf3SSimon J. Gerraty# our target should be of the form ${_target}-${_dir}
53c59c3bf3SSimon J. Gerraty_SUBDIR_USE: .USE
543cbdda60SSimon J. Gerraty	@Exists() { test -f $$1; }; \
55*6a7405f5SSimon J. Gerraty	_dir=${.TARGET:C/^[^-]*-//} \
56c59c3bf3SSimon J. Gerraty	_target=${.TARGET:C/-.*//:S/real//:S/.depend/depend/}; \
57c59c3bf3SSimon J. Gerraty	if ! Exists ${.CURDIR}/$$_dir/[mM]akefile; then \
583cbdda60SSimon J. Gerraty		${MISSING_DIR}; \
593cbdda60SSimon J. Gerraty	fi; \
603cbdda60SSimon J. Gerraty	if test X"${_THISDIR_}" = X""; then \
61c59c3bf3SSimon J. Gerraty		_nextdir_="$$_dir"; \
623cbdda60SSimon J. Gerraty	else \
63c59c3bf3SSimon J. Gerraty		_nextdir_="$${_THISDIR_}/$$_dir"; \
643cbdda60SSimon J. Gerraty	fi; \
65c59c3bf3SSimon J. Gerraty	${ECHO_DIR} "===> $${_nextdir_} ($$_target)"; \
66c59c3bf3SSimon J. Gerraty	(cd ${.CURDIR}/$$_dir && \
67c59c3bf3SSimon J. Gerraty		${.MAKE} _THISDIR_="$${_nextdir_}" $$_target)
683cbdda60SSimon J. Gerraty
693cbdda60SSimon J. Gerraty.if !target(install)
703cbdda60SSimon J. Gerraty.if !target(beforeinstall)
713cbdda60SSimon J. Gerratybeforeinstall:
723cbdda60SSimon J. Gerraty.endif
733cbdda60SSimon J. Gerraty.if !target(afterinstall)
743cbdda60SSimon J. Gerratyafterinstall:
753cbdda60SSimon J. Gerraty.endif
763cbdda60SSimon J. Gerratyinstall: maninstall
773cbdda60SSimon J. Gerratymaninstall: afterinstall
783cbdda60SSimon J. Gerratyafterinstall: realinstall
793cbdda60SSimon J. Gerratyrealinstall: beforeinstall _SUBDIRUSE
803cbdda60SSimon J. Gerraty.endif
813cbdda60SSimon J. Gerraty
82c59c3bf3SSimon J. Gerraty# the interface from others
83c59c3bf3SSimon J. Gerraty# this may require additions to SUBDIR_TAREGTS
84548bfc56SSimon J. Gerraty_SUBDIRUSE: .USE subdir-${.TARGET:C/-.*//:S/real//:S/.depend/depend/}
853cbdda60SSimon J. Gerraty
863cbdda60SSimon J. GerratySUBDIR_TARGETS += \
873cbdda60SSimon J. Gerraty	all \
883cbdda60SSimon J. Gerraty	clean \
893cbdda60SSimon J. Gerraty	cleandir \
903cbdda60SSimon J. Gerraty	includes \
91c59c3bf3SSimon J. Gerraty	install \
923cbdda60SSimon J. Gerraty	depend \
933cbdda60SSimon J. Gerraty	lint \
943cbdda60SSimon J. Gerraty	obj \
953cbdda60SSimon J. Gerraty	tags \
963cbdda60SSimon J. Gerraty	etags
973cbdda60SSimon J. Gerraty
98*6a7405f5SSimon J. Gerraty.if ${SUBDIR:U} == "@auto"
99c59c3bf3SSimon J. GerratySUBDIR = ${echo ${.CURDIR}/*/[Mm]akefile:L:sh:H:T:O:N\*}
100c59c3bf3SSimon J. Gerraty.endif
101*6a7405f5SSimon J. Gerraty# allow for things like SUBDIR.${MK_TESTS}
102*6a7405f5SSimon J. GerratySUBDIR += ${SUBDIR.yes:U}
103c59c3bf3SSimon J. Gerraty
104c59c3bf3SSimon J. Gerraty__subdirs =
105c59c3bf3SSimon J. Gerraty.for d in ${SUBDIR}
106c59c3bf3SSimon J. Gerraty.if $d != ".WAIT" && exists(${.CURDIR}/$d.${MACHINE})
107c59c3bf3SSimon J. Gerraty__subdirs += $d.${MACHINE}
108c59c3bf3SSimon J. Gerraty.else
109c59c3bf3SSimon J. Gerraty__subdirs += $d
110c59c3bf3SSimon J. Gerraty.endif
1113cbdda60SSimon J. Gerraty.endfor
1123cbdda60SSimon J. Gerraty
113c59c3bf3SSimon J. Gerraty.for t in ${SUBDIR_TARGETS:O:u}
114c59c3bf3SSimon J. Gerraty__subdir_$t =
115c59c3bf3SSimon J. Gerraty.for d in ${__subdirs}
116c59c3bf3SSimon J. Gerraty.if $d == ".WAIT"
117c59c3bf3SSimon J. Gerraty__subdir_$t += $d
118c59c3bf3SSimon J. Gerraty.elif !commands($t-$d)
119c59c3bf3SSimon J. Gerraty$t-$d: .PHONY .MAKE _SUBDIR_USE
120c59c3bf3SSimon J. Gerraty__subdir_$t += $t-$d
121c59c3bf3SSimon J. Gerraty.endif
122c59c3bf3SSimon J. Gerraty.endfor
123c59c3bf3SSimon J. Gerratysubdir-$t: .PHONY ${__subdir_$t}
124c59c3bf3SSimon J. Gerraty$t: subdir-$t
125c59c3bf3SSimon J. Gerraty.endfor
126c59c3bf3SSimon J. Gerraty
127c59c3bf3SSimon J. Gerraty.else
128c59c3bf3SSimon J. Gerraty_SUBDIRUSE:
129c59c3bf3SSimon J. Gerraty.endif				# SUBDIR
130c59c3bf3SSimon J. Gerraty
1313cbdda60SSimon J. Gerraty.include <own.mk>
1323cbdda60SSimon J. Gerraty.if make(destroy*)
1333cbdda60SSimon J. Gerraty.include <obj.mk>
1343cbdda60SSimon J. Gerraty.endif
1353cbdda60SSimon J. Gerraty.endif
1363cbdda60SSimon J. Gerraty# make sure this exists
1373cbdda60SSimon J. Gerratyall:
138c59c3bf3SSimon J. Gerraty
139c59c3bf3SSimon J. Gerraty.endif
140