bsd.dep.mk revision 175937
1# $FreeBSD: head/share/mk/bsd.dep.mk 175937 2008-02-03 11:34:56Z des $
2#
3# The include file <bsd.dep.mk> handles Makefile dependencies.
4#
5#
6# +++ variables +++
7#
8# CLEANDEPFILES Additional files to clean when doing cleandepend
9#
10# CTAGS		A tags file generation program [gtags]
11#
12# CTAGSFLAGS	Options for ctags(1) [not set]
13#
14# DEPENDFILE	dependencies file [.depend]
15#
16# GTAGSFLAGS	Options for gtags(1) [-o]
17#
18# HTAGSFLAGS	Options for htags(1) [not set]
19#
20# MKDEP		Options for ${MKDEPCMD} [not set]
21#
22# MKDEPCMD	Makefile dependency list program [mkdep]
23#
24# SRCS          List of source files (c, c++, assembler)
25#
26# DPSRCS	List of source files which are needed for generating
27#		dependencies, ${SRCS} are always part of it.
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
53.if ${CC} != "cc"
54MKDEPCMD?=	CC='${CC}' mkdep
55.else
56MKDEPCMD?=	mkdep
57.endif
58DEPENDFILE?=	.depend
59
60# Keep `tags' here, before SRCS are mangled below for `depend'.
61.if !target(tags) && defined(SRCS) && !defined(NO_TAGS)
62tags: ${SRCS}
63.if ${CTAGS:T} == "ctags"
64	@${CTAGS} ${CTAGSFLAGS} -f /dev/stdout \
65	    ${.ALLSRC:N*.h} | sed "s;${.CURDIR}/;;" > ${.TARGET}
66.elif ${CTAGS:T} == "gtags"
67	@cd ${.CURDIR} && ${CTAGS} ${GTAGSFLAGS} ${.OBJDIR}
68.if defined(HTML)
69	@cd ${.CURDIR} && htags ${HTAGSFLAGS} -d ${.OBJDIR} ${.OBJDIR}
70.endif
71.endif
72.endif
73
74.if defined(SRCS)
75CLEANFILES?=
76
77.if !exists(${.OBJDIR}/${DEPENDFILE})
78.for _S in ${SRCS:N*.[hly]}
79${_S:R}.o: ${_S}
80.endfor
81.endif
82
83.for _LSRC in ${SRCS:M*.l:N*/*}
84.for _LC in ${_LSRC:R}.c
85${_LC}: ${_LSRC}
86	${LEX} -t ${LFLAGS} ${.ALLSRC} > ${.TARGET}
87.if !exists(${.OBJDIR}/${DEPENDFILE})
88${_LC:R}.o: ${_LC}
89.endif
90SRCS:=	${SRCS:S/${_LSRC}/${_LC}/}
91CLEANFILES+= ${_LC}
92.endfor
93.endfor
94
95.for _YSRC in ${SRCS:M*.y:N*/*}
96.for _YC in ${_YSRC:R}.c
97SRCS:=	${SRCS:S/${_YSRC}/${_YC}/}
98CLEANFILES+= ${_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}
104CLEANFILES+= 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}
110SRCS+=	${_YH}
111CLEANFILES+= ${_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.endif
123
124.if !target(depend)
125.if defined(SRCS)
126depend: beforedepend ${DEPENDFILE} afterdepend
127
128# Different types of sources are compiled with slightly different flags.
129# Split up the sources, and filter out headers and non-applicable flags.
130.if ${CC} == "icc"
131MKDEP_CFLAGS=	${CFLAGS:M-X*} ${CFLAGS:M-[BID]*}
132MKDEP_CXXFLAGS=	${CXXFLAGS:M-X*} ${CXXFLAGS:M-[BID]*}
133MKDEP_OBJCFLAGS=${OBJCFLAGS:M-X*} ${OBJCFLAGS:M-[BID]*}
134.else
135MKDEP_CFLAGS=	${CFLAGS:M-nostdinc*} ${CFLAGS:M-[BID]*}
136MKDEP_CXXFLAGS=	${CXXFLAGS:M-nostdinc*} ${CXXFLAGS:M-[BID]*}
137MKDEP_OBJCFLAGS=${OBJCFLAGS:M-nostdinc*} ${OBJCFLAGS:M-[BID]*} ${OBJCFLAGS:M-Wno-import*}
138.endif
139
140DPSRCS+= ${SRCS}
141${DEPENDFILE}: ${DPSRCS}
142	rm -f ${DEPENDFILE}
143.if !empty(DPSRCS:M*.[cS])
144	${MKDEPCMD} -f ${DEPENDFILE} -a ${MKDEP} \
145	    ${MKDEP_CFLAGS} ${.ALLSRC:M*.[cS]}
146.endif
147.if !empty(DPSRCS:M*.cc) || !empty(DPSRCS:M*.C) || !empty(DPSRCS:M*.cpp) || \
148    !empty(DPSRCS:M*.cxx)
149	${MKDEPCMD} -f ${DEPENDFILE} -a ${MKDEP} \
150	    ${MKDEP_CXXFLAGS} \
151	    ${.ALLSRC:M*.cc} ${.ALLSRC:M*.C} ${.ALLSRC:M*.cpp} ${.ALLSRC:M*.cxx}
152.endif
153.if !empty(DPSRCS:M*.m)
154	${MKDEPCMD} -f ${DEPENDFILE} -a ${MKDEP} \
155	    ${MKDEP_OBJCFLAGS} ${.ALLSRC:M*.m}
156.endif
157.if target(_EXTRADEPEND)
158_EXTRADEPEND: .USE
159${DEPENDFILE}: _EXTRADEPEND
160.endif
161
162.ORDER: ${DEPENDFILE} afterdepend
163.else
164depend: beforedepend afterdepend
165.endif
166.if !target(beforedepend)
167beforedepend:
168.else
169.ORDER: beforedepend ${DEPENDFILE}
170.ORDER: beforedepend afterdepend
171.endif
172.if !target(afterdepend)
173afterdepend:
174.endif
175.endif
176
177.if !target(cleandepend)
178cleandepend:
179.if defined(SRCS)
180.if ${CTAGS:T} == "ctags"
181	rm -f ${DEPENDFILE} tags
182.elif ${CTAGS:T} == "gtags"
183	rm -f ${DEPENDFILE} GPATH GRTAGS GSYMS GTAGS
184.if defined(HTML)
185	rm -rf HTML
186.endif
187.if defined(CLEANDEPFILES)
188	rm -f ${CLEANDEPFILES}
189.endif
190.endif
191.endif
192.endif
193
194.if !target(checkdpadd) && (defined(DPADD) || defined(LDADD))
195_LDADD_FROM_DPADD=	${DPADD:C;^/usr/lib/lib(.*)\.a$;-l\1;}
196_LDADD_CANONICALIZED=	${LDADD:S/$//}
197checkdpadd:
198.if ${_LDADD_FROM_DPADD} != ${_LDADD_CANONICALIZED}
199	@echo ${.CURDIR}
200	@echo "DPADD -> ${_LDADD_FROM_DPADD}"
201	@echo "LDADD -> ${_LDADD_CANONICALIZED}"
202.endif
203.endif
204