xref: /freebsd/share/mk/bsd.dep.mk (revision 3e41d09d08f5bfa2fc1386241f334b865d6da085)
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
58# Keep `tags' here, before SRCS are mangled below for `depend'.
59.if !target(tags) && defined(SRCS) && !defined(NO_TAGS)
60tags: ${SRCS}
61.if ${CTAGS:T} == "gtags"
62	@cd ${.CURDIR} && ${CTAGS} ${GTAGSFLAGS} ${.OBJDIR}
63.if defined(HTML)
64	@cd ${.CURDIR} && htags ${HTAGSFLAGS} -d ${.OBJDIR} ${.OBJDIR}
65.endif
66.else
67	@${CTAGS} ${CTAGSFLAGS} -f /dev/stdout \
68	    ${.ALLSRC:N*.h} | sed "s;${.CURDIR}/;;" > ${.TARGET}
69.endif
70.endif
71
72# Skip reading .depend when not needed to speed up tree-walks
73# and simple lookups.
74.if !empty(.MAKEFLAGS:M-V${_V_READ_DEPEND}) || make(obj) || make(clean*) || \
75    make(install*) || make(analyze)
76_SKIP_READ_DEPEND=	1
77.if ${MK_DIRDEPS_BUILD} == "no"
78.MAKE.DEPENDFILE=	/dev/null
79.endif
80.endif
81
82.if defined(SRCS)
83CLEANFILES?=
84
85.for _S in ${SRCS:N*.[dhly]}
86OBJS_DEPEND_GUESS.${_S:R}.o=	${_S}
87.endfor
88
89# Lexical analyzers
90.for _LSRC in ${SRCS:M*.l:N*/*}
91.for _LC in ${_LSRC:R}.c
92${_LC}: ${_LSRC}
93	${LEX} ${LFLAGS} -o${.TARGET} ${.ALLSRC}
94OBJS_DEPEND_GUESS.${_LC:R}.o=	${_LC}
95SRCS:=	${SRCS:S/${_LSRC}/${_LC}/}
96CLEANFILES+= ${_LC}
97.endfor
98.endfor
99
100# Yacc grammars
101.for _YSRC in ${SRCS:M*.y:N*/*}
102.for _YC in ${_YSRC:R}.c
103SRCS:=	${SRCS:S/${_YSRC}/${_YC}/}
104CLEANFILES+= ${_YC}
105.if !empty(YFLAGS:M-d) && !empty(SRCS:My.tab.h)
106.ORDER: ${_YC} y.tab.h
107${_YC} y.tab.h: ${_YSRC}
108	${YACC} ${YFLAGS} ${.ALLSRC}
109	cp y.tab.c ${_YC}
110CLEANFILES+= y.tab.c y.tab.h
111.elif !empty(YFLAGS:M-d)
112.for _YH in ${_YC:R}.h
113.ORDER: ${_YC} ${_YH}
114${_YC} ${_YH}: ${_YSRC}
115	${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
116SRCS+=	${_YH}
117CLEANFILES+= ${_YH}
118.endfor
119.else
120${_YC}: ${_YSRC}
121	${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
122.endif
123OBJS_DEPEND_GUESS.${_YC:R}.o=	${_YC}
124.endfor
125.endfor
126
127# DTrace probe definitions
128.if ${SRCS:M*.d}
129CFLAGS+=	-I${.OBJDIR}
130.endif
131.for _DSRC in ${SRCS:M*.d:N*/*}
132.for _D in ${_DSRC:R}
133SRCS+=	${_D}.h
134${_D}.h: ${_DSRC}
135	${DTRACE} ${DTRACEFLAGS} -h -s ${.ALLSRC}
136SRCS:=	${SRCS:S/^${_DSRC}$//}
137OBJS+=	${_D}.o
138CLEANFILES+= ${_D}.h ${_D}.o
139${_D}.o: ${_DSRC} ${OBJS:S/^${_D}.o$//}
140	@rm -f ${.TARGET}
141	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
142.if defined(LIB)
143CLEANFILES+= ${_D}.So ${_D}.po
144${_D}.So: ${_DSRC} ${SOBJS:S/^${_D}.So$//}
145	@rm -f ${.TARGET}
146	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
147${_D}.po: ${_DSRC} ${POBJS:S/^${_D}.po$//}
148	@rm -f ${.TARGET}
149	${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
150.endif
151.endfor
152.endfor
153
154
155.if !empty(.MAKE.MODE:Mmeta) && empty(.MAKE.MODE:Mnofilemon)
156_meta_filemon=	1
157.endif
158.if ${MAKE_VERSION} < 20160220
159DEPEND_MP?=	-MP
160.endif
161# Handle OBJS=../somefile.o hacks.  Just replace '/' rather than use :T to
162# avoid collisions.
163DEPEND_FILTER=	C,/,_,g
164DEPENDSRCS=	${SRCS:M*.[cSC]} ${SRCS:M*.cxx} ${SRCS:M*.cpp} ${SRCS:M*.cc}
165.if !empty(DEPENDSRCS)
166DEPENDOBJS+=	${DEPENDSRCS:R:S,$,.o,}
167.endif
168DEPENDFILES_OBJS=	${DEPENDOBJS:O:u:${DEPEND_FILTER}:C/^/${DEPENDFILE}./}
169DEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF${DEPENDFILE}.${.TARGET:${DEPEND_FILTER}}
170DEPEND_CFLAGS+=	-MT${.TARGET}
171# Skip generating or including .depend.* files if in meta+filemon mode since
172# it will track dependencies itself.  OBJS_DEPEND_GUESS is still used though.
173.if !defined(_meta_filemon)
174.if defined(.PARSEDIR)
175# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
176# as those are the only ones we will include.
177DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:M${.TARGET:${DEPEND_FILTER}}}" != ""
178CFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
179.else
180CFLAGS+=	${DEPEND_CFLAGS}
181.endif
182.if !defined(_SKIP_READ_DEPEND)
183.for __depend_obj in ${DEPENDFILES_OBJS}
184.if ${MAKE_VERSION} < 20160220
185.sinclude "${.OBJDIR}/${__depend_obj}"
186.else
187.dinclude "${.OBJDIR}/${__depend_obj}"
188.endif
189.endfor
190.endif	# !defined(_SKIP_READ_DEPEND)
191.endif	# !defined(_meta_filemon)
192.endif	# defined(SRCS)
193
194.if ${MK_DIRDEPS_BUILD} == "yes"
195# Prevent meta.autodep.mk from tracking "local dependencies".
196.depend:
197.include <meta.autodep.mk>
198# If using filemon then _EXTRADEPEND is skipped since it is not needed.
199.if empty(.MAKE.MODE:Mnofilemon)
200# this depend: bypasses that below
201# the dependency helps when bootstrapping
202depend: beforedepend ${DPSRCS} ${SRCS} afterdepend
203beforedepend:
204afterdepend: beforedepend
205.endif
206.endif
207
208# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
209# For meta+filemon the .meta file is checked for since it is the dependency
210# file used.
211.for __obj in ${DEPENDOBJS:O:u}
212.if (defined(_meta_filemon) && !exists(${.OBJDIR}/${__obj}.meta)) || \
213    (!defined(_meta_filemon) && !exists(${.OBJDIR}/${DEPENDFILE}.${__obj}))
214${__obj}: ${OBJS_DEPEND_GUESS}
215${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
216.endif
217.endfor
218
219# Always run 'make depend' to generate dependencies early and to avoid the
220# need for manually running it.  The dirdeps build should only do this in
221# sub-makes though since MAKELEVEL0 is for dirdeps calculations.
222.if ${MK_DIRDEPS_BUILD} == "no" || ${.MAKE.LEVEL} > 0
223beforebuild: depend
224.endif
225
226.if !target(depend)
227.if defined(SRCS)
228depend: beforedepend ${DEPENDFILE} afterdepend
229
230# Tell bmake not to look for generated files via .PATH
231.NOPATH: ${DEPENDFILE} ${DEPENDFILES_OBJS}
232
233DPSRCS+= ${SRCS}
234# A .depend file will only be generated if there are commands in
235# beforedepend/_EXTRADEPEND/afterdepend.  The target is kept
236# to allow 'make depend' to generate files.
237${DEPENDFILE}: ${DPSRCS}
238.if exists(${.OBJDIR}/${DEPENDFILE})
239	rm -f ${DEPENDFILE}
240.endif
241.if target(_EXTRADEPEND)
242_EXTRADEPEND: .USE
243${DEPENDFILE}: _EXTRADEPEND
244.endif
245
246.ORDER: ${DEPENDFILE} afterdepend
247.else
248depend: beforedepend afterdepend
249.endif
250.if !target(beforedepend)
251beforedepend:
252.else
253.ORDER: beforedepend ${DEPENDFILE}
254.ORDER: beforedepend afterdepend
255.endif
256.if !target(afterdepend)
257afterdepend:
258.endif
259.endif
260
261.if defined(SRCS)
262.if ${CTAGS:T} == "gtags"
263CLEANDEPENDFILES+=	GPATH GRTAGS GSYMS GTAGS
264.if defined(HTML)
265CLEANDEPENDDIRS+=	HTML
266.endif
267.else
268CLEANDEPENDFILES+=	tags
269.endif
270.endif
271.if !target(cleandepend)
272cleandepend:
273.if !empty(CLEANDEPENDFILES)
274	rm -f ${CLEANDEPENDFILES}
275.endif
276.if !empty(CLEANDEPENDDIRS)
277	rm -rf ${CLEANDEPENDDIRS}
278.endif
279.endif
280
281.if !target(checkdpadd) && (defined(DPADD) || defined(LDADD))
282_LDADD_FROM_DPADD=	${DPADD:R:T:C;^lib(.*)$;-l\1;g}
283# Ignore -Wl,--start-group/-Wl,--end-group as it might be required in the
284# LDADD list due to unresolved symbols
285_LDADD_CANONICALIZED=	${LDADD:N:R:T:C;^lib(.*)$;-l\1;g:N-Wl,--[es]*-group}
286checkdpadd:
287.if ${_LDADD_FROM_DPADD} != ${_LDADD_CANONICALIZED}
288	@echo ${.CURDIR}
289	@echo "DPADD -> ${_LDADD_FROM_DPADD}"
290	@echo "LDADD -> ${_LDADD_CANONICALIZED}"
291.endif
292.endif
293