]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/mk/dirdeps.mk
Import mandoc cvs snapshot 20170121 (pre 1.14)
[FreeBSD/FreeBSD.git] / share / mk / dirdeps.mk
1 # $FreeBSD$
2 # $Id: dirdeps.mk,v 1.84 2016/11/27 02:44:34 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 #       Before each Makefile.depend file is read, we set 
61 #       DEP_RELDIR to be the the RELDIR (path relative to SRCTOP) for
62 #       its directory, and DEP_MACHINE etc according to the .<target_spec>
63 #       represented by the suffix of the corresponding target.
64 #       
65 #       Since each Makefile.depend file includes dirdeps.mk, this
66 #       processing is recursive and results in .MAKE.LEVEL 0 learning the
67 #       dependencies of the tree wrt the initial directory (_DEP_RELDIR).
68 #
69 # BUILD_AT_LEVEL0
70 #       Indicates whether .MAKE.LEVEL 0 builds anything:
71 #       if "no" sub-makes are used to build everything,
72 #       if "yes" sub-makes are only used to build for other machines.
73 #       It is best to use "no", but this can require fixing some
74 #       makefiles to not do anything at .MAKE.LEVEL 0.
75 #
76 # TARGET_SPEC_VARS
77 #       The default value is just MACHINE, and for most environments
78 #       this is sufficient.  The _DIRDEP_USE target actually sets
79 #       both MACHINE and TARGET_SPEC to the suffix of the current
80 #       target so that in the general case TARGET_SPEC can be ignored.
81 #
82 #       If more than MACHINE is needed then sys.mk needs to decompose
83 #       TARGET_SPEC and set the relevant variables accordingly.
84 #       It is important that MACHINE be included in and actually be
85 #       the first member of TARGET_SPEC_VARS.  This allows other
86 #       variables to be considered optional, and some of the treatment
87 #       below relies on MACHINE being the first entry.
88 #       Note: TARGET_SPEC cannot contain any '.'s so the target
89 #       triple used by compiler folk won't work (directly anyway).
90 #
91 #       For example:
92 #
93 #               # Always list MACHINE first, 
94 #               # other variables might be optional.
95 #               TARGET_SPEC_VARS = MACHINE TARGET_OS
96 #               .if ${TARGET_SPEC:Uno:M*,*} != ""
97 #               _tspec := ${TARGET_SPEC:S/,/ /g}
98 #               MACHINE := ${_tspec:[1]}
99 #               TARGET_OS := ${_tspec:[2]}
100 #               # etc.
101 #               # We need to stop that TARGET_SPEC affecting any submakes
102 #               # and deal with MACHINE=${TARGET_SPEC} in the environment.
103 #               TARGET_SPEC =
104 #               # export but do not track
105 #               .export-env TARGET_SPEC 
106 #               .export ${TARGET_SPEC_VARS}
107 #               .for v in ${TARGET_SPEC_VARS:O:u}
108 #               .if empty($v)
109 #               .undef $v
110 #               .endif
111 #               .endfor
112 #               .endif
113 #               # make sure we know what TARGET_SPEC is
114 #               # as we may need it to find Makefile.depend*
115 #               TARGET_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts,}
116 #       
117 #       The following variables can influence the initial DIRDEPS
118 #       computation with regard to the TARGET_SPECs that will be
119 #       built.
120 #       Most should also be considered by init.mk
121 #       
122 #       ONLY_TARGET_SPEC_LIST
123 #               Defines a list of TARGET_SPECs for which the current
124 #               directory can be built.
125 #               If ALL_MACHINES is defined, we build for all the
126 #               TARGET_SPECs listed.
127 #
128 #       ONLY_MACHINE_LIST
129 #               As for ONLY_TARGET_SPEC_LIST but only specifies
130 #               MACHINEs.
131 #
132 #       NOT_TARGET_SPEC_LIST
133 #               A list of TARGET_SPECs for which the current
134 #               directory should not be built.
135 #
136 #       NOT_MACHINE_LIST
137 #               A list of MACHINEs the current directory should not be
138 #               built for.
139 #
140
141 # touch this at your peril
142 _DIRDEP_USE_LEVEL?= 0
143 .if ${.MAKE.LEVEL} == ${_DIRDEP_USE_LEVEL}
144 # only the first instance is interested in all this
145
146 .if !target(_DIRDEP_USE)
147
148 # do some setup we only need once
149 _CURDIR ?= ${.CURDIR}
150 _OBJDIR ?= ${.OBJDIR}
151
152 now_utc = ${%s:L:gmtime}
153 .if !defined(start_utc)
154 start_utc := ${now_utc}
155 .endif
156
157 .if ${MAKEFILE:T} == ${.PARSEFILE} && empty(DIRDEPS) && ${.TARGETS:Uall:M*/*} != ""
158 # This little trick let's us do
159 #
160 # mk -f dirdeps.mk some/dir.${TARGET_SPEC}
161 #
162 all:
163 ${.TARGETS:Nall}: all
164 DIRDEPS := ${.TARGETS:M*[/.]*}
165 # so that -DNO_DIRDEPS works
166 DEP_RELDIR := ${DIRDEPS:[1]:R}
167 # this will become DEP_MACHINE below
168 TARGET_MACHINE := ${DIRDEPS:[1]:E:C/,.*//}
169 .if ${TARGET_MACHINE:N*/*} == ""
170 TARGET_MACHINE := ${MACHINE}
171 .endif
172 # disable DIRDEPS_CACHE as it does not like this trick
173 MK_DIRDEPS_CACHE = no
174 .endif
175
176 # make sure we get the behavior we expect
177 .MAKE.SAVE_DOLLARS = no
178
179 # make sure these are empty to start with
180 _DEP_TARGET_SPEC =
181
182 # If TARGET_SPEC_VARS is other than just MACHINE
183 # it should be set by sys.mk or similar by now.
184 # TARGET_SPEC must not contain any '.'s.
185 TARGET_SPEC_VARS ?= MACHINE
186 # this is what we started with
187 TARGET_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts,}
188 # this is what we mostly use below
189 DEP_TARGET_SPEC = ${TARGET_SPEC_VARS:S,^,DEP_,:@v@${$v:U}@:ts,}
190 # make sure we have defaults
191 .for v in ${TARGET_SPEC_VARS}
192 DEP_$v ?= ${$v}
193 .endfor
194
195 .if ${TARGET_SPEC_VARS:[#]} > 1
196 # Ok, this gets more complex (putting it mildly).
197 # In order to stay sane, we need to ensure that all the build_dirs
198 # we compute below are fully qualified wrt DEP_TARGET_SPEC.
199 # The makefiles may only partially specify (eg. MACHINE only),
200 # so we need to construct a set of modifiers to fill in the gaps.
201 .if ${TARGET_SPEC_VARS:[#]} > 10
202 # seriously? better have jot(1) or equivalent to produce suitable sequence
203 _tspec_x := ${${JOT:Ujot} ${TARGET_SPEC_VARS:[#]}:L:sh}
204 .else
205 # we can provide the sequence ourselves
206 _tspec_x := ${1 2 3 4 5 6 7 8 9 10:L:[1..${TARGET_SPEC_VARS:[#]}]}
207 .endif
208 # this handles unqualified entries
209 M_dep_qual_fixes = C;(/[^/.,]+)$$;\1.$${DEP_TARGET_SPEC};
210 # there needs to be at least one item missing for these to make sense
211 .for i in ${_tspec_x:[2..-1]}
212 _tspec_m$i := ${TARGET_SPEC_VARS:[2..$i]:@w@[^,]+@:ts,}
213 _tspec_a$i := ,${TARGET_SPEC_VARS:[$i..-1]:@v@$$$${DEP_$v}@:ts,}
214 M_dep_qual_fixes += C;(\.${_tspec_m$i})$$;\1${_tspec_a$i};
215 .endfor
216 .else
217 # A harmless? default.
218 M_dep_qual_fixes = U
219 .endif
220
221 .if !defined(.MAKE.DEPENDFILE_PREFERENCE)
222 # .MAKE.DEPENDFILE_PREFERENCE makes the logic below neater?
223 # you really want this set by sys.mk or similar
224 .MAKE.DEPENDFILE_PREFERENCE = ${_CURDIR}/${.MAKE.DEPENDFILE:T}
225 .if ${.MAKE.DEPENDFILE:E} == "${TARGET_SPEC}"
226 .if ${TARGET_SPEC} != ${MACHINE}
227 .MAKE.DEPENDFILE_PREFERENCE += ${_CURDIR}/${.MAKE.DEPENDFILE:T:R}.$${MACHINE}
228 .endif
229 .MAKE.DEPENDFILE_PREFERENCE += ${_CURDIR}/${.MAKE.DEPENDFILE:T:R}
230 .endif
231 .endif
232
233 _default_dependfile := ${.MAKE.DEPENDFILE_PREFERENCE:[1]:T}
234 _machine_dependfiles := ${.MAKE.DEPENDFILE_PREFERENCE:T:M*${MACHINE}*}
235
236 # for machine specific dependfiles we require ${MACHINE} to be at the end
237 # also for the sake of sanity we require a common prefix
238 .if !defined(.MAKE.DEPENDFILE_PREFIX)
239 # knowing .MAKE.DEPENDFILE_PREFIX helps
240 .if !empty(_machine_dependfiles)
241 .MAKE.DEPENDFILE_PREFIX := ${_machine_dependfiles:[1]:T:R}
242 .else
243 .MAKE.DEPENDFILE_PREFIX := ${_default_dependfile:T}
244 .endif
245 .endif
246
247
248 # this is how we identify non-machine specific dependfiles
249 N_notmachine := ${.MAKE.DEPENDFILE_PREFERENCE:E:N*${MACHINE}*:${M_ListToSkip}}
250
251 .endif                          # !target(_DIRDEP_USE)
252
253 # First off, we want to know what ${MACHINE} to build for.
254 # This can be complicated if we are using a mixture of ${MACHINE} specific
255 # and non-specific Makefile.depend*
256
257 # if we were included recursively _DEP_TARGET_SPEC should be valid.
258 .if empty(_DEP_TARGET_SPEC)
259 # we may or may not have included a dependfile yet
260 .if defined(.INCLUDEDFROMFILE)
261 _last_dependfile := ${.INCLUDEDFROMFILE:M${.MAKE.DEPENDFILE_PREFIX}*}
262 .else
263 _last_dependfile := ${.MAKE.MAKEFILES:M*/${.MAKE.DEPENDFILE_PREFIX}*:[-1]}
264 .endif
265 .if ${_debug_reldir:U0}
266 .info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: _last_dependfile='${_last_dependfile}'
267 .endif
268
269 .if empty(_last_dependfile) || ${_last_dependfile:E:${N_notmachine}} == ""
270 # this is all we have to work with
271 DEP_MACHINE = ${TARGET_MACHINE:U${MACHINE}}
272 _DEP_TARGET_SPEC := ${DEP_TARGET_SPEC}
273 .else
274 _DEP_TARGET_SPEC = ${_last_dependfile:${M_dep_qual_fixes:ts:}:E}
275 .endif
276 .if !empty(_last_dependfile)
277 # record that we've read dependfile for this
278 _dirdeps_checked.${_CURDIR}.${TARGET_SPEC}:
279 .endif
280 .endif
281
282 # by now _DEP_TARGET_SPEC should be set, parse it.
283 .if ${TARGET_SPEC_VARS:[#]} > 1
284 # we need to parse DEP_MACHINE may or may not contain more info
285 _tspec := ${_DEP_TARGET_SPEC:S/,/ /g}
286 .for i in ${_tspec_x}
287 DEP_${TARGET_SPEC_VARS:[$i]} := ${_tspec:[$i]}
288 .endfor
289 .for v in ${TARGET_SPEC_VARS:O:u}
290 .if empty(DEP_$v)
291 .undef DEP_$v
292 .endif
293 .endfor
294 .else
295 DEP_MACHINE := ${_DEP_TARGET_SPEC}
296 .endif
297
298 # reset each time through
299 _build_all_dirs =
300
301 # the first time we are included the _DIRDEP_USE target will not be defined
302 # we can use this as a clue to do initialization and other one time things.
303 .if !target(_DIRDEP_USE)
304 # make sure this target exists
305 dirdeps: beforedirdeps .WAIT
306 beforedirdeps:
307
308 # We normally expect to be included by Makefile.depend.*
309 # which sets the DEP_* macros below.
310 DEP_RELDIR ?= ${RELDIR}
311
312 # this can cause lots of output!
313 # set to a set of glob expressions that might match RELDIR
314 DEBUG_DIRDEPS ?= no
315
316 # remember the initial value of DEP_RELDIR - we test for it below.
317 _DEP_RELDIR := ${DEP_RELDIR}
318
319 .endif
320
321 # DIRDEPS_CACHE can be very handy for debugging.
322 # Also if repeatedly building the same target, 
323 # we can avoid the overhead of re-computing the tree dependencies.
324 MK_DIRDEPS_CACHE ?= no
325 BUILD_DIRDEPS_CACHE ?= no
326 BUILD_DIRDEPS ?= yes
327
328 .if ${MK_DIRDEPS_CACHE} == "yes"
329 # this is where we will cache all our work
330 DIRDEPS_CACHE ?= ${_OBJDIR:tA}/dirdeps.cache${.TARGETS:Nall:O:u:ts-:S,/,_,g:S,^,.,:N.}
331 .endif
332
333 # pickup customizations
334 # as below you can use !target(_DIRDEP_USE) to protect things
335 # which should only be done once.
336 .-include <local.dirdeps.mk>
337
338 .if !target(_DIRDEP_USE)
339 # things we skip for host tools
340 SKIP_HOSTDIR ?=
341
342 NSkipHostDir = ${SKIP_HOSTDIR:N*.host*:S,$,.host*,:N.host*:S,^,${SRCTOP}/,:${M_ListToSkip}}
343
344 # things we always skip
345 # SKIP_DIRDEPS allows for adding entries on command line.
346 SKIP_DIR += .host *.WAIT ${SKIP_DIRDEPS}
347 SKIP_DIR.host += ${SKIP_HOSTDIR}
348
349 DEP_SKIP_DIR = ${SKIP_DIR} \
350         ${SKIP_DIR.${DEP_TARGET_SPEC}:U} \
351         ${TARGET_SPEC_VARS:@v@${SKIP_DIR.${DEP_$v}:U}@} \
352         ${SKIP_DIRDEPS.${DEP_TARGET_SPEC}:U} \
353         ${TARGET_SPEC_VARS:@v@${SKIP_DIRDEPS.${DEP_$v}:U}@}
354
355
356 NSkipDir = ${DEP_SKIP_DIR:${M_ListToSkip}}
357
358 .if defined(NODIRDEPS) || defined(WITHOUT_DIRDEPS)
359 NO_DIRDEPS =
360 .elif defined(WITHOUT_DIRDEPS_BELOW)
361 NO_DIRDEPS_BELOW =
362 .endif
363
364 .if defined(NO_DIRDEPS)
365 # confine ourselves to the original dir and below.
366 DIRDEPS_FILTER += M${_DEP_RELDIR}*
367 .elif defined(NO_DIRDEPS_BELOW)
368 DIRDEPS_FILTER += M${_DEP_RELDIR}
369 .endif
370
371 # this is what we run below
372 DIRDEP_MAKE?= ${.MAKE}
373
374 # we suppress SUBDIR when visiting the leaves
375 # we assume sys.mk will set MACHINE_ARCH
376 # you can add extras to DIRDEP_USE_ENV
377 # if there is no makefile in the target directory, we skip it.
378 _DIRDEP_USE:    .USE .MAKE
379         @for m in ${.MAKE.MAKEFILE_PREFERENCE}; do \
380                 test -s ${.TARGET:R}/$$m || continue; \
381                 echo "${TRACER}Checking ${.TARGET:R} for ${.TARGET:E} ..."; \
382                 MACHINE_ARCH= NO_SUBDIR=1 ${DIRDEP_USE_ENV} \
383                 TARGET_SPEC=${.TARGET:E} \
384                 MACHINE=${.TARGET:E} \
385                 ${DIRDEP_MAKE} -C ${.TARGET:R} || exit 1; \
386                 break; \
387         done
388
389 .ifdef ALL_MACHINES
390 # this is how you limit it to only the machines we have been built for
391 # previously.
392 .if empty(ONLY_TARGET_SPEC_LIST) && empty(ONLY_MACHINE_LIST)
393 .if !empty(ALL_MACHINE_LIST)
394 # ALL_MACHINE_LIST is the list of all legal machines - ignore anything else
395 _machine_list != cd ${_CURDIR} && 'ls' -1 ${ALL_MACHINE_LIST:O:u:@m@${.MAKE.DEPENDFILE:T:R}.$m@} 2> /dev/null; echo
396 .else
397 _machine_list != 'ls' -1 ${_CURDIR}/${.MAKE.DEPENDFILE_PREFIX}.* 2> /dev/null; echo
398 .endif
399 _only_machines := ${_machine_list:${NIgnoreFiles:UN*.bak}:E:O:u}
400 .else
401 _only_machines := ${ONLY_TARGET_SPEC_LIST:U} ${ONLY_MACHINE_LIST:U}
402 .endif
403
404 .if empty(_only_machines)
405 # we must be boot-strapping
406 _only_machines := ${TARGET_MACHINE:U${ALL_MACHINE_LIST:U${DEP_MACHINE}}}
407 .endif
408
409 .else                           # ! ALL_MACHINES
410 # if ONLY_TARGET_SPEC_LIST or ONLY_MACHINE_LIST is set, we are limited to that.
411 # Note that ONLY_TARGET_SPEC_LIST should be fully qualified.
412 # if TARGET_MACHINE is set - it is really the same as ONLY_MACHINE_LIST
413 # otherwise DEP_MACHINE is it - so DEP_MACHINE will match.
414 _only_machines := ${ONLY_TARGET_SPEC_LIST:U:M${DEP_MACHINE},*}
415 .if empty(_only_machines)
416 _only_machines := ${ONLY_MACHINE_LIST:U${TARGET_MACHINE:U${DEP_MACHINE}}:M${DEP_MACHINE}}
417 .endif
418 .endif
419
420 .if !empty(NOT_MACHINE_LIST)
421 _only_machines := ${_only_machines:${NOT_MACHINE_LIST:${M_ListToSkip}}}
422 .endif
423 .if !empty(NOT_TARGET_SPEC_LIST)
424 # we must first qualify
425 _dm := ${DEP_MACHINE}
426 _only_machines := ${_only_machines:M*,*} ${_only_machines:N*,*:@DEP_MACHINE@${DEP_TARGET_SPEC}@:S,^,.,:${M_dep_qual_fixes:ts:}:O:u:S,^.,,}
427 DEP_MACHINE := ${_dm}
428 _only_machines := ${_only_machines:${NOT_TARGET_SPEC_LIST:${M_ListToSkip}}}
429 .endif
430 # clean up
431 _only_machines := ${_only_machines:O:u}
432
433 # make sure we have a starting place?
434 DIRDEPS ?= ${RELDIR}
435 .endif                          # target 
436
437 .if !defined(NO_DIRDEPS) && !defined(NO_DIRDEPS_BELOW)
438 .if ${MK_DIRDEPS_CACHE} == "yes"
439
440 # just ensure this exists
441 build-dirdeps:
442
443 M_oneperline = @x@\\${.newline} $$x@
444
445 .if ${BUILD_DIRDEPS_CACHE} == "no" 
446 .if !target(dirdeps-cached)
447 # we do this via sub-make
448 BUILD_DIRDEPS = no
449
450 # ignore anything but these
451 .MAKE.META.IGNORE_FILTER = M*/${.MAKE.DEPENDFILE_PREFIX}*
452
453 dirdeps: dirdeps-cached
454 dirdeps-cached: ${DIRDEPS_CACHE} .MAKE
455         @echo "${TRACER}Using ${DIRDEPS_CACHE}"
456         @MAKELEVEL=${.MAKE.LEVEL} ${.MAKE} -C ${_CURDIR} -f ${DIRDEPS_CACHE} \
457                 dirdeps MK_DIRDEPS_CACHE=no BUILD_DIRDEPS=no
458
459 # these should generally do
460 BUILD_DIRDEPS_MAKEFILE ?= ${MAKEFILE}
461 BUILD_DIRDEPS_TARGETS ?= ${.TARGETS}
462
463 # we need the .meta file to ensure we update if 
464 # any of the Makefile.depend* changed.
465 # We do not want to compare the command line though.
466 ${DIRDEPS_CACHE}:       .META .NOMETA_CMP
467         +@{ echo '# Autogenerated - do NOT edit!'; echo; \
468         echo 'BUILD_DIRDEPS=no'; echo; \
469         echo '.include <dirdeps.mk>'; \
470         } > ${.TARGET}.new
471         +@MAKELEVEL=${.MAKE.LEVEL} DIRDEPS_CACHE=${DIRDEPS_CACHE} \
472         DIRDEPS="${DIRDEPS}" \
473         TARGET_SPEC=${TARGET_SPEC} \
474         MAKEFLAGS= ${.MAKE} -C ${_CURDIR} -f ${BUILD_DIRDEPS_MAKEFILE} \
475         ${BUILD_DIRDEPS_TARGETS} BUILD_DIRDEPS_CACHE=yes \
476         .MAKE.DEPENDFILE=.none \
477         ${.MAKEFLAGS:tW:S,-D ,-D,g:tw:M*WITH*} \
478         3>&1 1>&2 | sed 's,${SRCTOP},$${SRCTOP},g' >> ${.TARGET}.new && \
479         mv ${.TARGET}.new ${.TARGET}
480
481 .endif
482 .elif !target(_count_dirdeps)
483 # we want to capture the dirdeps count in the cache
484 .END: _count_dirdeps
485 _count_dirdeps: .NOMETA
486         @echo '.info $${.newline}$${TRACER}Makefiles read: total=${.MAKE.MAKEFILES:[#]} depend=${.MAKE.MAKEFILES:M*depend*:[#]} dirdeps=${.ALLTARGETS:M${SRCTOP}*:O:u:[#]}' >&3
487
488 .endif
489 .elif !make(dirdeps) && !target(_count_dirdeps)
490 beforedirdeps: _count_dirdeps
491 _count_dirdeps: .NOMETA
492         @echo "${TRACER}Makefiles read: total=${.MAKE.MAKEFILES:[#]} depend=${.MAKE.MAKEFILES:M*depend*:[#]} dirdeps=${.ALLTARGETS:M${SRCTOP}*:O:u:[#]} seconds=`expr ${now_utc} - ${start_utc}`"
493
494 .endif
495 .endif
496
497 .if ${BUILD_DIRDEPS} == "yes"
498 .if ${DEBUG_DIRDEPS:@x@${DEP_RELDIR:M$x}${${DEP_RELDIR}.${DEP_MACHINE}:L:M$x}@} != ""
499 _debug_reldir = 1
500 .else
501 _debug_reldir = 0
502 .endif
503 .if ${DEBUG_DIRDEPS:@x@${DEP_RELDIR:M$x}${${DEP_RELDIR}.depend:L:M$x}@} != ""
504 _debug_search = 1
505 .else
506 _debug_search = 0
507 .endif
508
509 # the rest is done repeatedly for every Makefile.depend we read.
510 # if we are anything but the original dir we care only about the
511 # machine type we were included for..
512
513 .if ${DEP_RELDIR} == "."
514 _this_dir := ${SRCTOP}
515 .else
516 _this_dir := ${SRCTOP}/${DEP_RELDIR}
517 .endif
518
519 # on rare occasions, there can be a need for extra help
520 _dep_hack := ${_this_dir}/${.MAKE.DEPENDFILE_PREFIX}.inc
521 .-include <${_dep_hack}>
522
523 .if ${DEP_RELDIR} != ${_DEP_RELDIR} || ${DEP_TARGET_SPEC} != ${TARGET_SPEC}
524 # this should be all
525 _machines := ${DEP_MACHINE}
526 .else
527 # this is the machine list we actually use below
528 _machines := ${_only_machines}
529
530 .if defined(HOSTPROG) || ${DEP_MACHINE} == "host"
531 # we need to build this guy's dependencies for host as well.
532 _machines += host
533 .endif
534
535 _machines := ${_machines:O:u}
536 .endif
537
538 .if ${TARGET_SPEC_VARS:[#]} > 1
539 # we need to tweak _machines
540 _dm := ${DEP_MACHINE}
541 # apply the same filtering that we do when qualifying DIRDEPS.
542 # M_dep_qual_fixes expects .${MACHINE}* so add (and remove) '.'
543 # Again we expect that any already qualified machines are fully qualified.
544 _machines := ${_machines:M*,*} ${_machines:N*,*:@DEP_MACHINE@${DEP_TARGET_SPEC}@:S,^,.,:${M_dep_qual_fixes:ts:}:O:u:S,^.,,}
545 DEP_MACHINE := ${_dm}
546 _machines := ${_machines:O:u}
547 .endif
548
549 # reset each time through
550 _build_dirs =
551
552 .if ${DEP_RELDIR} == ${_DEP_RELDIR}
553 # pickup other machines for this dir if necessary
554 .if ${BUILD_AT_LEVEL0:Uyes} == "no"
555 _build_dirs += ${_machines:@m@${_CURDIR}.$m@}
556 .else
557 _build_dirs += ${_machines:N${DEP_TARGET_SPEC}:@m@${_CURDIR}.$m@}
558 .if ${DEP_TARGET_SPEC} == ${TARGET_SPEC}
559 # pickup local dependencies now
560 .if ${MAKE_VERSION} < 20160220
561 .-include <.depend>
562 .else
563 .dinclude <.depend>
564 .endif
565 .endif
566 .endif
567 .endif
568
569 .if ${_debug_reldir}
570 .info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: DIRDEPS='${DIRDEPS}'
571 .info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: _machines='${_machines}' 
572 .endif
573
574 .if !empty(DIRDEPS)
575 # these we reset each time through as they can depend on DEP_MACHINE
576 DEP_DIRDEPS_FILTER = \
577         ${DIRDEPS_FILTER.${DEP_TARGET_SPEC}:U} \
578         ${TARGET_SPEC_VARS:@v@${DIRDEPS_FILTER.${DEP_$v}:U}@} \
579         ${DIRDEPS_FILTER:U} 
580 .if empty(DEP_DIRDEPS_FILTER)
581 # something harmless
582 DEP_DIRDEPS_FILTER = U
583 .endif
584
585 # this is what we start with
586 __depdirs := ${DIRDEPS:${NSkipDir}:${DEP_DIRDEPS_FILTER:ts:}:C,//+,/,g:O:u:@d@${SRCTOP}/$d@}
587
588 # some entries may be qualified with .<machine> 
589 # the :M*/*/*.* just tries to limit the dirs we check to likely ones.
590 # the ${d:E:M*/*} ensures we don't consider junos/usr.sbin/mgd
591 __qual_depdirs := ${__depdirs:M*/*/*.*:@d@${exists($d):?:${"${d:E:M*/*}":?:${exists(${d:R}):?$d:}}}@}
592 __unqual_depdirs := ${__depdirs:${__qual_depdirs:Uno:${M_ListToSkip}}}
593
594 .if ${DEP_RELDIR} == ${_DEP_RELDIR}
595 # if it was called out - we likely need it.
596 __hostdpadd := ${DPADD:U.:M${HOST_OBJTOP}/*:S,${HOST_OBJTOP}/,,:H:${NSkipDir}:${DIRDEPS_FILTER:ts:}:S,$,.host,:N.*:@d@${SRCTOP}/$d@}
597 __qual_depdirs += ${__hostdpadd}
598 .endif
599
600 .if ${_debug_reldir}
601 .info depdirs=${__depdirs}
602 .info qualified=${__qual_depdirs}
603 .info unqualified=${__unqual_depdirs}
604 .endif
605
606 # _build_dirs is what we will feed to _DIRDEP_USE
607 _build_dirs += \
608         ${__qual_depdirs:M*.host:${NSkipHostDir}:N.host} \
609         ${__qual_depdirs:N*.host} \
610         ${_machines:Mhost*:@m@${__unqual_depdirs:@d@$d.$m@}@:${NSkipHostDir}:N.host} \
611         ${_machines:Nhost*:@m@${__unqual_depdirs:@d@$d.$m@}@}
612
613 # qualify everything now
614 _build_dirs := ${_build_dirs:${M_dep_qual_fixes:ts:}:O:u}
615
616 _build_all_dirs += ${_build_dirs}
617 _build_all_dirs := ${_build_all_dirs:O:u}
618
619 .endif                          # empty DIRDEPS
620
621 # Normally if doing make -V something,
622 # we do not want to waste time chasing DIRDEPS
623 # but if we want to count the number of Makefile.depend* read, we do.
624 .if ${.MAKEFLAGS:M-V${_V_READ_DIRDEPS}} == ""
625 .if !empty(_build_all_dirs)
626 .if ${BUILD_DIRDEPS_CACHE} == "yes"
627 x!= { echo; echo '\# ${DEP_RELDIR}.${DEP_TARGET_SPEC}'; \
628         echo 'dirdeps: ${_build_all_dirs:${M_oneperline}}'; echo; } >&3; echo
629 x!= { ${_build_all_dirs:@x@${target($x):?:echo '$x: _DIRDEP_USE';}@} echo; } >&3; echo
630 .else
631 # this makes it all happen
632 dirdeps: ${_build_all_dirs}
633 .endif
634 ${_build_all_dirs}:     _DIRDEP_USE
635
636 .if ${_debug_reldir}
637 .info ${DEP_RELDIR}.${DEP_TARGET_SPEC}: needs: ${_build_dirs}
638 .endif
639
640 # this builds the dependency graph
641 .for m in ${_machines}
642 # it would be nice to do :N${.TARGET}
643 .if !empty(__qual_depdirs)
644 .for q in ${__qual_depdirs:${M_dep_qual_fixes:ts:}:E:O:u:N$m}
645 .if ${_debug_reldir} || ${DEBUG_DIRDEPS:@x@${${DEP_RELDIR}.$m:L:M$x}${${DEP_RELDIR}.$q:L:M$x}@} != ""
646 .info ${DEP_RELDIR}.$m: graph: ${_build_dirs:M*.$q}
647 .endif
648 .if ${BUILD_DIRDEPS_CACHE} == "yes"
649 x!= { echo; echo '${_this_dir}.$m: ${_build_dirs:M*.$q:${M_oneperline}}'; echo; } >&3; echo
650 .else
651 ${_this_dir}.$m: ${_build_dirs:M*.$q}
652 .endif
653 .endfor
654 .endif
655 .if ${_debug_reldir}
656 .info ${DEP_RELDIR}.$m: graph: ${_build_dirs:M*.$m:N${_this_dir}.$m}
657 .endif
658 .if ${BUILD_DIRDEPS_CACHE} == "yes"
659 x!= { echo; echo '${_this_dir}.$m: ${_build_dirs:M*.$m:N${_this_dir}.$m:${M_oneperline}}'; echo; } >&3; echo
660 .else
661 ${_this_dir}.$m: ${_build_dirs:M*.$m:N${_this_dir}.$m}
662 .endif
663 .endfor
664
665 .endif
666
667 # Now find more dependencies - and recurse.
668 .for d in ${_build_all_dirs}
669 .if !target(_dirdeps_checked.$d)
670 # once only
671 _dirdeps_checked.$d:
672 .if ${_debug_search}
673 .info checking $d
674 .endif
675 # Note: _build_all_dirs is fully qualifed so d:R is always the directory
676 .if exists(${d:R})
677 # we pass _DEP_TARGET_SPEC to tell the next step what we want
678 _DEP_TARGET_SPEC := ${d:E}
679 # some makefiles may still look at this
680 _DEP_MACHINE := ${d:E:C/,.*//}
681 # set these too in case Makefile.depend* uses them
682 .if ${TARGET_SPEC_VARS:[#]} > 1
683 _dtspec := ${_DEP_TARGET_SPEC:S/,/ /g}
684 .for i in ${_tspec_x}
685 DEP_${TARGET_SPEC_VARS:[$i]} := ${_dtspec:[$i]}
686 .endfor
687 .else
688 DEP_MACHINE := ${_DEP_MACHINE}
689 .endif
690 # Warning: there is an assumption here that MACHINE is always 
691 # the first entry in TARGET_SPEC_VARS.
692 # If TARGET_SPEC and MACHINE are insufficient, you have a problem.
693 _m := ${.MAKE.DEPENDFILE_PREFERENCE:T:S;${TARGET_SPEC}$;${d:E};:S;${MACHINE};${d:E:C/,.*//};:@m@${exists(${d:R}/$m):?${d:R}/$m:}@:[1]}
694 .if !empty(_m)
695 # M_dep_qual_fixes isn't geared to Makefile.depend
696 _qm := ${_m:C;(\.depend)$;\1.${d:E};:${M_dep_qual_fixes:ts:}}
697 .if ${_debug_search}
698 .info Looking for ${_qm}
699 .endif
700 # set this "just in case" 
701 # we can skip :tA since we computed the path above
702 DEP_RELDIR := ${_m:H:S,${SRCTOP}/,,}
703 # and reset this
704 DIRDEPS =
705 .if ${_debug_reldir} && ${_qm} != ${_m}
706 .info loading ${_m} for ${d:E}
707 .endif
708 .include <${_m}>
709 .endif
710 .endif
711 .endif
712 .endfor
713
714 .endif                          # -V
715 .endif                          # BUILD_DIRDEPS
716
717 .elif ${.MAKE.LEVEL} > 42
718 .error You should have stopped recursing by now.
719 .else
720 # we are building something
721 DEP_RELDIR := ${RELDIR}
722 _DEP_RELDIR := ${RELDIR}
723 # pickup local dependencies
724 .if ${MAKE_VERSION} < 20160220
725 .-include <.depend>
726 .else
727 .dinclude <.depend>
728 .endif
729 .endif
730
731 # bootstrapping new dependencies made easy?
732 .if !target(bootstrap) && (make(bootstrap) || \
733         make(bootstrap-this) || \
734         make(bootstrap-recurse) || \
735         make(bootstrap-empty))
736
737 # if we are bootstrapping create the default
738 _want = ${.CURDIR}/${.MAKE.DEPENDFILE_DEFAULT:T}
739
740 .if exists(${_want})
741 # stop here
742 ${.TARGETS:Mboot*}:
743 .elif !make(bootstrap-empty)
744 # find a Makefile.depend to use as _src
745 _src != cd ${.CURDIR} && for m in ${.MAKE.DEPENDFILE_PREFERENCE:T:S,${MACHINE},*,}; do test -s $$m || continue; echo $$m; break; done; echo
746 .if empty(_src)
747 .error cannot find any of ${.MAKE.DEPENDFILE_PREFERENCE:T}${.newline}Use: bootstrap-empty
748 .endif
749
750 _src?= ${.MAKE.DEPENDFILE}
751
752 .MAKE.DEPENDFILE_BOOTSTRAP_SED+= -e 's/${_src:E:C/,.*//}/${MACHINE}/g'
753
754 # just create Makefile.depend* for this dir
755 bootstrap-this: .NOTMAIN
756         @echo Bootstrapping ${RELDIR}/${_want:T} from ${_src:T}; \
757         echo You need to build ${RELDIR} to correctly populate it.
758 .if ${_src:T} != ${.MAKE.DEPENDFILE_PREFIX:T}
759         (cd ${.CURDIR} && sed ${.MAKE.DEPENDFILE_BOOTSTRAP_SED} ${_src} > ${_want})
760 .else
761         cp ${.CURDIR}/${_src:T} ${_want}
762 .endif
763
764 # create Makefile.depend* for this dir and its dependencies
765 bootstrap: bootstrap-recurse
766 bootstrap-recurse: bootstrap-this
767
768 _mf := ${.PARSEFILE}
769 bootstrap-recurse:      .NOTMAIN .MAKE
770         @cd ${SRCTOP} && \
771         for d in `cd ${RELDIR} && ${.MAKE} -B -f ${"${.MAKEFLAGS:M-n}":?${_src}:${.MAKE.DEPENDFILE:T}} -V DIRDEPS`; do \
772                 test -d $$d || d=$${d%.*}; \
773                 test -d $$d || continue; \
774                 echo "Checking $$d for bootstrap ..."; \
775                 (cd $$d && ${.MAKE} -f ${_mf} bootstrap-recurse); \
776         done
777
778 .endif
779
780 # create an empty Makefile.depend* to get the ball rolling.
781 bootstrap-empty: .NOTMAIN .NOMETA
782         @echo Creating empty ${RELDIR}/${_want:T}; \
783         echo You need to build ${RELDIR} to correctly populate it.
784         @{ echo DIRDEPS=; echo ".include <dirdeps.mk>"; } > ${_want}
785
786 .endif