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