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