xref: /freebsd/share/mk/bsd.dep.mk (revision a0ee8cc6)
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" && ${.MAKE.MODE:Unormal:Mmeta*} == ""
154DEPENDFILES+=	${DEPENDFILE}.*
155DEPEND_MP?=	-MP
156# Handle OBJS=../somefile.o hacks.  Just replace '/' rather than use :T to
157# avoid collisions.
158DEPEND_FILTER=	C,/,_,g
159DEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF${DEPENDFILE}.${.TARGET:${DEPEND_FILTER}}
160DEPEND_CFLAGS+=	-MT${.TARGET}
161.if defined(.PARSEDIR)
162# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
163# as those are the only ones we will include.
164DEPEND_CFLAGS_CONDITION= !empty(DEPENDOBJS:M${.TARGET:${DEPEND_FILTER}})
165CFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
166.else
167CFLAGS+=	${DEPEND_CFLAGS}
168.endif
169DEPENDSRCS=	${SRCS:M*.[cSC]} ${SRCS:M*.cxx} ${SRCS:M*.cpp} ${SRCS:M*.cc}
170.if !empty(DEPENDSRCS)
171DEPENDOBJS+=	${DEPENDSRCS:R:S,$,.o,}
172.endif
173DEPENDFILES_OBJS=	${DEPENDOBJS:O:u:${DEPEND_FILTER}:C/^/${DEPENDFILE}./}
174.if ${.MAKEFLAGS:M-V} == ""
175.for __depend_obj in ${DEPENDFILES_OBJS}
176.sinclude "${__depend_obj}"
177.endfor
178.endif
179.endif	# ${MK_FAST_DEPEND} == "yes"
180.endif	# defined(SRCS)
181
182.if ${MK_DIRDEPS_BUILD} == "yes"
183.include <meta.autodep.mk>
184# this depend: bypasses that below
185# the dependency helps when bootstrapping
186depend: beforedepend ${DPSRCS} ${SRCS} afterdepend
187beforedepend:
188afterdepend: beforedepend
189.endif
190
191.if !target(depend)
192.if defined(SRCS)
193depend: beforedepend ${DEPENDFILE} afterdepend
194
195# Tell bmake not to look for generated files via .PATH
196.NOPATH: ${DEPENDFILE} ${DEPENDFILES_OBJS}
197
198# Different types of sources are compiled with slightly different flags.
199# Split up the sources, and filter out headers and non-applicable flags.
200MKDEP_CFLAGS=	${CFLAGS:M-nostdinc*} ${CFLAGS:M-[BIDU]*} ${CFLAGS:M-std=*} \
201		${CFLAGS:M-ansi}
202MKDEP_CXXFLAGS=	${CXXFLAGS:M-nostdinc*} ${CXXFLAGS:M-[BIDU]*} \
203		${CXXFLAGS:M-std=*} ${CXXFLAGS:M-ansi} ${CXXFLAGS:M-stdlib=*}
204
205DPSRCS+= ${SRCS}
206${DEPENDFILE}: ${DPSRCS}
207.if ${MK_FAST_DEPEND} == "no"
208	rm -f ${DEPENDFILE}
209.if !empty(DPSRCS:M*.[cS])
210	${MKDEPCMD} -f ${DEPENDFILE} -a ${MKDEP} \
211	    ${MKDEP_CFLAGS} ${.ALLSRC:M*.[cS]}
212.endif
213.if !empty(DPSRCS:M*.cc) || !empty(DPSRCS:M*.C) || !empty(DPSRCS:M*.cpp) || \
214    !empty(DPSRCS:M*.cxx)
215	${MKDEPCMD} -f ${DEPENDFILE} -a ${MKDEP} \
216	    ${MKDEP_CXXFLAGS} \
217	    ${.ALLSRC:M*.cc} ${.ALLSRC:M*.C} ${.ALLSRC:M*.cpp} ${.ALLSRC:M*.cxx}
218.else
219.endif
220.else
221	: > ${.TARGET}
222.endif	# ${MK_FAST_DEPEND} == "no"
223.if target(_EXTRADEPEND)
224_EXTRADEPEND: .USE
225${DEPENDFILE}: _EXTRADEPEND
226.endif
227
228.ORDER: ${DEPENDFILE} afterdepend
229.else
230depend: beforedepend afterdepend
231.endif
232.if !target(beforedepend)
233beforedepend:
234.else
235.ORDER: beforedepend ${DEPENDFILE}
236.ORDER: beforedepend afterdepend
237.endif
238.if !target(afterdepend)
239afterdepend:
240.endif
241.endif
242
243.if !target(cleandepend)
244cleandepend:
245.if defined(SRCS)
246.if ${CTAGS:T} == "gtags"
247	rm -f ${DEPENDFILES} GPATH GRTAGS GSYMS GTAGS
248.if defined(HTML)
249	rm -rf HTML
250.endif
251.else
252	rm -f ${DEPENDFILES} tags
253.endif
254.endif
255.endif
256
257.if !target(checkdpadd) && (defined(DPADD) || defined(LDADD))
258_LDADD_FROM_DPADD=	${DPADD:R:T:C;^lib(.*)$;-l\1;g}
259# Ignore -Wl,--start-group/-Wl,--end-group as it might be required in the
260# LDADD list due to unresolved symbols
261_LDADD_CANONICALIZED=	${LDADD:N:R:T:C;^lib(.*)$;-l\1;g:N-Wl,--[es]*-group}
262checkdpadd:
263.if ${_LDADD_FROM_DPADD} != ${_LDADD_CANONICALIZED}
264	@echo ${.CURDIR}
265	@echo "DPADD -> ${_LDADD_FROM_DPADD}"
266	@echo "LDADD -> ${_LDADD_CANONICALIZED}"
267.endif
268.endif
269