xref: /freebsd/contrib/bmake/mk/meta.autodep.mk (revision 114350b9de8403b9e43bbad0d1534455a077d999)
1# $Id: meta.autodep.mk,v 1.37 2015/06/16 06:29:17 sjg Exp $
2
3#
4#	@(#) Copyright (c) 2010, Simon J. Gerraty
5#
6#	This file is provided in the hope that it will
7#	be of use.  There is absolutely NO WARRANTY.
8#	Permission to copy, redistribute or otherwise
9#	use this file is hereby granted provided that
10#	the above copyright notice and this notice are
11#	left intact.
12#
13#	Please send copies of changes and bug-fixes to:
14#	sjg@crufty.net
15#
16
17_this ?= ${.PARSEFILE}
18.if !target(__${_this}__)
19__${_this}__: .NOTMAIN
20
21.-include "local.autodep.mk"
22
23.if defined(SRCS)
24# it would be nice to be able to query .SUFFIXES
25OBJ_EXTENSIONS+= .o .po .lo .So
26
27# explicit dependencies help short-circuit .SUFFIX searches
28SRCS_DEP_FILTER+= N*.[hly]
29.for s in ${SRCS:${SRCS_DEP_FILTER:O:u:ts:}}
30.for e in ${OBJ_EXTENSIONS:O:u}
31.if !target(${s:T:R}$e)
32${s:T:R}$e: $s
33.endif
34.endfor
35.endfor
36.endif
37
38.if make(gendirdeps)
39# you are supposed to know what you are doing!
40UPDATE_DEPENDFILE = yes
41.elif !empty(.TARGETS) && !make(all)
42# do not update the *depend* files
43# unless we are building the entire directory or the default target.
44# NO means don't update .depend - or Makefile.depend*
45# no means update .depend but not Makefile.depend*
46UPDATE_DEPENDFILE = NO
47.elif ${.MAKEFLAGS:M-k} != ""
48# it is a bad idea to update anything
49UPDATE_DEPENDFILE = NO
50.endif
51
52_CURDIR ?= ${.CURDIR}
53_DEPENDFILE := ${_CURDIR}/${.MAKE.DEPENDFILE:T}
54
55.if ${.MAKE.LEVEL} == 0
56.if ${BUILD_AT_LEVEL0:Uyes:tl} == "no"
57UPDATE_DEPENDFILE = NO
58.endif
59.endif
60.if !exists(${_DEPENDFILE})
61_bootstrap_dirdeps = yes
62.endif
63_bootstrap_dirdeps ?= no
64UPDATE_DEPENDFILE ?= yes
65
66.if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != ""
67.info ${_DEPENDFILE:S,${SRCTOP}/,,} update=${UPDATE_DEPENDFILE}
68.endif
69
70.if !empty(XMAKE_META_FILE)
71.if exists(${.OBJDIR}/${XMAKE_META_FILE})
72# we cannot get accurate dependencies from an update build
73UPDATE_DEPENDFILE = NO
74.else
75META_XTRAS += ${XMAKE_META_FILE}
76.endif
77.endif
78
79.if ${_bootstrap_dirdeps} == "yes" || exists(${_DEPENDFILE})
80# if it isn't supposed to be touched by us the Makefile should have
81# UPDATE_DEPENDFILE = no
82WANT_UPDATE_DEPENDFILE ?= yes
83.endif
84
85.if ${WANT_UPDATE_DEPENDFILE:Uno:tl} != "no"
86.if ${.MAKE.MODE:Mmeta*} == "" || ${.MAKE.MODE:M*read*} != ""
87UPDATE_DEPENDFILE = no
88.endif
89
90.if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != ""
91.info ${_DEPENDFILE:S,${SRCTOP}/,,} update=${UPDATE_DEPENDFILE}
92.endif
93
94.if ${UPDATE_DEPENDFILE:tl} == "yes"
95# sometimes we want .meta files generated to aid debugging/error detection
96# but do not want to consider them for dependencies
97# for example the result of running configure
98# just make sure this is not empty
99META_FILE_FILTER ?= N.meta
100# never consider these
101META_FILE_FILTER += Ndirdeps.cache*
102
103.if !empty(DPADD)
104# if we have any non-libs in DPADD,
105# they probably need to be paid attention to
106.if !empty(DPLIBS)
107FORCE_DPADD = ${DPADD:${DPLIBS:${M_ListToSkip}}:${DPADD_LAST:${M_ListToSkip}}}
108.else
109_nonlibs := ${DPADD:T:Nlib*:N*include}
110.if !empty(_nonlibs)
111FORCE_DPADD += ${_nonlibs:@x@${DPADD:M*/$x}@}
112.endif
113.endif
114.endif
115
116.if !make(gendirdeps)
117.END:	gendirdeps
118.endif
119
120# if we don't have OBJS, then .depend isn't useful
121.if !target(.depend) && (!empty(OBJS) || ${.ALLTARGETS:M*.o} != "")
122# some makefiles and/or targets contain
123# circular dependencies if you dig too deep
124# (as meta mode is apt to do)
125# so we provide a means of suppressing them.
126# the input to the loop below is target: dependency
127# with just one dependency per line.
128# Also some targets are not really local, or use random names.
129# Use local.autodep.mk to provide local additions!
130SUPPRESS_DEPEND += \
131	${SB:S,/,_,g}* \
132	*:y.tab.c \
133	*.c:*.c \
134	*.h:*.h
135
136.NOPATH:	.depend
137# we use ${.MAKE.META.CREATED} to trigger an update but
138# we process using ${.MAKE.META.FILES}
139# the double $$ defers initial evaluation
140# if necessary, we fake .po dependencies, just so the result
141# in Makefile.depend* is stable
142# The current objdir may be referred to in various ways
143OBJDIR_REFS += ${.OBJDIR} ${.OBJDIR:tA} ${_OBJDIR} ${RELOBJTOP}/${RELDIR}
144_depend = .depend
145# it would be nice to be able to get .SUFFIXES as ${.SUFFIXES}
146# we actually only care about the .SUFFIXES of files that might be
147# generated by tools like yacc.
148DEPEND_SUFFIXES += .c .h .cpp .hpp .cxx .hxx .cc .hh
149.depend: .NOMETA $${.MAKE.META.CREATED} ${_this}
150	@echo "Updating $@: ${.OODATE:T:[1..8]}"
151	@egrep -i '^R .*\.(${DEPEND_SUFFIXES:tl:O:u:S,^.,,:ts|})$$' /dev/null ${.MAKE.META.FILES:T:O:u:${META_FILE_FILTER:ts:}:M*o.meta} | \
152	sed -e 's, \./, ,${OBJDIR_REFS:O:u:@d@;s, $d/, ,@};/\//d' \
153		-e 's,^\([^/][^/]*\).meta...[0-9]* ,\1: ,' | \
154	sort -u | \
155	while read t d; do \
156		case "$$d:" in $$t) continue;; esac; \
157		case "$$t$$d" in ${SUPPRESS_DEPEND:U.:O:u:ts|}) continue;; esac; \
158		echo $$t $$d; \
159	done > $@.${.MAKE.PID}
160	@case "${.MAKE.META.FILES:T:M*.po.*}" in \
161	*.po.*) mv $@.${.MAKE.PID} $@;; \
162	*) { cat $@.${.MAKE.PID}; \
163	sed 's,\.So:,.o:,;s,\.o:,.po:,' $@.${.MAKE.PID}; } | sort -u > $@; \
164	rm -f $@.${.MAKE.PID};; \
165	esac
166.else
167# make sure this exists
168.depend:
169# do _not_ assume that .depend is in any fit state for us to use
170CAT_DEPEND = /dev/null
171.if ${.MAKE.LEVEL} > 0
172.export CAT_DEPEND
173.endif
174_depend =
175.endif
176
177.if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != ""
178.info ${_DEPENDFILE:S,${SRCTOP}/,,} _depend=${_depend}
179.endif
180
181.if ${UPDATE_DEPENDFILE} == "yes"
182gendirdeps:	${_DEPENDFILE}
183.endif
184
185.if !target(${_DEPENDFILE})
186.if ${_bootstrap_dirdeps} == "yes"
187# We are boot-strapping a new directory
188# Use DPADD to seed DIRDEPS
189.if !empty(DPADD)
190# anything which matches ${_OBJROOT}* but not ${_OBJTOP}*
191# needs to be qualified in DIRDEPS
192# The pseudo machine "host" is used for HOST_TARGET
193DIRDEPS = \
194	${DPADD:M${_OBJTOP}*:H:C,${_OBJTOP}[^/]*/,,:N.:O:u} \
195	${DPADD:M${_OBJROOT}*:N${_OBJTOP}*:H:S,${_OBJROOT},,:C,^([^/]+)/(.*),\2.\1,:S,${HOST_TARGET}$,host,:N.*:O:u}
196
197.endif
198.endif
199
200_gendirdeps_mutex =
201.if defined(NEED_GENDIRDEPS_MUTEX)
202# If a src dir gets built with multiple object dirs,
203# we need a mutex.  Obviously, this is best avoided.
204# Note if .MAKE.DEPENDFILE is common for all ${MACHINE}
205# you either need to mutex, or ensure only one machine builds at a time!
206# lockf is an example of a suitable tool
207LOCKF ?= /usr/bin/lockf
208.if exists(${LOCKF})
209GENDIRDEPS_MUTEXER ?= ${LOCKF} -k
210.endif
211.if empty(GENDIRDEPS_MUTEXER)
212.error NEED_GENDIRDEPS_MUTEX defined, but GENDIRDEPS_MUTEXER not set
213.else
214_gendirdeps_mutex = ${GENDIRDEPS_MUTEXER} ${GENDIRDEPS_MUTEX:U${_CURDIR}/Makefile}
215.endif
216.endif
217
218# If we have META_XTRAS we most likely did not create them
219# but we need to behave as if we did.
220# Avoid adding glob patterns to .MAKE.META.CREATED though.
221.MAKE.META.CREATED += ${META_XTRAS:N*\**:O:u}
222
223.if make(gendirdeps)
224META_FILES = *.meta
225.elif ${OPTIMIZE_OBJECT_META_FILES:Uno:tl} == "no"
226META_FILES = ${.MAKE.META.FILES:T:N.depend*:O:u}
227.else
228# if we have 1000's of .o.meta, .So.meta etc we need only look at one set
229# it is left as an exercise for the reader to work out what this does
230META_FILES = ${.MAKE.META.FILES:T:N.depend*:N*o.meta:O:u} \
231	${.MAKE.META.FILES:T:M*.${.MAKE.META.FILES:M*o.meta:R:E:O:u:[1]}.meta:O:u}
232.endif
233
234.if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != ""
235.info ${_DEPENDFILE:S,${SRCTOP}/,,}: ${_depend} ${.PARSEDIR}/gendirdeps.mk ${META2DEPS} xtras=${META_XTRAS}
236.endif
237
238.if ${.MAKE.LEVEL} > 0 && !empty(GENDIRDEPS_FILTER)
239.export GENDIRDEPS_FILTER
240.endif
241
242# we might have .../ in MAKESYSPATH
243_makesyspath:= ${_PARSEDIR}
244${_DEPENDFILE}: ${_depend} ${.PARSEDIR}/gendirdeps.mk  ${META2DEPS} $${.MAKE.META.CREATED}
245	@echo Checking $@: ${.OODATE:T:[1..8]}
246	@(cd . && \
247	SKIP_GENDIRDEPS='${SKIP_GENDIRDEPS:O:u}' \
248	DPADD='${FORCE_DPADD:O:u}' ${_gendirdeps_mutex} \
249	MAKESYSPATH=${_makesyspath} \
250	${.MAKE} -f gendirdeps.mk RELDIR=${RELDIR} _DEPENDFILE=${_DEPENDFILE} \
251	META_FILES='${META_XTRAS:T:O:u} ${META_FILES:T:O:u:${META_FILE_FILTER:ts:}}')
252	@test -s $@ && touch $@; :
253.endif
254
255.endif
256.endif
257
258.if ${_bootstrap_dirdeps} == "yes"
259.if ${BUILD_AT_LEVEL0:Uno} == "no"
260DIRDEPS+= ${RELDIR}.${TARGET_SPEC:U${MACHINE}}
261.endif
262# make sure this is included at least once
263.include <dirdeps.mk>
264.else
265${_DEPENDFILE}: .PRECIOUS
266.endif
267
268CLEANFILES += *.meta filemon.* *.db
269
270# these make it easy to gather some stats
271now_utc = ${%s:L:gmtime}
272start_utc := ${now_utc}
273
274meta_stats= meta=${empty(.MAKE.META.FILES):?0:${.MAKE.META.FILES:[#]}} \
275	created=${empty(.MAKE.META.CREATED):?0:${.MAKE.META.CREATED:[#]}}
276
277#.END: _reldir_finish
278.if target(gendirdeps)
279_reldir_finish: gendirdeps
280.endif
281_reldir_finish: .NOMETA
282	@echo "${TIME_STAMP} Finished ${RELDIR}.${TARGET_SPEC} seconds=$$(( ${now_utc} - ${start_utc} )) ${meta_stats}"
283
284#.ERROR: _reldir_failed
285_reldir_failed: .NOMETA
286	@echo "${TIME_STAMP} Failed ${RELDIR}.${TARGET_SPEC} seconds=$$(( ${now_utc} - ${start_utc} )) ${meta_stats}"
287
288.if defined(WITH_META_STATS) && ${.MAKE.LEVEL} > 0
289.END: _reldir_finish
290.ERROR: _reldir_failed
291.endif
292
293.endif
294