]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/mk/dirdeps.mk
Make libxo depend on libutil because it uses humanize_number after r287111
[FreeBSD/FreeBSD.git] / share / mk / dirdeps.mk
1 # $FreeBSD$
2 # $Id: dirdeps.mk,v 1.54 2015/06/08 20:55:11 sjg Exp $
3
4 # Copyright (c) 2010-2013, Juniper Networks, Inc.
5 # All rights reserved.
6
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions 
9 # are met: 
10 # 1. Redistributions of source code must retain the above copyright
11 #    notice, this list of conditions and the following disclaimer. 
12 # 2. Redistributions in binary form must reproduce the above copyright
13 #    notice, this list of conditions and the following disclaimer in the
14 #    documentation and/or other materials provided with the distribution.  
15
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
27
28 # Much of the complexity here is for supporting cross-building.
29 # If a tree does not support that, simply using plain Makefile.depend
30 # should provide sufficient clue.
31 # Otherwise the recommendation is to use Makefile.depend.${MACHINE}
32 # as expected below.
33
34 # Note: this file gets multiply included.
35 # This is what we do with DIRDEPS
36
37 # DIRDEPS:
38 #       This is a list of directories - relative to SRCTOP, it is
39 #       normally only of interest to .MAKE.LEVEL 0.
40 #       In some cases the entry may be qualified with a .<machine>
41 #       or .<target_spec> suffix (see TARGET_SPEC_VARS below),
42 #       for example to force building something for the pseudo
43 #       machines "host" or "common" regardless of current ${MACHINE}.
44 #       
45 #       All unqualified entries end up being qualified with .${TARGET_SPEC}
46 #       and partially qualified (if TARGET_SPEC_VARS has multiple
47 #       entries) are also expanded to a full .<target_spec>.
48 #       The  _DIRDEP_USE target uses the suffix to set TARGET_SPEC
49 #       correctly when visiting each entry.
50 #
51 #       The fully qualified directory entries are used to construct a
52 #       dependency graph that will drive the build later.
53 #       
54 #       Also, for each fully qualified directory target, we will search
55 #       using ${.MAKE.DEPENDFILE_PREFERENCE} to find additional
56 #       dependencies.  We use Makefile.depend (default value for
57 #       .MAKE.DEPENDFILE_PREFIX) to refer to these makefiles to
58 #       distinguish them from others.
59 #       
60 #       Each Makefile.depend file sets DEP_RELDIR to be the
61 #       the RELDIR (path relative to SRCTOP) for its directory, and
62 #       since each Makefile.depend file includes dirdeps.mk, this
63 #       processing is recursive and results in .MAKE.LEVEL 0 learning the
64 #       dependencies of the tree wrt the initial directory (_DEP_RELDIR).
65 #
66 # BUILD_AT_LEVEL0
67 #       Indicates whether .MAKE.LEVEL 0 builds anything:
68 #       if "no" sub-makes are used to build everything,
69 #       if "yes" sub-makes are only used to build for other machines.
70 #       It is best to use "no", but this can require fixing some
71 #       makefiles to not do anything at .MAKE.LEVEL 0.
72 #
73 # TARGET_SPEC_VARS
74 #       The default value is just MACHINE, and for most environments
75 #       this is sufficient.  The _DIRDEP_USE target actually sets
76 #       both MACHINE and TARGET_SPEC to the suffix of the current
77 #       target so that in the general case TARGET_SPEC can be ignored.
78 #
79 #       If more than MACHINE is needed then sys.mk needs to decompose
80 #       TARGET_SPEC and set the relevant variables accordingly.
81 #       It is important that MACHINE be included in and actually be
82 #       the first member of TARGET_SPEC_VARS.  This allows other
83 #       variables to be considered optional, and some of the treatment
84 #       below relies on MACHINE being the first entry.
85 #       Note: TARGET_SPEC cannot contain any '.'s so the target
86 #       triple used by compiler folk won't work (directly anyway).
87 #
88 #       For example:
89 #
90 #               # Always list MACHINE first, 
91 #               # other variables might be optional.
92 #               TARGET_SPEC_VARS = MACHINE TARGET_OS
93 #               .if ${TARGET_SPEC:Uno:M*,*} != ""
94 #               _tspec := ${TARGET_SPEC:S/,/ /g}
95 #               MACHINE := ${_tspec:[1]}
96 #               TARGET_OS := ${_tspec:[2]}
97 #               # etc.
98 #               # We need to stop that TARGET_SPEC affecting any submakes
99 #               # and deal with MACHINE=${TARGET_SPEC} in the environment.
100 #               TARGET_SPEC =
101 #               # export but do not track
102 #               .export-env TARGET_SPEC 
103 #               .export ${TARGET_SPEC_VARS}
104 #               .for v in ${TARGET_SPEC_VARS:O:u}
105 #               .if empty($v)
106 #               .undef $v
107 #               .endif
108 #               .endfor
109 #               .endif
110 #               # make sure we know what TARGET_SPEC is
111 #               # as we may need it to find Makefile.depend*
112 #               TARGET_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts,}
113 #       
114
115 # touch this at your peril
116 _DIRDEP_USE_LEVEL?= 0
117 .if ${.MAKE.LEVEL} == ${_DIRDEP_USE_LEVEL}
118 # only the first instance is interested in all this
119
120 # First off, we want to know what ${MACHINE} to build for.
121 # This can be complicated if we are using a mixture of ${MACHINE} specific
122 # and non-specific Makefile.depend*
123
124 .if !target(_DIRDEP_USE)
125 # do some setup we only need once
126 _CURDIR ?= ${.CURDIR}
127 _OBJDIR ?= ${.OBJDIR}
128
129 now_utc = ${%s:L:gmtime}
130 .if !defined(start_utc)
131 start_utc := ${now_utc}
132 .endif
133
134 # make sure these are empty to start with
135 _DEP_TARGET_SPEC =
136 _DIRDEP_CHECKED =
137
138 # If TARGET_SPEC_VARS is other than just MACHINE
139 # it should be set by sys.mk or similar by now.
140 # TARGET_SPEC must not contain any '.'s.
141 TARGET_SPEC_VARS ?= MACHINE
142 # this is what we started with
143 TARGET_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts,}
144 # this is what we mostly use below
145 DEP_TARGET_SPEC = ${TARGET_SPEC_VARS:S,^,DEP_,:@v@${$v:U}@:ts,}
146 # make sure we have defaults
147 .for v in ${TARGET_SPEC_VARS}
148 DEP_$v ?= ${$v}
149 .endfor
150
151 .if ${TARGET_SPEC_VARS:[#]} > 1
152 # Ok, this gets more complex (putting it mildly).
153 # In order to stay sane, we need to ensure that all the build_dirs
154 # we compute below are fully qualified wrt DEP_TARGET_SPEC.
155 # The makefiles may only partially specify (eg. MACHINE only),
156 # so we need to construct a set of modifiers to fill in the gaps.
157 # jot 10 should output 1 2 3 .. 10
158 JOT ?= jot
159 _tspec_x := ${${JOT} ${TARGET_SPEC_VARS:[#]}:L:sh}
160 # this handles unqualified entries
161 M_dep_qual_fixes = C;(/[^/.,]+)$$;\1.$${DEP_TARGET_SPEC};
162 # there needs to be at least one item missing for these to make sense
163 .for i in ${_tspec_x:[2..-1]}
164 _tspec_m$i := ${TARGET_SPEC_VARS:[2..$i]:@w@[^,]+@:ts,}
165 _tspec_a$i := ,${TARGET_SPEC_VARS:[$i..-1]:@v@$$$${DEP_$v}@:ts,}
166 M_dep_qual_fixes += C;(\.${_tspec_m$i})$$;\1${_tspec_a$i};
167 .endfor
168 .else
169 # A harmless? default.
170 M_dep_qual_fixes = U
171 .endif
172
173 .if !defined(.MAKE.DEPENDFILE_PREFERENCE)
174 # .MAKE.DEPENDFILE_PREFERENCE makes the logic below neater?
175 # you really want this set by sys.mk or similar
176 .MAKE.DEPENDFILE_PREFERENCE = ${_CURDIR}/${.MAKE.DEPENDFILE:T}
177 .if ${.MAKE.DEPENDFILE:E} == "${TARGET_SPEC}"
178 .if ${TARGET_SPEC} != ${MACHINE}
179 .MAKE.DEPENDFILE_PREFERENCE += ${_CURDIR}/${.MAKE.DEPENDFILE:T:R}.$${MACHINE}
180 .endif
181 .MAKE.DEPENDFILE_PREFERENCE += ${_CURDIR}/${.MAKE.DEPENDFILE:T:R}
182 .endif
183 .endif
184
185 _default_dependfile := ${.MAKE.DEPENDFILE_PREFERENCE:[1]:T}
186 _machine_dependfiles := ${.MAKE.DEPENDFILE_PREFERENCE:T:M*${MACHINE}*}
187
188 # for machine specific dependfiles we require ${MACHINE} to be at the end
189 # also for the sake of sanity we require a common prefix
190 .if !defined(.MAKE.DEPENDFILE_PREFIX)
191 # knowing .MAKE.DEPENDFILE_PREFIX helps
192 .if !empty(_machine_dependfiles)
193 .MAKE.DEPENDFILE_PREFIX := ${_machine_dependfiles:[1]:T:R}
194 .else
195 .MAKE.DEPENDFILE_PREFIX := ${_default_dependfile:T}
196 .endif
197 .endif
198
199
200 # this is how we identify non-machine specific dependfiles
201 N_notmachine := ${.MAKE.DEPENDFILE_PREFERENCE:E:N*${MACHINE}*:${M_ListToSkip}}
202
203 .endif                          # !target(_DIRDEP_USE)
204
205 # if we were included recursively _DEP_TARGET_SPEC should be valid.
206 .if empty(_DEP_TARGET_SPEC)
207 # we may or may not have included a dependfile yet
208 .if defined(.INCLUDEDFROMFILE)
209 _last_dependfile := ${.INCLUDEDFROMFILE:M${.MAKE.DEPENDFILE_PREFIX}*}
210 .else
211 _last_dependfile := ${.MAKE.MAKEFILES:M*/${.MAKE.DEPENDFILE_PREFIX}*:[-1]}
212 .endif
213 .if ${_debug_reldir:U0}
214 .info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: _last_dependfile='${_last_dependfile}'
215 .endif
216
217 .if empty(_last_dependfile) || ${_last_dependfile:E:${N_notmachine}} == ""
218 # this is all we have to work with
219 DEP_MACHINE = ${TARGET_MACHINE:U${MACHINE}}
220 _DEP_TARGET_SPEC := ${DEP_TARGET_SPEC}
221 .else
222 _DEP_TARGET_SPEC = ${_last_dependfile:${M_dep_qual_fixes:ts:}:E}
223 .endif
224 .if !empty(_last_dependfile)
225 # record that we've read dependfile for this
226 _DIRDEP_CHECKED += ${_CURDIR}.${TARGET_SPEC}
227 .endif
228 .endif
229
230 # by now _DEP_TARGET_SPEC should be set, parse it.
231 .if ${TARGET_SPEC_VARS:[#]} > 1
232 # we need to parse DEP_MACHINE may or may not contain more info
233 _tspec := ${_DEP_TARGET_SPEC:S/,/ /g}
234 .for i in ${_tspec_x}
235 DEP_${TARGET_SPEC_VARS:[$i]} := ${_tspec:[$i]}
236 .endfor
237 .for v in ${TARGET_SPEC_VARS:O:u}
238 .if empty(DEP_$v)
239 .undef DEP_$v
240 .endif
241 .endfor
242 .else
243 DEP_MACHINE := ${_DEP_TARGET_SPEC}
244 .endif
245
246 # pickup customizations
247 # as below you can use !target(_DIRDEP_USE) to protect things
248 # which should only be done once.
249 .-include "local.dirdeps.mk"
250
251 # the first time we are included the _DIRDEP_USE target will not be defined
252 # we can use this as a clue to do initialization and other one time things.
253 .if !target(_DIRDEP_USE)
254 # make sure this target exists
255 dirdeps: beforedirdeps .WAIT
256 beforedirdeps:
257
258 # We normally expect to be included by Makefile.depend.*
259 # which sets the DEP_* macros below.
260 DEP_RELDIR ?= ${RELDIR}
261
262 # this can cause lots of output!
263 # set to a set of glob expressions that might match RELDIR
264 DEBUG_DIRDEPS ?= no
265
266 # remember the initial value of DEP_RELDIR - we test for it below.
267 _DEP_RELDIR := ${DEP_RELDIR}
268
269 # things we skip for host tools
270 SKIP_HOSTDIR ?=
271
272 NSkipHostDir = ${SKIP_HOSTDIR:N*.host*:S,$,.host*,:N.host*:S,^,${SRCTOP}/,:${M_ListToSkip}}
273
274 # things we always skip
275 # SKIP_DIRDEPS allows for adding entries on command line.
276 SKIP_DIR += .host *.WAIT ${SKIP_DIRDEPS}
277 SKIP_DIR.host += ${SKIP_HOSTDIR}
278
279 DEP_SKIP_DIR = ${SKIP_DIR} \
280         ${SKIP_DIR.${DEP_TARGET_SPEC}:U} \
281         ${SKIP_DIR.${DEP_MACHINE}:U} \
282         ${SKIP_DIRDEPS.${DEP_MACHINE}:U}
283
284 NSkipDir = ${DEP_SKIP_DIR:${M_ListToSkip}}
285
286 .if defined(NO_DIRDEPS) || defined(NODIRDEPS) || defined(WITHOUT_DIRDEPS)
287 # confine ourselves to the original dir
288 DIRDEPS_FILTER += M${_DEP_RELDIR}*
289 .endif
290
291 # this is what we run below
292 DIRDEP_MAKE?= ${.MAKE}
293
294 # we suppress SUBDIR when visiting the leaves
295 # we assume sys.mk will set MACHINE_ARCH
296 # you can add extras to DIRDEP_USE_ENV
297 # if there is no makefile in the target directory, we skip it.
298 _DIRDEP_USE:    .USE .MAKE
299         @for m in ${.MAKE.MAKEFILE_PREFERENCE}; do \
300                 test -s ${.TARGET:R}/$$m || continue; \
301                 echo "${TRACER}Checking ${.TARGET:R} for ${.TARGET:E} ..."; \
302                 MACHINE_ARCH= NO_SUBDIR=1 ${DIRDEP_USE_ENV} \
303                 TARGET_SPEC=${.TARGET:E} \
304                 MACHINE=${.TARGET:E} \
305                 ${DIRDEP_MAKE} -C ${.TARGET:R} || exit 1; \
306                 break; \
307         done
308
309 .ifdef ALL_MACHINES
310 # this is how you limit it to only the machines we have been built for
311 # previously.
312 .if empty(ONLY_MACHINE_LIST)
313 .if !empty(ALL_MACHINE_LIST)
314 # ALL_MACHINE_LIST is the list of all legal machines - ignore anything else
315 _machine_list != cd ${_CURDIR} && 'ls' -1 ${ALL_MACHINE_LIST:O:u:@m@${.MAKE.DEPENDFILE:T:R}.$m@} 2> /dev/null; echo
316 .else
317 _machine_list != 'ls' -1 ${_CURDIR}/${.MAKE.DEPENDFILE_PREFIX}.* 2> /dev/null; echo
318 .endif
319 _only_machines := ${_machine_list:${NIgnoreFiles:UN*.bak}:E:O:u}
320 .else
321 _only_machines := ${ONLY_MACHINE_LIST}
322 .endif
323
324 .if empty(_only_machines)
325 # we must be boot-strapping
326 _only_machines := ${TARGET_MACHINE:U${ALL_MACHINE_LIST:U${DEP_MACHINE}}}
327 .endif
328
329 .else                           # ! ALL_MACHINES
330 # if ONLY_MACHINE_LIST is set, we are limited to that
331 # if TARGET_MACHINE is set - it is really the same as ONLY_MACHINE_LIST
332 # otherwise DEP_MACHINE is it - so DEP_MACHINE will match.
333 _only_machines := ${ONLY_MACHINE_LIST:U${TARGET_MACHINE:U${DEP_MACHINE}}:M${DEP_MACHINE}}
334 .endif
335
336 .if !empty(NOT_MACHINE_LIST)
337 _only_machines := ${_only_machines:${NOT_MACHINE_LIST:${M_ListToSkip}}}
338 .endif
339
340 # make sure we have a starting place?
341 DIRDEPS ?= ${RELDIR}
342 .endif                          # target 
343
344 # if repeatedly building the same target, 
345 # we can avoid the overhead of re-computing the tree dependencies.
346 MK_DIRDEPS_CACHE ?= no
347 BUILD_DIRDEPS_CACHE ?= no
348 BUILD_DIRDEPS ?= yes
349
350 .if !defined(NO_DIRDEPS)
351 .if ${MK_DIRDEPS_CACHE} == "yes"
352 # this is where we will cache all our work
353 DIRDEPS_CACHE?= ${_OBJDIR}/dirdeps.cache${.TARGETS:Nall:O:u:ts-:S,/,_,g:S,^,.,:N.}
354
355 # just ensure this exists
356 build-dirdeps:
357
358 M_oneperline = @x@\\${.newline} $$x@
359
360 .if ${BUILD_DIRDEPS_CACHE} == "no" 
361 .if !target(dirdeps-cached)
362 # we do this via sub-make
363 BUILD_DIRDEPS = no
364
365 dirdeps: dirdeps-cached
366 dirdeps-cached: ${DIRDEPS_CACHE} .MAKE
367         @echo "${TRACER}Using ${DIRDEPS_CACHE}"
368         @MAKELEVEL=${.MAKE.LEVEL} ${.MAKE} -C ${_CURDIR} -f ${DIRDEPS_CACHE} \
369                 dirdeps MK_DIRDEPS_CACHE=no BUILD_DIRDEPS=no
370
371 # these should generally do
372 BUILD_DIRDEPS_MAKEFILE ?= ${MAKEFILE}
373 BUILD_DIRDEPS_TARGETS ?= ${.TARGETS}
374
375 # we need the .meta file to ensure we update if 
376 # any of the Makefile.depend* changed.
377 # We do not want to compare the command line though.
378 ${DIRDEPS_CACHE}:       .META .NOMETA_CMP
379         +@{ echo '# Autogenerated - do NOT edit!'; echo; \
380         echo 'BUILD_DIRDEPS=no'; echo; \
381         echo '.include <dirdeps.mk>'; \
382         } > ${.TARGET}.new
383         +@MAKELEVEL=${.MAKE.LEVEL} DIRDEPS_CACHE=${DIRDEPS_CACHE} \
384         DIRDEPS="${DIRDEPS}" \
385         MAKEFLAGS= ${.MAKE} -C ${_CURDIR} -f ${BUILD_DIRDEPS_MAKEFILE} \
386         ${BUILD_DIRDEPS_TARGETS} BUILD_DIRDEPS_CACHE=yes \
387         .MAKE.DEPENDFILE=.none \
388         3>&1 1>&2 | sed 's,${SRCTOP},$${SRCTOP},g' >> ${.TARGET}.new && \
389         mv ${.TARGET}.new ${.TARGET}
390
391 .endif
392 .elif !target(_count_dirdeps)
393 # we want to capture the dirdeps count in the cache
394 .END: _count_dirdeps
395 _count_dirdeps: .NOMETA
396         @echo '.info $${.newline}$${TRACER}Makefiles read: total=${.MAKE.MAKEFILES:[#]} depend=${.MAKE.MAKEFILES:M*depend*:[#]} dirdeps=${.ALLTARGETS:M${SRCTOP}*:O:u:[#]}' >&3
397
398 .endif
399 .elif !make(dirdeps) && !target(_count_dirdeps)
400 beforedirdeps: _count_dirdeps
401 _count_dirdeps: .NOMETA
402         @echo "${TRACER}Makefiles read: total=${.MAKE.MAKEFILES:[#]} depend=${.MAKE.MAKEFILES:M*depend*:[#]} dirdeps=${.ALLTARGETS:M${SRCTOP}*:O:u:[#]} seconds=`expr ${now_utc} - ${start_utc}`"
403
404 .endif
405 .endif
406
407 .if ${BUILD_DIRDEPS} == "yes"
408 .if ${DEBUG_DIRDEPS:@x@${DEP_RELDIR:M$x}${${DEP_RELDIR}.${DEP_MACHINE}:L:M$x}@} != ""
409 _debug_reldir = 1
410 .else
411 _debug_reldir = 0
412 .endif
413 .if ${DEBUG_DIRDEPS:@x@${DEP_RELDIR:M$x}${${DEP_RELDIR}.depend:L:M$x}@} != ""
414 _debug_search = 1
415 .else
416 _debug_search = 0
417 .endif
418
419 # the rest is done repeatedly for every Makefile.depend we read.
420 # if we are anything but the original dir we care only about the
421 # machine type we were included for..
422
423 .if ${DEP_RELDIR} == "."
424 _this_dir := ${SRCTOP}
425 .else
426 _this_dir := ${SRCTOP}/${DEP_RELDIR}
427 .endif
428
429 # on rare occasions, there can be a need for extra help
430 _dep_hack := ${_this_dir}/${.MAKE.DEPENDFILE_PREFIX}.inc
431 .-include "${_dep_hack}"
432
433 .if ${DEP_RELDIR} != ${_DEP_RELDIR} || ${DEP_TARGET_SPEC} != ${TARGET_SPEC}
434 # this should be all
435 _machines := ${DEP_MACHINE}
436 .else
437 # this is the machine list we actually use below
438 _machines := ${_only_machines}
439
440 .if defined(HOSTPROG) || ${DEP_MACHINE} == "host"
441 # we need to build this guy's dependencies for host as well.
442 _machines += host
443 .endif
444
445 _machines := ${_machines:O:u}
446 .endif
447
448 .if ${TARGET_SPEC_VARS:[#]} > 1
449 # we need to tweak _machines
450 _dm := ${DEP_MACHINE}
451 # apply the same filtering that we do when qualifying DIRDEPS.
452 # M_dep_qual_fixes expects .${MACHINE}* so add (and remove) '.'
453 _machines := ${_machines:@DEP_MACHINE@${DEP_TARGET_SPEC}@:S,^,.,:${M_dep_qual_fixes:ts:}:O:u:S,^.,,}
454 DEP_MACHINE := ${_dm}
455 .endif
456
457 # reset each time through
458 _build_dirs =
459
460 .if ${DEP_RELDIR} == ${_DEP_RELDIR}
461 # pickup other machines for this dir if necessary
462 .if ${BUILD_AT_LEVEL0:Uyes} == "no"
463 _build_dirs += ${_machines:@m@${_CURDIR}.$m@}
464 .else
465 _build_dirs += ${_machines:N${DEP_TARGET_SPEC}:@m@${_CURDIR}.$m@}
466 .if ${DEP_TARGET_SPEC} == ${TARGET_SPEC}
467 # pickup local dependencies now
468 .-include <.depend>
469 .endif
470 .endif
471 .endif
472
473 .if ${_debug_reldir}
474 .info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: DIRDEPS='${DIRDEPS}'
475 .info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: _machines='${_machines}' 
476 .endif
477
478 .if !empty(DIRDEPS)
479 # these we reset each time through as they can depend on DEP_MACHINE
480 DEP_DIRDEPS_FILTER = \
481         ${DIRDEPS_FILTER.${DEP_TARGET_SPEC}:U} \
482         ${DIRDEPS_FILTER.${DEP_MACHINE}:U} \
483         ${DIRDEPS_FILTER:U} 
484 .if empty(DEP_DIRDEPS_FILTER)
485 # something harmless
486 DEP_DIRDEPS_FILTER = U
487 .endif
488
489 # this is what we start with
490 __depdirs := ${DIRDEPS:${NSkipDir}:${DEP_DIRDEPS_FILTER:ts:}:C,//+,/,g:O:u:@d@${SRCTOP}/$d@}
491
492 # some entries may be qualified with .<machine> 
493 # the :M*/*/*.* just tries to limit the dirs we check to likely ones.
494 # the ${d:E:M*/*} ensures we don't consider junos/usr.sbin/mgd
495 __qual_depdirs := ${__depdirs:M*/*/*.*:@d@${exists($d):?:${"${d:E:M*/*}":?:${exists(${d:R}):?$d:}}}@}
496 __unqual_depdirs := ${__depdirs:${__qual_depdirs:Uno:${M_ListToSkip}}}
497
498 .if ${DEP_RELDIR} == ${_DEP_RELDIR}
499 # if it was called out - we likely need it.
500 __hostdpadd := ${DPADD:U.:M${HOST_OBJTOP}/*:S,${HOST_OBJTOP}/,,:H:${NSkipDir}:${DIRDEPS_FILTER:ts:}:S,$,.host,:N.*:@d@${SRCTOP}/$d@}
501 __qual_depdirs += ${__hostdpadd}
502 .endif
503
504 .if ${_debug_reldir}
505 .info depdirs=${__depdirs}
506 .info qualified=${__qual_depdirs}
507 .info unqualified=${__unqual_depdirs}
508 .endif
509
510 # _build_dirs is what we will feed to _DIRDEP_USE
511 _build_dirs += \
512         ${__qual_depdirs:M*.host:${NSkipHostDir}:N.host} \
513         ${__qual_depdirs:N*.host} \
514         ${_machines:Mhost*:@m@${__unqual_depdirs:@d@$d.$m@}@:${NSkipHostDir}:N.host} \
515         ${_machines:Nhost*:@m@${__unqual_depdirs:@d@$d.$m@}@}
516
517 # qualify everything now
518 _build_dirs := ${_build_dirs:${M_dep_qual_fixes:ts:}:O:u}
519
520 .endif                          # empty DIRDEPS
521
522 # Normally if doing make -V something,
523 # we do not want to waste time chasing DIRDEPS
524 # but if we want to count the number of Makefile.depend* read, we do.
525 .if ${.MAKEFLAGS:M-V${_V_READ_DIRDEPS}} == ""
526 .if !empty(_build_dirs)
527 .if ${BUILD_DIRDEPS_CACHE} == "yes"
528 x!= { echo; echo '\# ${DEP_RELDIR}.${DEP_TARGET_SPEC}'; \
529         echo 'dirdeps: ${_build_dirs:${M_oneperline}}'; echo; } >&3; echo
530 x!= { ${_build_dirs:@x@${target($x):?:echo '$x: _DIRDEP_USE';}@} echo; } >&3; echo
531 .else
532 # this makes it all happen
533 dirdeps: ${_build_dirs}
534 .endif
535 ${_build_dirs}: _DIRDEP_USE
536
537 .if ${_debug_reldir}
538 .info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: needs: ${_build_dirs}
539 .endif
540
541 # this builds the dependency graph
542 .for m in ${_machines}
543 # it would be nice to do :N${.TARGET}
544 .if !empty(__qual_depdirs)
545 .for q in ${__qual_depdirs:${M_dep_qual_fixes:ts:}:E:O:u:N$m}
546 .if ${_debug_reldir} || ${DEBUG_DIRDEPS:@x@${${DEP_RELDIR}.$m:L:M$x}${${DEP_RELDIR}.$q:L:M$x}@} != ""
547 .info ${DEP_RELDIR}.$m: graph: ${_build_dirs:M*.$q}
548 .endif
549 .if ${BUILD_DIRDEPS_CACHE} == "yes"
550 x!= { echo; echo '${_this_dir}.$m: ${_build_dirs:M*.$q:${M_oneperline}}'; echo; } >&3; echo
551 .else
552 ${_this_dir}.$m: ${_build_dirs:M*.$q}
553 .endif
554 .endfor
555 .endif
556 .if ${_debug_reldir}
557 .info ${DEP_RELDIR}.$m: graph: ${_build_dirs:M*.$m:N${_this_dir}.$m}
558 .endif
559 .if ${BUILD_DIRDEPS_CACHE} == "yes"
560 x!= { echo; echo '${_this_dir}.$m: ${_build_dirs:M*.$m:N${_this_dir}.$m:${M_oneperline}}'; echo; } >&3; echo
561 .else
562 ${_this_dir}.$m: ${_build_dirs:M*.$m:N${_this_dir}.$m}
563 .endif
564 .endfor
565
566 .endif
567
568 # Now find more dependencies - and recurse.
569 .for d in ${_build_dirs}
570 .if ${_DIRDEP_CHECKED:M$d} == ""
571 # once only
572 _DIRDEP_CHECKED += $d
573 .if ${_debug_search}
574 .info checking $d
575 .endif
576 # Note: _build_dirs is fully qualifed so d:R is always the directory
577 .if exists(${d:R})
578 # Warning: there is an assumption here that MACHINE is always 
579 # the first entry in TARGET_SPEC_VARS.
580 # If TARGET_SPEC and MACHINE are insufficient, you have a problem.
581 _m := ${.MAKE.DEPENDFILE_PREFERENCE:T:S;${TARGET_SPEC}$;${d:E};:S;${MACHINE};${d:E:C/,.*//};:@m@${exists(${d:R}/$m):?${d:R}/$m:}@:[1]}
582 .if !empty(_m)
583 # M_dep_qual_fixes isn't geared to Makefile.depend
584 _qm := ${_m:C;(\.depend)$;\1.${d:E};:${M_dep_qual_fixes:ts:}}
585 .if ${_debug_search}
586 .info Looking for ${_qm}
587 .endif
588 # we pass _DEP_TARGET_SPEC to tell the next step what we want
589 _DEP_TARGET_SPEC := ${d:E}
590 # some makefiles may still look at this
591 _DEP_MACHINE := ${d:E:C/,.*//}
592 # set this "just in case" 
593 # we can skip :tA since we computed the path above
594 DEP_RELDIR := ${_m:H:S,${SRCTOP}/,,}
595 # and reset this
596 DIRDEPS =
597 .if ${_debug_reldir} && ${_qm} != ${_m}
598 .info loading ${_m} for ${d:E}
599 .endif
600 .include <${_m}>
601 .endif
602 .endif
603 .endif
604 .endfor
605
606 .endif                          # -V
607 .endif                          # BUILD_DIRDEPS
608
609 .elif ${.MAKE.LEVEL} > 42
610 .error You should have stopped recursing by now.
611 .else
612 # we are building something
613 DEP_RELDIR := ${RELDIR}
614 _DEP_RELDIR := ${RELDIR}
615 # pickup local dependencies
616 .-include <.depend>
617 .endif
618
619 # bootstrapping new dependencies made easy?
620 .if (make(bootstrap) || make(bootstrap-recurse)) && !target(bootstrap)
621
622 .if exists(${.CURDIR}/${.MAKE.DEPENDFILE:T})
623 # stop here
624 ${.TARGETS:Mboot*}:
625 .else
626 # find a Makefile.depend to use as _src
627 _src != cd ${.CURDIR} && for m in ${.MAKE.DEPENDFILE_PREFERENCE:T:S,${MACHINE},*,}; do test -s $$m || continue; echo $$m; break; done; echo
628 .if empty(_src)
629 .error cannot find any of ${.MAKE.DEPENDFILE_PREFERENCE:T}
630 .endif
631
632 _src?= ${.MAKE.DEPENDFILE:T}
633
634 bootstrap-this: .NOTMAIN
635         @echo Bootstrapping ${RELDIR}/${.MAKE.DEPENDFILE:T} from ${_src:T}
636         (cd ${.CURDIR} && sed 's,${_src:E},${MACHINE},g' ${_src} > ${.MAKE.DEPENDFILE:T})
637
638 bootstrap: bootstrap-recurse
639 bootstrap-recurse: bootstrap-this
640
641 _mf := ${.PARSEFILE}
642 bootstrap-recurse:      .NOTMAIN .MAKE
643         @cd ${SRCTOP} && \
644         for d in `cd ${RELDIR} && ${.MAKE} -B -f ${"${.MAKEFLAGS:M-n}":?${_src}:${.MAKE.DEPENDFILE:T}} -V DIRDEPS`; do \
645                 test -d $$d || d=$${d%.*}; \
646                 test -d $$d || continue; \
647                 echo "Checking $$d for bootstrap ..."; \
648                 (cd $$d && ${.MAKE} -f ${_mf} bootstrap-recurse); \
649         done
650
651 .endif
652 .endif