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