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