]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/mk/dirdeps.mk
Merge from head
[FreeBSD/FreeBSD.git] / share / mk / dirdeps.mk
1 # $Id: dirdeps.mk,v 1.53 2015/05/24 07:08:24 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  _DIRDEP_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 _DIRDEP_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 #               # Always list MACHINE first, 
90 #               # other variables might be optional.
91 #               TARGET_SPEC_VARS = MACHINE TARGET_OS
92 #               .if ${TARGET_SPEC:Uno:M*,*} != ""
93 #               _tspec := ${TARGET_SPEC:S/,/ /g}
94 #               MACHINE := ${_tspec:[1]}
95 #               TARGET_OS := ${_tspec:[2]}
96 #               # etc.
97 #               # We need to stop that TARGET_SPEC affecting any submakes
98 #               # and deal with MACHINE=${TARGET_SPEC} in the environment.
99 #               TARGET_SPEC =
100 #               # export but do not track
101 #               .export-env TARGET_SPEC 
102 #               .export ${TARGET_SPEC_VARS}
103 #               .for v in ${TARGET_SPEC_VARS:O:u}
104 #               .if empty($v)
105 #               .undef $v
106 #               .endif
107 #               .endfor
108 #               .endif
109 #               # make sure we know what TARGET_SPEC is
110 #               # as we may need it to find Makefile.depend*
111 #               TARGET_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts,}
112 #       
113
114 # touch this at your peril
115 _DIRDEP_USE_LEVEL?= 0
116 .if ${.MAKE.LEVEL} == ${_DIRDEP_USE_LEVEL}
117 # only the first instance is interested in all this
118
119 # First off, we want to know what ${MACHINE} to build for.
120 # This can be complicated if we are using a mixture of ${MACHINE} specific
121 # and non-specific Makefile.depend*
122
123 .if !target(_DIRDEP_USE)
124 # do some setup we only need once
125 _CURDIR ?= ${.CURDIR}
126 _OBJDIR ?= ${.OBJDIR}
127
128 now_utc = ${%s:L:gmtime}
129 .if !defined(start_utc)
130 start_utc := ${now_utc}
131 .endif
132
133 # make sure these are empty to start with
134 _DEP_TARGET_SPEC =
135 _DIRDEP_CHECKED =
136
137 # If TARGET_SPEC_VARS is other than just MACHINE
138 # it should be set by sys.mk or similar by now.
139 # TARGET_SPEC must not contain any '.'s.
140 TARGET_SPEC_VARS ?= MACHINE
141 # this is what we started with
142 TARGET_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts,}
143 # this is what we mostly use below
144 DEP_TARGET_SPEC = ${TARGET_SPEC_VARS:S,^,DEP_,:@v@${$v:U}@:ts,}
145 # make sure we have defaults
146 .for v in ${TARGET_SPEC_VARS}
147 DEP_$v ?= ${$v}
148 .endfor
149
150 .if ${TARGET_SPEC_VARS:[#]} > 1
151 # Ok, this gets more complex (putting it mildly).
152 # In order to stay sane, we need to ensure that all the build_dirs
153 # we compute below are fully qualified wrt DEP_TARGET_SPEC.
154 # The makefiles may only partially specify (eg. MACHINE only),
155 # so we need to construct a set of modifiers to fill in the gaps.
156 # jot 10 should output 1 2 3 .. 10
157 JOT ?= jot
158 _tspec_x := ${${JOT} ${TARGET_SPEC_VARS:[#]}:L:sh}
159 # this handles unqualified entries
160 M_dep_qual_fixes = C;(/[^/.,]+)$$;\1.$${DEP_TARGET_SPEC};
161 # there needs to be at least one item missing for these to make sense
162 .for i in ${_tspec_x:[2..-1]}
163 _tspec_m$i := ${TARGET_SPEC_VARS:[2..$i]:@w@[^,]+@:ts,}
164 _tspec_a$i := ,${TARGET_SPEC_VARS:[$i..-1]:@v@$$$${DEP_$v}@:ts,}
165 M_dep_qual_fixes += C;(\.${_tspec_m$i})$$;\1${_tspec_a$i};
166 .endfor
167 .else
168 # A harmless? default.
169 M_dep_qual_fixes = U
170 .endif
171
172 .if !defined(.MAKE.DEPENDFILE_PREFERENCE)
173 # .MAKE.DEPENDFILE_PREFERENCE makes the logic below neater?
174 # you really want this set by sys.mk or similar
175 .MAKE.DEPENDFILE_PREFERENCE = ${_CURDIR}/${.MAKE.DEPENDFILE:T}
176 .if ${.MAKE.DEPENDFILE:E} == "${TARGET_SPEC}"
177 .if ${TARGET_SPEC} != ${MACHINE}
178 .MAKE.DEPENDFILE_PREFERENCE += ${_CURDIR}/${.MAKE.DEPENDFILE:T:R}.$${MACHINE}
179 .endif
180 .MAKE.DEPENDFILE_PREFERENCE += ${_CURDIR}/${.MAKE.DEPENDFILE:T:R}
181 .endif
182 .endif
183
184 _default_dependfile := ${.MAKE.DEPENDFILE_PREFERENCE:[1]:T}
185 _machine_dependfiles := ${.MAKE.DEPENDFILE_PREFERENCE:T:M*${MACHINE}*}
186
187 # for machine specific dependfiles we require ${MACHINE} to be at the end
188 # also for the sake of sanity we require a common prefix
189 .if !defined(.MAKE.DEPENDFILE_PREFIX)
190 # knowing .MAKE.DEPENDFILE_PREFIX helps
191 .if !empty(_machine_dependfiles)
192 .MAKE.DEPENDFILE_PREFIX := ${_machine_dependfiles:[1]:T:R}
193 .else
194 .MAKE.DEPENDFILE_PREFIX := ${_default_dependfile:T}
195 .endif
196 .endif
197
198
199 # this is how we identify non-machine specific dependfiles
200 N_notmachine := ${.MAKE.DEPENDFILE_PREFERENCE:E:N*${MACHINE}*:${M_ListToSkip}}
201
202 .endif                          # !target(_DIRDEP_USE)
203
204 # if we were included recursively _DEP_TARGET_SPEC should be valid.
205 .if empty(_DEP_TARGET_SPEC)
206 # we may or may not have included a dependfile yet
207 .if defined(.INCLUDEDFROMFILE)
208 _last_dependfile := ${.INCLUDEDFROMFILE:M${.MAKE.DEPENDFILE_PREFIX}*}
209 .else
210 _last_dependfile := ${.MAKE.MAKEFILES:M*/${.MAKE.DEPENDFILE_PREFIX}*:[-1]}
211 .endif
212 .if ${_debug_reldir:U0}
213 .info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: _last_dependfile='${_last_dependfile}'
214 .endif
215
216 .if empty(_last_dependfile) || ${_last_dependfile:E:${N_notmachine}} == ""
217 # this is all we have to work with
218 DEP_MACHINE = ${TARGET_MACHINE:U${MACHINE}}
219 _DEP_TARGET_SPEC := ${DEP_TARGET_SPEC}
220 .else
221 _DEP_TARGET_SPEC = ${_last_dependfile:${M_dep_qual_fixes:ts:}:E}
222 .endif
223 .if !empty(_last_dependfile)
224 # record that we've read dependfile for this
225 _DIRDEP_CHECKED += ${_CURDIR}.${TARGET_SPEC}
226 .endif
227 .endif
228
229 # by now _DEP_TARGET_SPEC should be set, parse it.
230 .if ${TARGET_SPEC_VARS:[#]} > 1
231 # we need to parse DEP_MACHINE may or may not contain more info
232 _tspec := ${_DEP_TARGET_SPEC:S/,/ /g}
233 .for i in ${_tspec_x}
234 DEP_${TARGET_SPEC_VARS:[$i]} := ${_tspec:[$i]}
235 .endfor
236 .for v in ${TARGET_SPEC_VARS:O:u}
237 .if empty(DEP_$v)
238 .undef DEP_$v
239 .endif
240 .endfor
241 .else
242 DEP_MACHINE := ${_DEP_TARGET_SPEC}
243 .endif
244
245 # pickup customizations
246 # as below you can use !target(_DIRDEP_USE) to protect things
247 # which should only be done once.
248 .-include "local.dirdeps.mk"
249
250 # the first time we are included the _DIRDEP_USE target will not be defined
251 # we can use this as a clue to do initialization and other one time things.
252 .if !target(_DIRDEP_USE)
253 # make sure this target exists
254 dirdeps: beforedirdeps .WAIT
255 beforedirdeps:
256
257 # We normally expect to be included by Makefile.depend.*
258 # which sets the DEP_* macros below.
259 DEP_RELDIR ?= ${RELDIR}
260
261 # this can cause lots of output!
262 # set to a set of glob expressions that might match RELDIR
263 DEBUG_DIRDEPS ?= no
264
265 # remember the initial value of DEP_RELDIR - we test for it below.
266 _DEP_RELDIR := ${DEP_RELDIR}
267
268 # things we skip for host tools
269 SKIP_HOSTDIR ?=
270
271 NSkipHostDir = ${SKIP_HOSTDIR:N*.host*:S,$,.host*,:N.host*:S,^,${SRCTOP}/,:${M_ListToSkip}}
272
273 # things we always skip
274 # SKIP_DIRDEPS allows for adding entries on command line.
275 SKIP_DIR += .host *.WAIT ${SKIP_DIRDEPS}
276 SKIP_DIR.host += ${SKIP_HOSTDIR}
277
278 DEP_SKIP_DIR = ${SKIP_DIR} \
279         ${SKIP_DIR.${DEP_TARGET_SPEC}:U} \
280         ${SKIP_DIR.${DEP_MACHINE}:U} \
281         ${SKIP_DIRDEPS.${DEP_MACHINE}:U}
282
283 NSkipDir = ${DEP_SKIP_DIR:${M_ListToSkip}}
284
285 .if defined(NO_DIRDEPS) || defined(NODIRDEPS) || defined(WITHOUT_DIRDEPS)
286 # confine ourselves to the original dir
287 DIRDEPS_FILTER += M${_DEP_RELDIR}*
288 .endif
289
290 # this is what we run below
291 DIRDEP_MAKE?= ${.MAKE}
292
293 # we suppress SUBDIR when visiting the leaves
294 # we assume sys.mk will set MACHINE_ARCH
295 # you can add extras to DIRDEP_USE_ENV
296 # if there is no makefile in the target directory, we skip it.
297 _DIRDEP_USE:    .USE .MAKE
298         @for m in ${.MAKE.MAKEFILE_PREFERENCE}; do \
299                 test -s ${.TARGET:R}/$$m || continue; \
300                 echo "${TRACER}Checking ${.TARGET:R} for ${.TARGET:E} ..."; \
301                 MACHINE_ARCH= NO_SUBDIR=1 ${DIRDEP_USE_ENV} \
302                 TARGET_SPEC=${.TARGET:E} \
303                 MACHINE=${.TARGET:E} \
304                 ${DIRDEP_MAKE} -C ${.TARGET:R} || exit 1; \
305                 break; \
306         done
307
308 .ifdef ALL_MACHINES
309 # this is how you limit it to only the machines we have been built for
310 # previously.
311 .if empty(ONLY_MACHINE_LIST)
312 .if !empty(ALL_MACHINE_LIST)
313 # ALL_MACHINE_LIST is the list of all legal machines - ignore anything else
314 _machine_list != cd ${_CURDIR} && 'ls' -1 ${ALL_MACHINE_LIST:O:u:@m@${.MAKE.DEPENDFILE:T:R}.$m@} 2> /dev/null; echo
315 .else
316 _machine_list != 'ls' -1 ${_CURDIR}/${.MAKE.DEPENDFILE_PREFIX}.* 2> /dev/null; echo
317 .endif
318 _only_machines := ${_machine_list:${NIgnoreFiles:UN*.bak}:E:O:u}
319 .else
320 _only_machines := ${ONLY_MACHINE_LIST}
321 .endif
322
323 .if empty(_only_machines)
324 # we must be boot-strapping
325 _only_machines := ${TARGET_MACHINE:U${ALL_MACHINE_LIST:U${DEP_MACHINE}}}
326 .endif
327
328 .else                           # ! ALL_MACHINES
329 # if ONLY_MACHINE_LIST is set, we are limited to that
330 # if TARGET_MACHINE is set - it is really the same as ONLY_MACHINE_LIST
331 # otherwise DEP_MACHINE is it - so DEP_MACHINE will match.
332 _only_machines := ${ONLY_MACHINE_LIST:U${TARGET_MACHINE:U${DEP_MACHINE}}:M${DEP_MACHINE}}
333 .endif
334
335 .if !empty(NOT_MACHINE_LIST)
336 _only_machines := ${_only_machines:${NOT_MACHINE_LIST:${M_ListToSkip}}}
337 .endif
338
339 # make sure we have a starting place?
340 DIRDEPS ?= ${RELDIR}
341 .endif                          # target 
342
343 # if repeatedly building the same target, 
344 # we can avoid the overhead of re-computing the tree dependencies.
345 MK_DIRDEPS_CACHE ?= no
346 BUILD_DIRDEPS_CACHE ?= no
347 BUILD_DIRDEPS ?= yes
348
349 .if !defined(NO_DIRDEPS)
350 .if ${MK_DIRDEPS_CACHE} == "yes"
351 # this is where we will cache all our work
352 DIRDEPS_CACHE?= ${_OBJDIR}/dirdeps.cache${.TARGETS:Nall:O:u:ts-:S,/,_,g:S,^,.,:N.}
353
354 # just ensure this exists
355 build-dirdeps:
356
357 M_oneperline = @x@\\${.newline} $$x@
358
359 .if ${BUILD_DIRDEPS_CACHE} == "no" 
360 .if !target(dirdeps-cached)
361 # we do this via sub-make
362 BUILD_DIRDEPS = no
363
364 dirdeps: dirdeps-cached
365 dirdeps-cached: ${DIRDEPS_CACHE} .MAKE
366         @echo "${TRACER}Using ${DIRDEPS_CACHE}"
367         @MAKELEVEL=${.MAKE.LEVEL} ${.MAKE} -C ${_CURDIR} -f ${DIRDEPS_CACHE} \
368                 dirdeps MK_DIRDEPS_CACHE=no BUILD_DIRDEPS=no
369
370 # these should generally do
371 BUILD_DIRDEPS_MAKEFILE ?= ${MAKEFILE}
372 BUILD_DIRDEPS_TARGETS ?= ${.TARGETS}
373
374 # we need the .meta file to ensure we update if 
375 # any of the Makefile.depend* changed.
376 # We do not want to compare the command line though.
377 ${DIRDEPS_CACHE}:       .META .NOMETA_CMP
378         +@{ echo '# Autogenerated - do NOT edit!'; echo; \
379         echo 'BUILD_DIRDEPS=no'; echo; \
380         echo '.include <dirdeps.mk>'; \
381         } > ${.TARGET}.new
382         +@MAKELEVEL=${.MAKE.LEVEL} DIRDEPS_CACHE=${DIRDEPS_CACHE} \
383         DIRDEPS="${DIRDEPS}" \
384         MAKEFLAGS= ${.MAKE} -C ${_CURDIR} -f ${BUILD_DIRDEPS_MAKEFILE} \
385         ${BUILD_DIRDEPS_TARGETS} BUILD_DIRDEPS_CACHE=yes \
386         .MAKE.DEPENDFILE=.none \
387         3>&1 1>&2 | sed 's,${SRCTOP},$${SRCTOP},g' >> ${.TARGET}.new && \
388         mv ${.TARGET}.new ${.TARGET}
389
390 .endif
391 .elif !target(_count_dirdeps)
392 # we want to capture the dirdeps count in the cache
393 .END: _count_dirdeps
394 _count_dirdeps: .NOMETA
395         @echo '.info $${.newline}$${TRACER}Makefiles read: total=${.MAKE.MAKEFILES:[#]} depend=${.MAKE.MAKEFILES:M*depend*:[#]} dirdeps=${.ALLTARGETS:M${SRCTOP}*:O:u:[#]}' >&3
396
397 .endif
398 .elif !make(dirdeps) && !target(_count_dirdeps)
399 beforedirdeps: _count_dirdeps
400 _count_dirdeps: .NOMETA
401         @echo "${TRACER}Makefiles read: total=${.MAKE.MAKEFILES:[#]} depend=${.MAKE.MAKEFILES:M*depend*:[#]} dirdeps=${.ALLTARGETS:M${SRCTOP}*:O:u:[#]} seconds=`expr ${now_utc} - ${start_utc}`"
402
403 .endif
404 .endif
405
406 .if ${BUILD_DIRDEPS} == "yes"
407 .if ${DEBUG_DIRDEPS:@x@${DEP_RELDIR:M$x}${${DEP_RELDIR}.${DEP_MACHINE}:L:M$x}@} != ""
408 _debug_reldir = 1
409 .else
410 _debug_reldir = 0
411 .endif
412 .if ${DEBUG_DIRDEPS:@x@${DEP_RELDIR:M$x}${${DEP_RELDIR}.depend:L:M$x}@} != ""
413 _debug_search = 1
414 .else
415 _debug_search = 0
416 .endif
417
418 # the rest is done repeatedly for every Makefile.depend we read.
419 # if we are anything but the original dir we care only about the
420 # machine type we were included for..
421
422 .if ${DEP_RELDIR} == "."
423 _this_dir := ${SRCTOP}
424 .else
425 _this_dir := ${SRCTOP}/${DEP_RELDIR}
426 .endif
427
428 # on rare occasions, there can be a need for extra help
429 _dep_hack := ${_this_dir}/${.MAKE.DEPENDFILE_PREFIX}.inc
430 .-include "${_dep_hack}"
431
432 .if ${DEP_RELDIR} != ${_DEP_RELDIR} || ${DEP_TARGET_SPEC} != ${TARGET_SPEC}
433 # this should be all
434 _machines := ${DEP_MACHINE}
435 .else
436 # this is the machine list we actually use below
437 _machines := ${_only_machines}
438
439 .if defined(HOSTPROG) || ${DEP_MACHINE} == "host"
440 # we need to build this guy's dependencies for host as well.
441 _machines += host
442 .endif
443
444 _machines := ${_machines:O:u}
445 .endif
446
447 .if ${TARGET_SPEC_VARS:[#]} > 1
448 # we need to tweak _machines
449 _dm := ${DEP_MACHINE}
450 # apply the same filtering that we do when qualifying DIRDEPS.
451 # M_dep_qual_fixes expects .${MACHINE}* so add (and remove) '.'
452 _machines := ${_machines:@DEP_MACHINE@${DEP_TARGET_SPEC}@:S,^,.,:${M_dep_qual_fixes:ts:}:O:u:S,^.,,}
453 DEP_MACHINE := ${_dm}
454 .endif
455
456 # reset each time through
457 _build_dirs =
458
459 .if ${DEP_RELDIR} == ${_DEP_RELDIR}
460 # pickup other machines for this dir if necessary
461 .if ${BUILD_AT_LEVEL0:Uyes} == "no"
462 _build_dirs += ${_machines:@m@${_CURDIR}.$m@}
463 .else
464 _build_dirs += ${_machines:N${DEP_TARGET_SPEC}:@m@${_CURDIR}.$m@}
465 .if ${DEP_TARGET_SPEC} == ${TARGET_SPEC}
466 # pickup local dependencies now
467 .-include <.depend>
468 .endif
469 .endif
470 .endif
471
472 .if ${_debug_reldir}
473 .info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: DIRDEPS='${DIRDEPS}'
474 .info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: _machines='${_machines}' 
475 .endif
476
477 .if !empty(DIRDEPS)
478 # these we reset each time through as they can depend on DEP_MACHINE
479 DEP_DIRDEPS_FILTER = \
480         ${DIRDEPS_FILTER.${DEP_TARGET_SPEC}:U} \
481         ${DIRDEPS_FILTER.${DEP_MACHINE}:U} \
482         ${DIRDEPS_FILTER:U} 
483 .if empty(DEP_DIRDEPS_FILTER)
484 # something harmless
485 DEP_DIRDEPS_FILTER = U
486 .endif
487
488 # this is what we start with
489 __depdirs := ${DIRDEPS:${NSkipDir}:${DEP_DIRDEPS_FILTER:ts:}:C,//+,/,g:O:u:@d@${SRCTOP}/$d@}
490
491 # some entries may be qualified with .<machine> 
492 # the :M*/*/*.* just tries to limit the dirs we check to likely ones.
493 # the ${d:E:M*/*} ensures we don't consider junos/usr.sbin/mgd
494 __qual_depdirs := ${__depdirs:M*/*/*.*:@d@${exists($d):?:${"${d:E:M*/*}":?:${exists(${d:R}):?$d:}}}@}
495 __unqual_depdirs := ${__depdirs:${__qual_depdirs:Uno:${M_ListToSkip}}}
496
497 .if ${DEP_RELDIR} == ${_DEP_RELDIR}
498 # if it was called out - we likely need it.
499 __hostdpadd := ${DPADD:U.:M${HOST_OBJTOP}/*:S,${HOST_OBJTOP}/,,:H:${NSkipDir}:${DIRDEPS_FILTER:ts:}:S,$,.host,:N.*:@d@${SRCTOP}/$d@}
500 __qual_depdirs += ${__hostdpadd}
501 .endif
502
503 .if ${_debug_reldir}
504 .info depdirs=${__depdirs}
505 .info qualified=${__qual_depdirs}
506 .info unqualified=${__unqual_depdirs}
507 .endif
508
509 # _build_dirs is what we will feed to _DIRDEP_USE
510 _build_dirs += \
511         ${__qual_depdirs:M*.host:${NSkipHostDir}:N.host} \
512         ${__qual_depdirs:N*.host} \
513         ${_machines:Mhost*:@m@${__unqual_depdirs:@d@$d.$m@}@:${NSkipHostDir}:N.host} \
514         ${_machines:Nhost*:@m@${__unqual_depdirs:@d@$d.$m@}@}
515
516 # qualify everything now
517 _build_dirs := ${_build_dirs:${M_dep_qual_fixes:ts:}:O:u}
518
519 .endif                          # empty DIRDEPS
520
521 # Normally if doing make -V something,
522 # we do not want to waste time chasing DIRDEPS
523 # but if we want to count the number of Makefile.depend* read, we do.
524 .if ${.MAKEFLAGS:M-V${_V_READ_DIRDEPS}} == ""
525 .if !empty(_build_dirs)
526 .if ${BUILD_DIRDEPS_CACHE} == "yes"
527 x!= { echo; echo '\# ${DEP_RELDIR}.${DEP_TARGET_SPEC}'; \
528         echo 'dirdeps: ${_build_dirs:${M_oneperline}}'; echo; } >&3; echo
529 x!= { ${_build_dirs:@x@${target($x):?:echo '$x: _DIRDEP_USE';}@} echo; } >&3; echo
530 .else
531 # this makes it all happen
532 dirdeps: ${_build_dirs}
533 .endif
534 ${_build_dirs}: _DIRDEP_USE
535
536 .if ${_debug_reldir}
537 .info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: needs: ${_build_dirs}
538 .endif
539
540 # this builds the dependency graph
541 .for m in ${_machines}
542 # it would be nice to do :N${.TARGET}
543 .if !empty(__qual_depdirs)
544 .for q in ${__qual_depdirs:${M_dep_qual_fixes:ts:}:E:O:u:N$m}
545 .if ${_debug_reldir} || ${DEBUG_DIRDEPS:@x@${${DEP_RELDIR}.$m:L:M$x}${${DEP_RELDIR}.$q:L:M$x}@} != ""
546 .info ${DEP_RELDIR}.$m: graph: ${_build_dirs:M*.$q}
547 .endif
548 .if ${BUILD_DIRDEPS_CACHE} == "yes"
549 x!= { echo; echo '${_this_dir}.$m: ${_build_dirs:M*.$q:${M_oneperline}}'; echo; } >&3; echo
550 .else
551 ${_this_dir}.$m: ${_build_dirs:M*.$q}
552 .endif
553 .endfor
554 .endif
555 .if ${_debug_reldir}
556 .info ${DEP_RELDIR}.$m: graph: ${_build_dirs:M*.$m:N${_this_dir}.$m}
557 .endif
558 .if ${BUILD_DIRDEPS_CACHE} == "yes"
559 x!= { echo; echo '${_this_dir}.$m: ${_build_dirs:M*.$m:N${_this_dir}.$m:${M_oneperline}}'; echo; } >&3; echo
560 .else
561 ${_this_dir}.$m: ${_build_dirs:M*.$m:N${_this_dir}.$m}
562 .endif
563 .endfor
564
565 .endif
566
567 # Now find more dependencies - and recurse.
568 .for d in ${_build_dirs}
569 .if ${_DIRDEP_CHECKED:M$d} == ""
570 # once only
571 _DIRDEP_CHECKED += $d
572 .if ${_debug_search}
573 .info checking $d
574 .endif
575 # Note: _build_dirs is fully qualifed so d:R is always the directory
576 .if exists(${d:R})
577 # Warning: there is an assumption here that MACHINE is always 
578 # the first entry in TARGET_SPEC_VARS.
579 # If TARGET_SPEC and MACHINE are insufficient, you have a problem.
580 _m := ${.MAKE.DEPENDFILE_PREFERENCE:T:S;${TARGET_SPEC}$;${d:E};:S;${MACHINE};${d:E:C/,.*//};:@m@${exists(${d:R}/$m):?${d:R}/$m:}@:[1]}
581 .if !empty(_m)
582 # M_dep_qual_fixes isn't geared to Makefile.depend
583 _qm := ${_m:C;(\.depend)$;\1.${d:E};:${M_dep_qual_fixes:ts:}}
584 .if ${_debug_search}
585 .info Looking for ${_qm}
586 .endif
587 # we pass _DEP_TARGET_SPEC to tell the next step what we want
588 _DEP_TARGET_SPEC := ${d:E}
589 # some makefiles may still look at this
590 _DEP_MACHINE := ${d:E:C/,.*//}
591 .if ${_debug_reldir} && ${_qm} != ${_m}
592 .info loading ${_m} for ${d:E}
593 .endif
594 .include <${_m}>
595 .endif
596 .endif
597 .endif
598 .endfor
599
600 .endif                          # -V
601 .endif                          # BUILD_DIRDEPS
602
603 .elif ${.MAKE.LEVEL} > 42
604 .error You should have stopped recursing by now.
605 .else
606 _DEP_RELDIR := ${DEP_RELDIR}
607 # pickup local dependencies
608 .-include <.depend>
609 .endif
610
611 # bootstrapping new dependencies made easy?
612 .if (make(bootstrap) || make(bootstrap-recurse)) && !target(bootstrap)
613
614 .if exists(${.CURDIR}/${.MAKE.DEPENDFILE:T})
615 # stop here
616 ${.TARGETS:Mboot*}:
617 .else
618 # find a Makefile.depend to use as _src
619 _src != cd ${.CURDIR} && for m in ${.MAKE.DEPENDFILE_PREFERENCE:T:S,${MACHINE},*,}; do test -s $$m || continue; echo $$m; break; done; echo
620 .if empty(_src)
621 .error cannot find any of ${.MAKE.DEPENDFILE_PREFERENCE:T}
622 .endif
623
624 _src?= ${.MAKE.DEPENDFILE:T}
625
626 bootstrap-this: .NOTMAIN
627         @echo Bootstrapping ${RELDIR}/${.MAKE.DEPENDFILE:T} from ${_src:T}
628         (cd ${.CURDIR} && sed 's,${_src:E},${MACHINE},g' ${_src} > ${.MAKE.DEPENDFILE:T})
629
630 bootstrap: bootstrap-recurse
631 bootstrap-recurse: bootstrap-this
632
633 _mf := ${.PARSEFILE}
634 bootstrap-recurse:      .NOTMAIN .MAKE
635         @cd ${SRCTOP} && \
636         for d in `cd ${RELDIR} && ${.MAKE} -B -f ${"${.MAKEFLAGS:M-n}":?${_src}:${.MAKE.DEPENDFILE:T}} -V DIRDEPS`; do \
637                 test -d $$d || d=$${d%.*}; \
638                 test -d $$d || continue; \
639                 echo "Checking $$d for bootstrap ..."; \
640                 (cd $$d && ${.MAKE} -f ${_mf} bootstrap-recurse); \
641         done
642
643 .endif
644 .endif