bsd.dep.mk revision 295779
1# $FreeBSD: head/share/mk/bsd.dep.mk 295779 2016-02-19 00:41:24Z 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.if ${MK_FAST_DEPEND} == "yes" || !exists(${.OBJDIR}/${DEPENDFILE})
98.for _S in ${SRCS:N*.[dhly]}
99${_S:R}.o: ${_S}
100.endfor
101.endif
102
103# Lexical analyzers
104.for _LSRC in ${SRCS:M*.l:N*/*}
105.for _LC in ${_LSRC:R}.c
106${_LC}: ${_LSRC}
107	${LEX} ${LFLAGS} -o${.TARGET} ${.ALLSRC}
108.if ${MK_FAST_DEPEND} == "yes" || !exists(${.OBJDIR}/${DEPENDFILE})
109${_LC:R}.o: ${_LC}
110.endif
111SRCS:=	${SRCS:S/${_LSRC}/${_LC}/}
112CLEANFILES+= ${_LC}
113.endfor
114.endfor
115
116# Yacc grammars
117.for _YSRC in ${SRCS:M*.y:N*/*}
118.for _YC in ${_YSRC:R}.c
119SRCS:=	${SRCS:S/${_YSRC}/${_YC}/}
120CLEANFILES+= ${_YC}
121.if !empty(YFLAGS:M-d) && !empty(SRCS:My.tab.h)
122.ORDER: ${_YC} y.tab.h
123${_YC} y.tab.h: ${_YSRC}
124	${YACC} ${YFLAGS} ${.ALLSRC}
125	cp y.tab.c ${_YC}
126CLEANFILES+= y.tab.c y.tab.h
127.elif !empty(YFLAGS:M-d)
128.for _YH in ${_YC:R}.h
129.ORDER: ${_YC} ${_YH}
130${_YC} ${_YH}: ${_YSRC}
131	${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
132SRCS+=	${_YH}
133CLEANFILES+= ${_YH}
134.endfor
135.else
136${_YC}: ${_YSRC}
137	${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
138.endif
139.if ${MK_FAST_DEPEND} == "yes" || !exists(${.OBJDIR}/${DEPENDFILE})
140${_YC:R}.o: ${_YC}
141.endif
142.endfor
143.endfor
144
145# DTrace probe definitions
146.if ${SRCS:M*.d}
147CFLAGS+=	-I${.OBJDIR}
148.endif
149.for _DSRC in ${SRCS:M*.d:N*/*}
150.for _D in ${_DSRC:R}
151SRCS+=	${_D}.h
152${_D}.h: ${_DSRC}
153	${DTRACE} ${DTRACEFLAGS} -h -s ${.ALLSRC}
154SRCS:=	${SRCS:S/^${_DSRC}$//}
155OBJS+=	${_D}.o
156CLEANFILES+= ${_D}.h ${_D}.o
157${_D}.o: ${_DSRC} ${OBJS:S/^${_D}.o$//}
158	@rm -f ${.TARGET}
159	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
160.if defined(LIB)
161CLEANFILES+= ${_D}.So ${_D}.po
162${_D}.So: ${_DSRC} ${SOBJS:S/^${_D}.So$//}
163	@rm -f ${.TARGET}
164	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
165${_D}.po: ${_DSRC} ${POBJS:S/^${_D}.po$//}
166	@rm -f ${.TARGET}
167	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
168.endif
169.endfor
170.endfor
171
172
173.if ${MK_FAST_DEPEND} == "yes" && \
174    (${.MAKE.MODE:Mmeta} == "" || ${.MAKE.MODE:Mnofilemon} != "")
175DEPEND_MP?=	-MP
176# Handle OBJS=../somefile.o hacks.  Just replace '/' rather than use :T to
177# avoid collisions.
178DEPEND_FILTER=	C,/,_,g
179DEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF${DEPENDFILE}.${.TARGET:${DEPEND_FILTER}}
180DEPEND_CFLAGS+=	-MT${.TARGET}
181.if defined(.PARSEDIR)
182# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
183# as those are the only ones we will include.
184DEPEND_CFLAGS_CONDITION= !empty(DEPENDOBJS:M${.TARGET:${DEPEND_FILTER}})
185CFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
186.else
187CFLAGS+=	${DEPEND_CFLAGS}
188.endif
189DEPENDSRCS=	${SRCS:M*.[cSC]} ${SRCS:M*.cxx} ${SRCS:M*.cpp} ${SRCS:M*.cc}
190.if !empty(DEPENDSRCS)
191DEPENDOBJS+=	${DEPENDSRCS:R:S,$,.o,}
192.endif
193DEPENDFILES_OBJS=	${DEPENDOBJS:O:u:${DEPEND_FILTER}:C/^/${DEPENDFILE}./}
194.if !defined(_SKIP_READ_DEPEND)
195.for __depend_obj in ${DEPENDFILES_OBJS}
196.sinclude "${__depend_obj}"
197.endfor
198.endif
199.endif	# ${MK_FAST_DEPEND} == "yes"
200.endif	# defined(SRCS)
201
202.if ${MK_DIRDEPS_BUILD} == "yes"
203.include <meta.autodep.mk>
204# this depend: bypasses that below
205# the dependency helps when bootstrapping
206depend: beforedepend ${DPSRCS} ${SRCS} afterdepend
207beforedepend:
208afterdepend: beforedepend
209.endif
210
211.if !target(depend)
212.if defined(SRCS)
213depend: beforedepend ${DEPENDFILE} afterdepend
214
215# Tell bmake not to look for generated files via .PATH
216.NOPATH: ${DEPENDFILE} ${DEPENDFILES_OBJS}
217
218.if ${MK_FAST_DEPEND} == "no"
219# Capture -include from CFLAGS.
220# This could be simpler with bmake :tW but needs to support fmake for MFC.
221_CFLAGS_INCLUDES= ${CFLAGS:Q:S/\\ /,/g:C/-include,/-include%/g:C/,/ /g:M-include*:C/%/ /g}
222_CXXFLAGS_INCLUDES= ${CXXFLAGS:Q:S/\\ /,/g:C/-include,/-include%/g:C/,/ /g:M-include*:C/%/ /g}
223
224# Different types of sources are compiled with slightly different flags.
225# Split up the sources, and filter out headers and non-applicable flags.
226MKDEP_CFLAGS=	${CFLAGS:M-nostdinc*} ${CFLAGS:M-[BIDU]*} ${CFLAGS:M-std=*} \
227		${CFLAGS:M-ansi} ${_CFLAGS_INCLUDES}
228MKDEP_CXXFLAGS=	${CXXFLAGS:M-nostdinc*} ${CXXFLAGS:M-[BIDU]*} \
229		${CXXFLAGS:M-std=*} ${CXXFLAGS:M-ansi} ${CXXFLAGS:M-stdlib=*} \
230		${_CXXFLAGS_INCLUDES}
231.endif	# ${MK_FAST_DEPEND} == "no"
232
233DPSRCS+= ${SRCS}
234${DEPENDFILE}: ${DPSRCS}
235.if ${MK_FAST_DEPEND} == "no"
236	rm -f ${DEPENDFILE}
237.if !empty(DPSRCS:M*.[cS])
238	${MKDEPCMD} -f ${DEPENDFILE} -a ${MKDEP} \
239	    ${MKDEP_CFLAGS} ${.ALLSRC:M*.[cS]}
240.endif
241.if !empty(DPSRCS:M*.cc) || !empty(DPSRCS:M*.C) || !empty(DPSRCS:M*.cpp) || \
242    !empty(DPSRCS:M*.cxx)
243	${MKDEPCMD} -f ${DEPENDFILE} -a ${MKDEP} \
244	    ${MKDEP_CXXFLAGS} \
245	    ${.ALLSRC:M*.cc} ${.ALLSRC:M*.C} ${.ALLSRC:M*.cpp} ${.ALLSRC:M*.cxx}
246.else
247.endif
248.else
249	: > ${.TARGET}
250.endif	# ${MK_FAST_DEPEND} == "no"
251.if target(_EXTRADEPEND)
252_EXTRADEPEND: .USE
253${DEPENDFILE}: _EXTRADEPEND
254.endif
255
256.ORDER: ${DEPENDFILE} afterdepend
257.else
258depend: beforedepend afterdepend
259.endif
260.if !target(beforedepend)
261beforedepend:
262.else
263.ORDER: beforedepend ${DEPENDFILE}
264.ORDER: beforedepend afterdepend
265.endif
266.if !target(afterdepend)
267afterdepend:
268.endif
269.endif
270
271.if ${CTAGS:T} == "gtags"
272CLEANDEPENDFILES+=	GPATH GRTAGS GSYMS GTAGS
273.if defined(HTML)
274CLEANDEPENDDIRS+=	HTML
275.endif
276.else
277CLEANDEPENDFILES+=	tags
278.endif
279.if !target(cleandepend)
280cleandepend:
281.if defined(SRCS)
282	rm -f ${CLEANDEPENDFILES}
283.if !empty(CLEANDEPENDDIRS)
284	rm -rf ${CLEANDEPENDDIRS}
285.endif
286.endif
287.endif
288
289.if !target(checkdpadd) && (defined(DPADD) || defined(LDADD))
290_LDADD_FROM_DPADD=	${DPADD:R:T:C;^lib(.*)$;-l\1;g}
291# Ignore -Wl,--start-group/-Wl,--end-group as it might be required in the
292# LDADD list due to unresolved symbols
293_LDADD_CANONICALIZED=	${LDADD:N:R:T:C;^lib(.*)$;-l\1;g:N-Wl,--[es]*-group}
294checkdpadd:
295.if ${_LDADD_FROM_DPADD} != ${_LDADD_CANONICALIZED}
296	@echo ${.CURDIR}
297	@echo "DPADD -> ${_LDADD_FROM_DPADD}"
298	@echo "LDADD -> ${_LDADD_CANONICALIZED}"
299.endif
300.endif
301