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