xref: /src/contrib/bmake/mk/subdir.mk (revision c60f6422ffae3ea85e7b10bad950ad27c463af18)
1# $Id: subdir.mk,v 1.28 2025/08/09 22:42:24 sjg Exp $
2#
3#	@(#) Copyright (c) 2002-2024, Simon J. Gerraty
4#
5#	SPDX-License-Identifier: BSD-2-Clause
6#
7#	Please send copies of changes and bug-fixes to:
8#	sjg@crufty.net
9
10#	if SUBDIR=@auto replace that with each subdir that has
11#	a [Mm]akefile.
12#
13#	Unless SUBDIR_MUST_EXIST is defined, missing subdirs
14#	are ignored (to allow for sparse checkout).
15#
16#	If you use _SUBDIRUSE for a target you may need to add it to
17#	SUBDIR_TARGETS.
18
19# should be set properly in sys.mk
20_this ?= ${.PARSEFILE:S,bsd.,,}
21
22.if !target(__${_this}__)
23__${_this}__: .NOTMAIN
24
25.if defined(SUBDIR) || defined(SUBDIR.yes)
26
27.if ${.MAKE.LEVEL} == 0 && ${MK_DIRDEPS_BUILD:Uno} == "yes"
28.include <meta.subdir.mk>
29# keep everyone happy
30_SUBDIRUSE:
31.elif !commands(_SUBDIRUSE) && !defined(NO_SUBDIR) && !defined(NOSUBDIR)
32.-include <local.subdir.mk>
33.if !target(.MAIN)
34.MAIN: all
35.endif
36
37ECHO_DIR ?= echo
38.ifdef SUBDIR_MUST_EXIST
39MISSING_DIR=echo "Missing ===> ${.CURDIR}/$$_dir"; exit 1
40.else
41MISSING_DIR=echo "Skipping ===> ${.CURDIR}/$$_dir"; exit 0
42.endif
43
44# the actual implementation
45# our target should be of the form ${_target}-${_dir}
46_SUBDIR_USE: .USE
47	@Exists() { test -f $$1; }; \
48	_dir=${.TARGET:C/^[^-]*-//} \
49	_target=${.TARGET:C/-.*//:S/real//:S/.depend/depend/}; \
50	if ! Exists ${.CURDIR}/$$_dir/[mM]akefile; then \
51		${MISSING_DIR}; \
52	fi; \
53	if test X"${_THISDIR_}" = X""; then \
54		_nextdir_="$$_dir"; \
55	else \
56		_nextdir_="$${_THISDIR_}/$$_dir"; \
57	fi; \
58	${ECHO_DIR} "===> $${_nextdir_} ($$_target)"; \
59	(cd ${.CURDIR}/$$_dir && \
60		${.MAKE} _THISDIR_="$${_nextdir_}" $$_target)
61
62.if !target(install)
63.if !target(beforeinstall)
64beforeinstall:
65.endif
66.if !target(afterinstall)
67afterinstall:
68.endif
69install: maninstall
70maninstall: afterinstall
71afterinstall: realinstall
72realinstall: beforeinstall _SUBDIRUSE
73.endif
74
75# the interface from others
76# this may require additions to SUBDIR_TAREGTS
77_SUBDIRUSE: .USE subdir-${.TARGET:C/-.*//:S/real//:S/.depend/depend/}
78
79SUBDIR_TARGETS += \
80	all \
81	clean \
82	cleandir \
83	includes \
84	install \
85	depend \
86	lint \
87	obj \
88	tags \
89	etags
90
91.if ${SUBDIR:U} == "@auto"
92SUBDIR = ${echo ${.CURDIR}/*/[Mm]akefile:L:sh:H:T:O:N\*}
93.endif
94# allow for things like SUBDIR.${MK_TESTS}
95SUBDIR += ${SUBDIR.yes:U}
96
97__subdirs =
98.for d in ${SUBDIR}
99.if $d != ".WAIT" && exists(${.CURDIR}/$d.${MACHINE})
100__subdirs += $d.${MACHINE}
101.else
102__subdirs += $d
103.endif
104.endfor
105
106.for t in ${SUBDIR_TARGETS:O:u}
107__subdir_$t =
108.for d in ${__subdirs}
109.if $d == ".WAIT"
110__subdir_$t += $d
111.elif !commands($t-$d)
112$t-$d: .PHONY .MAKE _SUBDIR_USE
113__subdir_$t += $t-$d
114.endif
115.endfor
116subdir-$t: .PHONY ${__subdir_$t}
117$t: subdir-$t
118.endfor
119
120.else
121_SUBDIRUSE:
122.endif				# SUBDIR
123
124.include <own.mk>
125.if make(destroy*)
126.include <obj.mk>
127.endif
128.endif
129# make sure this exists
130all:
131
132.endif
133