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