3 # The include file <bsd.dep.mk> handles Makefile dependencies.
8 # CLEANDEPENDDIRS Additional directories to remove for the cleandepend
11 # CLEANDEPENDFILES Additional files to remove for the cleandepend target.
13 # CTAGS A tags file generation program [gtags]
15 # CTAGSFLAGS Options for ctags(1) [not set]
17 # DEPENDFILE dependencies file [.depend]
19 # GTAGSFLAGS Options for gtags(1) [-o]
21 # HTAGSFLAGS Options for htags(1) [not set]
23 # SRCS List of source files (c, c++, assembler)
25 # DPSRCS List of source files which are needed for generating
26 # dependencies, ${SRCS} are always part of it.
31 # remove ${CLEANDEPENDFILES}; remove ${CLEANDEPENDDIRS} and all
35 # Make the dependencies for the source files, and store
36 # them in the file ${DEPENDFILE}.
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
44 .if !target(__<bsd.init.mk>__)
45 .error bsd.dep.mk cannot be included directly.
53 .if ${MK_DIRDEPS_BUILD} == "no"
54 .MAKE.DEPENDFILE= ${DEPENDFILE}
56 CLEANDEPENDFILES+= ${DEPENDFILE} ${DEPENDFILE}.*
57 .if ${MK_META_MODE} == "yes"
58 CLEANDEPENDFILES+= *.meta
61 # Keep `tags' here, before SRCS are mangled below for `depend'.
62 .if !target(tags) && defined(SRCS) && !defined(NO_TAGS)
64 .if ${CTAGS:T} == "gtags"
65 @cd ${.CURDIR} && ${CTAGS} ${GTAGSFLAGS} ${.OBJDIR}
67 @cd ${.CURDIR} && htags ${HTAGSFLAGS} -d ${.OBJDIR} ${.OBJDIR}
70 @${CTAGS} ${CTAGSFLAGS} -f /dev/stdout \
71 ${.ALLSRC:N*.h} | sed "s;${.CURDIR}/;;" > ${.TARGET}
75 .if !empty(.MAKE.MODE:Mmeta) && empty(.MAKE.MODE:Mnofilemon)
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]*))
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)
94 .if ${MK_DIRDEPS_BUILD} == "no"
95 .if defined(_SKIP_DEPEND) || defined(_meta_filemon)
96 .MAKE.DEPENDFILE= /dev/null
103 .for _S in ${SRCS:N*.[dhly]}
104 OBJS_DEPEND_GUESS.${_S:${OBJS_SRCS_FILTER:ts:}}.o+= ${_S}
108 .for _LSRC in ${SRCS:M*.l:N*/*}
109 .for _LC in ${_LSRC:R}.c
111 ${LEX} ${LFLAGS} ${LFLAGS.${_LSRC}} -o${.TARGET} ${.ALLSRC}
112 OBJS_DEPEND_GUESS.${_LC:R}.o+= ${_LC}
113 SRCS:= ${SRCS:S/${_LSRC}/${_LC}/}
119 .for _YSRC in ${SRCS:M*.y:N*/*}
120 .for _YC in ${_YSRC:R}.c
121 SRCS:= ${SRCS:S/${_YSRC}/${_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.
126 y.tab.h: ${_YC} .NOMETA
127 # Force rebuild the .c file if any of its other outputs are missing.
132 ${YACC} ${YFLAGS} ${YFLAGS.${_YSRC}} ${.ALLSRC}
134 CLEANFILES+= 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.
145 ${YACC} ${YFLAGS} ${YFLAGS.${_YSRC}} -o ${_YC} ${.ALLSRC}
151 ${YACC} ${YFLAGS} ${YFLAGS.${_YSRC}} -o ${_YC} ${.ALLSRC}
153 OBJS_DEPEND_GUESS.${_YC:R}.o+= ${_YC}
157 # DTrace probe definitions
159 CFLAGS+= -I${.OBJDIR}
161 .for _DSRC in ${SRCS:M*.d:N*/*}
162 .for _D in ${_DSRC:R}
165 ${DTRACE} ${DTRACEFLAGS} -h -s ${.ALLSRC}
166 SRCS:= ${SRCS:S/^${_DSRC}$//}
168 CLEANFILES+= ${_D}.h ${_D}.o
169 ${_D}.o: ${_DSRC} ${OBJS:S/^${_D}.o$//}
171 ${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
173 CLEANFILES+= ${_D}.pico ${_D}.pieo ${_D}.po ${_D}.nossppico
174 ${_D}.pico: ${_DSRC} ${SOBJS:S/^${_D}.pico$//}
176 ${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
177 ${_D}.pieo: ${_DSRC} ${OBJS:S/^${_D}.pieo$//}
179 ${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
180 ${_D}.po: ${_DSRC} ${POBJS:S/^${_D}.po$//}
182 ${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
183 ${_D}.nossppico: ${_DSRC} ${SOBJS:S/^${_D}.nossppico$//}
185 ${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
189 .endif # defined(SRCS)
191 .if ${MAKE_VERSION} < 20160220
194 # Handle OBJS=../somefile.o hacks. Just replace '/' rather than use :T to
196 DEPEND_FILTER= C,/,_,g
198 .if !defined(_ALLOW_ABSOLUTE_OBJ_PATH) && ${OBJS:M/*}
199 # Absolute paths to OBJS should be an error inside ${SRCTOP}, but some users
200 # might be relying on this feature, so add an opt-out mechanism.
201 .if defined(SRCTOP) && ${OBJS:M${SRCTOP}*}
202 .error "$$OBJS inside $$SRCTOP not allowed: ${OBJS:M${SRCTOP}*}"
203 .elif ${OBJS:N${_ABSOLUTE_PATH_OBJS}:M/*}
204 .error "$$OBJS absolute path not allowed: ${OBJS:N${_ABSOLUTE_PATH_OBJS}:M/*}. \
205 If this is intended, add them to _ABSOLUTE_PATH_OBJS to silence this error\
206 or define _ALLOW_ABSOLUTE_OBJ_PATH to disable this diagnostic."
211 DEPENDSRCS+= ${SRCS:M*.[cSC]} ${SRCS:M*.cxx} ${SRCS:M*.cpp} ${SRCS:M*.cc}
212 DEPENDSRCS+= ${DPSRCS:M*.[cSC]} ${DPSRCS:M*.cxx} ${DPSRCS:M*.cpp} ${DPSRCS:M*.cc}
213 .if !empty(DEPENDSRCS)
214 DEPENDOBJS+= ${DEPENDSRCS:${OBJS_SRCS_FILTER:ts:}:S,$,.o,}
216 .endif # !empty(OBJS)
217 .if !empty(DEPENDOBJS)
218 DEPENDFILES+= ${DEPENDOBJS:O:u:${DEPEND_FILTER}:C/^/${DEPENDFILE}./}
220 .if defined(_SKIP_DEPEND)
221 # Don't bother statting any .meta files for .depend*
222 ${DEPENDOBJS}: .NOMETA
223 ${DEPENDFILE}: .NOMETA
224 # Unset these to avoid looping/statting on them later.
228 .endif # defined(_SKIP_DEPEND)
229 DEPEND_CFLAGS+= -MD ${DEPEND_MP} -MF${DEPENDFILE}.${.TARGET:${DEPEND_FILTER}}
230 DEPEND_CFLAGS+= -MT${.TARGET}
231 .if !defined(_meta_filemon)
232 .if !empty(DEPEND_CFLAGS)
233 # Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
234 # as those are the only ones we will include.
235 DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:${DEPEND_FILTER}:M${.TARGET:${DEPEND_FILTER}}}" != ""
236 CFLAGS+= ${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
238 .for __depend_obj in ${DEPENDFILES}
239 .if ${MAKE_VERSION} < 20160220
240 .sinclude "${.OBJDIR}/${__depend_obj}"
242 .dinclude "${.OBJDIR}/${__depend_obj}"
245 .endif # !defined(_meta_filemon)
247 .if ${MK_DIRDEPS_BUILD} == "yes" && ${.MAKE.DEPENDFILE} != "/dev/null"
248 # Prevent meta.autodep.mk from tracking "local dependencies".
250 .include <meta.autodep.mk>
251 # If using filemon then _EXTRADEPEND is skipped since it is not needed.
252 .if defined(_meta_filemon)
253 # this depend: bypasses that below
254 # the dependency helps when bootstrapping
255 depend: beforedepend ${DPSRCS} ${SRCS} afterdepend
257 afterdepend: beforedepend
261 # Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
262 # For meta+filemon the .meta file is checked for since it is the dependency
264 .for __obj in ${DEPENDOBJS:O:u}
265 # If the obj has any '/', then replace with '_'. For meta files, this is
266 # mimicing what bmake's meta_name() does and adding in the full path
267 # as well to ensure that the expected meta file is read.
269 .if ${MAKE_VERSION} < 20171028
270 _meta_obj= ${.OBJDIR:C,/,_,g}_${__obj:C,/,_,g}.meta
272 _meta_obj= ${__obj:C,/,_,g}.meta
273 .endif # ${MAKE_VERSION} < 20171028
275 _meta_obj= ${__obj}.meta
276 .endif # ${__obj:M*/*}
277 _dep_obj= ${DEPENDFILE}.${__obj:${DEPEND_FILTER}}
278 .if defined(_meta_filemon)
279 _depfile= ${.OBJDIR}/${_meta_obj}
281 _depfile= ${.OBJDIR}/${_dep_obj}
283 .if !exists(${_depfile}) || defined(_meta_filemon)
284 # - Headers are normally built in beforebuild when included in DPSRCS or SRCS.
285 # So we don't need it as a guessed dependency (it may lead to cyclic problems
286 # if custom rules are defined). The only time this causes a problem is when
287 # 'make foo.o' is ran.
288 # - For meta mode we still need to know which file to depend on to avoid
289 # ambiguous suffix transformation rules from .PATH. Meta mode does not
290 # use .depend files when filemon is in use.
291 .if !target(${__obj})
292 ${__obj}: ${OBJS_DEPEND_GUESS}
294 ${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
295 .endif # !exists(${_depfile}) || defined(_meta_filemon)
298 # Always run 'make depend' to generate dependencies early and to avoid the
299 # need for manually running it. The dirdeps build should only do this in
300 # sub-makes though since MAKELEVEL0 is for dirdeps calculations.
301 .if ${MK_DIRDEPS_BUILD} == "no" || ${.MAKE.LEVEL} > 0
307 depend: beforedepend ${DEPENDFILE} afterdepend
309 # Tell bmake not to look for generated files via .PATH
310 .NOPATH: ${DEPENDFILE} ${DEPENDFILES}
312 # A .depend file will only be generated if there are commands in
313 # beforedepend/_EXTRADEPEND/afterdepend The _EXTRADEPEND target is
314 # ignored if using meta+filemon since it handles all dependencies. The other
315 # targets are kept as they be used for generating something. The target is
316 # kept to allow 'make depend' to generate files.
317 ${DEPENDFILE}: ${SRCS} ${DPSRCS}
318 .if !defined(_SKIP_DEPEND)
319 .if exists(${.OBJDIR}/${DEPENDFILE}) || \
320 ((commands(beforedepend) || \
321 (!defined(_meta_filemon) && commands(_EXTRADEPEND)) || \
322 commands(afterdepend)) && !empty(.MAKE.MODE:Mmeta))
326 .if !defined(_meta_filemon) && target(_EXTRADEPEND)
328 ${DEPENDFILE}: _EXTRADEPEND
331 .ORDER: ${DEPENDFILE} afterdepend
333 depend: beforedepend afterdepend
335 .if !target(beforedepend)
338 .ORDER: beforedepend ${DEPENDFILE}
339 .ORDER: beforedepend afterdepend
341 .if !target(afterdepend)
347 .if ${CTAGS:T} == "gtags"
348 CLEANDEPENDFILES+= GPATH GRTAGS GSYMS GTAGS
350 CLEANDEPENDDIRS+= HTML
353 CLEANDEPENDFILES+= tags
356 .if !target(cleandepend)
358 .if !empty(CLEANDEPENDFILES)
359 rm -f ${CLEANDEPENDFILES}
361 .if !empty(CLEANDEPENDDIRS)
362 rm -rf ${CLEANDEPENDDIRS}
365 .ORDER: cleandepend all
366 .ORDER: cleandepend depend
367 .if ${MK_AUTO_OBJ} == "yes"
368 .ORDER: cleanobj depend
369 .ORDER: cleandir depend
372 .if !target(checkdpadd) && (defined(DPADD) || defined(LDADD))
373 _LDADD_FROM_DPADD= ${DPADD:R:T:C;^lib(.*)$;-l\1;g}
374 # Ignore -Wl,--start-group/-Wl,--end-group as it might be required in the
375 # LDADD list due to unresolved symbols
376 _LDADD_CANONICALIZED= ${LDADD:N:R:T:C;^lib(.*)$;-l\1;g:N-Wl,--[es]*-group}
378 .if ${_LDADD_FROM_DPADD} != ${_LDADD_CANONICALIZED}
380 @echo "DPADD -> ${_LDADD_FROM_DPADD}"
381 @echo "LDADD -> ${_LDADD_CANONICALIZED}"