xref: /freebsd/share/mk/bsd.dep.mk (revision e796cc77c586c2955b2f3940dbf4991b31e8d289)
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# By default META_MODE is disabled in bmake if there is no OBJDIR
79# unless .MAKE.MODE contains "curdirOk=[^0nNfF]"
80.if defined(_meta_filemon) && ${.OBJDIR} == ${.CURDIR} && \
81    (empty(.MAKE.MODE:tl:Mcurdirok=*) || \
82    !empty(.MAKE.MODE:tl:Mcurdirok=[0NnFf]*))
83.undef _meta_filemon
84.endif
85
86# Skip reading .depend when not needed to speed up tree-walks and simple
87# lookups.  See _SKIP_BUILD logic in bsd.init.mk for more details.
88# Also skip generating or including .depend.* files if in meta+filemon mode
89# since it will track dependencies itself.  OBJS_DEPEND_GUESS is still used
90# for _meta_filemon but not for _SKIP_DEPEND.
91.if !defined(NO_SKIP_DEPEND) && defined(_SKIP_BUILD)
92_SKIP_DEPEND=	1
93.endif
94.if ${MK_DIRDEPS_BUILD} == "no"
95.if defined(_SKIP_DEPEND) || defined(_meta_filemon)
96.MAKE.DEPENDFILE=	/dev/null
97.endif
98.endif
99
100.if defined(SRCS)
101CLEANFILES?=
102
103.for _S in ${SRCS:N*.[dhly]}
104OBJS_DEPEND_GUESS.${_S:${OBJS_SRCS_FILTER:ts:}}.o+=	${_S}
105.endfor
106
107# Lexical analyzers
108.for _LSRC in ${SRCS:M*.l:N*/*}
109.for _LC in ${_LSRC:R}.c
110${_LC}: ${_LSRC}
111	${LEX} ${LFLAGS} -o${.TARGET} ${.ALLSRC}
112OBJS_DEPEND_GUESS.${_LC:R}.o+=	${_LC}
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
125y.tab.h: .NOMETA
126${_YC} y.tab.h: ${_YSRC}
127	${YACC} ${YFLAGS} ${.ALLSRC}
128	cp y.tab.c ${_YC}
129CLEANFILES+= y.tab.c y.tab.h
130.elif !empty(YFLAGS:M-d)
131.for _YH in ${_YC:R}.h
132.ORDER: ${_YC} ${_YH}
133${_YH}: .NOMETA
134${_YC} ${_YH}: ${_YSRC}
135	${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
136SRCS+=	${_YH}
137CLEANFILES+= ${_YH}
138.endfor
139.else
140${_YC}: ${_YSRC}
141	${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
142.endif
143OBJS_DEPEND_GUESS.${_YC:R}.o+=	${_YC}
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}.pico ${_D}.po
164${_D}.pico: ${_DSRC} ${SOBJS:S/^${_D}.pico$//}
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 ${MAKE_VERSION} < 20160220
176DEPEND_MP?=	-MP
177.endif
178# Handle OBJS=../somefile.o hacks.  Just replace '/' rather than use :T to
179# avoid collisions.
180DEPEND_FILTER=	C,/,_,g
181DEPENDSRCS+=	${SRCS:M*.[cSC]} ${SRCS:M*.cxx} ${SRCS:M*.cpp} ${SRCS:M*.cc}
182DEPENDSRCS+=	${DPSRCS:M*.[cSC]} ${SRCS:M*.cxx} ${SRCS:M*.cpp} ${SRCS:M*.cc}
183.if !empty(DEPENDSRCS)
184DEPENDOBJS+=	${DEPENDSRCS:${OBJS_SRCS_FILTER:ts:}:S,$,.o,}
185.endif
186DEPENDFILES+=	${DEPENDOBJS:O:u:${DEPEND_FILTER}:C/^/${DEPENDFILE}./}
187.if defined(_SKIP_DEPEND)
188# Don't bother statting any .meta files for .depend*
189${DEPENDOBJS}:	.NOMETA
190${DEPENDFILE}:	.NOMETA
191# Unset these to avoid looping/statting on them later.
192.undef DEPENDSRCS
193.undef DEPENDOBJS
194.undef DEPENDFILES
195.endif	# defined(_SKIP_DEPEND)
196DEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF${DEPENDFILE}.${.TARGET:${DEPEND_FILTER}}
197DEPEND_CFLAGS+=	-MT${.TARGET}
198.if !defined(_meta_filemon)
199.if !empty(DEPEND_CFLAGS)
200# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
201# as those are the only ones we will include.
202DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:${DEPEND_FILTER}:M${.TARGET:${DEPEND_FILTER}}}" != ""
203CFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
204.endif
205.for __depend_obj in ${DEPENDFILES}
206.if ${MAKE_VERSION} < 20160220
207.sinclude "${.OBJDIR}/${__depend_obj}"
208.else
209.dinclude "${.OBJDIR}/${__depend_obj}"
210.endif
211.endfor
212.endif	# !defined(_meta_filemon)
213.endif	# defined(SRCS)
214
215.if ${MK_DIRDEPS_BUILD} == "yes" && ${.MAKE.DEPENDFILE} != "/dev/null"
216# Prevent meta.autodep.mk from tracking "local dependencies".
217.depend:
218.include <meta.autodep.mk>
219# If using filemon then _EXTRADEPEND is skipped since it is not needed.
220.if defined(_meta_filemon)
221# this depend: bypasses that below
222# the dependency helps when bootstrapping
223depend: beforedepend ${DPSRCS} ${SRCS} afterdepend
224beforedepend:
225afterdepend: beforedepend
226.endif
227.endif
228
229# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
230# For meta+filemon the .meta file is checked for since it is the dependency
231# file used.
232.for __obj in ${DEPENDOBJS:O:u}
233# If the obj has any '/', then replace with '_'.  For meta files, this is
234# mimicing what bmake's meta_name() does and adding in the full path
235# as well to ensure that the expected meta file is read.
236.if ${__obj:M*/*}
237.if ${MAKE_VERSION} < 20171028
238_meta_obj=	${.OBJDIR:C,/,_,g}_${__obj:C,/,_,g}.meta
239.else
240_meta_obj=	${__obj:C,/,_,g}.meta
241.endif	# ${MAKE_VERSION} < 20171028
242.else
243_meta_obj=	${__obj}.meta
244.endif	# ${__obj:M*/*}
245_dep_obj=	${DEPENDFILE}.${__obj:${DEPEND_FILTER}}
246.if defined(_meta_filemon)
247_depfile=	${.OBJDIR}/${_meta_obj}
248.else
249_depfile=	${.OBJDIR}/${_dep_obj}
250.endif
251.if !exists(${_depfile})
252${__obj}: ${OBJS_DEPEND_GUESS}
253${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
254.elif defined(_meta_filemon)
255# For meta mode we still need to know which file to depend on to avoid
256# ambiguous suffix transformation rules from .PATH.  Meta mode does not
257# use .depend files.  We really only need source files, not headers since
258# they are typically in SRCS/beforebuild already.  For target-specific
259# guesses do include headers though since they may not be in SRCS.
260${__obj}: ${OBJS_DEPEND_GUESS:N*.h}
261${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
262.endif	# !exists(${_depfile})
263.endfor
264
265# Always run 'make depend' to generate dependencies early and to avoid the
266# need for manually running it.  The dirdeps build should only do this in
267# sub-makes though since MAKELEVEL0 is for dirdeps calculations.
268.if ${MK_DIRDEPS_BUILD} == "no" || ${.MAKE.LEVEL} > 0
269beforebuild: depend
270.endif
271
272.if !target(depend)
273.if defined(SRCS)
274depend: beforedepend ${DEPENDFILE} afterdepend
275
276# Tell bmake not to look for generated files via .PATH
277.NOPATH: ${DEPENDFILE} ${DEPENDFILES}
278
279# A .depend file will only be generated if there are commands in
280# beforedepend/_EXTRADEPEND/afterdepend  The _EXTRADEPEND target is
281# ignored if using meta+filemon since it handles all dependencies.  The other
282# targets are kept as they be used for generating something.  The target is
283# kept to allow 'make depend' to generate files.
284${DEPENDFILE}: ${SRCS} ${DPSRCS}
285.if !defined(_SKIP_DEPEND)
286.if exists(${.OBJDIR}/${DEPENDFILE}) || \
287    ((commands(beforedepend) || \
288    (!defined(_meta_filemon) && commands(_EXTRADEPEND)) || \
289    commands(afterdepend)) && !empty(.MAKE.MODE:Mmeta))
290	rm -f ${DEPENDFILE}
291.endif
292.endif
293.if !defined(_meta_filemon) && target(_EXTRADEPEND)
294_EXTRADEPEND: .USE
295${DEPENDFILE}: _EXTRADEPEND
296.endif
297
298.ORDER: ${DEPENDFILE} afterdepend
299.else
300depend: beforedepend afterdepend
301.endif
302.if !target(beforedepend)
303beforedepend:
304.else
305.ORDER: beforedepend ${DEPENDFILE}
306.ORDER: beforedepend afterdepend
307.endif
308.if !target(afterdepend)
309afterdepend:
310.endif
311.endif
312
313.if defined(SRCS)
314.if ${CTAGS:T} == "gtags"
315CLEANDEPENDFILES+=	GPATH GRTAGS GSYMS GTAGS
316.if defined(HTML)
317CLEANDEPENDDIRS+=	HTML
318.endif
319.else
320CLEANDEPENDFILES+=	tags
321.endif
322.endif
323.if !target(cleandepend)
324cleandepend:
325.if !empty(CLEANDEPENDFILES)
326	rm -f ${CLEANDEPENDFILES}
327.endif
328.if !empty(CLEANDEPENDDIRS)
329	rm -rf ${CLEANDEPENDDIRS}
330.endif
331.endif
332.ORDER: cleandepend all
333.ORDER: cleandepend depend
334.if ${MK_AUTO_OBJ} == "yes"
335.ORDER: cleanobj depend
336.ORDER: cleandir depend
337.endif
338
339.if !target(checkdpadd) && (defined(DPADD) || defined(LDADD))
340_LDADD_FROM_DPADD=	${DPADD:R:T:C;^lib(.*)$;-l\1;g}
341# Ignore -Wl,--start-group/-Wl,--end-group as it might be required in the
342# LDADD list due to unresolved symbols
343_LDADD_CANONICALIZED=	${LDADD:N:R:T:C;^lib(.*)$;-l\1;g:N-Wl,--[es]*-group}
344checkdpadd:
345.if ${_LDADD_FROM_DPADD} != ${_LDADD_CANONICALIZED}
346	@echo ${.CURDIR}
347	@echo "DPADD -> ${_LDADD_FROM_DPADD}"
348	@echo "LDADD -> ${_LDADD_CANONICALIZED}"
349.endif
350.endif
351