bsd.dep.mk revision 295987
1# $FreeBSD: head/share/mk/bsd.dep.mk 295987 2016-02-24 17:19:13Z bdrewery $
2#
3# The include file <bsd.dep.mk> handles Makefile dependencies.
4#
5#
6# +++ variables +++
7#
8# CLEANDEPENDDIRS	Additional directories to remove for the cleandepend
9# 			target.
10#
11# CLEANDEPENDFILES	Additional files to remove for the cleandepend target.
12#
13# CTAGS		A tags file generation program [gtags]
14#
15# CTAGSFLAGS	Options for ctags(1) [not set]
16#
17# DEPENDFILE	dependencies file [.depend]
18#
19# GTAGSFLAGS	Options for gtags(1) [-o]
20#
21# HTAGSFLAGS	Options for htags(1) [not set]
22#
23# MKDEP		Options for ${MKDEPCMD} [not set]
24#
25# MKDEPCMD	Makefile dependency list program [mkdep]
26#
27# SRCS          List of source files (c, c++, assembler)
28#
29# DPSRCS	List of source files which are needed for generating
30#		dependencies, ${SRCS} are always part of it.
31#
32# +++ targets +++
33#
34#	cleandepend:
35#		remove ${CLEANDEPENDFILES}; remove ${CLEANDEPENDDIRS} and all
36#		contents.
37#
38#	depend:
39#		Make the dependencies for the source files, and store
40#		them in the file ${DEPENDFILE}.
41#
42#	tags:
43#		In "ctags" mode, create a tags file for the source files.
44#		In "gtags" mode, create a (GLOBAL) gtags file for the
45#		source files.  If HTML is defined, htags(1) is also run
46#		after gtags(1).
47
48.if !target(__<bsd.init.mk>__)
49.error bsd.dep.mk cannot be included directly.
50.endif
51
52CTAGS?=		gtags
53CTAGSFLAGS?=
54GTAGSFLAGS?=	-o
55HTAGSFLAGS?=
56
57_MKDEPCC:=	${CC:N${CCACHE_BIN}}
58# XXX: DEPFLAGS can come out once Makefile.inc1 properly passes down
59# CXXFLAGS.
60.if !empty(DEPFLAGS)
61_MKDEPCC+=	${DEPFLAGS}
62.endif
63MKDEPCMD?=	CC='${_MKDEPCC}' mkdep
64DEPENDFILE?=	.depend
65.if ${MK_DIRDEPS_BUILD} == "no"
66.MAKE.DEPENDFILE= ${DEPENDFILE}
67.endif
68CLEANDEPENDFILES=	${DEPENDFILE} ${DEPENDFILE}.*
69
70# Keep `tags' here, before SRCS are mangled below for `depend'.
71.if !target(tags) && defined(SRCS) && !defined(NO_TAGS)
72tags: ${SRCS}
73.if ${CTAGS:T} == "gtags"
74	@cd ${.CURDIR} && ${CTAGS} ${GTAGSFLAGS} ${.OBJDIR}
75.if defined(HTML)
76	@cd ${.CURDIR} && htags ${HTAGSFLAGS} -d ${.OBJDIR} ${.OBJDIR}
77.endif
78.else
79	@${CTAGS} ${CTAGSFLAGS} -f /dev/stdout \
80	    ${.ALLSRC:N*.h} | sed "s;${.CURDIR}/;;" > ${.TARGET}
81.endif
82.endif
83
84# Skip reading .depend when not needed to speed up tree-walks
85# and simple lookups.
86.if !empty(.MAKEFLAGS:M-V${_V_READ_DEPEND}) || make(obj) || make(clean*) || \
87    make(install*)
88_SKIP_READ_DEPEND=	1
89.if ${MK_DIRDEPS_BUILD} == "no"
90.MAKE.DEPENDFILE=	/dev/null
91.endif
92.endif
93
94.if defined(SRCS)
95CLEANFILES?=
96
97.for _S in ${SRCS:N*.[dhly]}
98OBJS_DEPEND_GUESS.${_S:R}.o=	${_S}
99.if ${MK_FAST_DEPEND} == "no" && !exists(${.OBJDIR}/${DEPENDFILE})
100${_S:R}.o: ${OBJS_DEPEND_GUESS.${_S:R}.o}
101.endif
102.endfor
103
104# Lexical analyzers
105.for _LSRC in ${SRCS:M*.l:N*/*}
106.for _LC in ${_LSRC:R}.c
107${_LC}: ${_LSRC}
108	${LEX} ${LFLAGS} -o${.TARGET} ${.ALLSRC}
109OBJS_DEPEND_GUESS.${_LC:R}.o=	${_LC}
110.if ${MK_FAST_DEPEND} == "no" && !exists(${.OBJDIR}/${DEPENDFILE})
111${_LC:R}.o: ${OBJS_DEPEND_GUESS.${_LC:R}.o}
112.endif
113SRCS:=	${SRCS:S/${_LSRC}/${_LC}/}
114CLEANFILES+= ${_LC}
115.endfor
116.endfor
117
118# Yacc grammars
119.for _YSRC in ${SRCS:M*.y:N*/*}
120.for _YC in ${_YSRC:R}.c
121SRCS:=	${SRCS:S/${_YSRC}/${_YC}/}
122CLEANFILES+= ${_YC}
123.if !empty(YFLAGS:M-d) && !empty(SRCS:My.tab.h)
124.ORDER: ${_YC} y.tab.h
125${_YC} y.tab.h: ${_YSRC}
126	${YACC} ${YFLAGS} ${.ALLSRC}
127	cp y.tab.c ${_YC}
128CLEANFILES+= y.tab.c y.tab.h
129.elif !empty(YFLAGS:M-d)
130.for _YH in ${_YC:R}.h
131.ORDER: ${_YC} ${_YH}
132${_YC} ${_YH}: ${_YSRC}
133	${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
134SRCS+=	${_YH}
135CLEANFILES+= ${_YH}
136.endfor
137.else
138${_YC}: ${_YSRC}
139	${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
140.endif
141OBJS_DEPEND_GUESS.${_YC:R}.o=	${_YC}
142.if ${MK_FAST_DEPEND} == "no" && !exists(${.OBJDIR}/${DEPENDFILE})
143${_YC:R}.o: ${OBJS_DEPEND_GUESS.${_YC:R}.o}
144.endif
145.endfor
146.endfor
147
148# DTrace probe definitions
149.if ${SRCS:M*.d}
150CFLAGS+=	-I${.OBJDIR}
151.endif
152.for _DSRC in ${SRCS:M*.d:N*/*}
153.for _D in ${_DSRC:R}
154SRCS+=	${_D}.h
155${_D}.h: ${_DSRC}
156	${DTRACE} ${DTRACEFLAGS} -h -s ${.ALLSRC}
157SRCS:=	${SRCS:S/^${_DSRC}$//}
158OBJS+=	${_D}.o
159CLEANFILES+= ${_D}.h ${_D}.o
160${_D}.o: ${_DSRC} ${OBJS:S/^${_D}.o$//}
161	@rm -f ${.TARGET}
162	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
163.if defined(LIB)
164CLEANFILES+= ${_D}.So ${_D}.po
165${_D}.So: ${_DSRC} ${SOBJS:S/^${_D}.So$//}
166	@rm -f ${.TARGET}
167	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
168${_D}.po: ${_DSRC} ${POBJS:S/^${_D}.po$//}
169	@rm -f ${.TARGET}
170	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
171.endif
172.endfor
173.endfor
174
175
176.if !empty(.MAKE.MODE:Mmeta) && empty(.MAKE.MODE:Mnofilemon)
177_meta_filemon=	1
178.endif
179.if ${MK_FAST_DEPEND} == "yes"
180DEPEND_MP?=	-MP
181# Handle OBJS=../somefile.o hacks.  Just replace '/' rather than use :T to
182# avoid collisions.
183DEPEND_FILTER=	C,/,_,g
184DEPENDSRCS=	${SRCS:M*.[cSC]} ${SRCS:M*.cxx} ${SRCS:M*.cpp} ${SRCS:M*.cc}
185.if !empty(DEPENDSRCS)
186DEPENDOBJS+=	${DEPENDSRCS:R:S,$,.o,}
187.endif
188DEPENDFILES_OBJS=	${DEPENDOBJS:O:u:${DEPEND_FILTER}:C/^/${DEPENDFILE}./}
189DEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF${DEPENDFILE}.${.TARGET:${DEPEND_FILTER}}
190DEPEND_CFLAGS+=	-MT${.TARGET}
191# Skip generating or including .depend.* files if in meta+filemon mode since
192# it will track dependencies itself.  OBJS_DEPEND_GUESS is still used though.
193.if !defined(_meta_filemon)
194.if defined(.PARSEDIR)
195# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
196# as those are the only ones we will include.
197DEPEND_CFLAGS_CONDITION= !empty(DEPENDOBJS:M${.TARGET:${DEPEND_FILTER}})
198CFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
199.else
200CFLAGS+=	${DEPEND_CFLAGS}
201.endif
202.if !defined(_SKIP_READ_DEPEND)
203.for __depend_obj in ${DEPENDFILES_OBJS}
204.sinclude "${__depend_obj}"
205.endfor
206.endif	# !defined(_SKIP_READ_DEPEND)
207.endif	# !defined(_meta_filemon)
208.endif	# ${MK_FAST_DEPEND} == "yes"
209.endif	# defined(SRCS)
210
211.if ${MK_DIRDEPS_BUILD} == "yes"
212.include <meta.autodep.mk>
213# If using filemon then _EXTRADEPEND is skipped since it is not needed.
214.if empty(.MAKE.MODE:Mnofilemon)
215# this depend: bypasses that below
216# the dependency helps when bootstrapping
217depend: beforedepend ${DPSRCS} ${SRCS} afterdepend
218beforedepend:
219afterdepend: beforedepend
220.endif
221.endif
222
223# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
224# For meta+filemon the .meta file is checked for since it is the dependency
225# file used.
226.if ${MK_FAST_DEPEND} == "yes"
227.for __obj in ${DEPENDOBJS:O:u}
228.if (defined(_meta_filemon) && !exists(${.OBJDIR}/${__obj}.meta)) || \
229    (!defined(_meta_filemon) && !exists(${.OBJDIR}/${DEPENDFILE}.${__obj}))
230${__obj}: ${OBJS_DEPEND_GUESS}
231${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
232.endif
233.endfor
234.endif
235
236.if !target(depend)
237.if defined(SRCS)
238depend: beforedepend ${DEPENDFILE} afterdepend
239
240# Tell bmake not to look for generated files via .PATH
241.NOPATH: ${DEPENDFILE} ${DEPENDFILES_OBJS}
242
243.if ${MK_FAST_DEPEND} == "no"
244# Capture -include from CFLAGS.
245# This could be simpler with bmake :tW but needs to support fmake for MFC.
246_CFLAGS_INCLUDES= ${CFLAGS:Q:S/\\ /,/g:C/-include,/-include%/g:C/,/ /g:M-include*:C/%/ /g}
247_CXXFLAGS_INCLUDES= ${CXXFLAGS:Q:S/\\ /,/g:C/-include,/-include%/g:C/,/ /g:M-include*:C/%/ /g}
248
249# Different types of sources are compiled with slightly different flags.
250# Split up the sources, and filter out headers and non-applicable flags.
251MKDEP_CFLAGS=	${CFLAGS:M-nostdinc*} ${CFLAGS:M-[BIDU]*} ${CFLAGS:M-std=*} \
252		${CFLAGS:M-ansi} ${_CFLAGS_INCLUDES}
253MKDEP_CXXFLAGS=	${CXXFLAGS:M-nostdinc*} ${CXXFLAGS:M-[BIDU]*} \
254		${CXXFLAGS:M-std=*} ${CXXFLAGS:M-ansi} ${CXXFLAGS:M-stdlib=*} \
255		${_CXXFLAGS_INCLUDES}
256.endif	# ${MK_FAST_DEPEND} == "no"
257
258DPSRCS+= ${SRCS}
259# FAST_DEPEND will only generate a .depend if _EXTRADEPEND is used but
260# the target is created to allow 'make depend' to generate files.
261${DEPENDFILE}: ${DPSRCS}
262.if ${MK_FAST_DEPEND} == "no"
263	rm -f ${DEPENDFILE}
264.if !empty(DPSRCS:M*.[cS])
265	${MKDEPCMD} -f ${DEPENDFILE} -a ${MKDEP} \
266	    ${MKDEP_CFLAGS} ${.ALLSRC:M*.[cS]}
267.endif
268.if !empty(DPSRCS:M*.cc) || !empty(DPSRCS:M*.C) || !empty(DPSRCS:M*.cpp) || \
269    !empty(DPSRCS:M*.cxx)
270	${MKDEPCMD} -f ${DEPENDFILE} -a ${MKDEP} \
271	    ${MKDEP_CXXFLAGS} \
272	    ${.ALLSRC:M*.cc} ${.ALLSRC:M*.C} ${.ALLSRC:M*.cpp} ${.ALLSRC:M*.cxx}
273.else
274.endif
275.endif	# ${MK_FAST_DEPEND} == "no"
276.if target(_EXTRADEPEND)
277_EXTRADEPEND: .USE
278${DEPENDFILE}: _EXTRADEPEND
279.endif
280
281.ORDER: ${DEPENDFILE} afterdepend
282.else
283depend: beforedepend afterdepend
284.endif
285.if !target(beforedepend)
286beforedepend:
287.else
288.ORDER: beforedepend ${DEPENDFILE}
289.ORDER: beforedepend afterdepend
290.endif
291.if !target(afterdepend)
292afterdepend:
293.endif
294.endif
295
296.if defined(SRCS)
297.if ${CTAGS:T} == "gtags"
298CLEANDEPENDFILES+=	GPATH GRTAGS GSYMS GTAGS
299.if defined(HTML)
300CLEANDEPENDDIRS+=	HTML
301.endif
302.else
303CLEANDEPENDFILES+=	tags
304.endif
305.endif
306.if !target(cleandepend)
307cleandepend:
308.if !empty(CLEANDEPENDFILES)
309	rm -f ${CLEANDEPENDFILES}
310.endif
311.if !empty(CLEANDEPENDDIRS)
312	rm -rf ${CLEANDEPENDDIRS}
313.endif
314.endif
315
316.if !target(checkdpadd) && (defined(DPADD) || defined(LDADD))
317_LDADD_FROM_DPADD=	${DPADD:R:T:C;^lib(.*)$;-l\1;g}
318# Ignore -Wl,--start-group/-Wl,--end-group as it might be required in the
319# LDADD list due to unresolved symbols
320_LDADD_CANONICALIZED=	${LDADD:N:R:T:C;^lib(.*)$;-l\1;g:N-Wl,--[es]*-group}
321checkdpadd:
322.if ${_LDADD_FROM_DPADD} != ${_LDADD_CANONICALIZED}
323	@echo ${.CURDIR}
324	@echo "DPADD -> ${_LDADD_FROM_DPADD}"
325	@echo "LDADD -> ${_LDADD_CANONICALIZED}"
326.endif
327.endif
328