xref: /freebsd/share/mk/bsd.dep.mk (revision 77013d11e6483b970af25e13c9b892075742f7e5)
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} ${LFLAGS.${_LSRC}} -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# Multi-output targets both expect a .meta file and will fight over it. Only
125# allow it on the .c file instead.
126y.tab.h: ${_YC} .NOMETA
127# Force rebuild the .c file if any of its other outputs are missing.
128.if !exists(y.tab.h)
129${_YC}: .PHONY .META
130.endif
131${_YC}: ${_YSRC}
132	${YACC} ${YFLAGS} ${YFLAGS.${_YSRC}} ${.ALLSRC}
133	cp y.tab.c ${_YC}
134CLEANFILES+= y.tab.c y.tab.h
135.elif !empty(YFLAGS:M-d)
136.for _YH in ${_YC:R}.h
137# Multi-output targets both expect a .meta file and will fight over it. Only
138# allow it on the .c file instead.
139${_YH}: ${_YC} .NOMETA
140# Force rebuild the .c file if any of its other outputs are missing.
141.if !exists(${_YH})
142${_YC}: .PHONY .META
143.endif
144${_YC}: ${_YSRC}
145	${YACC} ${YFLAGS} ${YFLAGS.${_YSRC}} -o ${_YC} ${.ALLSRC}
146SRCS+=	${_YH}
147CLEANFILES+= ${_YH}
148.endfor
149.else
150${_YC}: ${_YSRC}
151	${YACC} ${YFLAGS} ${YFLAGS.${_YSRC}} -o ${_YC} ${.ALLSRC}
152.endif
153OBJS_DEPEND_GUESS.${_YC:R}.o+=	${_YC}
154.endfor
155.endfor
156
157# DTrace probe definitions
158.if ${SRCS:M*.d}
159CFLAGS+=	-I${.OBJDIR}
160.endif
161.for _DSRC in ${SRCS:M*.d:N*/*}
162.for _D in ${_DSRC:R}
163SRCS+=	${_D}.h
164${_D}.h: ${_DSRC}
165	${DTRACE} ${DTRACEFLAGS} -h -s ${.ALLSRC}
166SRCS:=	${SRCS:S/^${_DSRC}$//}
167OBJS+=	${_D}.o
168CLEANFILES+= ${_D}.h ${_D}.o
169${_D}.o: ${_DSRC} ${OBJS:S/^${_D}.o$//}
170	@rm -f ${.TARGET}
171	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
172.if defined(LIB)
173CLEANFILES+= ${_D}.pico ${_D}.po ${_D}.nossppico
174${_D}.pico: ${_DSRC} ${SOBJS:S/^${_D}.pico$//}
175	@rm -f ${.TARGET}
176	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
177${_D}.po: ${_DSRC} ${POBJS:S/^${_D}.po$//}
178	@rm -f ${.TARGET}
179	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
180${_D}.nossppico: ${_DSRC} ${SOBJS:S/^${_D}.nossppico$//}
181	@rm -f ${.TARGET}
182	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
183.endif
184.endfor
185.endfor
186.endif	# defined(SRCS)
187
188.if ${MAKE_VERSION} < 20160220
189DEPEND_MP?=	-MP
190.endif
191# Handle OBJS=../somefile.o hacks.  Just replace '/' rather than use :T to
192# avoid collisions.
193DEPEND_FILTER=	C,/,_,g
194.if !empty(OBJS)
195.if !defined(_ALLOW_ABSOLUTE_OBJ_PATH) && ${OBJS:M/*}
196# Absolute paths to OBJS should be an error inside ${SRCTOP}, but some users
197# might be relying on this feature, so add an opt-out mechanism.
198.if defined(SRCTOP) && ${OBJS:M${SRCTOP}*}
199.error "$$OBJS inside $$SRCTOP not allowed: ${OBJS:M${SRCTOP}*}"
200.elif ${OBJS:N${_ABSOLUTE_PATH_OBJS}:M/*}
201.error "$$OBJS absolute path not allowed: ${OBJS:N${_ABSOLUTE_PATH_OBJS}:M/*}. \
202    If this is intended, add them to _ABSOLUTE_PATH_OBJS to silence this error\
203    or define _ALLOW_ABSOLUTE_OBJ_PATH to disable this diagnostic."
204.endif
205.endif
206DEPENDOBJS+=	${OBJS}
207.else
208DEPENDSRCS+=	${SRCS:M*.[cSC]} ${SRCS:M*.cxx} ${SRCS:M*.cpp} ${SRCS:M*.cc}
209DEPENDSRCS+=	${DPSRCS:M*.[cSC]} ${DPSRCS:M*.cxx} ${DPSRCS:M*.cpp} ${DPSRCS:M*.cc}
210.if !empty(DEPENDSRCS)
211DEPENDOBJS+=	${DEPENDSRCS:${OBJS_SRCS_FILTER:ts:}:S,$,.o,}
212.endif
213.endif	# !empty(OBJS)
214.if !empty(DEPENDOBJS)
215DEPENDFILES+=	${DEPENDOBJS:O:u:${DEPEND_FILTER}:C/^/${DEPENDFILE}./}
216.endif
217.if defined(_SKIP_DEPEND)
218# Don't bother statting any .meta files for .depend*
219${DEPENDOBJS}:	.NOMETA
220${DEPENDFILE}:	.NOMETA
221# Unset these to avoid looping/statting on them later.
222.undef DEPENDSRCS
223.undef DEPENDOBJS
224.undef DEPENDFILES
225.endif	# defined(_SKIP_DEPEND)
226DEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF${DEPENDFILE}.${.TARGET:${DEPEND_FILTER}}
227DEPEND_CFLAGS+=	-MT${.TARGET}
228.if !defined(_meta_filemon)
229.if !empty(DEPEND_CFLAGS)
230# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
231# as those are the only ones we will include.
232DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:${DEPEND_FILTER}:M${.TARGET:${DEPEND_FILTER}}}" != ""
233CFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
234.endif
235.for __depend_obj in ${DEPENDFILES}
236.if ${MAKE_VERSION} < 20160220
237.sinclude "${.OBJDIR}/${__depend_obj}"
238.else
239.dinclude "${.OBJDIR}/${__depend_obj}"
240.endif
241.endfor
242.endif	# !defined(_meta_filemon)
243
244.if ${MK_DIRDEPS_BUILD} == "yes" && ${.MAKE.DEPENDFILE} != "/dev/null"
245# Prevent meta.autodep.mk from tracking "local dependencies".
246.depend:
247.include <meta.autodep.mk>
248# If using filemon then _EXTRADEPEND is skipped since it is not needed.
249.if defined(_meta_filemon)
250# this depend: bypasses that below
251# the dependency helps when bootstrapping
252depend: beforedepend ${DPSRCS} ${SRCS} afterdepend
253beforedepend:
254afterdepend: beforedepend
255.endif
256.endif
257
258# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
259# For meta+filemon the .meta file is checked for since it is the dependency
260# file used.
261.for __obj in ${DEPENDOBJS:O:u}
262# If the obj has any '/', then replace with '_'.  For meta files, this is
263# mimicing what bmake's meta_name() does and adding in the full path
264# as well to ensure that the expected meta file is read.
265.if ${__obj:M*/*}
266.if ${MAKE_VERSION} < 20171028
267_meta_obj=	${.OBJDIR:C,/,_,g}_${__obj:C,/,_,g}.meta
268.else
269_meta_obj=	${__obj:C,/,_,g}.meta
270.endif	# ${MAKE_VERSION} < 20171028
271.else
272_meta_obj=	${__obj}.meta
273.endif	# ${__obj:M*/*}
274_dep_obj=	${DEPENDFILE}.${__obj:${DEPEND_FILTER}}
275.if defined(_meta_filemon)
276_depfile=	${.OBJDIR}/${_meta_obj}
277.else
278_depfile=	${.OBJDIR}/${_dep_obj}
279.endif
280.if !exists(${_depfile}) || defined(_meta_filemon)
281# - Headers are normally built in beforebuild when included in DPSRCS or SRCS.
282#   So we don't need it as a guessed dependency (it may lead to cyclic problems
283#   if custom rules are defined).  The only time this causes a problem is when
284#   'make foo.o' is ran.
285# - For meta mode we still need to know which file to depend on to avoid
286#   ambiguous suffix transformation rules from .PATH.  Meta mode does not
287#   use .depend files when filemon is in use.
288.if !target(${__obj})
289${__obj}: ${OBJS_DEPEND_GUESS}
290.endif
291${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
292.endif	# !exists(${_depfile}) || defined(_meta_filemon)
293.endfor
294
295# Always run 'make depend' to generate dependencies early and to avoid the
296# need for manually running it.  The dirdeps build should only do this in
297# sub-makes though since MAKELEVEL0 is for dirdeps calculations.
298.if ${MK_DIRDEPS_BUILD} == "no" || ${.MAKE.LEVEL} > 0
299beforebuild: depend
300.endif
301
302.if !target(depend)
303.if defined(SRCS)
304depend: beforedepend ${DEPENDFILE} afterdepend
305
306# Tell bmake not to look for generated files via .PATH
307.NOPATH: ${DEPENDFILE} ${DEPENDFILES}
308
309# A .depend file will only be generated if there are commands in
310# beforedepend/_EXTRADEPEND/afterdepend  The _EXTRADEPEND target is
311# ignored if using meta+filemon since it handles all dependencies.  The other
312# targets are kept as they be used for generating something.  The target is
313# kept to allow 'make depend' to generate files.
314${DEPENDFILE}: ${SRCS} ${DPSRCS}
315.if !defined(_SKIP_DEPEND)
316.if exists(${.OBJDIR}/${DEPENDFILE}) || \
317    ((commands(beforedepend) || \
318    (!defined(_meta_filemon) && commands(_EXTRADEPEND)) || \
319    commands(afterdepend)) && !empty(.MAKE.MODE:Mmeta))
320	rm -f ${DEPENDFILE}
321.endif
322.endif
323.if !defined(_meta_filemon) && target(_EXTRADEPEND)
324_EXTRADEPEND: .USE
325${DEPENDFILE}: _EXTRADEPEND
326.endif
327
328.ORDER: ${DEPENDFILE} afterdepend
329.else
330depend: beforedepend afterdepend
331.endif
332.if !target(beforedepend)
333beforedepend:
334.else
335.ORDER: beforedepend ${DEPENDFILE}
336.ORDER: beforedepend afterdepend
337.endif
338.if !target(afterdepend)
339afterdepend:
340.endif
341.endif
342
343.if defined(SRCS)
344.if ${CTAGS:T} == "gtags"
345CLEANDEPENDFILES+=	GPATH GRTAGS GSYMS GTAGS
346.if defined(HTML)
347CLEANDEPENDDIRS+=	HTML
348.endif
349.else
350CLEANDEPENDFILES+=	tags
351.endif
352.endif
353.if !target(cleandepend)
354cleandepend:
355.if !empty(CLEANDEPENDFILES)
356	rm -f ${CLEANDEPENDFILES}
357.endif
358.if !empty(CLEANDEPENDDIRS)
359	rm -rf ${CLEANDEPENDDIRS}
360.endif
361.endif
362.ORDER: cleandepend all
363.ORDER: cleandepend depend
364.if ${MK_AUTO_OBJ} == "yes"
365.ORDER: cleanobj depend
366.ORDER: cleandir depend
367.endif
368
369.if !target(checkdpadd) && (defined(DPADD) || defined(LDADD))
370_LDADD_FROM_DPADD=	${DPADD:R:T:C;^lib(.*)$;-l\1;g}
371# Ignore -Wl,--start-group/-Wl,--end-group as it might be required in the
372# LDADD list due to unresolved symbols
373_LDADD_CANONICALIZED=	${LDADD:N:R:T:C;^lib(.*)$;-l\1;g:N-Wl,--[es]*-group}
374checkdpadd:
375.if ${_LDADD_FROM_DPADD} != ${_LDADD_CANONICALIZED}
376	@echo ${.CURDIR}
377	@echo "DPADD -> ${_LDADD_FROM_DPADD}"
378	@echo "LDADD -> ${_LDADD_CANONICALIZED}"
379.endif
380.endif
381