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