xref: /freebsd/share/mk/bsd.dep.mk (revision 799483e49ea9412cd4d87a33a024bc3ab3f21e9d)
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# SRCS          List of source files (c, c++, assembler)
24#
25# DPSRCS	List of source files which are needed for generating
26#		dependencies, ${SRCS} are always part of it.
27#
28# +++ targets +++
29#
30#	cleandepend:
31#		remove ${CLEANDEPENDFILES}; remove ${CLEANDEPENDDIRS} and all
32#		contents.
33#
34#	depend:
35#		Make the dependencies for the source files, and store
36#		them in the file ${DEPENDFILE}.
37#
38#	tags:
39#		In "ctags" mode, create a tags file for the source files.
40#		In "gtags" mode, create a (GLOBAL) gtags file for the
41#		source files.  If HTML is defined, htags(1) is also run
42#		after gtags(1).
43
44.if !target(__<bsd.init.mk>__)
45.error bsd.dep.mk cannot be included directly.
46.endif
47
48CTAGS?=		gtags
49CTAGSFLAGS?=
50GTAGSFLAGS?=	-o
51HTAGSFLAGS?=
52
53.if ${MK_DIRDEPS_BUILD} == "no"
54.MAKE.DEPENDFILE= ${DEPENDFILE}
55.endif
56CLEANDEPENDFILES+=	${DEPENDFILE} ${DEPENDFILE}.*
57.if ${MK_META_MODE} == "yes"
58CLEANDEPENDFILES+=	*.meta
59.endif
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 !empty(.MAKE.MODE:Mmeta) && empty(.MAKE.MODE:Mnofilemon)
76_meta_filemon=	1
77.endif
78
79# Skip reading .depend when not needed to speed up tree-walks
80# and simple lookups.
81# Also skip generating or including .depend.* files if in meta+filemon mode
82# since it will track dependencies itself.  OBJS_DEPEND_GUESS is still used.
83.if !empty(.MAKEFLAGS:M-V${_V_READ_DEPEND}) || make(obj) || make(clean*) || \
84    make(install*) || make(analyze) || defined(_meta_filemon)
85_SKIP_READ_DEPEND=	1
86.if ${MK_DIRDEPS_BUILD} == "no"
87.MAKE.DEPENDFILE=	/dev/null
88.endif
89.endif
90
91.if defined(SRCS)
92CLEANFILES?=
93
94.for _S in ${SRCS:N*.[dhly]}
95OBJS_DEPEND_GUESS.${_S:R}.o+=	${_S}
96.endfor
97
98# Lexical analyzers
99.for _LSRC in ${SRCS:M*.l:N*/*}
100.for _LC in ${_LSRC:R}.c
101${_LC}: ${_LSRC}
102	${LEX} ${LFLAGS} -o${.TARGET} ${.ALLSRC}
103OBJS_DEPEND_GUESS.${_LC:R}.o+=	${_LC}
104SRCS:=	${SRCS:S/${_LSRC}/${_LC}/}
105CLEANFILES+= ${_LC}
106.endfor
107.endfor
108
109# Yacc grammars
110.for _YSRC in ${SRCS:M*.y:N*/*}
111.for _YC in ${_YSRC:R}.c
112SRCS:=	${SRCS:S/${_YSRC}/${_YC}/}
113CLEANFILES+= ${_YC}
114.if !empty(YFLAGS:M-d) && !empty(SRCS:My.tab.h)
115.ORDER: ${_YC} y.tab.h
116y.tab.h: .NOMETA
117${_YC} y.tab.h: ${_YSRC}
118	${YACC} ${YFLAGS} ${.ALLSRC}
119	cp y.tab.c ${_YC}
120CLEANFILES+= y.tab.c y.tab.h
121.elif !empty(YFLAGS:M-d)
122.for _YH in ${_YC:R}.h
123.ORDER: ${_YC} ${_YH}
124${_YH}: .NOMETA
125${_YC} ${_YH}: ${_YSRC}
126	${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
127SRCS+=	${_YH}
128CLEANFILES+= ${_YH}
129.endfor
130.else
131${_YC}: ${_YSRC}
132	${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
133.endif
134OBJS_DEPEND_GUESS.${_YC:R}.o+=	${_YC}
135.endfor
136.endfor
137
138# DTrace probe definitions
139.if ${SRCS:M*.d}
140CFLAGS+=	-I${.OBJDIR}
141.endif
142.for _DSRC in ${SRCS:M*.d:N*/*}
143.for _D in ${_DSRC:R}
144SRCS+=	${_D}.h
145${_D}.h: ${_DSRC}
146	${DTRACE} ${DTRACEFLAGS} -h -s ${.ALLSRC}
147SRCS:=	${SRCS:S/^${_DSRC}$//}
148OBJS+=	${_D}.o
149CLEANFILES+= ${_D}.h ${_D}.o
150${_D}.o: ${_DSRC} ${OBJS:S/^${_D}.o$//}
151	@rm -f ${.TARGET}
152	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
153.if defined(LIB)
154CLEANFILES+= ${_D}.So ${_D}.po
155${_D}.So: ${_DSRC} ${SOBJS:S/^${_D}.So$//}
156	@rm -f ${.TARGET}
157	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
158${_D}.po: ${_DSRC} ${POBJS:S/^${_D}.po$//}
159	@rm -f ${.TARGET}
160	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
161.endif
162.endfor
163.endfor
164
165
166.if ${MAKE_VERSION} < 20160220
167DEPEND_MP?=	-MP
168.endif
169# Handle OBJS=../somefile.o hacks.  Just replace '/' rather than use :T to
170# avoid collisions.
171DEPEND_FILTER=	C,/,_,g
172DEPENDSRCS=	${SRCS:M*.[cSC]} ${SRCS:M*.cxx} ${SRCS:M*.cpp} ${SRCS:M*.cc}
173.if !empty(DEPENDSRCS)
174DEPENDOBJS+=	${DEPENDSRCS:R:S,$,.o,}
175.endif
176DEPENDFILES_OBJS=	${DEPENDOBJS:O:u:${DEPEND_FILTER}:C/^/${DEPENDFILE}./}
177DEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF${DEPENDFILE}.${.TARGET:${DEPEND_FILTER}}
178DEPEND_CFLAGS+=	-MT${.TARGET}
179.if !defined(_meta_filemon)
180.if defined(.PARSEDIR)
181# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
182# as those are the only ones we will include.
183DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:M${.TARGET:${DEPEND_FILTER}}}" != ""
184CFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
185.else
186CFLAGS+=	${DEPEND_CFLAGS}
187.endif
188.if !defined(_SKIP_READ_DEPEND)
189.for __depend_obj in ${DEPENDFILES_OBJS}
190.if ${MAKE_VERSION} < 20160220
191.sinclude "${.OBJDIR}/${__depend_obj}"
192.else
193.dinclude "${.OBJDIR}/${__depend_obj}"
194.endif
195.endfor
196.endif	# !defined(_SKIP_READ_DEPEND)
197.endif	# !defined(_meta_filemon)
198.endif	# defined(SRCS)
199
200.if ${MK_DIRDEPS_BUILD} == "yes"
201# Prevent meta.autodep.mk from tracking "local dependencies".
202.depend:
203.include <meta.autodep.mk>
204# If using filemon then _EXTRADEPEND is skipped since it is not needed.
205.if defined(_meta_filemon)
206# this depend: bypasses that below
207# the dependency helps when bootstrapping
208depend: beforedepend ${DPSRCS} ${SRCS} afterdepend
209beforedepend:
210afterdepend: beforedepend
211.endif
212.endif
213
214# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
215# For meta+filemon the .meta file is checked for since it is the dependency
216# file used.
217.for __obj in ${DEPENDOBJS:O:u}
218.if (defined(_meta_filemon) && !exists(${.OBJDIR}/${__obj}.meta)) || \
219    (!defined(_meta_filemon) && !exists(${.OBJDIR}/${DEPENDFILE}.${__obj}))
220${__obj}: ${OBJS_DEPEND_GUESS}
221${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
222.elif defined(_meta_filemon)
223# For meta mode we still need to know which file to depend on to avoid
224# ambiguous suffix transformation rules from .PATH.  Meta mode does not
225# use .depend files.  We really only need source files, not headers since
226# they are typically in SRCS/beforebuild already.  For target-specific
227# guesses do include headers though since they may not be in SRCS.
228${__obj}: ${OBJS_DEPEND_GUESS:N*.h}
229${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
230.endif
231.endfor
232
233# Always run 'make depend' to generate dependencies early and to avoid the
234# need for manually running it.  The dirdeps build should only do this in
235# sub-makes though since MAKELEVEL0 is for dirdeps calculations.
236.if ${MK_DIRDEPS_BUILD} == "no" || ${.MAKE.LEVEL} > 0
237beforebuild: depend
238.endif
239
240.if !target(depend)
241.if defined(SRCS)
242depend: beforedepend ${DEPENDFILE} afterdepend
243
244# Tell bmake not to look for generated files via .PATH
245.NOPATH: ${DEPENDFILE} ${DEPENDFILES_OBJS}
246
247DPSRCS+= ${SRCS}
248# A .depend file will only be generated if there are commands in
249# beforedepend/_EXTRADEPEND/afterdepend  The _EXTRADEPEND target is
250# ignored if using meta+filemon since it handles all dependencies.  The other
251# targets are kept as they be used for generating something.  The target is
252# kept to allow 'make depend' to generate files.
253${DEPENDFILE}: ${DPSRCS}
254.if exists(${.OBJDIR}/${DEPENDFILE}) || \
255    ((commands(beforedepend) || \
256    (!defined(_meta_filemon) && commands(_EXTRADEPEND)) || \
257    commands(afterdepend)) && !empty(.MAKE.MODE:Mmeta))
258	rm -f ${DEPENDFILE}
259.endif
260.if !defined(_meta_filemon) && target(_EXTRADEPEND)
261_EXTRADEPEND: .USE
262${DEPENDFILE}: _EXTRADEPEND
263.endif
264
265.ORDER: ${DEPENDFILE} afterdepend
266.else
267depend: beforedepend afterdepend
268.endif
269.if !target(beforedepend)
270beforedepend:
271.else
272.ORDER: beforedepend ${DEPENDFILE}
273.ORDER: beforedepend afterdepend
274.endif
275.if !target(afterdepend)
276afterdepend:
277.endif
278.endif
279
280.if defined(SRCS)
281.if ${CTAGS:T} == "gtags"
282CLEANDEPENDFILES+=	GPATH GRTAGS GSYMS GTAGS
283.if defined(HTML)
284CLEANDEPENDDIRS+=	HTML
285.endif
286.else
287CLEANDEPENDFILES+=	tags
288.endif
289.endif
290.if !target(cleandepend)
291cleandepend:
292.if !empty(CLEANDEPENDFILES)
293	rm -f ${CLEANDEPENDFILES}
294.endif
295.if !empty(CLEANDEPENDDIRS)
296	rm -rf ${CLEANDEPENDDIRS}
297.endif
298.endif
299
300.if !target(checkdpadd) && (defined(DPADD) || defined(LDADD))
301_LDADD_FROM_DPADD=	${DPADD:R:T:C;^lib(.*)$;-l\1;g}
302# Ignore -Wl,--start-group/-Wl,--end-group as it might be required in the
303# LDADD list due to unresolved symbols
304_LDADD_CANONICALIZED=	${LDADD:N:R:T:C;^lib(.*)$;-l\1;g:N-Wl,--[es]*-group}
305checkdpadd:
306.if ${_LDADD_FROM_DPADD} != ${_LDADD_CANONICALIZED}
307	@echo ${.CURDIR}
308	@echo "DPADD -> ${_LDADD_FROM_DPADD}"
309	@echo "LDADD -> ${_LDADD_CANONICALIZED}"
310.endif
311.endif
312