1 # $Id: meta.autodep.mk,v 1.35 2014/05/09 00:05:46 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"
24 # it would be nice to be able to query .SUFFIXES
25 OBJ_EXTENSIONS+= .o .po .lo .So
27 # explicit dependencies help short-circuit .SUFFIX searches
28 SRCS_DEP_FILTER+= N*.[hly]
29 .for s in ${SRCS:${SRCS_DEP_FILTER:O:u:ts:}}
30 .for e in ${OBJ_EXTENSIONS:O:u}
31 .if !target(${s:T:R}$e)
39 # you are supposed to know what you are doing!
40 UPDATE_DEPENDFILE = yes
41 .elif !empty(.TARGETS) && !make(all)
42 # do not update the *depend* files
43 # unless we are building the entire directory or the default target.
44 # NO means don't update .depend - or Makefile.depend*
45 # no means update .depend but not Makefile.depend*
46 UPDATE_DEPENDFILE = NO
47 .elif ${.MAKEFLAGS:M-k} != ""
48 # it is a bad idea to update anything
49 UPDATE_DEPENDFILE = NO
53 _DEPENDFILE := ${_CURDIR}/${.MAKE.DEPENDFILE:T}
55 .if ${.MAKE.LEVEL} == 0
56 .if ${BUILD_AT_LEVEL0:Uyes:tl} == "no"
57 UPDATE_DEPENDFILE = NO
60 .if !exists(${_DEPENDFILE})
61 _bootstrap_dirdeps = yes
63 _bootstrap_dirdeps ?= no
64 UPDATE_DEPENDFILE ?= yes
66 .if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != ""
67 .info ${_DEPENDFILE:S,${SRCTOP}/,,} update=${UPDATE_DEPENDFILE}
70 .if !empty(XMAKE_META_FILE)
71 .if exists(${.OBJDIR}/${XMAKE_META_FILE})
72 # we cannot get accurate dependencies from an update build
73 UPDATE_DEPENDFILE = NO
75 META_XTRAS += ${XMAKE_META_FILE}
79 .if ${_bootstrap_dirdeps} == "yes" || exists(${_DEPENDFILE})
80 # if it isn't supposed to be touched by us the Makefile should have
81 # UPDATE_DEPENDFILE = no
82 WANT_UPDATE_DEPENDFILE ?= yes
85 .if ${WANT_UPDATE_DEPENDFILE:Uno:tl} != "no"
86 .if ${.MAKE.MODE:Mmeta*} == "" || ${.MAKE.MODE:M*read*} != ""
87 UPDATE_DEPENDFILE = no
90 .if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != ""
91 .info ${_DEPENDFILE:S,${SRCTOP}/,,} update=${UPDATE_DEPENDFILE}
94 .if ${UPDATE_DEPENDFILE:tl} == "yes"
95 # sometimes we want .meta files generated to aid debugging/error detection
96 # but do not want to consider them for dependencies
97 # for example the result of running configure
98 # just make sure this is not empty
99 META_FILE_FILTER ?= N.meta
102 # if we have any non-libs in DPADD,
103 # they probably need to be paid attention to
105 FORCE_DPADD = ${DPADD:${DPLIBS:${M_ListToSkip}}:${DPADD_LAST:${M_ListToSkip}}}
107 _nonlibs := ${DPADD:T:Nlib*:N*include}
109 FORCE_DPADD += ${_nonlibs:@x@${DPADD:M*/$x}@}
114 .if !make(gendirdeps)
118 # if we don't have OBJS, then .depend isn't useful
119 .if !target(.depend) && (!empty(OBJS) || ${.ALLTARGETS:M*.o} != "")
120 # some makefiles and/or targets contain
121 # circular dependencies if you dig too deep
122 # (as meta mode is apt to do)
123 # so we provide a means of suppressing them.
124 # the input to the loop below is target: dependency
125 # with just one dependency per line.
126 # Also some targets are not really local, or use random names.
127 # Use local.autodep.mk to provide local additions!
135 # we use ${.MAKE.META.CREATED} to trigger an update but
136 # we process using ${.MAKE.META.FILES}
137 # the double $$ defers initial evaluation
138 # if necessary, we fake .po dependencies, just so the result
139 # in Makefile.depend* is stable
140 # The current objdir may be referred to in various ways
141 OBJDIR_REFS += ${.OBJDIR} ${.OBJDIR:tA} ${_OBJDIR} ${RELOBJTOP}/${RELDIR}
143 # it would be nice to be able to get .SUFFIXES as ${.SUFFIXES}
144 # we actually only care about the .SUFFIXES of files that might be
145 # generated by tools like yacc.
146 DEPEND_SUFFIXES += .c .h .cpp .hpp .cxx .hxx .cc .hh
147 .depend: .NOMETA $${.MAKE.META.CREATED} ${_this}
148 @echo "Updating $@: ${.OODATE:T:[1..8]}"
149 @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} | \
150 sed -e 's, \./, ,${OBJDIR_REFS:O:u:@d@;s, $d/, ,@};/\//d' \
151 -e 's,^\([^/][^/]*\).meta...[0-9]* ,\1: ,' | \
154 case "$$d:" in $$t) continue;; esac; \
155 case "$$t$$d" in ${SUPPRESS_DEPEND:U.:O:u:ts|}) continue;; esac; \
157 done > $@.${.MAKE.PID}
158 @case "${.MAKE.META.FILES:T:M*.po.*}" in \
159 *.po.*) mv $@.${.MAKE.PID} $@;; \
160 *) { cat $@.${.MAKE.PID}; \
161 sed 's,\.So:,.o:,;s,\.o:,.po:,' $@.${.MAKE.PID}; } | sort -u > $@; \
162 rm -f $@.${.MAKE.PID};; \
165 # make sure this exists
167 # do _not_ assume that .depend is in any fit state for us to use
168 CAT_DEPEND = /dev/null
169 .if ${.MAKE.LEVEL} > 0
175 .if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != ""
176 .info ${_DEPENDFILE:S,${SRCTOP}/,,} _depend=${_depend}
179 .if ${UPDATE_DEPENDFILE} == "yes"
180 gendirdeps: ${_DEPENDFILE}
183 .if !target(${_DEPENDFILE})
184 .if ${_bootstrap_dirdeps} == "yes"
185 # We are boot-strapping a new directory
186 # Use DPADD to seed DIRDEPS
188 # anything which matches ${_OBJROOT}* but not ${_OBJTOP}*
189 # needs to be qualified in DIRDEPS
190 # The pseudo machine "host" is used for HOST_TARGET
192 ${DPADD:M${_OBJTOP}*:H:C,${_OBJTOP}[^/]*/,,:N.:O:u} \
193 ${DPADD:M${_OBJROOT}*:N${_OBJTOP}*:H:S,${_OBJROOT},,:C,^([^/]+)/(.*),\2.\1,:S,${HOST_TARGET}$,host,:N.*:O:u}
199 .if defined(NEED_GENDIRDEPS_MUTEX)
200 # If a src dir gets built with multiple object dirs,
201 # we need a mutex. Obviously, this is best avoided.
202 # Note if .MAKE.DEPENDFILE is common for all ${MACHINE}
203 # you either need to mutex, or ensure only one machine builds at a time!
204 # lockf is an example of a suitable tool
205 LOCKF ?= /usr/bin/lockf
207 GENDIRDEPS_MUTEXER ?= ${LOCKF} -k
209 .if empty(GENDIRDEPS_MUTEXER)
210 .error NEED_GENDIRDEPS_MUTEX defined, but GENDIRDEPS_MUTEXER not set
212 _gendirdeps_mutex = ${GENDIRDEPS_MUTEXER} ${GENDIRDEPS_MUTEX:U${_CURDIR}/Makefile}
216 # If we have META_XTRAS we most likely did not create them
217 # but we need to behave as if we did.
218 # Avoid adding glob patterns to .MAKE.META.CREATED though.
219 .MAKE.META.CREATED += ${META_XTRAS:N*\**:O:u}
223 .elif ${OPTIMIZE_OBJECT_META_FILES:Uno:tl} == "no"
224 META_FILES = ${.MAKE.META.FILES:T:N.depend*:O:u}
226 # if we have 1000's of .o.meta, .So.meta etc we need only look at one set
227 # it is left as an exercise for the reader to work out what this does
228 META_FILES = ${.MAKE.META.FILES:T:N.depend*:N*o.meta:O:u} \
229 ${.MAKE.META.FILES:T:M*.${.MAKE.META.FILES:M*o.meta:R:E:O:u:[1]}.meta:O:u}
232 .if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != ""
233 .info ${_DEPENDFILE:S,${SRCTOP}/,,}: ${_depend} ${.PARSEDIR}/gendirdeps.mk ${META2DEPS} xtras=${META_XTRAS}
236 .if ${.MAKE.LEVEL} > 0 && !empty(GENDIRDEPS_FILTER)
237 .export GENDIRDEPS_FILTER
240 # we might have .../ in MAKESYSPATH
241 _makesyspath:= ${_PARSEDIR}
242 ${_DEPENDFILE}: ${_depend} ${.PARSEDIR}/gendirdeps.mk ${META2DEPS} $${.MAKE.META.CREATED}
243 @echo Checking $@: ${.OODATE:T:[1..8]}
245 SKIP_GENDIRDEPS='${SKIP_GENDIRDEPS:O:u}' \
246 DPADD='${FORCE_DPADD:O:u}' ${_gendirdeps_mutex} \
247 MAKESYSPATH=${_makesyspath} \
248 ${.MAKE} -f gendirdeps.mk RELDIR=${RELDIR} _DEPENDFILE=${_DEPENDFILE} \
249 META_FILES='${META_XTRAS:T:O:u} ${META_FILES:T:O:u:${META_FILE_FILTER:ts:}}')
250 @test -s $@ && touch $@; :
256 .if ${_bootstrap_dirdeps} == "yes"
257 # make sure this is included at least once
258 .include <dirdeps.mk>
260 ${_DEPENDFILE}: .PRECIOUS
263 CLEANFILES += *.meta filemon.* *.db
265 # these make it easy to gather some stats
266 now_utc = ${%s:L:gmtime}
267 start_utc := ${now_utc}
269 meta_stats= meta=${empty(.MAKE.META.FILES):?0:${.MAKE.META.FILES:[#]}} \
270 created=${empty(.MAKE.META.CREATED):?0:${.MAKE.META.CREATED:[#]}}
272 #.END: _reldir_finish
273 .if target(gendirdeps)
274 _reldir_finish: gendirdeps
276 _reldir_finish: .NOMETA
277 @echo "${TIME_STAMP} Finished ${RELDIR}.${TARGET_SPEC} seconds=$$(( ${now_utc} - ${start_utc} )) ${meta_stats}"
279 #.ERROR: _reldir_failed
280 _reldir_failed: .NOMETA
281 @echo "${TIME_STAMP} Failed ${RELDIR}.${TARGET_SPEC} seconds=$$(( ${now_utc} - ${start_utc} )) ${meta_stats}"
283 .if defined(WITH_META_STATS) && ${.MAKE.LEVEL} > 0
285 .ERROR: _reldir_failed