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