xref: /freebsd/contrib/bmake/mk/dirdeps.mk (revision 8d5c8e21c690b35d0a9a604d6b886fba222cd2fe)
1*8d5c8e21SSimon J. Gerraty# $Id: dirdeps.mk,v 1.170 2024/06/24 02:21:00 sjg Exp $
23cbdda60SSimon J. Gerraty
38c973ee2SSimon J. Gerraty# SPDX-License-Identifier: BSD-2-Clause
48c973ee2SSimon J. Gerraty#
58c973ee2SSimon J. Gerraty# Copyright (c) 2010-2023, Simon J. Gerraty
649caa483SSimon J. Gerraty# Copyright (c) 2010-2018, Juniper Networks, Inc.
71748de26SSimon J. Gerraty# All rights reserved.
83cbdda60SSimon J. Gerraty#
93cbdda60SSimon J. Gerraty# Redistribution and use in source and binary forms, with or without
103cbdda60SSimon J. Gerraty# modification, are permitted provided that the following conditions
113cbdda60SSimon J. Gerraty# are met:
123cbdda60SSimon J. Gerraty# 1. Redistributions of source code must retain the above copyright
133cbdda60SSimon J. Gerraty#    notice, this list of conditions and the following disclaimer.
143cbdda60SSimon J. Gerraty# 2. Redistributions in binary form must reproduce the above copyright
153cbdda60SSimon J. Gerraty#    notice, this list of conditions and the following disclaimer in the
163cbdda60SSimon J. Gerraty#    documentation and/or other materials provided with the distribution.
173cbdda60SSimon J. Gerraty#
183cbdda60SSimon J. Gerraty# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
193cbdda60SSimon J. Gerraty# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
203cbdda60SSimon J. Gerraty# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
213cbdda60SSimon J. Gerraty# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
223cbdda60SSimon J. Gerraty# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
233cbdda60SSimon J. Gerraty# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
243cbdda60SSimon J. Gerraty# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
253cbdda60SSimon J. Gerraty# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
263cbdda60SSimon J. Gerraty# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
273cbdda60SSimon J. Gerraty# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
283cbdda60SSimon J. Gerraty# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
293cbdda60SSimon J. Gerraty
303cbdda60SSimon J. Gerraty# Much of the complexity here is for supporting cross-building.
313cbdda60SSimon J. Gerraty# If a tree does not support that, simply using plain Makefile.depend
323cbdda60SSimon J. Gerraty# should provide sufficient clue.
333cbdda60SSimon J. Gerraty# Otherwise the recommendation is to use Makefile.depend.${MACHINE}
343cbdda60SSimon J. Gerraty# as expected below.
353cbdda60SSimon J. Gerraty
363cbdda60SSimon J. Gerraty# Note: this file gets multiply included.
373cbdda60SSimon J. Gerraty# This is what we do with DIRDEPS
383cbdda60SSimon J. Gerraty
393cbdda60SSimon J. Gerraty# DIRDEPS:
401748de26SSimon J. Gerraty#	This is a list of directories - relative to SRCTOP, it is
411748de26SSimon J. Gerraty#	normally only of interest to .MAKE.LEVEL 0.
423cbdda60SSimon J. Gerraty#	In some cases the entry may be qualified with a .<machine>
431748de26SSimon J. Gerraty#	or .<target_spec> suffix (see TARGET_SPEC_VARS below),
441748de26SSimon J. Gerraty#	for example to force building something for the pseudo
453cbdda60SSimon J. Gerraty#	machines "host" or "common" regardless of current ${MACHINE}.
461748de26SSimon J. Gerraty#
471748de26SSimon J. Gerraty#	All unqualified entries end up being qualified with .${TARGET_SPEC}
481748de26SSimon J. Gerraty#	and partially qualified (if TARGET_SPEC_VARS has multiple
491748de26SSimon J. Gerraty#	entries) are also expanded to a full .<target_spec>.
505bcb7424SSimon J. Gerraty#	The  _DIRDEP_USE target uses the suffix to set TARGET_SPEC
513cbdda60SSimon J. Gerraty#	correctly when visiting each entry.
523cbdda60SSimon J. Gerraty#
531748de26SSimon J. Gerraty#	The fully qualified directory entries are used to construct a
541748de26SSimon J. Gerraty#	dependency graph that will drive the build later.
551748de26SSimon J. Gerraty#
561748de26SSimon J. Gerraty#	Also, for each fully qualified directory target, we will search
571748de26SSimon J. Gerraty#	using ${.MAKE.DEPENDFILE_PREFERENCE} to find additional
581748de26SSimon J. Gerraty#	dependencies.  We use Makefile.depend (default value for
591748de26SSimon J. Gerraty#	.MAKE.DEPENDFILE_PREFIX) to refer to these makefiles to
601748de26SSimon J. Gerraty#	distinguish them from others.
611748de26SSimon J. Gerraty#
6245447996SSimon J. Gerraty#	Before each Makefile.depend file is read, we set
63b46b9039SSimon J. Gerraty#	DEP_RELDIR to be the RELDIR (path relative to SRCTOP) for
6445447996SSimon J. Gerraty#	its directory, and DEP_MACHINE etc according to the .<target_spec>
6545447996SSimon J. Gerraty#	represented by the suffix of the corresponding target.
6645447996SSimon J. Gerraty#
6745447996SSimon J. Gerraty#	Since each Makefile.depend file includes dirdeps.mk, this
683cbdda60SSimon J. Gerraty#	processing is recursive and results in .MAKE.LEVEL 0 learning the
693cbdda60SSimon J. Gerraty#	dependencies of the tree wrt the initial directory (_DEP_RELDIR).
703cbdda60SSimon J. Gerraty#
7112904384SSimon J. Gerraty#	NOTE: given the extent of processing that DIRDEPS undergoes it
7212904384SSimon J. Gerraty#	is important that any variables in entries use :U to guard
7312904384SSimon J. Gerraty#	against surprises when undefined.
7412904384SSimon J. Gerraty#
753cbdda60SSimon J. Gerraty# TARGET_SPEC_VARS
761748de26SSimon J. Gerraty#	The default value is just MACHINE, and for most environments
775bcb7424SSimon J. Gerraty#	this is sufficient.  The _DIRDEP_USE target actually sets
781748de26SSimon J. Gerraty#	both MACHINE and TARGET_SPEC to the suffix of the current
791748de26SSimon J. Gerraty#	target so that in the general case TARGET_SPEC can be ignored.
803cbdda60SSimon J. Gerraty#
811748de26SSimon J. Gerraty#	If more than MACHINE is needed then sys.mk needs to decompose
823cbdda60SSimon J. Gerraty#	TARGET_SPEC and set the relevant variables accordingly.
831748de26SSimon J. Gerraty#	It is important that MACHINE be included in and actually be
841748de26SSimon J. Gerraty#	the first member of TARGET_SPEC_VARS.  This allows other
851748de26SSimon J. Gerraty#	variables to be considered optional, and some of the treatment
861748de26SSimon J. Gerraty#	below relies on MACHINE being the first entry.
873cbdda60SSimon J. Gerraty#	Note: TARGET_SPEC cannot contain any '.'s so the target
881748de26SSimon J. Gerraty#	triple used by compiler folk won't work (directly anyway).
893cbdda60SSimon J. Gerraty#
903cbdda60SSimon J. Gerraty#	For example:
913cbdda60SSimon J. Gerraty#
921748de26SSimon J. Gerraty#		# Always list MACHINE first,
931748de26SSimon J. Gerraty#		# other variables might be optional.
943cbdda60SSimon J. Gerraty#		TARGET_SPEC_VARS = MACHINE TARGET_OS
953cbdda60SSimon J. Gerraty#		.if ${TARGET_SPEC:Uno:M*,*} != ""
963cbdda60SSimon J. Gerraty#		_tspec := ${TARGET_SPEC:S/,/ /g}
973cbdda60SSimon J. Gerraty#		MACHINE := ${_tspec:[1]}
983cbdda60SSimon J. Gerraty#		TARGET_OS := ${_tspec:[2]}
993cbdda60SSimon J. Gerraty#		# etc.
1001748de26SSimon J. Gerraty#		# We need to stop that TARGET_SPEC affecting any submakes
1011748de26SSimon J. Gerraty#		# and deal with MACHINE=${TARGET_SPEC} in the environment.
1021748de26SSimon J. Gerraty#		TARGET_SPEC =
1031748de26SSimon J. Gerraty#		# export but do not track
1041748de26SSimon J. Gerraty#		.export-env TARGET_SPEC
1051748de26SSimon J. Gerraty#		.export ${TARGET_SPEC_VARS}
1063cbdda60SSimon J. Gerraty#		.for v in ${TARGET_SPEC_VARS:O:u}
1073cbdda60SSimon J. Gerraty#		.if empty($v)
1083cbdda60SSimon J. Gerraty#		.undef $v
1093cbdda60SSimon J. Gerraty#		.endif
1103cbdda60SSimon J. Gerraty#		.endfor
1113cbdda60SSimon J. Gerraty#		.endif
1121748de26SSimon J. Gerraty#		# make sure we know what TARGET_SPEC is
1131748de26SSimon J. Gerraty#		# as we may need it to find Makefile.depend*
1141748de26SSimon J. Gerraty#		TARGET_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts,}
1153cbdda60SSimon J. Gerraty#
11645447996SSimon J. Gerraty#	The following variables can influence the initial DIRDEPS
11745447996SSimon J. Gerraty#	computation with regard to the TARGET_SPECs that will be
11845447996SSimon J. Gerraty#	built.
11945447996SSimon J. Gerraty#	Most should also be considered by init.mk
12045447996SSimon J. Gerraty#
12145447996SSimon J. Gerraty#	ONLY_TARGET_SPEC_LIST
12245447996SSimon J. Gerraty#		Defines a list of TARGET_SPECs for which the current
12345447996SSimon J. Gerraty#		directory can be built.
12445447996SSimon J. Gerraty#		If ALL_MACHINES is defined, we build for all the
12545447996SSimon J. Gerraty#		TARGET_SPECs listed.
12645447996SSimon J. Gerraty#
12745447996SSimon J. Gerraty#	ONLY_MACHINE_LIST
12845447996SSimon J. Gerraty#		As for ONLY_TARGET_SPEC_LIST but only specifies
12945447996SSimon J. Gerraty#		MACHINEs.
13045447996SSimon J. Gerraty#
13145447996SSimon J. Gerraty#	NOT_TARGET_SPEC_LIST
13245447996SSimon J. Gerraty#		A list of TARGET_SPECs for which the current
13345447996SSimon J. Gerraty#		directory should not be built.
13445447996SSimon J. Gerraty#
13545447996SSimon J. Gerraty#	NOT_MACHINE_LIST
13645447996SSimon J. Gerraty#		A list of MACHINEs the current directory should not be
13745447996SSimon J. Gerraty#		built for.
13845447996SSimon J. Gerraty#
1399f45a3c8SSimon J. Gerraty# DIRDEPS_EXPORT_VARS (DEP_EXPORT_VARS)
1409f45a3c8SSimon J. Gerraty#	It is discouraged, but sometimes necessary for a
1419f45a3c8SSimon J. Gerraty#	Makefile.depend file to influence the environment.
142*8d5c8e21SSimon J. Gerraty#	Doing this correctly (especially if using DIRDEPS_CACHE) is
1439f45a3c8SSimon J. Gerraty#	tricky so a Makefile.depend file can set DIRDEPS_EXPORT_VARS
1449f45a3c8SSimon J. Gerraty#	and dirdeps.mk will do the deed:
1459f45a3c8SSimon J. Gerraty#
1469f45a3c8SSimon J. Gerraty#		MK_UEFI = yes
1479f45a3c8SSimon J. Gerraty#		DIRDEPS_EXPORT_VARS = MK_UEFI
1489f45a3c8SSimon J. Gerraty#
1492c3632d1SSimon J. Gerraty# _build_xtra_dirs
1502c3632d1SSimon J. Gerraty#	local.dirdeps.mk can add targets to this variable.
1512c3632d1SSimon J. Gerraty#	They will be hooked into the build, but independent of
1522c3632d1SSimon J. Gerraty#	any other DIRDEP.
1532c3632d1SSimon J. Gerraty#
1542c3632d1SSimon J. Gerraty#	This allows for adding TESTS to the build, such that the build
1552c3632d1SSimon J. Gerraty#	if any test fails, but without the risk of introducing
1562c3632d1SSimon J. Gerraty#	circular dependencies.
1572c3632d1SSimon J. Gerraty
15898875883SSimon J. Gerratynow_utc ?= ${%s:L:localtime}
1592c3632d1SSimon J. Gerraty.if !defined(start_utc)
1602c3632d1SSimon J. Gerratystart_utc := ${now_utc}
1612c3632d1SSimon J. Gerraty.endif
1623cbdda60SSimon J. Gerraty
163cac6fd11SSimon J. Gerraty.if !target(bootstrap) && (make(bootstrap) || \
164cac6fd11SSimon J. Gerraty	make(bootstrap-this) || \
165cac6fd11SSimon J. Gerraty	make(bootstrap-recurse) || \
166cac6fd11SSimon J. Gerraty	make(bootstrap-empty))
167cac6fd11SSimon J. Gerraty# disable most of below
168cac6fd11SSimon J. Gerraty.MAKE.LEVEL = 1
169cac6fd11SSimon J. Gerraty.endif
170cac6fd11SSimon J. Gerraty
171db29cad8SSimon J. Gerraty# touch this at your peril
172db29cad8SSimon J. Gerraty_DIRDEP_USE_LEVEL?= 0
173db29cad8SSimon J. Gerraty.if ${.MAKE.LEVEL} == ${_DIRDEP_USE_LEVEL}
1743cbdda60SSimon J. Gerraty# only the first instance is interested in all this
1753cbdda60SSimon J. Gerraty
1769f45a3c8SSimon J. Gerraty# the first time we are included the _DIRDEP_USE target will not be defined
1779f45a3c8SSimon J. Gerraty# we can use this as a clue to do initialization and other one time things.
1783cbdda60SSimon J. Gerraty.if !target(_DIRDEP_USE)
179e48f47ddSSimon J. Gerraty
18095e3ed2cSSimon J. Gerraty# do some setup we only need once
18195e3ed2cSSimon J. Gerraty_CURDIR ?= ${.CURDIR}
18295e3ed2cSSimon J. Gerraty_OBJDIR ?= ${.OBJDIR}
18395e3ed2cSSimon J. Gerraty
1848c973ee2SSimon J. Gerraty.if ${MAKEFILE:T} == ${.PARSEFILE} && empty(DIRDEPS) && ${.TARGETS:Uall:M*[/.]*} != ""
185e48f47ddSSimon J. Gerraty# This little trick let's us do
186e48f47ddSSimon J. Gerraty#
187e48f47ddSSimon J. Gerraty# mk -f dirdeps.mk some/dir.${TARGET_SPEC}
188e48f47ddSSimon J. Gerraty#
189e48f47ddSSimon J. Gerratyall:
190e48f47ddSSimon J. Gerraty${.TARGETS:Nall}: all
191e48f47ddSSimon J. GerratyDIRDEPS := ${.TARGETS:M*[/.]*}
192e48f47ddSSimon J. Gerraty# so that -DNO_DIRDEPS works
193e48f47ddSSimon J. GerratyDEP_RELDIR := ${DIRDEPS:[1]:R}
194e48f47ddSSimon J. Gerraty# this will become DEP_MACHINE below
195e48f47ddSSimon J. GerratyTARGET_MACHINE := ${DIRDEPS:[1]:E:C/,.*//}
196e48f47ddSSimon J. Gerraty.if ${TARGET_MACHINE:N*/*} == ""
197e48f47ddSSimon J. GerratyTARGET_MACHINE := ${MACHINE}
198e48f47ddSSimon J. Gerraty.endif
199e48f47ddSSimon J. Gerraty# disable DIRDEPS_CACHE as it does not like this trick
200e48f47ddSSimon J. GerratyMK_DIRDEPS_CACHE = no
2019f45a3c8SSimon J. Gerraty# incase anyone needs to know
2029f45a3c8SSimon J. Gerraty_dirdeps_cmdline:
203e48f47ddSSimon J. Gerraty.endif
204e48f47ddSSimon J. Gerraty
205be19d90bSSimon J. Gerraty# make sure we get the behavior we expect
206be19d90bSSimon J. Gerraty.MAKE.SAVE_DOLLARS = no
207be19d90bSSimon J. Gerraty
2081748de26SSimon J. Gerraty# make sure these are empty to start with
2091748de26SSimon J. Gerraty_DEP_TARGET_SPEC =
2101748de26SSimon J. Gerraty
2113cbdda60SSimon J. Gerraty# If TARGET_SPEC_VARS is other than just MACHINE
2123cbdda60SSimon J. Gerraty# it should be set by sys.mk or similar by now.
2133cbdda60SSimon J. Gerraty# TARGET_SPEC must not contain any '.'s.
2143cbdda60SSimon J. GerratyTARGET_SPEC_VARS ?= MACHINE
215148ee845SSimon J. Gerraty# we allow for this to be a subset
216148ee845SSimon J. GerratyTARGET_SPEC_VARS.host ?= MACHINE
217148ee845SSimon J. GerratyTARGET_SPEC_VARS.host32 = ${TARGET_SPEC_VARS.host}
2181748de26SSimon J. Gerraty# this is what we started with
2193cbdda60SSimon J. GerratyTARGET_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts,}
2201748de26SSimon J. Gerraty# this is what we mostly use below
221148ee845SSimon J. GerratyDEP_TARGET_SPEC_VARS = ${TARGET_SPEC_VARS.${DEP_MACHINE}:U${TARGET_SPEC_VARS}}
222148ee845SSimon J. GerratyDEP_TARGET_SPEC = ${DEP_TARGET_SPEC_VARS:S,^,DEP_,:@v@${$v:U}@:ts,}
2231748de26SSimon J. Gerraty# make sure we have defaults
224148ee845SSimon J. Gerraty.for v in ${DEP_TARGET_SPEC_VARS}
2251748de26SSimon J. GerratyDEP_$v ?= ${$v}
2261748de26SSimon J. Gerraty.endfor
2271748de26SSimon J. Gerraty
2281748de26SSimon J. Gerraty.if ${TARGET_SPEC_VARS:[#]} > 1
2291748de26SSimon J. Gerraty# Ok, this gets more complex (putting it mildly).
2301748de26SSimon J. Gerraty# In order to stay sane, we need to ensure that all the build_dirs
2311748de26SSimon J. Gerraty# we compute below are fully qualified wrt DEP_TARGET_SPEC.
2321748de26SSimon J. Gerraty# The makefiles may only partially specify (eg. MACHINE only),
2331748de26SSimon J. Gerraty# so we need to construct a set of modifiers to fill in the gaps.
234148ee845SSimon J. Gerraty_tspec_x := ${TARGET_SPEC_VARS:${M_RANGE:Urange}}
2351748de26SSimon J. Gerraty# this handles unqualified entries
2360dede8b0SSimon J. GerratyM_dep_qual_fixes = C;(/[^/.,]+)$$;\1.$${DEP_TARGET_SPEC};
2371748de26SSimon J. Gerraty# there needs to be at least one item missing for these to make sense
2381748de26SSimon J. Gerraty.for i in ${_tspec_x:[2..-1]}
2391748de26SSimon J. Gerraty_tspec_m$i := ${TARGET_SPEC_VARS:[2..$i]:@w@[^,]+@:ts,}
2400dede8b0SSimon J. Gerraty_tspec_a$i := ,${TARGET_SPEC_VARS:[$i..-1]:@v@$$$${DEP_$v}@:ts,}
2411748de26SSimon J. GerratyM_dep_qual_fixes += C;(\.${_tspec_m$i})$$;\1${_tspec_a$i};
2421748de26SSimon J. Gerraty.endfor
2432c3632d1SSimon J. GerratyTARGET_SPEC_VARSr := ${TARGET_SPEC_VARS:[-1..1]}
244148ee845SSimon J. Gerraty.if ${TARGET_SPEC_VARS.host} == ${TARGET_SPEC_VARS}
245148ee845SSimon J. GerratyM_dep_qual_fixes.host = ${M_dep_qual_fixes}
246148ee845SSimon J. Gerraty.elif ${TARGET_SPEC_VARS.host:[#]} > 1
247148ee845SSimon J. Gerraty_htspec_x := ${TARGET_SPEC_VARS.host:${M_RANGE:Urange}}
248148ee845SSimon J. Gerraty# this handles unqualified entries
249148ee845SSimon J. GerratyM_dep_qual_fixes.host = C;(/[^/.,]+)$$;\1.$${DEP_TARGET_SPEC};
250148ee845SSimon J. Gerraty# there needs to be at least one item missing for these to make sense
251148ee845SSimon J. Gerraty.for i in ${_htspec_x:[2..-1]}
252148ee845SSimon J. Gerraty_htspec_m$i := ${TARGET_SPEC_VARS.host:[2..$i]:@w@[^,]+@:ts,}
253148ee845SSimon J. Gerraty_htspec_a$i := ,${TARGET_SPEC_VARS.host:[$i..-1]:@v@$$$${DEP_$v}@:ts,}
254148ee845SSimon J. GerratyM_dep_qual_fixes.host += C;(\.${_htspec_m$i})$$;\1${_htspec_a$i};
255148ee845SSimon J. Gerraty.endfor
256148ee845SSimon J. Gerraty.else
257148ee845SSimon J. GerratyM_dep_qual_fixes.host = U
258148ee845SSimon J. Gerraty.endif
2591748de26SSimon J. Gerraty.else
2601748de26SSimon J. Gerraty# A harmless? default.
2611748de26SSimon J. GerratyM_dep_qual_fixes = U
2621748de26SSimon J. Gerraty.endif
263148ee845SSimon J. GerratyM_dep_qual_fixes.host ?= ${M_dep_qual_fixes}
264148ee845SSimon J. GerratyM_dep_qual_fixes.host32 = ${M_dep_qual_fixes.host}
2653cbdda60SSimon J. Gerraty
2663cbdda60SSimon J. Gerraty.if !defined(.MAKE.DEPENDFILE_PREFERENCE)
2671748de26SSimon J. Gerraty# .MAKE.DEPENDFILE_PREFERENCE makes the logic below neater?
2681748de26SSimon J. Gerraty# you really want this set by sys.mk or similar
2693cbdda60SSimon J. Gerraty.MAKE.DEPENDFILE_PREFERENCE = ${_CURDIR}/${.MAKE.DEPENDFILE:T}
2703cbdda60SSimon J. Gerraty.if ${.MAKE.DEPENDFILE:E} == "${TARGET_SPEC}"
2713cbdda60SSimon J. Gerraty.if ${TARGET_SPEC} != ${MACHINE}
2723cbdda60SSimon J. Gerraty.MAKE.DEPENDFILE_PREFERENCE += ${_CURDIR}/${.MAKE.DEPENDFILE:T:R}.$${MACHINE}
2733cbdda60SSimon J. Gerraty.endif
2743cbdda60SSimon J. Gerraty.MAKE.DEPENDFILE_PREFERENCE += ${_CURDIR}/${.MAKE.DEPENDFILE:T:R}
2753cbdda60SSimon J. Gerraty.endif
2763cbdda60SSimon J. Gerraty.endif
2773cbdda60SSimon J. Gerraty
2783cbdda60SSimon J. Gerraty_default_dependfile := ${.MAKE.DEPENDFILE_PREFERENCE:[1]:T}
2791748de26SSimon J. Gerraty_machine_dependfiles := ${.MAKE.DEPENDFILE_PREFERENCE:T:M*${MACHINE}*}
2803cbdda60SSimon J. Gerraty
2813cbdda60SSimon J. Gerraty# for machine specific dependfiles we require ${MACHINE} to be at the end
2823cbdda60SSimon J. Gerraty# also for the sake of sanity we require a common prefix
2833cbdda60SSimon J. Gerraty.if !defined(.MAKE.DEPENDFILE_PREFIX)
2841748de26SSimon J. Gerraty# knowing .MAKE.DEPENDFILE_PREFIX helps
2853cbdda60SSimon J. Gerraty.if !empty(_machine_dependfiles)
2863cbdda60SSimon J. Gerraty.MAKE.DEPENDFILE_PREFIX := ${_machine_dependfiles:[1]:T:R}
2873cbdda60SSimon J. Gerraty.else
2883cbdda60SSimon J. Gerraty.MAKE.DEPENDFILE_PREFIX := ${_default_dependfile:T}
2893cbdda60SSimon J. Gerraty.endif
2903cbdda60SSimon J. Gerraty.endif
2913cbdda60SSimon J. Gerraty
292c1d01b5fSSimon J. Gerraty# turn a list into a set of :N modifiers
293c1d01b5fSSimon J. Gerraty# NskipFoo = ${Foo:${M_ListToSkip}}
294c1d01b5fSSimon J. GerratyM_ListToSkip ?= O:u:S,^,N,:ts:
295c1d01b5fSSimon J. Gerraty
2963cbdda60SSimon J. Gerraty# this is how we identify non-machine specific dependfiles
2971748de26SSimon J. GerratyN_notmachine := ${.MAKE.DEPENDFILE_PREFERENCE:E:N*${MACHINE}*:${M_ListToSkip}}
2983cbdda60SSimon J. Gerraty
2999f45a3c8SSimon J. Gerraty# this gets reset for each dirdep we check
3009f45a3c8SSimon J. GerratyDEP_RELDIR ?= ${RELDIR}
3019f45a3c8SSimon J. Gerraty
3029f45a3c8SSimon J. Gerraty# remember the initial value of DEP_RELDIR - we test for it below.
3039f45a3c8SSimon J. Gerraty_DEP_RELDIR := ${DEP_RELDIR}
3049f45a3c8SSimon J. Gerraty
3059f45a3c8SSimon J. Gerraty# this can cause lots of output!
3069f45a3c8SSimon J. Gerraty# set to a set of glob expressions that might match RELDIR
3079f45a3c8SSimon J. GerratyDEBUG_DIRDEPS ?= no
3089f45a3c8SSimon J. Gerraty
3099f45a3c8SSimon J. Gerraty# make sure this target exists
3109f45a3c8SSimon J. Gerratydirdeps: beforedirdeps .WAIT
3119f45a3c8SSimon J. Gerratybeforedirdeps:
3129f45a3c8SSimon J. Gerraty
3133cbdda60SSimon J. Gerraty.endif				# !target(_DIRDEP_USE)
3143cbdda60SSimon J. Gerraty
3159f45a3c8SSimon J. Gerraty.if ${DEBUG_DIRDEPS:@x@${DEP_RELDIR:M$x}${${DEP_RELDIR}.${DEP_MACHINE}:L:M$x}@} != ""
3169f45a3c8SSimon J. Gerraty_debug_reldir = 1
3179f45a3c8SSimon J. Gerraty.else
3189f45a3c8SSimon J. Gerraty_debug_reldir = 0
3199f45a3c8SSimon J. Gerraty.endif
3209f45a3c8SSimon J. Gerraty.if ${DEBUG_DIRDEPS:@x@${DEP_RELDIR:M$x}${${DEP_RELDIR}.depend depend:L:M$x}@} != ""
3219f45a3c8SSimon J. Gerraty_debug_search = 1
3229f45a3c8SSimon J. Gerraty.else
3239f45a3c8SSimon J. Gerraty_debug_search = 0
3249f45a3c8SSimon J. Gerraty.endif
3259f45a3c8SSimon J. Gerraty
32695e3ed2cSSimon J. Gerraty# First off, we want to know what ${MACHINE} to build for.
32795e3ed2cSSimon J. Gerraty# This can be complicated if we are using a mixture of ${MACHINE} specific
32895e3ed2cSSimon J. Gerraty# and non-specific Makefile.depend*
32995e3ed2cSSimon J. Gerraty
3301748de26SSimon J. Gerraty# if we were included recursively _DEP_TARGET_SPEC should be valid.
3311748de26SSimon J. Gerraty.if empty(_DEP_TARGET_SPEC)
332dba7b0efSSimon J. GerratyDEP_MACHINE = ${TARGET_MACHINE:U${MACHINE}}
333dba7b0efSSimon J. Gerraty_DEP_TARGET_SPEC := ${DEP_TARGET_SPEC}
33406b9b3e0SSimon J. Gerraty.if ${.INCLUDEDFROMFILE:U:M${.MAKE.DEPENDFILE_PREFIX}*} != ""
3351748de26SSimon J. Gerraty# record that we've read dependfile for this
3363bebe729SSimon J. Gerraty_dirdeps_checked.${_CURDIR}.${TARGET_SPEC}:
3371748de26SSimon J. Gerraty.endif
3381748de26SSimon J. Gerraty.endif
3391748de26SSimon J. Gerraty
3401748de26SSimon J. Gerraty# by now _DEP_TARGET_SPEC should be set, parse it.
3411748de26SSimon J. Gerraty.if ${TARGET_SPEC_VARS:[#]} > 1
3421748de26SSimon J. Gerraty# we need to parse DEP_MACHINE may or may not contain more info
3431748de26SSimon J. Gerraty_tspec := ${_DEP_TARGET_SPEC:S/,/ /g}
3441748de26SSimon J. Gerraty.for i in ${_tspec_x}
3451748de26SSimon J. GerratyDEP_${TARGET_SPEC_VARS:[$i]} := ${_tspec:[$i]}
3461748de26SSimon J. Gerraty.endfor
347148ee845SSimon J. Gerraty.for v in ${DEP_TARGET_SPEC_VARS:O:u}
3481748de26SSimon J. Gerraty.if empty(DEP_$v)
3491748de26SSimon J. Gerraty.undef DEP_$v
3501748de26SSimon J. Gerraty.endif
3511748de26SSimon J. Gerraty.endfor
3521748de26SSimon J. Gerraty.else
3531748de26SSimon J. GerratyDEP_MACHINE := ${_DEP_TARGET_SPEC}
3543cbdda60SSimon J. Gerraty.endif
3553cbdda60SSimon J. Gerraty
356be19d90bSSimon J. Gerraty# reset each time through
357be19d90bSSimon J. Gerraty_build_all_dirs =
3582c3632d1SSimon J. Gerraty_build_xtra_dirs =
3593cbdda60SSimon J. Gerraty
36045447996SSimon J. Gerraty# DIRDEPS_CACHE can be very handy for debugging.
36145447996SSimon J. Gerraty# Also if repeatedly building the same target,
36245447996SSimon J. Gerraty# we can avoid the overhead of re-computing the tree dependencies.
36345447996SSimon J. GerratyMK_DIRDEPS_CACHE ?= no
36445447996SSimon J. GerratyBUILD_DIRDEPS_CACHE ?= no
36545447996SSimon J. GerratyBUILD_DIRDEPS ?= yes
36645447996SSimon J. Gerraty
36745447996SSimon J. Gerraty.if ${MK_DIRDEPS_CACHE} == "yes"
36845447996SSimon J. Gerraty# this is where we will cache all our work
3692c3632d1SSimon J. GerratyDIRDEPS_CACHE ?= ${_OBJDIR:tA}/dirdeps.cache${_TARGETS:U${.TARGETS}:Nall:O:u:ts-:S,/,_,g:S,^,.,:N.}
37045447996SSimon J. Gerraty.endif
37145447996SSimon J. Gerraty
3728c973ee2SSimon J. Gerraty# sanity check: Makefile.depend.options should *not* include us
3738c973ee2SSimon J. Gerraty.if ${.INCLUDEDFROMFILE:U:M${.MAKE.DEPENDFILE_PREFIX}.options} != ""
3748c973ee2SSimon J. Gerraty.error ${DEP_RELDIR}/${.MAKE.DEPENDFILE_PREFIX}.options: should include dirdeps-options.mk
3758c973ee2SSimon J. Gerraty.endif
37649caa483SSimon J. Gerraty
377be19d90bSSimon J. Gerraty# pickup customizations
378be19d90bSSimon J. Gerraty# as below you can use !target(_DIRDEP_USE) to protect things
379be19d90bSSimon J. Gerraty# which should only be done once.
380e48f47ddSSimon J. Gerraty.-include <local.dirdeps.mk>
381be19d90bSSimon J. Gerraty
382be19d90bSSimon J. Gerraty.if !target(_DIRDEP_USE)
3833cbdda60SSimon J. Gerraty# things we skip for host tools
3843cbdda60SSimon J. GerratySKIP_HOSTDIR ?=
3853cbdda60SSimon J. Gerraty
386db29cad8SSimon J. GerratyNSkipHostDir = ${SKIP_HOSTDIR:N*.host*:S,$,.host*,:N.host*:S,^,${SRCTOP}/,:${M_ListToSkip}}
3873cbdda60SSimon J. Gerraty
3883cbdda60SSimon J. Gerraty# things we always skip
3893cbdda60SSimon J. Gerraty# SKIP_DIRDEPS allows for adding entries on command line.
3903cbdda60SSimon J. GerratySKIP_DIR += .host *.WAIT ${SKIP_DIRDEPS}
3911748de26SSimon J. GerratySKIP_DIR.host += ${SKIP_HOSTDIR}
3923cbdda60SSimon J. Gerraty
3931748de26SSimon J. GerratyDEP_SKIP_DIR = ${SKIP_DIR} \
3941748de26SSimon J. Gerraty	${SKIP_DIR.${DEP_TARGET_SPEC}:U} \
39545447996SSimon J. Gerraty	${TARGET_SPEC_VARS:@v@${SKIP_DIR.${DEP_$v}:U}@} \
39645447996SSimon J. Gerraty	${SKIP_DIRDEPS.${DEP_TARGET_SPEC}:U} \
39745447996SSimon J. Gerraty	${TARGET_SPEC_VARS:@v@${SKIP_DIRDEPS.${DEP_$v}:U}@}
39845447996SSimon J. Gerraty
3993cbdda60SSimon J. Gerraty
4001748de26SSimon J. GerratyNSkipDir = ${DEP_SKIP_DIR:${M_ListToSkip}}
4013cbdda60SSimon J. Gerraty
402e48f47ddSSimon J. Gerraty.if defined(NODIRDEPS) || defined(WITHOUT_DIRDEPS)
403e48f47ddSSimon J. GerratyNO_DIRDEPS =
404e48f47ddSSimon J. Gerraty.elif defined(WITHOUT_DIRDEPS_BELOW)
405e48f47ddSSimon J. GerratyNO_DIRDEPS_BELOW =
406e48f47ddSSimon J. Gerraty.endif
407e48f47ddSSimon J. Gerraty
408e48f47ddSSimon J. Gerraty.if defined(NO_DIRDEPS)
4093bebe729SSimon J. Gerraty# confine ourselves to the original dir and below.
4103cbdda60SSimon J. GerratyDIRDEPS_FILTER += M${_DEP_RELDIR}*
4113bebe729SSimon J. Gerraty.elif defined(NO_DIRDEPS_BELOW)
4123bebe729SSimon J. GerratyDIRDEPS_FILTER += M${_DEP_RELDIR}
4133cbdda60SSimon J. Gerraty.endif
4143cbdda60SSimon J. Gerraty
4155bcb7424SSimon J. Gerraty# this is what we run below
4165bcb7424SSimon J. GerratyDIRDEP_MAKE ?= ${.MAKE}
41749caa483SSimon J. GerratyDIRDEP_DIR ?= ${.TARGET:R}
41898875883SSimon J. Gerraty# we normally want the default target
41998875883SSimon J. GerratyDIRDEP_TARGETS ?=
4205bcb7424SSimon J. Gerraty
4212c3632d1SSimon J. Gerraty# if you want us to report load averages during build
4222c3632d1SSimon J. Gerraty# DIRDEP_USE_PRELUDE += ${DIRDEP_LOADAVG_REPORT};
4232c3632d1SSimon J. Gerraty
4242c3632d1SSimon J. GerratyDIRDEP_LOADAVG_CMD ?= ${UPTIME:Uuptime} | sed 's,.*\(load\),\1,'
4252c3632d1SSimon J. GerratyDIRDEP_LOADAVG_LAST = 0
4262c3632d1SSimon J. Gerraty# yes the expression here is a bit complicated,
4272c3632d1SSimon J. Gerraty# the trick is to only eval ${DIRDEP_LOADAVG_LAST::=${now_utc}}
4282c3632d1SSimon J. Gerraty# when we want to report.
429dba7b0efSSimon J. Gerraty# Note: expr(1) will exit 1 if the expression evaluates to 0
430dba7b0efSSimon J. Gerraty# hence the  || true
4312c3632d1SSimon J. GerratyDIRDEP_LOADAVG_REPORT = \
4324fde40d9SSimon J. Gerraty	test -z "${"${expr ${now_utc} - ${DIRDEP_LOADAVG_INTERVAL:U60} - ${DIRDEP_LOADAVG_LAST} || true:L:sh:N-*}":?yes${DIRDEP_LOADAVG_LAST::=${now_utc}}:}" || \
4332c3632d1SSimon J. Gerraty	echo "${TRACER}`${DIRDEP_LOADAVG_CMD}`"
4342c3632d1SSimon J. Gerraty
4355bcb7424SSimon J. Gerraty# we suppress SUBDIR when visiting the leaves
4363cbdda60SSimon J. Gerraty# we assume sys.mk will set MACHINE_ARCH
4371748de26SSimon J. Gerraty# you can add extras to DIRDEP_USE_ENV
4381748de26SSimon J. Gerraty# if there is no makefile in the target directory, we skip it.
4393cbdda60SSimon J. Gerraty_DIRDEP_USE:	.USE .MAKE
4403cbdda60SSimon J. Gerraty	@for m in ${.MAKE.MAKEFILE_PREFERENCE}; do \
4413cbdda60SSimon J. Gerraty		test -s ${.TARGET:R}/$$m || continue; \
44212904384SSimon J. Gerraty		echo "${TRACER}Checking ${.TARGET:S,^${SRCTOP}/,,} for ${.TARGET:E} ..."; \
44349caa483SSimon J. Gerraty		${DIRDEP_USE_PRELUDE} \
4441748de26SSimon J. Gerraty		MACHINE_ARCH= NO_SUBDIR=1 ${DIRDEP_USE_ENV} \
4453cbdda60SSimon J. Gerraty		TARGET_SPEC=${.TARGET:E} \
4461748de26SSimon J. Gerraty		MACHINE=${.TARGET:E} \
44798875883SSimon J. Gerraty		${DIRDEP_MAKE} -C ${DIRDEP_DIR} ${DIRDEP_TARGETS} || exit 1; \
4483cbdda60SSimon J. Gerraty		break; \
4493cbdda60SSimon J. Gerraty	done
4503cbdda60SSimon J. Gerraty
4513cbdda60SSimon J. Gerraty.ifdef ALL_MACHINES
45245447996SSimon J. Gerraty.if empty(ONLY_TARGET_SPEC_LIST) && empty(ONLY_MACHINE_LIST)
453b0c40a00SSimon J. Gerraty# we start with everything
454b0c40a00SSimon J. Gerraty_machine_list != echo; 'ls' -1 ${_CURDIR}/${.MAKE.DEPENDFILE_PREFIX}* 2> /dev/null
455b0c40a00SSimon J. Gerraty
456b0c40a00SSimon J. Gerraty# some things we know we want to ignore
457b0c40a00SSimon J. GerratyDIRDEPS_TARGETS_SKIP_LIST += \
458b0c40a00SSimon J. Gerraty	*~ \
459b0c40a00SSimon J. Gerraty	*.bak \
460b0c40a00SSimon J. Gerraty	*.inc \
461b0c40a00SSimon J. Gerraty	*.old \
462b0c40a00SSimon J. Gerraty	*.options \
463b0c40a00SSimon J. Gerraty	*.orig \
464b0c40a00SSimon J. Gerraty	*.rej \
465b0c40a00SSimon J. Gerraty
466b0c40a00SSimon J. Gerraty# first trim things we know we want to skip
467b0c40a00SSimon J. Gerraty# and provide canonical form
468b0c40a00SSimon J. Gerraty_machine_list := ${_machine_list:${DIRDEPS_TARGETS_SKIP_LIST:${M_ListToSkip}}:T:E}
469b0c40a00SSimon J. Gerraty
470b0c40a00SSimon J. Gerraty# cater for local complexities
471b0c40a00SSimon J. Gerraty# local.dirdeps.mk can set
472b0c40a00SSimon J. Gerraty# DIRDEPS_ALL_MACHINES_FILTER and
473b0c40a00SSimon J. Gerraty# DIRDEPS_ALL_MACHINES_FILTER_XTRAS for final tweaks
474b0c40a00SSimon J. Gerraty
475b0c40a00SSimon J. Gerraty.if !empty(ALL_TARGET_SPEC_LIST)
476b0c40a00SSimon J. Gerraty.if ${_debug_reldir}
477b0c40a00SSimon J. Gerraty.info ALL_TARGET_SPEC_LIST=${ALL_TARGET_SPEC_LIST}
4783cbdda60SSimon J. Gerraty.endif
479b0c40a00SSimon J. GerratyDIRDEPS_ALL_MACHINES_FILTER += \
480b0c40a00SSimon J. Gerraty	@x@$${ALL_TARGET_SPEC_LIST:@s@$${x:M$$s}@}@
481b0c40a00SSimon J. Gerraty.elif !empty(ALL_MACHINE_LIST)
482b0c40a00SSimon J. Gerraty.if ${_debug_reldir}
483b0c40a00SSimon J. Gerraty.info ALL_MACHINE_LIST=${ALL_MACHINE_LIST}
484b0c40a00SSimon J. Gerraty.endif
485b0c40a00SSimon J. Gerraty.if ${TARGET_SPEC_VARS:[#]} > 1
486b0c40a00SSimon J. Gerraty# the space below can result in extraneous ':'
487b0c40a00SSimon J. GerratyDIRDEPS_ALL_MACHINES_FILTER += \
488b0c40a00SSimon J. Gerraty	@x@$${ALL_MACHINE_LIST:@m@$${x:M$$m,*} $${x:M$$m}@}@
489b0c40a00SSimon J. Gerraty.else
490b0c40a00SSimon J. GerratyDIRDEPS_ALL_MACHINES_FILTER += \
491b0c40a00SSimon J. Gerraty	@x@$${ALL_MACHINE_LIST:@m@$${x:M$$m}@}@
492b0c40a00SSimon J. Gerraty.endif
493b0c40a00SSimon J. Gerraty.endif
494b0c40a00SSimon J. Gerraty# add local XTRAS - default to something benign
495b0c40a00SSimon J. GerratyDIRDEPS_ALL_MACHINES_FILTER += \
496b0c40a00SSimon J. Gerraty	${DIRDEPS_ALL_MACHINES_FILTER_XTRAS:UNbak}
497b0c40a00SSimon J. Gerraty
498b0c40a00SSimon J. Gerraty.if ${_debug_reldir}
499b0c40a00SSimon J. Gerraty.info _machine_list=${_machine_list}
500b0c40a00SSimon J. Gerraty.info DIRDEPS_ALL_MACHINES_FILTER=${DIRDEPS_ALL_MACHINES_FILTER}
501b0c40a00SSimon J. Gerraty.endif
502b0c40a00SSimon J. Gerraty
503b0c40a00SSimon J. Gerraty_only_machines := ${_machine_list:${DIRDEPS_ALL_MACHINES_FILTER:ts:}:S,:, ,g}
5043cbdda60SSimon J. Gerraty.else
50545447996SSimon J. Gerraty_only_machines := ${ONLY_TARGET_SPEC_LIST:U} ${ONLY_MACHINE_LIST:U}
5063cbdda60SSimon J. Gerraty.endif
5073cbdda60SSimon J. Gerraty
5083cbdda60SSimon J. Gerraty.if empty(_only_machines)
5093cbdda60SSimon J. Gerraty# we must be boot-strapping
510b0c40a00SSimon J. Gerraty_only_machines := ${TARGET_MACHINE:U${ALL_TARGET_SPEC_LIST:U${ALL_MACHINE_LIST:U${DEP_MACHINE}}}}
511b0c40a00SSimon J. Gerraty.endif
512b0c40a00SSimon J. Gerraty
513b0c40a00SSimon J. Gerraty# cleanup the result
514b0c40a00SSimon J. Gerraty_only_machines := ${_only_machines:O:u}
515b0c40a00SSimon J. Gerraty
516b0c40a00SSimon J. Gerraty.if ${_debug_reldir}
517b0c40a00SSimon J. Gerraty.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: ALL_MACHINES _only_machines=${_only_machines}
5183cbdda60SSimon J. Gerraty.endif
5193cbdda60SSimon J. Gerraty
5203cbdda60SSimon J. Gerraty.else				# ! ALL_MACHINES
52145447996SSimon J. Gerraty# if ONLY_TARGET_SPEC_LIST or ONLY_MACHINE_LIST is set, we are limited to that.
52245447996SSimon J. Gerraty# Note that ONLY_TARGET_SPEC_LIST should be fully qualified.
5233cbdda60SSimon J. Gerraty# if TARGET_MACHINE is set - it is really the same as ONLY_MACHINE_LIST
5243cbdda60SSimon J. Gerraty# otherwise DEP_MACHINE is it - so DEP_MACHINE will match.
52545447996SSimon J. Gerraty_only_machines := ${ONLY_TARGET_SPEC_LIST:U:M${DEP_MACHINE},*}
52645447996SSimon J. Gerraty.if empty(_only_machines)
5273cbdda60SSimon J. Gerraty_only_machines := ${ONLY_MACHINE_LIST:U${TARGET_MACHINE:U${DEP_MACHINE}}:M${DEP_MACHINE}}
5283cbdda60SSimon J. Gerraty.endif
52945447996SSimon J. Gerraty.endif
5303cbdda60SSimon J. Gerraty
5313cbdda60SSimon J. Gerraty.if !empty(NOT_MACHINE_LIST)
5323cbdda60SSimon J. Gerraty_only_machines := ${_only_machines:${NOT_MACHINE_LIST:${M_ListToSkip}}}
5333cbdda60SSimon J. Gerraty.endif
53445447996SSimon J. Gerraty.if !empty(NOT_TARGET_SPEC_LIST)
53545447996SSimon J. Gerraty# we must first qualify
53645447996SSimon J. Gerraty_dm := ${DEP_MACHINE}
53745447996SSimon J. Gerraty_only_machines := ${_only_machines:M*,*} ${_only_machines:N*,*:@DEP_MACHINE@${DEP_TARGET_SPEC}@:S,^,.,:${M_dep_qual_fixes:ts:}:O:u:S,^.,,}
53845447996SSimon J. GerratyDEP_MACHINE := ${_dm}
53945447996SSimon J. Gerraty_only_machines := ${_only_machines:${NOT_TARGET_SPEC_LIST:${M_ListToSkip}}}
54045447996SSimon J. Gerraty.endif
54145447996SSimon J. Gerraty# clean up
54245447996SSimon J. Gerraty_only_machines := ${_only_machines:O:u}
5433cbdda60SSimon J. Gerraty
544b0c40a00SSimon J. Gerraty.if ${_debug_reldir}
545b0c40a00SSimon J. Gerraty.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: _only_machines=${_only_machines}
546b0c40a00SSimon J. Gerraty.endif
547b0c40a00SSimon J. Gerraty
5483cbdda60SSimon J. Gerraty# make sure we have a starting place?
5493cbdda60SSimon J. GerratyDIRDEPS ?= ${RELDIR}
5503cbdda60SSimon J. Gerraty.endif				# target
5513cbdda60SSimon J. Gerraty
552e48f47ddSSimon J. Gerraty.if !defined(NO_DIRDEPS) && !defined(NO_DIRDEPS_BELOW)
553db29cad8SSimon J. Gerraty.if ${MK_DIRDEPS_CACHE} == "yes"
554db29cad8SSimon J. Gerraty
555db29cad8SSimon J. Gerraty# just ensure this exists
556db29cad8SSimon J. Gerratybuild-dirdeps:
557db29cad8SSimon J. Gerraty
558db29cad8SSimon J. GerratyM_oneperline = @x@\\${.newline}	$$x@
559db29cad8SSimon J. Gerraty
560db29cad8SSimon J. Gerraty.if ${BUILD_DIRDEPS_CACHE} == "no"
561db29cad8SSimon J. Gerraty.if !target(dirdeps-cached)
562db29cad8SSimon J. Gerraty# we do this via sub-make
563db29cad8SSimon J. GerratyBUILD_DIRDEPS = no
564db29cad8SSimon J. Gerraty
56595e3ed2cSSimon J. Gerraty# ignore anything but these
56695e3ed2cSSimon J. Gerraty.MAKE.META.IGNORE_FILTER = M*/${.MAKE.DEPENDFILE_PREFIX}*
56795e3ed2cSSimon J. Gerraty
568db29cad8SSimon J. Gerratydirdeps: dirdeps-cached
569db29cad8SSimon J. Gerratydirdeps-cached:	${DIRDEPS_CACHE} .MAKE
570db29cad8SSimon J. Gerraty	@echo "${TRACER}Using ${DIRDEPS_CACHE}"
5719f45a3c8SSimon J. Gerraty	@MAKELEVEL=${.MAKE.LEVEL} \
5729f45a3c8SSimon J. Gerraty	TARGET_SPEC=${TARGET_SPEC} \
5739f45a3c8SSimon J. Gerraty	${TARGET_SPEC_VARS:@v@$v=${$v}@} \
5749f45a3c8SSimon J. Gerraty	${.MAKE} -C ${_CURDIR} -f ${DIRDEPS_CACHE} \
575db29cad8SSimon J. Gerraty		dirdeps MK_DIRDEPS_CACHE=no BUILD_DIRDEPS=no
576db29cad8SSimon J. Gerraty
577956e45f6SSimon J. Gerraty# leaf makefiles rarely work for building DIRDEPS_CACHE
578956e45f6SSimon J. Gerraty.if ${RELDIR} != "."
579956e45f6SSimon J. GerratyBUILD_DIRDEPS_MAKEFILE ?= -f dirdeps.mk
580956e45f6SSimon J. Gerraty.endif
581956e45f6SSimon J. Gerraty
582db29cad8SSimon J. Gerraty# these should generally do
5832c3632d1SSimon J. GerratyBUILD_DIRDEPS_MAKEFILE ?=
584c9f4001fSSimon J. GerratyBUILD_DIRDEPS_OVERRIDES ?=
585db29cad8SSimon J. GerratyBUILD_DIRDEPS_TARGETS ?= ${.TARGETS}
586db29cad8SSimon J. Gerraty
5872c3632d1SSimon J. Gerraty.if ${DIRDEPS_CACHE} != ${STATIC_DIRDEPS_CACHE:Uno} && ${DIRDEPS_CACHE:M${SRCTOP}/*} == ""
5882c3632d1SSimon J. Gerraty# export this for dirdeps-cache-update.mk
5892c3632d1SSimon J. GerratyDYNAMIC_DIRDEPS_CACHE := ${DIRDEPS_CACHE}
5902c3632d1SSimon J. Gerraty.export DYNAMIC_DIRDEPS_CACHE
591db29cad8SSimon J. Gerraty# we need the .meta file to ensure we update if
592db29cad8SSimon J. Gerraty# any of the Makefile.depend* changed.
593db29cad8SSimon J. Gerraty# We do not want to compare the command line though.
594db29cad8SSimon J. Gerraty${DIRDEPS_CACHE}:	.META .NOMETA_CMP
595db29cad8SSimon J. Gerraty	+@{ echo '# Autogenerated - do NOT edit!'; echo; \
596db29cad8SSimon J. Gerraty	echo 'BUILD_DIRDEPS=no'; echo; \
59749caa483SSimon J. Gerraty	echo '.include <dirdeps.mk>'; echo; \
598db29cad8SSimon J. Gerraty	} > ${.TARGET}.new
599db29cad8SSimon J. Gerraty	+@MAKELEVEL=${.MAKE.LEVEL} DIRDEPS_CACHE=${DIRDEPS_CACHE} \
600db29cad8SSimon J. Gerraty	DIRDEPS="${DIRDEPS}" \
60145447996SSimon J. Gerraty	TARGET_SPEC=${TARGET_SPEC} \
6022c3632d1SSimon J. Gerraty	MAKEFLAGS= ${DIRDEP_CACHE_MAKE:U${.MAKE}} -C ${_CURDIR} \
6032c3632d1SSimon J. Gerraty	${BUILD_DIRDEPS_MAKEFILE} \
604c9f4001fSSimon J. Gerraty	${BUILD_DIRDEPS_TARGETS} \
605c9f4001fSSimon J. Gerraty	${BUILD_DIRDEPS_OVERRIDES} \
606c9f4001fSSimon J. Gerraty	BUILD_DIRDEPS_CACHE=yes \
607128a4105SSimon J. Gerraty	.MAKE.DEPENDFILE=.none \
608956e45f6SSimon J. Gerraty	${"${DEBUG_DIRDEPS:Nno}":?DEBUG_DIRDEPS='${DEBUG_DIRDEPS}':} \
609be19d90bSSimon J. Gerraty	${.MAKEFLAGS:tW:S,-D ,-D,g:tw:M*WITH*} \
61049caa483SSimon J. Gerraty	${.MAKEFLAGS:tW:S,-d ,-d,g:tw:M-d*} \
6118c973ee2SSimon J. Gerraty	3>&1 1>&2 | sed 's,${SRCTOP},_{SRCTOP},g;s,_{SRCTOP}/_{SRCTOP},_{SRCTOP},g;s,_{,$${,g' >> ${.TARGET}.new && \
612db29cad8SSimon J. Gerraty	mv ${.TARGET}.new ${.TARGET}
613db29cad8SSimon J. Gerraty
614db29cad8SSimon J. Gerraty.endif
6152c3632d1SSimon J. Gerraty.endif
616db29cad8SSimon J. Gerraty.elif !target(_count_dirdeps)
617db29cad8SSimon J. Gerraty# we want to capture the dirdeps count in the cache
618db29cad8SSimon J. Gerraty.END: _count_dirdeps
619db29cad8SSimon J. Gerraty_count_dirdeps: .NOMETA
6202c3632d1SSimon J. Gerraty	@{ 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
621db29cad8SSimon J. Gerraty
622db29cad8SSimon J. Gerraty.endif
62328a6bc81SSimon J. Gerraty.elif !make(dirdeps) && !target(_count_dirdeps)
624db29cad8SSimon J. Gerratybeforedirdeps: _count_dirdeps
625db29cad8SSimon J. Gerraty_count_dirdeps: .NOMETA
6262c3632d1SSimon J. Gerraty	@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}`"
627db29cad8SSimon J. Gerraty
628db29cad8SSimon J. Gerraty.endif
62928a6bc81SSimon J. Gerraty.endif
63028a6bc81SSimon J. Gerraty
631db29cad8SSimon J. Gerraty.if ${BUILD_DIRDEPS} == "yes"
6323cbdda60SSimon J. Gerraty
6333cbdda60SSimon J. Gerraty# the rest is done repeatedly for every Makefile.depend we read.
6343cbdda60SSimon J. Gerraty# if we are anything but the original dir we care only about the
6353cbdda60SSimon J. Gerraty# machine type we were included for..
6363cbdda60SSimon J. Gerraty
6373cbdda60SSimon J. Gerraty.if ${DEP_RELDIR} == "."
6383cbdda60SSimon J. Gerraty_this_dir := ${SRCTOP}
6393cbdda60SSimon J. Gerraty.else
6403cbdda60SSimon J. Gerraty_this_dir := ${SRCTOP}/${DEP_RELDIR}
6413cbdda60SSimon J. Gerraty.endif
6423cbdda60SSimon J. Gerraty
6433cbdda60SSimon J. Gerraty# on rare occasions, there can be a need for extra help
6443cbdda60SSimon J. Gerraty_dep_hack := ${_this_dir}/${.MAKE.DEPENDFILE_PREFIX}.inc
645e48f47ddSSimon J. Gerraty.-include <${_dep_hack}>
646494f7191SSimon J. Gerraty.-include <${_dep_hack:R}.options>
6473cbdda60SSimon J. Gerraty
6481748de26SSimon J. Gerraty.if ${DEP_RELDIR} != ${_DEP_RELDIR} || ${DEP_TARGET_SPEC} != ${TARGET_SPEC}
6493cbdda60SSimon J. Gerraty# this should be all
6503cbdda60SSimon J. Gerraty_machines := ${DEP_MACHINE}
6513cbdda60SSimon J. Gerraty.else
6523cbdda60SSimon J. Gerraty# this is the machine list we actually use below
6533cbdda60SSimon J. Gerraty_machines := ${_only_machines}
6543cbdda60SSimon J. Gerraty
655e22fef7dSSimon J. Gerraty.if defined(HOSTPROG) || ${DEP_MACHINE:Nhost*} == ""
6563cbdda60SSimon J. Gerraty# we need to build this guy's dependencies for host as well.
657e22fef7dSSimon J. Gerraty.if ${DEP_MACHINE:Nhost*} == ""
658e22fef7dSSimon J. Gerraty_machines += ${DEP_MACHINE}
659e22fef7dSSimon J. Gerraty.else
6603cbdda60SSimon J. Gerraty_machines += host
6613cbdda60SSimon J. Gerraty.endif
662e22fef7dSSimon J. Gerraty.endif
6633cbdda60SSimon J. Gerraty
6643cbdda60SSimon J. Gerraty_machines := ${_machines:O:u}
6653cbdda60SSimon J. Gerraty.endif
6663cbdda60SSimon J. Gerraty
667148ee845SSimon J. Gerraty.if ${DEP_TARGET_SPEC_VARS:[#]} > 1
6681748de26SSimon J. Gerraty# we need to tweak _machines
6691748de26SSimon J. Gerraty_dm := ${DEP_MACHINE}
6700dede8b0SSimon J. Gerraty# apply the same filtering that we do when qualifying DIRDEPS.
671db29cad8SSimon J. Gerraty# M_dep_qual_fixes expects .${MACHINE}* so add (and remove) '.'
67245447996SSimon J. Gerraty# Again we expect that any already qualified machines are fully qualified.
6738c973ee2SSimon J. Gerraty_machines := ${_machines:M*,*} ${_machines:N*,*:@DEP_MACHINE@${DEP_TARGET_SPEC}@:S,^,.,:S,^.,,}
6741748de26SSimon J. GerratyDEP_MACHINE := ${_dm}
675148ee845SSimon J. Gerraty_machines := ${_machines:${M_dep_qual_fixes.${DEP_MACHINE}:U${M_dep_qual_fixes}:ts:}:O:u}
6761748de26SSimon J. Gerraty.endif
6771748de26SSimon J. Gerraty
6781748de26SSimon J. Gerraty# reset each time through
6793cbdda60SSimon J. Gerraty_build_dirs =
6803cbdda60SSimon J. Gerraty
681c1d01b5fSSimon J. Gerraty.if ${DEP_RELDIR} == ${_DEP_RELDIR} && ${_CURDIR} != ${SRCTOP}
6823cbdda60SSimon J. Gerraty# pickup other machines for this dir if necessary
6833cbdda60SSimon J. Gerraty_build_dirs += ${_machines:@m@${_CURDIR}.$m@}
6843cbdda60SSimon J. Gerraty.endif
6853cbdda60SSimon J. Gerraty
686db29cad8SSimon J. Gerraty.if ${_debug_reldir}
68712904384SSimon J. Gerraty.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: nDIRDEPS=${DIRDEPS:[#]}
68898875883SSimon J. Gerraty.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: DIRDEPS=${DIRDEPS:${DEBUG_DIRDEPS_LIST_FILTER:U:N/:ts:}}
6891748de26SSimon J. Gerraty.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: _machines='${_machines}'
6903cbdda60SSimon J. Gerraty.endif
6913cbdda60SSimon J. Gerraty
6923cbdda60SSimon J. Gerraty.if !empty(DIRDEPS)
6931748de26SSimon J. Gerraty# these we reset each time through as they can depend on DEP_MACHINE
6941748de26SSimon J. GerratyDEP_DIRDEPS_FILTER = \
6951748de26SSimon J. Gerraty	${DIRDEPS_FILTER.${DEP_TARGET_SPEC}:U} \
69645447996SSimon J. Gerraty	${TARGET_SPEC_VARS:@v@${DIRDEPS_FILTER.${DEP_$v}:U}@} \
6971748de26SSimon J. Gerraty	${DIRDEPS_FILTER:U}
698*8d5c8e21SSimon J. Gerraty
6991748de26SSimon J. Gerraty.if empty(DEP_DIRDEPS_FILTER)
7001748de26SSimon J. Gerraty# something harmless
701*8d5c8e21SSimon J. GerratyDEP_DIRDEPS_FILTER = u
702*8d5c8e21SSimon J. Gerraty.endif
703*8d5c8e21SSimon J. Gerraty
704*8d5c8e21SSimon J. Gerraty# this is applied after we have computed build dirs
705*8d5c8e21SSimon J. Gerraty# so everything is fully qualified and starts with ${SRCTOP}/
706*8d5c8e21SSimon J. GerratyDEP_DIRDEPS_BUILD_DIR_FILTER = \
707*8d5c8e21SSimon J. Gerraty	${DIRDEPS_BUILD_DIR_FILTER.${DEP_TARGET_SPEC}:U} \
708*8d5c8e21SSimon J. Gerraty	${TARGET_SPEC_VARS:@v@${DIRDEPS_BUILD_DIR_FILTER.${DEP_$v}:U}@} \
709*8d5c8e21SSimon J. Gerraty	${DIRDEPS_BUILD_DIR_FILTER:U}
710*8d5c8e21SSimon J. Gerraty
711*8d5c8e21SSimon J. Gerraty.if empty(DEP_DIRDEPS_BUILD_DIR_FILTER)
712*8d5c8e21SSimon J. Gerraty# something harmless
713*8d5c8e21SSimon J. GerratyDEP_DIRDEPS_BUILD_DIR_FILTER = u
7141748de26SSimon J. Gerraty.endif
7153cbdda60SSimon J. Gerraty
7163cbdda60SSimon J. Gerraty# this is what we start with
7175bcb7424SSimon J. Gerraty__depdirs := ${DIRDEPS:${NSkipDir}:${DEP_DIRDEPS_FILTER:ts:}:C,//+,/,g:O:u:@d@${SRCTOP}/$d@}
7183cbdda60SSimon J. Gerraty
71912904384SSimon J. Gerraty# some entries may be qualified with .<machine> or .<target_spec>
72012904384SSimon J. Gerraty# we can tell the unqualified ones easily - because they exist
72112904384SSimon J. Gerraty__unqual_depdirs := ${__depdirs:@d@${exists($d):?$d:}@}
72212904384SSimon J. Gerraty__qual_depdirs := ${__depdirs:${__unqual_depdirs:Uno:${M_ListToSkip}}}
7233cbdda60SSimon J. Gerraty
7243cbdda60SSimon J. Gerraty.if ${DEP_RELDIR} == ${_DEP_RELDIR}
7253cbdda60SSimon J. Gerraty# if it was called out - we likely need it.
726494f7191SSimon J. Gerraty__hostdpadd := ${DPADD:U.:M${HOST_OBJTOP}/*:S,${HOST_OBJTOP}/,,:H:${NSkipDir}:${DIRDEPS_FILTER:ts:}:S,$,.host,:N.*:@d@${SRCTOP}/$d@} \
727494f7191SSimon J. Gerraty	${DPADD:U.:M${HOST_OBJTOP32:Uno}/*:S,${HOST_OBJTOP32:Uno}/,,:H:${NSkipDir}:${DIRDEPS_FILTER:ts:}:S,$,.host32,:N.*:@d@${SRCTOP}/$d@}
7283cbdda60SSimon J. Gerraty__qual_depdirs += ${__hostdpadd}
7293cbdda60SSimon J. Gerraty.endif
7303cbdda60SSimon J. Gerraty
731db29cad8SSimon J. Gerraty.if ${_debug_reldir}
732956e45f6SSimon J. Gerraty.info DEP_DIRDEPS_FILTER=${DEP_DIRDEPS_FILTER:ts:}
733*8d5c8e21SSimon J. Gerraty.info DEP_DIRDEPS_BUILD_DIR_FILTER=${DEP_DIRDEPS_BUILD_DIR_FILTER:ts:}
73498875883SSimon J. Gerraty.info depdirs=${__depdirs:S,^${SRCTOP}/,,:${DEBUG_DIRDEPS_LIST_FILTER:U:N/:ts:}}
73598875883SSimon J. Gerraty.info qualified=${__qual_depdirs:S,^${SRCTOP}/,,:${DEBUG_DIRDEPS_LIST_FILTER:U:N/:ts:}}
73698875883SSimon J. Gerraty.info unqualified=${__unqual_depdirs:S,^${SRCTOP}/,,:${DEBUG_DIRDEPS_LIST_FILTER:U:N/:ts:}}
7373cbdda60SSimon J. Gerraty.endif
7383cbdda60SSimon J. Gerraty
7393cbdda60SSimon J. Gerraty# _build_dirs is what we will feed to _DIRDEP_USE
7403cbdda60SSimon J. Gerraty_build_dirs += \
7413cbdda60SSimon J. Gerraty	${__qual_depdirs:M*.host:${NSkipHostDir}:N.host} \
7423cbdda60SSimon J. Gerraty	${__qual_depdirs:N*.host} \
743db29cad8SSimon J. Gerraty	${_machines:Mhost*:@m@${__unqual_depdirs:@d@$d.$m@}@:${NSkipHostDir}:N.host} \
744db29cad8SSimon J. Gerraty	${_machines:Nhost*:@m@${__unqual_depdirs:@d@$d.$m@}@}
7453cbdda60SSimon J. Gerraty
7461748de26SSimon J. Gerraty# qualify everything now
747148ee845SSimon J. Gerraty.if ${_debug_reldir}
74898875883SSimon J. Gerraty.info _build_dirs=${_build_dirs:${DEBUG_DIRDEPS_LIST_FILTER:U:N/:ts:}}
749148ee845SSimon J. Gerraty.endif
750148ee845SSimon J. Gerraty# make sure we do not mess with qualifying "host" entries
751148ee845SSimon J. Gerraty_build_dirs := ${_build_dirs:M*.host*:${M_dep_qual_fixes.host:ts:}} \
752148ee845SSimon J. Gerraty	${_build_dirs:N*.host*:${M_dep_qual_fixes:ts:}}
753*8d5c8e21SSimon J. Gerraty# some filters can only be applied now
754*8d5c8e21SSimon J. Gerraty_build_dirs := ${_build_dirs:${DEP_DIRDEPS_BUILD_DIR_FILTER:ts:}:O:u}
755148ee845SSimon J. Gerraty.if ${_debug_reldir}
75698875883SSimon J. Gerraty.info _build_dirs=${_build_dirs:${DEBUG_DIRDEPS_LIST_FILTER:U:N/:ts:}}
757148ee845SSimon J. Gerraty.endif
7583cbdda60SSimon J. Gerraty
7591ce939a7SSimon J. Gerraty.endif				# empty DIRDEPS
7601ce939a7SSimon J. Gerraty
7612c3632d1SSimon J. Gerraty_build_all_dirs += ${_build_dirs} ${_build_xtra_dirs}
762be19d90bSSimon J. Gerraty_build_all_dirs := ${_build_all_dirs:O:u}
763be19d90bSSimon J. Gerraty
764*8d5c8e21SSimon J. Gerraty# we prefer DIRDEPS_EXPORT_VARS
765*8d5c8e21SSimon J. Gerraty.if empty(DIRDEPS_EXPORT_VARS) && !empty(DEP_EXPORT_VARS)
766*8d5c8e21SSimon J. GerratyDIRDEPS_EXPORT_VARS = ${DEP_EXPORT_VARS}
767*8d5c8e21SSimon J. Gerraty.endif
768*8d5c8e21SSimon J. Gerraty
7693cbdda60SSimon J. Gerraty# Normally if doing make -V something,
7703cbdda60SSimon J. Gerraty# we do not want to waste time chasing DIRDEPS
7713cbdda60SSimon J. Gerraty# but if we want to count the number of Makefile.depend* read, we do.
77212904384SSimon J. Gerraty.if ${.MAKEFLAGS:M-V${_V_READ_DIRDEPS:U}} == ""
773be19d90bSSimon J. Gerraty.if !empty(_build_all_dirs)
774db29cad8SSimon J. Gerraty.if ${BUILD_DIRDEPS_CACHE} == "yes"
7759f45a3c8SSimon J. Gerraty# we use _cache_script to minimize the number of times we fork the shell
7769f45a3c8SSimon J. Gerraty_cache_script = echo '\# ${DEP_RELDIR}.${DEP_TARGET_SPEC}';
7773841c287SSimon J. Gerraty# guard against _new_dirdeps being too big for a single command line
77812904384SSimon J. Gerraty_new_dirdeps := ${_build_all_dirs:@x@${target($x):?:$x}@:S,^${SRCTOP}/,,}
77912904384SSimon J. Gerraty_cache_xtra_deps := ${_build_xtra_dirs:S,^${SRCTOP}/,,}
780*8d5c8e21SSimon J. Gerraty.if !empty(DIRDEPS_EXPORT_VARS)
7818695518cSSimon J. Gerraty# Discouraged, but there are always exceptions.
7828695518cSSimon J. Gerraty# Handle it here rather than explain how.
7839f45a3c8SSimon J. Gerraty_cache_xvars := echo; ${DIRDEPS_EXPORT_VARS:@v@echo '$v = ${$v}';@} echo '.export ${DIRDEPS_EXPORT_VARS}'; echo;
7849f45a3c8SSimon J. Gerraty_cache_script += ${_cache_xvars}
7858695518cSSimon J. Gerraty.endif
786db29cad8SSimon J. Gerraty.else
7873cbdda60SSimon J. Gerraty# this makes it all happen
788be19d90bSSimon J. Gerratydirdeps: ${_build_all_dirs}
789db29cad8SSimon J. Gerraty.endif
790be19d90bSSimon J. Gerraty${_build_all_dirs}:	_DIRDEP_USE
7913cbdda60SSimon J. Gerraty
792db29cad8SSimon J. Gerraty.if ${_debug_reldir}
79398875883SSimon J. Gerraty.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: needs: ${_build_dirs:S,^${SRCTOP}/,,:${DEBUG_DIRDEPS_LIST_FILTER:U:N/:ts:}}
7943cbdda60SSimon J. Gerraty.endif
7953cbdda60SSimon J. Gerraty
7961748de26SSimon J. Gerraty# this builds the dependency graph
7973cbdda60SSimon J. Gerraty.for m in ${_machines}
7983841c287SSimon J. Gerraty.if ${BUILD_DIRDEPS_CACHE} == "yes" && !empty(_build_dirs)
7992c3632d1SSimon J. Gerraty_cache_deps =
8009f45a3c8SSimon J. Gerraty_cache_script += echo; echo 'DIRDEPS.${_this_dir}.$m = \';
8013841c287SSimon J. Gerraty.endif
8023cbdda60SSimon J. Gerraty# it would be nice to do :N${.TARGET}
8033cbdda60SSimon J. Gerraty.if !empty(__qual_depdirs)
804148ee845SSimon J. Gerraty.for q in ${__qual_depdirs:M*.host*:${M_dep_qual_fixes.host:ts:}:E:O:u:N$m} \
805148ee845SSimon J. Gerraty	${__qual_depdirs:N*.host*:${M_dep_qual_fixes:ts:}:E:O:u:N$m}
806db29cad8SSimon J. Gerraty.if ${_debug_reldir} || ${DEBUG_DIRDEPS:@x@${${DEP_RELDIR}.$m:L:M$x}${${DEP_RELDIR}.$q:L:M$x}@} != ""
807148ee845SSimon J. Gerraty.info ${DEP_RELDIR}.$m: q=$q graph: ${_build_dirs:M*.$q:S,^${SRCTOP}/,,}
8083cbdda60SSimon J. Gerraty.endif
809db29cad8SSimon J. Gerraty.if ${BUILD_DIRDEPS_CACHE} == "yes"
81012904384SSimon J. Gerraty_cache_deps += ${_build_dirs:M*.$q:S,^${SRCTOP}/,,}
811db29cad8SSimon J. Gerraty.else
8123cbdda60SSimon J. Gerraty${_this_dir}.$m: ${_build_dirs:M*.$q}
813db29cad8SSimon J. Gerraty.endif
8143cbdda60SSimon J. Gerraty.endfor
8153cbdda60SSimon J. Gerraty.endif
816db29cad8SSimon J. Gerraty.if ${_debug_reldir}
81712904384SSimon J. Gerraty.info ${DEP_RELDIR}.$m: graph: ${_build_dirs:M*.$m:N${_this_dir}.$m:S,^${SRCTOP}/,,}
8183cbdda60SSimon J. Gerraty.endif
819db29cad8SSimon J. Gerraty.if ${BUILD_DIRDEPS_CACHE} == "yes"
8202c3632d1SSimon J. Gerraty.if !empty(_build_dirs)
82112904384SSimon J. Gerraty_cache_deps += ${_build_dirs:M*.$m:N${_this_dir}.$m:S,^${SRCTOP}/,,}
822d5e0a182SSimon J. Gerraty# anything in _{build,env}_xtra_dirs is hooked to dirdeps: only
823d5e0a182SSimon J. Gerraty.if ${MAKE_VERSION} < 20240105
8243841c287SSimon J. Gerraty.if !empty(_cache_deps)
82549caa483SSimon J. Gerraty.export _cache_deps
8269f45a3c8SSimon J. Gerraty_cache_script += for x in $$_cache_deps; do echo "	_{SRCTOP}/$$x \\"; done;
8273841c287SSimon J. Gerraty.endif
828d5e0a182SSimon J. Gerraty.export _cache_xtra_deps _new_dirdeps
8299f45a3c8SSimon J. Gerratyx!= echo; { echo; ${_cache_script} echo; echo '${_this_dir}.$m: $${DIRDEPS.${_this_dir}.$m}'; \
8302c3632d1SSimon J. Gerraty	echo; echo 'dirdeps: ${_this_dir}.$m \'; \
83112904384SSimon J. Gerraty	for x in $$_cache_xtra_deps; do echo "	_{SRCTOP}/$$x \\"; done; \
83212904384SSimon J. Gerraty	echo; for x in $$_new_dirdeps; do echo "_{SRCTOP}/$$x: _DIRDEP_USE"; done; } >&3
833d5e0a182SSimon J. Gerraty.else
834d5e0a182SSimon J. Gerraty# we do not have the same limits on command lines
835d5e0a182SSimon J. Gerraty.if !empty(_cache_deps)
836d5e0a182SSimon J. Gerraty_cache_script += for x in ${_cache_deps}; do echo "	_{SRCTOP}/$$x \\"; done;
837d5e0a182SSimon J. Gerraty.endif
838d5e0a182SSimon J. Gerratyx!= echo; { echo; ${_cache_script} echo; echo '${_this_dir}.$m: $${DIRDEPS.${_this_dir}.$m}'; \
839d5e0a182SSimon J. Gerraty	echo; echo 'dirdeps: ${_this_dir}.$m \'; \
840d5e0a182SSimon J. Gerraty	for x in ${_cache_xtra_deps}; do echo "	_{SRCTOP}/$$x \\"; done; \
841d5e0a182SSimon J. Gerraty	echo; for x in ${_new_dirdeps}; do echo "_{SRCTOP}/$$x: _DIRDEP_USE"; done; } >&3
842d5e0a182SSimon J. Gerraty.endif
8432c3632d1SSimon J. Gerraty.endif
844db29cad8SSimon J. Gerraty.else
8453cbdda60SSimon J. Gerraty${_this_dir}.$m: ${_build_dirs:M*.$m:N${_this_dir}.$m}
846db29cad8SSimon J. Gerraty.endif
8473cbdda60SSimon J. Gerraty.endfor
8483cbdda60SSimon J. Gerraty
8493cbdda60SSimon J. Gerraty.endif
8503cbdda60SSimon J. Gerraty
851*8d5c8e21SSimon J. Gerraty.if !empty(DIRDEPS_EXPORT_VARS)
852*8d5c8e21SSimon J. Gerraty.if ${BUILD_DIRDEPS_CACHE} == "no"
853*8d5c8e21SSimon J. Gerraty.export ${DIRDEPS_EXPORT_VARS}
854*8d5c8e21SSimon J. Gerraty.endif
855*8d5c8e21SSimon J. Gerraty# Reset these, we are done with them for this iteration.
856*8d5c8e21SSimon J. GerratyDIRDEPS_EXPORT_VARS =
857*8d5c8e21SSimon J. GerratyDEP_EXPORT_VARS =
858*8d5c8e21SSimon J. Gerraty.endif
859*8d5c8e21SSimon J. Gerraty
8601748de26SSimon J. Gerraty# Now find more dependencies - and recurse.
861be19d90bSSimon J. Gerraty.for d in ${_build_all_dirs}
8623bebe729SSimon J. Gerraty.if !target(_dirdeps_checked.$d)
8631748de26SSimon J. Gerraty# once only
8643bebe729SSimon J. Gerraty_dirdeps_checked.$d:
8658c973ee2SSimon J. Gerraty_dr := ${d:S,^${SRCTOP}/,,}
866db29cad8SSimon J. Gerraty.if ${_debug_search}
8678c973ee2SSimon J. Gerraty.info checking ${_dr}
8680dede8b0SSimon J. Gerraty.endif
869be19d90bSSimon J. Gerraty# Note: _build_all_dirs is fully qualifed so d:R is always the directory
8701748de26SSimon J. Gerraty.if exists(${d:R})
87145447996SSimon J. Gerraty# we pass _DEP_TARGET_SPEC to tell the next step what we want
87245447996SSimon J. Gerraty_DEP_TARGET_SPEC := ${d:E}
87345447996SSimon J. Gerraty# some makefiles may still look at this
87445447996SSimon J. Gerraty_DEP_MACHINE := ${d:E:C/,.*//}
875148ee845SSimon J. GerratyDEP_MACHINE := ${_DEP_MACHINE}
87645447996SSimon J. Gerraty# set these too in case Makefile.depend* uses them
877148ee845SSimon J. Gerraty.if ${DEP_TARGET_SPEC_VARS:[#]} > 1
87845447996SSimon J. Gerraty_dtspec := ${_DEP_TARGET_SPEC:S/,/ /g}
87945447996SSimon J. Gerraty.for i in ${_tspec_x}
880148ee845SSimon J. GerratyDEP_${DEP_TARGET_SPEC_VARS:[$i]} := ${_dtspec:[$i]}
88145447996SSimon J. Gerraty.endfor
88245447996SSimon J. Gerraty.endif
8831748de26SSimon J. Gerraty# Warning: there is an assumption here that MACHINE is always
8841748de26SSimon J. Gerraty# the first entry in TARGET_SPEC_VARS.
8851748de26SSimon J. Gerraty# If TARGET_SPEC and MACHINE are insufficient, you have a problem.
886b0c40a00SSimon J. Gerraty_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]}
8871748de26SSimon J. Gerraty.if !empty(_m)
8880dede8b0SSimon J. Gerraty# M_dep_qual_fixes isn't geared to Makefile.depend
889148ee845SSimon J. Gerraty_qm := ${_m:C;(\.depend)$;\1.${d:E};:${M_dep_qual_fixes.${d:E}:U${M_dep_qual_fixes}:ts:}}
890db29cad8SSimon J. Gerraty.if ${_debug_search}
8911748de26SSimon J. Gerraty.info Looking for ${_qm}
8923cbdda60SSimon J. Gerraty.endif
893128a4105SSimon J. Gerraty# set this "just in case"
894128a4105SSimon J. Gerraty# we can skip :tA since we computed the path above
89512904384SSimon J. GerratyDEP_RELDIR := ${_m:H:S,^${SRCTOP}/,,}
896128a4105SSimon J. Gerraty# and reset this
897128a4105SSimon J. GerratyDIRDEPS =
898db29cad8SSimon J. Gerraty.if ${_debug_reldir} && ${_qm} != ${_m}
8998c973ee2SSimon J. Gerraty.info loading ${_m:S,${SRCTOP}/,,} for ${_dr}
9003cbdda60SSimon J. Gerraty.endif
9011748de26SSimon J. Gerraty.include <${_m}>
9022eae894cSSimon J. Gerraty.else
9038c973ee2SSimon J. Gerraty# set these as if we found Makefile.depend*
9048c973ee2SSimon J. GerratyDEP_RELDIR := ${_dr:R}
9058c973ee2SSimon J. GerratyDIRDEPS =
9068c973ee2SSimon J. Gerraty.if ${_debug_reldir}
9078c973ee2SSimon J. Gerraty.info loading local.dirdeps-missing.mk for ${_dr}
9088c973ee2SSimon J. Gerraty.endif
9092eae894cSSimon J. Gerraty.-include <local.dirdeps-missing.mk>
9101748de26SSimon J. Gerraty.endif
9113cbdda60SSimon J. Gerraty.endif
9123cbdda60SSimon J. Gerraty.endif
9133cbdda60SSimon J. Gerraty.endfor
9141748de26SSimon J. Gerraty
9153cbdda60SSimon J. Gerraty.endif				# -V
916db29cad8SSimon J. Gerraty.endif				# BUILD_DIRDEPS
9173cbdda60SSimon J. Gerraty
9183cbdda60SSimon J. Gerraty.elif ${.MAKE.LEVEL} > 42
9193cbdda60SSimon J. Gerraty.error You should have stopped recursing by now.
9203cbdda60SSimon J. Gerraty.else
921128a4105SSimon J. Gerraty# we are building something
922128a4105SSimon J. GerratyDEP_RELDIR := ${RELDIR}
923128a4105SSimon J. Gerraty_DEP_RELDIR := ${RELDIR}
924494f7191SSimon J. Gerraty# Since we are/should be included by .MAKE.DEPENDFILE
9252eae894cSSimon J. Gerraty# This is a final opportunity to add/hook global rules.
926494f7191SSimon J. Gerraty.-include <local.dirdeps-build.mk>
927494f7191SSimon J. Gerraty
9282c3632d1SSimon J. Gerraty# skip _reldir_{finish,failed} if not included from Makefile.depend*
9292c3632d1SSimon J. Gerraty# or not in meta mode
9302c3632d1SSimon J. Gerraty.if !defined(WITHOUT_META_STATS) && ${.INCLUDEDFROMFILE:U:M${.MAKE.DEPENDFILE_PREFIX}*} != "" && ${.MAKE.MODE:Mmeta} != ""
9312c3632d1SSimon J. Gerraty
9322c3632d1SSimon J. Gerratymeta_stats= meta=${empty(.MAKE.META.FILES):?0:${.MAKE.META.FILES:[#]}} \
9332c3632d1SSimon J. Gerraty	created=${empty(.MAKE.META.CREATED):?0:${.MAKE.META.CREATED:[#]}}
9342c3632d1SSimon J. Gerraty
9352c3632d1SSimon J. Gerraty.if !target(_reldir_finish)
9362c3632d1SSimon J. Gerraty.END: _reldir_finish
9372c3632d1SSimon J. Gerraty_reldir_finish: .NOMETA
9382c3632d1SSimon J. Gerraty	@echo "${TRACER}Finished ${RELDIR}.${TARGET_SPEC} seconds=$$(( ${now_utc} - ${start_utc} )) ${meta_stats}"
9392c3632d1SSimon J. Gerraty.endif
9402c3632d1SSimon J. Gerraty
9412c3632d1SSimon J. Gerraty.if !target(_reldir_failed)
9422c3632d1SSimon J. Gerraty.ERROR: _reldir_failed
9432c3632d1SSimon J. Gerraty_reldir_failed: .NOMETA
9442c3632d1SSimon J. Gerraty	@echo "${TRACER}Failed ${RELDIR}.${TARGET_SPEC} seconds=$$(( ${now_utc} - ${start_utc} )) ${meta_stats}"
9452c3632d1SSimon J. Gerraty.endif
9462c3632d1SSimon J. Gerraty
9472c3632d1SSimon J. Gerraty.endif
9482c3632d1SSimon J. Gerraty
9493cbdda60SSimon J. Gerraty# pickup local dependencies
950be19d90bSSimon J. Gerraty.if ${MAKE_VERSION} < 20160220
9513cbdda60SSimon J. Gerraty.-include <.depend>
952be19d90bSSimon J. Gerraty.else
953be19d90bSSimon J. Gerraty.dinclude <.depend>
954be19d90bSSimon J. Gerraty.endif
9553cbdda60SSimon J. Gerraty.endif
9563cbdda60SSimon J. Gerraty
957db29cad8SSimon J. Gerraty# bootstrapping new dependencies made easy?
958b0c40a00SSimon J. Gerraty.if !target(bootstrap-empty)
959be19d90bSSimon J. Gerraty.if !target(bootstrap) && (make(bootstrap) || \
960be19d90bSSimon J. Gerraty	make(bootstrap-this) || \
961be19d90bSSimon J. Gerraty	make(bootstrap-recurse) || \
962be19d90bSSimon J. Gerraty	make(bootstrap-empty))
963db29cad8SSimon J. Gerraty
96495e3ed2cSSimon J. Gerraty# if we are bootstrapping create the default
96595e3ed2cSSimon J. Gerraty_want = ${.CURDIR}/${.MAKE.DEPENDFILE_DEFAULT:T}
96695e3ed2cSSimon J. Gerraty
96795e3ed2cSSimon J. Gerraty.if exists(${_want})
968db29cad8SSimon J. Gerraty# stop here
969db29cad8SSimon J. Gerraty${.TARGETS:Mboot*}:
970be19d90bSSimon J. Gerraty.elif !make(bootstrap-empty)
971db29cad8SSimon J. Gerraty# find a Makefile.depend to use as _src
972db29cad8SSimon J. Gerraty_src != cd ${.CURDIR} && for m in ${.MAKE.DEPENDFILE_PREFERENCE:T:S,${MACHINE},*,}; do test -s $$m || continue; echo $$m; break; done; echo
973db29cad8SSimon J. Gerraty.if empty(_src)
974be19d90bSSimon J. Gerraty.error cannot find any of ${.MAKE.DEPENDFILE_PREFERENCE:T}${.newline}Use: bootstrap-empty
975db29cad8SSimon J. Gerraty.endif
976db29cad8SSimon J. Gerraty
97795e3ed2cSSimon J. Gerraty_src?= ${.MAKE.DEPENDFILE}
97895e3ed2cSSimon J. Gerraty
97945447996SSimon J. Gerraty.MAKE.DEPENDFILE_BOOTSTRAP_SED+= -e 's/${_src:E:C/,.*//}/${MACHINE}/g'
980db29cad8SSimon J. Gerraty
981be19d90bSSimon J. Gerraty# just create Makefile.depend* for this dir
982db29cad8SSimon J. Gerratybootstrap-this:	.NOTMAIN
98395e3ed2cSSimon J. Gerraty	@echo Bootstrapping ${RELDIR}/${_want:T} from ${_src:T}; \
98495e3ed2cSSimon J. Gerraty	echo You need to build ${RELDIR} to correctly populate it.
98595e3ed2cSSimon J. Gerraty.if ${_src:T} != ${.MAKE.DEPENDFILE_PREFIX:T}
986cac6fd11SSimon J. Gerraty	(cd ${.CURDIR} && sed ${.MAKE.DEPENDFILE_BOOTSTRAP_SED} ${_src} > ${_want:T})
98795e3ed2cSSimon J. Gerraty.else
98845447996SSimon J. Gerraty	cp ${.CURDIR}/${_src:T} ${_want}
98995e3ed2cSSimon J. Gerraty.endif
990db29cad8SSimon J. Gerraty
991be19d90bSSimon J. Gerraty# create Makefile.depend* for this dir and its dependencies
992db29cad8SSimon J. Gerratybootstrap: bootstrap-recurse
993db29cad8SSimon J. Gerratybootstrap-recurse: bootstrap-this
994db29cad8SSimon J. Gerraty
995db29cad8SSimon J. Gerraty_mf := ${.PARSEFILE}
996db29cad8SSimon J. Gerratybootstrap-recurse:	.NOTMAIN .MAKE
997db29cad8SSimon J. Gerraty	@cd ${SRCTOP} && \
998db29cad8SSimon J. Gerraty	for d in `cd ${RELDIR} && ${.MAKE} -B -f ${"${.MAKEFLAGS:M-n}":?${_src}:${.MAKE.DEPENDFILE:T}} -V DIRDEPS`; do \
999db29cad8SSimon J. Gerraty		test -d $$d || d=$${d%.*}; \
1000db29cad8SSimon J. Gerraty		test -d $$d || continue; \
1001db29cad8SSimon J. Gerraty		echo "Checking $$d for bootstrap ..."; \
1002db29cad8SSimon J. Gerraty		(cd $$d && ${.MAKE} -f ${_mf} bootstrap-recurse); \
1003db29cad8SSimon J. Gerraty	done
1004db29cad8SSimon J. Gerraty
1005db29cad8SSimon J. Gerraty.endif
1006be19d90bSSimon J. Gerraty
1007be19d90bSSimon J. Gerraty# create an empty Makefile.depend* to get the ball rolling.
1008be19d90bSSimon J. Gerratybootstrap-empty: .NOTMAIN .NOMETA
100995e3ed2cSSimon J. Gerraty	@echo Creating empty ${RELDIR}/${_want:T}; \
1010be19d90bSSimon J. Gerraty	echo You need to build ${RELDIR} to correctly populate it.
101195e3ed2cSSimon J. Gerraty	@{ echo DIRDEPS=; echo ".include <dirdeps.mk>"; } > ${_want}
1012be19d90bSSimon J. Gerraty
1013db29cad8SSimon J. Gerraty.endif
1014b0c40a00SSimon J. Gerraty.endif
1015