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