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