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