xref: /freebsd/share/mk/bsd.dep.mk (revision 799483e49ea9412cd4d87a33a024bc3ab3f21e9d)
17f3dea24SPeter Wemm# $FreeBSD$
253996fadSWolfram Schneider#
353996fadSWolfram Schneider# The include file <bsd.dep.mk> handles Makefile dependencies.
453996fadSWolfram Schneider#
553996fadSWolfram Schneider#
653996fadSWolfram Schneider# +++ variables +++
753996fadSWolfram Schneider#
8ae038fc3SBryan Drewery# CLEANDEPENDDIRS	Additional directories to remove for the cleandepend
9ae038fc3SBryan Drewery# 			target.
10ae038fc3SBryan Drewery#
11ae038fc3SBryan Drewery# CLEANDEPENDFILES	Additional files to remove for the cleandepend target.
12ae038fc3SBryan Drewery#
13ccc4bab1SRuslan Ermilov# CTAGS		A tags file generation program [gtags]
14ccc4bab1SRuslan Ermilov#
15ccc4bab1SRuslan Ermilov# CTAGSFLAGS	Options for ctags(1) [not set]
16ccc4bab1SRuslan Ermilov#
1753996fadSWolfram Schneider# DEPENDFILE	dependencies file [.depend]
1853996fadSWolfram Schneider#
19ccc4bab1SRuslan Ermilov# GTAGSFLAGS	Options for gtags(1) [-o]
20ccc4bab1SRuslan Ermilov#
21ccc4bab1SRuslan Ermilov# HTAGSFLAGS	Options for htags(1) [not set]
22ccc4bab1SRuslan Ermilov#
2353996fadSWolfram Schneider# SRCS          List of source files (c, c++, assembler)
2453996fadSWolfram Schneider#
2511dc9298SRuslan Ermilov# DPSRCS	List of source files which are needed for generating
2611dc9298SRuslan Ermilov#		dependencies, ${SRCS} are always part of it.
2753996fadSWolfram Schneider#
2853996fadSWolfram Schneider# +++ targets +++
2953996fadSWolfram Schneider#
3053996fadSWolfram Schneider#	cleandepend:
31ae038fc3SBryan Drewery#		remove ${CLEANDEPENDFILES}; remove ${CLEANDEPENDDIRS} and all
32ae038fc3SBryan Drewery#		contents.
3353996fadSWolfram Schneider#
3453996fadSWolfram Schneider#	depend:
3553996fadSWolfram Schneider#		Make the dependencies for the source files, and store
3653996fadSWolfram Schneider#		them in the file ${DEPENDFILE}.
3753996fadSWolfram Schneider#
3853996fadSWolfram Schneider#	tags:
39ccc4bab1SRuslan Ermilov#		In "ctags" mode, create a tags file for the source files.
40ccc4bab1SRuslan Ermilov#		In "gtags" mode, create a (GLOBAL) gtags file for the
41ccc4bab1SRuslan Ermilov#		source files.  If HTML is defined, htags(1) is also run
42ccc4bab1SRuslan Ermilov#		after gtags(1).
4353996fadSWolfram Schneider
445ffdf361SRuslan Ermilov.if !target(__<bsd.init.mk>__)
455ffdf361SRuslan Ermilov.error bsd.dep.mk cannot be included directly.
465ffdf361SRuslan Ermilov.endif
47b16495e7SWolfram Schneider
48ccc4bab1SRuslan ErmilovCTAGS?=		gtags
49ccc4bab1SRuslan ErmilovCTAGSFLAGS?=
50ccc4bab1SRuslan ErmilovGTAGSFLAGS?=	-o
51ccc4bab1SRuslan ErmilovHTAGSFLAGS?=
52ccc4bab1SRuslan Ermilov
53369737d7SBryan Drewery.if ${MK_DIRDEPS_BUILD} == "no"
54fc2c2cd9SBryan Drewery.MAKE.DEPENDFILE= ${DEPENDFILE}
55369737d7SBryan Drewery.endif
56caf1fb32SBryan DreweryCLEANDEPENDFILES+=	${DEPENDFILE} ${DEPENDFILE}.*
57b964b943SBryan Drewery.if ${MK_META_MODE} == "yes"
58b964b943SBryan DreweryCLEANDEPENDFILES+=	*.meta
59b964b943SBryan Drewery.endif
60d14f862cSGarrett Wollman
61ccc4bab1SRuslan Ermilov# Keep `tags' here, before SRCS are mangled below for `depend'.
626260f1d8SRuslan Ermilov.if !target(tags) && defined(SRCS) && !defined(NO_TAGS)
63ccc4bab1SRuslan Ermilovtags: ${SRCS}
64213a4447SRuslan Ermilov.if ${CTAGS:T} == "gtags"
65ccc4bab1SRuslan Ermilov	@cd ${.CURDIR} && ${CTAGS} ${GTAGSFLAGS} ${.OBJDIR}
66ccc4bab1SRuslan Ermilov.if defined(HTML)
67ccc4bab1SRuslan Ermilov	@cd ${.CURDIR} && htags ${HTAGSFLAGS} -d ${.OBJDIR} ${.OBJDIR}
68ccc4bab1SRuslan Ermilov.endif
69213a4447SRuslan Ermilov.else
70213a4447SRuslan Ermilov	@${CTAGS} ${CTAGSFLAGS} -f /dev/stdout \
71213a4447SRuslan Ermilov	    ${.ALLSRC:N*.h} | sed "s;${.CURDIR}/;;" > ${.TARGET}
72ccc4bab1SRuslan Ermilov.endif
73ccc4bab1SRuslan Ermilov.endif
74ccc4bab1SRuslan Ermilov
75*799483e4SBryan Drewery.if !empty(.MAKE.MODE:Mmeta) && empty(.MAKE.MODE:Mnofilemon)
76*799483e4SBryan Drewery_meta_filemon=	1
77*799483e4SBryan Drewery.endif
78*799483e4SBryan Drewery
799fb02a70SBryan Drewery# Skip reading .depend when not needed to speed up tree-walks
809fb02a70SBryan Drewery# and simple lookups.
81*799483e4SBryan Drewery# Also skip generating or including .depend.* files if in meta+filemon mode
82*799483e4SBryan Drewery# since it will track dependencies itself.  OBJS_DEPEND_GUESS is still used.
839fb02a70SBryan Drewery.if !empty(.MAKEFLAGS:M-V${_V_READ_DEPEND}) || make(obj) || make(clean*) || \
84*799483e4SBryan Drewery    make(install*) || make(analyze) || defined(_meta_filemon)
859fb02a70SBryan Drewery_SKIP_READ_DEPEND=	1
869fb02a70SBryan Drewery.if ${MK_DIRDEPS_BUILD} == "no"
879fb02a70SBryan Drewery.MAKE.DEPENDFILE=	/dev/null
889fb02a70SBryan Drewery.endif
899fb02a70SBryan Drewery.endif
909fb02a70SBryan Drewery
912ec8b6deSBruce Evans.if defined(SRCS)
922ec8b6deSBruce EvansCLEANFILES?=
932ec8b6deSBruce Evans
948a152f97SRui Paulo.for _S in ${SRCS:N*.[dhly]}
95919154d9SBryan DreweryOBJS_DEPEND_GUESS.${_S:R}.o+=	${_S}
96d7beff08SBryan Drewery.endfor
976b14fdfaSRuslan Ermilov
989f65e34fSRui Paulo# Lexical analyzers
992ec8b6deSBruce Evans.for _LSRC in ${SRCS:M*.l:N*/*}
100484933a9SRuslan Ermilov.for _LC in ${_LSRC:R}.c
1015852ae2dSBryan Drewery${_LC}: ${_LSRC}
1028f812fecSDag-Erling Smørgrav	${LEX} ${LFLAGS} -o${.TARGET} ${.ALLSRC}
103919154d9SBryan DreweryOBJS_DEPEND_GUESS.${_LC:R}.o+=	${_LC}
1042ec8b6deSBruce EvansSRCS:=	${SRCS:S/${_LSRC}/${_LC}/}
105484933a9SRuslan ErmilovCLEANFILES+= ${_LC}
1062ec8b6deSBruce Evans.endfor
1072ec8b6deSBruce Evans.endfor
1082ec8b6deSBruce Evans
1099f65e34fSRui Paulo# Yacc grammars
1102ec8b6deSBruce Evans.for _YSRC in ${SRCS:M*.y:N*/*}
111484933a9SRuslan Ermilov.for _YC in ${_YSRC:R}.c
1122ec8b6deSBruce EvansSRCS:=	${SRCS:S/${_YSRC}/${_YC}/}
113484933a9SRuslan ErmilovCLEANFILES+= ${_YC}
11465d2bdc6SRuslan Ermilov.if !empty(YFLAGS:M-d) && !empty(SRCS:My.tab.h)
1152ec8b6deSBruce Evans.ORDER: ${_YC} y.tab.h
11692edc966SBryan Dreweryy.tab.h: .NOMETA
1172ec8b6deSBruce Evans${_YC} y.tab.h: ${_YSRC}
1182ec8b6deSBruce Evans	${YACC} ${YFLAGS} ${.ALLSRC}
1192ec8b6deSBruce Evans	cp y.tab.c ${_YC}
120484933a9SRuslan ErmilovCLEANFILES+= y.tab.c y.tab.h
12165d2bdc6SRuslan Ermilov.elif !empty(YFLAGS:M-d)
122484933a9SRuslan Ermilov.for _YH in ${_YC:R}.h
1235444d429SBryan Drewery.ORDER: ${_YC} ${_YH}
12492edc966SBryan Drewery${_YH}: .NOMETA
1255444d429SBryan Drewery${_YC} ${_YH}: ${_YSRC}
1262ec8b6deSBruce Evans	${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
127484933a9SRuslan ErmilovSRCS+=	${_YH}
128484933a9SRuslan ErmilovCLEANFILES+= ${_YH}
1292ec8b6deSBruce Evans.endfor
1302ec8b6deSBruce Evans.else
1315852ae2dSBryan Drewery${_YC}: ${_YSRC}
1322ec8b6deSBruce Evans	${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
1332ec8b6deSBruce Evans.endif
134919154d9SBryan DreweryOBJS_DEPEND_GUESS.${_YC:R}.o+=	${_YC}
1352ec8b6deSBruce Evans.endfor
1362ec8b6deSBruce Evans.endfor
1379f65e34fSRui Paulo
1389f65e34fSRui Paulo# DTrace probe definitions
139ff0f36e7SRui Paulo.if ${SRCS:M*.d}
1405439a715SMark JohnstonCFLAGS+=	-I${.OBJDIR}
141ff0f36e7SRui Paulo.endif
1429f65e34fSRui Paulo.for _DSRC in ${SRCS:M*.d:N*/*}
143ff0f36e7SRui Paulo.for _D in ${_DSRC:R}
144cfa5f2beSBryan DrewerySRCS+=	${_D}.h
1455852ae2dSBryan Drewery${_D}.h: ${_DSRC}
1460c76c8aaSMark Johnston	${DTRACE} ${DTRACEFLAGS} -h -s ${.ALLSRC}
147e9083b2dSMark JohnstonSRCS:=	${SRCS:S/^${_DSRC}$//}
148d8b63155SRui PauloOBJS+=	${_D}.o
1499b646f25SRui PauloCLEANFILES+= ${_D}.h ${_D}.o
1505852ae2dSBryan Drewery${_D}.o: ${_DSRC} ${OBJS:S/^${_D}.o$//}
151a298c279SBryan Drewery	@rm -f ${.TARGET}
152cfa5f2beSBryan Drewery	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
1539b646f25SRui Paulo.if defined(LIB)
1549b646f25SRui PauloCLEANFILES+= ${_D}.So ${_D}.po
1555852ae2dSBryan Drewery${_D}.So: ${_DSRC} ${SOBJS:S/^${_D}.So$//}
156a298c279SBryan Drewery	@rm -f ${.TARGET}
157cfa5f2beSBryan Drewery	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
1585852ae2dSBryan Drewery${_D}.po: ${_DSRC} ${POBJS:S/^${_D}.po$//}
159a298c279SBryan Drewery	@rm -f ${.TARGET}
160cfa5f2beSBryan Drewery	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
161ff0f36e7SRui Paulo.endif
1629f65e34fSRui Paulo.endfor
1639f65e34fSRui Paulo.endfor
164b9964333SBryan Drewery
165b9964333SBryan Drewery
166e32a2cbeSBryan Drewery.if ${MAKE_VERSION} < 20160220
167b9964333SBryan DreweryDEPEND_MP?=	-MP
168e32a2cbeSBryan Drewery.endif
169b9964333SBryan Drewery# Handle OBJS=../somefile.o hacks.  Just replace '/' rather than use :T to
170b9964333SBryan Drewery# avoid collisions.
171b9964333SBryan DreweryDEPEND_FILTER=	C,/,_,g
17270ca9ec4SBryan DreweryDEPENDSRCS=	${SRCS:M*.[cSC]} ${SRCS:M*.cxx} ${SRCS:M*.cpp} ${SRCS:M*.cc}
17370ca9ec4SBryan Drewery.if !empty(DEPENDSRCS)
17470ca9ec4SBryan DreweryDEPENDOBJS+=	${DEPENDSRCS:R:S,$,.o,}
17570ca9ec4SBryan Drewery.endif
17670ca9ec4SBryan DreweryDEPENDFILES_OBJS=	${DEPENDOBJS:O:u:${DEPEND_FILTER}:C/^/${DEPENDFILE}./}
177b9964333SBryan DreweryDEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF${DEPENDFILE}.${.TARGET:${DEPEND_FILTER}}
178b9964333SBryan DreweryDEPEND_CFLAGS+=	-MT${.TARGET}
17970ca9ec4SBryan Drewery.if !defined(_meta_filemon)
18010f696a5SBryan Drewery.if defined(.PARSEDIR)
18110f696a5SBryan Drewery# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
18210f696a5SBryan Drewery# as those are the only ones we will include.
183d7296a8fSBryan DreweryDEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:M${.TARGET:${DEPEND_FILTER}}}" != ""
18410f696a5SBryan DreweryCFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
18510f696a5SBryan Drewery.else
186b9964333SBryan DreweryCFLAGS+=	${DEPEND_CFLAGS}
18710f696a5SBryan Drewery.endif
1889fb02a70SBryan Drewery.if !defined(_SKIP_READ_DEPEND)
1899f286e82SBryan Drewery.for __depend_obj in ${DEPENDFILES_OBJS}
190e32a2cbeSBryan Drewery.if ${MAKE_VERSION} < 20160220
191e9508dc2SBryan Drewery.sinclude "${.OBJDIR}/${__depend_obj}"
192e32a2cbeSBryan Drewery.else
193e32a2cbeSBryan Drewery.dinclude "${.OBJDIR}/${__depend_obj}"
194e32a2cbeSBryan Drewery.endif
195b9964333SBryan Drewery.endfor
196d7beff08SBryan Drewery.endif	# !defined(_SKIP_READ_DEPEND)
19770ca9ec4SBryan Drewery.endif	# !defined(_meta_filemon)
198b9964333SBryan Drewery.endif	# defined(SRCS)
1992ec8b6deSBruce Evans
200948f327eSSimon J. Gerraty.if ${MK_DIRDEPS_BUILD} == "yes"
201b9cd412eSBryan Drewery# Prevent meta.autodep.mk from tracking "local dependencies".
202b9cd412eSBryan Drewery.depend:
2037750ad47SMarcel Moolenaar.include <meta.autodep.mk>
20470ca9ec4SBryan Drewery# If using filemon then _EXTRADEPEND is skipped since it is not needed.
205*799483e4SBryan Drewery.if defined(_meta_filemon)
2067750ad47SMarcel Moolenaar# this depend: bypasses that below
2077750ad47SMarcel Moolenaar# the dependency helps when bootstrapping
2087750ad47SMarcel Moolenaardepend: beforedepend ${DPSRCS} ${SRCS} afterdepend
2097750ad47SMarcel Moolenaarbeforedepend:
2107750ad47SMarcel Moolenaarafterdepend: beforedepend
2117750ad47SMarcel Moolenaar.endif
21270ca9ec4SBryan Drewery.endif
2137750ad47SMarcel Moolenaar
214d7beff08SBryan Drewery# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
21570ca9ec4SBryan Drewery# For meta+filemon the .meta file is checked for since it is the dependency
21670ca9ec4SBryan Drewery# file used.
217d7beff08SBryan Drewery.for __obj in ${DEPENDOBJS:O:u}
21870ca9ec4SBryan Drewery.if (defined(_meta_filemon) && !exists(${.OBJDIR}/${__obj}.meta)) || \
21970ca9ec4SBryan Drewery    (!defined(_meta_filemon) && !exists(${.OBJDIR}/${DEPENDFILE}.${__obj}))
220d7beff08SBryan Drewery${__obj}: ${OBJS_DEPEND_GUESS}
221d7beff08SBryan Drewery${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
2223b828e44SBryan Drewery.elif defined(_meta_filemon)
2233b828e44SBryan Drewery# For meta mode we still need to know which file to depend on to avoid
2243b828e44SBryan Drewery# ambiguous suffix transformation rules from .PATH.  Meta mode does not
225c155deccSBryan Drewery# use .depend files.  We really only need source files, not headers since
226c155deccSBryan Drewery# they are typically in SRCS/beforebuild already.  For target-specific
227c155deccSBryan Drewery# guesses do include headers though since they may not be in SRCS.
2283b828e44SBryan Drewery${__obj}: ${OBJS_DEPEND_GUESS:N*.h}
229c155deccSBryan Drewery${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
230d7beff08SBryan Drewery.endif
231d7beff08SBryan Drewery.endfor
232b9cd412eSBryan Drewery
233b9cd412eSBryan Drewery# Always run 'make depend' to generate dependencies early and to avoid the
234b9cd412eSBryan Drewery# need for manually running it.  The dirdeps build should only do this in
235b9cd412eSBryan Drewery# sub-makes though since MAKELEVEL0 is for dirdeps calculations.
236b9cd412eSBryan Drewery.if ${MK_DIRDEPS_BUILD} == "no" || ${.MAKE.LEVEL} > 0
237b9cd412eSBryan Drewerybeforebuild: depend
238d7beff08SBryan Drewery.endif
239d7beff08SBryan Drewery
240d14f862cSGarrett Wollman.if !target(depend)
241d14f862cSGarrett Wollman.if defined(SRCS)
242b25a566dSRuslan Ermilovdepend: beforedepend ${DEPENDFILE} afterdepend
243b16495e7SWolfram Schneider
2447750ad47SMarcel Moolenaar# Tell bmake not to look for generated files via .PATH
245cf1eeb33SBryan Drewery.NOPATH: ${DEPENDFILE} ${DEPENDFILES_OBJS}
2467750ad47SMarcel Moolenaar
24711dc9298SRuslan ErmilovDPSRCS+= ${SRCS}
248497e8091SBryan Drewery# A .depend file will only be generated if there are commands in
249*799483e4SBryan Drewery# beforedepend/_EXTRADEPEND/afterdepend  The _EXTRADEPEND target is
250*799483e4SBryan Drewery# ignored if using meta+filemon since it handles all dependencies.  The other
251*799483e4SBryan Drewery# targets are kept as they be used for generating something.  The target is
252*799483e4SBryan Drewery# kept to allow 'make depend' to generate files.
2535852ae2dSBryan Drewery${DEPENDFILE}: ${DPSRCS}
254987c375fSBryan Drewery.if exists(${.OBJDIR}/${DEPENDFILE}) || \
255*799483e4SBryan Drewery    ((commands(beforedepend) || \
256*799483e4SBryan Drewery    (!defined(_meta_filemon) && commands(_EXTRADEPEND)) || \
257987c375fSBryan Drewery    commands(afterdepend)) && !empty(.MAKE.MODE:Mmeta))
258b16495e7SWolfram Schneider	rm -f ${DEPENDFILE}
25980f9a973SBryan Drewery.endif
260*799483e4SBryan Drewery.if !defined(_meta_filemon) && target(_EXTRADEPEND)
26170e82473SRuslan Ermilov_EXTRADEPEND: .USE
2620cfacc86SRuslan Ermilov${DEPENDFILE}: _EXTRADEPEND
263663e9677SBruce Evans.endif
264b16495e7SWolfram Schneider
26597fe7f47SJordan K. Hubbard.ORDER: ${DEPENDFILE} afterdepend
266d14f862cSGarrett Wollman.else
267b25a566dSRuslan Ermilovdepend: beforedepend afterdepend
268d14f862cSGarrett Wollman.endif
269d14f862cSGarrett Wollman.if !target(beforedepend)
270d14f862cSGarrett Wollmanbeforedepend:
27197fe7f47SJordan K. Hubbard.else
27297fe7f47SJordan K. Hubbard.ORDER: beforedepend ${DEPENDFILE}
27397fe7f47SJordan K. Hubbard.ORDER: beforedepend afterdepend
274d14f862cSGarrett Wollman.endif
275d14f862cSGarrett Wollman.if !target(afterdepend)
276d14f862cSGarrett Wollmanafterdepend:
277d14f862cSGarrett Wollman.endif
278d14f862cSGarrett Wollman.endif
279d14f862cSGarrett Wollman
2803d437ee2SBryan Drewery.if defined(SRCS)
281ae038fc3SBryan Drewery.if ${CTAGS:T} == "gtags"
282ae038fc3SBryan DreweryCLEANDEPENDFILES+=	GPATH GRTAGS GSYMS GTAGS
283ae038fc3SBryan Drewery.if defined(HTML)
284ae038fc3SBryan DreweryCLEANDEPENDDIRS+=	HTML
285ae038fc3SBryan Drewery.endif
286ae038fc3SBryan Drewery.else
287ae038fc3SBryan DreweryCLEANDEPENDFILES+=	tags
288ae038fc3SBryan Drewery.endif
2893d437ee2SBryan Drewery.endif
290cd9a2f5cSJordan K. Hubbard.if !target(cleandepend)
291b25a566dSRuslan Ermilovcleandepend:
2923d437ee2SBryan Drewery.if !empty(CLEANDEPENDFILES)
293ae038fc3SBryan Drewery	rm -f ${CLEANDEPENDFILES}
2943d437ee2SBryan Drewery.endif
295ae038fc3SBryan Drewery.if !empty(CLEANDEPENDDIRS)
296ae038fc3SBryan Drewery	rm -rf ${CLEANDEPENDDIRS}
2976eb5e456SJordan K. Hubbard.endif
298cd9a2f5cSJordan K. Hubbard.endif
2996e02167bSRuslan Ermilov
3006e02167bSRuslan Ermilov.if !target(checkdpadd) && (defined(DPADD) || defined(LDADD))
30141ae64f3SEnji Cooper_LDADD_FROM_DPADD=	${DPADD:R:T:C;^lib(.*)$;-l\1;g}
30241ae64f3SEnji Cooper# Ignore -Wl,--start-group/-Wl,--end-group as it might be required in the
30341ae64f3SEnji Cooper# LDADD list due to unresolved symbols
30441ae64f3SEnji Cooper_LDADD_CANONICALIZED=	${LDADD:N:R:T:C;^lib(.*)$;-l\1;g:N-Wl,--[es]*-group}
3056e02167bSRuslan Ermilovcheckdpadd:
3062e3c6179SBruce Evans.if ${_LDADD_FROM_DPADD} != ${_LDADD_CANONICALIZED}
3072e3c6179SBruce Evans	@echo ${.CURDIR}
3082e3c6179SBruce Evans	@echo "DPADD -> ${_LDADD_FROM_DPADD}"
3092e3c6179SBruce Evans	@echo "LDADD -> ${_LDADD_CANONICALIZED}"
3102e3c6179SBruce Evans.endif
3116e02167bSRuslan Ermilov.endif
312