xref: /freebsd/share/mk/dirdeps.mk (revision f5f7c05209ca2c3748fd8b27c5e80ffad49120eb)
1# $Id: dirdeps.mk,v 1.24 2013/02/10 19:21:46 sjg Exp $
2
3# Copyright (c) 2010-2013, Juniper Networks, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27# Much of the complexity here is for supporting cross-building.
28# If a tree does not support that, simply using plain Makefile.depend
29# should provide sufficient clue.
30# Otherwise the recommendation is to use Makefile.depend.${MACHINE}
31# as expected below.
32
33# Note: this file gets multiply included.
34# This is what we do with DIRDEPS
35
36# DIRDEPS:
37#	This is a list of directories - relative to SRCTOP, it is
38#	normally only of interest to .MAKE.LEVEL 0.
39#	In some cases the entry may be qualified with a .<machine>
40#	or .<target_spec> suffix (see TARGET_SPEC_VARS below),
41#	for example to force building something for the pseudo
42#	machines "host" or "common" regardless of current ${MACHINE}.
43#
44#	All unqualified entries end up being qualified with .${TARGET_SPEC}
45#	and partially qualified (if TARGET_SPEC_VARS has multiple
46#	entries) are also expanded to a full .<target_spec>.
47#	The  _DIRDEPS_USE target uses the suffix to set TARGET_SPEC
48#	correctly when visiting each entry.
49#
50#	The fully qualified directory entries are used to construct a
51#	dependency graph that will drive the build later.
52#
53#	Also, for each fully qualified directory target, we will search
54#	using ${.MAKE.DEPENDFILE_PREFERENCE} to find additional
55#	dependencies.  We use Makefile.depend (default value for
56#	.MAKE.DEPENDFILE_PREFIX) to refer to these makefiles to
57#	distinguish them from others.
58#
59#	Each Makefile.depend file sets DEP_RELDIR to be the
60#	the RELDIR (path relative to SRCTOP) for its directory, and
61#	since each Makefile.depend file includes dirdeps.mk, this
62#	processing is recursive and results in .MAKE.LEVEL 0 learning the
63#	dependencies of the tree wrt the initial directory (_DEP_RELDIR).
64#
65# BUILD_AT_LEVEL0
66#	Indicates whether .MAKE.LEVEL 0 builds anything:
67#	if "no" sub-makes are used to build everything,
68#	if "yes" sub-makes are only used to build for other machines.
69#	It is best to use "no", but this can require fixing some
70#	makefiles to not do anything at .MAKE.LEVEL 0.
71#
72# TARGET_SPEC_VARS
73#	The default value is just MACHINE, and for most environments
74#	this is sufficient.  The _DIRDEPS_USE target actually sets
75#	both MACHINE and TARGET_SPEC to the suffix of the current
76#	target so that in the general case TARGET_SPEC can be ignored.
77#
78#	If more than MACHINE is needed then sys.mk needs to decompose
79#	TARGET_SPEC and set the relevant variables accordingly.
80#	It is important that MACHINE be included in and actually be
81#	the first member of TARGET_SPEC_VARS.  This allows other
82#	variables to be considered optional, and some of the treatment
83#	below relies on MACHINE being the first entry.
84#	Note: TARGET_SPEC cannot contain any '.'s so the target
85#	triple used by compiler folk won't work (directly anyway).
86#
87#	For example:
88#
89#		# variables other than MACHINE might be optional
90#		TARGET_SPEC_VARS = MACHINE TARGET_OS
91#		.if ${TARGET_SPEC:Uno:M*,*} != ""
92#		_tspec := ${TARGET_SPEC:S/,/ /g}
93#		MACHINE := ${_tspec:[1]}
94#		TARGET_OS := ${_tspec:[2]}
95#		# etc.
96#		.for v in ${TARGET_SPEC_VARS:O:u}
97#		.if empty($v)
98#		.undef $v
99#		.endif
100#		.endfor
101#		.endif
102#
103
104.if ${.MAKE.LEVEL} == 0
105# only the first instance is interested in all this
106
107# First off, we want to know what ${MACHINE} to build for.
108# This can be complicated if we are using a mixture of ${MACHINE} specific
109# and non-specific Makefile.depend*
110
111.if !target(_DIRDEP_USE)
112# do some setup we only need once
113_CURDIR ?= ${.CURDIR}
114
115# make sure these are empty to start with
116_DEP_TARGET_SPEC =
117_DIRDEP_CHECKED =
118
119# If TARGET_SPEC_VARS is other than just MACHINE
120# it should be set by sys.mk or similar by now.
121# TARGET_SPEC must not contain any '.'s.
122TARGET_SPEC_VARS ?= MACHINE
123# this is what we started with
124TARGET_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts,}
125# this is what we mostly use below
126DEP_TARGET_SPEC = ${TARGET_SPEC_VARS:S,^,DEP_,:@v@${$v:U}@:ts,}
127# make sure we have defaults
128.for v in ${TARGET_SPEC_VARS}
129DEP_$v ?= ${$v}
130.endfor
131
132.if ${TARGET_SPEC_VARS:[#]} > 1
133# Ok, this gets more complex (putting it mildly).
134# In order to stay sane, we need to ensure that all the build_dirs
135# we compute below are fully qualified wrt DEP_TARGET_SPEC.
136# The makefiles may only partially specify (eg. MACHINE only),
137# so we need to construct a set of modifiers to fill in the gaps.
138# jot 10 should output 1 2 3 .. 10
139JOT ?= jot
140_tspec_x := ${${JOT} ${TARGET_SPEC_VARS:[#]}:L:sh}
141# this handles unqualified entries
142M_dep_qual_fixes = C;(/[^/.,]+)$$;\1.${DEP_TARGET_SPEC};
143# there needs to be at least one item missing for these to make sense
144.for i in ${_tspec_x:[2..-1]}
145_tspec_m$i := ${TARGET_SPEC_VARS:[2..$i]:@w@[^,]+@:ts,}
146_tspec_a$i := ,${TARGET_SPEC_VARS:[$i..-1]:@v@$${DEP_$v}@:ts,}
147M_dep_qual_fixes += C;(\.${_tspec_m$i})$$;\1${_tspec_a$i};
148.endfor
149.else
150# A harmless? default.
151M_dep_qual_fixes = U
152.endif
153
154.if !defined(.MAKE.DEPENDFILE_PREFERENCE)
155# .MAKE.DEPENDFILE_PREFERENCE makes the logic below neater?
156# you really want this set by sys.mk or similar
157.MAKE.DEPENDFILE_PREFERENCE = ${_CURDIR}/${.MAKE.DEPENDFILE:T}
158.if ${.MAKE.DEPENDFILE:E} == "${TARGET_SPEC}"
159.if ${TARGET_SPEC} != ${MACHINE}
160.MAKE.DEPENDFILE_PREFERENCE += ${_CURDIR}/${.MAKE.DEPENDFILE:T:R}.$${MACHINE}
161.endif
162.MAKE.DEPENDFILE_PREFERENCE += ${_CURDIR}/${.MAKE.DEPENDFILE:T:R}
163.endif
164.endif
165
166_default_dependfile := ${.MAKE.DEPENDFILE_PREFERENCE:[1]:T}
167_machine_dependfiles := ${.MAKE.DEPENDFILE_PREFERENCE:T:M*${MACHINE}*}
168
169# for machine specific dependfiles we require ${MACHINE} to be at the end
170# also for the sake of sanity we require a common prefix
171.if !defined(.MAKE.DEPENDFILE_PREFIX)
172# knowing .MAKE.DEPENDFILE_PREFIX helps
173.if !empty(_machine_dependfiles)
174.MAKE.DEPENDFILE_PREFIX := ${_machine_dependfiles:[1]:T:R}
175.else
176.MAKE.DEPENDFILE_PREFIX := ${_default_dependfile:T}
177.endif
178.endif
179
180
181# this is how we identify non-machine specific dependfiles
182N_notmachine := ${.MAKE.DEPENDFILE_PREFERENCE:E:N*${MACHINE}*:${M_ListToSkip}}
183
184.endif				# !target(_DIRDEP_USE)
185
186# if we were included recursively _DEP_TARGET_SPEC should be valid.
187.if empty(_DEP_TARGET_SPEC)
188# we may or may not have included a dependfile yet
189_last_dependfile := ${.MAKE.MAKEFILES:M*/${.MAKE.DEPENDFILE_PREFIX}*:[-1]}
190.if !empty(_debug_reldir)
191.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: _last_dependfile='${_last_dependfile}'
192.endif
193
194.if empty(_last_dependfile) || ${_last_dependfile:E:${N_notmachine}} == ""
195# this is all we have to work with
196DEP_MACHINE = ${TARGET_MACHINE:U${MACHINE}}
197_DEP_TARGET_SPEC := ${DEP_TARGET_SPEC}
198.else
199_DEP_TARGET_SPEC = ${_last_dependfile:${M_dep_qual_fixes:ts:}:E}
200.endif
201.if !empty(_last_dependfile)
202# record that we've read dependfile for this
203_DIRDEP_CHECKED += ${_CURDIR}.${TARGET_SPEC}
204.endif
205.endif
206
207# by now _DEP_TARGET_SPEC should be set, parse it.
208.if ${TARGET_SPEC_VARS:[#]} > 1
209# we need to parse DEP_MACHINE may or may not contain more info
210_tspec := ${_DEP_TARGET_SPEC:S/,/ /g}
211.for i in ${_tspec_x}
212DEP_${TARGET_SPEC_VARS:[$i]} := ${_tspec:[$i]}
213.endfor
214.for v in ${TARGET_SPEC_VARS:O:u}
215.if empty(DEP_$v)
216.undef DEP_$v
217.endif
218.endfor
219.else
220DEP_MACHINE := ${_DEP_TARGET_SPEC}
221.endif
222
223# pickup customizations
224# as below you can use !target(_DIRDEP_USE) to protect things
225# which should only be done once.
226.-include "local.dirdeps.mk"
227
228# the first time we are included the _DIRDEP_USE target will not be defined
229# we can use this as a clue to do initialization and other one time things.
230.if !target(_DIRDEP_USE)
231# make sure this target exists
232dirdeps:
233
234# We normally expect to be included by Makefile.depend.*
235# which sets the DEP_* macros below.
236DEP_RELDIR ?= ${RELDIR}
237
238# this can cause lots of output!
239# set to a set of glob expressions that might match RELDIR
240DEBUG_DIRDEPS ?= no
241
242# remember the initial value of DEP_RELDIR - we test for it below.
243_DEP_RELDIR := ${DEP_RELDIR}
244
245# things we skip for host tools
246SKIP_HOSTDIR ?=
247
248NSkipHostDir = ${SKIP_HOSTDIR:N*.host:S,$,.host,:N.host:${M_ListToSkip}}
249NSkipHostDep = ${SKIP_HOSTDIR:R:@d@*/$d*.host@:${M_ListToSkip}}
250
251# things we always skip
252# SKIP_DIRDEPS allows for adding entries on command line.
253SKIP_DIR += .host *.WAIT ${SKIP_DIRDEPS}
254
255.ifdef HOSTPROG
256SKIP_DIR += ${SKIP_HOSTDIR}
257.endif
258
259NSkipDir = ${SKIP_DIR:${M_ListToSkip}}
260
261.if defined(NO_DIRDEPS) || defined(NODIRDEPS)
262# confine ourselves to the original dir
263DIRDEPS_FILTER += M${_DEP_RELDIR}*
264.endif
265
266# we supress SUBDIR when visiting the leaves
267# we assume sys.mk will set MACHINE_ARCH
268# you can add extras to DIRDEP_USE_ENV
269# if there is no makefile in the target directory, we skip it.
270_DIRDEP_USE:	.USE .MAKE
271	@for m in ${.MAKE.MAKEFILE_PREFERENCE}; do \
272		test -s ${.TARGET:R}/$$m || continue; \
273		echo "${TRACER}Checking ${.TARGET:R} for ${.TARGET:E} ..."; \
274		MACHINE_ARCH= NO_SUBDIR=1 ${DIRDEP_USE_ENV} \
275		TARGET_SPEC=${.TARGET:E} \
276		MACHINE=${.TARGET:E} \
277		${.MAKE} -C ${.TARGET:R} || exit 1; \
278		break; \
279	done
280
281.ifdef ALL_MACHINES
282# this is how you limit it to only the machines we have been built for
283# previously.
284.if empty(ONLY_MACHINE_LIST)
285.if !empty(ALL_MACHINE_LIST)
286# ALL_MACHINE_LIST is the list of all legal machines - ignore anything else
287_machine_list != cd ${_CURDIR} && 'ls' -1 ${ALL_MACHINE_LIST:O:u:@m@${.MAKE.DEPENDFILE:T:R}.$m@} 2> /dev/null; echo
288.else
289_machine_list != 'ls' -1 ${_CURDIR}/${.MAKE.DEPENDFILE_PREFIX}.* 2> /dev/null; echo
290.endif
291_only_machines := ${_machine_list:${NIgnoreFiles:UN*.bak}:E:O:u}
292.else
293_only_machines := ${ONLY_MACHINE_LIST}
294.endif
295
296.if empty(_only_machines)
297# we must be boot-strapping
298_only_machines := ${TARGET_MACHINE:U${ALL_MACHINE_LIST:U${DEP_MACHINE}}}
299.endif
300
301.else				# ! ALL_MACHINES
302# if ONLY_MACHINE_LIST is set, we are limited to that
303# if TARGET_MACHINE is set - it is really the same as ONLY_MACHINE_LIST
304# otherwise DEP_MACHINE is it - so DEP_MACHINE will match.
305_only_machines := ${ONLY_MACHINE_LIST:U${TARGET_MACHINE:U${DEP_MACHINE}}:M${DEP_MACHINE}}
306.endif
307
308.if !empty(NOT_MACHINE_LIST)
309_only_machines := ${_only_machines:${NOT_MACHINE_LIST:${M_ListToSkip}}}
310.endif
311
312# make sure we have a starting place?
313DIRDEPS ?= ${RELDIR}
314.endif				# target
315
316_debug_reldir := ${DEBUG_DIRDEPS:@x@${DEP_RELDIR:M$x}${${DEP_RELDIR}.${DEP_MACHINE}:L:M$x}@}
317_debug_search := ${DEBUG_DIRDEPS:@x@${DEP_RELDIR:M$x}${${DEP_RELDIR}.depend:L:M$x}@}
318
319# the rest is done repeatedly for every Makefile.depend we read.
320# if we are anything but the original dir we care only about the
321# machine type we were included for..
322
323.if ${DEP_RELDIR} == "."
324_this_dir := ${SRCTOP}
325.else
326_this_dir := ${SRCTOP}/${DEP_RELDIR}
327.endif
328
329# on rare occasions, there can be a need for extra help
330_dep_hack := ${_this_dir}/${.MAKE.DEPENDFILE_PREFIX}.inc
331.-include "${_dep_hack}"
332
333.if ${DEP_RELDIR} != ${_DEP_RELDIR} || ${DEP_TARGET_SPEC} != ${TARGET_SPEC}
334# this should be all
335_machines := ${DEP_MACHINE}
336.else
337# this is the machine list we actually use below
338_machines := ${_only_machines}
339
340.if defined(HOSTPROG) || ${DEP_MACHINE} == "host"
341# we need to build this guy's dependencies for host as well.
342_machines += host
343.endif
344
345_machines := ${_machines:O:u}
346.endif
347
348.if ${TARGET_SPEC_VARS:[#]} > 1
349# we need to tweak _machines
350_dm := ${DEP_MACHINE}
351_machines := ${_machines:@DEP_MACHINE@${DEP_TARGET_SPEC}@}
352DEP_MACHINE := ${_dm}
353.endif
354
355# reset each time through
356_build_dirs =
357
358.if ${DEP_RELDIR} == ${_DEP_RELDIR}
359# pickup other machines for this dir if necessary
360.if ${BUILD_AT_LEVEL0:Uyes} == "no"
361_build_dirs += ${_machines:@m@${_CURDIR}.$m@}
362.else
363_build_dirs += ${_machines:N${DEP_TARGET_SPEC}:@m@${_CURDIR}.$m@}
364.if ${DEP_TARGET_SPEC} == ${TARGET_SPEC}
365# pickup local dependencies now
366.-include <.depend>
367.endif
368.endif
369.endif
370
371.if !empty(_debug_reldir)
372.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: DIRDEPS='${DIRDEPS}'
373.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: _machines='${_machines}'
374.endif
375
376.if !empty(DIRDEPS)
377
378# this is what we start with
379__depdirs := ${DIRDEPS:${NSkipDir}:${DIRDEPS_FILTER:ts:}:O:u:@d@${SRCTOP}/$d@}
380
381# some entries may be qualified with .<machine>
382# the :M*/*/*.* just tries to limit the dirs we check to likely ones.
383# the ${d:E:M*/*} ensures we don't consider junos/usr.sbin/mgd
384__qual_depdirs := ${__depdirs:M*/*/*.*:@d@${exists($d):?:${"${d:E:M*/*}":?:${exists(${d:R}):?$d:}}}@}
385__unqual_depdirs := ${__depdirs:${__qual_depdirs:Uno:${M_ListToSkip}}}
386
387.if ${DEP_RELDIR} == ${_DEP_RELDIR}
388# if it was called out - we likely need it.
389__hostdpadd := ${DPADD:U.:M${HOST_OBJTOP}/*:S,${HOST_OBJTOP}/,,:H:${NSkipDir}:${DIRDEPS_FILTER:ts:}:S,$,.host,:N.*:@d@${SRCTOP}/$d@}
390__qual_depdirs += ${__hostdpadd}
391.endif
392
393.if !empty(_debug_reldir)
394.info depdirs=${__depdirs}
395.info qualified=${__qual_depdirs}
396.info unqualified=${__unqual_depdirs}
397.endif
398
399# _build_dirs is what we will feed to _DIRDEP_USE
400_build_dirs += \
401	${__qual_depdirs:M*.host:${NSkipHostDir}:N.host} \
402	${__qual_depdirs:N*.host} \
403	${_machines:@m@${__unqual_depdirs:@d@$d.$m@}@}
404
405# qualify everything now
406_build_dirs := ${_build_dirs:${M_dep_qual_fixes:ts:}:O:u}
407
408.endif				# empty DIRDEPS
409
410# Normally if doing make -V something,
411# we do not want to waste time chasing DIRDEPS
412# but if we want to count the number of Makefile.depend* read, we do.
413.if ${.MAKEFLAGS:M-V${_V_READ_DIRDEPS}} == ""
414.if !empty(_build_dirs)
415# this makes it all happen
416dirdeps: ${_build_dirs}
417${_build_dirs}:	_DIRDEP_USE
418
419.if !empty(_debug_reldir)
420.info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: needs: ${_build_dirs}
421.endif
422
423# this builds the dependency graph
424.for m in ${_machines}
425# it would be nice to do :N${.TARGET}
426.if !empty(__qual_depdirs)
427.for q in ${__qual_depdirs:${M_dep_qual_fixes:ts:}:E:O:u:N$m}
428.if !empty(_debug_reldir) || ${DEBUG_DIRDEPS:@x@${${DEP_RELDIR}.$m:L:M$x}${${DEP_RELDIR}.$q:L:M$x}@} != ""
429.info ${DEP_RELDIR}.$m: graph: ${_build_dirs:M*.$q}
430.endif
431${_this_dir}.$m: ${_build_dirs:M*.$q}
432.endfor
433.endif
434.if !empty(_debug_reldir)
435.info ${DEP_RELDIR}.$m: graph: ${_build_dirs:M*.$m:N${_this_dir}.$m}
436.endif
437${_this_dir}.$m: ${_build_dirs:M*.$m:N${_this_dir}.$m}
438.endfor
439
440.endif
441
442# Now find more dependencies - and recurse.
443.for d in ${_build_dirs}
444.if ${_DIRDEP_CHECKED:M$d} == ""
445# once only
446_DIRDEP_CHECKED += $d
447# Note: _build_dirs is fully qualifed so d:R is always the directory
448.if exists(${d:R})
449# Warning: there is an assumption here that MACHINE is always
450# the first entry in TARGET_SPEC_VARS.
451# If TARGET_SPEC and MACHINE are insufficient, you have a problem.
452_m := ${.MAKE.DEPENDFILE_PREFERENCE:T:S;${TARGET_SPEC}$;${d:E};:S;${MACHINE};${d:E:C/,.*//};:@m@${exists(${d:R}/$m):?${d:R}/$m:}@:[1]}
453.if !empty(_m)
454_qm := ${_m:${M_dep_qual_fixes:ts:}}
455.if !empty(_debug_search)
456.info Looking for ${_qm}
457.endif
458# we pass _DEP_TARGET_SPEC to tell the next step what we want
459_DEP_TARGET_SPEC := ${d:E}
460# some makefiles may still look at this
461_DEP_MACHINE := ${d:E:C/,.*//}
462.if !empty(_debug_reldir) && ${_qm} != ${_m}
463.info loading ${_m} for ${d:E}
464.endif
465.include <${_m}>
466.endif
467.endif
468.endif
469.endfor
470
471.endif				# -V
472
473.elif ${.MAKE.LEVEL} > 42
474.error You should have stopped recursing by now.
475.else
476_DEP_RELDIR := ${DEP_RELDIR}
477# pickup local dependencies
478.-include <.depend>
479.endif
480
481