]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - share/mk/bsd.dep.mk
MFC r334394:
[FreeBSD/stable/10.git] / share / mk / bsd.dep.mk
1 # $FreeBSD$
2 #
3 # The include file <bsd.dep.mk> handles Makefile dependencies.
4 #
5 #
6 # +++ variables +++
7 #
8 # CTAGS         A tags file generation program [gtags]
9 #
10 # CTAGSFLAGS    Options for ctags(1) [not set]
11 #
12 # DEPENDFILE    dependencies file [.depend]
13 #
14 # GTAGSFLAGS    Options for gtags(1) [-o]
15 #
16 # HTAGSFLAGS    Options for htags(1) [not set]
17 #
18 # MKDEP         Options for ${MKDEPCMD} [not set]
19 #
20 # MKDEPCMD      Makefile dependency list program [mkdep]
21 #
22 # SRCS          List of source files (c, c++, assembler)
23 #
24 # DPSRCS        List of source files which are needed for generating
25 #               dependencies, ${SRCS} are always part of it.
26 #
27 # +++ targets +++
28 #
29 #       cleandepend:
30 #               Remove depend and tags file
31 #
32 #       depend:
33 #               Make the dependencies for the source files, and store
34 #               them in the file ${DEPENDFILE}.
35 #
36 #       tags:
37 #               In "ctags" mode, create a tags file for the source files.
38 #               In "gtags" mode, create a (GLOBAL) gtags file for the
39 #               source files.  If HTML is defined, htags(1) is also run
40 #               after gtags(1).
41
42 .if !target(__<bsd.init.mk>__)
43 .error bsd.dep.mk cannot be included directly.
44 .endif
45
46 CTAGS?=         gtags
47 CTAGSFLAGS?=
48 GTAGSFLAGS?=    -o
49 HTAGSFLAGS?=
50
51 .if ${CC} != "cc"
52 MKDEPCMD?=      CC='${CC}' mkdep
53 .else
54 MKDEPCMD?=      mkdep
55 .endif
56 DEPENDFILE?=    .depend
57
58 # Keep `tags' here, before SRCS are mangled below for `depend'.
59 .if !target(tags) && defined(SRCS) && !defined(NO_TAGS)
60 tags: ${SRCS}
61 .if ${CTAGS:T} == "gtags"
62         @cd ${.CURDIR} && ${CTAGS} ${GTAGSFLAGS} ${.OBJDIR}
63 .if defined(HTML)
64         @cd ${.CURDIR} && htags ${HTAGSFLAGS} -d ${.OBJDIR} ${.OBJDIR}
65 .endif
66 .else
67         @${CTAGS} ${CTAGSFLAGS} -f /dev/stdout \
68             ${.ALLSRC:N*.h} | sed "s;${.CURDIR}/;;" > ${.TARGET}
69 .endif
70 .endif
71
72 .if defined(SRCS)
73 CLEANFILES?=
74
75 .if !exists(${.OBJDIR}/${DEPENDFILE})
76 .for _S in ${SRCS:N*.[dhly]}
77 ${_S:R}.o: ${_S}
78 .endfor
79 .endif
80
81 # Lexical analyzers
82 .for _LSRC in ${SRCS:M*.l:N*/*}
83 .for _LC in ${_LSRC:R}.c
84 ${_LC}: ${_LSRC}
85         ${LEX} ${LFLAGS} -o${.TARGET} ${.ALLSRC}
86 .if !exists(${.OBJDIR}/${DEPENDFILE})
87 ${_LC:R}.o: ${_LC}
88 .endif
89 SRCS:=  ${SRCS:S/${_LSRC}/${_LC}/}
90 CLEANFILES+= ${_LC}
91 .endfor
92 .endfor
93
94 # Yacc grammars
95 .for _YSRC in ${SRCS:M*.y:N*/*}
96 .for _YC in ${_YSRC:R}.c
97 SRCS:=  ${SRCS:S/${_YSRC}/${_YC}/}
98 CLEANFILES+= ${_YC}
99 .if !empty(YFLAGS:M-d) && !empty(SRCS:My.tab.h)
100 .ORDER: ${_YC} y.tab.h
101 ${_YC} y.tab.h: ${_YSRC}
102         ${YACC} ${YFLAGS} ${.ALLSRC}
103         cp y.tab.c ${_YC}
104 CLEANFILES+= y.tab.c y.tab.h
105 .elif !empty(YFLAGS:M-d)
106 .for _YH in ${_YC:R}.h
107 .ORDER: ${_YC} ${_YH}
108 ${_YC} ${_YH}: ${_YSRC}
109         ${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
110 SRCS+=  ${_YH}
111 CLEANFILES+= ${_YH}
112 .endfor
113 .else
114 ${_YC}: ${_YSRC}
115         ${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
116 .endif
117 .if !exists(${.OBJDIR}/${DEPENDFILE})
118 ${_YC:R}.o: ${_YC}
119 .endif
120 .endfor
121 .endfor
122
123 # DTrace probe definitions
124 # libelf is currently needed for drti.o
125 .if ${SRCS:M*.d}
126 LDFLAGS+=       -lelf
127 LDADD+=         ${LIBELF}
128 CFLAGS+=        -D_DTRACE_VERSION=1 -I${.OBJDIR}
129 .endif
130 .for _DSRC in ${SRCS:M*.d:N*/*}
131 .for _D in ${_DSRC:R}
132 DHDRS+= ${_D}.h
133 ${_D}.h: ${_DSRC}
134         ${DTRACE} -xnolibs -h -s ${.ALLSRC}
135 SRCS:=  ${SRCS:S/${_DSRC}/${_D}.h/}
136 OBJS+=  ${_D}.o
137 CLEANFILES+= ${_D}.h ${_D}.o
138 ${_D}.o: ${_D}.h ${OBJS:S/${_D}.o//}
139         ${DTRACE} -xnolibs -G -o ${.TARGET} -s ${.CURDIR}/${_DSRC} \
140                 ${OBJS:S/${_D}.o//}
141 .if defined(LIB)
142 CLEANFILES+= ${_D}.So ${_D}.po
143 ${_D}.So: ${_D}.h ${SOBJS:S/${_D}.So//}
144         ${DTRACE} -xnolibs -G -o ${.TARGET} -s ${.CURDIR}/${_DSRC} \
145                 ${SOBJS:S/${_D}.So//}
146 ${_D}.po: ${_D}.h ${POBJS:S/${_D}.po//}
147         ${DTRACE} -xnolibs -G -o ${.TARGET} -s ${.CURDIR}/${_DSRC} \
148                 ${POBJS:S/${_D}.po//}
149 .endif
150 .endfor
151 .endfor
152 beforedepend: ${DHDRS}
153 beforebuild: ${DHDRS}
154 .endif
155
156 .if !target(depend)
157 .if defined(SRCS)
158 depend: beforedepend ${DEPENDFILE} afterdepend
159
160 # Tell bmake not to look for generated files via .PATH
161 .NOPATH: ${DEPENDFILE}
162
163 # Capture -include from CFLAGS.
164 # This could be simpler with bmake :tW but needs to support fmake for MFC.
165 _CFLAGS_INCLUDES= ${CFLAGS:Q:S/\\ /,/g:C/-include,/-include%/g:C/,/ /g:M-include*:C/%/ /g}
166 _CXXFLAGS_INCLUDES= ${CXXFLAGS:Q:S/\\ /,/g:C/-include,/-include%/g:C/,/ /g:M-include*:C/%/ /g}
167
168 # Different types of sources are compiled with slightly different flags.
169 # Split up the sources, and filter out headers and non-applicable flags.
170 MKDEP_CFLAGS=   ${CFLAGS:M-nostdinc*} ${CFLAGS:M-[BIDU]*} ${CFLAGS:M-std=*} \
171                 ${CFLAGS:M-ansi} ${_CFLAGS_INCLUDES}
172 MKDEP_CXXFLAGS= ${CXXFLAGS:M-nostdinc*} ${CXXFLAGS:M-[BIDU]*} \
173                 ${CXXFLAGS:M-std=*} ${CXXFLAGS:M-ansi} ${CXXFLAGS:M-stdlib=*} \
174                 ${_CXXFLAGS_INCLUDES}
175
176 DPSRCS+= ${SRCS}
177 ${DEPENDFILE}: ${DPSRCS}
178         rm -f ${DEPENDFILE}
179 .if !empty(DPSRCS:M*.[cS])
180         ${MKDEPCMD} -f ${DEPENDFILE} -a ${MKDEP} \
181             ${MKDEP_CFLAGS} ${.ALLSRC:M*.[cS]}
182 .endif
183 .if !empty(DPSRCS:M*.cc) || !empty(DPSRCS:M*.C) || !empty(DPSRCS:M*.cpp) || \
184     !empty(DPSRCS:M*.cxx)
185         ${MKDEPCMD} -f ${DEPENDFILE} -a ${MKDEP} \
186             ${MKDEP_CXXFLAGS} \
187             ${.ALLSRC:M*.cc} ${.ALLSRC:M*.C} ${.ALLSRC:M*.cpp} ${.ALLSRC:M*.cxx}
188 .endif
189 .if target(_EXTRADEPEND)
190 _EXTRADEPEND: .USE
191 ${DEPENDFILE}: _EXTRADEPEND
192 .endif
193
194 .ORDER: ${DEPENDFILE} afterdepend
195 .else
196 depend: beforedepend afterdepend
197 .endif
198 .if !target(beforedepend)
199 beforedepend:
200 .else
201 .ORDER: beforedepend ${DEPENDFILE}
202 .ORDER: beforedepend afterdepend
203 .endif
204 .if !target(afterdepend)
205 afterdepend:
206 .endif
207 .endif
208
209 .if !target(cleandepend)
210 cleandepend:
211 .if defined(SRCS)
212 .if ${CTAGS:T} == "gtags"
213         rm -f ${DEPENDFILE} GPATH GRTAGS GSYMS GTAGS
214 .if defined(HTML)
215         rm -rf HTML
216 .endif
217 .else
218         rm -f ${DEPENDFILE} tags
219 .endif
220 .endif
221 .endif
222
223 .if !target(checkdpadd) && (defined(DPADD) || defined(LDADD))
224 _LDADD_FROM_DPADD=      ${DPADD:R:T:C;^lib(.*)$;-l\1;g}
225 # Ignore -Wl,--start-group/-Wl,--end-group as it might be required in the
226 # LDADD list due to unresolved symbols
227 _LDADD_CANONICALIZED=   ${LDADD:N:R:T:C;^lib(.*)$;-l\1;g:N-Wl,--[es]*-group}
228 checkdpadd:
229 .if ${_LDADD_FROM_DPADD} != ${_LDADD_CANONICALIZED}
230         @echo ${.CURDIR}
231         @echo "DPADD -> ${_LDADD_FROM_DPADD}"
232         @echo "LDADD -> ${_LDADD_CANONICALIZED}"
233 .endif
234 .endif