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