bsd.dep.mk revision 124637
1# $FreeBSD: head/share/mk/bsd.dep.mk 124637 2004-01-17 18:51:55Z ru $
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
46CTAGS?=		gtags
47CTAGSFLAGS?=
48GTAGSFLAGS?=	-o
49HTAGSFLAGS?=
50
51.if ${CC} != "cc"
52MKDEPCMD?=	CC='${CC}' mkdep
53.else
54MKDEPCMD?=	mkdep
55.endif
56DEPENDFILE?=	.depend
57
58# Keep `tags' here, before SRCS are mangled below for `depend'.
59.if !target(tags) && defined(SRCS) && !defined(NOTAGS)
60tags: ${SRCS}
61.if ${CTAGS:T} == "ctags"
62	@${CTAGS} ${CTAGSFLAGS} -f /dev/stdout \
63	    ${.ALLSRC:N*.h} | sed "s;${.CURDIR}/;;" > ${.TARGET}
64.elif ${CTAGS:T} == "gtags"
65	@cd ${.CURDIR} && ${CTAGS} ${GTAGSFLAGS} ${.OBJDIR}
66.if defined(HTML)
67	@cd ${.CURDIR} && htags ${HTAGSFLAGS} -d ${.OBJDIR} ${.OBJDIR}
68.endif
69.endif
70.endif
71
72.if defined(SRCS)
73CLEANFILES?=
74
75.if !exists(${.OBJDIR}/${DEPENDFILE})
76.for _S in ${SRCS:N*.[hly]}
77${_S:R}.o: ${_S}
78.endfor
79.endif
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.if !exists(${.OBJDIR}/${DEPENDFILE})
86${_LC:R}.o: ${_LC}
87.endif
88SRCS:=	${SRCS:S/${_LSRC}/${_LC}/}
89CLEANFILES+= ${_LC}
90.endfor
91.endfor
92
93.for _YSRC in ${SRCS:M*.y:N*/*}
94.for _YC in ${_YSRC:R}.c
95SRCS:=	${SRCS:S/${_YSRC}/${_YC}/}
96CLEANFILES+= ${_YC}
97.if ${YFLAGS:M-d} != "" && ${SRCS:My.tab.h}
98.ORDER: ${_YC} y.tab.h
99${_YC} y.tab.h: ${_YSRC}
100	${YACC} ${YFLAGS} ${.ALLSRC}
101	cp y.tab.c ${_YC}
102CLEANFILES+= y.tab.c y.tab.h
103.elif ${YFLAGS:M-d} != ""
104.for _YH in ${_YC:R}.h
105.ORDER: ${_YC} ${_YH}
106${_YC} ${_YH}: ${_YSRC}
107	${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
108SRCS+=	${_YH}
109CLEANFILES+= ${_YH}
110.endfor
111.else
112${_YC}: ${_YSRC}
113	${YACC} ${YFLAGS} -o ${_YC} ${.ALLSRC}
114.endif
115.if !exists(${.OBJDIR}/${DEPENDFILE})
116${_YC:R}.o: ${_YC}
117.endif
118.endfor
119.endfor
120.endif
121
122.if !target(depend)
123.if defined(SRCS)
124depend: beforedepend ${DEPENDFILE} afterdepend
125
126# Different types of sources are compiled with slightly different flags.
127# Split up the sources, and filter out headers and non-applicable flags.
128DPSRCS+= ${SRCS}
129${DEPENDFILE}: ${DPSRCS}
130	rm -f ${DEPENDFILE}
131.if ${DPSRCS:M*.[cS]} != ""
132	${MKDEPCMD} -f ${DEPENDFILE} -a ${MKDEP} \
133	    ${CFLAGS:M-nostdinc*} ${CFLAGS:M-[BID]*} \
134	    ${.ALLSRC:M*.[cS]}
135.endif
136.if ${DPSRCS:M*.cc} != "" || ${DPSRCS:M*.C} != "" || ${DPSRCS:M*.cpp} != "" || \
137    ${DPSRCS:M*.cxx} != ""
138	${MKDEPCMD} -f ${DEPENDFILE} -a ${MKDEP} \
139	    ${CXXFLAGS:M-nostdinc*} ${CXXFLAGS:M-[BID]*} \
140	    ${.ALLSRC:M*.cc} ${.ALLSRC:M*.C} ${.ALLSRC:M*.cpp} ${.ALLSRC:M*.cxx}
141.endif
142.if ${DPSRCS:M*.m} != ""
143	${MKDEPCMD} -f ${DEPENDFILE} -a ${MKDEP} \
144	    ${OBJCFLAGS:M-nostdinc*} ${OBJCFLAGS:M-[BID]*} \
145	    ${OBJCFLAGS:M-Wno-import*} \
146	    ${.ALLSRC:M*.m}
147.endif
148.if target(_EXTRADEPEND)
149_EXTRADEPEND: .USE
150${DEPENDFILE}: _EXTRADEPEND
151.endif
152
153.ORDER: ${DEPENDFILE} afterdepend
154.else
155depend: beforedepend afterdepend
156.endif
157.if !target(beforedepend)
158beforedepend:
159.else
160.ORDER: beforedepend ${DEPENDFILE}
161.ORDER: beforedepend afterdepend
162.endif
163.if !target(afterdepend)
164afterdepend:
165.endif
166.endif
167
168.if !target(cleandepend)
169cleandepend:
170.if defined(SRCS)
171.if ${CTAGS:T} == "ctags"
172	rm -f ${DEPENDFILE} tags
173.elif ${CTAGS:T} == "gtags"
174	rm -f ${DEPENDFILE} GPATH GRTAGS GSYMS GTAGS
175.if defined(HTML)
176	rm -rf HTML
177.endif
178.endif
179.endif
180.endif
181
182.if !target(checkdpadd) && (defined(DPADD) || defined(LDADD))
183_LDADD_FROM_DPADD=	${DPADD:C;^/usr/lib/lib(.*)\.a$;-l\1;}
184_LDADD_CANONICALIZED=	${LDADD:S/$//}
185checkdpadd:
186.if ${_LDADD_FROM_DPADD} != ${_LDADD_CANONICALIZED}
187	@echo ${.CURDIR}
188	@echo "DPADD -> ${_LDADD_FROM_DPADD}"
189	@echo "LDADD -> ${_LDADD_CANONICALIZED}"
190.endif
191.endif
192