xref: /illumos-gate/usr/src/tools/scripts/xref.mk (revision 8c426718)
1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
22# Use is subject to license terms.
23#
24# This Makefile is used exclusively by `xref' to generate and maintain
25# cross-reference databases (right now: cscope, ctags, and etags).
26#
27# By default, the cross-reference is built for all files underneath the
28# currrent working directory that match the criteria specified in the
29# xref.files rule below, and any files that would also be hauled over as
30# part of a `bringover' of the working directory (though this can be
31# turned off via the -f flag to `xref').
32#
33# However, this behavior can be customized in each directory of the build
34# tree through the following Makefile macros, if necessary:
35#
36#   XRDIRS:	The list of directories to include; defaults to `.'.
37#		The more interesting directories should be listed earlier.
38#   XRPRUNE:	The list of directories to prune out.
39#   XRADD:	The list of additional filename globs to include.
40#   XRDEL:	The list of additional filename globs to exclude.
41#   XRINCDIRS:	The list of additional include paths, in "foo bar" format.
42#   XRINCS:	The list of additional include paths, in "-Ifoo -Ibar" format.
43#
44# Note that XRINCDIRS and XRINCS are for specifying header paths that are
45# not already included in CPPFLAGS and HDRDIR.
46#
47# These macros are assumed to be set in a file named `Makefile', but this
48# too can be overridden via the -m option to `xref'.
49#
50# This Makefile should *never* be included by other Makefiles.
51#
52
53XRMAKEFILE=Makefile
54include $(SRC)/Makefile.master
55
56#
57# Default values for the cross-reference tools; these can be overridden
58# either in the environment or in XRMAKEFILE.  To use regular cscope, set
59# CSCOPE to cscope and CSFLAGS to -b.
60#
61CSCOPE	= $(BUILD_TOOLS)/onbld/bin/$(MACH)/cscope-fast
62CSFLAGS	= -bq
63CTAGS	= /usr/bin/ctags
64CTFLAGS	= -wt
65
66# etags was historically part of the Sun compiler distribution and is now
67# distributed in various ways for illumos, we do a path search lacking better
68# options.
69ETAGS	= etags
70ETFLAGS	=
71
72FLGFLP	= $(BUILD_TOOLS)/onbld/bin/flg.flp
73
74XRDIRS	= .
75XRINCS	= $(XRINCDIRS:%=-I%) $(HDRDIR:%=-I%) $(CPPFLAGS)
76
77include $(XRMAKEFILE)
78
79XRADDLIST	= $(XRADD) *.[CcdSshlxy] Makefile* *.cc *.xml \
80		  *.dtd.* *.ndl
81XRDELLIST	= $(XRDEL)
82XRPRUNELIST	= $(XRPRUNE) .hg .git
83XRFINDADD	= $(XRADDLIST:%=-o -name '%')
84XRFINDDEL	= $(XRDELLIST:%=-a ! -name '%')
85XRFINDPRUNE	= $(XRPRUNELIST:%=-o -name '%')
86XRSEDPRUNE	= $(XRPRUNELIST:%=/\/%\//d; /^%\//d;)
87
88.KEEP_STATE:
89.PRECIOUS: cscope.out cscope.in.out cscope.po.out tags TAGS
90
91#
92# Build the list of files to be included in the cross-reference database.
93#
94# Please note that:
95#
96#	* Any additional FLG-related source files are in xref.flg.
97#
98#	* We use relative pathnames for the file list; this makes it easier
99#	  to share the resulting cross-reference across machines.  We also
100#	  strip the leading './' off of pathnames (if necessary) so that we
101#	  don't trip up vi (since it thinks foo.c and ./foo.c are different
102#	  files).
103#
104#	* We strip out any duplicate file names, being careful not to
105#	  disturb the order of the file list.
106#
107#	* We put all the Makefiles at the end of the file list, since they're
108#	  not really source files and thus can cause problems.
109#
110#	* We otherwise do not sort the file list, since we assume that if
111#	  the order matters, then XRDIRS would've been set so that the more
112#	  important directories are first.
113#
114xref.files:
115	$(TOUCH) xref.flg
116	$(FIND) $(XRDIRS) `$(CAT) xref.flg`			\
117	    -type d \( -name SCCS $(XRFINDPRUNE) \) -prune -o	\
118	    -type f \( \( -name '' $(XRFINDADD) \) $(XRFINDDEL) \) -print |\
119	    $(PERL) -ne 's:^\./::; next if ($$seen{$$_}++); print' > xref.tmp
120	> xref.files
121	-$(GREP) -v Makefile xref.tmp >> xref.files
122	-$(GREP) Makefile xref.tmp >> xref.files
123	$(RM) xref.tmp
124
125#
126# Use the .flg files to assemble a list of other source files that are
127# important for building the sources in XRDIRS.  So that the list can be
128# fed to the $(FIND) in xref.files, we tell $(FLGFLP) to generate relative
129# pathnames.  We filter out any files that are along paths that are being
130# pruned.
131#
132xref.flg:
133	> xref.tmp
134	for dir in $(XRDIRS); do					\
135		$(FLGFLP) -r $$dir >> xref.tmp;				\
136	done
137	$(SED) '$(XRSEDPRUNE)' < xref.tmp | $(SORT) -u > xref.flg
138	$(RM) xref.tmp
139
140#
141# Note that we don't remove the old cscope.out since cscope is smart enough
142# to rebuild only what has changed.  It can become confused, however, if files
143# are renamed or removed, so it may be necessary to do an `xref -c' if
144# a lot of reorganization has occured.
145#
146xref.cscope: xref.files
147	-$(ECHO) $(XRINCS) | $(XARGS) -n1 | $(GREP) '^-I' |		\
148	    $(CAT) - xref.files > cscope.files
149	$(CSCOPE) $(CSFLAGS)
150
151xref.cscope.clobber: xref.clean
152	-$(RM) cscope.out cscope.in.out cscope.po.out cscope.files
153
154#
155# Create tags databases, similar to above.
156#
157# Since assembler files contain C fragments for lint, the lint fragments will
158# allow tags to "work" on assembler.  Please note that:
159#
160#	* We order the tags file such that source files that tags seems to
161#	  get along with best are earlier in the list, and so that structure
162#	  definitions are ordered before their uses.
163#
164#	* We *don't* sort the file list within a given suffix, since we
165#	  assume that if someone cared about ordering, they would've already
166#	  set XRDIRS so that the more important directories are first.
167#
168#	* We include "/dev/null" in the xref.ctags rule to prevent ctags
169#	  from barfing if "xref.tfiles" ends up empty (alas, ctags is
170#	  too lame to read its file list from stdin like etags does).
171#
172
173xref.ctags: xref.tfiles
174	$(CTAGS) $(CTFLAGS) /dev/null `$(CAT) xref.tfiles`
175
176xref.ctags.clobber: xref.clean
177	-$(RM) tags
178
179xref.etags: xref.tfiles
180	$(CAT) xref.tfiles | $(ETAGS) $(ETFLAGS) -
181
182xref.etags.check:
183	@$(CAT) /dev/null | $(ETAGS) -
184
185xref.etags.clobber: xref.clean
186	-$(RM) TAGS
187
188xref.tfiles: xref.files
189	> xref.tfiles
190	-for suffix in h c C cc l y s; do				\
191		$(GREP) "\.$${suffix}$$" xref.files >> xref.tfiles;	\
192	done
193
194#
195# Note that we put `cscope.files' in clobber rather than clean because
196# cscope will whine if it doesn't exist (unless it's passed -d).
197#
198xref.clean:
199	-$(RM) xref.tfiles xref.files xref.tmp xref.flg ncscope.*
200