crunchgen(1): Add "linkopts" to specify linker options
[dragonfly.git] / share / mk / bsd.dep.mk
1 # $FreeBSD: src/share/mk/bsd.dep.mk,v 1.27.2.3 2002/12/23 16:33:37 ru Exp $
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 # MKDEPCC       CC to be used in ${MKDEPCMD} script [cc]
21 #
22 # MKDEPCMD      Makefile dependency list program [mkdep]
23 #
24 # MKDEPINTDEPS  Extra internal dependencies for intermediates [not set]
25 #
26 # SRCS          List of source files (c, c++, assembler)
27 #
28 #
29 # +++ targets +++
30 #
31 #       cleandepend:
32 #               Remove depend and tags file
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
48 CTAGS?=         gtags
49 CTAGSFLAGS?=
50 GTAGSFLAGS?=    -o
51 HTAGSFLAGS?=
52
53 MKDEPCMD?=      mkdep
54 MKDEPCC?=       ${CC}
55 DEPENDFILE?=    .depend
56
57 # Support better dependencies tracking for both host cc and target cc.
58 .if defined(__USE_HOST_CCVER)
59 _MKDEPENV=      ${NXENV:NPATH=*}
60 .else
61 _MKDEPENV=      CCVER=${CCVER}
62 .endif
63
64 # Keep `tags' here, before SRCS are mangled below for `depend'.
65 .if !target(tags) && defined(SRCS) && !defined(NOTAGS)
66 tags: ${SRCS}
67 .if ${CTAGS:T} == "ctags"
68         @${CTAGS} ${CTAGSFLAGS} -f /dev/stdout \
69             ${.ALLSRC:N*.h} | sed "s;${.CURDIR}/;;" > ${.TARGET}
70 .elif ${CTAGS:T} == "gtags"
71         @cd ${.CURDIR} && ${CTAGS} ${GTAGSFLAGS} ${.OBJDIR}
72 .if defined(HTML)
73         @cd ${.CURDIR} && htags ${HTAGSFLAGS} -d ${.OBJDIR} ${.OBJDIR}
74 .endif
75 .endif
76 .endif
77
78 .if defined(SRCS)
79 CLEANFILES?=
80
81 .for _LSRC in ${SRCS:M*.l:N*/*}
82 .for _LC in ${_LSRC:R}.c
83 ${_LC}: ${_LSRC}
84         ${LEX} -t ${LFLAGS} ${.ALLSRC} > ${.TARGET}
85 SRCS:=  ${SRCS:S/${_LSRC}/${_LC}/}
86 CLEANFILES+= ${_LC}
87 .endfor
88 .endfor
89
90 .for _YSRC in ${SRCS:M*.y:N*/*}
91 .for _YC in ${_YSRC:R}.c
92 SRCS:=  ${SRCS:S/${_YSRC}/${_YC}/}
93 CLEANFILES+= ${_YC}
94 .if !empty(YFLAGS:M-d) && !empty(SRCS:My.tab.h)
95 .ORDER: ${_YC} y.tab.h
96 ${_YC} y.tab.h: ${_YSRC}
97         ${YACC} ${YFLAGS} ${.ALLSRC}
98         cp y.tab.c ${_YC}
99 CLEANFILES+= y.tab.c y.tab.h
100 .elif !empty(YFLAGS:M-d)
101 .for _YH in ${_YC:S/.c/.h/}
102 ${_YH}: ${_YC}
103 ${_YC}: ${_YSRC}
104         ${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
105 SRCS+= ${_YH}
106 CLEANFILES+= ${_YH}
107 .endfor
108 .else
109 ${_YC}: ${_YSRC}
110         ${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
111 .endif
112 .endfor
113 .endfor
114 .endif
115
116 .if !target(depend)
117 .if defined(SRCS)
118 depend: beforedepend _dependincs ${DEPENDFILE} afterdepend
119
120 # Tell bmake not to look for generated files via .PATH
121 .NOPATH: ${DEPENDFILE}
122
123 # Different types of sources are compiled with slightly different flags.
124 # Split up the sources, and filter out headers and non-applicable flags.
125 # Separate flag groups out of the sources and treat them differently.
126 # The special "_" group is for all files not in any group.
127 __FLAGS=
128 __FLAGS_FILES=  ${SRCS}
129 .for _FG in ${FLAGS_GROUPS}
130 .for _FFILE in ${${_FG}_FLAGS_FILES}
131 __FLAGS_FILES:= ${__FLAGS_FILES:N${_FFILE}}
132 .endfor
133 .endfor
134
135 _DEPENDFILES=   ${FLAGS_GROUPS:S/^/.depend_/g}
136
137 ${DEPENDFILE}: ${_DEPENDFILES}
138
139 #
140 # __FLAG_FILES is built from SRCS.  That means it will contain
141 # also .h files and other files that are not direct sources, but which
142 # might be required to even run mkdep.  This is important if those are
143 # generated as well, like some forwarding headers.
144 #
145 # We'll have to pass these "sources" on to the other .depend_ file targets,
146 # since otherwise they might be run before the generated sources are
147 # generated.  _ALL_DEPENDS captures all files in SRCS that are not handled
148 # by the mkdep calls, i.e. all sources that are not being used directly
149 # for the .depend* file.
150 #
151 _ALL_DEPENDS=${__FLAGS_FILES:N*.[sS]:N*.c:N*.cc:N*.C:N*.cpp:N*.cpp:N*.cxx:N*.m}
152
153 .for _FG in _ ${FLAGS_GROUPS}
154 .depend${_FG:S/^/_/:N__}: ${${_FG}_FLAGS_FILES} ${_ALL_DEPENDS}
155         -rm -f ${.TARGET}
156         -> ${.TARGET}
157 .if ${${_FG}_FLAGS_FILES:M*.[csS]} != ""
158         ${_MKDEPENV} CC=${MKDEPCC} ${MKDEPCMD} -f ${.TARGET} -a ${MKDEP} \
159             ${${_FG}_FLAGS} \
160             ${CFLAGS:M-nostdinc*} ${CFLAGS:M-[BID]*} \
161             ${CFLAGS:M-std=*} \
162             ${.ALLSRC:M*.[csS]}
163 .endif
164 .if ${${_FG}_FLAGS_FILES:M*.cc} != "" || \
165     ${${_FG}_FLAGS_FILES:M*.C} != "" || \
166     ${${_FG}_FLAGS_FILES:M*.cpp} != "" || \
167     ${${_FG}_FLAGS_FILES:M*.cxx} != ""
168         ${_MKDEPENV} CC=${CXX} ${MKDEPCMD} -f ${.TARGET} -a ${MKDEP} \
169             ${${_FG}_FLAGS} \
170             ${CXXFLAGS:M-nostdinc*} ${CXXFLAGS:M-[BID]*} \
171             ${CXXFLAGS:M-std=*} \
172             ${.ALLSRC:M*.cc} ${.ALLSRC:M*.C} ${.ALLSRC:M*.cpp} ${.ALLSRC:M*.cxx}
173 .endif
174 .if ${${_FG}_FLAGS_FILES:M*.m} != ""
175         ${MKDEPCMD} -f ${.TARGET} -a ${MKDEP} \
176             ${${_FG}_FLAGS} \
177             ${OBJCFLAGS:M-nostdinc*} ${OBJCFLAGS:M-[BID]*} \
178             ${OBJCFLAGS:M-Wno-import*} \
179             ${.ALLSRC:M*.m}
180 .endif
181 .if !empty(${_FG:M_}) && !empty(_DEPENDFILES)
182         cat ${_DEPENDFILES} >> ${.TARGET}
183 .endif
184 .if defined(MKDEPINTDEPS) && !empty(MKDEPINTDEPS)
185 . for _ITD in ${MKDEPINTDEPS:O:u}
186         TMP=_depend$$$$; \
187         sed -e "s,${_ITD:C/^(.*:)(.*)/\1,\2 \1/},g" < ${.TARGET} > $$TMP; \
188         mv $$TMP ${.TARGET}
189 . endfor
190 .endif
191 .endfor
192
193 .if target(_EXTRADEPEND)
194 _EXTRADEPEND: .USE
195 ${DEPENDFILE}: _EXTRADEPEND
196 .endif
197
198 .else
199 depend: beforedepend _dependincs afterdepend
200 .endif
201 .if !target(beforedepend)
202 beforedepend:
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} == "ctags"
213         rm -f ${DEPENDFILE} ${_DEPENDFILES} tags
214 .elif ${CTAGS:T} == "gtags"
215         rm -f ${DEPENDFILE} ${_DEPENDFILES} GPATH GRTAGS GSYMS GTAGS
216 .if defined(HTML)
217         rm -rf HTML
218 .endif
219 .endif
220 .endif
221 .endif
222
223 .if !target(checkdpadd) && (defined(DPADD) || defined(LDADD))
224 checkdpadd:
225 .if ${OBJFORMAT} != aout
226         @ldadd=`echo \`for lib in ${DPADD} ; do \
227                 echo $$lib | sed 's;^/usr/lib/lib\(.*\)\.a;-l\1;' ; \
228         done \`` ; \
229         ldadd1=`echo ${LDADD}` ; \
230         if [ "$$ldadd" != "$$ldadd1" ] ; then \
231                 echo ${.CURDIR} ; \
232                 echo "DPADD -> $$ldadd" ; \
233                 echo "LDADD -> $$ldadd1" ; \
234         fi
235 .else
236         @dpadd=`echo \`ld -Bstatic -f ${LDADD}\`` ; \
237         if [ "$$dpadd" != "${DPADD}" ] ; then \
238                 echo ${.CURDIR} ; \
239                 echo "LDADD -> $$dpadd" ; \
240                 echo "DPADD =  ${DPADD}" ; \
241         fi
242 .endif
243 .endif
244
245 .if defined(INCS) && make(depend)
246 _dependincs: buildincludes .WAIT installincludes
247 .else
248 _dependincs:
249 .endif
250
251 .ORDER: beforedepend _dependincs ${DEPENDFILE} afterdepend