xref: /titanic_44/usr/src/tools/scripts/xref.sh (revision 2ec7cc7fc084163eaed884efee9bbd322cc8951b)
1#! /bin/ksh -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22#
23# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26# xref: build and maintain source cross-reference databases.
27#
28
29ONBLDDIR=$(dirname $(whence $0))
30
31PATH=/usr/bin:/usr/ccs/bin:${BUILD_TOOLS:-/opt}/teamware/bin:$ONBLDDIR
32export PATH
33PROG=`basename $0`
34XREFMK=`dirname $0`/xref.mk
35XRMAKEFILE=Makefile export XRMAKEFILE
36
37#
38# The CSCOPEOPTIONS variable can cause problems if it's set in the environment
39# when using cscope; remove it.
40#
41unset CSCOPEOPTIONS
42
43#
44# The CDPATH variable causes ksh's `cd' builtin to emit messages to stdout
45# under certain circumstances, which can really screw things up; unset it.
46#
47unset CDPATH
48
49#
50# Print the provided failure message and exit with an error.
51#
52fail()
53{
54        echo $PROG: $@ > /dev/stderr
55        exit 1
56}
57
58#
59# Print the provided warning message.
60#
61warn()
62{
63        echo $PROG: warning: $@ > /dev/stderr
64}
65
66#
67# Print the provided informational message.
68#
69info()
70{
71        echo $PROG: $@
72}
73
74#
75# Print the provided informational message, and the current value of $SECONDS
76# in a user-friendly format.
77#
78timeinfo()
79{
80	typeset -Z2 sec
81	typeset -i min seconds
82
83	((seconds = SECONDS))
84	((min = seconds / 60))
85	((sec = seconds % 60))
86
87	info "$1 in ${min}m${sec}s"
88}
89
90which_scm | read SCM_MODE CODEMGR_WS || exit 1
91
92if [[ $SCM_MODE == "unknown" ]];then
93	print -u2 "Unable to determine SCM type currently in use."
94	exit 1
95fi
96
97export CODEMGR_WS
98SRC=$CODEMGR_WS/usr/src export SRC
99MACH=`uname -p` export MACH
100
101[ -f $XREFMK ] || fail "cannot locate xref.mk"
102
103clobber=
104noflg=
105xrefs=
106
107while getopts cfm:px: flag; do
108	case $flag in
109	c)
110		clobber=y
111		;;
112	f)
113		noflg=y
114		;;
115	m)
116		XRMAKEFILE=$OPTARG
117		;;
118	p)
119		#
120		# The ENVCPPFLAGS* environment variables contain the include
121		# paths to our proto areas; clear 'em so that they don't end
122		# up in CPPFLAGS, and thus don't end up in XRINCS in xref.mk.
123		#
124		ENVCPPFLAGS1=
125		ENVCPPFLAGS2=
126		ENVCPPFLAGS3=
127		ENVCPPFLAGS4=
128		;;
129 	x)
130		xrefs=$OPTARG
131		;;
132	\?)
133		echo "usage: $PROG [-cfp] [-m <makefile>]"\
134		     "[-x cscope|ctags|etags[,...]] [<subtree> ...]"\
135		      > /dev/stderr
136		exit 1
137		;;
138	esac
139done
140
141shift $((OPTIND - 1))
142
143#
144# Get the list of directories before we reset $@.
145#
146dirs=$@
147[ -z "$dirs" ] && dirs=.
148
149#
150# Get the canonical path to the workspace.  This allows xref to work
151# even in the presence of lofs(7FS).
152#
153cd $CODEMGR_WS
154CODEMGR_WS=`/bin/pwd`
155cd - > /dev/null
156
157#
158# Process the xref format list.  For convenience, support common synonyms
159# for the xref formats.
160#
161if [ -z "$xrefs" ]; then
162	#
163	# Disable etags if we can't find it.
164	#
165	xrefs="cscope ctags"
166	make -e -f $XREFMK xref.etags.check 2>/dev/null 1>&2 && \
167	    xrefs="$xrefs etags"
168else
169	oldifs=$IFS
170	IFS=,
171	set -- $xrefs
172	IFS=$oldifs
173
174	xrefs=
175	for xref; do
176		case $xref in
177		cscope|cscope.out)
178			xrefs="$xrefs cscope"
179			;;
180		ctags|tags)
181			xrefs="$xrefs ctags"
182			;;
183		etags|TAGS)
184			xrefs="$xrefs etags"
185			;;
186		*)
187			warn "ignoring unknown cross-reference \"$xref\""
188			;;
189 		esac
190 	done
191
192	[ -z "$xrefs" ] && fail "no known cross-reference formats specified"
193fi
194
195#
196# Process the requested list of directories.
197#
198for dir in $dirs; do
199	if [ ! -d $dir ]; then
200		warn "directory \"$dir\" does not exist; skipping"
201		continue
202	fi
203
204	#
205	# NOTE: we cannot use $PWD because it will mislead in the presence
206	# of lofs(7FS).
207	#
208	cd $dir || fail "cannot change to directory $dir"
209	pwd=`/bin/pwd`
210	reldir=${pwd##${CODEMGR_WS}/}
211	if [ "$reldir" = "$pwd" ]; then
212		warn "directory \"$pwd\" is not beneath \$CODEMGR_WS; skipping"
213		cd - > /dev/null
214		continue
215	fi
216
217	#
218	# If we're building cross-references, then run `xref.clean' first
219	# to purge any crud that may be lying around from previous aborted runs.
220	#
221	if [ -z "$clobber" ]; then
222		make -e -f $XREFMK xref.clean > /dev/null
223	fi
224
225	#
226	# Find flg-related source files, if requested.
227	#
228	if [ -z "$noflg" -a -z "$clobber" ]; then
229		SECONDS=0
230    		info "$reldir: finding flg-related source files"
231		make -e -f $XREFMK xref.flg > /dev/null
232		if [ $? -ne 0 ]; then
233			warn "$reldir: unable to find flg-related source files"
234		else
235			nfiles=`wc -l < xref.flg`
236			if [ "$nfiles" -eq 1 ]; then
237				msg="found 1 flg-related source file"
238			else
239				msg="found $nfiles flg-related source files"
240			fi
241			timeinfo "$reldir: $msg"
242		fi
243	fi
244
245	#
246	# Build or clobber all of the requested cross-references.
247	#
248	for xref in $xrefs; do
249		if [ -n "$clobber" ]; then
250			info "$reldir: clobbering $xref cross-reference"
251			make -e -f $XREFMK xref.${xref}.clobber > /dev/null ||
252 			    warn "$reldir: cannot clobber $xref cross-reference"
253			continue
254		fi
255
256		SECONDS=0
257		info "$reldir: building $xref cross-reference"
258		make -e -f $XREFMK xref.${xref} > /dev/null ||
259		    fail "$reldir: cannot build $xref cross-reference"
260		timeinfo "$reldir: built $xref cross-reference"
261 	done
262
263	make -e -f $XREFMK xref.clean > /dev/null ||
264	    warn "$reldir: cannot clean up temporary files"
265	cd - > /dev/null
266done
267exit 0
268