3 # $Id: autodep.mk,v 1.32 2010/04/19 17:37:56 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 compatability,
20 # dependencies are normally updated as part of compilation.
22 # set MKDEP=autodep and dep.mk will include us
23 .if !target(__${.PARSEFILE}__)
26 # different versions of bsd.dep.mk use these
31 .for d in ${DEPENDFILE:N.depend}
32 # bmake only groks .depend
36 # it does nothing if SRCS is not defined or is empty
37 .if defined(SRCS) && !empty(SRCS)
39 __depsrcs=${DEPSRCS:M*.c}
40 __depsrcs+=${DEPSRCS:M*.y}
41 __depsrcs+=${DEPSRCS:M*.l}
42 __depsrcs+=${DEPSRCS:M*.s}
43 __depsrcs+=${DEPSRCS:M*.S}
44 __depsrcs+=${DEPSRCS:M*.cc}
45 __depsrcs+=${DEPSRCS:M*.cpp}
46 __depsrcs+=${DEPSRCS:M*.C}
47 __depsrcs+=${DEPSRCS:M*.cxx}
48 __depsrcs+=${DEPSRCS:M*.pc}
50 .for s in ${__depsrcs}
54 __depsrcs:=${__depsrcs:T:R:S/$/.d/g}
55 # we also need to handle makefiles where the .d's from __depsrcs
56 # don't match those from OBJS
57 # we avoid using := here, since the modifier applied to OBJS
58 # can cause trouble if there are any undefined vars in OBJS.
59 __dependsrcsx?= ${__depsrcs} ${OBJS:S/.o/.d/}
60 __dependsrcs= ${__dependsrcsx:O:u}
62 # clean up any .c files we may have generated
63 #__gensrcs:= ${DEPSRCS:M*.y} ${DEPSRCS:M*.l}
64 #CLEANFILES+= ${__gensrcs:T:R:S/$/.c/g}
66 # set this to -MMD to ignore /usr/include
67 # actually it ignores <> so may not be a great idea
69 # -MF etc not available on all gcc versions.
70 # we "fix" the .o later
71 CFLAGS_MF?=-MF ${.TARGET:T:R}.d -MT ${.TARGET:T:R}.o
72 CFLAGS+= ${CFLAGS_MD} ${CFLAGS_MF}
75 # watch out for people who don't use CPPFLAGS
76 CPPFLAGS_MD=${CFLAGS:M-[IUD]*} ${CPPFLAGS}
77 CXXFLAGS_MD=${CXXFLAGS:M-[IUD]*} ${CPPFLAGS}
79 # just in case these need to be different
83 # so we can do an explicit make depend, but not otherwise
89 @echo updating dependencies for $<
91 @${SHELL} -ec "${CC_MD} -M ${CPPFLAGS_MD} y.tab.c | sed '/:/s/^/$@ /' > $@" || { ${RM} -f y.tab.c $@; false; }
95 @echo updating dependencies for $<
97 @${SHELL} -ec "${CC_MD} -M ${CPPFLAGS_MD} lex.yy.c | sed '/:/s/^/$@ /' > $@" || { ${RM} -f lex.yy.c $@; false; }
101 @echo updating dependencies for $<
102 @${SHELL} -ec "${CC_MD} -M ${CPPFLAGS_MD} $< | sed '/:/s/^/$@ /' > $@" || { ${RM} -f $@; false; }
105 @echo updating dependencies for $<
106 @${SHELL} -ec "${CC_MD} -M ${CPPFLAGS_MD} ${AINC} $< | sed '/:/s/^/$@ /' > $@" || { ${RM} -f $@; false; }
108 .cc.d .cpp.d .C.d .cxx.d:
109 @echo updating dependencies for $<
110 @${SHELL} -ec "${CXX_MD} -M ${CXXFLAGS_MD} $< | sed '/:/s/^/$@ /' > $@" || { ${RM} -f $@; false; }
114 ${CC_MD} ${CFLAGS_MD:S/D//} ${CPPFLAGS_MD} y.tab.c > $@ || { ${RM} -f y.tab.c $@; false; }
119 ${CC_MD} ${CFLAGS_MD:S/D//} ${CPPFLAGS_MD} lex.yy.c > $@ || { ${RM} -f lex.yy.c $@; false; }
123 ${CC_MD} ${CFLAGS_MD:S/D//} ${CPPFLAGS_MD} $< > $@ || { ${RM} -f $@; false; }
126 ${CC_MD} ${CFLAGS_MD:S/D//} ${CPPFLAGS_MD} ${AINC} $< > $@ || { ${RM} -f $@; false; }
128 .cc.d .cpp.d .C.d .cxx.d:
129 ${CXX_MD} ${CFLAGS_MD:S/D//} ${CXXFLAGS_MD} $< > $@ || { ${RM} -f $@; false; }
133 depend: beforedepend ${DEPENDFILE} afterdepend _SUBDIRUSE
135 ${DEPENDFILE}: ${DEPSRCS} ${__dependsrcs}
136 .NOPATH: ${__dependsrcs}
137 .OPTIONAL: ${__dependsrcs}
139 .endif # make(depend)
142 # make sure the .d's are generated/updated
143 ${PROG} ${_LIBS}: ${DEPENDFILE}
146 .ORDER: beforedepend ${DEPENDFILE} afterdepend
148 .if ${.OBJDIR} != ${.CURDIR}
151 __depfiles= ${__dependsrcs}
154 DEPCLEANFILES= ${DEPENDFILE} ${__depfiles} y.tab.d *.tmp.d
156 cleandir: cleanautodepend
158 ${RM} -f ${DEPCLEANFILES}
160 CLEANFILES+= ${DEPCLEANFILES}
162 .if defined(__dependsrcs) && !empty(__dependsrcs)
163 .if make(depend) || !(make(clean*) || make(destroy*) || make(obj) || make(*install) || make(install-*))
164 # this ensures we do the right thing if only building a shared or
166 OBJ_EXTENSIONS?=.o .po .so .So
167 MDLIB_SED= -e '/:/s,^\([^\.:]*\)\.[psS]*o,${OBJ_EXTENSIONS:S,^,\1,},'
170 MD_SED=sed ${MDLIB_SED}
175 # arrange to put some variable names into ${DEPENDFILE}
177 MD_SED=sed ${MDLIB_SED}
181 SUBST_DEPVARS+= SB TOP BACKING SRC SRCDIR BASE BASEDIR
182 .for v in ${SUBST_DEPVARS}
183 .if defined(${v}) && !empty(${v})
184 MD_SED+= -e 's,${$v},$${$v},'
188 .if (${MD_SED} == "sed")
192 # this will be done whenever make finishes successfully
193 .if ${MAKE_VERSION:U0:[1]:C/.*-//} < 20050530
197 # we do not want to trigger building .d's just use them if they exist
198 ${DEPENDFILE}: ${__dependsrcs:@d@${exists($d):?$d:}@}
200 -@${MD_SED} ${__depfiles} > ${DEPENDFILE}.new 2> /dev/null && \
201 test -s ${DEPENDFILE}.new && mv ${DEPENDFILE}.new ${DEPENDFILE}; \
202 ${RM} -f ${DEPENDFILE}.new
206 depend: beforedepend afterdepend _SUBDIRUSE
209 .if !target(beforedepend)
212 .if !target(afterdepend)