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