xref: /freebsd/share/mk/bsd.dep.mk (revision c501d73c7e2d5c843583084b84bd3e6f68a0047e)
1# $FreeBSD$
2#
3# The include file <bsd.dep.mk> handles Makefile dependencies.
4#
5#
6# +++ variables +++
7#
8# CLEANDEPENDDIRS	Additional directories to remove for the cleandepend
9# 			target.
10#
11# CLEANDEPENDFILES	Additional files to remove for the cleandepend target.
12#
13# CTAGS		A tags file generation program [gtags]
14#
15# CTAGSFLAGS	Options for ctags(1) [not set]
16#
17# DEPENDFILE	dependencies file [.depend]
18#
19# GTAGSFLAGS	Options for gtags(1) [-o]
20#
21# HTAGSFLAGS	Options for htags(1) [not set]
22#
23# MKDEP		Options for ${MKDEPCMD} [not set]
24#
25# MKDEPCMD	Makefile dependency list program [mkdep]
26#
27# SRCS          List of source files (c, c++, assembler)
28#
29# DPSRCS	List of source files which are needed for generating
30#		dependencies, ${SRCS} are always part of it.
31#
32# +++ targets +++
33#
34#	cleandepend:
35#		remove ${CLEANDEPENDFILES}; remove ${CLEANDEPENDDIRS} and all
36#		contents.
37#
38#	depend:
39#		Make the dependencies for the source files, and store
40#		them in the file ${DEPENDFILE}.
41#
42#	tags:
43#		In "ctags" mode, create a tags file for the source files.
44#		In "gtags" mode, create a (GLOBAL) gtags file for the
45#		source files.  If HTML is defined, htags(1) is also run
46#		after gtags(1).
47
48.if !target(__<bsd.init.mk>__)
49.error bsd.dep.mk cannot be included directly.
50.endif
51
52CTAGS?=		gtags
53CTAGSFLAGS?=
54GTAGSFLAGS?=	-o
55HTAGSFLAGS?=
56
57_MKDEPCC:=	${CC:N${CCACHE_BIN}}
58# XXX: DEPFLAGS can come out once Makefile.inc1 properly passes down
59# CXXFLAGS.
60.if !empty(DEPFLAGS)
61_MKDEPCC+=	${DEPFLAGS}
62.endif
63MKDEPCMD?=	CC='${_MKDEPCC}' mkdep
64DEPENDFILE?=	.depend
65.if ${MK_DIRDEPS_BUILD} == "no"
66.MAKE.DEPENDFILE= ${DEPENDFILE}
67.endif
68CLEANDEPENDFILES=	${DEPENDFILE} ${DEPENDFILE}.*
69
70# Keep `tags' here, before SRCS are mangled below for `depend'.
71.if !target(tags) && defined(SRCS) && !defined(NO_TAGS)
72tags: ${SRCS}
73.if ${CTAGS:T} == "gtags"
74	@cd ${.CURDIR} && ${CTAGS} ${GTAGSFLAGS} ${.OBJDIR}
75.if defined(HTML)
76	@cd ${.CURDIR} && htags ${HTAGSFLAGS} -d ${.OBJDIR} ${.OBJDIR}
77.endif
78.else
79	@${CTAGS} ${CTAGSFLAGS} -f /dev/stdout \
80	    ${.ALLSRC:N*.h} | sed "s;${.CURDIR}/;;" > ${.TARGET}
81.endif
82.endif
83
84# Skip reading .depend when not needed to speed up tree-walks
85# and simple lookups.
86.if !empty(.MAKEFLAGS:M-V${_V_READ_DEPEND}) || make(obj) || make(clean*) || \
87    make(install*)
88_SKIP_READ_DEPEND=	1
89.if ${MK_DIRDEPS_BUILD} == "no"
90.MAKE.DEPENDFILE=	/dev/null
91.endif
92.endif
93
94.if defined(SRCS)
95CLEANFILES?=
96
97.for _S in ${SRCS:N*.[dhly]}
98OBJS_DEPEND_GUESS.${_S:R}.o=	${_S}
99.if ${MK_FAST_DEPEND} == "no" && !exists(${.OBJDIR}/${DEPENDFILE})
100${_S:R}.o: ${OBJS_DEPEND_GUESS.${_S:R}.o}
101.endif
102.endfor
103
104# Lexical analyzers
105.for _LSRC in ${SRCS:M*.l:N*/*}
106.for _LC in ${_LSRC:R}.c
107${_LC}: ${_LSRC}
108	${LEX} ${LFLAGS} -o${.TARGET} ${.ALLSRC}
109OBJS_DEPEND_GUESS.${_LC:R}.o=	${_LC}
110.if ${MK_FAST_DEPEND} == "no" && !exists(${.OBJDIR}/${DEPENDFILE})
111${_LC:R}.o: ${OBJS_DEPEND_GUESS.${_LC:R}.o}
112.endif
113SRCS:=	${SRCS:S/${_LSRC}/${_LC}/}
114CLEANFILES+= ${_LC}
115.endfor
116.endfor
117
118# Yacc grammars
119.for _YSRC in ${SRCS:M*.y:N*/*}
120.for _YC in ${_YSRC:R}.c
121SRCS:=	${SRCS:S/${_YSRC}/${_YC}/}
122CLEANFILES+= ${_YC}
123.if !empty(YFLAGS:M-d) && !empty(SRCS:My.tab.h)
124.ORDER: ${_YC} y.tab.h
125${_YC} y.tab.h: ${_YSRC}
126	${YACC} ${YFLAGS} ${.ALLSRC}
127	cp y.tab.c ${_YC}
128CLEANFILES+= y.tab.c y.tab.h
129.elif !empty(YFLAGS:M-d)
130.for _YH in ${_YC:R}.h
131.ORDER: ${_YC} ${_YH}
132${_YC} ${_YH}: ${_YSRC}
133	${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
134SRCS+=	${_YH}
135CLEANFILES+= ${_YH}
136.endfor
137.else
138${_YC}: ${_YSRC}
139	${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
140.endif
141OBJS_DEPEND_GUESS.${_YC:R}.o=	${_YC}
142.if ${MK_FAST_DEPEND} == "no" && !exists(${.OBJDIR}/${DEPENDFILE})
143${_YC:R}.o: ${OBJS_DEPEND_GUESS.${_YC:R}.o}
144.endif
145.endfor
146.endfor
147
148# DTrace probe definitions
149.if ${SRCS:M*.d}
150CFLAGS+=	-I${.OBJDIR}
151.endif
152.for _DSRC in ${SRCS:M*.d:N*/*}
153.for _D in ${_DSRC:R}
154SRCS+=	${_D}.h
155${_D}.h: ${_DSRC}
156	${DTRACE} ${DTRACEFLAGS} -h -s ${.ALLSRC}
157SRCS:=	${SRCS:S/^${_DSRC}$//}
158OBJS+=	${_D}.o
159CLEANFILES+= ${_D}.h ${_D}.o
160${_D}.o: ${_DSRC} ${OBJS:S/^${_D}.o$//}
161	@rm -f ${.TARGET}
162	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
163.if defined(LIB)
164CLEANFILES+= ${_D}.So ${_D}.po
165${_D}.So: ${_DSRC} ${SOBJS:S/^${_D}.So$//}
166	@rm -f ${.TARGET}
167	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
168${_D}.po: ${_DSRC} ${POBJS:S/^${_D}.po$//}
169	@rm -f ${.TARGET}
170	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
171.endif
172.endfor
173.endfor
174
175
176.if !empty(.MAKE.MODE:Mmeta) && empty(.MAKE.MODE:Mnofilemon)
177_meta_filemon=	1
178.endif
179.if ${MK_FAST_DEPEND} == "yes"
180DEPEND_MP?=	-MP
181# Handle OBJS=../somefile.o hacks.  Just replace '/' rather than use :T to
182# avoid collisions.
183DEPEND_FILTER=	C,/,_,g
184DEPENDSRCS=	${SRCS:M*.[cSC]} ${SRCS:M*.cxx} ${SRCS:M*.cpp} ${SRCS:M*.cc}
185.if !empty(DEPENDSRCS)
186DEPENDOBJS+=	${DEPENDSRCS:R:S,$,.o,}
187.endif
188DEPENDFILES_OBJS=	${DEPENDOBJS:O:u:${DEPEND_FILTER}:C/^/${DEPENDFILE}./}
189DEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF${DEPENDFILE}.${.TARGET:${DEPEND_FILTER}}
190DEPEND_CFLAGS+=	-MT${.TARGET}
191# Skip generating or including .depend.* files if in meta+filemon mode since
192# it will track dependencies itself.  OBJS_DEPEND_GUESS is still used though.
193.if !defined(_meta_filemon)
194.if defined(.PARSEDIR)
195# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
196# as those are the only ones we will include.
197DEPEND_CFLAGS_CONDITION= !empty(DEPENDOBJS:M${.TARGET:${DEPEND_FILTER}})
198CFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
199.else
200CFLAGS+=	${DEPEND_CFLAGS}
201.endif
202.if !defined(_SKIP_READ_DEPEND)
203.for __depend_obj in ${DEPENDFILES_OBJS}
204.sinclude "${__depend_obj}"
205.endfor
206.endif	# !defined(_SKIP_READ_DEPEND)
207.endif	# !defined(_meta_filemon)
208.endif	# ${MK_FAST_DEPEND} == "yes"
209.endif	# defined(SRCS)
210
211.if ${MK_DIRDEPS_BUILD} == "yes"
212# Prevent meta.autodep.mk from tracking "local dependencies".
213.depend:
214.include <meta.autodep.mk>
215# If using filemon then _EXTRADEPEND is skipped since it is not needed.
216.if empty(.MAKE.MODE:Mnofilemon)
217# this depend: bypasses that below
218# the dependency helps when bootstrapping
219depend: beforedepend ${DPSRCS} ${SRCS} afterdepend
220beforedepend:
221afterdepend: beforedepend
222.endif
223.endif
224
225# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
226# For meta+filemon the .meta file is checked for since it is the dependency
227# file used.
228.if ${MK_FAST_DEPEND} == "yes"
229.for __obj in ${DEPENDOBJS:O:u}
230.if (defined(_meta_filemon) && !exists(${.OBJDIR}/${__obj}.meta)) || \
231    (!defined(_meta_filemon) && !exists(${.OBJDIR}/${DEPENDFILE}.${__obj}))
232${__obj}: ${OBJS_DEPEND_GUESS}
233${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
234.endif
235.endfor
236
237# Always run 'make depend' to generate dependencies early and to avoid the
238# need for manually running it.  The dirdeps build should only do this in
239# sub-makes though since MAKELEVEL0 is for dirdeps calculations.
240.if ${MK_DIRDEPS_BUILD} == "no" || ${.MAKE.LEVEL} > 0
241beforebuild: depend
242.endif
243.endif	# ${MK_FAST_DEPEND} == "yes"
244
245.if !target(depend)
246.if defined(SRCS)
247depend: beforedepend ${DEPENDFILE} afterdepend
248
249# Tell bmake not to look for generated files via .PATH
250.NOPATH: ${DEPENDFILE} ${DEPENDFILES_OBJS}
251
252.if ${MK_FAST_DEPEND} == "no"
253# Capture -include from CFLAGS.
254# This could be simpler with bmake :tW but needs to support fmake for MFC.
255_CFLAGS_INCLUDES= ${CFLAGS:Q:S/\\ /,/g:C/-include,/-include%/g:C/,/ /g:M-include*:C/%/ /g}
256_CXXFLAGS_INCLUDES= ${CXXFLAGS:Q:S/\\ /,/g:C/-include,/-include%/g:C/,/ /g:M-include*:C/%/ /g}
257
258# Different types of sources are compiled with slightly different flags.
259# Split up the sources, and filter out headers and non-applicable flags.
260MKDEP_CFLAGS=	${CFLAGS:M-nostdinc*} ${CFLAGS:M-[BIDU]*} ${CFLAGS:M-std=*} \
261		${CFLAGS:M-ansi} ${_CFLAGS_INCLUDES}
262MKDEP_CXXFLAGS=	${CXXFLAGS:M-nostdinc*} ${CXXFLAGS:M-[BIDU]*} \
263		${CXXFLAGS:M-std=*} ${CXXFLAGS:M-ansi} ${CXXFLAGS:M-stdlib=*} \
264		${_CXXFLAGS_INCLUDES}
265.endif	# ${MK_FAST_DEPEND} == "no"
266
267DPSRCS+= ${SRCS}
268# FAST_DEPEND will only generate a .depend if _EXTRADEPEND is used but
269# the target is created to allow 'make depend' to generate files.
270${DEPENDFILE}: ${DPSRCS}
271.if exists(${.OBJDIR}/${DEPENDFILE})
272	rm -f ${DEPENDFILE}
273.endif
274.if ${MK_FAST_DEPEND} == "no"
275.if !empty(DPSRCS:M*.[cS])
276	${MKDEPCMD} -f ${DEPENDFILE} -a ${MKDEP} \
277	    ${MKDEP_CFLAGS} ${.ALLSRC:M*.[cS]}
278.endif
279.if !empty(DPSRCS:M*.cc) || !empty(DPSRCS:M*.C) || !empty(DPSRCS:M*.cpp) || \
280    !empty(DPSRCS:M*.cxx)
281	${MKDEPCMD} -f ${DEPENDFILE} -a ${MKDEP} \
282	    ${MKDEP_CXXFLAGS} \
283	    ${.ALLSRC:M*.cc} ${.ALLSRC:M*.C} ${.ALLSRC:M*.cpp} ${.ALLSRC:M*.cxx}
284.else
285.endif
286.endif	# ${MK_FAST_DEPEND} == "no"
287.if target(_EXTRADEPEND)
288_EXTRADEPEND: .USE
289${DEPENDFILE}: _EXTRADEPEND
290.endif
291
292.ORDER: ${DEPENDFILE} afterdepend
293.else
294depend: beforedepend afterdepend
295.endif
296.if !target(beforedepend)
297beforedepend:
298.else
299.ORDER: beforedepend ${DEPENDFILE}
300.ORDER: beforedepend afterdepend
301.endif
302.if !target(afterdepend)
303afterdepend:
304.endif
305.endif
306
307.if defined(SRCS)
308.if ${CTAGS:T} == "gtags"
309CLEANDEPENDFILES+=	GPATH GRTAGS GSYMS GTAGS
310.if defined(HTML)
311CLEANDEPENDDIRS+=	HTML
312.endif
313.else
314CLEANDEPENDFILES+=	tags
315.endif
316.endif
317.if !target(cleandepend)
318cleandepend:
319.if !empty(CLEANDEPENDFILES)
320	rm -f ${CLEANDEPENDFILES}
321.endif
322.if !empty(CLEANDEPENDDIRS)
323	rm -rf ${CLEANDEPENDDIRS}
324.endif
325.endif
326
327.if !target(checkdpadd) && (defined(DPADD) || defined(LDADD))
328_LDADD_FROM_DPADD=	${DPADD:R:T:C;^lib(.*)$;-l\1;g}
329# Ignore -Wl,--start-group/-Wl,--end-group as it might be required in the
330# LDADD list due to unresolved symbols
331_LDADD_CANONICALIZED=	${LDADD:N:R:T:C;^lib(.*)$;-l\1;g:N-Wl,--[es]*-group}
332checkdpadd:
333.if ${_LDADD_FROM_DPADD} != ${_LDADD_CANONICALIZED}
334	@echo ${.CURDIR}
335	@echo "DPADD -> ${_LDADD_FROM_DPADD}"
336	@echo "LDADD -> ${_LDADD_CANONICALIZED}"
337.endif
338.endif
339