xref: /freebsd/share/mk/bsd.dep.mk (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1# $FreeBSD$
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.ORDER: ${_YC} ${_YH}
114${_YC} ${_YH}: ${_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_hdrincludes=${_CFLAGS_INCLUDES:M*.h} ${_CXXFLAGS_INCLUDES:M*.h}
210.for _hdr in ${_hdrincludes:O:u}
211.if exists(${_hdr})
212${OBJS} ${POBJS} ${SOBJS}: ${_hdr}
213.endif
214.endfor
215.undef _hdrincludes
216
217# Different types of sources are compiled with slightly different flags.
218# Split up the sources, and filter out headers and non-applicable flags.
219MKDEP_CFLAGS=	${CFLAGS:M-nostdinc*} ${CFLAGS:M-[BIDU]*} ${CFLAGS:M-std=*} \
220		${CFLAGS:M-ansi} ${_CFLAGS_INCLUDES}
221MKDEP_CXXFLAGS=	${CXXFLAGS:M-nostdinc*} ${CXXFLAGS:M-[BIDU]*} \
222		${CXXFLAGS:M-std=*} ${CXXFLAGS:M-ansi} ${CXXFLAGS:M-stdlib=*} \
223		${_CXXFLAGS_INCLUDES}
224.endif	# ${MK_FAST_DEPEND} == "no"
225
226DPSRCS+= ${SRCS}
227${DEPENDFILE}: ${DPSRCS}
228.if ${MK_FAST_DEPEND} == "no"
229	rm -f ${DEPENDFILE}
230.if !empty(DPSRCS:M*.[cS])
231	${MKDEPCMD} -f ${DEPENDFILE} -a ${MKDEP} \
232	    ${MKDEP_CFLAGS} ${.ALLSRC:M*.[cS]}
233.endif
234.if !empty(DPSRCS:M*.cc) || !empty(DPSRCS:M*.C) || !empty(DPSRCS:M*.cpp) || \
235    !empty(DPSRCS:M*.cxx)
236	${MKDEPCMD} -f ${DEPENDFILE} -a ${MKDEP} \
237	    ${MKDEP_CXXFLAGS} \
238	    ${.ALLSRC:M*.cc} ${.ALLSRC:M*.C} ${.ALLSRC:M*.cpp} ${.ALLSRC:M*.cxx}
239.else
240.endif
241.else
242	: > ${.TARGET}
243.endif	# ${MK_FAST_DEPEND} == "no"
244.if target(_EXTRADEPEND)
245_EXTRADEPEND: .USE
246${DEPENDFILE}: _EXTRADEPEND
247.endif
248
249.ORDER: ${DEPENDFILE} afterdepend
250.else
251depend: beforedepend afterdepend
252.endif
253.if !target(beforedepend)
254beforedepend:
255.else
256.ORDER: beforedepend ${DEPENDFILE}
257.ORDER: beforedepend afterdepend
258.endif
259.if !target(afterdepend)
260afterdepend:
261.endif
262.endif
263
264.if !target(cleandepend)
265cleandepend:
266.if defined(SRCS)
267.if ${CTAGS:T} == "gtags"
268	rm -f ${DEPENDFILES} GPATH GRTAGS GSYMS GTAGS
269.if defined(HTML)
270	rm -rf HTML
271.endif
272.else
273	rm -f ${DEPENDFILES} tags
274.endif
275.endif
276.endif
277
278.if !target(checkdpadd) && (defined(DPADD) || defined(LDADD))
279_LDADD_FROM_DPADD=	${DPADD:R:T:C;^lib(.*)$;-l\1;g}
280# Ignore -Wl,--start-group/-Wl,--end-group as it might be required in the
281# LDADD list due to unresolved symbols
282_LDADD_CANONICALIZED=	${LDADD:N:R:T:C;^lib(.*)$;-l\1;g:N-Wl,--[es]*-group}
283checkdpadd:
284.if ${_LDADD_FROM_DPADD} != ${_LDADD_CANONICALIZED}
285	@echo ${.CURDIR}
286	@echo "DPADD -> ${_LDADD_FROM_DPADD}"
287	@echo "LDADD -> ${_LDADD_CANONICALIZED}"
288.endif
289.endif
290