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