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