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