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