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