1255253Ssjg# $Id: gendirdeps.mk,v 1.23 2013/09/04 17:49:20 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
85246149Ssjg.-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
96246149Ssjg# this (*should* be set in meta.sys.mk) 
97246149Ssjg# is the script that extracts what we want.
98246149SsjgMETA2DEPS ?= ${.PARSEDIR}/meta2deps.sh
99246149SsjgMETA2DEPS := ${META2DEPS}
100246149Ssjg
101246149Ssjg.if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != "" && ${DEBUG_GENDIRDEPS:Uno:Mmeta2d*} != ""
102246149Ssjg_time = time
103246149Ssjg_sh_x = sh -x
104246149Ssjg_py_d = -ddd
105246149Ssjg.else
106246149Ssjg_time =
107246149Ssjg_sh_x =
108246149Ssjg_py_d =
109246149Ssjg.endif
110246149Ssjg
111246149Ssjg.if ${META2DEPS:E} == "py"
112246149Ssjg# we can afford to do this all the time.
113246149SsjgDPDEPS ?= no
114250837SsjgMETA2DEPS_CMD = ${_time} ${PYTHON} ${META2DEPS} ${_py_d} 
115246149Ssjg.if ${DPDEPS:tl} != "no"
116246149SsjgMETA2DEPS_CMD += -D ${DPDEPS}
117246149Ssjg.endif
118250837SsjgMETA2DEPS_FILTER = sed 's,^src:,${SRCTOP}/,;s,^\([^/]\),${OBJTOP}/\1,' |
119250837Ssjg.elif ${META2DEPS:E} == "sh"
120250837SsjgMETA2DEPS_CMD = ${_time} ${_sh_x} ${META2DEPS} OBJTOP=${_OBJTOP}
121250837Ssjg.else
122250837SsjgMETA2DEPS_CMD ?= ${META2DEPS}
123250837Ssjg.endif
124249033Ssjg
125250837Ssjg.if ${TARGET_OBJ_SPEC:U${MACHINE}} != ${MACHINE}
126250837SsjgMETA2DEPS_CMD += -T ${TARGET_OBJ_SPEC}
127250837Ssjg.endif
128250837SsjgMETA2DEPS_CMD += \
129250837Ssjg	-R ${RELDIR} -H ${HOST_TARGET} \
130250837Ssjg	${M2D_OBJROOTS:O:u:@o@-O $o@}
131250837Ssjg
132250837Ssjg
133249033SsjgM2D_OBJROOTS += ${OBJTOP} ${_OBJROOT} ${_objroot}
134249033Ssjg.if defined(SB_OBJROOT)
135249033SsjgM2D_OBJROOTS += ${SB_OBJROOT}
136249033Ssjg.endif
137246149Ssjg.if ${.MAKE.DEPENDFILE_PREFERENCE:U${.MAKE.DEPENDFILE}:M*.${MACHINE}} == ""
138246149Ssjg# meta2deps.py only groks objroot
139246149Ssjg# so we need to give it what it expects
140246149Ssjg# and tell it not to add machine qualifiers
141246149SsjgMETA2DEPS_ARGS += MACHINE=none
142246149Ssjg.endif
143246149Ssjg.if defined(SB_BACKING_SB) 
144249033SsjgMETA2DEPS_CMD += -S ${SB_BACKING_SB}/src 
145249033SsjgM2D_OBJROOTS += ${SB_BACKING_SB}/${SB_OBJPREFIX}
146246149Ssjg.endif
147246149Ssjg
148246149Ssjg# we are only interested in the dirs
149246149Ssjg# sepecifically those we read something from.
150246149Ssjg# we canonicalize them to keep things simple
151246149Ssjg# if we are using a split-fs sandbox, it gets a little messier.
152246149Ssjg_objtop := ${_OBJTOP:tA}
153246149Ssjgdir_list != cd ${_OBJDIR} && \
154246149Ssjg	${META2DEPS_CMD} MACHINE=${MACHINE} \
155246149Ssjg	SRCTOP=${SRCTOP} RELDIR=${RELDIR} CURDIR=${_CURDIR} \
156246149Ssjg	${META2DEPS_ARGS} \
157246149Ssjg	${META_FILES:O:u} | ${META2DEPS_FILTER} ${_skip_gendirdeps} \
158246149Ssjg	sed 's,//*$$,,;s,\.${HOST_TARGET}$$,.host,'
159246149Ssjg
160246149Ssjg.if ${dir_list:M*ERROR\:*} != ""
161246149Ssjg.warning ${dir_list:tW:C,.*(ERROR),\1,}
162246149Ssjg.warning Skipping ${_DEPENDFILE:S,${SRCTOP}/,,}
163246149Ssjg# we are not going to update anything
164246149Ssjg.else
165255253Ssjgdpadd_dir_list=
166246149Ssjg.if !empty(DPADD)
167246149Ssjg_nonlibs := ${DPADD:T:Nlib*:N*include}
168246149Ssjg.if !empty(_nonlibs)
169249033Ssjgddep_list =
170249033Ssjg.for f in ${_nonlibs:@x@${DPADD:M*/$x}@}
171249033Ssjg.if exists($f.dirdep)
172249033Ssjgddep_list += $f.dirdep
173249033Ssjg.elif exists(${f:H}.dirdep)
174249033Ssjgddep_list += ${f:H}.dirdep
175249033Ssjg.else
176249033Ssjgdir_list += ${f:H:tA}
177255253Ssjgdpadd_dir_list += ${f:H:tA}
178246149Ssjg.endif
179249033Ssjg.endfor
180249033Ssjg.if !empty(ddep_list)
181249033Ssjgddeps != cat ${ddep_list:O:u} | ${META2DEPS_FILTER} ${_skip_gendirdeps} \
182249033Ssjg        sed 's,//*$$,,;s,\.${HOST_TARGET}$$,.host,;s,\.${MACHINE}$$,,'
183249033Ssjg
184249033Ssjg.if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != ""
185249033Ssjg.info ${RELDIR}: raw_dir_list='${dir_list}'
186249033Ssjg.info ${RELDIR}: ddeps='${ddeps}'
187246149Ssjg.endif
188249033Ssjgdir_list += ${ddeps}
189249033Ssjg.endif
190249033Ssjg.endif
191249033Ssjg.endif
192246149Ssjg
193246149Ssjg# DIRDEPS represent things that had to have been built first
194246149Ssjg# so they should all be undir OBJTOP.
195246149Ssjg# Note that ${_OBJTOP}/bsd/include/machine will get reported 
196246149Ssjg# to us as $SRCTOP/bsd/sys/$MACHINE_ARCH/include meaning we
197246149Ssjg# will want to visit bsd/include
198246149Ssjg# so we add 
199246149Ssjg# ${"${dir_list:M*bsd/sys/${MACHINE_ARCH}/include}":?bsd/include:}
200246149Ssjg# to GENDIRDEPS_DIR_LIST_XTRAS
201255253Ssjg_objtops = ${OBJTOP} ${_OBJTOP} ${_objtop}
202249033Ssjg_objtops := ${_objtops:O:u}
203246149Ssjgdirdep_list = \
204249033Ssjg	${_objtops:@o@${dir_list:M$o*/*:C,$o[^/]*/,,}@} \
205246149Ssjg	${GENDIRDEPS_DIR_LIST_XTRAS}
206246149Ssjg
207249033Ssjg# sort longest first
208249033SsjgM2D_OBJROOTS := ${M2D_OBJROOTS:O:u:[-1..1]}
209249033Ssjg
210246149Ssjg# anything we use from an object dir other than ours
211246149Ssjg# needs to be qualified with its .<machine> suffix
212246149Ssjg# (we used the pseudo machine "host" for the HOST_TARGET).
213249033Ssjgskip_ql= ${SRCTOP}* ${_objtops:@o@$o*@}
214249033Ssjg.for o in ${M2D_OBJROOTS:${skip_ql:${M_ListToSkip}}}
215249033Ssjg# we need := so only skip_ql to this point applies
216255253Ssjgql.$o := ${dir_list:${skip_ql:${M_ListToSkip}}:M$o*/*/*:C,$o([^/]+)/(.*),\2.\1,:S,.${HOST_TARGET},.host,}
217255253Ssjgqualdir_list += ${ql.$o}
218255253Ssjg.if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != ""
219255253Ssjg.info ${RELDIR}: o=$o ${ql.$o qualdir_list:L:@v@$v=${$v}@}
220255253Ssjg.endif
221249033Ssjgskip_ql+= $o*
222249033Ssjg.endfor
223246149Ssjg
224246149Ssjgdirdep_list := ${dirdep_list:O:u}
225249033Ssjgqualdir_list := ${qualdir_list:N*.${MACHINE}:O:u}
226246149Ssjg
227246149SsjgDIRDEPS = \
228246149Ssjg	${dirdep_list:N${RELDIR}:N${RELDIR}/*} \
229246149Ssjg	${qualdir_list:N${RELDIR}.*:N${RELDIR}/*}
230246149Ssjg
231246149Ssjg# We only consider things below $RELDIR/ if they have a makefile.
232246149Ssjg# This is the same test that _DIRDEPS_USE applies.
233246149Ssjg# We have do a double test with dirdep_list as it _may_ contain 
234246149Ssjg# qualified dirs - if we got anything from a stage dir.
235246149Ssjg# qualdir_list we know are all qualified.
236246149Ssjg# It would be nice do peform this check for all of DIRDEPS,
237246149Ssjg# but we cannot assume that all of the tree is present, 
238246149Ssjg# in fact we can only assume that RELDIR is.
239246149SsjgDIRDEPS += \
240246149Ssjg	${dirdep_list:M${RELDIR}/*:@d@${.MAKE.MAKEFILE_PREFERENCE:@m@${exists(${SRCTOP}/$d/$m):?$d:${exists(${SRCTOP}/${d:R}/$m):?$d:}}@}@} \
241246149Ssjg	${qualdir_list:M${RELDIR}/*:@d@${.MAKE.MAKEFILE_PREFERENCE:@m@${exists(${SRCTOP}/${d:R}/$m):?$d:}@}@}
242246149Ssjg
243246149SsjgDIRDEPS := ${DIRDEPS:${GENDIRDEPS_FILTER:UNno:ts:}:O:u}
244246149Ssjg
245246149Ssjg.if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != ""
246249033Ssjg.info ${RELDIR}: M2D_OBJROOTS=${M2D_OBJROOTS}
247246149Ssjg.info ${RELDIR}: dir_list='${dir_list}'
248255253Ssjg.info ${RELDIR}: dpadd_dir_list='${dpadd_dir_list}'
249246149Ssjg.info ${RELDIR}: dirdep_list='${dirdep_list}'
250246149Ssjg.info ${RELDIR}: qualdir_list='${qualdir_list}'
251246149Ssjg.info ${RELDIR}: SKIP_GENDIRDEPS='${SKIP_GENDIRDEPS}'
252246149Ssjg.info ${RELDIR}: GENDIRDEPS_FILTER='${GENDIRDEPS_FILTER}'
253246149Ssjg.info ${RELDIR}: FORCE_DPADD='${DPADD}'
254246149Ssjg.info ${RELDIR}: DIRDEPS='${DIRDEPS}'
255246149Ssjg.endif
256246149Ssjg
257246149Ssjg# SRC_DIRDEPS is for checkout logic
258246149Ssjgsrc_dirdep_list = \
259246149Ssjg	${dir_list:M${SRCTOP}/*:S,${SRCTOP}/,,}
260246149Ssjg
261246149SsjgSRC_DIRDEPS = \
262246149Ssjg	${src_dirdep_list:N${RELDIR}:N${RELDIR}/*:C,(/h)/.*,,}
263246149Ssjg
264246149SsjgSRC_DIRDEPS := ${SRC_DIRDEPS:${GENDIRDEPS_SRC_FILTER:UN/*:ts:}:O:u}
265246149Ssjg
266246149Ssjg# if you want to capture SRC_DIRDEPS in .MAKE.DEPENDFILE put
267246149Ssjg# SRC_DIRDEPS_FILE = ${_DEPENDFILE} 
268246149Ssjg# in local.gendirdeps.mk
269246149Ssjg.if ${SRC_DIRDEPS_FILE:Uno:tl} != "no"
270246149SsjgECHO_SRC_DIRDEPS = echo 'SRC_DIRDEPS = \'; echo '${SRC_DIRDEPS:@d@	$d \\${.newline}@}'; echo;
271246149Ssjg
272246149Ssjg.if ${SRC_DIRDEPS_FILE:T} == ${_DEPENDFILE:T}
273246149Ssjg_include_src_dirdeps = ${ECHO_SRC_DIRDEPS}
274246149Ssjg.else
275246149Ssjgall: ${SRC_DIRDEPS_FILE}
276246149Ssjg.if !target(${SRC_DIRDEPS_FILE})
277246149Ssjg${SRC_DIRDEPS_FILE}: ${META_FILES} ${_this} ${META2DEPS}
278246149Ssjg	@(${ECHO_SRC_DIRDEPS}) > $@
279246149Ssjg.endif
280246149Ssjg.endif
281246149Ssjg.endif
282246149Ssjg_include_src_dirdeps ?= 
283246149Ssjg
284246149Ssjgall:	${_DEPENDFILE}
285246149Ssjg
286246149Ssjg# if this is going to exist it would be there by now
287246149Ssjg.if !exists(.depend)
288246149SsjgCAT_DEPEND = /dev/null
289246149Ssjg.endif
290246149SsjgCAT_DEPEND ?= .depend
291246149Ssjg
292246149Ssjg.if !empty(_DIRDEPS) && ${DIRDEPS} != ${_DIRDEPS}
293246149Ssjg# we may have changed a filter
294246149Ssjg.PHONY: ${_DEPENDFILE}
295246149Ssjg.endif
296246149Ssjg
297246149Ssjg# 'cat .depend' should suffice, but if we are mixing build modes
298246149Ssjg# .depend may contain things we don't want.
299246149Ssjg# The sed command at the end of the stream, allows for the filters
300246149Ssjg# to output _{VAR} tokens which we will turn into proper ${VAR} references.
301246149Ssjg${_DEPENDFILE}: ${CAT_DEPEND:M.depend} ${META_FILES:O:u:@m@${exists($m):?$m:}@} ${_this} ${META2DEPS}
302246149Ssjg	@(echo '# Autogenerated - do NOT edit!'; echo; \
303246149Ssjg	echo 'DEP_RELDIR := $${_PARSEDIR:S,$${SRCTOP}/,,}'; echo; \
304246149Ssjg	echo 'DIRDEPS = \'; \
305246149Ssjg	echo '${DIRDEPS:@d@	$d \\${.newline}@}'; echo; \
306246149Ssjg	${_include_src_dirdeps} \
307246149Ssjg	echo '.include <dirdeps.mk>'; \
308246149Ssjg	echo; \
309246149Ssjg	echo '.if $${DEP_RELDIR} == $${_DEP_RELDIR}'; \
310246149Ssjg	echo '# local dependencies - needed for -jN in clean tree'; \
311246149Ssjg	[ -s ${CAT_DEPEND} ] && { grep : ${CAT_DEPEND} | grep -v '[/\\]'; }; \
312246149Ssjg	echo '.endif' ) | sed 's,_\([{(]\),$$\1,g' > $@.new${.MAKE.PID}
313246149Ssjg	@${InstallNew}; InstallNew -s $@.new${.MAKE.PID}
314246149Ssjg
315246149Ssjg.endif				# meta2deps failed
316246149Ssjg.elif !empty(SUBDIR)
317246149Ssjg
318246149SsjgDIRDEPS := ${SUBDIR:S,^,${RELDIR}/,:O:u}
319246149Ssjg
320246149Ssjgall:	${_DEPENDFILE}
321246149Ssjg
322246149Ssjg${_DEPENDFILE}: ${MAKEFILE} ${_this}
323246149Ssjg	@(echo '# Autogenerated - do NOT edit!'; echo; \
324246149Ssjg	echo 'DEP_RELDIR := $${_PARSEDIR:S,$${SRCTOP}/,,}'; echo; \
325246149Ssjg	echo 'DIRDEPS = \'; \
326246149Ssjg	echo '${DIRDEPS:@d@	$d \\${.newline}@}'; echo; \
327246149Ssjg	echo '.include <dirdeps.mk>'; \
328246149Ssjg	echo ) | sed 's,_\([{(]\),$$\1,g' > $@.new
329246149Ssjg	@${InstallNew}; InstallNew $@.new
330246149Ssjg
331246149Ssjg.else
332246149Ssjg
333246149Ssjg# nothing to do
334246149Ssjgall ${_DEPENDFILE}:
335246149Ssjg
336246149Ssjg.endif
337246149Ssjg${_DEPENDFILE}: .PRECIOUS
338