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