3 # $Id: autodep.mk,v 1.34 2014/08/04 05:12:27 sjg Exp $
5 # @(#) Copyright (c) 1999-2010, Simon J. Gerraty
7 # This file is provided in the hope that it will
8 # be of use. There is absolutely NO WARRANTY.
9 # Permission to copy, redistribute or otherwise
10 # use this file is hereby granted provided that
11 # the above copyright notice and this notice are
14 # Please send copies of changes and bug-fixes to:
17 # This module provides automagic dependency generation along the
18 # lines suggested in the GNU make.info
19 # The depend target is mainly for backwards compatibility,
20 # dependencies are normally updated as part of compilation.
22 .if !target(__${.PARSEFILE}__)
26 .for d in ${DEPENDFILE:N.depend}
27 # bmake only groks .depend
31 # it does nothing if SRCS is not defined or is empty
32 .if defined(SRCS) && !empty(SRCS)
34 __depsrcs=${DEPSRCS:M*.c}
35 __depsrcs+=${DEPSRCS:M*.y}
36 __depsrcs+=${DEPSRCS:M*.l}
37 __depsrcs+=${DEPSRCS:M*.s}
38 __depsrcs+=${DEPSRCS:M*.S}
39 __depsrcs+=${DEPSRCS:M*.cc}
40 __depsrcs+=${DEPSRCS:M*.cpp}
41 __depsrcs+=${DEPSRCS:M*.C}
42 __depsrcs+=${DEPSRCS:M*.cxx}
43 __depsrcs+=${DEPSRCS:M*.pc}
45 .for s in ${__depsrcs}
49 __depsrcs:=${__depsrcs:T:R:S/$/.d/g}
50 # we also need to handle makefiles where the .d's from __depsrcs
51 # don't match those from OBJS
52 # we avoid using := here, since the modifier applied to OBJS
53 # can cause trouble if there are any undefined vars in OBJS.
54 __dependsrcsx?= ${__depsrcs} ${OBJS:S/.o/.d/}
55 __dependsrcs= ${__dependsrcsx:O:u}
57 # clean up any .c files we may have generated
58 #__gensrcs:= ${DEPSRCS:M*.y} ${DEPSRCS:M*.l}
59 #CLEANFILES+= ${__gensrcs:T:R:S/$/.c/g}
61 # set this to -MMD to ignore /usr/include
62 # actually it ignores <> so may not be a great idea
64 # -MF etc not available on all gcc versions.
65 # we "fix" the .o later
66 CFLAGS_MF?=-MF ${.TARGET:T:R}.d -MT ${.TARGET:T:R}.o
67 CFLAGS+= ${CFLAGS_MD} ${CFLAGS_MF}
70 # watch out for people who don't use CPPFLAGS
71 CPPFLAGS_MD=${CFLAGS:M-[IUD]*} ${CPPFLAGS}
72 CXXFLAGS_MD=${CXXFLAGS:M-[IUD]*} ${CPPFLAGS}
74 # just in case these need to be different
78 # so we can do an explicit make depend, but not otherwise
84 @echo updating dependencies for $<
86 @${SHELL} -ec "${CC_MD} -M ${CPPFLAGS_MD} y.tab.c | sed '/:/s/^/$@ /' > $@" || { ${RM} -f y.tab.c $@; false; }
90 @echo updating dependencies for $<
92 @${SHELL} -ec "${CC_MD} -M ${CPPFLAGS_MD} lex.yy.c | sed '/:/s/^/$@ /' > $@" || { ${RM} -f lex.yy.c $@; false; }
96 @echo updating dependencies for $<
97 @${SHELL} -ec "${CC_MD} -M ${CPPFLAGS_MD} $< | sed '/:/s/^/$@ /' > $@" || { ${RM} -f $@; false; }
100 @echo updating dependencies for $<
101 @${SHELL} -ec "${CC_MD} -M ${CPPFLAGS_MD} ${AINC} $< | sed '/:/s/^/$@ /' > $@" || { ${RM} -f $@; false; }
103 .cc.d .cpp.d .C.d .cxx.d:
104 @echo updating dependencies for $<
105 @${SHELL} -ec "${CXX_MD} -M ${CXXFLAGS_MD} $< | sed '/:/s/^/$@ /' > $@" || { ${RM} -f $@; false; }
109 ${CC_MD} ${CFLAGS_MD:S/D//} ${CPPFLAGS_MD} y.tab.c > $@ || { ${RM} -f y.tab.c $@; false; }
114 ${CC_MD} ${CFLAGS_MD:S/D//} ${CPPFLAGS_MD} lex.yy.c > $@ || { ${RM} -f lex.yy.c $@; false; }
118 ${CC_MD} ${CFLAGS_MD:S/D//} ${CPPFLAGS_MD} $< > $@ || { ${RM} -f $@; false; }
121 ${CC_MD} ${CFLAGS_MD:S/D//} ${CPPFLAGS_MD} ${AINC} $< > $@ || { ${RM} -f $@; false; }
123 .cc.d .cpp.d .C.d .cxx.d:
124 ${CXX_MD} ${CFLAGS_MD:S/D//} ${CXXFLAGS_MD} $< > $@ || { ${RM} -f $@; false; }
128 depend: beforedepend ${DEPENDFILE} afterdepend _SUBDIRUSE
130 ${DEPENDFILE}: ${DEPSRCS} ${__dependsrcs}
131 .NOPATH: ${__dependsrcs}
132 .OPTIONAL: ${__dependsrcs}
134 .endif # make(depend)
137 # make sure the .d's are generated/updated
138 ${PROG} ${_LIBS}: ${DEPENDFILE}
141 .ORDER: beforedepend ${DEPENDFILE} afterdepend
143 .if ${.OBJDIR} != ${.CURDIR}
146 __depfiles= ${__dependsrcs}
149 DEPCLEANFILES= ${DEPENDFILE} ${__depfiles} y.tab.d *.tmp.d
151 cleandir: cleanautodepend
153 ${RM} -f ${DEPCLEANFILES}
155 CLEANFILES+= ${DEPCLEANFILES}
157 .if defined(__dependsrcs) && !empty(__dependsrcs)
158 .if make(depend) || !(make(clean*) || make(destroy*) || make(obj) || make(*install) || make(install-*))
159 # this ensures we do the right thing if only building a shared or
161 OBJ_EXTENSIONS?=.o .po .so .So
162 MDLIB_SED= -e '/:/s,^\([^\.:]*\)\.[psS]*o,${OBJ_EXTENSIONS:S,^,\1,},'
165 MD_SED=sed ${MDLIB_SED}
170 # arrange to put some variable names into ${DEPENDFILE}
172 MD_SED=sed ${MDLIB_SED}
176 SUBST_DEPVARS+= SB TOP BACKING SRC SRCDIR BASE BASEDIR
177 .for v in ${SUBST_DEPVARS}
178 .if defined(${v}) && !empty(${v})
179 MD_SED+= -e 's,${$v},$${$v},'
183 .if (${MD_SED} == "sed")
187 # this will be done whenever make finishes successfully
188 .if ${MAKE_VERSION:U0:[1]:C/.*-//} < 20050530
192 # we do not want to trigger building .d's just use them if they exist
193 ${DEPENDFILE}: ${__dependsrcs:@d@${exists($d):?$d:}@}
195 -@${MD_SED} ${__depfiles} > ${DEPENDFILE}.new 2> /dev/null && \
196 test -s ${DEPENDFILE}.new && mv ${DEPENDFILE}.new ${DEPENDFILE}; \
197 ${RM} -f ${DEPENDFILE}.new
201 depend: beforedepend afterdepend _SUBDIRUSE
204 .if !target(beforedepend)
207 .if !target(afterdepend)