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