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