aboutsummaryrefslogtreecommitdiff
path: root/contrib/bmake/mk/subdir.mk
blob: 0feb63af44d285e79706c559a6509cbe0efada6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# SPDX-License-Identifier: BSD-2-Clause
#
#	$Id: subdir.mk,v 1.24 2024/04/10 01:47:23 sjg Exp $
#
#	@(#) Copyright (c) 2002-2024, Simon J. Gerraty
#
#	This file is provided in the hope that it will
#	be of use.  There is absolutely NO WARRANTY.
#	Permission to copy, redistribute or otherwise
#	use this file is hereby granted provided that
#	the above copyright notice and this notice are
#	left intact.
#
#	Please send copies of changes and bug-fixes to:
#	sjg@crufty.net

#	if SUBDIR=@auto replace that with each subdir that has
#	a [Mm]akefile.
#
#	Unless SUBDIR_MUST_EXIST is defined, missing subdirs
#	are ignored (to allow for sparse checkout).
#
#	If you use _SUBDIRUSE for a target you may need to add it to
#	SUBDIR_TARGETS.

# should be set properly in sys.mk
_this ?= ${.PARSEFILE:S,bsd.,,}

.if !target(__${_this}__)
__${_this}__: .NOTMAIN

.if defined(SUBDIR)

.if ${.MAKE.LEVEL} == 0 && ${MK_DIRDEPS_BUILD:Uno} == "yes"
.include <meta.subdir.mk>
# keep everyone happy
_SUBDIRUSE:
.elif !commands(_SUBDIRUSE) && !defined(NO_SUBDIR) && !defined(NOSUBDIR)
.-include <local.subdir.mk>
.if !target(.MAIN)
.MAIN: all
.endif

ECHO_DIR ?= echo
.ifdef SUBDIR_MUST_EXIST
MISSING_DIR=echo "Missing ===> ${.CURDIR}/$$_dir"; exit 1
.else
MISSING_DIR=echo "Skipping ===> ${.CURDIR}/$$_dir"; exit 0
.endif

# the actual implementation
# our target should be of the form ${_target}-${_dir}
_SUBDIR_USE: .USE
	@Exists() { test -f $$1; }; \
	_dir=${.TARGET:C/^.*-//} \
	_target=${.TARGET:C/-.*//:S/real//:S/.depend/depend/}; \
	if ! Exists ${.CURDIR}/$$_dir/[mM]akefile; then \
		${MISSING_DIR}; \
	fi; \
	if test X"${_THISDIR_}" = X""; then \
		_nextdir_="$$_dir"; \
	else \
		_nextdir_="$${_THISDIR_}/$$_dir"; \
	fi; \
	${ECHO_DIR} "===> $${_nextdir_} ($$_target)"; \
	(cd ${.CURDIR}/$$_dir && \
		${.MAKE} _THISDIR_="$${_nextdir_}" $$_target)

.if !target(install)
.if !target(beforeinstall)
beforeinstall:
.endif
.if !target(afterinstall)
afterinstall:
.endif
install: maninstall
maninstall: afterinstall
afterinstall: realinstall
realinstall: beforeinstall _SUBDIRUSE
.endif

# the interface from others
# this may require additions to SUBDIR_TAREGTS
_SUBDIRUSE: .USE subdir-${.TARGET:C/-.*//:S/real//:S/.depend/depend/}

SUBDIR_TARGETS += \
	all \
	clean \
	cleandir \
	includes \
	install \
	depend \
	lint \
	obj \
	tags \
	etags

.if ${SUBDIR} == "@auto"
SUBDIR = ${echo ${.CURDIR}/*/[Mm]akefile:L:sh:H:T:O:N\*}
.endif

__subdirs =
.for d in ${SUBDIR}
.if $d != ".WAIT" && exists(${.CURDIR}/$d.${MACHINE})
__subdirs += $d.${MACHINE}
.else
__subdirs += $d
.endif
.endfor

.for t in ${SUBDIR_TARGETS:O:u}
__subdir_$t =
.for d in ${__subdirs}
.if $d == ".WAIT"
__subdir_$t += $d
.elif !commands($t-$d)
$t-$d: .PHONY .MAKE _SUBDIR_USE
__subdir_$t += $t-$d
.endif
.endfor
subdir-$t: .PHONY ${__subdir_$t}
$t: subdir-$t
.endfor

.else
_SUBDIRUSE:
.endif				# SUBDIR

.include <own.mk>
.if make(destroy*)
.include <obj.mk>
.endif
.endif
# make sure this exists
all:

.endif