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 79# Skip reading .depend when not needed to speed up tree-walks and simple 80# lookups. For install, only do this if no other targets are specified. 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 !empty(.MAKEFLAGS:M-V${_V_READ_DEPEND}) || make(obj) || make(clean*) || \ 84 ${.TARGETS:M*install*} == ${.TARGETS} || \ 85 make(analyze) || defined(_meta_filemon) || make(print-dir) 86_SKIP_READ_DEPEND= 1 87.if ${MK_DIRDEPS_BUILD} == "no" || make(analyze) || make(print-dir) 88.MAKE.DEPENDFILE= /dev/null 89.endif 90.endif 91 92.if defined(SRCS) 93CLEANFILES?= 94 95.for _S in ${SRCS:N*.[dhly]} 96OBJS_DEPEND_GUESS.${_S:R}.o+= ${_S} 97.endfor 98 99# Lexical analyzers 100.for _LSRC in ${SRCS:M*.l:N*/*} 101.for _LC in ${_LSRC:R}.c 102${_LC}: ${_LSRC} 103 ${LEX} ${LFLAGS} -o${.TARGET} ${.ALLSRC} 104OBJS_DEPEND_GUESS.${_LC:R}.o+= ${_LC} 105SRCS:= ${SRCS:S/${_LSRC}/${_LC}/} 106CLEANFILES+= ${_LC} 107.endfor 108.endfor 109 110# Yacc grammars 111.for _YSRC in ${SRCS:M*.y:N*/*} 112.for _YC in ${_YSRC:R}.c 113SRCS:= ${SRCS:S/${_YSRC}/${_YC}/} 114CLEANFILES+= ${_YC} 115.if !empty(YFLAGS:M-d) && !empty(SRCS:My.tab.h) 116.ORDER: ${_YC} y.tab.h 117y.tab.h: .NOMETA 118${_YC} y.tab.h: ${_YSRC} 119 ${YACC} ${YFLAGS} ${.ALLSRC} 120 cp y.tab.c ${_YC} 121CLEANFILES+= y.tab.c y.tab.h 122.elif !empty(YFLAGS:M-d) 123.for _YH in ${_YC:R}.h 124.ORDER: ${_YC} ${_YH} 125${_YH}: .NOMETA 126${_YC} ${_YH}: ${_YSRC} 127 ${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC} 128SRCS+= ${_YH} 129CLEANFILES+= ${_YH} 130.endfor 131.else 132${_YC}: ${_YSRC} 133 ${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC} 134.endif 135OBJS_DEPEND_GUESS.${_YC:R}.o+= ${_YC} 136.endfor 137.endfor 138 139# DTrace probe definitions 140.if ${SRCS:M*.d} 141CFLAGS+= -I${.OBJDIR} 142.endif 143.for _DSRC in ${SRCS:M*.d:N*/*} 144.for _D in ${_DSRC:R} 145SRCS+= ${_D}.h 146${_D}.h: ${_DSRC} 147 ${DTRACE} ${DTRACEFLAGS} -h -s ${.ALLSRC} 148SRCS:= ${SRCS:S/^${_DSRC}$//} 149OBJS+= ${_D}.o 150CLEANFILES+= ${_D}.h ${_D}.o 151${_D}.o: ${_DSRC} ${OBJS:S/^${_D}.o$//} 152 @rm -f ${.TARGET} 153 ${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h} 154.if defined(LIB) 155CLEANFILES+= ${_D}.pico ${_D}.po 156${_D}.pico: ${_DSRC} ${SOBJS:S/^${_D}.pico$//} 157 @rm -f ${.TARGET} 158 ${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h} 159${_D}.po: ${_DSRC} ${POBJS:S/^${_D}.po$//} 160 @rm -f ${.TARGET} 161 ${DTRACE} ${DTRACEFLAGS} -G -o ${.TARGET} -s ${.ALLSRC:N*.h} 162.endif 163.endfor 164.endfor 165 166 167.if ${MAKE_VERSION} < 20160220 168DEPEND_MP?= -MP 169.endif 170# Handle OBJS=../somefile.o hacks. Just replace '/' rather than use :T to 171# avoid collisions. 172DEPEND_FILTER= C,/,_,g 173DEPENDSRCS= ${SRCS:M*.[cSC]} ${SRCS:M*.cxx} ${SRCS:M*.cpp} ${SRCS:M*.cc} 174.if !empty(DEPENDSRCS) 175DEPENDOBJS+= ${DEPENDSRCS:R:S,$,.o,} 176.endif 177DEPENDFILES_OBJS= ${DEPENDOBJS:O:u:${DEPEND_FILTER}:C/^/${DEPENDFILE}./} 178DEPEND_CFLAGS+= -MD ${DEPEND_MP} -MF${DEPENDFILE}.${.TARGET:${DEPEND_FILTER}} 179DEPEND_CFLAGS+= -MT${.TARGET} 180.if !defined(_meta_filemon) 181.if defined(.PARSEDIR) 182# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS 183# as those are the only ones we will include. 184DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:${DEPEND_FILTER}:M${.TARGET:${DEPEND_FILTER}}}" != "" 185CFLAGS+= ${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:} 186.else 187CFLAGS+= ${DEPEND_CFLAGS} 188.endif 189.if !defined(_SKIP_READ_DEPEND) 190.for __depend_obj in ${DEPENDFILES_OBJS} 191.if ${MAKE_VERSION} < 20160220 192.sinclude "${.OBJDIR}/${__depend_obj}" 193.else 194.dinclude "${.OBJDIR}/${__depend_obj}" 195.endif 196.endfor 197.endif # !defined(_SKIP_READ_DEPEND) 198.endif # !defined(_meta_filemon) 199.endif # defined(SRCS) 200 201.if ${MK_DIRDEPS_BUILD} == "yes" && !make(analyze) && !make(print-dir) 202# Prevent meta.autodep.mk from tracking "local dependencies". 203.depend: 204.include <meta.autodep.mk> 205# If using filemon then _EXTRADEPEND is skipped since it is not needed. 206.if defined(_meta_filemon) 207# this depend: bypasses that below 208# the dependency helps when bootstrapping 209depend: beforedepend ${DPSRCS} ${SRCS} afterdepend 210beforedepend: 211afterdepend: beforedepend 212.endif 213.endif 214 215# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet. 216# For meta+filemon the .meta file is checked for since it is the dependency 217# file used. 218.for __obj in ${DEPENDOBJS:O:u} 219# If the obj has any '/', then replace with '_'. For meta files, this is 220# mimicing what bmake's meta_name() does and adding in the full path 221# as well to ensure that the expected meta file is read. 222.if ${__obj:M*/*} 223_meta_obj= ${.OBJDIR:C,/,_,g}_${__obj:C,/,_,g}.meta 224.else 225_meta_obj= ${__obj}.meta 226.endif 227_dep_obj= ${DEPENDFILE}.${__obj:${DEPEND_FILTER}} 228.if (defined(_meta_filemon) && !exists(${.OBJDIR}/${_meta_obj})) || \ 229 (!defined(_meta_filemon) && !exists(${.OBJDIR}/${_dep_obj})) 230${__obj}: ${OBJS_DEPEND_GUESS} 231${__obj}: ${OBJS_DEPEND_GUESS.${__obj}} 232.elif defined(_meta_filemon) 233# For meta mode we still need to know which file to depend on to avoid 234# ambiguous suffix transformation rules from .PATH. Meta mode does not 235# use .depend files. We really only need source files, not headers since 236# they are typically in SRCS/beforebuild already. For target-specific 237# guesses do include headers though since they may not be in SRCS. 238${__obj}: ${OBJS_DEPEND_GUESS:N*.h} 239${__obj}: ${OBJS_DEPEND_GUESS.${__obj}} 240.endif 241.endfor 242 243# Always run 'make depend' to generate dependencies early and to avoid the 244# need for manually running it. The dirdeps build should only do this in 245# sub-makes though since MAKELEVEL0 is for dirdeps calculations. 246.if ${MK_DIRDEPS_BUILD} == "no" || ${.MAKE.LEVEL} > 0 247beforebuild: depend 248.endif 249 250.if !target(depend) 251.if defined(SRCS) 252depend: beforedepend ${DEPENDFILE} afterdepend 253 254# Tell bmake not to look for generated files via .PATH 255.NOPATH: ${DEPENDFILE} ${DEPENDFILES_OBJS} 256 257DPSRCS+= ${SRCS} 258# A .depend file will only be generated if there are commands in 259# beforedepend/_EXTRADEPEND/afterdepend The _EXTRADEPEND target is 260# ignored if using meta+filemon since it handles all dependencies. The other 261# targets are kept as they be used for generating something. The target is 262# kept to allow 'make depend' to generate files. 263${DEPENDFILE}: ${DPSRCS} 264.if exists(${.OBJDIR}/${DEPENDFILE}) || \ 265 ((commands(beforedepend) || \ 266 (!defined(_meta_filemon) && commands(_EXTRADEPEND)) || \ 267 commands(afterdepend)) && !empty(.MAKE.MODE:Mmeta)) 268 rm -f ${DEPENDFILE} 269.endif 270.if !defined(_meta_filemon) && target(_EXTRADEPEND) 271_EXTRADEPEND: .USE 272${DEPENDFILE}: _EXTRADEPEND 273.endif 274 275.ORDER: ${DEPENDFILE} afterdepend 276.else 277depend: beforedepend afterdepend 278.endif 279.if !target(beforedepend) 280beforedepend: 281.else 282.ORDER: beforedepend ${DEPENDFILE} 283.ORDER: beforedepend afterdepend 284.endif 285.if !target(afterdepend) 286afterdepend: 287.endif 288.endif 289 290.if defined(SRCS) 291.if ${CTAGS:T} == "gtags" 292CLEANDEPENDFILES+= GPATH GRTAGS GSYMS GTAGS 293.if defined(HTML) 294CLEANDEPENDDIRS+= HTML 295.endif 296.else 297CLEANDEPENDFILES+= tags 298.endif 299.endif 300.if !target(cleandepend) 301cleandepend: 302.if !empty(CLEANDEPENDFILES) 303 rm -f ${CLEANDEPENDFILES} 304.endif 305.if !empty(CLEANDEPENDDIRS) 306 rm -rf ${CLEANDEPENDDIRS} 307.endif 308.endif 309.ORDER: cleandepend all 310.ORDER: cleandepend depend 311 312.if !target(checkdpadd) && (defined(DPADD) || defined(LDADD)) 313_LDADD_FROM_DPADD= ${DPADD:R:T:C;^lib(.*)$;-l\1;g} 314# Ignore -Wl,--start-group/-Wl,--end-group as it might be required in the 315# LDADD list due to unresolved symbols 316_LDADD_CANONICALIZED= ${LDADD:N:R:T:C;^lib(.*)$;-l\1;g:N-Wl,--[es]*-group} 317checkdpadd: 318.if ${_LDADD_FROM_DPADD} != ${_LDADD_CANONICALIZED} 319 @echo ${.CURDIR} 320 @echo "DPADD -> ${_LDADD_FROM_DPADD}" 321 @echo "LDADD -> ${_LDADD_CANONICALIZED}" 322.endif 323.endif 324