xref: /illumos-gate/usr/src/tools/scripts/xref.sh (revision bbf21555)
17c478bd9Sstevel@tonic-gate#! /bin/ksh -p
27c478bd9Sstevel@tonic-gate#
37c478bd9Sstevel@tonic-gate# CDDL HEADER START
47c478bd9Sstevel@tonic-gate#
57c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6cdf0c1d5Smjnelson# Common Development and Distribution License (the "License").
7cdf0c1d5Smjnelson# You may not use this file except in compliance with the License.
87c478bd9Sstevel@tonic-gate#
97c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate# and limitations under the License.
137c478bd9Sstevel@tonic-gate#
147c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate#
207c478bd9Sstevel@tonic-gate# CDDL HEADER END
217c478bd9Sstevel@tonic-gate#
227c478bd9Sstevel@tonic-gate#
2306519974Smeem# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate# Use is subject to license terms.
257c478bd9Sstevel@tonic-gate#
267c478bd9Sstevel@tonic-gate# xref: build and maintain source cross-reference databases.
277c478bd9Sstevel@tonic-gate#
287c478bd9Sstevel@tonic-gate
29cdf0c1d5SmjnelsonONBLDDIR=$(dirname $(whence $0))
30cdf0c1d5Smjnelson
317c478bd9Sstevel@tonic-gatePROG=`basename $0`
327c478bd9Sstevel@tonic-gateXREFMK=`dirname $0`/xref.mk
337c478bd9Sstevel@tonic-gateXRMAKEFILE=Makefile export XRMAKEFILE
34e119f243SRobert MustacchiMAKE="dmake -m serial"
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate#
377c478bd9Sstevel@tonic-gate# The CSCOPEOPTIONS variable can cause problems if it's set in the environment
387c478bd9Sstevel@tonic-gate# when using cscope; remove it.
397c478bd9Sstevel@tonic-gate#
407c478bd9Sstevel@tonic-gateunset CSCOPEOPTIONS
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate#
437c478bd9Sstevel@tonic-gate# The CDPATH variable causes ksh's `cd' builtin to emit messages to stdout
447c478bd9Sstevel@tonic-gate# under certain circumstances, which can really screw things up; unset it.
457c478bd9Sstevel@tonic-gate#
467c478bd9Sstevel@tonic-gateunset CDPATH
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate#
497c478bd9Sstevel@tonic-gate# Print the provided failure message and exit with an error.
507c478bd9Sstevel@tonic-gate#
517c478bd9Sstevel@tonic-gatefail()
527c478bd9Sstevel@tonic-gate{
537c478bd9Sstevel@tonic-gate        echo $PROG: $@ > /dev/stderr
547c478bd9Sstevel@tonic-gate        exit 1
557c478bd9Sstevel@tonic-gate}
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate#
587c478bd9Sstevel@tonic-gate# Print the provided warning message.
597c478bd9Sstevel@tonic-gate#
607c478bd9Sstevel@tonic-gatewarn()
617c478bd9Sstevel@tonic-gate{
627c478bd9Sstevel@tonic-gate        echo $PROG: warning: $@ > /dev/stderr
637c478bd9Sstevel@tonic-gate}
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate#
667c478bd9Sstevel@tonic-gate# Print the provided informational message.
677c478bd9Sstevel@tonic-gate#
687c478bd9Sstevel@tonic-gateinfo()
697c478bd9Sstevel@tonic-gate{
707c478bd9Sstevel@tonic-gate        echo $PROG: $@
717c478bd9Sstevel@tonic-gate}
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate#
747c478bd9Sstevel@tonic-gate# Print the provided informational message, and the current value of $SECONDS
757c478bd9Sstevel@tonic-gate# in a user-friendly format.
767c478bd9Sstevel@tonic-gate#
777c478bd9Sstevel@tonic-gatetimeinfo()
787c478bd9Sstevel@tonic-gate{
797c478bd9Sstevel@tonic-gate	typeset -Z2 sec
8006519974Smeem	typeset -i min seconds
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate	((seconds = SECONDS))
837c478bd9Sstevel@tonic-gate	((min = seconds / 60))
847c478bd9Sstevel@tonic-gate	((sec = seconds % 60))
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate	info "$1 in ${min}m${sec}s"
877c478bd9Sstevel@tonic-gate}
887c478bd9Sstevel@tonic-gate
89cdf0c1d5Smjnelsonwhich_scm | read SCM_MODE CODEMGR_WS || exit 1
90cdf0c1d5Smjnelson
91cdf0c1d5Smjnelsonif [[ $SCM_MODE == "unknown" ]];then
92cdf0c1d5Smjnelson	print -u2 "Unable to determine SCM type currently in use."
93cdf0c1d5Smjnelson	exit 1
947c478bd9Sstevel@tonic-gatefi
957c478bd9Sstevel@tonic-gate
96cdf0c1d5Smjnelsonexport CODEMGR_WS
97cdf0c1d5SmjnelsonSRC=$CODEMGR_WS/usr/src export SRC
98cdf0c1d5SmjnelsonMACH=`uname -p` export MACH
99cdf0c1d5Smjnelson
1007c478bd9Sstevel@tonic-gate[ -f $XREFMK ] || fail "cannot locate xref.mk"
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gateclobber=
1037c478bd9Sstevel@tonic-gatenoflg=
1047c478bd9Sstevel@tonic-gatexrefs=
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gatewhile getopts cfm:px: flag; do
1077c478bd9Sstevel@tonic-gate	case $flag in
1087c478bd9Sstevel@tonic-gate	c)
1097c478bd9Sstevel@tonic-gate		clobber=y
1107c478bd9Sstevel@tonic-gate		;;
1117c478bd9Sstevel@tonic-gate	f)
1127c478bd9Sstevel@tonic-gate		noflg=y
1137c478bd9Sstevel@tonic-gate		;;
1147c478bd9Sstevel@tonic-gate	m)
1157c478bd9Sstevel@tonic-gate		XRMAKEFILE=$OPTARG
1167c478bd9Sstevel@tonic-gate		;;
1177c478bd9Sstevel@tonic-gate	p)
1187c478bd9Sstevel@tonic-gate		#
1197c478bd9Sstevel@tonic-gate		# The ENVCPPFLAGS* environment variables contain the include
1207c478bd9Sstevel@tonic-gate		# paths to our proto areas; clear 'em so that they don't end
1217c478bd9Sstevel@tonic-gate		# up in CPPFLAGS, and thus don't end up in XRINCS in xref.mk.
1227c478bd9Sstevel@tonic-gate		#
1237c478bd9Sstevel@tonic-gate		ENVCPPFLAGS1=
1247c478bd9Sstevel@tonic-gate		ENVCPPFLAGS2=
1257c478bd9Sstevel@tonic-gate		ENVCPPFLAGS3=
1267c478bd9Sstevel@tonic-gate		ENVCPPFLAGS4=
1277c478bd9Sstevel@tonic-gate		;;
1287c478bd9Sstevel@tonic-gate 	x)
1297c478bd9Sstevel@tonic-gate		xrefs=$OPTARG
1307c478bd9Sstevel@tonic-gate		;;
1317c478bd9Sstevel@tonic-gate	\?)
1327c478bd9Sstevel@tonic-gate		echo "usage: $PROG [-cfp] [-m <makefile>]"\
1337c478bd9Sstevel@tonic-gate		     "[-x cscope|ctags|etags[,...]] [<subtree> ...]"\
1347c478bd9Sstevel@tonic-gate		      > /dev/stderr
1357c478bd9Sstevel@tonic-gate		exit 1
1367c478bd9Sstevel@tonic-gate		;;
1377c478bd9Sstevel@tonic-gate	esac
1387c478bd9Sstevel@tonic-gatedone
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gateshift $((OPTIND - 1))
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate#
1437c478bd9Sstevel@tonic-gate# Get the list of directories before we reset $@.
1447c478bd9Sstevel@tonic-gate#
1457c478bd9Sstevel@tonic-gatedirs=$@
1467c478bd9Sstevel@tonic-gate[ -z "$dirs" ] && dirs=.
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate#
1497c478bd9Sstevel@tonic-gate# Get the canonical path to the workspace.  This allows xref to work
150*bbf21555SRichard Lowe# even in the presence of lofs(4FS).
1517c478bd9Sstevel@tonic-gate#
1527c478bd9Sstevel@tonic-gatecd $CODEMGR_WS
1537c478bd9Sstevel@tonic-gateCODEMGR_WS=`/bin/pwd`
1547c478bd9Sstevel@tonic-gatecd - > /dev/null
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate#
1577c478bd9Sstevel@tonic-gate# Process the xref format list.  For convenience, support common synonyms
1587c478bd9Sstevel@tonic-gate# for the xref formats.
1597c478bd9Sstevel@tonic-gate#
1607c478bd9Sstevel@tonic-gateif [ -z "$xrefs" ]; then
1617c478bd9Sstevel@tonic-gate	#
1627c478bd9Sstevel@tonic-gate	# Disable etags if we can't find it.
1637c478bd9Sstevel@tonic-gate	#
1647c478bd9Sstevel@tonic-gate	xrefs="cscope ctags"
165e119f243SRobert Mustacchi	$MAKE -e -f $XREFMK xref.etags.check 2>/dev/null 1>&2 && \
1667c478bd9Sstevel@tonic-gate	    xrefs="$xrefs etags"
1677c478bd9Sstevel@tonic-gateelse
1687c478bd9Sstevel@tonic-gate	oldifs=$IFS
1697c478bd9Sstevel@tonic-gate	IFS=,
1707c478bd9Sstevel@tonic-gate	set -- $xrefs
1717c478bd9Sstevel@tonic-gate	IFS=$oldifs
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate	xrefs=
1747c478bd9Sstevel@tonic-gate	for xref; do
1757c478bd9Sstevel@tonic-gate		case $xref in
1767c478bd9Sstevel@tonic-gate		cscope|cscope.out)
1777c478bd9Sstevel@tonic-gate			xrefs="$xrefs cscope"
1787c478bd9Sstevel@tonic-gate			;;
1797c478bd9Sstevel@tonic-gate		ctags|tags)
1807c478bd9Sstevel@tonic-gate			xrefs="$xrefs ctags"
1817c478bd9Sstevel@tonic-gate			;;
1827c478bd9Sstevel@tonic-gate		etags|TAGS)
1837c478bd9Sstevel@tonic-gate			xrefs="$xrefs etags"
1847c478bd9Sstevel@tonic-gate			;;
1857c478bd9Sstevel@tonic-gate		*)
1867c478bd9Sstevel@tonic-gate			warn "ignoring unknown cross-reference \"$xref\""
1877c478bd9Sstevel@tonic-gate			;;
1887c478bd9Sstevel@tonic-gate 		esac
1897c478bd9Sstevel@tonic-gate 	done
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate	[ -z "$xrefs" ] && fail "no known cross-reference formats specified"
1927c478bd9Sstevel@tonic-gatefi
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate#
1957c478bd9Sstevel@tonic-gate# Process the requested list of directories.
1967c478bd9Sstevel@tonic-gate#
1977c478bd9Sstevel@tonic-gatefor dir in $dirs; do
1987c478bd9Sstevel@tonic-gate	if [ ! -d $dir ]; then
1997c478bd9Sstevel@tonic-gate		warn "directory \"$dir\" does not exist; skipping"
2007c478bd9Sstevel@tonic-gate		continue
2017c478bd9Sstevel@tonic-gate	fi
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate	#
2047c478bd9Sstevel@tonic-gate	# NOTE: we cannot use $PWD because it will mislead in the presence
205*bbf21555SRichard Lowe	# of lofs(4FS).
2067c478bd9Sstevel@tonic-gate	#
2077c478bd9Sstevel@tonic-gate	cd $dir || fail "cannot change to directory $dir"
2087c478bd9Sstevel@tonic-gate	pwd=`/bin/pwd`
2097c478bd9Sstevel@tonic-gate	reldir=${pwd##${CODEMGR_WS}/}
2107c478bd9Sstevel@tonic-gate	if [ "$reldir" = "$pwd" ]; then
2117c478bd9Sstevel@tonic-gate		warn "directory \"$pwd\" is not beneath \$CODEMGR_WS; skipping"
2127c478bd9Sstevel@tonic-gate		cd - > /dev/null
2137c478bd9Sstevel@tonic-gate		continue
2147c478bd9Sstevel@tonic-gate	fi
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate	#
2177c478bd9Sstevel@tonic-gate	# If we're building cross-references, then run `xref.clean' first
2187c478bd9Sstevel@tonic-gate	# to purge any crud that may be lying around from previous aborted runs.
2197c478bd9Sstevel@tonic-gate	#
2207c478bd9Sstevel@tonic-gate	if [ -z "$clobber" ]; then
221e119f243SRobert Mustacchi		$MAKE -e -f $XREFMK xref.clean > /dev/null
2227c478bd9Sstevel@tonic-gate	fi
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate	#
2257c478bd9Sstevel@tonic-gate	# Find flg-related source files, if requested.
2267c478bd9Sstevel@tonic-gate	#
2277c478bd9Sstevel@tonic-gate	if [ -z "$noflg" -a -z "$clobber" ]; then
2287c478bd9Sstevel@tonic-gate		SECONDS=0
2297c478bd9Sstevel@tonic-gate    		info "$reldir: finding flg-related source files"
230e119f243SRobert Mustacchi		$MAKE -e -f $XREFMK xref.flg > /dev/null
2317c478bd9Sstevel@tonic-gate		if [ $? -ne 0 ]; then
2327c478bd9Sstevel@tonic-gate			warn "$reldir: unable to find flg-related source files"
2337c478bd9Sstevel@tonic-gate		else
2347c478bd9Sstevel@tonic-gate			nfiles=`wc -l < xref.flg`
2357c478bd9Sstevel@tonic-gate			if [ "$nfiles" -eq 1 ]; then
2367c478bd9Sstevel@tonic-gate				msg="found 1 flg-related source file"
2377c478bd9Sstevel@tonic-gate			else
2387c478bd9Sstevel@tonic-gate				msg="found $nfiles flg-related source files"
2397c478bd9Sstevel@tonic-gate			fi
2407c478bd9Sstevel@tonic-gate			timeinfo "$reldir: $msg"
2417c478bd9Sstevel@tonic-gate		fi
2427c478bd9Sstevel@tonic-gate	fi
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate	#
2457c478bd9Sstevel@tonic-gate	# Build or clobber all of the requested cross-references.
2467c478bd9Sstevel@tonic-gate	#
2477c478bd9Sstevel@tonic-gate	for xref in $xrefs; do
2487c478bd9Sstevel@tonic-gate		if [ -n "$clobber" ]; then
2497c478bd9Sstevel@tonic-gate			info "$reldir: clobbering $xref cross-reference"
250e119f243SRobert Mustacchi			$MAKE -e -f $XREFMK xref.${xref}.clobber > /dev/null ||
2517c478bd9Sstevel@tonic-gate 			    warn "$reldir: cannot clobber $xref cross-reference"
2527c478bd9Sstevel@tonic-gate			continue
2537c478bd9Sstevel@tonic-gate		fi
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate		SECONDS=0
2567c478bd9Sstevel@tonic-gate		info "$reldir: building $xref cross-reference"
257e119f243SRobert Mustacchi		$MAKE -e -f $XREFMK xref.${xref} > /dev/null ||
2587c478bd9Sstevel@tonic-gate		    fail "$reldir: cannot build $xref cross-reference"
2597c478bd9Sstevel@tonic-gate		timeinfo "$reldir: built $xref cross-reference"
2607c478bd9Sstevel@tonic-gate 	done
2617c478bd9Sstevel@tonic-gate
262e119f243SRobert Mustacchi	$MAKE -e -f $XREFMK xref.clean > /dev/null ||
2637c478bd9Sstevel@tonic-gate	    warn "$reldir: cannot clean up temporary files"
2647c478bd9Sstevel@tonic-gate	cd - > /dev/null
2657c478bd9Sstevel@tonic-gatedone
2667c478bd9Sstevel@tonic-gateexit 0
267