xref: /freebsd-13-stable/share/mk/bsd.dep.mk (revision 023fc80ee38a117fa65b2ccb2abf8bdc7dbd6fd9)
1#
2# The include file <bsd.dep.mk> handles Makefile dependencies.
3#
4#
5# +++ variables +++
6#
7# CLEANDEPENDDIRS	Additional directories to remove for the cleandepend
8# 			target.
9#
10# CLEANDEPENDFILES	Additional files to remove for the cleandepend target.
11#
12# CTAGS		A tags file generation program [gtags]
13#
14# CTAGSFLAGS	Options for ctags(1) [not set]
15#
16# DEPENDFILE	dependencies file [.depend]
17#
18# GTAGSFLAGS	Options for gtags(1) [-o]
19#
20# HTAGSFLAGS	Options for htags(1) [not set]
21#
22# SRCS          List of source files (c, c++, assembler)
23#
24# DPSRCS	List of source files which are needed for generating
25#		dependencies, ${SRCS} are always part of it.
26#
27# +++ targets +++
28#
29#	cleandepend:
30#		remove ${CLEANDEPENDFILES}; remove ${CLEANDEPENDDIRS} and all
31#		contents.
32#
33#	depend:
34#		Make the dependencies for the source files, and store
35#		them in the file ${DEPENDFILE}.
36#
37#	tags:
38#		In "ctags" mode, create a tags file for the source files.
39#		In "gtags" mode, create a (GLOBAL) gtags file for the
40#		source files.  If HTML is defined, htags(1) is also run
41#		after gtags(1).
42
43.if !target(__<bsd.init.mk>__)
44.error bsd.dep.mk cannot be included directly.
45.endif
46
47CTAGS?=		gtags
48CTAGSFLAGS?=
49GTAGSFLAGS?=	-o
50HTAGSFLAGS?=
51
52.if ${MK_DIRDEPS_BUILD} == "no"
53.MAKE.DEPENDFILE= ${DEPENDFILE}
54.endif
55CLEANDEPENDFILES+=	${DEPENDFILE} ${DEPENDFILE}.*
56.if ${MK_META_MODE} == "yes"
57CLEANDEPENDFILES+=	*.meta
58.endif
59
60# Keep `tags' here, before SRCS are mangled below for `depend'.
61.if !target(tags) && defined(SRCS) && !defined(NO_TAGS)
62tags: ${SRCS}
63.if ${CTAGS:T} == "gtags"
64	@cd ${.CURDIR} && ${CTAGS} ${GTAGSFLAGS} ${.OBJDIR}
65.if defined(HTML)
66	@cd ${.CURDIR} && htags ${HTAGSFLAGS} -d ${.OBJDIR} ${.OBJDIR}
67.endif
68.else
69	@${CTAGS} ${CTAGSFLAGS} -f /dev/stdout \
70	    ${.ALLSRC:N*.h} | sed "s;${.CURDIR}/;;" > ${.TARGET}
71.endif
72.endif
73
74.if !empty(.MAKE.MODE:Mmeta) && empty(.MAKE.MODE:Mnofilemon)
75_meta_filemon=	1
76.endif
77# By default META_MODE is disabled in bmake if there is no OBJDIR
78# unless .MAKE.MODE contains "curdirOk=[^0nNfF]"
79.if defined(_meta_filemon) && ${.OBJDIR} == ${.CURDIR} && \
80    (empty(.MAKE.MODE:tl:Mcurdirok=*) || \
81    !empty(.MAKE.MODE:tl:Mcurdirok=[0NnFf]*))
82.undef _meta_filemon
83.endif
84
85# Skip reading .depend when not needed to speed up tree-walks and simple
86# lookups.  See _SKIP_BUILD logic in bsd.init.mk for more details.
87# Also skip generating or including .depend.* files if in meta+filemon mode
88# since it will track dependencies itself.  OBJS_DEPEND_GUESS is still used
89# for _meta_filemon but not for _SKIP_DEPEND.
90.if !defined(NO_SKIP_DEPEND) && defined(_SKIP_BUILD)
91_SKIP_DEPEND=	1
92.endif
93.if ${MK_DIRDEPS_BUILD} == "no"
94.if defined(_SKIP_DEPEND) || defined(_meta_filemon)
95.MAKE.DEPENDFILE=	/dev/null
96.endif
97.endif
98
99.if defined(SRCS)
100CLEANFILES?=
101
102.for _S in ${SRCS:N*.[dhly]}
103OBJS_DEPEND_GUESS.${_S:${OBJS_SRCS_FILTER:ts:}}.o+=	${_S}
104.endfor
105
106# Lexical analyzers
107.for _LSRC in ${SRCS:M*.l:N*/*}
108.for _LC in ${_LSRC:R}.c
109${_LC}: ${_LSRC}
110	${LEX} ${LFLAGS} ${LFLAGS.${_LSRC}} -o${.TARGET} ${.ALLSRC}
111OBJS_DEPEND_GUESS.${_LC:R}.o+=	${_LC}
112SRCS:=	${SRCS:S/${_LSRC}/${_LC}/}
113CLEANFILES+= ${_LC}
114.endfor
115.endfor
116
117# Yacc grammars
118.for _YSRC in ${SRCS:M*.y:N*/*}
119.for _YC in ${_YSRC:R}.c
120SRCS:=	${SRCS:S/${_YSRC}/${_YC}/}
121CLEANFILES+= ${_YC}
122.if !empty(YFLAGS:M-d) && !empty(SRCS:My.tab.h)
123# Multi-output targets both expect a .meta file and will fight over it. Only
124# allow it on the .c file instead.
125y.tab.h: ${_YC} .NOMETA
126# Force rebuild the .c file if any of its other outputs are missing.
127.if !exists(y.tab.h)
128${_YC}: .PHONY .META
129.endif
130${_YC}: ${_YSRC}
131	${YACC} ${YFLAGS} ${YFLAGS.${_YSRC}} ${.ALLSRC}
132	cp y.tab.c ${_YC}
133CLEANFILES+= y.tab.c y.tab.h
134.elif !empty(YFLAGS:M-d)
135.for _YH in ${_YC:R}.h
136# Multi-output targets both expect a .meta file and will fight over it. Only
137# allow it on the .c file instead.
138${_YH}: ${_YC} .NOMETA
139# Force rebuild the .c file if any of its other outputs are missing.
140.if !exists(${_YH})
141${_YC}: .PHONY .META
142.endif
143${_YC}: ${_YSRC}
144	${YACC} ${YFLAGS} ${YFLAGS.${_YSRC}} -o ${_YC} ${.ALLSRC}
145SRCS+=	${_YH}
146CLEANFILES+= ${_YH}
147.endfor
148.else
149${_YC}: ${_YSRC}
150	${YACC} ${YFLAGS} ${YFLAGS.${_YSRC}} -o ${_YC} ${.ALLSRC}
151.endif
152OBJS_DEPEND_GUESS.${_YC:R}.o+=	${_YC}
153.endfor
154.endfor
155
156# DTrace probe definitions
157.if ${SRCS:M*.d}
158CFLAGS+=	-I${.OBJDIR}
159.endif
160.for _DSRC in ${SRCS:M*.d:N*/*}
161.for _D in ${_DSRC:R}
162SRCS+=	${_D}.h
163${_D}.h: ${_DSRC}
164	${DTRACE} ${DTRACEFLAGS} -h -s ${.ALLSRC}
165SRCS:=	${SRCS:S/^${_DSRC}$//}
166OBJS+=	${_D}.o
167CLEANFILES+= ${_D}.h ${_D}.o
168${_D}.o: ${_DSRC} ${OBJS:S/^${_D}.o$//}
169	@rm -f ${.TARGET}
170	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
171.if defined(LIB)
172CLEANFILES+= ${_D}.pico ${_D}.pieo ${_D}.po ${_D}.nossppico
173${_D}.pico: ${_DSRC} ${SOBJS:S/^${_D}.pico$//}
174	@rm -f ${.TARGET}
175	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
176${_D}.pieo: ${_DSRC} ${OBJS:S/^${_D}.pieo$//}
177	@rm -f ${.TARGET}
178	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
179${_D}.po: ${_DSRC} ${POBJS:S/^${_D}.po$//}
180	@rm -f ${.TARGET}
181	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
182${_D}.nossppico: ${_DSRC} ${SOBJS:S/^${_D}.nossppico$//}
183	@rm -f ${.TARGET}
184	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
185.endif
186.endfor
187.endfor
188.endif	# defined(SRCS)
189
190.if ${MAKE_VERSION} < 20160220
191DEPEND_MP?=	-MP
192.endif
193# Handle OBJS=../somefile.o hacks.  Just replace '/' rather than use :T to
194# avoid collisions.
195DEPEND_FILTER=	C,/,_,g
196.if !empty(OBJS)
197DEPENDOBJS+=	${OBJS}
198.else
199DEPENDSRCS+=	${SRCS:M*.[cSC]} ${SRCS:M*.cxx} ${SRCS:M*.cpp} ${SRCS:M*.cc}
200DEPENDSRCS+=	${DPSRCS:M*.[cSC]} ${DPSRCS:M*.cxx} ${DPSRCS:M*.cpp} ${DPSRCS:M*.cc}
201.if !empty(DEPENDSRCS)
202DEPENDOBJS+=	${DEPENDSRCS:${OBJS_SRCS_FILTER:ts:}:S,$,.o,}
203.endif
204.endif	# !empty(OBJS)
205.if !empty(DEPENDOBJS)
206DEPENDFILES+=	${DEPENDOBJS:O:u:${DEPEND_FILTER}:C/^/${DEPENDFILE}./}
207.endif
208.if defined(_SKIP_DEPEND)
209# Don't bother statting any .meta files for .depend*
210${DEPENDOBJS}:	.NOMETA
211${DEPENDFILE}:	.NOMETA
212# Unset these to avoid looping/statting on them later.
213.undef DEPENDSRCS
214.undef DEPENDOBJS
215.undef DEPENDFILES
216.endif	# defined(_SKIP_DEPEND)
217DEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF${DEPENDFILE}.${.TARGET:${DEPEND_FILTER}}
218DEPEND_CFLAGS+=	-MT${.TARGET}
219.if !defined(_meta_filemon)
220.if !empty(DEPEND_CFLAGS)
221# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
222# as those are the only ones we will include.
223DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:${DEPEND_FILTER}:M${.TARGET:${DEPEND_FILTER}}}" != ""
224CFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
225.endif
226.for __depend_obj in ${DEPENDFILES}
227.if ${MAKE_VERSION} < 20160220
228.sinclude "${.OBJDIR}/${__depend_obj}"
229.else
230.dinclude "${.OBJDIR}/${__depend_obj}"
231.endif
232.endfor
233.endif	# !defined(_meta_filemon)
234
235.if ${MK_DIRDEPS_BUILD} == "yes" && ${.MAKE.DEPENDFILE} != "/dev/null"
236# Prevent meta.autodep.mk from tracking "local dependencies".
237.depend:
238.include <meta.autodep.mk>
239# If using filemon then _EXTRADEPEND is skipped since it is not needed.
240.if defined(_meta_filemon)
241# this depend: bypasses that below
242# the dependency helps when bootstrapping
243depend: beforedepend ${DPSRCS} ${SRCS} afterdepend
244beforedepend:
245afterdepend: beforedepend
246.endif
247.endif
248
249# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
250# For meta+filemon the .meta file is checked for since it is the dependency
251# file used.
252.for __obj in ${DEPENDOBJS:O:u}
253# If the obj has any '/', then replace with '_'.  For meta files, this is
254# mimicing what bmake's meta_name() does and adding in the full path
255# as well to ensure that the expected meta file is read.
256.if ${__obj:M*/*}
257.if ${MAKE_VERSION} < 20171028
258_meta_obj=	${.OBJDIR:C,/,_,g}_${__obj:C,/,_,g}.meta
259.else
260_meta_obj=	${__obj:C,/,_,g}.meta
261.endif	# ${MAKE_VERSION} < 20171028
262.else
263_meta_obj=	${__obj}.meta
264.endif	# ${__obj:M*/*}
265_dep_obj=	${DEPENDFILE}.${__obj:${DEPEND_FILTER}}
266.if defined(_meta_filemon)
267_depfile=	${.OBJDIR}/${_meta_obj}
268.else
269_depfile=	${.OBJDIR}/${_dep_obj}
270.endif
271.if !exists(${_depfile}) || defined(_meta_filemon)
272# - Headers are normally built in beforebuild when included in DPSRCS or SRCS.
273#   So we don't need it as a guessed dependency (it may lead to cyclic problems
274#   if custom rules are defined).  The only time this causes a problem is when
275#   'make foo.o' is ran.
276# - For meta mode we still need to know which file to depend on to avoid
277#   ambiguous suffix transformation rules from .PATH.  Meta mode does not
278#   use .depend files when filemon is in use.
279.if !target(${__obj})
280${__obj}: ${OBJS_DEPEND_GUESS}
281.endif
282${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
283.endif	# !exists(${_depfile}) || defined(_meta_filemon)
284.endfor
285
286# Always run 'make depend' to generate dependencies early and to avoid the
287# need for manually running it.  The dirdeps build should only do this in
288# sub-makes though since MAKELEVEL0 is for dirdeps calculations.
289.if ${MK_DIRDEPS_BUILD} == "no" || ${.MAKE.LEVEL} > 0
290beforebuild: depend
291.endif
292
293.if !target(depend)
294.if defined(SRCS)
295depend: beforedepend ${DEPENDFILE} afterdepend
296
297# Tell bmake not to look for generated files via .PATH
298.NOPATH: ${DEPENDFILE} ${DEPENDFILES}
299
300# A .depend file will only be generated if there are commands in
301# beforedepend/_EXTRADEPEND/afterdepend  The _EXTRADEPEND target is
302# ignored if using meta+filemon since it handles all dependencies.  The other
303# targets are kept as they be used for generating something.  The target is
304# kept to allow 'make depend' to generate files.
305${DEPENDFILE}: ${SRCS} ${DPSRCS}
306.if !defined(_SKIP_DEPEND)
307.if exists(${.OBJDIR}/${DEPENDFILE}) || \
308    ((commands(beforedepend) || \
309    (!defined(_meta_filemon) && commands(_EXTRADEPEND)) || \
310    commands(afterdepend)) && !empty(.MAKE.MODE:Mmeta))
311	rm -f ${DEPENDFILE}
312.endif
313.endif
314.if !defined(_meta_filemon) && target(_EXTRADEPEND)
315_EXTRADEPEND: .USE
316${DEPENDFILE}: _EXTRADEPEND
317.endif
318
319.ORDER: ${DEPENDFILE} afterdepend
320.else
321depend: beforedepend afterdepend
322.endif
323.if !target(beforedepend)
324beforedepend:
325.else
326.ORDER: beforedepend ${DEPENDFILE}
327.ORDER: beforedepend afterdepend
328.endif
329.if !target(afterdepend)
330afterdepend:
331.endif
332.endif
333
334.if defined(SRCS)
335.if ${CTAGS:T} == "gtags"
336CLEANDEPENDFILES+=	GPATH GRTAGS GSYMS GTAGS
337.if defined(HTML)
338CLEANDEPENDDIRS+=	HTML
339.endif
340.else
341CLEANDEPENDFILES+=	tags
342.endif
343.endif
344.if !target(cleandepend)
345cleandepend:
346.if !empty(CLEANDEPENDFILES)
347	rm -f ${CLEANDEPENDFILES}
348.endif
349.if !empty(CLEANDEPENDDIRS)
350	rm -rf ${CLEANDEPENDDIRS}
351.endif
352.endif
353.ORDER: cleandepend all
354.ORDER: cleandepend depend
355.if ${MK_AUTO_OBJ} == "yes"
356.ORDER: cleanobj depend
357.ORDER: cleandir depend
358.endif
359
360.if !target(checkdpadd) && (defined(DPADD) || defined(LDADD))
361_LDADD_FROM_DPADD=	${DPADD:R:T:C;^lib(.*)$;-l\1;g}
362# Ignore -Wl,--start-group/-Wl,--end-group as it might be required in the
363# LDADD list due to unresolved symbols
364_LDADD_CANONICALIZED=	${LDADD:N:R:T:C;^lib(.*)$;-l\1;g:N-Wl,--[es]*-group}
365checkdpadd:
366.if ${_LDADD_FROM_DPADD} != ${_LDADD_CANONICALIZED}
367	@echo ${.CURDIR}
368	@echo "DPADD -> ${_LDADD_FROM_DPADD}"
369	@echo "LDADD -> ${_LDADD_CANONICALIZED}"
370.endif
371.endif
372