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