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