1 # $Id: meta.autodep.mk,v 1.56 2022/09/09 17:44:29 sjg Exp $
4 # @(#) Copyright (c) 2010, Simon J. Gerraty
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
13 # Please send copies of changes and bug-fixes to:
17 _this ?= ${.PARSEFILE}
18 .if !target(__${_this}__)
19 __${_this}__: .NOTMAIN
21 .-include <local.autodep.mk>
26 .if ${MAKE_VERSION:U0} >= 20211212
27 OBJ_EXTENSIONS += ${.SUFFIXES:M*o}
29 # it would be nice to be able to query .SUFFIXES
30 OBJ_EXTENSIONS += .o .po .lo ${PICO}
33 # explicit dependencies help short-circuit .SUFFIX searches
34 SRCS_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)
45 # you are supposed to know what you are doing!
46 UPDATE_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*
52 UPDATE_DEPENDFILE = NO
53 .elif ${.MAKEFLAGS:M-k} != ""
54 # it is a bad idea to update anything
55 UPDATE_DEPENDFILE = NO
61 _OBJROOT ?= ${OBJROOT:U${_OBJTOP}}
62 _DEPENDFILE := ${_CURDIR}/${.MAKE.DEPENDFILE:T}
64 .if ${.MAKE.LEVEL} > 0
65 # do not allow auto update if we ever built this dir without filemon
66 NO_FILEMON_COOKIE = .nofilemon
67 CLEANFILES += ${NO_FILEMON_COOKIE}
68 .if ${.MAKE.MODE:Uno:Mnofilemon} != ""
69 UPDATE_DEPENDFILE = NO
70 all: ${NO_FILEMON_COOKIE}
71 ${NO_FILEMON_COOKIE}: .NOMETA
72 @echo UPDATE_DEPENDFILE=NO > ${.TARGET}
73 .elif exists(${NO_FILEMON_COOKIE})
74 UPDATE_DEPENDFILE = NO
75 .warning ${RELDIR} built with nofilemon; UPDATE_DEPENDFILE=NO
79 .if ${.MAKE.LEVEL} == 0
80 UPDATE_DEPENDFILE = NO
82 .if !exists(${_DEPENDFILE})
83 _bootstrap_dirdeps = yes
85 _bootstrap_dirdeps ?= no
86 UPDATE_DEPENDFILE ?= yes
88 .if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != ""
89 .info ${_DEPENDFILE:S,${SRCTOP}/,,} update=${UPDATE_DEPENDFILE}
92 .if !empty(XMAKE_META_FILE)
93 .if exists(${.OBJDIR}/${XMAKE_META_FILE})
94 # we cannot get accurate dependencies from an update build
95 UPDATE_DEPENDFILE = NO
97 META_XTRAS += ${XMAKE_META_FILE}
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
104 WANT_UPDATE_DEPENDFILE ?= yes
107 .if ${WANT_UPDATE_DEPENDFILE:Uno:tl} != "no"
108 .if ${.MAKE.MODE:Uno:Mmeta*} == "" || ${.MAKE.MODE:Uno:M*read*} != ""
109 UPDATE_DEPENDFILE = no
112 .if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != ""
113 .info ${_DEPENDFILE:S,${SRCTOP}/,,} update=${UPDATE_DEPENDFILE}
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
121 META_FILE_FILTER ?= N.meta
122 # never consider these
123 META_FILE_FILTER += Ndirdeps.cache*
126 # if we have any non-libs in DPADD,
127 # they probably need to be paid attention to
129 FORCE_DPADD = ${DPADD:${DPLIBS:${M_ListToSkip}}:${DPADD_LAST:${M_ListToSkip}}}
131 _nonlibs := ${DPADD:T:Nlib*:N*include}
133 FORCE_DPADD += ${_nonlibs:@x@${DPADD:M*/$x}@}
138 .if !make(gendirdeps)
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!
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
165 OBJDIR_REFS += ${.OBJDIR} ${.OBJDIR:tA} ${_OBJDIR} ${RELOBJTOP}/${RELDIR}
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.
170 .if ${MAKE_VERSION:U0} >= 20211212
171 DEPEND_SUFFIXES += ${.SUFFIXES:N.sh:N*[0-9aFfglopmnrSsty]}
173 DEPEND_SUFFIXES += .c .h .cpp .hpp .cxx .hxx .cc .hh
175 .depend: .NOMETA $${.MAKE.META.CREATED} ${_this}
176 @echo "Updating $@: ${.OODATE:T:[1..8]}"
177 @${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} | \
178 sed -e 's, \./, ,${OBJDIR_REFS:O:u:@d@;s, $d/, ,@};/\//d' \
179 -e 's,^\([^/][^/]*\).meta...[0-9]* ,\1: ,' | \
182 case "$$d:" in $$t) continue;; esac; \
183 case "$$t$$d" in ${SUPPRESS_DEPEND:U.:O:u:ts|}) continue;; esac; \
185 done > $@.${.MAKE.PID}
186 @case "${.MAKE.META.FILES:T:M*.po.*}" in \
187 *.po.*) mv $@.${.MAKE.PID} $@;; \
188 *) { cat $@.${.MAKE.PID}; \
189 sed ${OBJ_EXTENSIONS:N.o:N.po:@o@-e 's,\$o:,.o:,'@} \
190 -e 's,\.o:,.po:,' $@.${.MAKE.PID}; } | sort -u > $@; \
191 rm -f $@.${.MAKE.PID};; \
194 # make sure this exists
196 # do _not_ assume that .depend is in any fit state for us to use
197 CAT_DEPEND = /dev/null
198 .if ${.MAKE.LEVEL} > 0
204 .if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != ""
205 .info ${_DEPENDFILE:S,${SRCTOP}/,,} _depend=${_depend}
208 .if ${UPDATE_DEPENDFILE} == "yes"
209 gendirdeps: ${_DEPENDFILE}
212 .if !target(${_DEPENDFILE})
213 .if ${_bootstrap_dirdeps} == "yes"
214 # We are boot-strapping a new directory
215 # Use DPADD to seed DIRDEPS
217 # anything which matches ${_OBJROOT}* but not ${_OBJTOP}*
218 # needs to be qualified in DIRDEPS
219 # The pseudo machine "host" is used for HOST_TARGET
221 ${DPADD:M${_OBJTOP}*:H:C,${_OBJTOP}[^/]*/,,:N.:O:u} \
222 ${DPADD:M${_OBJROOT}*:N${_OBJTOP}*:N${STAGE_ROOT:U${_OBJTOP}}/*:H:S,${_OBJROOT},,:C,^([^/]+)/(.*),\2.\1,:S,${HOST_TARGET}$,host,:N.*:O:u}
228 .if defined(NEED_GENDIRDEPS_MUTEX)
229 # If a src dir gets built with multiple object dirs,
230 # we need a mutex. Obviously, this is best avoided.
231 # Note if .MAKE.DEPENDFILE is common for all ${MACHINE}
232 # you either need to mutex, or ensure only one machine builds at a time!
233 # lockf is an example of a suitable tool
234 LOCKF ?= /usr/bin/lockf
236 GENDIRDEPS_MUTEXER ?= ${LOCKF} -k
238 .if empty(GENDIRDEPS_MUTEXER)
239 .error NEED_GENDIRDEPS_MUTEX defined, but GENDIRDEPS_MUTEXER not set
241 _gendirdeps_mutex = ${GENDIRDEPS_MUTEXER} ${GENDIRDEPS_MUTEX:U${_CURDIR}/Makefile}
245 # If we have META_XTRAS we most likely did not create them
246 # but we need to behave as if we did.
247 # Avoid adding glob patterns to .MAKE.META.CREATED though.
248 .MAKE.META.CREATED += ${META_XTRAS:N*\**:O:u}
252 .elif ${OPTIMIZE_OBJECT_META_FILES:Uno:tl} == "no"
253 META_FILES = ${.MAKE.META.FILES:T:N.depend*:O:u}
255 # if we have 1000's of .o.meta, ${PICO}.meta etc we need only look at one set
256 # it is left as an exercise for the reader to work out what this does
257 META_FILES = ${.MAKE.META.FILES:T:N.depend*:N*o.meta:O:u} \
258 ${.MAKE.META.FILES:T:M*.${.MAKE.META.FILES:M*o.meta:R:E:O:u:[1]}.meta:O:u}
261 .if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != ""
262 .info ${_DEPENDFILE:S,${SRCTOP}/,,}: ${_depend} ${.PARSEDIR}/gendirdeps.mk ${META2DEPS} xtras=${META_XTRAS}
265 .if ${.MAKE.LEVEL} > 0
266 .if ${UPDATE_DEPENDFILE} == "yes"
267 .-include <${.CURDIR}/${.MAKE.DEPENDFILE_PREFIX}.options>
269 .if !empty(GENDIRDEPS_FILTER)
270 .export GENDIRDEPS_FILTER
272 # export to avoid blowing command line limit
273 META_FILES := ${META_XTRAS:U:O:u} ${META_FILES:U:T:O:u:${META_FILE_FILTER:ts:}}
277 # we might have .../ in MAKESYSPATH
278 _makesyspath:= ${_PARSEDIR}
279 ${_DEPENDFILE}: ${_depend} ${.PARSEDIR}/gendirdeps.mk ${META2DEPS} $${.MAKE.META.CREATED}
280 @echo Checking $@: ${.OODATE:T:[1..8]}
281 @(cd . && ${GENDIRDEPS_ENV} \
282 SKIP_GENDIRDEPS='${SKIP_GENDIRDEPS:O:u}' \
283 DPADD='${FORCE_DPADD:O:u}' ${_gendirdeps_mutex} \
284 MAKESYSPATH=${_makesyspath} \
285 ${.MAKE} -f gendirdeps.mk RELDIR=${RELDIR} _DEPENDFILE=${_DEPENDFILE})
286 @test -s $@ && touch $@; :
292 .if ${_bootstrap_dirdeps} == "yes"
293 DIRDEPS+= ${RELDIR}.${TARGET_SPEC:U${MACHINE}}
294 # make sure this is included at least once
295 .include <dirdeps.mk>
297 ${_DEPENDFILE}: .PRECIOUS
300 CLEANFILES += *.meta filemon.* *.db
302 # these make it easy to gather some stats
303 now_utc = ${%s:L:gmtime}
304 start_utc := ${now_utc}
306 meta_stats= meta=${empty(.MAKE.META.FILES):?0:${.MAKE.META.FILES:[#]}} \
307 created=${empty(.MAKE.META.CREATED):?0:${.MAKE.META.CREATED:[#]}}
309 .if !target(_reldir_finish)
310 #.END: _reldir_finish
311 .if target(gendirdeps)
312 _reldir_finish: gendirdeps
314 _reldir_finish: .NOMETA
315 @echo "${TIME_STAMP} Finished ${RELDIR}.${TARGET_SPEC} seconds=$$(( ${now_utc} - ${start_utc} )) ${meta_stats}"
318 .if !target(_reldir_failed)
319 #.ERROR: _reldir_failed
320 _reldir_failed: .NOMETA
321 @echo "${TIME_STAMP} Failed ${RELDIR}.${TARGET_SPEC} seconds=$$(( ${now_utc} - ${start_utc} )) ${meta_stats}"
324 .if !defined(WITHOUT_META_STATS) && ${.MAKE.LEVEL} > 0
326 .ERROR: _reldir_failed