xref: /freebsd/share/mk/dirdeps.mk (revision 13ec1e3155c7e9bf037b12af186351b7fa9b9450)
1# $FreeBSD$
2# $Id: dirdeps.mk,v 1.147 2021/12/14 02:09:53 sjg Exp $
3
4# Copyright (c) 2010-2021, Simon J. Gerraty
5# Copyright (c) 2010-2018, Juniper Networks, Inc.
6# All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29# Much of the complexity here is for supporting cross-building.
30# If a tree does not support that, simply using plain Makefile.depend
31# should provide sufficient clue.
32# Otherwise the recommendation is to use Makefile.depend.${MACHINE}
33# as expected below.
34
35# Note: this file gets multiply included.
36# This is what we do with DIRDEPS
37
38# DIRDEPS:
39#	This is a list of directories - relative to SRCTOP, it is
40#	normally only of interest to .MAKE.LEVEL 0.
41#	In some cases the entry may be qualified with a .<machine>
42#	or .<target_spec> suffix (see TARGET_SPEC_VARS below),
43#	for example to force building something for the pseudo
44#	machines "host" or "common" regardless of current ${MACHINE}.
45#
46#	All unqualified entries end up being qualified with .${TARGET_SPEC}
47#	and partially qualified (if TARGET_SPEC_VARS has multiple
48#	entries) are also expanded to a full .<target_spec>.
49#	The  _DIRDEP_USE target uses the suffix to set TARGET_SPEC
50#	correctly when visiting each entry.
51#
52#	The fully qualified directory entries are used to construct a
53#	dependency graph that will drive the build later.
54#
55#	Also, for each fully qualified directory target, we will search
56#	using ${.MAKE.DEPENDFILE_PREFERENCE} to find additional
57#	dependencies.  We use Makefile.depend (default value for
58#	.MAKE.DEPENDFILE_PREFIX) to refer to these makefiles to
59#	distinguish them from others.
60#
61#	Before each Makefile.depend file is read, we set
62#	DEP_RELDIR to be the RELDIR (path relative to SRCTOP) for
63#	its directory, and DEP_MACHINE etc according to the .<target_spec>
64#	represented by the suffix of the corresponding target.
65#
66#	Since each Makefile.depend file includes dirdeps.mk, this
67#	processing is recursive and results in .MAKE.LEVEL 0 learning the
68#	dependencies of the tree wrt the initial directory (_DEP_RELDIR).
69#
70#	NOTE: given the extent of processing that DIRDEPS undergoes it
71#	is important that any variables in entries use :U to guard
72#	against surprises when undefined.
73#
74# TARGET_SPEC_VARS
75#	The default value is just MACHINE, and for most environments
76#	this is sufficient.  The _DIRDEP_USE target actually sets
77#	both MACHINE and TARGET_SPEC to the suffix of the current
78#	target so that in the general case TARGET_SPEC can be ignored.
79#
80#	If more than MACHINE is needed then sys.mk needs to decompose
81#	TARGET_SPEC and set the relevant variables accordingly.
82#	It is important that MACHINE be included in and actually be
83#	the first member of TARGET_SPEC_VARS.  This allows other
84#	variables to be considered optional, and some of the treatment
85#	below relies on MACHINE being the first entry.
86#	Note: TARGET_SPEC cannot contain any '.'s so the target
87#	triple used by compiler folk won't work (directly anyway).
88#
89#	For example:
90#
91#		# Always list MACHINE first,
92#		# other variables might be optional.
93#		TARGET_SPEC_VARS = MACHINE TARGET_OS
94#		.if ${TARGET_SPEC:Uno:M*,*} != ""
95#		_tspec := ${TARGET_SPEC:S/,/ /g}
96#		MACHINE := ${_tspec:[1]}
97#		TARGET_OS := ${_tspec:[2]}
98#		# etc.
99#		# We need to stop that TARGET_SPEC affecting any submakes
100#		# and deal with MACHINE=${TARGET_SPEC} in the environment.
101#		TARGET_SPEC =
102#		# export but do not track
103#		.export-env TARGET_SPEC
104#		.export ${TARGET_SPEC_VARS}
105#		.for v in ${TARGET_SPEC_VARS:O:u}
106#		.if empty($v)
107#		.undef $v
108#		.endif
109#		.endfor
110#		.endif
111#		# make sure we know what TARGET_SPEC is
112#		# as we may need it to find Makefile.depend*
113#		TARGET_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts,}
114#
115#	The following variables can influence the initial DIRDEPS
116#	computation with regard to the TARGET_SPECs that will be
117#	built.
118#	Most should also be considered by init.mk
119#
120#	ONLY_TARGET_SPEC_LIST
121#		Defines a list of TARGET_SPECs for which the current
122#		directory can be built.
123#		If ALL_MACHINES is defined, we build for all the
124#		TARGET_SPECs listed.
125#
126#	ONLY_MACHINE_LIST
127#		As for ONLY_TARGET_SPEC_LIST but only specifies
128#		MACHINEs.
129#
130#	NOT_TARGET_SPEC_LIST
131#		A list of TARGET_SPECs for which the current
132#		directory should not be built.
133#
134#	NOT_MACHINE_LIST
135#		A list of MACHINEs the current directory should not be
136#		built for.
137#
138# _build_xtra_dirs
139#	local.dirdeps.mk can add targets to this variable.
140#	They will be hooked into the build, but independent of
141#	any other DIRDEP.
142#
143#	This allows for adding TESTS to the build, such that the build
144#	if any test fails, but without the risk of introducing
145#	circular dependencies.
146
147now_utc ?= ${%s:L:gmtime}
148.if !defined(start_utc)
149start_utc := ${now_utc}
150.endif
151
152.if !target(bootstrap) && (make(bootstrap) || \
153	make(bootstrap-this) || \
154	make(bootstrap-recurse) || \
155	make(bootstrap-empty))
156# disable most of below
157.MAKE.LEVEL = 1
158.endif
159
160# touch this at your peril
161_DIRDEP_USE_LEVEL?= 0
162.if ${.MAKE.LEVEL} == ${_DIRDEP_USE_LEVEL}
163# only the first instance is interested in all this
164
165.if !target(_DIRDEP_USE)
166
167# do some setup we only need once
168_CURDIR ?= ${.CURDIR}
169_OBJDIR ?= ${.OBJDIR}
170
171.if ${MAKEFILE:T} == ${.PARSEFILE} && empty(DIRDEPS) && ${.TARGETS:Uall:M*/*} != ""
172# This little trick let's us do
173#
174# mk -f dirdeps.mk some/dir.${TARGET_SPEC}
175#
176all:
177${.TARGETS:Nall}: all
178DIRDEPS := ${.TARGETS:M*[/.]*}
179# so that -DNO_DIRDEPS works
180DEP_RELDIR := ${DIRDEPS:[1]:R}
181# this will become DEP_MACHINE below
182TARGET_MACHINE := ${DIRDEPS:[1]:E:C/,.*//}
183.if ${TARGET_MACHINE:N*/*} == ""
184TARGET_MACHINE := ${MACHINE}
185.endif
186# disable DIRDEPS_CACHE as it does not like this trick
187MK_DIRDEPS_CACHE = no
188.endif
189
190# make sure we get the behavior we expect
191.MAKE.SAVE_DOLLARS = no
192
193# make sure these are empty to start with
194_DEP_TARGET_SPEC =
195
196# If TARGET_SPEC_VARS is other than just MACHINE
197# it should be set by sys.mk or similar by now.
198# TARGET_SPEC must not contain any '.'s.
199TARGET_SPEC_VARS ?= MACHINE
200# this is what we started with
201TARGET_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts,}
202# this is what we mostly use below
203DEP_TARGET_SPEC = ${TARGET_SPEC_VARS:S,^,DEP_,:@v@${$v:U}@:ts,}
204# make sure we have defaults
205.for v in ${TARGET_SPEC_VARS}
206DEP_$v ?= ${$v}
207.endfor
208
209.if ${TARGET_SPEC_VARS:[#]} > 1
210# Ok, this gets more complex (putting it mildly).
211# In order to stay sane, we need to ensure that all the build_dirs
212# we compute below are fully qualified wrt DEP_TARGET_SPEC.
213# The makefiles may only partially specify (eg. MACHINE only),
214# so we need to construct a set of modifiers to fill in the gaps.
215.if ${MAKE_VERSION} >= 20170130
216_tspec_x := ${TARGET_SPEC_VARS:range}
217.else
218# do it the hard way
219_tspec_x := ${TARGET_SPEC_VARS:[#]:@x@i=1;while [ $$i -le $x ]; do echo $$i; i=$$((i + 1)); done;@:sh}
220.endif
221# this handles unqualified entries
222M_dep_qual_fixes = C;(/[^/.,]+)$$;\1.$${DEP_TARGET_SPEC};
223# there needs to be at least one item missing for these to make sense
224.for i in ${_tspec_x:[2..-1]}
225_tspec_m$i := ${TARGET_SPEC_VARS:[2..$i]:@w@[^,]+@:ts,}
226_tspec_a$i := ,${TARGET_SPEC_VARS:[$i..-1]:@v@$$$${DEP_$v}@:ts,}
227M_dep_qual_fixes += C;(\.${_tspec_m$i})$$;\1${_tspec_a$i};
228.endfor
229TARGET_SPEC_VARSr := ${TARGET_SPEC_VARS:[-1..1]}
230.else
231# A harmless? default.
232M_dep_qual_fixes = U
233.endif
234
235.if !defined(.MAKE.DEPENDFILE_PREFERENCE)
236# .MAKE.DEPENDFILE_PREFERENCE makes the logic below neater?
237# you really want this set by sys.mk or similar
238.MAKE.DEPENDFILE_PREFERENCE = ${_CURDIR}/${.MAKE.DEPENDFILE:T}
239.if ${.MAKE.DEPENDFILE:E} == "${TARGET_SPEC}"
240.if ${TARGET_SPEC} != ${MACHINE}
241.MAKE.DEPENDFILE_PREFERENCE += ${_CURDIR}/${.MAKE.DEPENDFILE:T:R}.$${MACHINE}
242.endif
243.MAKE.DEPENDFILE_PREFERENCE += ${_CURDIR}/${.MAKE.DEPENDFILE:T:R}
244.endif
245.endif
246
247_default_dependfile := ${.MAKE.DEPENDFILE_PREFERENCE:[1]:T}
248_machine_dependfiles := ${.MAKE.DEPENDFILE_PREFERENCE:T:M*${MACHINE}*}
249
250# for machine specific dependfiles we require ${MACHINE} to be at the end
251# also for the sake of sanity we require a common prefix
252.if !defined(.MAKE.DEPENDFILE_PREFIX)
253# knowing .MAKE.DEPENDFILE_PREFIX helps
254.if !empty(_machine_dependfiles)
255.MAKE.DEPENDFILE_PREFIX := ${_machine_dependfiles:[1]:T:R}
256.else
257.MAKE.DEPENDFILE_PREFIX := ${_default_dependfile:T}
258.endif
259.endif
260
261
262# this is how we identify non-machine specific dependfiles
263N_notmachine := ${.MAKE.DEPENDFILE_PREFERENCE:E:N*${MACHINE}*:${M_ListToSkip}}
264
265.endif				# !target(_DIRDEP_USE)
266
267# First off, we want to know what ${MACHINE} to build for.
268# This can be complicated if we are using a mixture of ${MACHINE} specific
269# and non-specific Makefile.depend*
270
271# if we were included recursively _DEP_TARGET_SPEC should be valid.
272.if empty(_DEP_TARGET_SPEC)
273DEP_MACHINE = ${TARGET_MACHINE:U${MACHINE}}
274_DEP_TARGET_SPEC := ${DEP_TARGET_SPEC}
275.if ${.INCLUDEDFROMFILE:U:M${.MAKE.DEPENDFILE_PREFIX}*} != ""
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_build_xtra_dirs =
300
301# the first time we are included the _DIRDEP_USE target will not be defined
302# we can use this as a clue to do initialization and other one time things.
303.if !target(_DIRDEP_USE)
304# make sure this target exists
305dirdeps: beforedirdeps .WAIT
306beforedirdeps:
307
308# We normally expect to be included by Makefile.depend.*
309# which sets the DEP_* macros below.
310DEP_RELDIR ?= ${RELDIR}
311
312# this can cause lots of output!
313# set to a set of glob expressions that might match RELDIR
314DEBUG_DIRDEPS ?= no
315
316# remember the initial value of DEP_RELDIR - we test for it below.
317_DEP_RELDIR := ${DEP_RELDIR}
318
319.endif
320
321# DIRDEPS_CACHE can be very handy for debugging.
322# Also if repeatedly building the same target,
323# we can avoid the overhead of re-computing the tree dependencies.
324MK_DIRDEPS_CACHE ?= no
325BUILD_DIRDEPS_CACHE ?= no
326BUILD_DIRDEPS ?= yes
327
328.if ${MK_DIRDEPS_CACHE} == "yes"
329# this is where we will cache all our work
330DIRDEPS_CACHE ?= ${_OBJDIR:tA}/dirdeps.cache${_TARGETS:U${.TARGETS}:Nall:O:u:ts-:S,/,_,g:S,^,.,:N.}
331.endif
332
333.if ${DEBUG_DIRDEPS:@x@${DEP_RELDIR:M$x}${${DEP_RELDIR}.${DEP_MACHINE}:L:M$x}@} != ""
334_debug_reldir = 1
335.else
336_debug_reldir = 0
337.endif
338.if ${DEBUG_DIRDEPS:@x@${DEP_RELDIR:M$x}${${DEP_RELDIR}.depend:L:M$x}@} != ""
339_debug_search = 1
340.else
341_debug_search = 0
342.endif
343
344# pickup customizations
345# as below you can use !target(_DIRDEP_USE) to protect things
346# which should only be done once.
347.-include <local.dirdeps.mk>
348
349.if !target(_DIRDEP_USE)
350# things we skip for host tools
351SKIP_HOSTDIR ?=
352
353NSkipHostDir = ${SKIP_HOSTDIR:N*.host*:S,$,.host*,:N.host*:S,^,${SRCTOP}/,:${M_ListToSkip}}
354
355# things we always skip
356# SKIP_DIRDEPS allows for adding entries on command line.
357SKIP_DIR += .host *.WAIT ${SKIP_DIRDEPS}
358SKIP_DIR.host += ${SKIP_HOSTDIR}
359
360DEP_SKIP_DIR = ${SKIP_DIR} \
361	${SKIP_DIR.${DEP_TARGET_SPEC}:U} \
362	${TARGET_SPEC_VARS:@v@${SKIP_DIR.${DEP_$v}:U}@} \
363	${SKIP_DIRDEPS.${DEP_TARGET_SPEC}:U} \
364	${TARGET_SPEC_VARS:@v@${SKIP_DIRDEPS.${DEP_$v}:U}@}
365
366
367NSkipDir = ${DEP_SKIP_DIR:${M_ListToSkip}}
368
369.if defined(NODIRDEPS) || defined(WITHOUT_DIRDEPS)
370NO_DIRDEPS =
371.elif defined(WITHOUT_DIRDEPS_BELOW)
372NO_DIRDEPS_BELOW =
373.endif
374
375.if defined(NO_DIRDEPS)
376# confine ourselves to the original dir and below.
377DIRDEPS_FILTER += M${_DEP_RELDIR}*
378.elif defined(NO_DIRDEPS_BELOW)
379DIRDEPS_FILTER += M${_DEP_RELDIR}
380.endif
381
382# this is what we run below
383DIRDEP_MAKE ?= ${.MAKE}
384DIRDEP_DIR ?= ${.TARGET:R}
385
386# if you want us to report load averages during build
387# DIRDEP_USE_PRELUDE += ${DIRDEP_LOADAVG_REPORT};
388
389DIRDEP_LOADAVG_CMD ?= ${UPTIME:Uuptime} | sed 's,.*\(load\),\1,'
390DIRDEP_LOADAVG_LAST = 0
391# yes the expression here is a bit complicated,
392# the trick is to only eval ${DIRDEP_LOADAVG_LAST::=${now_utc}}
393# when we want to report.
394# Note: expr(1) will exit 1 if the expression evaluates to 0
395# hence the  || true
396DIRDEP_LOADAVG_REPORT = \
397	test -z "${"${expr ${now_utc} - ${DIRDEP_LOADAVG_INTEVAL:U60} - ${DIRDEP_LOADAVG_LAST} || true:L:sh:N-*}":?yes${DIRDEP_LOADAVG_LAST::=${now_utc}}:}" || \
398	echo "${TRACER}`${DIRDEP_LOADAVG_CMD}`"
399
400# we suppress SUBDIR when visiting the leaves
401# we assume sys.mk will set MACHINE_ARCH
402# you can add extras to DIRDEP_USE_ENV
403# if there is no makefile in the target directory, we skip it.
404_DIRDEP_USE:	.USE .MAKE
405	@for m in ${.MAKE.MAKEFILE_PREFERENCE}; do \
406		test -s ${.TARGET:R}/$$m || continue; \
407		echo "${TRACER}Checking ${.TARGET:S,^${SRCTOP}/,,} for ${.TARGET:E} ..."; \
408		${DIRDEP_USE_PRELUDE} \
409		MACHINE_ARCH= NO_SUBDIR=1 ${DIRDEP_USE_ENV} \
410		TARGET_SPEC=${.TARGET:E} \
411		MACHINE=${.TARGET:E} \
412		${DIRDEP_MAKE} -C ${DIRDEP_DIR} || exit 1; \
413		break; \
414	done
415
416.ifdef ALL_MACHINES
417.if empty(ONLY_TARGET_SPEC_LIST) && empty(ONLY_MACHINE_LIST)
418# we start with everything
419_machine_list != echo; 'ls' -1 ${_CURDIR}/${.MAKE.DEPENDFILE_PREFIX}* 2> /dev/null
420
421# some things we know we want to ignore
422DIRDEPS_TARGETS_SKIP_LIST += \
423	*~ \
424	*.bak \
425	*.inc \
426	*.old \
427	*.options \
428	*.orig \
429	*.rej \
430
431# first trim things we know we want to skip
432# and provide canonical form
433_machine_list := ${_machine_list:${DIRDEPS_TARGETS_SKIP_LIST:${M_ListToSkip}}:T:E}
434
435# cater for local complexities
436# local.dirdeps.mk can set
437# DIRDEPS_ALL_MACHINES_FILTER and
438# DIRDEPS_ALL_MACHINES_FILTER_XTRAS for final tweaks
439
440.if !empty(ALL_TARGET_SPEC_LIST)
441.if ${_debug_reldir}
442.info ALL_TARGET_SPEC_LIST=${ALL_TARGET_SPEC_LIST}
443.endif
444DIRDEPS_ALL_MACHINES_FILTER += \
445	@x@$${ALL_TARGET_SPEC_LIST:@s@$${x:M$$s}@}@
446.elif !empty(ALL_MACHINE_LIST)
447.if ${_debug_reldir}
448.info ALL_MACHINE_LIST=${ALL_MACHINE_LIST}
449.endif
450.if ${TARGET_SPEC_VARS:[#]} > 1
451# the space below can result in extraneous ':'
452DIRDEPS_ALL_MACHINES_FILTER += \
453	@x@$${ALL_MACHINE_LIST:@m@$${x:M$$m,*} $${x:M$$m}@}@
454.else
455DIRDEPS_ALL_MACHINES_FILTER += \
456	@x@$${ALL_MACHINE_LIST:@m@$${x:M$$m}@}@
457.endif
458.endif
459# add local XTRAS - default to something benign
460DIRDEPS_ALL_MACHINES_FILTER += \
461	${DIRDEPS_ALL_MACHINES_FILTER_XTRAS:UNbak}
462
463.if ${_debug_reldir}
464.info _machine_list=${_machine_list}
465.info DIRDEPS_ALL_MACHINES_FILTER=${DIRDEPS_ALL_MACHINES_FILTER}
466.endif
467
468_only_machines := ${_machine_list:${DIRDEPS_ALL_MACHINES_FILTER:ts:}:S,:, ,g}
469.else
470_only_machines := ${ONLY_TARGET_SPEC_LIST:U} ${ONLY_MACHINE_LIST:U}
471.endif
472
473.if empty(_only_machines)
474# we must be boot-strapping
475_only_machines := ${TARGET_MACHINE:U${ALL_TARGET_SPEC_LIST:U${ALL_MACHINE_LIST:U${DEP_MACHINE}}}}
476.endif
477
478# cleanup the result
479_only_machines := ${_only_machines:O:u}
480
481.if ${_debug_reldir}
482.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: ALL_MACHINES _only_machines=${_only_machines}
483.endif
484
485.else				# ! ALL_MACHINES
486# if ONLY_TARGET_SPEC_LIST or ONLY_MACHINE_LIST is set, we are limited to that.
487# Note that ONLY_TARGET_SPEC_LIST should be fully qualified.
488# if TARGET_MACHINE is set - it is really the same as ONLY_MACHINE_LIST
489# otherwise DEP_MACHINE is it - so DEP_MACHINE will match.
490_only_machines := ${ONLY_TARGET_SPEC_LIST:U:M${DEP_MACHINE},*}
491.if empty(_only_machines)
492_only_machines := ${ONLY_MACHINE_LIST:U${TARGET_MACHINE:U${DEP_MACHINE}}:M${DEP_MACHINE}}
493.endif
494.endif
495
496.if !empty(NOT_MACHINE_LIST)
497_only_machines := ${_only_machines:${NOT_MACHINE_LIST:${M_ListToSkip}}}
498.endif
499.if !empty(NOT_TARGET_SPEC_LIST)
500# we must first qualify
501_dm := ${DEP_MACHINE}
502_only_machines := ${_only_machines:M*,*} ${_only_machines:N*,*:@DEP_MACHINE@${DEP_TARGET_SPEC}@:S,^,.,:${M_dep_qual_fixes:ts:}:O:u:S,^.,,}
503DEP_MACHINE := ${_dm}
504_only_machines := ${_only_machines:${NOT_TARGET_SPEC_LIST:${M_ListToSkip}}}
505.endif
506# clean up
507_only_machines := ${_only_machines:O:u}
508
509.if ${_debug_reldir}
510.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: _only_machines=${_only_machines}
511.endif
512
513# make sure we have a starting place?
514DIRDEPS ?= ${RELDIR}
515.endif				# target
516
517.if !defined(NO_DIRDEPS) && !defined(NO_DIRDEPS_BELOW)
518.if ${MK_DIRDEPS_CACHE} == "yes"
519
520# just ensure this exists
521build-dirdeps:
522
523M_oneperline = @x@\\${.newline}	$$x@
524
525.if ${BUILD_DIRDEPS_CACHE} == "no"
526.if !target(dirdeps-cached)
527# we do this via sub-make
528BUILD_DIRDEPS = no
529
530# ignore anything but these
531.MAKE.META.IGNORE_FILTER = M*/${.MAKE.DEPENDFILE_PREFIX}*
532
533dirdeps: dirdeps-cached
534dirdeps-cached:	${DIRDEPS_CACHE} .MAKE
535	@echo "${TRACER}Using ${DIRDEPS_CACHE}"
536	@MAKELEVEL=${.MAKE.LEVEL} ${.MAKE} -C ${_CURDIR} -f ${DIRDEPS_CACHE} \
537		dirdeps MK_DIRDEPS_CACHE=no BUILD_DIRDEPS=no
538
539# leaf makefiles rarely work for building DIRDEPS_CACHE
540.if ${RELDIR} != "."
541BUILD_DIRDEPS_MAKEFILE ?= -f dirdeps.mk
542.endif
543
544# these should generally do
545BUILD_DIRDEPS_MAKEFILE ?=
546BUILD_DIRDEPS_TARGETS ?= ${.TARGETS}
547
548.if ${DIRDEPS_CACHE} != ${STATIC_DIRDEPS_CACHE:Uno} && ${DIRDEPS_CACHE:M${SRCTOP}/*} == ""
549# export this for dirdeps-cache-update.mk
550DYNAMIC_DIRDEPS_CACHE := ${DIRDEPS_CACHE}
551.export DYNAMIC_DIRDEPS_CACHE
552# we need the .meta file to ensure we update if
553# any of the Makefile.depend* changed.
554# We do not want to compare the command line though.
555${DIRDEPS_CACHE}:	.META .NOMETA_CMP
556	+@{ echo '# Autogenerated - do NOT edit!'; echo; \
557	echo 'BUILD_DIRDEPS=no'; echo; \
558	echo '.include <dirdeps.mk>'; echo; \
559	} > ${.TARGET}.new
560	+@MAKELEVEL=${.MAKE.LEVEL} DIRDEPS_CACHE=${DIRDEPS_CACHE} \
561	DIRDEPS="${DIRDEPS}" \
562	TARGET_SPEC=${TARGET_SPEC} \
563	MAKEFLAGS= ${DIRDEP_CACHE_MAKE:U${.MAKE}} -C ${_CURDIR} \
564	${BUILD_DIRDEPS_MAKEFILE} \
565	${BUILD_DIRDEPS_TARGETS} BUILD_DIRDEPS_CACHE=yes \
566	.MAKE.DEPENDFILE=.none \
567	${"${DEBUG_DIRDEPS:Nno}":?DEBUG_DIRDEPS='${DEBUG_DIRDEPS}':} \
568	${.MAKEFLAGS:tW:S,-D ,-D,g:tw:M*WITH*} \
569	${.MAKEFLAGS:tW:S,-d ,-d,g:tw:M-d*} \
570	3>&1 1>&2 | sed 's,${SRCTOP},_{SRCTOP},g;s,_{,$${,g' >> ${.TARGET}.new && \
571	mv ${.TARGET}.new ${.TARGET}
572
573.endif
574.endif
575.elif !target(_count_dirdeps)
576# we want to capture the dirdeps count in the cache
577.END: _count_dirdeps
578_count_dirdeps: .NOMETA
579	@{ echo; echo '.info $${.newline}$${TRACER}Makefiles read: total=${.MAKE.MAKEFILES:[#]} depend=${.MAKE.MAKEFILES:M*depend*:[#]} dirdeps=${.ALLTARGETS:M${SRCTOP}*:O:u:[#]} ${DIRDEP_INFO_XTRAS}'; } >&3
580
581.endif
582.elif !make(dirdeps) && !target(_count_dirdeps)
583beforedirdeps: _count_dirdeps
584_count_dirdeps: .NOMETA
585	@echo "${TRACER}Makefiles read: total=${.MAKE.MAKEFILES:[#]} depend=${.MAKE.MAKEFILES:M*depend*:[#]} dirdeps=${.ALLTARGETS:M${SRCTOP}*:O:u:[#]} ${DIRDEP_INFO_XTRAS} seconds=`expr ${now_utc} - ${start_utc}`"
586
587.endif
588.endif
589
590.if ${BUILD_DIRDEPS} == "yes"
591
592# the rest is done repeatedly for every Makefile.depend we read.
593# if we are anything but the original dir we care only about the
594# machine type we were included for..
595
596.if ${DEP_RELDIR} == "."
597_this_dir := ${SRCTOP}
598.else
599_this_dir := ${SRCTOP}/${DEP_RELDIR}
600.endif
601
602# on rare occasions, there can be a need for extra help
603_dep_hack := ${_this_dir}/${.MAKE.DEPENDFILE_PREFIX}.inc
604.-include <${_dep_hack}>
605.-include <${_dep_hack:R}.options>
606
607.if ${DEP_RELDIR} != ${_DEP_RELDIR} || ${DEP_TARGET_SPEC} != ${TARGET_SPEC}
608# this should be all
609_machines := ${DEP_MACHINE}
610.else
611# this is the machine list we actually use below
612_machines := ${_only_machines}
613
614.if defined(HOSTPROG) || ${DEP_MACHINE:Nhost*} == ""
615# we need to build this guy's dependencies for host as well.
616.if ${DEP_MACHINE:Nhost*} == ""
617_machines += ${DEP_MACHINE}
618.else
619_machines += host
620.endif
621.endif
622
623_machines := ${_machines:O:u}
624.endif
625
626.if ${TARGET_SPEC_VARS:[#]} > 1
627# we need to tweak _machines
628_dm := ${DEP_MACHINE}
629# apply the same filtering that we do when qualifying DIRDEPS.
630# M_dep_qual_fixes expects .${MACHINE}* so add (and remove) '.'
631# Again we expect that any already qualified machines are fully qualified.
632_machines := ${_machines:M*,*} ${_machines:N*,*:@DEP_MACHINE@${DEP_TARGET_SPEC}@:S,^,.,:${M_dep_qual_fixes:ts:}:O:u:S,^.,,}
633DEP_MACHINE := ${_dm}
634_machines := ${_machines:O:u}
635.endif
636
637# reset each time through
638_build_dirs =
639
640.if ${DEP_RELDIR} == ${_DEP_RELDIR}
641# pickup other machines for this dir if necessary
642_build_dirs += ${_machines:@m@${_CURDIR}.$m@}
643.endif
644
645.if ${_debug_reldir}
646.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: nDIRDEPS=${DIRDEPS:[#]}
647.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: DIRDEPS='${DIRDEPS}'
648.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: _machines='${_machines}'
649.endif
650
651.if !empty(DIRDEPS)
652# these we reset each time through as they can depend on DEP_MACHINE
653DEP_DIRDEPS_FILTER = \
654	${DIRDEPS_FILTER.${DEP_TARGET_SPEC}:U} \
655	${TARGET_SPEC_VARS:@v@${DIRDEPS_FILTER.${DEP_$v}:U}@} \
656	${DIRDEPS_FILTER:U}
657.if empty(DEP_DIRDEPS_FILTER)
658# something harmless
659DEP_DIRDEPS_FILTER = U
660.endif
661
662# this is what we start with
663__depdirs := ${DIRDEPS:${NSkipDir}:${DEP_DIRDEPS_FILTER:ts:}:C,//+,/,g:O:u:@d@${SRCTOP}/$d@}
664
665# some entries may be qualified with .<machine> or .<target_spec>
666# we can tell the unqualified ones easily - because they exist
667__unqual_depdirs := ${__depdirs:@d@${exists($d):?$d:}@}
668__qual_depdirs := ${__depdirs:${__unqual_depdirs:Uno:${M_ListToSkip}}}
669
670.if ${DEP_RELDIR} == ${_DEP_RELDIR}
671# if it was called out - we likely need it.
672__hostdpadd := ${DPADD:U.:M${HOST_OBJTOP}/*:S,${HOST_OBJTOP}/,,:H:${NSkipDir}:${DIRDEPS_FILTER:ts:}:S,$,.host,:N.*:@d@${SRCTOP}/$d@} \
673	${DPADD:U.:M${HOST_OBJTOP32:Uno}/*:S,${HOST_OBJTOP32:Uno}/,,:H:${NSkipDir}:${DIRDEPS_FILTER:ts:}:S,$,.host32,:N.*:@d@${SRCTOP}/$d@}
674__qual_depdirs += ${__hostdpadd}
675.endif
676
677.if ${_debug_reldir}
678.info DEP_DIRDEPS_FILTER=${DEP_DIRDEPS_FILTER:ts:}
679.info depdirs=${__depdirs:S,^${SRCTOP}/,,}
680.info qualified=${__qual_depdirs:S,^${SRCTOP}/,,}
681.info unqualified=${__unqual_depdirs:S,^${SRCTOP}/,,}
682.endif
683
684# _build_dirs is what we will feed to _DIRDEP_USE
685_build_dirs += \
686	${__qual_depdirs:M*.host:${NSkipHostDir}:N.host} \
687	${__qual_depdirs:N*.host} \
688	${_machines:Mhost*:@m@${__unqual_depdirs:@d@$d.$m@}@:${NSkipHostDir}:N.host} \
689	${_machines:Nhost*:@m@${__unqual_depdirs:@d@$d.$m@}@}
690
691# qualify everything now
692_build_dirs := ${_build_dirs:${M_dep_qual_fixes:ts:}:O:u}
693
694.endif				# empty DIRDEPS
695
696_build_all_dirs += ${_build_dirs} ${_build_xtra_dirs}
697_build_all_dirs := ${_build_all_dirs:O:u}
698
699# Normally if doing make -V something,
700# we do not want to waste time chasing DIRDEPS
701# but if we want to count the number of Makefile.depend* read, we do.
702.if ${.MAKEFLAGS:M-V${_V_READ_DIRDEPS:U}} == ""
703.if !empty(_build_all_dirs)
704.if ${BUILD_DIRDEPS_CACHE} == "yes"
705x!= echo; { echo; echo '\# ${DEP_RELDIR}.${DEP_TARGET_SPEC}'; } >&3
706# guard against _new_dirdeps being too big for a single command line
707_new_dirdeps := ${_build_all_dirs:@x@${target($x):?:$x}@:S,^${SRCTOP}/,,}
708_cache_xtra_deps := ${_build_xtra_dirs:S,^${SRCTOP}/,,}
709.export _cache_xtra_deps _new_dirdeps
710.if !empty(DEP_EXPORT_VARS)
711# Discouraged, but there are always exceptions.
712# Handle it here rather than explain how.
713x!= echo; { echo; ${DEP_EXPORT_VARS:@v@echo '$v=${$v}';@} echo '.export ${DEP_EXPORT_VARS}'; echo; } >&3
714.endif
715.else
716# this makes it all happen
717dirdeps: ${_build_all_dirs}
718.endif
719${_build_all_dirs}:	_DIRDEP_USE
720
721.if ${_debug_reldir}
722.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: needs: ${_build_dirs:S,^${SRCTOP}/,,}
723.endif
724
725.if !empty(DEP_EXPORT_VARS)
726.export ${DEP_EXPORT_VARS}
727DEP_EXPORT_VARS=
728.endif
729
730# this builds the dependency graph
731.for m in ${_machines}
732.if ${BUILD_DIRDEPS_CACHE} == "yes" && !empty(_build_dirs)
733_cache_deps =
734x!= echo; { echo; echo 'DIRDEPS.${_this_dir}.$m = \'; } >&3
735.endif
736# it would be nice to do :N${.TARGET}
737.if !empty(__qual_depdirs)
738.for q in ${__qual_depdirs:${M_dep_qual_fixes:ts:}:E:O:u:N$m}
739.if ${_debug_reldir} || ${DEBUG_DIRDEPS:@x@${${DEP_RELDIR}.$m:L:M$x}${${DEP_RELDIR}.$q:L:M$x}@} != ""
740.info ${DEP_RELDIR}.$m: graph: ${_build_dirs:M*.$q:S,^${SRCTOP}/,,}
741.endif
742.if ${BUILD_DIRDEPS_CACHE} == "yes"
743_cache_deps += ${_build_dirs:M*.$q:S,^${SRCTOP}/,,}
744.else
745${_this_dir}.$m: ${_build_dirs:M*.$q}
746.endif
747.endfor
748.endif
749.if ${_debug_reldir}
750.info ${DEP_RELDIR}.$m: graph: ${_build_dirs:M*.$m:N${_this_dir}.$m:S,^${SRCTOP}/,,}
751.endif
752.if ${BUILD_DIRDEPS_CACHE} == "yes"
753.if !empty(_build_dirs)
754_cache_deps += ${_build_dirs:M*.$m:N${_this_dir}.$m:S,^${SRCTOP}/,,}
755.if !empty(_cache_deps)
756.export _cache_deps
757x!= echo; for x in $$_cache_deps; do echo "	_{SRCTOP}/$$x \\"; done >&3
758.endif
759# anything in _{build,env}_xtra_dirs is hooked to dirdeps: only
760x!= echo; { echo; echo '${_this_dir}.$m: $${DIRDEPS.${_this_dir}.$m}'; \
761	echo; echo 'dirdeps: ${_this_dir}.$m \'; \
762	for x in $$_cache_xtra_deps; do echo "	_{SRCTOP}/$$x \\"; done; \
763	echo; for x in $$_new_dirdeps; do echo "_{SRCTOP}/$$x: _DIRDEP_USE"; done; } >&3
764.endif
765.else
766${_this_dir}.$m: ${_build_dirs:M*.$m:N${_this_dir}.$m}
767.endif
768.endfor
769
770.endif
771
772# Now find more dependencies - and recurse.
773.for d in ${_build_all_dirs}
774.if !target(_dirdeps_checked.$d)
775# once only
776_dirdeps_checked.$d:
777.if ${_debug_search}
778.info checking ${d:S,^${SRCTOP}/,,}
779.endif
780# Note: _build_all_dirs is fully qualifed so d:R is always the directory
781.if exists(${d:R})
782# we pass _DEP_TARGET_SPEC to tell the next step what we want
783_DEP_TARGET_SPEC := ${d:E}
784# some makefiles may still look at this
785_DEP_MACHINE := ${d:E:C/,.*//}
786# set these too in case Makefile.depend* uses them
787.if ${TARGET_SPEC_VARS:[#]} > 1
788_dtspec := ${_DEP_TARGET_SPEC:S/,/ /g}
789.for i in ${_tspec_x}
790DEP_${TARGET_SPEC_VARS:[$i]} := ${_dtspec:[$i]}
791.endfor
792.else
793DEP_MACHINE := ${_DEP_MACHINE}
794.endif
795# Warning: there is an assumption here that MACHINE is always
796# the first entry in TARGET_SPEC_VARS.
797# If TARGET_SPEC and MACHINE are insufficient, you have a problem.
798_m := ${.MAKE.DEPENDFILE_PREFERENCE:T:S;${TARGET_SPEC}$;${d:E};:C;${MACHINE}((,.+)?)$;${d:E:C/,.*//}\1;:@m@${exists(${d:R}/$m):?${d:R}/$m:}@:[1]}
799.if !empty(_m)
800# M_dep_qual_fixes isn't geared to Makefile.depend
801_qm := ${_m:C;(\.depend)$;\1.${d:E};:${M_dep_qual_fixes:ts:}}
802.if ${_debug_search}
803.info Looking for ${_qm}
804.endif
805# set this "just in case"
806# we can skip :tA since we computed the path above
807DEP_RELDIR := ${_m:H:S,^${SRCTOP}/,,}
808# and reset this
809DIRDEPS =
810.if ${_debug_reldir} && ${_qm} != ${_m}
811.info loading ${_m} for ${d:E}
812.endif
813.include <${_m}>
814.else
815.-include <local.dirdeps-missing.mk>
816.endif
817.endif
818.endif
819.endfor
820
821.endif				# -V
822.endif				# BUILD_DIRDEPS
823
824.elif ${.MAKE.LEVEL} > 42
825.error You should have stopped recursing by now.
826.else
827# we are building something
828DEP_RELDIR := ${RELDIR}
829_DEP_RELDIR := ${RELDIR}
830# Since we are/should be included by .MAKE.DEPENDFILE
831# This is a final opportunity to add/hook global rules.
832.-include <local.dirdeps-build.mk>
833
834# skip _reldir_{finish,failed} if not included from Makefile.depend*
835# or not in meta mode
836.if !defined(WITHOUT_META_STATS) && ${.INCLUDEDFROMFILE:U:M${.MAKE.DEPENDFILE_PREFIX}*} != "" && ${.MAKE.MODE:Mmeta} != ""
837
838meta_stats= meta=${empty(.MAKE.META.FILES):?0:${.MAKE.META.FILES:[#]}} \
839	created=${empty(.MAKE.META.CREATED):?0:${.MAKE.META.CREATED:[#]}}
840
841.if !target(_reldir_finish)
842.END: _reldir_finish
843_reldir_finish: .NOMETA
844	@echo "${TRACER}Finished ${RELDIR}.${TARGET_SPEC} seconds=$$(( ${now_utc} - ${start_utc} )) ${meta_stats}"
845.endif
846
847.if !target(_reldir_failed)
848.ERROR: _reldir_failed
849_reldir_failed: .NOMETA
850	@echo "${TRACER}Failed ${RELDIR}.${TARGET_SPEC} seconds=$$(( ${now_utc} - ${start_utc} )) ${meta_stats}"
851.endif
852
853.endif
854
855# pickup local dependencies
856.if ${MAKE_VERSION} < 20160220
857.-include <.depend>
858.else
859.dinclude <.depend>
860.endif
861.endif
862
863# bootstrapping new dependencies made easy?
864.if !target(bootstrap-empty)
865.if !target(bootstrap) && (make(bootstrap) || \
866	make(bootstrap-this) || \
867	make(bootstrap-recurse) || \
868	make(bootstrap-empty))
869
870# if we are bootstrapping create the default
871_want = ${.CURDIR}/${.MAKE.DEPENDFILE_DEFAULT:T}
872
873.if exists(${_want})
874# stop here
875${.TARGETS:Mboot*}:
876.elif !make(bootstrap-empty)
877# find a Makefile.depend to use as _src
878_src != cd ${.CURDIR} && for m in ${.MAKE.DEPENDFILE_PREFERENCE:T:S,${MACHINE},*,}; do test -s $$m || continue; echo $$m; break; done; echo
879.if empty(_src)
880.error cannot find any of ${.MAKE.DEPENDFILE_PREFERENCE:T}${.newline}Use: bootstrap-empty
881.endif
882
883_src?= ${.MAKE.DEPENDFILE}
884
885.MAKE.DEPENDFILE_BOOTSTRAP_SED+= -e 's/${_src:E:C/,.*//}/${MACHINE}/g'
886
887# just create Makefile.depend* for this dir
888bootstrap-this:	.NOTMAIN
889	@echo Bootstrapping ${RELDIR}/${_want:T} from ${_src:T}; \
890	echo You need to build ${RELDIR} to correctly populate it.
891.if ${_src:T} != ${.MAKE.DEPENDFILE_PREFIX:T}
892	(cd ${.CURDIR} && sed ${.MAKE.DEPENDFILE_BOOTSTRAP_SED} ${_src} > ${_want:T})
893.else
894	cp ${.CURDIR}/${_src:T} ${_want}
895.endif
896
897# create Makefile.depend* for this dir and its dependencies
898bootstrap: bootstrap-recurse
899bootstrap-recurse: bootstrap-this
900
901_mf := ${.PARSEFILE}
902bootstrap-recurse:	.NOTMAIN .MAKE
903	@cd ${SRCTOP} && \
904	for d in `cd ${RELDIR} && ${.MAKE} -B -f ${"${.MAKEFLAGS:M-n}":?${_src}:${.MAKE.DEPENDFILE:T}} -V DIRDEPS`; do \
905		test -d $$d || d=$${d%.*}; \
906		test -d $$d || continue; \
907		echo "Checking $$d for bootstrap ..."; \
908		(cd $$d && ${.MAKE} -f ${_mf} bootstrap-recurse); \
909	done
910
911.endif
912
913# create an empty Makefile.depend* to get the ball rolling.
914bootstrap-empty: .NOTMAIN .NOMETA
915	@echo Creating empty ${RELDIR}/${_want:T}; \
916	echo You need to build ${RELDIR} to correctly populate it.
917	@{ echo DIRDEPS=; echo ".include <dirdeps.mk>"; } > ${_want}
918
919.endif
920.endif
921