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