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