dirdeps.mk revision 246223
1155131Srwatson# $Id: dirdeps.mk,v 1.23 2012/11/06 05:44:03 sjg Exp $
2155131Srwatson
3155131Srwatson# Copyright (c) 2010-2012, Juniper Networks, Inc.
4155131Srwatson# 
5155131Srwatson# Redistribution and use in source and binary forms, with or without
6155131Srwatson# modification, are permitted provided that the following conditions 
7155131Srwatson# are met: 
8155131Srwatson# 1. Redistributions of source code must retain the above copyright
9155131Srwatson#    notice, this list of conditions and the following disclaimer. 
10155131Srwatson# 2. Redistributions in binary form must reproduce the above copyright
11155131Srwatson#    notice, this list of conditions and the following disclaimer in the
12155131Srwatson#    documentation and/or other materials provided with the distribution.  
13155131Srwatson# 
14155131Srwatson# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15155131Srwatson# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16155131Srwatson# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17155131Srwatson# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18155131Srwatson# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19155131Srwatson# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20155131Srwatson# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21155131Srwatson# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22155131Srwatson# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23155131Srwatson# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24155131Srwatson# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
25155131Srwatson
26# Much of the complexity here is for supporting cross-building.
27# If a tree does not support that, simply using plain Makefile.depend
28# should provide sufficient clue.
29# Otherwise the recommendation is to use Makefile.depend.${MACHINE}
30# as expected below.
31
32# Note: this file gets multiply included.
33# This is what we do with DIRDEPS
34
35# DIRDEPS:
36#	This is a list of directories - relative to SRCTOP, it is only
37#	of interest to .MAKE.LEVEL 0.
38#	In some cases the entry may be qualified with a .<machine>
39#	suffix, for example to force building something for the pseudo
40#	machines "host" or "common" regardless of current ${MACHINE}.
41#	All unqualified entries end up being qualified with .${MACHINE}
42#	and _DIRDEPS_USE below, uses the suffix to set MACHINE
43#	correctly when visiting each entry.
44#
45#	Each entry is also converted into a set of paths to look for
46#	Makefile.depend.<machine> to learn the dependencies of each.
47#	Each Makefile.depend.<machine> sets DEP_RELDIR to be the
48#	the RELDIR (path relative to SRCTOP) for its directory, and
49#	DEP_MACHINE to its suffix (<machine>), further since
50#	each Makefile.depend.<machine> includes dirdeps.mk, this
51#	processing is recursive and results in .MAKE.LEVEL 0 learning the
52#	dependencies of the tree wrt the initial directory (_DEP_RELDIR).
53#
54# BUILD_AT_LEVEL0
55#	Indicates whether .MAKE.LEVEL 0 builds anything:
56#	if "no" sub-makes are used to build everything,
57#	if "yes" sub-makes are only used to build for other machines.
58#
59# TARGET_SPEC_VARS
60#	All the description above (and below) assumes <machine> is the
61#	only data needed to control the build.
62#	This is not always the case.  So in addition to setting
63#	MACHINE in the build environment we set TARGET_SPEC which is
64#	composed of the values of TARGET_SPEC_VARS separated by
65#	commas.  The default is just MACHINE.
66#
67#	If more that MACHINE is needed then sys.mk needs to decompose
68#	TARGET_SPEC and set the relevant variables accordingly.
69#	It is important that MACHINE be included in TARGET_SPEC_VARS
70#	since if there is more the value passed as MACHINE will infact
71#	be the TARGET_SPEC.
72#	Note: TARGET_SPEC cannot contain any '.'s so the target
73#	tripple used by compiler folk won't work (directly anyway).
74#
75#	For example:
76#
77#		# variables other than MACHINE might be optional
78#		TARGET_SPEC_VARS = MACHINE TARGET_OS
79#		.if ${TARGET_SPEC:Uno:M*,*} != ""
80#		_tspec := ${TARGET_SPEC:S/,/ /g}
81#		MACHINE := ${_tspec:[1]}
82#		TARGET_OS := ${_tspec:[2]}
83#		# etc.
84#		.for v in ${TARGET_SPEC_VARS:O:u}
85#		.if empty($v)
86#		.undef $v
87#		.endif
88#		.endfor
89#		.endif
90#	
91
92.if ${.MAKE.LEVEL} == 0
93# only the first instance is interested in all this
94
95# First off, we want to know what ${MACHINE} to build for.
96# This can be complicated if we are using a mixture of ${MACHINE} specific
97# and non-specific Makefile.depend*
98
99.if !target(_DIRDEP_USE)
100# do some setup we only need once
101_CURDIR ?= ${.CURDIR}
102
103# If TARGET_SPEC_VARS is other than just MACHINE
104# it should be set by sys.mk or similar by now.
105# TARGET_SPEC must not contain any '.'s.
106TARGET_SPEC_VARS ?= MACHINE
107TARGET_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts,}
108
109.if !defined(.MAKE.DEPENDFILE_PREFERENCE)
110# this makes the logic below neater?
111.MAKE.DEPENDFILE_PREFERENCE = ${_CURDIR}/${.MAKE.DEPENDFILE:T}
112.if ${.MAKE.DEPENDFILE:E} == "${TARGET_SPEC}"
113.if ${TARGET_SPEC} != ${MACHINE}
114.MAKE.DEPENDFILE_PREFERENCE += ${_CURDIR}/${.MAKE.DEPENDFILE:T:R}.$${MACHINE}
115.endif
116.MAKE.DEPENDFILE_PREFERENCE += ${_CURDIR}/${.MAKE.DEPENDFILE:T:R}
117.endif
118.endif
119
120_default_dependfile := ${.MAKE.DEPENDFILE_PREFERENCE:[1]:T}
121_machine_dependfiles := ${.MAKE.DEPENDFILE_PREFERENCE:M*.${TARGET_SPEC}} \
122	${.MAKE.DEPENDFILE_PREFERENCE:M*.${MACHINE}}
123
124# for machine specific dependfiles we require ${MACHINE} to be at the end
125# also for the sake of sanity we require a common prefix
126.if !defined(.MAKE.DEPENDFILE_PREFIX)
127.if !empty(_machine_dependfiles)
128.MAKE.DEPENDFILE_PREFIX := ${_machine_dependfiles:[1]:T:R}
129.else
130.MAKE.DEPENDFILE_PREFIX := ${_default_dependfile:T}
131.endif
132.endif
133
134
135# this is how we identify non-machine specific dependfiles
136N_notmachine := ${.MAKE.DEPENDFILE_PREFERENCE:E:N${TARGET_SPEC}:N${MACHINE}:${M_ListToSkip}}
137
138.endif				# !target(_DIRDEP_USE)
139
140_last_dependfile := ${.MAKE.MAKEFILES:M*/${.MAKE.DEPENDFILE_PREFIX}*:[-1]}
141
142# Note: if a makefile is read many times, the above
143# will not work, so we also test for DEP_MACHINE==depend below.
144.if empty(_last_dependfile)
145# we haven't included one yet
146DEP_MACHINE ?= ${TARGET_MACHINE:U${TARGET_SPEC}}
147# else it should be correctly set by ${.MAKE.DEPENDFILE}
148.elif ${_last_dependfile:E:${N_notmachine}} == "" || ${DEP_MACHINE:Uno:${N_notmachine}} == ""
149# don't rely on manually maintained files to be correct
150DEP_MACHINE := ${_DEP_MACHINE:U${TARGET_SPEC}}
151.else
152# just in case
153DEP_MACHINE ?= ${_last_dependfile:E}
154.endif
155
156# pickup customizations
157# as below you can use !target(_DIRDEP_USE) to protect things
158# which should only be done once.
159.-include "local.dirdeps.mk"
160
161# the first time we are included the _DIRDEP_USE target will not be defined
162# we can use this as a clue to do initialization and other one time things.
163.if !target(_DIRDEP_USE)
164# make sure this target exists
165dirdeps:
166
167# We normally expect to be included by Makefile.depend.*
168# which sets the DEP_* macros below.
169DEP_RELDIR ?= ${RELDIR}
170
171# this can cause lots of output!
172# set to a set of glob expressions that might match RELDIR
173DEBUG_DIRDEPS ?= no
174
175# remember the initial value of DEP_RELDIR - we test for it below.
176_DEP_RELDIR := ${DEP_RELDIR}
177
178# things we skip for host tools
179SKIP_HOSTDIR ?=
180
181NSkipHostDir = ${SKIP_HOSTDIR:N*.host:S,$,.host,:N.host:${M_ListToSkip}}
182NSkipHostDep = ${SKIP_HOSTDIR:R:@d@*/$d*.host@:${M_ListToSkip}}
183
184# things we always skip
185# SKIP_DIRDEPS allows for adding entries on command line.
186SKIP_DIR += .host *.WAIT ${SKIP_DIRDEPS}
187
188.ifdef HOSTPROG
189SKIP_DIR += ${SKIP_HOSTDIR}
190.endif
191
192NSkipDir = ${SKIP_DIR:${M_ListToSkip}}
193
194.if defined(NO_DIRDEPS) || defined(NODIRDEPS)
195# confine ourselves to the original dir
196DIRDEPS_FILTER += M${_DEP_RELDIR}*
197.endif
198
199# we supress SUBDIR when visiting the leaves
200# we assume sys.mk will set MACHINE_ARCH
201_DIRDEP_USE:	.USE .MAKE
202	@for m in ${.MAKE.MAKEFILE_PREFERENCE}; do \
203		test -s ${.TARGET:R}/$$m || continue; \
204		echo "${TRACER}Checking ${.TARGET:R} for ${.TARGET:E} ..."; \
205		TARGET_SPEC=${.TARGET:E} \
206		MACHINE=${.TARGET:E} MACHINE_ARCH= NO_SUBDIR=1 \
207		${.MAKE} -C ${.TARGET:R} || exit 1; \
208		break; \
209	done
210
211.ifdef ALL_MACHINES
212# this is how you limit it to only the machines we have been built for
213# previously.
214.if empty(ONLY_MACHINE_LIST)
215.if !empty(ALL_MACHINE_LIST)
216# ALL_MACHINE_LIST is the list of all legal machines - ignore anything else
217_machine_list != cd ${_CURDIR} && 'ls' -1 ${ALL_MACHINE_LIST:O:u:@m@${.MAKE.DEPENDFILE:T:R}.$m@} 2> /dev/null; echo
218.else
219_machine_list != 'ls' -1 ${_CURDIR}/${.MAKE.DEPENDFILE_PREFIX}.* 2> /dev/null; echo
220.endif
221_only_machines := ${_machine_list:${NIgnoreFiles:UN*.bak}:E:O:u}
222.else
223_only_machines := ${ONLY_MACHINE_LIST}
224.endif
225
226.if empty(_only_machines)
227# we must be boot-strapping
228_only_machines := ${TARGET_MACHINE:U${ALL_MACHINE_LIST:U${DEP_MACHINE}}}
229.endif
230
231.else				# ! ALL_MACHINES
232# if ONLY_MACHINE_LIST is set, we are limited to that
233# if TARGET_MACHINE is set - it is really the same as ONLY_MACHINE_LIST
234# otherwise DEP_MACHINE is it - so DEP_MACHINE will match.
235_only_machines := ${ONLY_MACHINE_LIST:U${TARGET_MACHINE:U${DEP_MACHINE}}:M${DEP_MACHINE}}
236.endif
237
238.if !empty(NOT_MACHINE_LIST)
239_only_machines := ${_only_machines:${NOT_MACHINE_LIST:${M_ListToSkip}}}
240.endif
241
242# make sure we have a starting place?
243DIRDEPS ?= ${RELDIR}
244.endif				# target 
245
246_debug_reldir := ${DEBUG_DIRDEPS:@x@${DEP_RELDIR:M$x}${${DEP_RELDIR}.${DEP_MACHINE}:L:M$x}@}
247_debug_search := ${DEBUG_DIRDEPS:@x@${DEP_RELDIR:M$x}${${DEP_RELDIR}.depend:L:M$x}@}
248
249# the rest is done repeatedly for every Makefile.depend we read.
250# if we are anything but the original dir we care only about the
251# machine type we were included for..
252
253.if ${DEP_RELDIR} == "."
254_this_dir := ${SRCTOP}
255.else
256_this_dir := ${SRCTOP}/${DEP_RELDIR}
257.endif
258
259# on rare occasions, there can be a need for extra help
260_dep_hack := ${_this_dir}/${.MAKE.DEPENDFILE_PREFIX}.inc
261.-include "${_dep_hack}"
262
263.if ${DEP_RELDIR} != ${_DEP_RELDIR} || ${DEP_MACHINE} != ${TARGET_SPEC}
264# this should be all
265_machines := ${DEP_MACHINE}
266.else
267# this is the machine list we actually use below
268_machines := ${_only_machines}
269
270.if defined(HOSTPROG) || ${DEP_MACHINE} == "host"
271# we need to build this guy's dependencies for host as well.
272_machines += host
273.endif
274
275_machines := ${_machines:O:u}
276.endif
277
278# reset these each time through
279_build_dirs =
280_depdir_files =
281
282.if ${DEP_RELDIR} == ${_DEP_RELDIR}
283# pickup other machines for this dir if necessary
284.if ${BUILD_AT_LEVEL0:Uyes} == "no"
285_build_dirs += ${_machines:@m@${_CURDIR}.$m@}
286.else
287_build_dirs += ${_machines:N${DEP_MACHINE}:@m@${_CURDIR}.$m@}
288.if ${DEP_MACHINE} == ${TARGET_SPEC}
289# pickup local dependencies now
290.-include <.depend>
291.endif
292.endif
293.endif
294
295.if !empty(_debug_reldir)
296.info ${DEP_RELDIR}.${DEP_MACHINE}: _last_dependfile='${_last_dependfile}'
297.info ${DEP_RELDIR}.${DEP_MACHINE}: DIRDEPS='${DIRDEPS}'
298.info ${DEP_RELDIR}.${DEP_MACHINE}: _machines='${_machines}' 
299.endif
300
301.if !empty(DIRDEPS)
302
303# this is what we start with
304__depdirs := ${DIRDEPS:${NSkipDir}:${DIRDEPS_FILTER:ts:}:O:u:@d@${SRCTOP}/$d@}
305
306# some entries may be qualified with .<machine> 
307# the :M*/*/*.* just tries to limit the dirs we check to likely ones.
308# the ${d:E:M*/*} ensures we don't consider junos/usr.sbin/mgd
309__qual_depdirs := ${__depdirs:M*/*/*.*:@d@${exists($d):?:${"${d:E:M*/*}":?:${exists(${d:R}):?$d:}}}@}
310__unqual_depdirs := ${__depdirs:${__qual_depdirs:Uno:${M_ListToSkip}}}
311
312.if ${DEP_RELDIR} == ${_DEP_RELDIR}
313# if it was called out - we likely need it.
314__hostdpadd := ${DPADD:U.:M${HOST_OBJTOP}/*:S,${HOST_OBJTOP}/,,:H:${NSkipDir}:${DIRDEPS_FILTER:ts:}:S,$,.host,:N.*:@d@${SRCTOP}/$d@}
315__qual_depdirs += ${__hostdpadd}
316.endif
317
318.if !empty(_debug_reldir)
319.info depdirs=${__depdirs}
320.info qualified=${__qual_depdirs}
321.info unqualified=${__unqual_depdirs}
322.endif
323
324# _build_dirs is what we will feed to _DIRDEP_USE
325_build_dirs += \
326	${__qual_depdirs:M*.host:${NSkipHostDir}:N.host} \
327	${__qual_depdirs:N*.host} \
328	${_machines:@m@${__unqual_depdirs:@d@$d.$m@}@}
329
330_build_dirs := ${_build_dirs:O:u}
331
332# this is where we will pick up more dependencies from
333# the inner inline loops look complex, but save a significant
334# amount of memory compared to a .for loop.
335_depdir_files =
336.for d in ${_build_dirs}
337.if exists($d)
338# easy, we're building for ${MACHINE}
339_depdir_files += ${.MAKE.DEPENDFILE_PREFERENCE:T:@m@${exists($d/$m):?$d/$m:}@:[1]}
340.elif exists(${d:R}) && ${d:R:T} == ${d:T:R}
341# a little more complex - building for another machine
342# we will ensure the file is qualified with a machine
343# so that if necessary _DEP_MACHINE can be set below
344_depdir_files += ${.MAKE.DEPENDFILE_PREFERENCE:T:S,.${TARGET_SPEC}$,.${d:E},:S,.${MACHINE}$,.${d:E},:@m@${exists(${d:R}/$m):?${d:R}/$m:}@:[1]:@m@${"${m:M*.${d:E}}":?$m:$m.${d:E}}@}
345.endif
346.endfor
347
348# clean up
349_depdir_files := ${_depdir_files:O:u}
350
351.endif				# empty DIRDEPS
352
353# Normally if doing make -V something,
354# we do not want to waste time chasing DIRDEPS
355# but if we want to count the number of Makefile.depend* read, we do.
356.if ${.MAKEFLAGS:M-V${_V_READ_DIRDEPS}} == ""
357.if !empty(_build_dirs)
358# this makes it all happen
359dirdeps: ${_build_dirs}
360${_build_dirs}:	_DIRDEP_USE
361
362.if !empty(_debug_reldir)
363.info ${DEP_RELDIR}.${DEP_MACHINE}: ${_build_dirs}
364.endif
365
366.for m in ${_machines}
367# it would be nice to do :N${.TARGET}
368.if !empty(__qual_depdirs)
369.for q in ${__qual_depdirs:E:O:u:N$m}
370.if !empty(_debug_reldir) || ${DEBUG_DIRDEPS:@x@${${DEP_RELDIR}.$m:L:M$x}${${DEP_RELDIR}.$q:L:M$x}@} != ""
371.info ${DEP_RELDIR}.$m: ${_build_dirs:M*.$q}
372.endif
373${_this_dir}.$m: ${_build_dirs:M*.$q}
374.endfor
375.endif
376.if !empty(_debug_reldir)
377.info ${DEP_RELDIR}.$m: ${_build_dirs:M*.$m:N${_this_dir}.$m}
378.endif
379${_this_dir}.$m: ${_build_dirs:M*.$m:N${_this_dir}.$m}
380.endfor
381
382.endif
383
384.for d in ${_depdir_files}
385.if ${.MAKE.MAKEFILES:M${d}} == ""
386.if !empty(_debug_search)
387.info Looking for $d
388.endif
389.if exists($d)
390.include <$d>
391.elif exists(${d:R})
392# an unqualified file exists, we qualified it above so we can set _DEP_MACHINE
393# it might be manually maintained and shared by all machine types
394# tell it the machine we are interested in.
395_DEP_MACHINE := ${d:E}
396.if !empty(_debug_reldir)
397.info loading ${d:R} for ${_DEP_MACHINE}
398.endif
399# pretend we read $d, so we don't come by here again.
400.MAKE.MAKEFILES += $d
401.include <${d:R}>
402.endif
403.endif
404.endfor
405.endif				# -V
406
407.elif ${.MAKE.LEVEL} > 42
408.error You should have stopped recursing by now.
409.else
410_DEP_RELDIR := ${DEP_RELDIR}
411# pickup local dependencies
412.-include <.depend>
413.endif
414
415