1284345Ssjg# $FreeBSD: stable/11/share/mk/meta.autodep.mk 309843 2016-12-11 03:57:21Z marcel $
2296637Ssjg# $Id: meta.autodep.mk,v 1.40 2016/02/22 22:44:58 sjg Exp $
3284345Ssjg
4284345Ssjg#
5284345Ssjg#	@(#) Copyright (c) 2010, Simon J. Gerraty
6284345Ssjg#
7284345Ssjg#	This file is provided in the hope that it will
8284345Ssjg#	be of use.  There is absolutely NO WARRANTY.
9284345Ssjg#	Permission to copy, redistribute or otherwise
10284345Ssjg#	use this file is hereby granted provided that 
11284345Ssjg#	the above copyright notice and this notice are
12284345Ssjg#	left intact. 
13284345Ssjg#      
14284345Ssjg#	Please send copies of changes and bug-fixes to:
15284345Ssjg#	sjg@crufty.net
16284345Ssjg#
17284345Ssjg
18284345Ssjg_this ?= ${.PARSEFILE}
19284345Ssjg.if !target(__${_this}__)
20284345Ssjg__${_this}__: .NOTMAIN
21284345Ssjg
22298225Ssjg.-include <local.autodep.mk>
23284345Ssjg
24284345Ssjg.if defined(SRCS)
25284345Ssjg# it would be nice to be able to query .SUFFIXES
26309843SmarcelOBJ_EXTENSIONS+= .o .po .lo .pico
27284345Ssjg
28284345Ssjg# explicit dependencies help short-circuit .SUFFIX searches
29284345SsjgSRCS_DEP_FILTER+= N*.[hly]
30284345Ssjg.for s in ${SRCS:${SRCS_DEP_FILTER:O:u:ts:}}
31284345Ssjg.for e in ${OBJ_EXTENSIONS:O:u}
32284345Ssjg.if !target(${s:T:R}$e)
33284345Ssjg${s:T:R}$e: $s
34284345Ssjg.endif
35284345Ssjg.endfor
36284345Ssjg.endfor
37284345Ssjg.endif
38284345Ssjg
39284345Ssjg.if make(gendirdeps)
40284345Ssjg# you are supposed to know what you are doing!
41284345SsjgUPDATE_DEPENDFILE = yes
42284345Ssjg.elif !empty(.TARGETS) && !make(all)
43284345Ssjg# do not update the *depend* files 
44284345Ssjg# unless we are building the entire directory or the default target.
45284345Ssjg# NO means don't update .depend - or Makefile.depend*
46284345Ssjg# no means update .depend but not Makefile.depend*
47284345SsjgUPDATE_DEPENDFILE = NO
48284345Ssjg.elif ${.MAKEFLAGS:M-k} != ""
49284345Ssjg# it is a bad idea to update anything
50284345SsjgUPDATE_DEPENDFILE = NO
51284345Ssjg.endif
52284345Ssjg
53284345Ssjg_CURDIR ?= ${.CURDIR}
54291315Sbdrewery_OBJDIR ?= ${.OBJDIR}
55291315Sbdrewery_OBJTOP ?= ${OBJTOP}
56291315Sbdrewery_OBJROOT ?= ${OBJROOT:U${_OBJTOP}}
57284345Ssjg_DEPENDFILE := ${_CURDIR}/${.MAKE.DEPENDFILE:T}
58284345Ssjg
59298225Ssjg.if ${.MAKE.LEVEL} > 0 || ${BUILD_AT_LEVEL0:Uyes:tl} == "yes"
60298225Ssjg# do not allow auto update if we ever built this dir without filemon
61298225SsjgNO_FILEMON_COOKIE = .nofilemon
62298225SsjgCLEANFILES += ${NO_FILEMON_COOKIE}
63298225Ssjg.if ${.MAKE.MODE:Uno:Mnofilemon} != ""
64298225SsjgUPDATE_DEPENDFILE = NO
65298225Ssjgall: ${NO_FILEMON_COOKIE}
66298225Ssjg${NO_FILEMON_COOKIE}: .NOMETA
67298225Ssjg	@echo UPDATE_DEPENDFILE=NO > ${.TARGET}
68298225Ssjg.elif exists(${NO_FILEMON_COOKIE})
69298225SsjgUPDATE_DEPENDFILE = NO
70298225Ssjg.warning ${RELDIR} built with nofilemon; UPDATE_DEPENDFILE=NO
71298225Ssjg.endif
72298225Ssjg.endif
73298225Ssjg
74284345Ssjg.if ${.MAKE.LEVEL} == 0
75284345Ssjg.if ${BUILD_AT_LEVEL0:Uyes:tl} == "no"
76284345SsjgUPDATE_DEPENDFILE = NO
77284345Ssjg.endif
78284345Ssjg.endif
79284345Ssjg.if !exists(${_DEPENDFILE})
80284345Ssjg_bootstrap_dirdeps = yes
81284345Ssjg.endif
82284345Ssjg_bootstrap_dirdeps ?= no
83284345SsjgUPDATE_DEPENDFILE ?= yes
84284345Ssjg
85284345Ssjg.if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != ""
86284345Ssjg.info ${_DEPENDFILE:S,${SRCTOP}/,,} update=${UPDATE_DEPENDFILE}
87284345Ssjg.endif
88284345Ssjg
89284345Ssjg.if !empty(XMAKE_META_FILE)
90284345Ssjg.if exists(${.OBJDIR}/${XMAKE_META_FILE})
91284345Ssjg# we cannot get accurate dependencies from an update build
92284345SsjgUPDATE_DEPENDFILE = NO
93284345Ssjg.else
94284345SsjgMETA_XTRAS += ${XMAKE_META_FILE}
95284345Ssjg.endif
96284345Ssjg.endif
97284345Ssjg
98284345Ssjg.if ${_bootstrap_dirdeps} == "yes" || exists(${_DEPENDFILE})
99284345Ssjg# if it isn't supposed to be touched by us the Makefile should have
100284345Ssjg# UPDATE_DEPENDFILE = no
101284345SsjgWANT_UPDATE_DEPENDFILE ?= yes
102284345Ssjg.endif
103284345Ssjg
104284345Ssjg.if ${WANT_UPDATE_DEPENDFILE:Uno:tl} != "no"
105296637Ssjg.if ${.MAKE.MODE:Uno:Mmeta*} == "" || ${.MAKE.MODE:Uno:M*read*} != ""
106284345SsjgUPDATE_DEPENDFILE = no
107284345Ssjg.endif
108284345Ssjg
109284345Ssjg.if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != ""
110284345Ssjg.info ${_DEPENDFILE:S,${SRCTOP}/,,} update=${UPDATE_DEPENDFILE}
111284345Ssjg.endif
112284345Ssjg
113284345Ssjg.if ${UPDATE_DEPENDFILE:tl} == "yes"
114284345Ssjg# sometimes we want .meta files generated to aid debugging/error detection
115284345Ssjg# but do not want to consider them for dependencies
116284345Ssjg# for example the result of running configure
117284345Ssjg# just make sure this is not empty
118284345SsjgMETA_FILE_FILTER ?= N.meta
119284345Ssjg# never consider these
120284345SsjgMETA_FILE_FILTER += Ndirdeps.cache*
121284345Ssjg
122284345Ssjg.if !empty(DPADD)
123284345Ssjg# if we have any non-libs in DPADD, 
124284345Ssjg# they probably need to be paid attention to
125284345Ssjg.if !empty(DPLIBS)
126284345SsjgFORCE_DPADD = ${DPADD:${DPLIBS:${M_ListToSkip}}:${DPADD_LAST:${M_ListToSkip}}}
127284345Ssjg.else
128284345Ssjg_nonlibs := ${DPADD:T:Nlib*:N*include}
129284345Ssjg.if !empty(_nonlibs)
130284345SsjgFORCE_DPADD += ${_nonlibs:@x@${DPADD:M*/$x}@}
131284345Ssjg.endif
132284345Ssjg.endif
133284345Ssjg.endif
134284345Ssjg
135284345Ssjg.if !make(gendirdeps)
136284345Ssjg.END:	gendirdeps
137284345Ssjg.endif
138284345Ssjg
139284345Ssjg# if we don't have OBJS, then .depend isn't useful
140284345Ssjg.if !target(.depend) && (!empty(OBJS) || ${.ALLTARGETS:M*.o} != "")
141284345Ssjg# some makefiles and/or targets contain
142284345Ssjg# circular dependencies if you dig too deep 
143284345Ssjg# (as meta mode is apt to do) 
144284345Ssjg# so we provide a means of suppressing them.
145284345Ssjg# the input to the loop below is target: dependency
146284345Ssjg# with just one dependency per line.
147284345Ssjg# Also some targets are not really local, or use random names.
148284345Ssjg# Use local.autodep.mk to provide local additions!
149284345SsjgSUPPRESS_DEPEND += \
150284345Ssjg	${SB:S,/,_,g}* \
151284345Ssjg	*:y.tab.c \
152284345Ssjg	*.c:*.c \
153284345Ssjg	*.h:*.h
154284345Ssjg
155284345Ssjg.NOPATH:	.depend
156284345Ssjg# we use ${.MAKE.META.CREATED} to trigger an update but
157284345Ssjg# we process using ${.MAKE.META.FILES}
158284345Ssjg# the double $$ defers initial evaluation
159284345Ssjg# if necessary, we fake .po dependencies, just so the result 
160284345Ssjg# in Makefile.depend* is stable
161284345Ssjg# The current objdir may be referred to in various ways
162284345SsjgOBJDIR_REFS += ${.OBJDIR} ${.OBJDIR:tA} ${_OBJDIR} ${RELOBJTOP}/${RELDIR}
163284345Ssjg_depend = .depend
164284345Ssjg# it would be nice to be able to get .SUFFIXES as ${.SUFFIXES}
165284345Ssjg# we actually only care about the .SUFFIXES of files that might be 
166284345Ssjg# generated by tools like yacc.
167284345SsjgDEPEND_SUFFIXES += .c .h .cpp .hpp .cxx .hxx .cc .hh
168284345Ssjg.depend: .NOMETA $${.MAKE.META.CREATED} ${_this}
169284345Ssjg	@echo "Updating $@: ${.OODATE:T:[1..8]}"
170284345Ssjg	@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} | \
171284345Ssjg	sed -e 's, \./, ,${OBJDIR_REFS:O:u:@d@;s, $d/, ,@};/\//d' \
172284345Ssjg		-e 's,^\([^/][^/]*\).meta...[0-9]* ,\1: ,' | \
173284345Ssjg	sort -u | \
174284345Ssjg	while read t d; do \
175284345Ssjg		case "$$d:" in $$t) continue;; esac; \
176284345Ssjg		case "$$t$$d" in ${SUPPRESS_DEPEND:U.:O:u:ts|}) continue;; esac; \
177284345Ssjg		echo $$t $$d; \
178284345Ssjg	done > $@.${.MAKE.PID}
179284345Ssjg	@case "${.MAKE.META.FILES:T:M*.po.*}" in \
180284345Ssjg	*.po.*) mv $@.${.MAKE.PID} $@;; \
181284345Ssjg	*) { cat $@.${.MAKE.PID}; \
182309843Smarcel	sed 's,\.pico:,.o:,;s,\.o:,.po:,' $@.${.MAKE.PID}; } | sort -u > $@; \
183284345Ssjg	rm -f $@.${.MAKE.PID};; \
184284345Ssjg	esac
185284345Ssjg.else
186284345Ssjg# make sure this exists
187284345Ssjg.depend:
188284345Ssjg# do _not_ assume that .depend is in any fit state for us to use
189284345SsjgCAT_DEPEND = /dev/null
190284345Ssjg.if ${.MAKE.LEVEL} > 0
191284345Ssjg.export CAT_DEPEND
192284345Ssjg.endif
193284345Ssjg_depend =
194284345Ssjg.endif
195284345Ssjg
196284345Ssjg.if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != ""
197284345Ssjg.info ${_DEPENDFILE:S,${SRCTOP}/,,} _depend=${_depend}
198284345Ssjg.endif
199284345Ssjg
200284345Ssjg.if ${UPDATE_DEPENDFILE} == "yes"
201284345Ssjggendirdeps:	${_DEPENDFILE}
202284345Ssjg.endif
203284345Ssjg
204284345Ssjg.if !target(${_DEPENDFILE})
205284345Ssjg.if ${_bootstrap_dirdeps} == "yes"
206284345Ssjg# We are boot-strapping a new directory
207284345Ssjg# Use DPADD to seed DIRDEPS
208284345Ssjg.if !empty(DPADD)
209284345Ssjg# anything which matches ${_OBJROOT}* but not ${_OBJTOP}*
210284345Ssjg# needs to be qualified in DIRDEPS
211284345Ssjg# The pseudo machine "host" is used for HOST_TARGET
212291314SbdreweryDIRDEPS += \
213284345Ssjg	${DPADD:M${_OBJTOP}*:H:C,${_OBJTOP}[^/]*/,,:N.:O:u} \
214291316Sbdrewery	${DPADD:M${_OBJROOT}*:N${_OBJTOP}*:N${STAGE_ROOT:U${_OBJTOP}}/*:H:S,${_OBJROOT},,:C,^([^/]+)/(.*),\2.\1,:S,${HOST_TARGET}$,host,:N.*:O:u}
215284345Ssjg
216284345Ssjg.endif
217284345Ssjg.endif
218284345Ssjg
219284345Ssjg_gendirdeps_mutex =
220284345Ssjg.if defined(NEED_GENDIRDEPS_MUTEX)
221284345Ssjg# If a src dir gets built with multiple object dirs,
222284345Ssjg# we need a mutex.  Obviously, this is best avoided.
223284345Ssjg# Note if .MAKE.DEPENDFILE is common for all ${MACHINE}
224284345Ssjg# you either need to mutex, or ensure only one machine builds at a time!
225284345Ssjg# lockf is an example of a suitable tool
226284345SsjgLOCKF ?= /usr/bin/lockf
227284345Ssjg.if exists(${LOCKF})
228284345SsjgGENDIRDEPS_MUTEXER ?= ${LOCKF} -k
229284345Ssjg.endif
230284345Ssjg.if empty(GENDIRDEPS_MUTEXER)
231284345Ssjg.error NEED_GENDIRDEPS_MUTEX defined, but GENDIRDEPS_MUTEXER not set
232284345Ssjg.else
233284345Ssjg_gendirdeps_mutex = ${GENDIRDEPS_MUTEXER} ${GENDIRDEPS_MUTEX:U${_CURDIR}/Makefile}
234284345Ssjg.endif
235284345Ssjg.endif
236284345Ssjg
237284345Ssjg# If we have META_XTRAS we most likely did not create them
238284345Ssjg# but we need to behave as if we did.
239284345Ssjg# Avoid adding glob patterns to .MAKE.META.CREATED though.
240284345Ssjg.MAKE.META.CREATED += ${META_XTRAS:N*\**:O:u}
241284345Ssjg
242284345Ssjg.if make(gendirdeps)
243284345SsjgMETA_FILES = *.meta
244284345Ssjg.elif ${OPTIMIZE_OBJECT_META_FILES:Uno:tl} == "no"
245284345SsjgMETA_FILES = ${.MAKE.META.FILES:T:N.depend*:O:u}
246284345Ssjg.else
247309843Smarcel# if we have 1000's of .o.meta, .pico.meta etc we need only look at one set
248284345Ssjg# it is left as an exercise for the reader to work out what this does
249284345SsjgMETA_FILES = ${.MAKE.META.FILES:T:N.depend*:N*o.meta:O:u} \
250284345Ssjg	${.MAKE.META.FILES:T:M*.${.MAKE.META.FILES:M*o.meta:R:E:O:u:[1]}.meta:O:u}
251284345Ssjg.endif
252284345Ssjg
253284345Ssjg.if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != ""
254284345Ssjg.info ${_DEPENDFILE:S,${SRCTOP}/,,}: ${_depend} ${.PARSEDIR}/gendirdeps.mk ${META2DEPS} xtras=${META_XTRAS}
255284345Ssjg.endif
256284345Ssjg
257284345Ssjg.if ${.MAKE.LEVEL} > 0 && !empty(GENDIRDEPS_FILTER)
258284345Ssjg.export GENDIRDEPS_FILTER
259284345Ssjg.endif
260284345Ssjg
261284345Ssjg# we might have .../ in MAKESYSPATH
262284345Ssjg_makesyspath:= ${_PARSEDIR}
263284345Ssjg${_DEPENDFILE}: ${_depend} ${.PARSEDIR}/gendirdeps.mk  ${META2DEPS} $${.MAKE.META.CREATED}
264284345Ssjg	@echo Checking $@: ${.OODATE:T:[1..8]}
265284345Ssjg	@(cd . && \
266284345Ssjg	SKIP_GENDIRDEPS='${SKIP_GENDIRDEPS:O:u}' \
267284345Ssjg	DPADD='${FORCE_DPADD:O:u}' ${_gendirdeps_mutex} \
268284345Ssjg	MAKESYSPATH=${_makesyspath} \
269284345Ssjg	${.MAKE} -f gendirdeps.mk RELDIR=${RELDIR} _DEPENDFILE=${_DEPENDFILE} \
270300805Sbdrewery	META_FILES='${META_XTRAS:O:u} ${META_FILES:T:O:u:${META_FILE_FILTER:ts:}}')
271284345Ssjg	@test -s $@ && touch $@; :
272284345Ssjg.endif
273284345Ssjg
274284345Ssjg.endif
275284345Ssjg.endif
276284345Ssjg
277284345Ssjg.if ${_bootstrap_dirdeps} == "yes"
278284345Ssjg.if ${BUILD_AT_LEVEL0:Uno} == "no"
279284345SsjgDIRDEPS+= ${RELDIR}.${TARGET_SPEC:U${MACHINE}}
280284345Ssjg.endif
281284345Ssjg# make sure this is included at least once
282284345Ssjg.include <dirdeps.mk>
283284345Ssjg.else
284284345Ssjg${_DEPENDFILE}: .PRECIOUS
285284345Ssjg.endif
286284345Ssjg
287284345SsjgCLEANFILES += *.meta filemon.* *.db
288284345Ssjg
289284345Ssjg# these make it easy to gather some stats
290284345Ssjgnow_utc = ${%s:L:gmtime}
291284345Ssjgstart_utc := ${now_utc}
292284345Ssjg
293284345Ssjgmeta_stats= meta=${empty(.MAKE.META.FILES):?0:${.MAKE.META.FILES:[#]}} \
294284345Ssjg	created=${empty(.MAKE.META.CREATED):?0:${.MAKE.META.CREATED:[#]}}
295284345Ssjg
296284345Ssjg#.END: _reldir_finish
297284345Ssjg.if target(gendirdeps)
298284345Ssjg_reldir_finish: gendirdeps
299284345Ssjg.endif
300284345Ssjg_reldir_finish: .NOMETA
301284345Ssjg	@echo "${TIME_STAMP} Finished ${RELDIR}.${TARGET_SPEC} seconds=$$(( ${now_utc} - ${start_utc} )) ${meta_stats}"
302284345Ssjg
303284345Ssjg#.ERROR: _reldir_failed
304284345Ssjg_reldir_failed: .NOMETA
305284345Ssjg	@echo "${TIME_STAMP} Failed ${RELDIR}.${TARGET_SPEC} seconds=$$(( ${now_utc} - ${start_utc} )) ${meta_stats}"
306284345Ssjg
307284345Ssjg.if defined(WITH_META_STATS) && ${.MAKE.LEVEL} > 0
308284345Ssjg.END: _reldir_finish
309284345Ssjg.ERROR: _reldir_failed
310284345Ssjg.endif
311284345Ssjg
312284345Ssjg.endif
313