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)
79 # Skip reading .depend when not needed to speed up tree-walks and simple
80 # lookups. See _SKIP_BUILD logic in bsd.init.mk for more details.
81 # Also skip generating or including .depend.* files if in meta+filemon mode
82 # since it will track dependencies itself. OBJS_DEPEND_GUESS is still used.
83 .if defined(_SKIP_BUILD) || defined(_meta_filemon)
85 .if ${MK_DIRDEPS_BUILD} == "no" || make(analyze) || make(print-dir) || \
86 make(obj) || make(clean*) || make(destroy*)
87 .MAKE.DEPENDFILE= /dev/null
94 .for _S in ${SRCS:N*.[dhly]}
95 OBJS_DEPEND_GUESS.${_S:R}.o+= ${_S}
99 .for _LSRC in ${SRCS:M*.l:N*/*}
100 .for _LC in ${_LSRC:R}.c
102 ${LEX} ${LFLAGS} -o${.TARGET} ${.ALLSRC}
103 OBJS_DEPEND_GUESS.${_LC:R}.o+= ${_LC}
104 SRCS:= ${SRCS:S/${_LSRC}/${_LC}/}
110 .for _YSRC in ${SRCS:M*.y:N*/*}
111 .for _YC in ${_YSRC:R}.c
112 SRCS:= ${SRCS:S/${_YSRC}/${_YC}/}
114 .if !empty(YFLAGS:M-d) && !empty(SRCS:My.tab.h)
115 .ORDER: ${_YC} y.tab.h
117 ${_YC} y.tab.h: ${_YSRC}
118 ${YACC} ${YFLAGS} ${.ALLSRC}
120 CLEANFILES+= y.tab.c y.tab.h
121 .elif !empty(YFLAGS:M-d)
122 .for _YH in ${_YC:R}.h
123 .ORDER: ${_YC} ${_YH}
125 ${_YC} ${_YH}: ${_YSRC}
126 ${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
132 ${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
134 OBJS_DEPEND_GUESS.${_YC:R}.o+= ${_YC}
138 # DTrace probe definitions
140 CFLAGS+= -I${.OBJDIR}
142 .for _DSRC in ${SRCS:M*.d:N*/*}
143 .for _D in ${_DSRC:R}
146 ${DTRACE} ${DTRACEFLAGS} -h -s ${.ALLSRC}
147 SRCS:= ${SRCS:S/^${_DSRC}$//}
149 CLEANFILES+= ${_D}.h ${_D}.o
150 ${_D}.o: ${_DSRC} ${OBJS:S/^${_D}.o$//}
152 ${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
154 CLEANFILES+= ${_D}.pico ${_D}.po
155 ${_D}.pico: ${_DSRC} ${SOBJS:S/^${_D}.pico$//}
157 ${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
158 ${_D}.po: ${_DSRC} ${POBJS:S/^${_D}.po$//}
160 ${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h}
166 .if ${MAKE_VERSION} < 20160220
169 # Handle OBJS=../somefile.o hacks. Just replace '/' rather than use :T to
171 DEPEND_FILTER= C,/,_,g
172 DEPENDSRCS= ${SRCS:M*.[cSC]} ${SRCS:M*.cxx} ${SRCS:M*.cpp} ${SRCS:M*.cc}
173 .if !empty(DEPENDSRCS)
174 DEPENDOBJS+= ${DEPENDSRCS:R:S,$,.o,}
176 DEPENDFILES_OBJS= ${DEPENDOBJS:O:u:${DEPEND_FILTER}:C/^/${DEPENDFILE}./}
177 DEPEND_CFLAGS+= -MD ${DEPEND_MP} -MF${DEPENDFILE}.${.TARGET:${DEPEND_FILTER}}
178 DEPEND_CFLAGS+= -MT${.TARGET}
179 .if !defined(_meta_filemon)
180 .if defined(.PARSEDIR)
181 # Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
182 # as those are the only ones we will include.
183 DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:${DEPEND_FILTER}:M${.TARGET:${DEPEND_FILTER}}}" != ""
184 CFLAGS+= ${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
186 CFLAGS+= ${DEPEND_CFLAGS}
188 .if !defined(_SKIP_READ_DEPEND)
189 .for __depend_obj in ${DEPENDFILES_OBJS}
190 .if ${MAKE_VERSION} < 20160220
191 .sinclude "${.OBJDIR}/${__depend_obj}"
193 .dinclude "${.OBJDIR}/${__depend_obj}"
196 .endif # !defined(_SKIP_READ_DEPEND)
197 .endif # !defined(_meta_filemon)
198 .endif # defined(SRCS)
200 .if ${MK_DIRDEPS_BUILD} == "yes" && ${.MAKE.DEPENDFILE} != "/dev/null"
201 # Prevent meta.autodep.mk from tracking "local dependencies".
203 .include <meta.autodep.mk>
204 # If using filemon then _EXTRADEPEND is skipped since it is not needed.
205 .if defined(_meta_filemon)
206 # this depend: bypasses that below
207 # the dependency helps when bootstrapping
208 depend: beforedepend ${DPSRCS} ${SRCS} afterdepend
210 afterdepend: beforedepend
214 # Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
215 # For meta+filemon the .meta file is checked for since it is the dependency
217 .for __obj in ${DEPENDOBJS:O:u}
218 # If the obj has any '/', then replace with '_'. For meta files, this is
219 # mimicing what bmake's meta_name() does and adding in the full path
220 # as well to ensure that the expected meta file is read.
222 _meta_obj= ${.OBJDIR:C,/,_,g}_${__obj:C,/,_,g}.meta
224 _meta_obj= ${__obj}.meta
226 _dep_obj= ${DEPENDFILE}.${__obj:${DEPEND_FILTER}}
227 .if (defined(_meta_filemon) && !exists(${.OBJDIR}/${_meta_obj})) || \
228 (!defined(_meta_filemon) && !exists(${.OBJDIR}/${_dep_obj}))
229 ${__obj}: ${OBJS_DEPEND_GUESS}
230 ${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
231 .elif defined(_meta_filemon)
232 # For meta mode we still need to know which file to depend on to avoid
233 # ambiguous suffix transformation rules from .PATH. Meta mode does not
234 # use .depend files. We really only need source files, not headers since
235 # they are typically in SRCS/beforebuild already. For target-specific
236 # guesses do include headers though since they may not be in SRCS.
237 ${__obj}: ${OBJS_DEPEND_GUESS:N*.h}
238 ${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
242 # Always run 'make depend' to generate dependencies early and to avoid the
243 # need for manually running it. The dirdeps build should only do this in
244 # sub-makes though since MAKELEVEL0 is for dirdeps calculations.
245 .if ${MK_DIRDEPS_BUILD} == "no" || ${.MAKE.LEVEL} > 0
251 depend: beforedepend ${DEPENDFILE} afterdepend
253 # Tell bmake not to look for generated files via .PATH
254 .NOPATH: ${DEPENDFILE} ${DEPENDFILES_OBJS}
257 # A .depend file will only be generated if there are commands in
258 # beforedepend/_EXTRADEPEND/afterdepend The _EXTRADEPEND target is
259 # ignored if using meta+filemon since it handles all dependencies. The other
260 # targets are kept as they be used for generating something. The target is
261 # kept to allow 'make depend' to generate files.
262 ${DEPENDFILE}: ${DPSRCS}
263 .if exists(${.OBJDIR}/${DEPENDFILE}) || \
264 ((commands(beforedepend) || \
265 (!defined(_meta_filemon) && commands(_EXTRADEPEND)) || \
266 commands(afterdepend)) && !empty(.MAKE.MODE:Mmeta))
269 .if !defined(_meta_filemon) && target(_EXTRADEPEND)
271 ${DEPENDFILE}: _EXTRADEPEND
274 .ORDER: ${DEPENDFILE} afterdepend
276 depend: beforedepend afterdepend
278 .if !target(beforedepend)
281 .ORDER: beforedepend ${DEPENDFILE}
282 .ORDER: beforedepend afterdepend
284 .if !target(afterdepend)
290 .if ${CTAGS:T} == "gtags"
291 CLEANDEPENDFILES+= GPATH GRTAGS GSYMS GTAGS
293 CLEANDEPENDDIRS+= HTML
296 CLEANDEPENDFILES+= tags
299 .if !target(cleandepend)
301 .if !empty(CLEANDEPENDFILES)
302 rm -f ${CLEANDEPENDFILES}
304 .if !empty(CLEANDEPENDDIRS)
305 rm -rf ${CLEANDEPENDDIRS}
308 .ORDER: cleandepend all
309 .ORDER: cleandepend depend
311 .if !target(checkdpadd) && (defined(DPADD) || defined(LDADD))
312 _LDADD_FROM_DPADD= ${DPADD:R:T:C;^lib(.*)$;-l\1;g}
313 # Ignore -Wl,--start-group/-Wl,--end-group as it might be required in the
314 # LDADD list due to unresolved symbols
315 _LDADD_CANONICALIZED= ${LDADD:N:R:T:C;^lib(.*)$;-l\1;g:N-Wl,--[es]*-group}
317 .if ${_LDADD_FROM_DPADD} != ${_LDADD_CANONICALIZED}
319 @echo "DPADD -> ${_LDADD_FROM_DPADD}"
320 @echo "LDADD -> ${_LDADD_CANONICALIZED}"