xref: /freebsd/share/mk/dirdeps.mk (revision e0bb199925565a3770733afd1a4d8bb2d4d0ce31)
1# $Id: dirdeps.mk,v 1.155 2023/04/15 05:46:19 sjg Exp $
2
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2010-2023, Simon J. Gerraty
6# Copyright (c) 2010-2018, Juniper Networks, Inc.
7# All rights reserved.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30# Much of the complexity here is for supporting cross-building.
31# If a tree does not support that, simply using plain Makefile.depend
32# should provide sufficient clue.
33# Otherwise the recommendation is to use Makefile.depend.${MACHINE}
34# as expected below.
35
36# Note: this file gets multiply included.
37# This is what we do with DIRDEPS
38
39# DIRDEPS:
40#	This is a list of directories - relative to SRCTOP, it is
41#	normally only of interest to .MAKE.LEVEL 0.
42#	In some cases the entry may be qualified with a .<machine>
43#	or .<target_spec> suffix (see TARGET_SPEC_VARS below),
44#	for example to force building something for the pseudo
45#	machines "host" or "common" regardless of current ${MACHINE}.
46#
47#	All unqualified entries end up being qualified with .${TARGET_SPEC}
48#	and partially qualified (if TARGET_SPEC_VARS has multiple
49#	entries) are also expanded to a full .<target_spec>.
50#	The  _DIRDEP_USE target uses the suffix to set TARGET_SPEC
51#	correctly when visiting each entry.
52#
53#	The fully qualified directory entries are used to construct a
54#	dependency graph that will drive the build later.
55#
56#	Also, for each fully qualified directory target, we will search
57#	using ${.MAKE.DEPENDFILE_PREFERENCE} to find additional
58#	dependencies.  We use Makefile.depend (default value for
59#	.MAKE.DEPENDFILE_PREFIX) to refer to these makefiles to
60#	distinguish them from others.
61#
62#	Before each Makefile.depend file is read, we set
63#	DEP_RELDIR to be the RELDIR (path relative to SRCTOP) for
64#	its directory, and DEP_MACHINE etc according to the .<target_spec>
65#	represented by the suffix of the corresponding target.
66#
67#	Since each Makefile.depend file includes dirdeps.mk, this
68#	processing is recursive and results in .MAKE.LEVEL 0 learning the
69#	dependencies of the tree wrt the initial directory (_DEP_RELDIR).
70#
71#	NOTE: given the extent of processing that DIRDEPS undergoes it
72#	is important that any variables in entries use :U to guard
73#	against surprises when undefined.
74#
75# TARGET_SPEC_VARS
76#	The default value is just MACHINE, and for most environments
77#	this is sufficient.  The _DIRDEP_USE target actually sets
78#	both MACHINE and TARGET_SPEC to the suffix of the current
79#	target so that in the general case TARGET_SPEC can be ignored.
80#
81#	If more than MACHINE is needed then sys.mk needs to decompose
82#	TARGET_SPEC and set the relevant variables accordingly.
83#	It is important that MACHINE be included in and actually be
84#	the first member of TARGET_SPEC_VARS.  This allows other
85#	variables to be considered optional, and some of the treatment
86#	below relies on MACHINE being the first entry.
87#	Note: TARGET_SPEC cannot contain any '.'s so the target
88#	triple used by compiler folk won't work (directly anyway).
89#
90#	For example:
91#
92#		# Always list MACHINE first,
93#		# other variables might be optional.
94#		TARGET_SPEC_VARS = MACHINE TARGET_OS
95#		.if ${TARGET_SPEC:Uno:M*,*} != ""
96#		_tspec := ${TARGET_SPEC:S/,/ /g}
97#		MACHINE := ${_tspec:[1]}
98#		TARGET_OS := ${_tspec:[2]}
99#		# etc.
100#		# We need to stop that TARGET_SPEC affecting any submakes
101#		# and deal with MACHINE=${TARGET_SPEC} in the environment.
102#		TARGET_SPEC =
103#		# export but do not track
104#		.export-env TARGET_SPEC
105#		.export ${TARGET_SPEC_VARS}
106#		.for v in ${TARGET_SPEC_VARS:O:u}
107#		.if empty($v)
108#		.undef $v
109#		.endif
110#		.endfor
111#		.endif
112#		# make sure we know what TARGET_SPEC is
113#		# as we may need it to find Makefile.depend*
114#		TARGET_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts,}
115#
116#	The following variables can influence the initial DIRDEPS
117#	computation with regard to the TARGET_SPECs that will be
118#	built.
119#	Most should also be considered by init.mk
120#
121#	ONLY_TARGET_SPEC_LIST
122#		Defines a list of TARGET_SPECs for which the current
123#		directory can be built.
124#		If ALL_MACHINES is defined, we build for all the
125#		TARGET_SPECs listed.
126#
127#	ONLY_MACHINE_LIST
128#		As for ONLY_TARGET_SPEC_LIST but only specifies
129#		MACHINEs.
130#
131#	NOT_TARGET_SPEC_LIST
132#		A list of TARGET_SPECs for which the current
133#		directory should not be built.
134#
135#	NOT_MACHINE_LIST
136#		A list of MACHINEs the current directory should not be
137#		built for.
138#
139# DIRDEPS_EXPORT_VARS (DEP_EXPORT_VARS)
140#	It is discouraged, but sometimes necessary for a
141#	Makefile.depend file to influence the environment.
142#	Doing this is correctly (especially if using DIRDEPS_CACHE) is
143#	tricky so a Makefile.depend file can set DIRDEPS_EXPORT_VARS
144#	and dirdeps.mk will do the deed:
145#
146#		MK_UEFI = yes
147#		DIRDEPS_EXPORT_VARS = MK_UEFI
148#
149# _build_xtra_dirs
150#	local.dirdeps.mk can add targets to this variable.
151#	They will be hooked into the build, but independent of
152#	any other DIRDEP.
153#
154#	This allows for adding TESTS to the build, such that the build
155#	if any test fails, but without the risk of introducing
156#	circular dependencies.
157
158now_utc ?= ${%s:L:gmtime}
159.if !defined(start_utc)
160start_utc := ${now_utc}
161.endif
162
163.if !target(bootstrap) && (make(bootstrap) || \
164	make(bootstrap-this) || \
165	make(bootstrap-recurse) || \
166	make(bootstrap-empty))
167# disable most of below
168.MAKE.LEVEL = 1
169.endif
170
171# touch this at your peril
172_DIRDEP_USE_LEVEL?= 0
173.if ${.MAKE.LEVEL} == ${_DIRDEP_USE_LEVEL}
174# only the first instance is interested in all this
175
176# the first time we are included the _DIRDEP_USE target will not be defined
177# we can use this as a clue to do initialization and other one time things.
178.if !target(_DIRDEP_USE)
179
180# do some setup we only need once
181_CURDIR ?= ${.CURDIR}
182_OBJDIR ?= ${.OBJDIR}
183
184.if ${MAKEFILE:T} == ${.PARSEFILE} && empty(DIRDEPS) && ${.TARGETS:Uall:M*[/.]*} != ""
185# This little trick let's us do
186#
187# mk -f dirdeps.mk some/dir.${TARGET_SPEC}
188#
189all:
190${.TARGETS:Nall}: all
191DIRDEPS := ${.TARGETS:M*[/.]*}
192# so that -DNO_DIRDEPS works
193DEP_RELDIR := ${DIRDEPS:[1]:R}
194# this will become DEP_MACHINE below
195TARGET_MACHINE := ${DIRDEPS:[1]:E:C/,.*//}
196.if ${TARGET_MACHINE:N*/*} == ""
197TARGET_MACHINE := ${MACHINE}
198.endif
199# disable DIRDEPS_CACHE as it does not like this trick
200MK_DIRDEPS_CACHE = no
201# incase anyone needs to know
202_dirdeps_cmdline:
203.endif
204
205# make sure we get the behavior we expect
206.MAKE.SAVE_DOLLARS = no
207
208# make sure these are empty to start with
209_DEP_TARGET_SPEC =
210
211# If TARGET_SPEC_VARS is other than just MACHINE
212# it should be set by sys.mk or similar by now.
213# TARGET_SPEC must not contain any '.'s.
214TARGET_SPEC_VARS ?= MACHINE
215# this is what we started with
216TARGET_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts,}
217# this is what we mostly use below
218DEP_TARGET_SPEC = ${TARGET_SPEC_VARS:S,^,DEP_,:@v@${$v:U}@:ts,}
219# make sure we have defaults
220.for v in ${TARGET_SPEC_VARS}
221DEP_$v ?= ${$v}
222.endfor
223
224.if ${TARGET_SPEC_VARS:[#]} > 1
225# Ok, this gets more complex (putting it mildly).
226# In order to stay sane, we need to ensure that all the build_dirs
227# we compute below are fully qualified wrt DEP_TARGET_SPEC.
228# The makefiles may only partially specify (eg. MACHINE only),
229# so we need to construct a set of modifiers to fill in the gaps.
230.if ${MAKE_VERSION} >= 20170130
231_tspec_x := ${TARGET_SPEC_VARS:range}
232.else
233# do it the hard way
234_tspec_x := ${TARGET_SPEC_VARS:[#]:@x@i=1;while [ $$i -le $x ]; do echo $$i; i=$$((i + 1)); done;@:sh}
235.endif
236# this handles unqualified entries
237M_dep_qual_fixes = C;(/[^/.,]+)$$;\1.$${DEP_TARGET_SPEC};
238# there needs to be at least one item missing for these to make sense
239.for i in ${_tspec_x:[2..-1]}
240_tspec_m$i := ${TARGET_SPEC_VARS:[2..$i]:@w@[^,]+@:ts,}
241_tspec_a$i := ,${TARGET_SPEC_VARS:[$i..-1]:@v@$$$${DEP_$v}@:ts,}
242M_dep_qual_fixes += C;(\.${_tspec_m$i})$$;\1${_tspec_a$i};
243.endfor
244TARGET_SPEC_VARSr := ${TARGET_SPEC_VARS:[-1..1]}
245.else
246# A harmless? default.
247M_dep_qual_fixes = U
248.endif
249
250.if !defined(.MAKE.DEPENDFILE_PREFERENCE)
251# .MAKE.DEPENDFILE_PREFERENCE makes the logic below neater?
252# you really want this set by sys.mk or similar
253.MAKE.DEPENDFILE_PREFERENCE = ${_CURDIR}/${.MAKE.DEPENDFILE:T}
254.if ${.MAKE.DEPENDFILE:E} == "${TARGET_SPEC}"
255.if ${TARGET_SPEC} != ${MACHINE}
256.MAKE.DEPENDFILE_PREFERENCE += ${_CURDIR}/${.MAKE.DEPENDFILE:T:R}.$${MACHINE}
257.endif
258.MAKE.DEPENDFILE_PREFERENCE += ${_CURDIR}/${.MAKE.DEPENDFILE:T:R}
259.endif
260.endif
261
262_default_dependfile := ${.MAKE.DEPENDFILE_PREFERENCE:[1]:T}
263_machine_dependfiles := ${.MAKE.DEPENDFILE_PREFERENCE:T:M*${MACHINE}*}
264
265# for machine specific dependfiles we require ${MACHINE} to be at the end
266# also for the sake of sanity we require a common prefix
267.if !defined(.MAKE.DEPENDFILE_PREFIX)
268# knowing .MAKE.DEPENDFILE_PREFIX helps
269.if !empty(_machine_dependfiles)
270.MAKE.DEPENDFILE_PREFIX := ${_machine_dependfiles:[1]:T:R}
271.else
272.MAKE.DEPENDFILE_PREFIX := ${_default_dependfile:T}
273.endif
274.endif
275
276# this is how we identify non-machine specific dependfiles
277N_notmachine := ${.MAKE.DEPENDFILE_PREFERENCE:E:N*${MACHINE}*:${M_ListToSkip}}
278
279# this gets reset for each dirdep we check
280DEP_RELDIR ?= ${RELDIR}
281
282# remember the initial value of DEP_RELDIR - we test for it below.
283_DEP_RELDIR := ${DEP_RELDIR}
284
285# this can cause lots of output!
286# set to a set of glob expressions that might match RELDIR
287DEBUG_DIRDEPS ?= no
288
289# make sure this target exists
290dirdeps: beforedirdeps .WAIT
291beforedirdeps:
292
293.endif				# !target(_DIRDEP_USE)
294
295.if ${DEBUG_DIRDEPS:@x@${DEP_RELDIR:M$x}${${DEP_RELDIR}.${DEP_MACHINE}:L:M$x}@} != ""
296_debug_reldir = 1
297.else
298_debug_reldir = 0
299.endif
300.if ${DEBUG_DIRDEPS:@x@${DEP_RELDIR:M$x}${${DEP_RELDIR}.depend depend:L:M$x}@} != ""
301_debug_search = 1
302.else
303_debug_search = 0
304.endif
305
306# First off, we want to know what ${MACHINE} to build for.
307# This can be complicated if we are using a mixture of ${MACHINE} specific
308# and non-specific Makefile.depend*
309
310# if we were included recursively _DEP_TARGET_SPEC should be valid.
311.if empty(_DEP_TARGET_SPEC)
312DEP_MACHINE = ${TARGET_MACHINE:U${MACHINE}}
313_DEP_TARGET_SPEC := ${DEP_TARGET_SPEC}
314.if ${.INCLUDEDFROMFILE:U:M${.MAKE.DEPENDFILE_PREFIX}*} != ""
315# record that we've read dependfile for this
316_dirdeps_checked.${_CURDIR}.${TARGET_SPEC}:
317.endif
318.endif
319
320# by now _DEP_TARGET_SPEC should be set, parse it.
321.if ${TARGET_SPEC_VARS:[#]} > 1
322# we need to parse DEP_MACHINE may or may not contain more info
323_tspec := ${_DEP_TARGET_SPEC:S/,/ /g}
324.for i in ${_tspec_x}
325DEP_${TARGET_SPEC_VARS:[$i]} := ${_tspec:[$i]}
326.endfor
327.for v in ${TARGET_SPEC_VARS:O:u}
328.if empty(DEP_$v)
329.undef DEP_$v
330.endif
331.endfor
332.else
333DEP_MACHINE := ${_DEP_TARGET_SPEC}
334.endif
335
336# reset each time through
337_build_all_dirs =
338_build_xtra_dirs =
339
340# DIRDEPS_CACHE can be very handy for debugging.
341# Also if repeatedly building the same target,
342# we can avoid the overhead of re-computing the tree dependencies.
343MK_DIRDEPS_CACHE ?= no
344BUILD_DIRDEPS_CACHE ?= no
345BUILD_DIRDEPS ?= yes
346
347.if ${MK_DIRDEPS_CACHE} == "yes"
348# this is where we will cache all our work
349DIRDEPS_CACHE ?= ${_OBJDIR:tA}/dirdeps.cache${_TARGETS:U${.TARGETS}:Nall:O:u:ts-:S,/,_,g:S,^,.,:N.}
350.endif
351
352
353# pickup customizations
354# as below you can use !target(_DIRDEP_USE) to protect things
355# which should only be done once.
356.-include <local.dirdeps.mk>
357
358.if !target(_DIRDEP_USE)
359# things we skip for host tools
360SKIP_HOSTDIR ?=
361
362NSkipHostDir = ${SKIP_HOSTDIR:N*.host*:S,$,.host*,:N.host*:S,^,${SRCTOP}/,:${M_ListToSkip}}
363
364# things we always skip
365# SKIP_DIRDEPS allows for adding entries on command line.
366SKIP_DIR += .host *.WAIT ${SKIP_DIRDEPS}
367SKIP_DIR.host += ${SKIP_HOSTDIR}
368
369DEP_SKIP_DIR = ${SKIP_DIR} \
370	${SKIP_DIR.${DEP_TARGET_SPEC}:U} \
371	${TARGET_SPEC_VARS:@v@${SKIP_DIR.${DEP_$v}:U}@} \
372	${SKIP_DIRDEPS.${DEP_TARGET_SPEC}:U} \
373	${TARGET_SPEC_VARS:@v@${SKIP_DIRDEPS.${DEP_$v}:U}@}
374
375
376NSkipDir = ${DEP_SKIP_DIR:${M_ListToSkip}}
377
378.if defined(NODIRDEPS) || defined(WITHOUT_DIRDEPS)
379NO_DIRDEPS =
380.elif defined(WITHOUT_DIRDEPS_BELOW)
381NO_DIRDEPS_BELOW =
382.endif
383
384.if defined(NO_DIRDEPS)
385# confine ourselves to the original dir and below.
386DIRDEPS_FILTER += M${_DEP_RELDIR}*
387.elif defined(NO_DIRDEPS_BELOW)
388DIRDEPS_FILTER += M${_DEP_RELDIR}
389.endif
390
391# this is what we run below
392DIRDEP_MAKE ?= ${.MAKE}
393DIRDEP_DIR ?= ${.TARGET:R}
394
395# if you want us to report load averages during build
396# DIRDEP_USE_PRELUDE += ${DIRDEP_LOADAVG_REPORT};
397
398DIRDEP_LOADAVG_CMD ?= ${UPTIME:Uuptime} | sed 's,.*\(load\),\1,'
399DIRDEP_LOADAVG_LAST = 0
400# yes the expression here is a bit complicated,
401# the trick is to only eval ${DIRDEP_LOADAVG_LAST::=${now_utc}}
402# when we want to report.
403# Note: expr(1) will exit 1 if the expression evaluates to 0
404# hence the  || true
405DIRDEP_LOADAVG_REPORT = \
406	test -z "${"${expr ${now_utc} - ${DIRDEP_LOADAVG_INTERVAL:U60} - ${DIRDEP_LOADAVG_LAST} || true:L:sh:N-*}":?yes${DIRDEP_LOADAVG_LAST::=${now_utc}}:}" || \
407	echo "${TRACER}`${DIRDEP_LOADAVG_CMD}`"
408
409# we suppress SUBDIR when visiting the leaves
410# we assume sys.mk will set MACHINE_ARCH
411# you can add extras to DIRDEP_USE_ENV
412# if there is no makefile in the target directory, we skip it.
413_DIRDEP_USE:	.USE .MAKE
414	@for m in ${.MAKE.MAKEFILE_PREFERENCE}; do \
415		test -s ${.TARGET:R}/$$m || continue; \
416		echo "${TRACER}Checking ${.TARGET:S,^${SRCTOP}/,,} for ${.TARGET:E} ..."; \
417		${DIRDEP_USE_PRELUDE} \
418		MACHINE_ARCH= NO_SUBDIR=1 ${DIRDEP_USE_ENV} \
419		TARGET_SPEC=${.TARGET:E} \
420		MACHINE=${.TARGET:E} \
421		${DIRDEP_MAKE} -C ${DIRDEP_DIR} || exit 1; \
422		break; \
423	done
424
425.ifdef ALL_MACHINES
426.if empty(ONLY_TARGET_SPEC_LIST) && empty(ONLY_MACHINE_LIST)
427# we start with everything
428_machine_list != echo; 'ls' -1 ${_CURDIR}/${.MAKE.DEPENDFILE_PREFIX}* 2> /dev/null
429
430# some things we know we want to ignore
431DIRDEPS_TARGETS_SKIP_LIST += \
432	*~ \
433	*.bak \
434	*.inc \
435	*.old \
436	*.options \
437	*.orig \
438	*.rej \
439
440# first trim things we know we want to skip
441# and provide canonical form
442_machine_list := ${_machine_list:${DIRDEPS_TARGETS_SKIP_LIST:${M_ListToSkip}}:T:E}
443
444# cater for local complexities
445# local.dirdeps.mk can set
446# DIRDEPS_ALL_MACHINES_FILTER and
447# DIRDEPS_ALL_MACHINES_FILTER_XTRAS for final tweaks
448
449.if !empty(ALL_TARGET_SPEC_LIST)
450.if ${_debug_reldir}
451.info ALL_TARGET_SPEC_LIST=${ALL_TARGET_SPEC_LIST}
452.endif
453DIRDEPS_ALL_MACHINES_FILTER += \
454	@x@$${ALL_TARGET_SPEC_LIST:@s@$${x:M$$s}@}@
455.elif !empty(ALL_MACHINE_LIST)
456.if ${_debug_reldir}
457.info ALL_MACHINE_LIST=${ALL_MACHINE_LIST}
458.endif
459.if ${TARGET_SPEC_VARS:[#]} > 1
460# the space below can result in extraneous ':'
461DIRDEPS_ALL_MACHINES_FILTER += \
462	@x@$${ALL_MACHINE_LIST:@m@$${x:M$$m,*} $${x:M$$m}@}@
463.else
464DIRDEPS_ALL_MACHINES_FILTER += \
465	@x@$${ALL_MACHINE_LIST:@m@$${x:M$$m}@}@
466.endif
467.endif
468# add local XTRAS - default to something benign
469DIRDEPS_ALL_MACHINES_FILTER += \
470	${DIRDEPS_ALL_MACHINES_FILTER_XTRAS:UNbak}
471
472.if ${_debug_reldir}
473.info _machine_list=${_machine_list}
474.info DIRDEPS_ALL_MACHINES_FILTER=${DIRDEPS_ALL_MACHINES_FILTER}
475.endif
476
477_only_machines := ${_machine_list:${DIRDEPS_ALL_MACHINES_FILTER:ts:}:S,:, ,g}
478.else
479_only_machines := ${ONLY_TARGET_SPEC_LIST:U} ${ONLY_MACHINE_LIST:U}
480.endif
481
482.if empty(_only_machines)
483# we must be boot-strapping
484_only_machines := ${TARGET_MACHINE:U${ALL_TARGET_SPEC_LIST:U${ALL_MACHINE_LIST:U${DEP_MACHINE}}}}
485.endif
486
487# cleanup the result
488_only_machines := ${_only_machines:O:u}
489
490.if ${_debug_reldir}
491.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: ALL_MACHINES _only_machines=${_only_machines}
492.endif
493
494.else				# ! ALL_MACHINES
495# if ONLY_TARGET_SPEC_LIST or ONLY_MACHINE_LIST is set, we are limited to that.
496# Note that ONLY_TARGET_SPEC_LIST should be fully qualified.
497# if TARGET_MACHINE is set - it is really the same as ONLY_MACHINE_LIST
498# otherwise DEP_MACHINE is it - so DEP_MACHINE will match.
499_only_machines := ${ONLY_TARGET_SPEC_LIST:U:M${DEP_MACHINE},*}
500.if empty(_only_machines)
501_only_machines := ${ONLY_MACHINE_LIST:U${TARGET_MACHINE:U${DEP_MACHINE}}:M${DEP_MACHINE}}
502.endif
503.endif
504
505.if !empty(NOT_MACHINE_LIST)
506_only_machines := ${_only_machines:${NOT_MACHINE_LIST:${M_ListToSkip}}}
507.endif
508.if !empty(NOT_TARGET_SPEC_LIST)
509# we must first qualify
510_dm := ${DEP_MACHINE}
511_only_machines := ${_only_machines:M*,*} ${_only_machines:N*,*:@DEP_MACHINE@${DEP_TARGET_SPEC}@:S,^,.,:${M_dep_qual_fixes:ts:}:O:u:S,^.,,}
512DEP_MACHINE := ${_dm}
513_only_machines := ${_only_machines:${NOT_TARGET_SPEC_LIST:${M_ListToSkip}}}
514.endif
515# clean up
516_only_machines := ${_only_machines:O:u}
517
518.if ${_debug_reldir}
519.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: _only_machines=${_only_machines}
520.endif
521
522# make sure we have a starting place?
523DIRDEPS ?= ${RELDIR}
524.endif				# target
525
526.if !defined(NO_DIRDEPS) && !defined(NO_DIRDEPS_BELOW)
527.if ${MK_DIRDEPS_CACHE} == "yes"
528
529# just ensure this exists
530build-dirdeps:
531
532M_oneperline = @x@\\${.newline}	$$x@
533
534.if ${BUILD_DIRDEPS_CACHE} == "no"
535.if !target(dirdeps-cached)
536# we do this via sub-make
537BUILD_DIRDEPS = no
538
539# ignore anything but these
540.MAKE.META.IGNORE_FILTER = M*/${.MAKE.DEPENDFILE_PREFIX}*
541
542dirdeps: dirdeps-cached
543dirdeps-cached:	${DIRDEPS_CACHE} .MAKE
544	@echo "${TRACER}Using ${DIRDEPS_CACHE}"
545	@MAKELEVEL=${.MAKE.LEVEL} \
546	TARGET_SPEC=${TARGET_SPEC} \
547	${TARGET_SPEC_VARS:@v@$v=${$v}@} \
548	${.MAKE} -C ${_CURDIR} -f ${DIRDEPS_CACHE} \
549		dirdeps MK_DIRDEPS_CACHE=no BUILD_DIRDEPS=no
550
551# leaf makefiles rarely work for building DIRDEPS_CACHE
552.if ${RELDIR} != "."
553BUILD_DIRDEPS_MAKEFILE ?= -f dirdeps.mk
554.endif
555
556# these should generally do
557BUILD_DIRDEPS_MAKEFILE ?=
558BUILD_DIRDEPS_TARGETS ?= ${.TARGETS}
559
560.if ${DIRDEPS_CACHE} != ${STATIC_DIRDEPS_CACHE:Uno} && ${DIRDEPS_CACHE:M${SRCTOP}/*} == ""
561# export this for dirdeps-cache-update.mk
562DYNAMIC_DIRDEPS_CACHE := ${DIRDEPS_CACHE}
563.export DYNAMIC_DIRDEPS_CACHE
564# we need the .meta file to ensure we update if
565# any of the Makefile.depend* changed.
566# We do not want to compare the command line though.
567${DIRDEPS_CACHE}:	.META .NOMETA_CMP
568	+@{ echo '# Autogenerated - do NOT edit!'; echo; \
569	echo 'BUILD_DIRDEPS=no'; echo; \
570	echo '.include <dirdeps.mk>'; echo; \
571	} > ${.TARGET}.new
572	+@MAKELEVEL=${.MAKE.LEVEL} DIRDEPS_CACHE=${DIRDEPS_CACHE} \
573	DIRDEPS="${DIRDEPS}" \
574	TARGET_SPEC=${TARGET_SPEC} \
575	MAKEFLAGS= ${DIRDEP_CACHE_MAKE:U${.MAKE}} -C ${_CURDIR} \
576	${BUILD_DIRDEPS_MAKEFILE} \
577	${BUILD_DIRDEPS_TARGETS} BUILD_DIRDEPS_CACHE=yes \
578	.MAKE.DEPENDFILE=.none \
579	${"${DEBUG_DIRDEPS:Nno}":?DEBUG_DIRDEPS='${DEBUG_DIRDEPS}':} \
580	${.MAKEFLAGS:tW:S,-D ,-D,g:tw:M*WITH*} \
581	${.MAKEFLAGS:tW:S,-d ,-d,g:tw:M-d*} \
582	3>&1 1>&2 | sed 's,${SRCTOP},_{SRCTOP},g;s,_{SRCTOP}/_{SRCTOP},_{SRCTOP},g;s,_{,$${,g' >> ${.TARGET}.new && \
583	mv ${.TARGET}.new ${.TARGET}
584
585.endif
586.endif
587.elif !target(_count_dirdeps)
588# we want to capture the dirdeps count in the cache
589.END: _count_dirdeps
590_count_dirdeps: .NOMETA
591	@{ echo; echo '.info $${.newline}$${TRACER}Makefiles read: total=${.MAKE.MAKEFILES:[#]} depend=${.MAKE.MAKEFILES:M*depend*:[#]} dirdeps=${.ALLTARGETS:M${SRCTOP}*:O:u:[#]} ${DIRDEP_INFO_XTRAS}'; } >&3
592
593.endif
594.elif !make(dirdeps) && !target(_count_dirdeps)
595beforedirdeps: _count_dirdeps
596_count_dirdeps: .NOMETA
597	@echo "${TRACER}Makefiles read: total=${.MAKE.MAKEFILES:[#]} depend=${.MAKE.MAKEFILES:M*depend*:[#]} dirdeps=${.ALLTARGETS:M${SRCTOP}*:O:u:[#]} ${DIRDEP_INFO_XTRAS} seconds=`expr ${now_utc} - ${start_utc}`"
598
599.endif
600.endif
601
602.if ${BUILD_DIRDEPS} == "yes"
603
604# the rest is done repeatedly for every Makefile.depend we read.
605# if we are anything but the original dir we care only about the
606# machine type we were included for..
607
608.if ${DEP_RELDIR} == "."
609_this_dir := ${SRCTOP}
610.else
611_this_dir := ${SRCTOP}/${DEP_RELDIR}
612.endif
613
614# on rare occasions, there can be a need for extra help
615_dep_hack := ${_this_dir}/${.MAKE.DEPENDFILE_PREFIX}.inc
616.-include <${_dep_hack}>
617.-include <${_dep_hack:R}.options>
618
619.if ${DEP_RELDIR} != ${_DEP_RELDIR} || ${DEP_TARGET_SPEC} != ${TARGET_SPEC}
620# this should be all
621_machines := ${DEP_MACHINE}
622.else
623# this is the machine list we actually use below
624_machines := ${_only_machines}
625
626.if defined(HOSTPROG) || ${DEP_MACHINE:Nhost*} == ""
627# we need to build this guy's dependencies for host as well.
628.if ${DEP_MACHINE:Nhost*} == ""
629_machines += ${DEP_MACHINE}
630.else
631_machines += host
632.endif
633.endif
634
635_machines := ${_machines:O:u}
636.endif
637
638.if ${TARGET_SPEC_VARS:[#]} > 1
639# we need to tweak _machines
640_dm := ${DEP_MACHINE}
641# apply the same filtering that we do when qualifying DIRDEPS.
642# M_dep_qual_fixes expects .${MACHINE}* so add (and remove) '.'
643# Again we expect that any already qualified machines are fully qualified.
644_machines := ${_machines:M*,*} ${_machines:N*,*:@DEP_MACHINE@${DEP_TARGET_SPEC}@:S,^,.,:${M_dep_qual_fixes:ts:}:O:u:S,^.,,}
645DEP_MACHINE := ${_dm}
646_machines := ${_machines:O:u}
647.endif
648
649# reset each time through
650_build_dirs =
651
652.if ${DEP_RELDIR} == ${_DEP_RELDIR}
653# pickup other machines for this dir if necessary
654_build_dirs += ${_machines:@m@${_CURDIR}.$m@}
655.endif
656
657.if ${_debug_reldir}
658.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: nDIRDEPS=${DIRDEPS:[#]}
659.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: DIRDEPS='${DIRDEPS}'
660.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: _machines='${_machines}'
661.endif
662
663.if !empty(DIRDEPS)
664# these we reset each time through as they can depend on DEP_MACHINE
665DEP_DIRDEPS_FILTER = \
666	${DIRDEPS_FILTER.${DEP_TARGET_SPEC}:U} \
667	${TARGET_SPEC_VARS:@v@${DIRDEPS_FILTER.${DEP_$v}:U}@} \
668	${DIRDEPS_FILTER:U}
669.if empty(DEP_DIRDEPS_FILTER)
670# something harmless
671DEP_DIRDEPS_FILTER = U
672.endif
673
674# this is what we start with
675__depdirs := ${DIRDEPS:${NSkipDir}:${DEP_DIRDEPS_FILTER:ts:}:C,//+,/,g:O:u:@d@${SRCTOP}/$d@}
676
677# some entries may be qualified with .<machine> or .<target_spec>
678# we can tell the unqualified ones easily - because they exist
679__unqual_depdirs := ${__depdirs:@d@${exists($d):?$d:}@}
680__qual_depdirs := ${__depdirs:${__unqual_depdirs:Uno:${M_ListToSkip}}}
681
682.if ${DEP_RELDIR} == ${_DEP_RELDIR}
683# if it was called out - we likely need it.
684__hostdpadd := ${DPADD:U.:M${HOST_OBJTOP}/*:S,${HOST_OBJTOP}/,,:H:${NSkipDir}:${DIRDEPS_FILTER:ts:}:S,$,.host,:N.*:@d@${SRCTOP}/$d@} \
685	${DPADD:U.:M${HOST_OBJTOP32:Uno}/*:S,${HOST_OBJTOP32:Uno}/,,:H:${NSkipDir}:${DIRDEPS_FILTER:ts:}:S,$,.host32,:N.*:@d@${SRCTOP}/$d@}
686__qual_depdirs += ${__hostdpadd}
687.endif
688
689.if ${_debug_reldir}
690.info DEP_DIRDEPS_FILTER=${DEP_DIRDEPS_FILTER:ts:}
691.info depdirs=${__depdirs:S,^${SRCTOP}/,,}
692.info qualified=${__qual_depdirs:S,^${SRCTOP}/,,}
693.info unqualified=${__unqual_depdirs:S,^${SRCTOP}/,,}
694.endif
695
696# _build_dirs is what we will feed to _DIRDEP_USE
697_build_dirs += \
698	${__qual_depdirs:M*.host:${NSkipHostDir}:N.host} \
699	${__qual_depdirs:N*.host} \
700	${_machines:Mhost*:@m@${__unqual_depdirs:@d@$d.$m@}@:${NSkipHostDir}:N.host} \
701	${_machines:Nhost*:@m@${__unqual_depdirs:@d@$d.$m@}@}
702
703# qualify everything now
704_build_dirs := ${_build_dirs:${M_dep_qual_fixes:ts:}:O:u}
705
706.endif				# empty DIRDEPS
707
708_build_all_dirs += ${_build_dirs} ${_build_xtra_dirs}
709_build_all_dirs := ${_build_all_dirs:O:u}
710
711# Normally if doing make -V something,
712# we do not want to waste time chasing DIRDEPS
713# but if we want to count the number of Makefile.depend* read, we do.
714.if ${.MAKEFLAGS:M-V${_V_READ_DIRDEPS:U}} == ""
715.if !empty(_build_all_dirs)
716.if ${BUILD_DIRDEPS_CACHE} == "yes"
717# we use _cache_script to minimize the number of times we fork the shell
718_cache_script = echo '\# ${DEP_RELDIR}.${DEP_TARGET_SPEC}';
719# guard against _new_dirdeps being too big for a single command line
720_new_dirdeps := ${_build_all_dirs:@x@${target($x):?:$x}@:S,^${SRCTOP}/,,}
721_cache_xtra_deps := ${_build_xtra_dirs:S,^${SRCTOP}/,,}
722.export _cache_xtra_deps _new_dirdeps
723.if !empty(DIRDEPS_EXPORT_VARS) || !empty(DEP_EXPORT_VARS)
724# Discouraged, but there are always exceptions.
725# Handle it here rather than explain how.
726DIRDEPS_EXPORT_VARS ?= ${DEP_EXPORT_VARS}
727_cache_xvars := echo; ${DIRDEPS_EXPORT_VARS:@v@echo '$v = ${$v}';@} echo '.export ${DIRDEPS_EXPORT_VARS}'; echo;
728_cache_script += ${_cache_xvars}
729.endif
730.else
731# this makes it all happen
732dirdeps: ${_build_all_dirs}
733.endif
734${_build_all_dirs}:	_DIRDEP_USE
735
736.if ${_debug_reldir}
737.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: needs: ${_build_dirs:S,^${SRCTOP}/,,}
738.endif
739
740.if !empty(DIRDEPS_EXPORT_VARS) || !empty(DEP_EXPORT_VARS)
741.export ${DIRDEPS_EXPORT_VARS} ${DEP_EXPORT_VARS}
742DIRDEPS_EXPORT_VARS =
743DEP_EXPORT_VARS =
744.endif
745
746# this builds the dependency graph
747.for m in ${_machines}
748.if ${BUILD_DIRDEPS_CACHE} == "yes" && !empty(_build_dirs)
749_cache_deps =
750_cache_script += echo; echo 'DIRDEPS.${_this_dir}.$m = \';
751.endif
752# it would be nice to do :N${.TARGET}
753.if !empty(__qual_depdirs)
754.for q in ${__qual_depdirs:${M_dep_qual_fixes:ts:}:E:O:u:N$m}
755.if ${_debug_reldir} || ${DEBUG_DIRDEPS:@x@${${DEP_RELDIR}.$m:L:M$x}${${DEP_RELDIR}.$q:L:M$x}@} != ""
756.info ${DEP_RELDIR}.$m: graph: ${_build_dirs:M*.$q:S,^${SRCTOP}/,,}
757.endif
758.if ${BUILD_DIRDEPS_CACHE} == "yes"
759_cache_deps += ${_build_dirs:M*.$q:S,^${SRCTOP}/,,}
760.else
761${_this_dir}.$m: ${_build_dirs:M*.$q}
762.endif
763.endfor
764.endif
765.if ${_debug_reldir}
766.info ${DEP_RELDIR}.$m: graph: ${_build_dirs:M*.$m:N${_this_dir}.$m:S,^${SRCTOP}/,,}
767.endif
768.if ${BUILD_DIRDEPS_CACHE} == "yes"
769.if !empty(_build_dirs)
770_cache_deps += ${_build_dirs:M*.$m:N${_this_dir}.$m:S,^${SRCTOP}/,,}
771.if !empty(_cache_deps)
772.export _cache_deps
773_cache_script += for x in $$_cache_deps; do echo "	_{SRCTOP}/$$x \\"; done;
774.endif
775# anything in _{build,env}_xtra_dirs is hooked to dirdeps: only
776x!= echo; { echo; ${_cache_script} echo; echo '${_this_dir}.$m: $${DIRDEPS.${_this_dir}.$m}'; \
777	echo; echo 'dirdeps: ${_this_dir}.$m \'; \
778	for x in $$_cache_xtra_deps; do echo "	_{SRCTOP}/$$x \\"; done; \
779	echo; for x in $$_new_dirdeps; do echo "_{SRCTOP}/$$x: _DIRDEP_USE"; done; } >&3
780.endif
781.else
782${_this_dir}.$m: ${_build_dirs:M*.$m:N${_this_dir}.$m}
783.endif
784.endfor
785
786.endif
787
788# Now find more dependencies - and recurse.
789.for d in ${_build_all_dirs}
790.if !target(_dirdeps_checked.$d)
791# once only
792_dirdeps_checked.$d:
793_dr := ${d:S,^${SRCTOP}/,,}
794.if ${_debug_search}
795.info checking ${_dr}
796.endif
797# Note: _build_all_dirs is fully qualifed so d:R is always the directory
798.if exists(${d:R})
799# we pass _DEP_TARGET_SPEC to tell the next step what we want
800_DEP_TARGET_SPEC := ${d:E}
801# some makefiles may still look at this
802_DEP_MACHINE := ${d:E:C/,.*//}
803# set these too in case Makefile.depend* uses them
804.if ${TARGET_SPEC_VARS:[#]} > 1
805_dtspec := ${_DEP_TARGET_SPEC:S/,/ /g}
806.for i in ${_tspec_x}
807DEP_${TARGET_SPEC_VARS:[$i]} := ${_dtspec:[$i]}
808.endfor
809.else
810DEP_MACHINE := ${_DEP_MACHINE}
811.endif
812# Warning: there is an assumption here that MACHINE is always
813# the first entry in TARGET_SPEC_VARS.
814# If TARGET_SPEC and MACHINE are insufficient, you have a problem.
815_m := ${.MAKE.DEPENDFILE_PREFERENCE:T:S;${TARGET_SPEC}$;${d:E};:C;${MACHINE}((,.+)?)$;${d:E:C/,.*//}\1;:@m@${exists(${d:R}/$m):?${d:R}/$m:}@:[1]}
816.if !empty(_m)
817# M_dep_qual_fixes isn't geared to Makefile.depend
818_qm := ${_m:C;(\.depend)$;\1.${d:E};:${M_dep_qual_fixes:ts:}}
819.if ${_debug_search}
820.info Looking for ${_qm}
821.endif
822# set this "just in case"
823# we can skip :tA since we computed the path above
824DEP_RELDIR := ${_m:H:S,^${SRCTOP}/,,}
825# and reset this
826DIRDEPS =
827.if ${_debug_reldir} && ${_qm} != ${_m}
828.info loading ${_m:S,${SRCTOP}/,,} for ${_dr}
829.endif
830.include <${_m}>
831.else
832# set these as if we found Makefile.depend*
833DEP_RELDIR := ${_dr:R}
834DIRDEPS =
835.if ${_debug_reldir}
836.info loading local.dirdeps-missing.mk for ${_dr}
837.endif
838.-include <local.dirdeps-missing.mk>
839.endif
840.endif
841.endif
842.endfor
843
844.endif				# -V
845.endif				# BUILD_DIRDEPS
846
847.elif ${.MAKE.LEVEL} > 42
848.error You should have stopped recursing by now.
849.else
850# we are building something
851DEP_RELDIR := ${RELDIR}
852_DEP_RELDIR := ${RELDIR}
853# Since we are/should be included by .MAKE.DEPENDFILE
854# This is a final opportunity to add/hook global rules.
855.-include <local.dirdeps-build.mk>
856
857# skip _reldir_{finish,failed} if not included from Makefile.depend*
858# or not in meta mode
859.if !defined(WITHOUT_META_STATS) && ${.INCLUDEDFROMFILE:U:M${.MAKE.DEPENDFILE_PREFIX}*} != "" && ${.MAKE.MODE:Mmeta} != ""
860
861meta_stats= meta=${empty(.MAKE.META.FILES):?0:${.MAKE.META.FILES:[#]}} \
862	created=${empty(.MAKE.META.CREATED):?0:${.MAKE.META.CREATED:[#]}}
863
864.if !target(_reldir_finish)
865.END: _reldir_finish
866_reldir_finish: .NOMETA
867	@echo "${TRACER}Finished ${RELDIR}.${TARGET_SPEC} seconds=$$(( ${now_utc} - ${start_utc} )) ${meta_stats}"
868.endif
869
870.if !target(_reldir_failed)
871.ERROR: _reldir_failed
872_reldir_failed: .NOMETA
873	@echo "${TRACER}Failed ${RELDIR}.${TARGET_SPEC} seconds=$$(( ${now_utc} - ${start_utc} )) ${meta_stats}"
874.endif
875
876.endif
877
878# pickup local dependencies
879.if ${MAKE_VERSION} < 20160220
880.-include <.depend>
881.else
882.dinclude <.depend>
883.endif
884.endif
885
886# bootstrapping new dependencies made easy?
887.if !target(bootstrap-empty)
888.if !target(bootstrap) && (make(bootstrap) || \
889	make(bootstrap-this) || \
890	make(bootstrap-recurse) || \
891	make(bootstrap-empty))
892
893# if we are bootstrapping create the default
894_want = ${.CURDIR}/${.MAKE.DEPENDFILE_DEFAULT:T}
895
896.if exists(${_want})
897# stop here
898${.TARGETS:Mboot*}:
899.elif !make(bootstrap-empty)
900# find a Makefile.depend to use as _src
901_src != cd ${.CURDIR} && for m in ${.MAKE.DEPENDFILE_PREFERENCE:T:S,${MACHINE},*,}; do test -s $$m || continue; echo $$m; break; done; echo
902.if empty(_src)
903.error cannot find any of ${.MAKE.DEPENDFILE_PREFERENCE:T}${.newline}Use: bootstrap-empty
904.endif
905
906_src?= ${.MAKE.DEPENDFILE}
907
908.MAKE.DEPENDFILE_BOOTSTRAP_SED+= -e 's/${_src:E:C/,.*//}/${MACHINE}/g'
909
910# just create Makefile.depend* for this dir
911bootstrap-this:	.NOTMAIN
912	@echo Bootstrapping ${RELDIR}/${_want:T} from ${_src:T}; \
913	echo You need to build ${RELDIR} to correctly populate it.
914.if ${_src:T} != ${.MAKE.DEPENDFILE_PREFIX:T}
915	(cd ${.CURDIR} && sed ${.MAKE.DEPENDFILE_BOOTSTRAP_SED} ${_src} > ${_want:T})
916.else
917	cp ${.CURDIR}/${_src:T} ${_want}
918.endif
919
920# create Makefile.depend* for this dir and its dependencies
921bootstrap: bootstrap-recurse
922bootstrap-recurse: bootstrap-this
923
924_mf := ${.PARSEFILE}
925bootstrap-recurse:	.NOTMAIN .MAKE
926	@cd ${SRCTOP} && \
927	for d in `cd ${RELDIR} && ${.MAKE} -B -f ${"${.MAKEFLAGS:M-n}":?${_src}:${.MAKE.DEPENDFILE:T}} -V DIRDEPS`; do \
928		test -d $$d || d=$${d%.*}; \
929		test -d $$d || continue; \
930		echo "Checking $$d for bootstrap ..."; \
931		(cd $$d && ${.MAKE} -f ${_mf} bootstrap-recurse); \
932	done
933
934.endif
935
936# create an empty Makefile.depend* to get the ball rolling.
937bootstrap-empty: .NOTMAIN .NOMETA
938	@echo Creating empty ${RELDIR}/${_want:T}; \
939	echo You need to build ${RELDIR} to correctly populate it.
940	@{ echo DIRDEPS=; echo ".include <dirdeps.mk>"; } > ${_want}
941
942.endif
943.endif
944