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