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