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