xref: /dragonfly/share/mk/bsd.dep.mk (revision 6a3cbbc2)
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
48CTAGS?=		gtags
49CTAGSFLAGS?=
50GTAGSFLAGS?=	-o
51HTAGSFLAGS?=
52
53MKDEPCMD?=	mkdep
54MKDEPCC?=	${CC}
55DEPENDFILE?=	.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)
66tags: ${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)
79CLEANFILES?=
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}
85SRCS:=	${SRCS:S/${_LSRC}/${_LC}/}
86CLEANFILES+= ${_LC}
87.endfor
88.endfor
89
90.for _YSRC in ${SRCS:M*.y:N*/*}
91.for _YC in ${_YSRC:R}.c
92SRCS:=	${SRCS:S/${_YSRC}/${_YC}/}
93CLEANFILES+= ${_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}
99CLEANFILES+= 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}
105SRCS+= ${_YH}
106CLEANFILES+= ${_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)
118depend: 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--sysroot=*} \
161	    ${CFLAGS:M-nostdinc*} ${CFLAGS:M-[BID]*} \
162	    ${CFLAGS:M-std=*} \
163	    ${.ALLSRC:M*.[csS]}
164.endif
165.if ${${_FG}_FLAGS_FILES:M*.cc} != "" || \
166    ${${_FG}_FLAGS_FILES:M*.C} != "" || \
167    ${${_FG}_FLAGS_FILES:M*.cpp} != "" || \
168    ${${_FG}_FLAGS_FILES:M*.cxx} != ""
169	${_MKDEPENV} CC=${CXX} ${MKDEPCMD} -f ${.TARGET} -a ${MKDEP} \
170	    ${${_FG}_FLAGS} \
171	    ${CXXFLAGS:M--sysroot=*} \
172	    ${CXXFLAGS:M-nostdinc*} ${CXXFLAGS:M-[BID]*} \
173	    ${CXXFLAGS:M-std=*} \
174	    ${.ALLSRC:M*.cc} ${.ALLSRC:M*.C} ${.ALLSRC:M*.cpp} ${.ALLSRC:M*.cxx}
175.endif
176.if ${${_FG}_FLAGS_FILES:M*.m} != ""
177	${MKDEPCMD} -f ${.TARGET} -a ${MKDEP} \
178	    ${${_FG}_FLAGS} \
179	    ${OBJCFLAGS:M-nostdinc*} ${OBJCFLAGS:M-[BID]*} \
180	    ${OBJCFLAGS:M-Wno-import*} \
181	    ${.ALLSRC:M*.m}
182.endif
183.if !empty(${_FG:M_}) && !empty(_DEPENDFILES)
184	cat ${_DEPENDFILES} >> ${.TARGET}
185.endif
186.if defined(MKDEPINTDEPS) && !empty(MKDEPINTDEPS)
187. for _ITD in ${MKDEPINTDEPS:O:u}
188	TMP=_depend$$$$; \
189	sed -e "s,${_ITD:C/^(.*:)(.*)/\1,\2 \1/},g" < ${.TARGET} > $$TMP; \
190	mv $$TMP ${.TARGET}
191. endfor
192.endif
193.endfor
194
195.if target(_EXTRADEPEND)
196_EXTRADEPEND: .USE
197${DEPENDFILE}: _EXTRADEPEND
198.endif
199
200.else
201depend: beforedepend _dependincs afterdepend
202.endif
203.if !target(beforedepend)
204beforedepend:
205.endif
206.if !target(afterdepend)
207afterdepend:
208.endif
209.endif
210
211.if !target(cleandepend)
212cleandepend:
213.if defined(SRCS)
214.if ${CTAGS:T} == "ctags"
215	rm -f ${DEPENDFILE} ${_DEPENDFILES} tags
216.elif ${CTAGS:T} == "gtags"
217	rm -f ${DEPENDFILE} ${_DEPENDFILES} GPATH GRTAGS GSYMS GTAGS
218.if defined(HTML)
219	rm -rf HTML
220.endif
221.endif
222.endif
223.endif
224
225.if !target(checkdpadd) && (defined(DPADD) || defined(LDADD))
226checkdpadd:
227.if ${OBJFORMAT} != aout
228	@ldadd=`echo \`for lib in ${DPADD} ; do \
229		echo $$lib | sed 's;^/usr/lib/lib\(.*\)\.a;-l\1;' ; \
230	done \`` ; \
231	ldadd1=`echo ${LDADD}` ; \
232	if [ "$$ldadd" != "$$ldadd1" ] ; then \
233		echo ${.CURDIR} ; \
234		echo "DPADD -> $$ldadd" ; \
235		echo "LDADD -> $$ldadd1" ; \
236	fi
237.else
238	@dpadd=`echo \`ld -Bstatic -f ${LDADD}\`` ; \
239	if [ "$$dpadd" != "${DPADD}" ] ; then \
240		echo ${.CURDIR} ; \
241		echo "LDADD -> $$dpadd" ; \
242		echo "DPADD =  ${DPADD}" ; \
243	fi
244.endif
245.endif
246
247.if defined(INCS) && make(depend)
248_dependincs: buildincludes .WAIT installincludes
249.else
250_dependincs:
251.endif
252
253.ORDER: beforedepend _dependincs ${DEPENDFILE} afterdepend
254