1284345Ssjg# $FreeBSD: releng/11.0/share/mk/gendirdeps.mk 298225 2016-04-18 20:56:21Z sjg $
2296637Ssjg# $Id: gendirdeps.mk,v 1.30 2016/02/27 00:20:39 sjg Exp $
3284345Ssjg
4284345Ssjg# Copyright (c) 2010-2013, Juniper Networks, Inc.
5284345Ssjg# All rights reserved.
6284345Ssjg# 
7284345Ssjg# Redistribution and use in source and binary forms, with or without
8284345Ssjg# modification, are permitted provided that the following conditions 
9284345Ssjg# are met: 
10284345Ssjg# 1. Redistributions of source code must retain the above copyright
11284345Ssjg#    notice, this list of conditions and the following disclaimer. 
12284345Ssjg# 2. Redistributions in binary form must reproduce the above copyright
13284345Ssjg#    notice, this list of conditions and the following disclaimer in the
14284345Ssjg#    documentation and/or other materials provided with the distribution.  
15284345Ssjg# 
16284345Ssjg# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17284345Ssjg# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18284345Ssjg# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19284345Ssjg# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20284345Ssjg# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21284345Ssjg# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22284345Ssjg# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23284345Ssjg# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24284345Ssjg# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25284345Ssjg# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26284345Ssjg# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
27284345Ssjg
28284345Ssjg#
29284345Ssjg# This makefile [re]generates ${.MAKE.DEPENDFILE}
30284345Ssjg#
31284345Ssjg
32284345Ssjg.include <install-new.mk>
33284345Ssjg
34284345Ssjg# Assumptions:
35284345Ssjg#	RELDIR is the relative path from ${SRCTOP} to ${_CURDIR}
36284345Ssjg#		(SRCTOP is ${SB}/src)
37284345Ssjg#	_CURDIR is the absolute version of ${.CURDIR}
38284345Ssjg#	_OBJDIR is the absolute version of ${.OBJDIR}
39284345Ssjg#	_objroot is realpath of ${_OBJTOP} without ${MACHINE}
40284345Ssjg#		this may be different from _OBJROOT if $SB/obj is a
41284345Ssjg#		symlink to another filesystem.
42284345Ssjg#		_objroot must be a prefix match for _objtop
43284345Ssjg
44284345Ssjg.MAIN: all
45284345Ssjg
46284345Ssjg# keep this simple
47284345Ssjg.MAKE.MODE = compat
48284345Ssjg
49284345Ssjgall:
50284345Ssjg
51284345Ssjg_CURDIR ?= ${.CURDIR}
52284345Ssjg_OBJDIR ?= ${.OBJDIR}
53284345Ssjg_OBJTOP ?= ${OBJTOP}
54284345Ssjg_OBJROOT ?= ${OBJROOT:U${_OBJTOP}}
55284345Ssjg.if ${_OBJROOT:M*/}
56284345Ssjg_slash=/
57284345Ssjg.else
58284345Ssjg_slash=
59284345Ssjg.endif
60284345Ssjg_objroot ?= ${_OBJROOT:tA}${_slash}
61284345Ssjg
62284345Ssjg_this = ${.PARSEDIR}/${.PARSEFILE}
63284345Ssjg
64284345Ssjg# remember what to make
65284345Ssjg_DEPENDFILE := ${_CURDIR}/${.MAKE.DEPENDFILE:T}
66284345Ssjg
67284345Ssjg# We do _not_ want to read our own output!
68284345Ssjg.MAKE.DEPENDFILE = /dev/null
69284345Ssjg
70284345Ssjg# caller should have set this
71284345SsjgMETA_FILES ?= ${.MAKE.META.FILES}
72284345Ssjg
73284345Ssjg.if !empty(META_FILES)
74284345Ssjg
75284345Ssjg.if ${.MAKE.LEVEL} > 0 && !empty(GENDIRDEPS_FILTER)
76284345Ssjg# so we can compare below
77284345Ssjg.-include <${_DEPENDFILE}>
78284345Ssjg# yes, I mean :U with no value
79284345Ssjg_DIRDEPS := ${DIRDEPS:U:O:u}
80284345Ssjg.endif
81284345Ssjg
82284345SsjgMETA_FILES := ${META_FILES:T:O:u}
83284345Ssjg.export META_FILES
84284345Ssjg
85284345Ssjg# pickup customizations
86298225Ssjg.-include <local.gendirdeps.mk>
87284345Ssjg
88284345Ssjg# these are actually prefixes that we'll skip
89284345Ssjg# they should all be absolute paths
90284345SsjgSKIP_GENDIRDEPS ?=
91284345Ssjg.if !empty(SKIP_GENDIRDEPS)
92284345Ssjg_skip_gendirdeps = egrep -v '^(${SKIP_GENDIRDEPS:O:u:ts|})' |
93284345Ssjg.else
94284345Ssjg_skip_gendirdeps =
95284345Ssjg.endif
96284345Ssjg
97284345Ssjg# Below we will turn _{VAR} into ${VAR} which keeps this simple
98284345Ssjg# GENDIRDEPS_FILTER_DIR_VARS is a list of dirs to be substiuted for.
99284345Ssjg# GENDIRDEPS_FILTER_VARS is more general.
100284345Ssjg# In each case order matters.
101284345Ssjg.if !empty(GENDIRDEPS_FILTER_DIR_VARS)
102284345SsjgGENDIRDEPS_FILTER += ${GENDIRDEPS_FILTER_DIR_VARS:@v@S,${$v},_{${v}},@}
103284345Ssjg.endif
104284345Ssjg.if !empty(GENDIRDEPS_FILTER_VARS)
105284345SsjgGENDIRDEPS_FILTER += ${GENDIRDEPS_FILTER_VARS:@v@S,/${$v}/,/_{${v}}/,@:NS,//,*:u}
106284345Ssjg.endif
107284345Ssjg
108284345Ssjg# this (*should* be set in meta.sys.mk) 
109284345Ssjg# is the script that extracts what we want.
110284345SsjgMETA2DEPS ?= ${.PARSEDIR}/meta2deps.sh
111284345SsjgMETA2DEPS := ${META2DEPS}
112284345Ssjg
113284345Ssjg.if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != "" && ${DEBUG_GENDIRDEPS:Uno:Mmeta2d*} != ""
114284345Ssjg_time = time
115284345Ssjg_sh_x = sh -x
116284345Ssjg_py_d = -ddd
117284345Ssjg.else
118284345Ssjg_time =
119284345Ssjg_sh_x =
120284345Ssjg_py_d =
121284345Ssjg.endif
122284345Ssjg
123284345Ssjg.if ${META2DEPS:E} == "py"
124284345Ssjg# we can afford to do this all the time.
125284345SsjgDPDEPS ?= no
126284345SsjgMETA2DEPS_CMD = ${_time} ${PYTHON} ${META2DEPS} ${_py_d} 
127284345Ssjg.if ${DPDEPS:tl} != "no"
128284345SsjgMETA2DEPS_CMD += -D ${DPDEPS}
129284345Ssjg.endif
130284345SsjgMETA2DEPS_FILTER = sed 's,^src:,${SRCTOP}/,;s,^\([^/]\),${OBJTOP}/\1,' |
131284345Ssjg.elif ${META2DEPS:E} == "sh"
132284345SsjgMETA2DEPS_CMD = ${_time} ${_sh_x} ${META2DEPS} OBJTOP=${_OBJTOP}
133284345Ssjg.else
134284345SsjgMETA2DEPS_CMD ?= ${META2DEPS}
135284345Ssjg.endif
136284345Ssjg
137284345Ssjg.if ${TARGET_OBJ_SPEC:U${MACHINE}} != ${MACHINE}
138284345SsjgMETA2DEPS_CMD += -T ${TARGET_OBJ_SPEC}
139284345Ssjg.endif
140284345SsjgMETA2DEPS_CMD += \
141284345Ssjg	-R ${RELDIR} -H ${HOST_TARGET} \
142298225Ssjg	${M2D_OBJROOTS:O:u:@o@-O $o@} \
143298225Ssjg	${M2D_EXCLUDES:O:u:@o@-X $o@} \
144284345Ssjg
145284345Ssjg
146284345SsjgM2D_OBJROOTS += ${OBJTOP} ${_OBJROOT} ${_objroot}
147284345Ssjg.if defined(SB_OBJROOT)
148284345SsjgM2D_OBJROOTS += ${SB_OBJROOT}
149284345Ssjg.endif
150284345Ssjg.if ${.MAKE.DEPENDFILE_PREFERENCE:U${.MAKE.DEPENDFILE}:M*.${MACHINE}} == ""
151284345Ssjg# meta2deps.py only groks objroot
152284345Ssjg# so we need to give it what it expects
153284345Ssjg# and tell it not to add machine qualifiers
154284345SsjgMETA2DEPS_ARGS += MACHINE=none
155284345Ssjg.endif
156284345Ssjg.if defined(SB_BACKING_SB) 
157284345SsjgMETA2DEPS_CMD += -S ${SB_BACKING_SB}/src 
158284345SsjgM2D_OBJROOTS += ${SB_BACKING_SB}/${SB_OBJPREFIX}
159284345Ssjg.endif
160284345Ssjg
161284345Ssjg# we are only interested in the dirs
162294743Sbdrewery# specifically those we read something from.
163284345Ssjg# we canonicalize them to keep things simple
164284345Ssjg# if we are using a split-fs sandbox, it gets a little messier.
165284345Ssjg_objtop := ${_OBJTOP:tA}
166284345Ssjgdir_list != cd ${_OBJDIR} && \
167284345Ssjg	${META2DEPS_CMD} MACHINE=${MACHINE} \
168284345Ssjg	SRCTOP=${SRCTOP} RELDIR=${RELDIR} CURDIR=${_CURDIR} \
169284345Ssjg	${META2DEPS_ARGS} \
170284345Ssjg	${META_FILES:O:u} | ${META2DEPS_FILTER} ${_skip_gendirdeps} \
171284345Ssjg	sed 's,//*$$,,;s,\.${HOST_TARGET}$$,.host,'
172284345Ssjg
173284345Ssjg.if ${dir_list:M*ERROR\:*} != ""
174284345Ssjg.warning ${dir_list:tW:C,.*(ERROR),\1,}
175284345Ssjg.warning Skipping ${_DEPENDFILE:S,${SRCTOP}/,,}
176284345Ssjg# we are not going to update anything
177284345Ssjg.else
178284345Ssjgdpadd_dir_list=
179284345Ssjg.if !empty(DPADD)
180284345Ssjg_nonlibs := ${DPADD:T:Nlib*:N*include}
181284345Ssjg.if !empty(_nonlibs)
182284345Ssjgddep_list =
183284345Ssjg.for f in ${_nonlibs:@x@${DPADD:M*/$x}@}
184284345Ssjg.if exists($f.dirdep)
185284345Ssjgddep_list += $f.dirdep
186284345Ssjg.elif exists(${f:H}.dirdep)
187284345Ssjgddep_list += ${f:H}.dirdep
188284345Ssjg.else
189284345Ssjgdir_list += ${f:H:tA}
190284345Ssjgdpadd_dir_list += ${f:H:tA}
191284345Ssjg.endif
192284345Ssjg.endfor
193284345Ssjg.if !empty(ddep_list)
194284345Ssjgddeps != cat ${ddep_list:O:u} | ${META2DEPS_FILTER} ${_skip_gendirdeps} \
195284345Ssjg        sed 's,//*$$,,;s,\.${HOST_TARGET}$$,.host,;s,\.${MACHINE}$$,,'
196284345Ssjg
197284345Ssjg.if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != ""
198284345Ssjg.info ${RELDIR}: raw_dir_list='${dir_list}'
199284345Ssjg.info ${RELDIR}: ddeps='${ddeps}'
200284345Ssjg.endif
201284345Ssjgdir_list += ${ddeps}
202284345Ssjg.endif
203284345Ssjg.endif
204284345Ssjg.endif
205284345Ssjg
206284345Ssjg# DIRDEPS represent things that had to have been built first
207284345Ssjg# so they should all be undir OBJTOP.
208284345Ssjg# Note that ${_OBJTOP}/bsd/include/machine will get reported 
209284345Ssjg# to us as $SRCTOP/bsd/sys/$MACHINE_ARCH/include meaning we
210284345Ssjg# will want to visit bsd/include
211284345Ssjg# so we add 
212284345Ssjg# ${"${dir_list:M*bsd/sys/${MACHINE_ARCH}/include}":?bsd/include:}
213284345Ssjg# to GENDIRDEPS_DIR_LIST_XTRAS
214284345Ssjg_objtops = ${OBJTOP} ${_OBJTOP} ${_objtop}
215284345Ssjg_objtops := ${_objtops:O:u}
216284345Ssjgdirdep_list = \
217284345Ssjg	${_objtops:@o@${dir_list:M$o*/*:C,$o[^/]*/,,}@} \
218284345Ssjg	${GENDIRDEPS_DIR_LIST_XTRAS}
219284345Ssjg
220284345Ssjg# sort longest first
221284345SsjgM2D_OBJROOTS := ${M2D_OBJROOTS:O:u:[-1..1]}
222284345Ssjg
223284345Ssjg# anything we use from an object dir other than ours
224284345Ssjg# needs to be qualified with its .<machine> suffix
225284345Ssjg# (we used the pseudo machine "host" for the HOST_TARGET).
226284345Ssjgskip_ql= ${SRCTOP}* ${_objtops:@o@$o*@}
227284345Ssjg.for o in ${M2D_OBJROOTS:${skip_ql:${M_ListToSkip}}}
228284345Ssjg# we need := so only skip_ql to this point applies
229284345Ssjgql.$o := ${dir_list:${skip_ql:${M_ListToSkip}}:M$o*/*/*:C,$o([^/]+)/(.*),\2.\1,:S,.${HOST_TARGET},.host,}
230284345Ssjgqualdir_list += ${ql.$o}
231284345Ssjg.if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != ""
232284345Ssjg.info ${RELDIR}: o=$o ${ql.$o qualdir_list:L:@v@$v=${$v}@}
233284345Ssjg.endif
234284345Ssjgskip_ql+= $o*
235284345Ssjg.endfor
236284345Ssjg
237284345Ssjgdirdep_list := ${dirdep_list:O:u}
238284345Ssjgqualdir_list := ${qualdir_list:N*.${MACHINE}:O:u}
239284345Ssjg
240284345SsjgDIRDEPS = \
241284345Ssjg	${dirdep_list:N${RELDIR}:N${RELDIR}/*} \
242284345Ssjg	${qualdir_list:N${RELDIR}.*:N${RELDIR}/*}
243284345Ssjg
244284345Ssjg# We only consider things below $RELDIR/ if they have a makefile.
245284345Ssjg# This is the same test that _DIRDEP_USE applies.
246284345Ssjg# We have do a double test with dirdep_list as it _may_ contain 
247284345Ssjg# qualified dirs - if we got anything from a stage dir.
248284345Ssjg# qualdir_list we know are all qualified.
249284345Ssjg# It would be nice do peform this check for all of DIRDEPS,
250284345Ssjg# but we cannot assume that all of the tree is present, 
251284345Ssjg# in fact we can only assume that RELDIR is.
252284345SsjgDIRDEPS += \
253284345Ssjg	${dirdep_list:M${RELDIR}/*:@d@${.MAKE.MAKEFILE_PREFERENCE:@m@${exists(${SRCTOP}/$d/$m):?$d:${exists(${SRCTOP}/${d:R}/$m):?$d:}}@}@} \
254284345Ssjg	${qualdir_list:M${RELDIR}/*:@d@${.MAKE.MAKEFILE_PREFERENCE:@m@${exists(${SRCTOP}/${d:R}/$m):?$d:}@}@}
255284345Ssjg
256284345SsjgDIRDEPS := ${DIRDEPS:${GENDIRDEPS_FILTER:UNno:ts:}:C,//+,/,g:O:u}
257284345Ssjg
258284345Ssjg.if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != ""
259284345Ssjg.info ${RELDIR}: M2D_OBJROOTS=${M2D_OBJROOTS}
260298225Ssjg.info ${RELDIR}: M2D_EXCLUDES=${M2D_EXCLUDES}
261284345Ssjg.info ${RELDIR}: dir_list='${dir_list}'
262284345Ssjg.info ${RELDIR}: dpadd_dir_list='${dpadd_dir_list}'
263284345Ssjg.info ${RELDIR}: dirdep_list='${dirdep_list}'
264284345Ssjg.info ${RELDIR}: qualdir_list='${qualdir_list}'
265284345Ssjg.info ${RELDIR}: SKIP_GENDIRDEPS='${SKIP_GENDIRDEPS}'
266284345Ssjg.info ${RELDIR}: GENDIRDEPS_FILTER='${GENDIRDEPS_FILTER}'
267284345Ssjg.info ${RELDIR}: FORCE_DPADD='${DPADD}'
268284345Ssjg.info ${RELDIR}: DIRDEPS='${DIRDEPS}'
269284345Ssjg.endif
270284345Ssjg
271284345Ssjg# SRC_DIRDEPS is for checkout logic
272284345Ssjgsrc_dirdep_list = \
273284345Ssjg	${dir_list:M${SRCTOP}/*:S,${SRCTOP}/,,}
274284345Ssjg
275284345SsjgSRC_DIRDEPS = \
276284345Ssjg	${src_dirdep_list:N${RELDIR}:N${RELDIR}/*:C,(/h)/.*,,}
277284345Ssjg
278284345SsjgSRC_DIRDEPS := ${SRC_DIRDEPS:${GENDIRDEPS_SRC_FILTER:UN/*:ts:}:C,//+,/,g:O:u}
279284345Ssjg
280284345Ssjg# if you want to capture SRC_DIRDEPS in .MAKE.DEPENDFILE put
281284345Ssjg# SRC_DIRDEPS_FILE = ${_DEPENDFILE} 
282284345Ssjg# in local.gendirdeps.mk
283284345Ssjg.if ${SRC_DIRDEPS_FILE:Uno:tl} != "no"
284284345SsjgECHO_SRC_DIRDEPS = echo 'SRC_DIRDEPS = \'; echo '${SRC_DIRDEPS:@d@	$d \\${.newline}@}'; echo;
285284345Ssjg
286284345Ssjg.if ${SRC_DIRDEPS_FILE:T} == ${_DEPENDFILE:T}
287284345Ssjg_include_src_dirdeps = ${ECHO_SRC_DIRDEPS}
288284345Ssjg.else
289284345Ssjgall: ${SRC_DIRDEPS_FILE}
290284345Ssjg.if !target(${SRC_DIRDEPS_FILE})
291284345Ssjg${SRC_DIRDEPS_FILE}: ${META_FILES} ${_this} ${META2DEPS}
292284345Ssjg	@(${ECHO_SRC_DIRDEPS}) > $@
293284345Ssjg.endif
294284345Ssjg.endif
295284345Ssjg.endif
296284345Ssjg_include_src_dirdeps ?= 
297284345Ssjg
298284345Ssjgall:	${_DEPENDFILE}
299284345Ssjg
300284345Ssjg# if this is going to exist it would be there by now
301284345Ssjg.if !exists(.depend)
302284345SsjgCAT_DEPEND = /dev/null
303284345Ssjg.endif
304284345SsjgCAT_DEPEND ?= .depend
305284345Ssjg
306284345Ssjg.if !empty(_DIRDEPS) && ${DIRDEPS} != ${_DIRDEPS}
307284345Ssjg# we may have changed a filter
308284345Ssjg.PHONY: ${_DEPENDFILE}
309284345Ssjg.endif
310284345Ssjg
311284345Ssjg# 'cat .depend' should suffice, but if we are mixing build modes
312284345Ssjg# .depend may contain things we don't want.
313284345Ssjg# The sed command at the end of the stream, allows for the filters
314284345Ssjg# to output _{VAR} tokens which we will turn into proper ${VAR} references.
315296637Ssjg${_DEPENDFILE}: .NOMETA ${CAT_DEPEND:M.depend} ${META_FILES:O:u:@m@${exists($m):?$m:}@} ${_this} ${META2DEPS}
316284345Ssjg	@(${GENDIRDEPS_HEADER} echo '# Autogenerated - do NOT edit!'; echo; \
317284345Ssjg	echo 'DIRDEPS = \'; \
318284345Ssjg	echo '${DIRDEPS:@d@	$d \\${.newline}@}'; echo; \
319284345Ssjg	${_include_src_dirdeps} \
320284345Ssjg	echo '.include <dirdeps.mk>'; \
321284345Ssjg	echo; \
322284345Ssjg	echo '.if $${DEP_RELDIR} == $${_DEP_RELDIR}'; \
323284345Ssjg	echo '# local dependencies - needed for -jN in clean tree'; \
324284345Ssjg	[ -s ${CAT_DEPEND} ] && { grep : ${CAT_DEPEND} | grep -v '[/\\]'; }; \
325284345Ssjg	echo '.endif' ) | sed 's,_\([{(]\),$$\1,g' > $@.new${.MAKE.PID}
326284345Ssjg	@${InstallNew}; InstallNew -s $@.new${.MAKE.PID}
327284345Ssjg
328284345Ssjg.endif				# meta2deps failed
329284345Ssjg.elif !empty(SUBDIR)
330284345Ssjg
331284345SsjgDIRDEPS := ${SUBDIR:S,^,${RELDIR}/,:O:u}
332284345Ssjg
333284345Ssjgall:	${_DEPENDFILE}
334284345Ssjg
335296637Ssjg${_DEPENDFILE}: .NOMETA ${MAKEFILE} ${_this}
336284345Ssjg	@(${GENDIRDEPS_HEADER} echo '# Autogenerated - do NOT edit!'; echo; \
337284345Ssjg	echo 'DIRDEPS = \'; \
338284345Ssjg	echo '${DIRDEPS:@d@	$d \\${.newline}@}'; echo; \
339284345Ssjg	echo '.include <dirdeps.mk>'; \
340284345Ssjg	echo ) | sed 's,_\([{(]\),$$\1,g' > $@.new
341284345Ssjg	@${InstallNew}; InstallNew $@.new
342284345Ssjg
343284345Ssjg.else
344284345Ssjg
345284345Ssjg# nothing to do
346284345Ssjgall ${_DEPENDFILE}:
347284345Ssjg
348284345Ssjg.endif
349284345Ssjg${_DEPENDFILE}: .PRECIOUS
350