xref: /freebsd/share/mk/gendirdeps.mk (revision 01f18a0c)
1# $Id: gendirdeps.mk,v 1.21 2013/03/28 20:01:05 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# this (*should* be set in meta.sys.mk)
97# is the script that extracts what we want.
98META2DEPS ?= ${.PARSEDIR}/meta2deps.sh
99META2DEPS := ${META2DEPS}
100
101.if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != "" && ${DEBUG_GENDIRDEPS:Uno:Mmeta2d*} != ""
102_time = time
103_sh_x = sh -x
104_py_d = -ddd
105.else
106_time =
107_sh_x =
108_py_d =
109.endif
110
111.if ${META2DEPS:E} == "py"
112# we can afford to do this all the time.
113DPDEPS ?= no
114META2DEPS_CMD = ${_time} ${PYTHON} ${META2DEPS} ${_py_d} \
115	-R ${RELDIR} -H ${HOST_TARGET} \
116	${M2D_OBJROOTS:O:u:@o@-O $o@}
117
118.if ${TARGET_OBJ_SPEC:U${MACHINE}} != ${MACHINE}
119META2DEPS_CMD += -T ${TARGET_OBJ_SPEC}
120.endif
121.if ${DPDEPS:tl} != "no"
122META2DEPS_CMD += -D ${DPDEPS}
123.endif
124
125M2D_OBJROOTS += ${OBJTOP} ${_OBJROOT} ${_objroot}
126.if defined(SB_OBJROOT)
127M2D_OBJROOTS += ${SB_OBJROOT}
128.endif
129.if ${.MAKE.DEPENDFILE_PREFERENCE:U${.MAKE.DEPENDFILE}:M*.${MACHINE}} == ""
130# meta2deps.py only groks objroot
131# so we need to give it what it expects
132# and tell it not to add machine qualifiers
133META2DEPS_ARGS += MACHINE=none
134.endif
135.if defined(SB_BACKING_SB)
136META2DEPS_CMD += -S ${SB_BACKING_SB}/src
137M2D_OBJROOTS += ${SB_BACKING_SB}/${SB_OBJPREFIX}
138.endif
139META2DEPS_FILTER = sed 's,^src:,${SRCTOP}/,;s,^\([^/]\),${OBJTOP}/\1,' |
140.elif ${META2DEPS:E} == "sh"
141META2DEPS_CMD = ${_time} ${_sh_x} ${META2DEPS} \
142	OBJTOP=${_objtop} SB_OBJROOT=${_objroot}
143.else
144META2DEPS_CMD ?= ${META2DEPS}
145.endif
146
147# we are only interested in the dirs
148# sepecifically those we read something from.
149# we canonicalize them to keep things simple
150# if we are using a split-fs sandbox, it gets a little messier.
151_objtop := ${_OBJTOP:tA}
152dir_list != cd ${_OBJDIR} && \
153	${META2DEPS_CMD} MACHINE=${MACHINE} \
154	SRCTOP=${SRCTOP} RELDIR=${RELDIR} CURDIR=${_CURDIR} \
155	${META2DEPS_ARGS} \
156	${META_FILES:O:u} | ${META2DEPS_FILTER} ${_skip_gendirdeps} \
157	sed 's,//*$$,,;s,\.${HOST_TARGET}$$,.host,'
158
159.if ${dir_list:M*ERROR\:*} != ""
160.warning ${dir_list:tW:C,.*(ERROR),\1,}
161.warning Skipping ${_DEPENDFILE:S,${SRCTOP}/,,}
162# we are not going to update anything
163.else
164
165.if !empty(DPADD)
166_nonlibs := ${DPADD:T:Nlib*:N*include}
167.if !empty(_nonlibs)
168ddep_list =
169.for f in ${_nonlibs:@x@${DPADD:M*/$x}@}
170.if exists($f.dirdep)
171ddep_list += $f.dirdep
172.elif exists(${f:H}.dirdep)
173ddep_list += ${f:H}.dirdep
174.else
175dir_list += ${f:H:tA}
176.endif
177.endfor
178.if !empty(ddep_list)
179ddeps != cat ${ddep_list:O:u} | ${META2DEPS_FILTER} ${_skip_gendirdeps} \
180        sed 's,//*$$,,;s,\.${HOST_TARGET}$$,.host,;s,\.${MACHINE}$$,,'
181
182.if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != ""
183.info ${RELDIR}: raw_dir_list='${dir_list}'
184.info ${RELDIR}: ddeps='${ddeps}'
185.endif
186dir_list += ${ddeps}
187.endif
188.endif
189.endif
190
191# DIRDEPS represent things that had to have been built first
192# so they should all be undir OBJTOP.
193# Note that ${_OBJTOP}/bsd/include/machine will get reported
194# to us as $SRCTOP/bsd/sys/$MACHINE_ARCH/include meaning we
195# will want to visit bsd/include
196# so we add
197# ${"${dir_list:M*bsd/sys/${MACHINE_ARCH}/include}":?bsd/include:}
198# to GENDIRDEPS_DIR_LIST_XTRAS
199_objtops = ${OBJTOP} ${_OBJTOP} ${_obtop}
200_objtops := ${_objtops:O:u}
201dirdep_list = \
202	${_objtops:@o@${dir_list:M$o*/*:C,$o[^/]*/,,}@} \
203	${GENDIRDEPS_DIR_LIST_XTRAS}
204
205# sort longest first
206M2D_OBJROOTS := ${M2D_OBJROOTS:O:u:[-1..1]}
207
208# anything we use from an object dir other than ours
209# needs to be qualified with its .<machine> suffix
210# (we used the pseudo machine "host" for the HOST_TARGET).
211skip_ql= ${SRCTOP}* ${_objtops:@o@$o*@}
212.for o in ${M2D_OBJROOTS:${skip_ql:${M_ListToSkip}}}
213# we need := so only skip_ql to this point applies
214ql :=	${dir_list:${skip_ql:${M_ListToSkip}}:M$o*/*/*:C,$o([^/]+)/(.*),\2.\1,:S,.${HOST_TARGET},.host,}
215qualdir_list += ${ql}
216skip_ql+= $o*
217.endfor
218
219dirdep_list := ${dirdep_list:O:u}
220qualdir_list := ${qualdir_list:N*.${MACHINE}:O:u}
221
222DIRDEPS = \
223	${dirdep_list:N${RELDIR}:N${RELDIR}/*} \
224	${qualdir_list:N${RELDIR}.*:N${RELDIR}/*}
225
226# We only consider things below $RELDIR/ if they have a makefile.
227# This is the same test that _DIRDEPS_USE applies.
228# We have do a double test with dirdep_list as it _may_ contain
229# qualified dirs - if we got anything from a stage dir.
230# qualdir_list we know are all qualified.
231# It would be nice do peform this check for all of DIRDEPS,
232# but we cannot assume that all of the tree is present,
233# in fact we can only assume that RELDIR is.
234DIRDEPS += \
235	${dirdep_list:M${RELDIR}/*:@d@${.MAKE.MAKEFILE_PREFERENCE:@m@${exists(${SRCTOP}/$d/$m):?$d:${exists(${SRCTOP}/${d:R}/$m):?$d:}}@}@} \
236	${qualdir_list:M${RELDIR}/*:@d@${.MAKE.MAKEFILE_PREFERENCE:@m@${exists(${SRCTOP}/${d:R}/$m):?$d:}@}@}
237
238DIRDEPS := ${DIRDEPS:${GENDIRDEPS_FILTER:UNno:ts:}:O:u}
239
240.if ${DEBUG_GENDIRDEPS:Uno:@x@${RELDIR:M$x}@} != ""
241.info ${RELDIR}: M2D_OBJROOTS=${M2D_OBJROOTS}
242.info ${RELDIR}: dir_list='${dir_list}'
243.info ${RELDIR}: dirdep_list='${dirdep_list}'
244.info ${RELDIR}: qualdir_list='${qualdir_list}'
245.info ${RELDIR}: SKIP_GENDIRDEPS='${SKIP_GENDIRDEPS}'
246.info ${RELDIR}: GENDIRDEPS_FILTER='${GENDIRDEPS_FILTER}'
247.info ${RELDIR}: FORCE_DPADD='${DPADD}'
248.info ${RELDIR}: DIRDEPS='${DIRDEPS}'
249.endif
250
251# SRC_DIRDEPS is for checkout logic
252src_dirdep_list = \
253	${dir_list:M${SRCTOP}/*:S,${SRCTOP}/,,}
254
255SRC_DIRDEPS = \
256	${src_dirdep_list:N${RELDIR}:N${RELDIR}/*:C,(/h)/.*,,}
257
258SRC_DIRDEPS := ${SRC_DIRDEPS:${GENDIRDEPS_SRC_FILTER:UN/*:ts:}:O:u}
259
260# if you want to capture SRC_DIRDEPS in .MAKE.DEPENDFILE put
261# SRC_DIRDEPS_FILE = ${_DEPENDFILE}
262# in local.gendirdeps.mk
263.if ${SRC_DIRDEPS_FILE:Uno:tl} != "no"
264ECHO_SRC_DIRDEPS = echo 'SRC_DIRDEPS = \'; echo '${SRC_DIRDEPS:@d@	$d \\${.newline}@}'; echo;
265
266.if ${SRC_DIRDEPS_FILE:T} == ${_DEPENDFILE:T}
267_include_src_dirdeps = ${ECHO_SRC_DIRDEPS}
268.else
269all: ${SRC_DIRDEPS_FILE}
270.if !target(${SRC_DIRDEPS_FILE})
271${SRC_DIRDEPS_FILE}: ${META_FILES} ${_this} ${META2DEPS}
272	@(${ECHO_SRC_DIRDEPS}) > $@
273.endif
274.endif
275.endif
276_include_src_dirdeps ?=
277
278all:	${_DEPENDFILE}
279
280# if this is going to exist it would be there by now
281.if !exists(.depend)
282CAT_DEPEND = /dev/null
283.endif
284CAT_DEPEND ?= .depend
285
286.if !empty(_DIRDEPS) && ${DIRDEPS} != ${_DIRDEPS}
287# we may have changed a filter
288.PHONY: ${_DEPENDFILE}
289.endif
290
291# 'cat .depend' should suffice, but if we are mixing build modes
292# .depend may contain things we don't want.
293# The sed command at the end of the stream, allows for the filters
294# to output _{VAR} tokens which we will turn into proper ${VAR} references.
295${_DEPENDFILE}: ${CAT_DEPEND:M.depend} ${META_FILES:O:u:@m@${exists($m):?$m:}@} ${_this} ${META2DEPS}
296	@(echo '# Autogenerated - do NOT edit!'; echo; \
297	echo 'DEP_RELDIR := $${_PARSEDIR:S,$${SRCTOP}/,,}'; echo; \
298	echo 'DIRDEPS = \'; \
299	echo '${DIRDEPS:@d@	$d \\${.newline}@}'; echo; \
300	${_include_src_dirdeps} \
301	echo '.include <dirdeps.mk>'; \
302	echo; \
303	echo '.if $${DEP_RELDIR} == $${_DEP_RELDIR}'; \
304	echo '# local dependencies - needed for -jN in clean tree'; \
305	[ -s ${CAT_DEPEND} ] && { grep : ${CAT_DEPEND} | grep -v '[/\\]'; }; \
306	echo '.endif' ) | sed 's,_\([{(]\),$$\1,g' > $@.new${.MAKE.PID}
307	@${InstallNew}; InstallNew -s $@.new${.MAKE.PID}
308
309.endif				# meta2deps failed
310.elif !empty(SUBDIR)
311
312DIRDEPS := ${SUBDIR:S,^,${RELDIR}/,:O:u}
313
314all:	${_DEPENDFILE}
315
316${_DEPENDFILE}: ${MAKEFILE} ${_this}
317	@(echo '# Autogenerated - do NOT edit!'; echo; \
318	echo 'DEP_RELDIR := $${_PARSEDIR:S,$${SRCTOP}/,,}'; echo; \
319	echo 'DIRDEPS = \'; \
320	echo '${DIRDEPS:@d@	$d \\${.newline}@}'; echo; \
321	echo '.include <dirdeps.mk>'; \
322	echo ) | sed 's,_\([{(]\),$$\1,g' > $@.new
323	@${InstallNew}; InstallNew $@.new
324
325.else
326
327# nothing to do
328all ${_DEPENDFILE}:
329
330.endif
331${_DEPENDFILE}: .PRECIOUS
332