xref: /freebsd/share/mk/bsd.dep.mk (revision c321779676d0722f5d0a65c30e8b23b8a6547364)
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.if defined(_SKIP_BUILD) || defined(_meta_filemon)
91_SKIP_READ_DEPEND=	1
92.if ${MK_DIRDEPS_BUILD} == "no" || make(analyze) || make(print-dir) || \
93    make(obj) || make(clean*) || make(destroy*)
94.MAKE.DEPENDFILE=	/dev/null
95.endif
96.endif
97
98.if defined(SRCS)
99CLEANFILES?=
100
101.for _S in ${SRCS:N*.[dhly]}
102OBJS_DEPEND_GUESS.${_S:R}.o+=	${_S}
103.endfor
104
105# Lexical analyzers
106.for _LSRC in ${SRCS:M*.l:N*/*}
107.for _LC in ${_LSRC:R}.c
108${_LC}: ${_LSRC}
109	${LEX} ${LFLAGS} -o${.TARGET} ${.ALLSRC}
110OBJS_DEPEND_GUESS.${_LC:R}.o+=	${_LC}
111SRCS:=	${SRCS:S/${_LSRC}/${_LC}/}
112CLEANFILES+= ${_LC}
113.endfor
114.endfor
115
116# Yacc grammars
117.for _YSRC in ${SRCS:M*.y:N*/*}
118.for _YC in ${_YSRC:R}.c
119SRCS:=	${SRCS:S/${_YSRC}/${_YC}/}
120CLEANFILES+= ${_YC}
121.if !empty(YFLAGS:M-d) && !empty(SRCS:My.tab.h)
122.ORDER: ${_YC} y.tab.h
123y.tab.h: .NOMETA
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${_YH}: .NOMETA
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.endfor
143.endfor
144
145# DTrace probe definitions
146.if ${SRCS:M*.d}
147CFLAGS+=	-I${.OBJDIR}
148.endif
149.for _DSRC in ${SRCS:M*.d:N*/*}
150.for _D in ${_DSRC:R}
151SRCS+=	${_D}.h
152${_D}.h: ${_DSRC}
153	${DTRACE} ${DTRACEFLAGS} -h -s ${.ALLSRC}
154SRCS:=	${SRCS:S/^${_DSRC}$//}
155OBJS+=	${_D}.o
156CLEANFILES+= ${_D}.h ${_D}.o
157${_D}.o: ${_DSRC} ${OBJS:S/^${_D}.o$//}
158	@rm -f ${.TARGET}
159	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
160.if defined(LIB)
161CLEANFILES+= ${_D}.pico ${_D}.po
162${_D}.pico: ${_DSRC} ${SOBJS:S/^${_D}.pico$//}
163	@rm -f ${.TARGET}
164	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
165${_D}.po: ${_DSRC} ${POBJS:S/^${_D}.po$//}
166	@rm -f ${.TARGET}
167	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
168.endif
169.endfor
170.endfor
171
172
173.if ${MAKE_VERSION} < 20160220
174DEPEND_MP?=	-MP
175.endif
176# Handle OBJS=../somefile.o hacks.  Just replace '/' rather than use :T to
177# avoid collisions.
178DEPEND_FILTER=	C,/,_,g
179DEPENDSRCS=	${SRCS:M*.[cSC]} ${SRCS:M*.cxx} ${SRCS:M*.cpp} ${SRCS:M*.cc}
180.if !empty(DEPENDSRCS)
181DEPENDOBJS+=	${DEPENDSRCS:R:S,$,.o,}
182.endif
183DEPENDFILES_OBJS=	${DEPENDOBJS:O:u:${DEPEND_FILTER}:C/^/${DEPENDFILE}./}
184DEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF${DEPENDFILE}.${.TARGET:${DEPEND_FILTER}}
185DEPEND_CFLAGS+=	-MT${.TARGET}
186.if !defined(_meta_filemon)
187.if defined(.PARSEDIR)
188# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
189# as those are the only ones we will include.
190DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:${DEPEND_FILTER}:M${.TARGET:${DEPEND_FILTER}}}" != ""
191CFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
192.else
193CFLAGS+=	${DEPEND_CFLAGS}
194.endif
195.if !defined(_SKIP_READ_DEPEND)
196.for __depend_obj in ${DEPENDFILES_OBJS}
197.if ${MAKE_VERSION} < 20160220
198.sinclude "${.OBJDIR}/${__depend_obj}"
199.else
200.dinclude "${.OBJDIR}/${__depend_obj}"
201.endif
202.endfor
203.endif	# !defined(_SKIP_READ_DEPEND)
204.endif	# !defined(_meta_filemon)
205.endif	# defined(SRCS)
206
207.if ${MK_DIRDEPS_BUILD} == "yes" && ${.MAKE.DEPENDFILE} != "/dev/null"
208# Prevent meta.autodep.mk from tracking "local dependencies".
209.depend:
210.include <meta.autodep.mk>
211# If using filemon then _EXTRADEPEND is skipped since it is not needed.
212.if defined(_meta_filemon)
213# this depend: bypasses that below
214# the dependency helps when bootstrapping
215depend: beforedepend ${DPSRCS} ${SRCS} afterdepend
216beforedepend:
217afterdepend: beforedepend
218.endif
219.endif
220
221# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
222# For meta+filemon the .meta file is checked for since it is the dependency
223# file used.
224.for __obj in ${DEPENDOBJS:O:u}
225# If the obj has any '/', then replace with '_'.  For meta files, this is
226# mimicing what bmake's meta_name() does and adding in the full path
227# as well to ensure that the expected meta file is read.
228.if ${__obj:M*/*}
229_meta_obj=	${.OBJDIR:C,/,_,g}_${__obj:C,/,_,g}.meta
230.else
231_meta_obj=	${__obj}.meta
232.endif
233_dep_obj=	${DEPENDFILE}.${__obj:${DEPEND_FILTER}}
234.if (defined(_meta_filemon) && !exists(${.OBJDIR}/${_meta_obj})) || \
235    (!defined(_meta_filemon) && !exists(${.OBJDIR}/${_dep_obj}))
236${__obj}: ${OBJS_DEPEND_GUESS}
237${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
238.elif defined(_meta_filemon)
239# For meta mode we still need to know which file to depend on to avoid
240# ambiguous suffix transformation rules from .PATH.  Meta mode does not
241# use .depend files.  We really only need source files, not headers since
242# they are typically in SRCS/beforebuild already.  For target-specific
243# guesses do include headers though since they may not be in SRCS.
244${__obj}: ${OBJS_DEPEND_GUESS:N*.h}
245${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
246.endif
247.endfor
248
249# Always run 'make depend' to generate dependencies early and to avoid the
250# need for manually running it.  The dirdeps build should only do this in
251# sub-makes though since MAKELEVEL0 is for dirdeps calculations.
252.if ${MK_DIRDEPS_BUILD} == "no" || ${.MAKE.LEVEL} > 0
253beforebuild: depend
254.endif
255
256.if !target(depend)
257.if defined(SRCS)
258depend: beforedepend ${DEPENDFILE} afterdepend
259
260# Tell bmake not to look for generated files via .PATH
261.NOPATH: ${DEPENDFILE} ${DEPENDFILES_OBJS}
262
263DPSRCS+= ${SRCS}
264# A .depend file will only be generated if there are commands in
265# beforedepend/_EXTRADEPEND/afterdepend  The _EXTRADEPEND target is
266# ignored if using meta+filemon since it handles all dependencies.  The other
267# targets are kept as they be used for generating something.  The target is
268# kept to allow 'make depend' to generate files.
269${DEPENDFILE}: ${DPSRCS}
270.if exists(${.OBJDIR}/${DEPENDFILE}) || \
271    ((commands(beforedepend) || \
272    (!defined(_meta_filemon) && commands(_EXTRADEPEND)) || \
273    commands(afterdepend)) && !empty(.MAKE.MODE:Mmeta))
274	rm -f ${DEPENDFILE}
275.endif
276.if !defined(_meta_filemon) && target(_EXTRADEPEND)
277_EXTRADEPEND: .USE
278${DEPENDFILE}: _EXTRADEPEND
279.endif
280
281.ORDER: ${DEPENDFILE} afterdepend
282.else
283depend: beforedepend afterdepend
284.endif
285.if !target(beforedepend)
286beforedepend:
287.else
288.ORDER: beforedepend ${DEPENDFILE}
289.ORDER: beforedepend afterdepend
290.endif
291.if !target(afterdepend)
292afterdepend:
293.endif
294.endif
295
296.if defined(SRCS)
297.if ${CTAGS:T} == "gtags"
298CLEANDEPENDFILES+=	GPATH GRTAGS GSYMS GTAGS
299.if defined(HTML)
300CLEANDEPENDDIRS+=	HTML
301.endif
302.else
303CLEANDEPENDFILES+=	tags
304.endif
305.endif
306.if !target(cleandepend)
307cleandepend:
308.if !empty(CLEANDEPENDFILES)
309	rm -f ${CLEANDEPENDFILES}
310.endif
311.if !empty(CLEANDEPENDDIRS)
312	rm -rf ${CLEANDEPENDDIRS}
313.endif
314.endif
315.ORDER: cleandepend all
316.ORDER: cleandepend depend
317
318.if !target(checkdpadd) && (defined(DPADD) || defined(LDADD))
319_LDADD_FROM_DPADD=	${DPADD:R:T:C;^lib(.*)$;-l\1;g}
320# Ignore -Wl,--start-group/-Wl,--end-group as it might be required in the
321# LDADD list due to unresolved symbols
322_LDADD_CANONICALIZED=	${LDADD:N:R:T:C;^lib(.*)$;-l\1;g:N-Wl,--[es]*-group}
323checkdpadd:
324.if ${_LDADD_FROM_DPADD} != ${_LDADD_CANONICALIZED}
325	@echo ${.CURDIR}
326	@echo "DPADD -> ${_LDADD_FROM_DPADD}"
327	@echo "LDADD -> ${_LDADD_CANONICALIZED}"
328.endif
329.endif
330