]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bmake/mk/gendirdeps.mk
Update to bmake-20200517
[FreeBSD/FreeBSD.git] / contrib / bmake / mk / gendirdeps.mk
1 # $Id: gendirdeps.mk,v 1.42 2020/05/16 23:21:48 sjg Exp $
2
3 # Copyright (c) 2011-2020, 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 #
29 # This makefile [re]generates ${.MAKE.DEPENDFILE}
30 #
31
32 .include <install-new.mk>
33
34 # Assumptions:
35 #       RELDIR is the relative path from ${SRCTOP} to ${_CURDIR}
36 #               (SRCTOP is ${SB}/src)
37 #       _CURDIR is the absolute version of ${.CURDIR}
38 #       _OBJDIR is the absolute version of ${.OBJDIR}
39 #       _objroot is realpath of ${_OBJTOP} without ${MACHINE}
40 #               this may be different from _OBJROOT if $SB/obj is a
41 #               symlink to another filesystem.
42 #               _objroot must be a prefix match for _objtop
43
44 .MAIN: all
45
46 # keep this simple
47 .MAKE.MODE = compat
48
49 all:
50
51 _CURDIR ?= ${.CURDIR}
52 _OBJDIR ?= ${.OBJDIR}
53 _OBJTOP ?= ${OBJTOP}
54 _OBJROOT ?= ${OBJROOT:U${_OBJTOP}}
55 .if ${_OBJROOT:M*/}
56 _slash=/
57 .else
58 _slash=
59 .endif
60 _objroot ?= ${_OBJROOT:tA}${_slash}
61
62 _this = ${.PARSEDIR}/${.PARSEFILE}
63
64 # remember what to make
65 _DEPENDFILE := ${_CURDIR}/${.MAKE.DEPENDFILE:T}
66
67 # We do _not_ want to read our own output!
68 .MAKE.DEPENDFILE = /dev/null
69
70 # caller should have set this
71 META_FILES ?= ${.MAKE.META.FILES}
72
73 .if !empty(META_FILES)
74
75 .if ${.MAKE.LEVEL} > 0 && !empty(GENDIRDEPS_FILTER)
76 # so we can compare below
77 .-include <${_DEPENDFILE}>
78 # yes, I mean :U with no value
79 _DIRDEPS := ${DIRDEPS:U:O:u}
80 .endif
81
82 META_FILES := ${META_FILES:T:O:u}
83
84 # pickup customizations
85 .-include <local.gendirdeps.mk>
86
87 # these are actually prefixes that we'll skip
88 # they should all be absolute paths
89 SKIP_GENDIRDEPS ?=
90 .if !empty(SKIP_GENDIRDEPS)
91 _skip_gendirdeps = egrep -v '^(${SKIP_GENDIRDEPS:O:u:ts|})' |
92 .else
93 _skip_gendirdeps =
94 .endif
95
96 # Below we will turn _{VAR} into ${VAR} which keeps this simple
97 # GENDIRDEPS_FILTER_DIR_VARS is a list of dirs to be substiuted for.
98 # GENDIRDEPS_FILTER_VARS is more general.
99 # In each case order matters.
100 .if !empty(GENDIRDEPS_FILTER_DIR_VARS)
101 GENDIRDEPS_FILTER += ${GENDIRDEPS_FILTER_DIR_VARS:@v@S,${$v},_{${v}},@}
102 .endif
103 .if !empty(GENDIRDEPS_FILTER_VARS)
104 GENDIRDEPS_FILTER += ${GENDIRDEPS_FILTER_VARS:@v@S,/${$v}/,/_{${v}}/,@:NS,//,*:u}
105 .endif
106
107 # this (*should* be set in meta.sys.mk) 
108 # is the script that extracts what we want.
109 META2DEPS ?= ${.PARSEDIR}/meta2deps.sh
110 META2DEPS := ${META2DEPS}
111
112 .if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != "" && ${DEBUG_GENDIRDEPS:Uno:Mmeta2d*} != ""
113 _time = time
114 _sh_x = sh -x
115 _py_d = -ddd
116 .else
117 _time =
118 _sh_x =
119 _py_d =
120 .endif
121
122 .if ${META2DEPS:E} == "py"
123 # we can afford to do this all the time.
124 DPDEPS ?= no
125 META2DEPS_CMD = ${_time} ${PYTHON} ${META2DEPS} ${_py_d} 
126 .if ${DPDEPS:tl} != "no"
127 META2DEPS_CMD += -D ${DPDEPS}
128 .endif
129 META2DEPS_FILTER = sed 's,^src:,${SRCTOP}/,;s,^\([^/]\),${OBJTOP}/\1,' |
130 .elif ${META2DEPS:E} == "sh"
131 META2DEPS_CMD = ${_time} ${_sh_x} ${META2DEPS} OBJTOP=${_OBJTOP}
132 .else
133 META2DEPS_CMD ?= ${META2DEPS}
134 .endif
135
136 .if ${TARGET_OBJ_SPEC:U${MACHINE}} != ${MACHINE}
137 META2DEPS_CMD += -T ${TARGET_OBJ_SPEC}
138 .endif
139 META2DEPS_CMD += \
140         -R ${RELDIR} -H ${HOST_TARGET} \
141         ${M2D_OBJROOTS:O:u:@o@-O $o@} \
142         ${M2D_EXCLUDES:O:u:@o@-X $o@} \
143
144
145 M2D_OBJROOTS += ${OBJTOP} ${_OBJROOT} ${_objroot}
146 .if defined(SB_OBJROOT)
147 M2D_OBJROOTS += ${SB_OBJROOT}
148 .endif
149 .if defined(STAGE_ROOT)
150 M2D_OBJROOTS += ${STAGE_ROOT}
151 .endif
152 .if ${.MAKE.DEPENDFILE_PREFERENCE:U${.MAKE.DEPENDFILE}:M*.${MACHINE}} == ""
153 # meta2deps.py only groks objroot
154 # so we need to give it what it expects
155 # and tell it not to add machine qualifiers
156 META2DEPS_ARGS += MACHINE=none
157 .endif
158 .if defined(SB_BACKING_SB) 
159 META2DEPS_CMD += -S ${SB_BACKING_SB}/src 
160 M2D_OBJROOTS += ${SB_BACKING_SB}/${SB_OBJPREFIX}
161 .endif
162
163 GENDIRDEPS_SEDCMDS += \
164         -e 's,//*$$,,;s,\.${HOST_TARGET:Uhost}$$,.host,' \
165         -e 's,\.${HOST_TARGET32:Uhost32}$$,.host32,' \
166         -e 's,\.${MACHINE}$$,,' \
167         -e 's:\.${TARGET_SPEC:U${MACHINE}}$$::'
168
169 # we are only interested in the dirs
170 # specifically those we read something from.
171 # we canonicalize them to keep things simple
172 # if we are using a split-fs sandbox, it gets a little messier.
173 _objtop := ${_OBJTOP:tA}
174
175 # some people put *.meta in META_XTRAS to make sure we get here
176 _meta_files := ${META_FILES:N\*.meta:O:u}
177 # assume a big list
178 _meta_files_arg= @meta.list
179 .if empty(_meta_files) && ${META_FILES:M\*.meta} != ""
180 # XXX this should be considered a bad idea, 
181 # since we cannot ignore stale .meta
182 x != cd ${_OBJDIR} && find . -name '*.meta' -print -o \( -type d ! -name . -prune \) | sed 's,^./,,' > meta.list; echo
183 .elif ${_meta_files:[#]} > 500
184 .export _meta_files
185 x != echo; for m in $$_meta_files; do echo $$m; done > meta.list
186 # _meta_files is consuming a lot of env space
187 # that can impact command line length,
188 # and we do not need it any more
189 .undef _meta_files
190 .unexport _meta_files
191 .else
192 _meta_files_arg:= ${_meta_files}
193 .endif
194
195 dir_list != cd ${_OBJDIR} && \
196         ${META2DEPS_CMD} MACHINE=${MACHINE} \
197         SRCTOP=${SRCTOP} RELDIR=${RELDIR} CURDIR=${_CURDIR} \
198         ${META2DEPS_ARGS} \
199         ${_meta_files_arg} | ${META2DEPS_FILTER} ${_skip_gendirdeps} \
200         sed ${GENDIRDEPS_SEDCMDS}
201
202 .if ${dir_list:M*ERROR\:*} != ""
203 .warning ${dir_list:tW:C,.*(ERROR),\1,}
204 .warning Skipping ${_DEPENDFILE:S,${SRCTOP}/,,}
205 # we are not going to update anything
206 .else
207 dpadd_dir_list=
208 .if !empty(DPADD)
209 _nonlibs := ${DPADD:T:Nlib*:N*include}
210 .if !empty(_nonlibs)
211 ddep_list =
212 .for f in ${_nonlibs:@x@${DPADD:M*/$x}@}
213 .if exists($f.dirdep)
214 ddep_list += $f.dirdep
215 .elif exists(${f:H}.dirdep)
216 ddep_list += ${f:H}.dirdep
217 .else
218 dir_list += ${f:H:tA}
219 dpadd_dir_list += ${f:H:tA}
220 .endif
221 .endfor
222 .if !empty(ddep_list)
223 ddeps != cat ${ddep_list:O:u} | ${META2DEPS_FILTER} ${_skip_gendirdeps} \
224         sed ${GENDIRDEPS_SEDCMDS}
225
226 .if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != ""
227 .info ${RELDIR}: raw_dir_list='${dir_list}'
228 .info ${RELDIR}: ddeps='${ddeps}'
229 .endif
230 dir_list += ${ddeps}
231 .endif
232 .endif
233 .endif
234
235 # DIRDEPS represent things that had to have been built first
236 # so they should all be undir OBJTOP.
237 # Note that ${_OBJTOP}/bsd/include/machine will get reported 
238 # to us as $SRCTOP/bsd/sys/$MACHINE_ARCH/include meaning we
239 # will want to visit bsd/include
240 # so we add 
241 # ${"${dir_list:M*bsd/sys/${MACHINE_ARCH}/include}":?bsd/include:}
242 # to GENDIRDEPS_DIR_LIST_XTRAS
243 _objtops = ${OBJTOP} ${_OBJTOP} ${_objtop}
244 _objtops := ${_objtops:O:u}
245 dirdep_list = \
246         ${_objtops:@o@${dir_list:M$o*/*:C,$o[^/]*/,,}@} \
247         ${GENDIRDEPS_DIR_LIST_XTRAS}
248
249 # sort longest first
250 M2D_OBJROOTS := ${M2D_OBJROOTS:O:u:[-1..1]}
251
252 # anything we use from an object dir other than ours
253 # needs to be qualified with its .<machine> suffix
254 # (we used the pseudo machine "host" for the HOST_TARGET).
255 skip_ql= ${SRCTOP}* ${_objtops:@o@$o*@}
256 .for o in ${M2D_OBJROOTS:${skip_ql:${M_ListToSkip}}}
257 # we need := so only skip_ql to this point applies
258 ql.$o := ${dir_list:${skip_ql:${M_ListToSkip}}:M$o*/*/*:C,$o([^/]+)/(.*),\2.\1,:S,.${HOST_TARGET},.host,}
259 qualdir_list += ${ql.$o}
260 .if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != ""
261 .info ${RELDIR}: o=$o ${ql.$o qualdir_list:L:@v@$v=${$v}@}
262 .endif
263 skip_ql+= $o*
264 .endfor
265
266 dirdep_list := ${dirdep_list:O:u}
267 qualdir_list := ${qualdir_list:N*.${MACHINE}:O:u}
268
269 DIRDEPS = \
270         ${dirdep_list:N${RELDIR}:N${RELDIR}/*} \
271         ${qualdir_list:N${RELDIR}.*:N${RELDIR}/*}
272
273 # We only consider things below $RELDIR/ if they have a makefile.
274 # This is the same test that _DIRDEP_USE applies.
275 # We have do a double test with dirdep_list as it _may_ contain 
276 # qualified dirs - if we got anything from a stage dir.
277 # qualdir_list we know are all qualified.
278 # It would be nice do peform this check for all of DIRDEPS,
279 # but we cannot assume that all of the tree is present, 
280 # in fact we can only assume that RELDIR is.
281 DIRDEPS += \
282         ${dirdep_list:M${RELDIR}/*:@d@${.MAKE.MAKEFILE_PREFERENCE:@m@${exists(${SRCTOP}/$d/$m):?$d:${exists(${SRCTOP}/${d:R}/$m):?$d:}}@}@} \
283         ${qualdir_list:M${RELDIR}/*:@d@${.MAKE.MAKEFILE_PREFERENCE:@m@${exists(${SRCTOP}/${d:R}/$m):?$d:}@}@}
284
285 # what modifiers do we allow in GENDIRDEPS_FILTER
286 GENDIRDEPS_FILTER_MASK += @CMNS
287 DIRDEPS := ${DIRDEPS:${GENDIRDEPS_FILTER:UNno:M[${GENDIRDEPS_FILTER_MASK:O:u:ts}]*:ts:}:C,//+,/,g:O:u}
288
289 .if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != ""
290 .info ${RELDIR}: M2D_OBJROOTS=${M2D_OBJROOTS}
291 .info ${RELDIR}: M2D_EXCLUDES=${M2D_EXCLUDES}
292 .info ${RELDIR}: dir_list='${dir_list}'
293 .info ${RELDIR}: dpadd_dir_list='${dpadd_dir_list}'
294 .info ${RELDIR}: dirdep_list='${dirdep_list}'
295 .info ${RELDIR}: qualdir_list='${qualdir_list}'
296 .info ${RELDIR}: SKIP_GENDIRDEPS='${SKIP_GENDIRDEPS}'
297 .info ${RELDIR}: GENDIRDEPS_FILTER='${GENDIRDEPS_FILTER}'
298 .info ${RELDIR}: FORCE_DPADD='${DPADD}'
299 .info ${RELDIR}: DIRDEPS='${DIRDEPS}'
300 .endif
301
302 # SRC_DIRDEPS is for checkout logic
303 src_dirdep_list = \
304         ${dir_list:M${SRCTOP}/*:S,${SRCTOP}/,,}
305
306 SRC_DIRDEPS = \
307         ${src_dirdep_list:N${RELDIR}:N${RELDIR}/*:C,(/h)/.*,,}
308
309 SRC_DIRDEPS := ${SRC_DIRDEPS:${GENDIRDEPS_SRC_FILTER:UN/*:ts:}:C,//+,/,g:O:u}
310
311 # if you want to capture SRC_DIRDEPS in .MAKE.DEPENDFILE put
312 # SRC_DIRDEPS_FILE = ${_DEPENDFILE} 
313 # in local.gendirdeps.mk
314 .if ${SRC_DIRDEPS_FILE:Uno:tl} != "no"
315 ECHO_SRC_DIRDEPS = echo 'SRC_DIRDEPS = \'; echo '${SRC_DIRDEPS:@d@      $d \\${.newline}@}'; echo;
316
317 .if ${SRC_DIRDEPS_FILE:T} == ${_DEPENDFILE:T}
318 _include_src_dirdeps = ${ECHO_SRC_DIRDEPS}
319 .else
320 all: ${SRC_DIRDEPS_FILE}
321 .if !target(${SRC_DIRDEPS_FILE})
322 ${SRC_DIRDEPS_FILE}: ${META_FILES} ${_this} ${META2DEPS}
323         @(${ECHO_SRC_DIRDEPS}) > $@
324 .endif
325 .endif
326 .endif
327 _include_src_dirdeps ?= 
328
329 all:    ${_DEPENDFILE}
330
331 # if this is going to exist it would be there by now
332 .if !exists(.depend)
333 CAT_DEPEND = /dev/null
334 .endif
335 CAT_DEPEND ?= .depend
336
337 .if !empty(_DIRDEPS) && ${DIRDEPS} != ${_DIRDEPS}
338 # we may have changed a filter
339 .PHONY: ${_DEPENDFILE}
340 .endif
341
342 # 'cat .depend' should suffice, but if we are mixing build modes
343 # .depend may contain things we don't want.
344 # The sed command at the end of the stream, allows for the filters
345 # to output _{VAR} tokens which we will turn into proper ${VAR} references.
346 ${_DEPENDFILE}: .NOMETA ${CAT_DEPEND:M.depend} ${META_FILES:O:u:@m@${exists($m):?$m:}@} ${_this} ${META2DEPS}
347         @(${GENDIRDEPS_HEADER} echo '# Autogenerated - do NOT edit!'; echo; \
348         echo 'DIRDEPS = \'; \
349         echo '${DIRDEPS:@d@     $d \\${.newline}@}'; echo; \
350         ${_include_src_dirdeps} \
351         echo '.include <dirdeps.mk>'; \
352         echo; \
353         echo '.if $${DEP_RELDIR} == $${_DEP_RELDIR}'; \
354         echo '# local dependencies - needed for -jN in clean tree'; \
355         [ -s ${CAT_DEPEND} ] && { grep : ${CAT_DEPEND} | grep -v '[/\\]'; }; \
356         echo '.endif' ) | sed 's,_\([{(]\),$$\1,g' > $@.new${.MAKE.PID}
357         @${InstallNew}; InstallNew -s $@.new${.MAKE.PID}
358
359 .endif                          # meta2deps failed
360 .elif !empty(SUBDIR)
361
362 DIRDEPS := ${SUBDIR:S,^,${RELDIR}/,:O:u}
363
364 all:    ${_DEPENDFILE}
365
366 ${_DEPENDFILE}: .NOMETA ${MAKEFILE} ${_this}
367         @(${GENDIRDEPS_HEADER} echo '# Autogenerated - do NOT edit!'; echo; \
368         echo 'DIRDEPS = \'; \
369         echo '${DIRDEPS:@d@     $d \\${.newline}@}'; echo; \
370         echo '.include <dirdeps.mk>'; \
371         echo ) | sed 's,_\([{(]\),$$\1,g' > $@.new
372         @${InstallNew}; InstallNew $@.new
373
374 .else
375
376 # nothing to do
377 all ${_DEPENDFILE}:
378
379 .endif
380 ${_DEPENDFILE}: .PRECIOUS
381
382 # don't waste time looking for ways to make .meta files
383 .SUFFIXES: