1331246Ssjg# $Id: gendirdeps.mk,v 1.37 2018/01/31 19:06:46 sjg Exp $
2246149Ssjg
3249033Ssjg# Copyright (c) 2010-2013, Juniper Networks, Inc.
4249033Ssjg# All rights reserved.
5246149Ssjg# 
6246149Ssjg# Redistribution and use in source and binary forms, with or without
7246149Ssjg# modification, are permitted provided that the following conditions 
8246149Ssjg# are met: 
9246149Ssjg# 1. Redistributions of source code must retain the above copyright
10246149Ssjg#    notice, this list of conditions and the following disclaimer. 
11246149Ssjg# 2. Redistributions in binary form must reproduce the above copyright
12246149Ssjg#    notice, this list of conditions and the following disclaimer in the
13246149Ssjg#    documentation and/or other materials provided with the distribution.  
14246149Ssjg# 
15246149Ssjg# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16246149Ssjg# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17246149Ssjg# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18246149Ssjg# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19246149Ssjg# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20246149Ssjg# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21246149Ssjg# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22246149Ssjg# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23246149Ssjg# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24246149Ssjg# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25246149Ssjg# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
26246149Ssjg
27246149Ssjg#
28246149Ssjg# This makefile [re]generates ${.MAKE.DEPENDFILE}
29246149Ssjg#
30246149Ssjg
31246149Ssjg.include <install-new.mk>
32246149Ssjg
33246149Ssjg# Assumptions:
34246149Ssjg#	RELDIR is the relative path from ${SRCTOP} to ${_CURDIR}
35246149Ssjg#		(SRCTOP is ${SB}/src)
36246149Ssjg#	_CURDIR is the absolute version of ${.CURDIR}
37246149Ssjg#	_OBJDIR is the absolute version of ${.OBJDIR}
38246149Ssjg#	_objroot is realpath of ${_OBJTOP} without ${MACHINE}
39246149Ssjg#		this may be different from _OBJROOT if $SB/obj is a
40246149Ssjg#		symlink to another filesystem.
41246149Ssjg#		_objroot must be a prefix match for _objtop
42246149Ssjg
43246149Ssjg.MAIN: all
44246149Ssjg
45246149Ssjg# keep this simple
46246149Ssjg.MAKE.MODE = compat
47246149Ssjg
48246149Ssjgall:
49246149Ssjg
50246149Ssjg_CURDIR ?= ${.CURDIR}
51246149Ssjg_OBJDIR ?= ${.OBJDIR}
52246149Ssjg_OBJTOP ?= ${OBJTOP}
53246149Ssjg_OBJROOT ?= ${OBJROOT:U${_OBJTOP}}
54249033Ssjg.if ${_OBJROOT:M*/}
55249033Ssjg_slash=/
56249033Ssjg.else
57249033Ssjg_slash=
58249033Ssjg.endif
59249033Ssjg_objroot ?= ${_OBJROOT:tA}${_slash}
60246149Ssjg
61246149Ssjg_this = ${.PARSEDIR}/${.PARSEFILE}
62246149Ssjg
63246149Ssjg# remember what to make
64246149Ssjg_DEPENDFILE := ${_CURDIR}/${.MAKE.DEPENDFILE:T}
65246149Ssjg
66246149Ssjg# We do _not_ want to read our own output!
67246149Ssjg.MAKE.DEPENDFILE = /dev/null
68246149Ssjg
69246149Ssjg# caller should have set this
70246149SsjgMETA_FILES ?= ${.MAKE.META.FILES}
71246149Ssjg
72246149Ssjg.if !empty(META_FILES)
73246149Ssjg
74246149Ssjg.if ${.MAKE.LEVEL} > 0 && !empty(GENDIRDEPS_FILTER)
75246149Ssjg# so we can compare below
76246149Ssjg.-include <${_DEPENDFILE}>
77246149Ssjg# yes, I mean :U with no value
78246149Ssjg_DIRDEPS := ${DIRDEPS:U:O:u}
79246149Ssjg.endif
80246149Ssjg
81246149SsjgMETA_FILES := ${META_FILES:T:O:u}
82246149Ssjg.export META_FILES
83246149Ssjg
84246149Ssjg# pickup customizations
85300313Ssjg.-include <local.gendirdeps.mk>
86246149Ssjg
87246149Ssjg# these are actually prefixes that we'll skip
88246149Ssjg# they should all be absolute paths
89246149SsjgSKIP_GENDIRDEPS ?=
90246149Ssjg.if !empty(SKIP_GENDIRDEPS)
91246149Ssjg_skip_gendirdeps = egrep -v '^(${SKIP_GENDIRDEPS:O:u:ts|})' |
92246149Ssjg.else
93246149Ssjg_skip_gendirdeps =
94246149Ssjg.endif
95246149Ssjg
96281812Ssjg# Below we will turn _{VAR} into ${VAR} which keeps this simple
97281812Ssjg# GENDIRDEPS_FILTER_DIR_VARS is a list of dirs to be substiuted for.
98281812Ssjg# GENDIRDEPS_FILTER_VARS is more general.
99281812Ssjg# In each case order matters.
100281812Ssjg.if !empty(GENDIRDEPS_FILTER_DIR_VARS)
101281812SsjgGENDIRDEPS_FILTER += ${GENDIRDEPS_FILTER_DIR_VARS:@v@S,${$v},_{${v}},@}
102281812Ssjg.endif
103281812Ssjg.if !empty(GENDIRDEPS_FILTER_VARS)
104281812SsjgGENDIRDEPS_FILTER += ${GENDIRDEPS_FILTER_VARS:@v@S,/${$v}/,/_{${v}}/,@:NS,//,*:u}
105281812Ssjg.endif
106281812Ssjg
107246149Ssjg# this (*should* be set in meta.sys.mk) 
108246149Ssjg# is the script that extracts what we want.
109246149SsjgMETA2DEPS ?= ${.PARSEDIR}/meta2deps.sh
110246149SsjgMETA2DEPS := ${META2DEPS}
111246149Ssjg
112246149Ssjg.if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != "" && ${DEBUG_GENDIRDEPS:Uno:Mmeta2d*} != ""
113246149Ssjg_time = time
114246149Ssjg_sh_x = sh -x
115246149Ssjg_py_d = -ddd
116246149Ssjg.else
117246149Ssjg_time =
118246149Ssjg_sh_x =
119246149Ssjg_py_d =
120246149Ssjg.endif
121246149Ssjg
122246149Ssjg.if ${META2DEPS:E} == "py"
123246149Ssjg# we can afford to do this all the time.
124246149SsjgDPDEPS ?= no
125250837SsjgMETA2DEPS_CMD = ${_time} ${PYTHON} ${META2DEPS} ${_py_d} 
126246149Ssjg.if ${DPDEPS:tl} != "no"
127246149SsjgMETA2DEPS_CMD += -D ${DPDEPS}
128246149Ssjg.endif
129250837SsjgMETA2DEPS_FILTER = sed 's,^src:,${SRCTOP}/,;s,^\([^/]\),${OBJTOP}/\1,' |
130250837Ssjg.elif ${META2DEPS:E} == "sh"
131250837SsjgMETA2DEPS_CMD = ${_time} ${_sh_x} ${META2DEPS} OBJTOP=${_OBJTOP}
132250837Ssjg.else
133250837SsjgMETA2DEPS_CMD ?= ${META2DEPS}
134250837Ssjg.endif
135249033Ssjg
136250837Ssjg.if ${TARGET_OBJ_SPEC:U${MACHINE}} != ${MACHINE}
137250837SsjgMETA2DEPS_CMD += -T ${TARGET_OBJ_SPEC}
138250837Ssjg.endif
139250837SsjgMETA2DEPS_CMD += \
140250837Ssjg	-R ${RELDIR} -H ${HOST_TARGET} \
141300313Ssjg	${M2D_OBJROOTS:O:u:@o@-O $o@} \
142300313Ssjg	${M2D_EXCLUDES:O:u:@o@-X $o@} \
143250837Ssjg
144250837Ssjg
145249033SsjgM2D_OBJROOTS += ${OBJTOP} ${_OBJROOT} ${_objroot}
146249033Ssjg.if defined(SB_OBJROOT)
147249033SsjgM2D_OBJROOTS += ${SB_OBJROOT}
148249033Ssjg.endif
149319884Ssjg.if defined(STAGE_ROOT)
150319884SsjgM2D_OBJROOTS += ${STAGE_ROOT}
151319884Ssjg.endif
152246149Ssjg.if ${.MAKE.DEPENDFILE_PREFERENCE:U${.MAKE.DEPENDFILE}:M*.${MACHINE}} == ""
153246149Ssjg# meta2deps.py only groks objroot
154246149Ssjg# so we need to give it what it expects
155246149Ssjg# and tell it not to add machine qualifiers
156246149SsjgMETA2DEPS_ARGS += MACHINE=none
157246149Ssjg.endif
158246149Ssjg.if defined(SB_BACKING_SB) 
159249033SsjgMETA2DEPS_CMD += -S ${SB_BACKING_SB}/src 
160249033SsjgM2D_OBJROOTS += ${SB_BACKING_SB}/${SB_OBJPREFIX}
161246149Ssjg.endif
162246149Ssjg
163246149Ssjg# we are only interested in the dirs
164289842Ssjg# specifically those we read something from.
165246149Ssjg# we canonicalize them to keep things simple
166246149Ssjg# if we are using a split-fs sandbox, it gets a little messier.
167246149Ssjg_objtop := ${_OBJTOP:tA}
168246149Ssjgdir_list != cd ${_OBJDIR} && \
169246149Ssjg	${META2DEPS_CMD} MACHINE=${MACHINE} \
170246149Ssjg	SRCTOP=${SRCTOP} RELDIR=${RELDIR} CURDIR=${_CURDIR} \
171246149Ssjg	${META2DEPS_ARGS} \
172246149Ssjg	${META_FILES:O:u} | ${META2DEPS_FILTER} ${_skip_gendirdeps} \
173246149Ssjg	sed 's,//*$$,,;s,\.${HOST_TARGET}$$,.host,'
174246149Ssjg
175246149Ssjg.if ${dir_list:M*ERROR\:*} != ""
176246149Ssjg.warning ${dir_list:tW:C,.*(ERROR),\1,}
177246149Ssjg.warning Skipping ${_DEPENDFILE:S,${SRCTOP}/,,}
178246149Ssjg# we are not going to update anything
179246149Ssjg.else
180255253Ssjgdpadd_dir_list=
181246149Ssjg.if !empty(DPADD)
182246149Ssjg_nonlibs := ${DPADD:T:Nlib*:N*include}
183246149Ssjg.if !empty(_nonlibs)
184249033Ssjgddep_list =
185249033Ssjg.for f in ${_nonlibs:@x@${DPADD:M*/$x}@}
186249033Ssjg.if exists($f.dirdep)
187249033Ssjgddep_list += $f.dirdep
188249033Ssjg.elif exists(${f:H}.dirdep)
189249033Ssjgddep_list += ${f:H}.dirdep
190249033Ssjg.else
191249033Ssjgdir_list += ${f:H:tA}
192255253Ssjgdpadd_dir_list += ${f:H:tA}
193246149Ssjg.endif
194249033Ssjg.endfor
195249033Ssjg.if !empty(ddep_list)
196249033Ssjgddeps != cat ${ddep_list:O:u} | ${META2DEPS_FILTER} ${_skip_gendirdeps} \
197331246Ssjg        sed 's,//*$$,,;s,\.${HOST_TARGET:Uhost}$$,.host,;s,\.${HOST_TARGET32:Uhost32}$$,.host32,;s,\.${MACHINE}$$,,'
198249033Ssjg
199249033Ssjg.if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != ""
200249033Ssjg.info ${RELDIR}: raw_dir_list='${dir_list}'
201249033Ssjg.info ${RELDIR}: ddeps='${ddeps}'
202246149Ssjg.endif
203249033Ssjgdir_list += ${ddeps}
204249033Ssjg.endif
205249033Ssjg.endif
206249033Ssjg.endif
207246149Ssjg
208246149Ssjg# DIRDEPS represent things that had to have been built first
209246149Ssjg# so they should all be undir OBJTOP.
210246149Ssjg# Note that ${_OBJTOP}/bsd/include/machine will get reported 
211246149Ssjg# to us as $SRCTOP/bsd/sys/$MACHINE_ARCH/include meaning we
212246149Ssjg# will want to visit bsd/include
213246149Ssjg# so we add 
214246149Ssjg# ${"${dir_list:M*bsd/sys/${MACHINE_ARCH}/include}":?bsd/include:}
215246149Ssjg# to GENDIRDEPS_DIR_LIST_XTRAS
216255253Ssjg_objtops = ${OBJTOP} ${_OBJTOP} ${_objtop}
217249033Ssjg_objtops := ${_objtops:O:u}
218246149Ssjgdirdep_list = \
219249033Ssjg	${_objtops:@o@${dir_list:M$o*/*:C,$o[^/]*/,,}@} \
220246149Ssjg	${GENDIRDEPS_DIR_LIST_XTRAS}
221246149Ssjg
222249033Ssjg# sort longest first
223249033SsjgM2D_OBJROOTS := ${M2D_OBJROOTS:O:u:[-1..1]}
224249033Ssjg
225246149Ssjg# anything we use from an object dir other than ours
226246149Ssjg# needs to be qualified with its .<machine> suffix
227246149Ssjg# (we used the pseudo machine "host" for the HOST_TARGET).
228249033Ssjgskip_ql= ${SRCTOP}* ${_objtops:@o@$o*@}
229249033Ssjg.for o in ${M2D_OBJROOTS:${skip_ql:${M_ListToSkip}}}
230249033Ssjg# we need := so only skip_ql to this point applies
231255253Ssjgql.$o := ${dir_list:${skip_ql:${M_ListToSkip}}:M$o*/*/*:C,$o([^/]+)/(.*),\2.\1,:S,.${HOST_TARGET},.host,}
232255253Ssjgqualdir_list += ${ql.$o}
233255253Ssjg.if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != ""
234255253Ssjg.info ${RELDIR}: o=$o ${ql.$o qualdir_list:L:@v@$v=${$v}@}
235255253Ssjg.endif
236249033Ssjgskip_ql+= $o*
237249033Ssjg.endfor
238246149Ssjg
239246149Ssjgdirdep_list := ${dirdep_list:O:u}
240249033Ssjgqualdir_list := ${qualdir_list:N*.${MACHINE}:O:u}
241246149Ssjg
242246149SsjgDIRDEPS = \
243246149Ssjg	${dirdep_list:N${RELDIR}:N${RELDIR}/*} \
244246149Ssjg	${qualdir_list:N${RELDIR}.*:N${RELDIR}/*}
245246149Ssjg
246246149Ssjg# We only consider things below $RELDIR/ if they have a makefile.
247268437Ssjg# This is the same test that _DIRDEP_USE applies.
248246149Ssjg# We have do a double test with dirdep_list as it _may_ contain 
249246149Ssjg# qualified dirs - if we got anything from a stage dir.
250246149Ssjg# qualdir_list we know are all qualified.
251246149Ssjg# It would be nice do peform this check for all of DIRDEPS,
252246149Ssjg# but we cannot assume that all of the tree is present, 
253246149Ssjg# in fact we can only assume that RELDIR is.
254246149SsjgDIRDEPS += \
255246149Ssjg	${dirdep_list:M${RELDIR}/*:@d@${.MAKE.MAKEFILE_PREFERENCE:@m@${exists(${SRCTOP}/$d/$m):?$d:${exists(${SRCTOP}/${d:R}/$m):?$d:}}@}@} \
256246149Ssjg	${qualdir_list:M${RELDIR}/*:@d@${.MAKE.MAKEFILE_PREFERENCE:@m@${exists(${SRCTOP}/${d:R}/$m):?$d:}@}@}
257246149Ssjg
258331246Ssjg# what modifiers do we allow in GENDIRDEPS_FILTER
259331246SsjgGENDIRDEPS_FILTER_MASK += @CMNS
260331246SsjgDIRDEPS := ${DIRDEPS:${GENDIRDEPS_FILTER:UNno:M[${GENDIRDEPS_FILTER_MASK:O:u:ts}]*:ts:}:C,//+,/,g:O:u}
261246149Ssjg
262246149Ssjg.if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != ""
263249033Ssjg.info ${RELDIR}: M2D_OBJROOTS=${M2D_OBJROOTS}
264300313Ssjg.info ${RELDIR}: M2D_EXCLUDES=${M2D_EXCLUDES}
265246149Ssjg.info ${RELDIR}: dir_list='${dir_list}'
266255253Ssjg.info ${RELDIR}: dpadd_dir_list='${dpadd_dir_list}'
267246149Ssjg.info ${RELDIR}: dirdep_list='${dirdep_list}'
268246149Ssjg.info ${RELDIR}: qualdir_list='${qualdir_list}'
269246149Ssjg.info ${RELDIR}: SKIP_GENDIRDEPS='${SKIP_GENDIRDEPS}'
270246149Ssjg.info ${RELDIR}: GENDIRDEPS_FILTER='${GENDIRDEPS_FILTER}'
271246149Ssjg.info ${RELDIR}: FORCE_DPADD='${DPADD}'
272246149Ssjg.info ${RELDIR}: DIRDEPS='${DIRDEPS}'
273246149Ssjg.endif
274246149Ssjg
275246149Ssjg# SRC_DIRDEPS is for checkout logic
276246149Ssjgsrc_dirdep_list = \
277246149Ssjg	${dir_list:M${SRCTOP}/*:S,${SRCTOP}/,,}
278246149Ssjg
279246149SsjgSRC_DIRDEPS = \
280246149Ssjg	${src_dirdep_list:N${RELDIR}:N${RELDIR}/*:C,(/h)/.*,,}
281246149Ssjg
282268437SsjgSRC_DIRDEPS := ${SRC_DIRDEPS:${GENDIRDEPS_SRC_FILTER:UN/*:ts:}:C,//+,/,g:O:u}
283246149Ssjg
284246149Ssjg# if you want to capture SRC_DIRDEPS in .MAKE.DEPENDFILE put
285246149Ssjg# SRC_DIRDEPS_FILE = ${_DEPENDFILE} 
286246149Ssjg# in local.gendirdeps.mk
287246149Ssjg.if ${SRC_DIRDEPS_FILE:Uno:tl} != "no"
288246149SsjgECHO_SRC_DIRDEPS = echo 'SRC_DIRDEPS = \'; echo '${SRC_DIRDEPS:@d@	$d \\${.newline}@}'; echo;
289246149Ssjg
290246149Ssjg.if ${SRC_DIRDEPS_FILE:T} == ${_DEPENDFILE:T}
291246149Ssjg_include_src_dirdeps = ${ECHO_SRC_DIRDEPS}
292246149Ssjg.else
293246149Ssjgall: ${SRC_DIRDEPS_FILE}
294246149Ssjg.if !target(${SRC_DIRDEPS_FILE})
295246149Ssjg${SRC_DIRDEPS_FILE}: ${META_FILES} ${_this} ${META2DEPS}
296246149Ssjg	@(${ECHO_SRC_DIRDEPS}) > $@
297246149Ssjg.endif
298246149Ssjg.endif
299246149Ssjg.endif
300246149Ssjg_include_src_dirdeps ?= 
301246149Ssjg
302246149Ssjgall:	${_DEPENDFILE}
303246149Ssjg
304246149Ssjg# if this is going to exist it would be there by now
305246149Ssjg.if !exists(.depend)
306246149SsjgCAT_DEPEND = /dev/null
307246149Ssjg.endif
308246149SsjgCAT_DEPEND ?= .depend
309246149Ssjg
310246149Ssjg.if !empty(_DIRDEPS) && ${DIRDEPS} != ${_DIRDEPS}
311246149Ssjg# we may have changed a filter
312246149Ssjg.PHONY: ${_DEPENDFILE}
313246149Ssjg.endif
314246149Ssjg
315246149Ssjg# 'cat .depend' should suffice, but if we are mixing build modes
316246149Ssjg# .depend may contain things we don't want.
317246149Ssjg# The sed command at the end of the stream, allows for the filters
318246149Ssjg# to output _{VAR} tokens which we will turn into proper ${VAR} references.
319296637Ssjg${_DEPENDFILE}: .NOMETA ${CAT_DEPEND:M.depend} ${META_FILES:O:u:@m@${exists($m):?$m:}@} ${_this} ${META2DEPS}
320289842Ssjg	@(${GENDIRDEPS_HEADER} echo '# Autogenerated - do NOT edit!'; echo; \
321246149Ssjg	echo 'DIRDEPS = \'; \
322246149Ssjg	echo '${DIRDEPS:@d@	$d \\${.newline}@}'; echo; \
323246149Ssjg	${_include_src_dirdeps} \
324246149Ssjg	echo '.include <dirdeps.mk>'; \
325246149Ssjg	echo; \
326246149Ssjg	echo '.if $${DEP_RELDIR} == $${_DEP_RELDIR}'; \
327246149Ssjg	echo '# local dependencies - needed for -jN in clean tree'; \
328246149Ssjg	[ -s ${CAT_DEPEND} ] && { grep : ${CAT_DEPEND} | grep -v '[/\\]'; }; \
329246149Ssjg	echo '.endif' ) | sed 's,_\([{(]\),$$\1,g' > $@.new${.MAKE.PID}
330246149Ssjg	@${InstallNew}; InstallNew -s $@.new${.MAKE.PID}
331246149Ssjg
332246149Ssjg.endif				# meta2deps failed
333246149Ssjg.elif !empty(SUBDIR)
334246149Ssjg
335246149SsjgDIRDEPS := ${SUBDIR:S,^,${RELDIR}/,:O:u}
336246149Ssjg
337246149Ssjgall:	${_DEPENDFILE}
338246149Ssjg
339296637Ssjg${_DEPENDFILE}: .NOMETA ${MAKEFILE} ${_this}
340289842Ssjg	@(${GENDIRDEPS_HEADER} echo '# Autogenerated - do NOT edit!'; echo; \
341246149Ssjg	echo 'DIRDEPS = \'; \
342246149Ssjg	echo '${DIRDEPS:@d@	$d \\${.newline}@}'; echo; \
343246149Ssjg	echo '.include <dirdeps.mk>'; \
344246149Ssjg	echo ) | sed 's,_\([{(]\),$$\1,g' > $@.new
345246149Ssjg	@${InstallNew}; InstallNew $@.new
346246149Ssjg
347246149Ssjg.else
348246149Ssjg
349246149Ssjg# nothing to do
350246149Ssjgall ${_DEPENDFILE}:
351246149Ssjg
352246149Ssjg.endif
353246149Ssjg${_DEPENDFILE}: .PRECIOUS
354