gendirdeps.mk revision 300313
1300313Ssjg# $Id: gendirdeps.mk,v 1.32 2016/04/05 15:58:37 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
149246149Ssjg.if ${.MAKE.DEPENDFILE_PREFERENCE:U${.MAKE.DEPENDFILE}:M*.${MACHINE}} == ""
150246149Ssjg# meta2deps.py only groks objroot
151246149Ssjg# so we need to give it what it expects
152246149Ssjg# and tell it not to add machine qualifiers
153246149SsjgMETA2DEPS_ARGS += MACHINE=none
154246149Ssjg.endif
155246149Ssjg.if defined(SB_BACKING_SB) 
156249033SsjgMETA2DEPS_CMD += -S ${SB_BACKING_SB}/src 
157249033SsjgM2D_OBJROOTS += ${SB_BACKING_SB}/${SB_OBJPREFIX}
158246149Ssjg.endif
159246149Ssjg
160246149Ssjg# we are only interested in the dirs
161289842Ssjg# specifically those we read something from.
162246149Ssjg# we canonicalize them to keep things simple
163246149Ssjg# if we are using a split-fs sandbox, it gets a little messier.
164246149Ssjg_objtop := ${_OBJTOP:tA}
165246149Ssjgdir_list != cd ${_OBJDIR} && \
166246149Ssjg	${META2DEPS_CMD} MACHINE=${MACHINE} \
167246149Ssjg	SRCTOP=${SRCTOP} RELDIR=${RELDIR} CURDIR=${_CURDIR} \
168246149Ssjg	${META2DEPS_ARGS} \
169246149Ssjg	${META_FILES:O:u} | ${META2DEPS_FILTER} ${_skip_gendirdeps} \
170246149Ssjg	sed 's,//*$$,,;s,\.${HOST_TARGET}$$,.host,'
171246149Ssjg
172246149Ssjg.if ${dir_list:M*ERROR\:*} != ""
173246149Ssjg.warning ${dir_list:tW:C,.*(ERROR),\1,}
174246149Ssjg.warning Skipping ${_DEPENDFILE:S,${SRCTOP}/,,}
175246149Ssjg# we are not going to update anything
176246149Ssjg.else
177255253Ssjgdpadd_dir_list=
178246149Ssjg.if !empty(DPADD)
179246149Ssjg_nonlibs := ${DPADD:T:Nlib*:N*include}
180246149Ssjg.if !empty(_nonlibs)
181249033Ssjgddep_list =
182249033Ssjg.for f in ${_nonlibs:@x@${DPADD:M*/$x}@}
183249033Ssjg.if exists($f.dirdep)
184249033Ssjgddep_list += $f.dirdep
185249033Ssjg.elif exists(${f:H}.dirdep)
186249033Ssjgddep_list += ${f:H}.dirdep
187249033Ssjg.else
188249033Ssjgdir_list += ${f:H:tA}
189255253Ssjgdpadd_dir_list += ${f:H:tA}
190246149Ssjg.endif
191249033Ssjg.endfor
192249033Ssjg.if !empty(ddep_list)
193249033Ssjgddeps != cat ${ddep_list:O:u} | ${META2DEPS_FILTER} ${_skip_gendirdeps} \
194249033Ssjg        sed 's,//*$$,,;s,\.${HOST_TARGET}$$,.host,;s,\.${MACHINE}$$,,'
195249033Ssjg
196249033Ssjg.if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != ""
197249033Ssjg.info ${RELDIR}: raw_dir_list='${dir_list}'
198249033Ssjg.info ${RELDIR}: ddeps='${ddeps}'
199246149Ssjg.endif
200249033Ssjgdir_list += ${ddeps}
201249033Ssjg.endif
202249033Ssjg.endif
203249033Ssjg.endif
204246149Ssjg
205246149Ssjg# DIRDEPS represent things that had to have been built first
206246149Ssjg# so they should all be undir OBJTOP.
207246149Ssjg# Note that ${_OBJTOP}/bsd/include/machine will get reported 
208246149Ssjg# to us as $SRCTOP/bsd/sys/$MACHINE_ARCH/include meaning we
209246149Ssjg# will want to visit bsd/include
210246149Ssjg# so we add 
211246149Ssjg# ${"${dir_list:M*bsd/sys/${MACHINE_ARCH}/include}":?bsd/include:}
212246149Ssjg# to GENDIRDEPS_DIR_LIST_XTRAS
213255253Ssjg_objtops = ${OBJTOP} ${_OBJTOP} ${_objtop}
214249033Ssjg_objtops := ${_objtops:O:u}
215246149Ssjgdirdep_list = \
216249033Ssjg	${_objtops:@o@${dir_list:M$o*/*:C,$o[^/]*/,,}@} \
217246149Ssjg	${GENDIRDEPS_DIR_LIST_XTRAS}
218246149Ssjg
219249033Ssjg# sort longest first
220249033SsjgM2D_OBJROOTS := ${M2D_OBJROOTS:O:u:[-1..1]}
221249033Ssjg
222246149Ssjg# anything we use from an object dir other than ours
223246149Ssjg# needs to be qualified with its .<machine> suffix
224246149Ssjg# (we used the pseudo machine "host" for the HOST_TARGET).
225249033Ssjgskip_ql= ${SRCTOP}* ${_objtops:@o@$o*@}
226249033Ssjg.for o in ${M2D_OBJROOTS:${skip_ql:${M_ListToSkip}}}
227249033Ssjg# we need := so only skip_ql to this point applies
228255253Ssjgql.$o := ${dir_list:${skip_ql:${M_ListToSkip}}:M$o*/*/*:C,$o([^/]+)/(.*),\2.\1,:S,.${HOST_TARGET},.host,}
229255253Ssjgqualdir_list += ${ql.$o}
230255253Ssjg.if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != ""
231255253Ssjg.info ${RELDIR}: o=$o ${ql.$o qualdir_list:L:@v@$v=${$v}@}
232255253Ssjg.endif
233249033Ssjgskip_ql+= $o*
234249033Ssjg.endfor
235246149Ssjg
236246149Ssjgdirdep_list := ${dirdep_list:O:u}
237249033Ssjgqualdir_list := ${qualdir_list:N*.${MACHINE}:O:u}
238246149Ssjg
239246149SsjgDIRDEPS = \
240246149Ssjg	${dirdep_list:N${RELDIR}:N${RELDIR}/*} \
241246149Ssjg	${qualdir_list:N${RELDIR}.*:N${RELDIR}/*}
242246149Ssjg
243246149Ssjg# We only consider things below $RELDIR/ if they have a makefile.
244268437Ssjg# This is the same test that _DIRDEP_USE applies.
245246149Ssjg# We have do a double test with dirdep_list as it _may_ contain 
246246149Ssjg# qualified dirs - if we got anything from a stage dir.
247246149Ssjg# qualdir_list we know are all qualified.
248246149Ssjg# It would be nice do peform this check for all of DIRDEPS,
249246149Ssjg# but we cannot assume that all of the tree is present, 
250246149Ssjg# in fact we can only assume that RELDIR is.
251246149SsjgDIRDEPS += \
252246149Ssjg	${dirdep_list:M${RELDIR}/*:@d@${.MAKE.MAKEFILE_PREFERENCE:@m@${exists(${SRCTOP}/$d/$m):?$d:${exists(${SRCTOP}/${d:R}/$m):?$d:}}@}@} \
253246149Ssjg	${qualdir_list:M${RELDIR}/*:@d@${.MAKE.MAKEFILE_PREFERENCE:@m@${exists(${SRCTOP}/${d:R}/$m):?$d:}@}@}
254246149Ssjg
255268437SsjgDIRDEPS := ${DIRDEPS:${GENDIRDEPS_FILTER:UNno:ts:}:C,//+,/,g:O:u}
256246149Ssjg
257246149Ssjg.if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != ""
258249033Ssjg.info ${RELDIR}: M2D_OBJROOTS=${M2D_OBJROOTS}
259300313Ssjg.info ${RELDIR}: M2D_EXCLUDES=${M2D_EXCLUDES}
260246149Ssjg.info ${RELDIR}: dir_list='${dir_list}'
261255253Ssjg.info ${RELDIR}: dpadd_dir_list='${dpadd_dir_list}'
262246149Ssjg.info ${RELDIR}: dirdep_list='${dirdep_list}'
263246149Ssjg.info ${RELDIR}: qualdir_list='${qualdir_list}'
264246149Ssjg.info ${RELDIR}: SKIP_GENDIRDEPS='${SKIP_GENDIRDEPS}'
265246149Ssjg.info ${RELDIR}: GENDIRDEPS_FILTER='${GENDIRDEPS_FILTER}'
266246149Ssjg.info ${RELDIR}: FORCE_DPADD='${DPADD}'
267246149Ssjg.info ${RELDIR}: DIRDEPS='${DIRDEPS}'
268246149Ssjg.endif
269246149Ssjg
270246149Ssjg# SRC_DIRDEPS is for checkout logic
271246149Ssjgsrc_dirdep_list = \
272246149Ssjg	${dir_list:M${SRCTOP}/*:S,${SRCTOP}/,,}
273246149Ssjg
274246149SsjgSRC_DIRDEPS = \
275246149Ssjg	${src_dirdep_list:N${RELDIR}:N${RELDIR}/*:C,(/h)/.*,,}
276246149Ssjg
277268437SsjgSRC_DIRDEPS := ${SRC_DIRDEPS:${GENDIRDEPS_SRC_FILTER:UN/*:ts:}:C,//+,/,g:O:u}
278246149Ssjg
279246149Ssjg# if you want to capture SRC_DIRDEPS in .MAKE.DEPENDFILE put
280246149Ssjg# SRC_DIRDEPS_FILE = ${_DEPENDFILE} 
281246149Ssjg# in local.gendirdeps.mk
282246149Ssjg.if ${SRC_DIRDEPS_FILE:Uno:tl} != "no"
283246149SsjgECHO_SRC_DIRDEPS = echo 'SRC_DIRDEPS = \'; echo '${SRC_DIRDEPS:@d@	$d \\${.newline}@}'; echo;
284246149Ssjg
285246149Ssjg.if ${SRC_DIRDEPS_FILE:T} == ${_DEPENDFILE:T}
286246149Ssjg_include_src_dirdeps = ${ECHO_SRC_DIRDEPS}
287246149Ssjg.else
288246149Ssjgall: ${SRC_DIRDEPS_FILE}
289246149Ssjg.if !target(${SRC_DIRDEPS_FILE})
290246149Ssjg${SRC_DIRDEPS_FILE}: ${META_FILES} ${_this} ${META2DEPS}
291246149Ssjg	@(${ECHO_SRC_DIRDEPS}) > $@
292246149Ssjg.endif
293246149Ssjg.endif
294246149Ssjg.endif
295246149Ssjg_include_src_dirdeps ?= 
296246149Ssjg
297246149Ssjgall:	${_DEPENDFILE}
298246149Ssjg
299246149Ssjg# if this is going to exist it would be there by now
300246149Ssjg.if !exists(.depend)
301246149SsjgCAT_DEPEND = /dev/null
302246149Ssjg.endif
303246149SsjgCAT_DEPEND ?= .depend
304246149Ssjg
305246149Ssjg.if !empty(_DIRDEPS) && ${DIRDEPS} != ${_DIRDEPS}
306246149Ssjg# we may have changed a filter
307246149Ssjg.PHONY: ${_DEPENDFILE}
308246149Ssjg.endif
309246149Ssjg
310246149Ssjg# 'cat .depend' should suffice, but if we are mixing build modes
311246149Ssjg# .depend may contain things we don't want.
312246149Ssjg# The sed command at the end of the stream, allows for the filters
313246149Ssjg# to output _{VAR} tokens which we will turn into proper ${VAR} references.
314296637Ssjg${_DEPENDFILE}: .NOMETA ${CAT_DEPEND:M.depend} ${META_FILES:O:u:@m@${exists($m):?$m:}@} ${_this} ${META2DEPS}
315289842Ssjg	@(${GENDIRDEPS_HEADER} echo '# Autogenerated - do NOT edit!'; echo; \
316246149Ssjg	echo 'DIRDEPS = \'; \
317246149Ssjg	echo '${DIRDEPS:@d@	$d \\${.newline}@}'; echo; \
318246149Ssjg	${_include_src_dirdeps} \
319246149Ssjg	echo '.include <dirdeps.mk>'; \
320246149Ssjg	echo; \
321246149Ssjg	echo '.if $${DEP_RELDIR} == $${_DEP_RELDIR}'; \
322246149Ssjg	echo '# local dependencies - needed for -jN in clean tree'; \
323246149Ssjg	[ -s ${CAT_DEPEND} ] && { grep : ${CAT_DEPEND} | grep -v '[/\\]'; }; \
324246149Ssjg	echo '.endif' ) | sed 's,_\([{(]\),$$\1,g' > $@.new${.MAKE.PID}
325246149Ssjg	@${InstallNew}; InstallNew -s $@.new${.MAKE.PID}
326246149Ssjg
327246149Ssjg.endif				# meta2deps failed
328246149Ssjg.elif !empty(SUBDIR)
329246149Ssjg
330246149SsjgDIRDEPS := ${SUBDIR:S,^,${RELDIR}/,:O:u}
331246149Ssjg
332246149Ssjgall:	${_DEPENDFILE}
333246149Ssjg
334296637Ssjg${_DEPENDFILE}: .NOMETA ${MAKEFILE} ${_this}
335289842Ssjg	@(${GENDIRDEPS_HEADER} echo '# Autogenerated - do NOT edit!'; echo; \
336246149Ssjg	echo 'DIRDEPS = \'; \
337246149Ssjg	echo '${DIRDEPS:@d@	$d \\${.newline}@}'; echo; \
338246149Ssjg	echo '.include <dirdeps.mk>'; \
339246149Ssjg	echo ) | sed 's,_\([{(]\),$$\1,g' > $@.new
340246149Ssjg	@${InstallNew}; InstallNew $@.new
341246149Ssjg
342246149Ssjg.else
343246149Ssjg
344246149Ssjg# nothing to do
345246149Ssjgall ${_DEPENDFILE}:
346246149Ssjg
347246149Ssjg.endif
348246149Ssjg${_DEPENDFILE}: .PRECIOUS
349