xref: /titanic_52/usr/src/tools/scripts/flg.flp.sh (revision 8bcea973790ad3e762bf78b7c6ad5776e463fd51)
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#
2383d7a252SJohn Beck# Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate# Use is subject to license terms.
257c478bd9Sstevel@tonic-gate#
26cdf0c1d5Smjnelson#
277c478bd9Sstevel@tonic-gate# Generates the list of source files that would get brought over with the
287c478bd9Sstevel@tonic-gate# specified subtree as a result of inc.flg and req.flg files.  If no subtree
297c478bd9Sstevel@tonic-gate# is named, then the current directory is assumed.
307c478bd9Sstevel@tonic-gate#
317c478bd9Sstevel@tonic-gate# Based loosely on ON's version of Teamware's def.dir.flp.
327c478bd9Sstevel@tonic-gate#
337c478bd9Sstevel@tonic-gate
34cdf0c1d5SmjnelsonONBLDDIR=$(dirname $(whence $0))
35cdf0c1d5Smjnelson
36cdf0c1d5SmjnelsonPATH=/usr/bin:${BUILD_TOOLS:-/opt}/teamware/bin:$ONBLDDIR
37cdf0c1d5Smjnelsonexport PATH
387c478bd9Sstevel@tonic-gatePROG=`basename $0`
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate#
417c478bd9Sstevel@tonic-gate# The CDPATH variable causes ksh's `cd' builtin to emit messages to stdout
427c478bd9Sstevel@tonic-gate# under certain circumstances, which will screw up consumers of incflg()
437c478bd9Sstevel@tonic-gate# (and perhaps other things as well); unset it.
447c478bd9Sstevel@tonic-gate#
457c478bd9Sstevel@tonic-gateunset CDPATH
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate#
487c478bd9Sstevel@tonic-gate# Print the usage message and exit with an error.
497c478bd9Sstevel@tonic-gate#
507c478bd9Sstevel@tonic-gateusage()
517c478bd9Sstevel@tonic-gate{
527c478bd9Sstevel@tonic-gate	echo "usage: $PROG [-r] [<dir>]" > /dev/stderr
537c478bd9Sstevel@tonic-gate	exit 1
547c478bd9Sstevel@tonic-gate}
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate#
577c478bd9Sstevel@tonic-gate# Print the provided failure message and exit with an error.
587c478bd9Sstevel@tonic-gate#
597c478bd9Sstevel@tonic-gatefail()
607c478bd9Sstevel@tonic-gate{
617c478bd9Sstevel@tonic-gate	echo $PROG: $@ > /dev/stderr
627c478bd9Sstevel@tonic-gate	exit 1
637c478bd9Sstevel@tonic-gate}
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate# Find the files matching the pattern specified by the first argument in the
667c478bd9Sstevel@tonic-gate# directories named by the remaining arguments.  Unlike def.dir.flp, print
677c478bd9Sstevel@tonic-gate# the name of the source file since we want to make a list of source files,
687c478bd9Sstevel@tonic-gate# not SCCS files.
697c478bd9Sstevel@tonic-gate#
707c478bd9Sstevel@tonic-gatefind_files()
717c478bd9Sstevel@tonic-gate{
727c478bd9Sstevel@tonic-gate   	pat=$1
737c478bd9Sstevel@tonic-gate   	shift
747c478bd9Sstevel@tonic-gate
75*8bcea973SRichard Lowe	if [[ "$SCM_MODE" = "teamware" ]]; then
767c478bd9Sstevel@tonic-gate		for dir; do
77*8bcea973SRichard Lowe			if [[ -d $CODEMGR_WS/$dir ]]; then
787c478bd9Sstevel@tonic-gate				cd $CODEMGR_WS
79cdf0c1d5Smjnelson				find $dir -name "$pat" | \
80cdf0c1d5Smjnelson					sed -n s:/SCCS/s.:/:p | prpath
817c478bd9Sstevel@tonic-gate				cd - > /dev/null
827c478bd9Sstevel@tonic-gate			fi
837c478bd9Sstevel@tonic-gate		done
84*8bcea973SRichard Lowe	elif [[ "$SCM_MODE" = "mercurial" || "$SCM_MODE" == "git" ]]; then
85cdf0c1d5Smjnelson		dirs=""
86cdf0c1d5Smjnelson		for dir; do
87*8bcea973SRichard Lowe			if [[ -d $CODEMGR_WS/$dir ]]; then
88cdf0c1d5Smjnelson				dirs="$dirs|${dir%/}"
89cdf0c1d5Smjnelson			fi
90cdf0c1d5Smjnelson		done
91cdf0c1d5Smjnelson
92cdf0c1d5Smjnelson		# Remove leading pipe before it can confuse egrep
93cdf0c1d5Smjnelson		dirs=${dirs#\|}
94cdf0c1d5Smjnelson		echo "$FILELIST" | egrep "^($dirs)/.*/${pat#s.}\$" | prpath
95cdf0c1d5Smjnelson	fi
967c478bd9Sstevel@tonic-gate}
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate#
997c478bd9Sstevel@tonic-gate# Echo the filename if it exists in the workspace.
1007c478bd9Sstevel@tonic-gate#
1017c478bd9Sstevel@tonic-gateecho_file()
1027c478bd9Sstevel@tonic-gate{
1037c478bd9Sstevel@tonic-gate	[ -f $CODEMGR_WS/$1 ] && echo $1 | prpath
1047c478bd9Sstevel@tonic-gate}
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate#
1077c478bd9Sstevel@tonic-gate# Source the named script, specified as either a full path or a path relative
1087c478bd9Sstevel@tonic-gate# to $CODEMGR_WS.  Although def.dir.flp allows for situations in which the
1097c478bd9Sstevel@tonic-gate# script is actually executed (rather than sourced), this feature has never
1107c478bd9Sstevel@tonic-gate# been used in ON, since it precludes use of echo_file() and find_files().
1117c478bd9Sstevel@tonic-gate#
1127c478bd9Sstevel@tonic-gateexec_file()
1137c478bd9Sstevel@tonic-gate{
114*8bcea973SRichard Lowe	if [[ "${1##/}" = "$1" ]]; then
1157c478bd9Sstevel@tonic-gate		. $CODEMGR_WS/$1
1167c478bd9Sstevel@tonic-gate	else
1177c478bd9Sstevel@tonic-gate		. $1
1187c478bd9Sstevel@tonic-gate	fi
1197c478bd9Sstevel@tonic-gate}
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate#
1227c478bd9Sstevel@tonic-gate# Iterate up through all directories below the named directory, and
1237c478bd9Sstevel@tonic-gate# execute any inc.flg's that may exist.
1247c478bd9Sstevel@tonic-gate#
1257c478bd9Sstevel@tonic-gateincflg()
1267c478bd9Sstevel@tonic-gate{
1277c478bd9Sstevel@tonic-gate	cd $1
1287c478bd9Sstevel@tonic-gate	for i in * .*; 	do
1297c478bd9Sstevel@tonic-gate    		case $i in
1307c478bd9Sstevel@tonic-gate		'*'|.|..)
1317c478bd9Sstevel@tonic-gate			;;
1327c478bd9Sstevel@tonic-gate		inc.flg)
1337c478bd9Sstevel@tonic-gate			exec_file $1/$i
1347c478bd9Sstevel@tonic-gate			;;
1357c478bd9Sstevel@tonic-gate		*)
136*8bcea973SRichard Lowe			if [[ -d $i && ! -h $i ]]; then
1377c478bd9Sstevel@tonic-gate				incflg $1/$i
1387c478bd9Sstevel@tonic-gate				cd $1
1397c478bd9Sstevel@tonic-gate			fi
1407c478bd9Sstevel@tonic-gate			;;
1417c478bd9Sstevel@tonic-gate		esac
1427c478bd9Sstevel@tonic-gate	done
1437c478bd9Sstevel@tonic-gate}
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate#
1467c478bd9Sstevel@tonic-gate# Convert the absolute pathnames named on input to relative pathnames (if
1477c478bd9Sstevel@tonic-gate# necessary) and print them.
1487c478bd9Sstevel@tonic-gate#
1497c478bd9Sstevel@tonic-gateprpath()
1507c478bd9Sstevel@tonic-gate{
151cdf0c1d5Smjnelson	#
152cdf0c1d5Smjnelson	# $CURTREE may be a subdirectory of $CODEMGR_WS, or it
153cdf0c1d5Smjnelson	# may be the root of $CODEMGR_WS.  We want to strip it
154cdf0c1d5Smjnelson	# and end up with a relative path in either case, so the
155cdf0c1d5Smjnelson	# ?(/) pattern is important.  If we don't do that, the
156cdf0c1d5Smjnelson	# dots/tree loop will go on forever.
157cdf0c1d5Smjnelson	#
158cdf0c1d5Smjnelson	reltree=${CURTREE##$CODEMGR_WS?(/)}
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate	while read srcfile; do
161*8bcea973SRichard Lowe		if [[ "$RELPATHS" != y ]]; then
1627c478bd9Sstevel@tonic-gate			echo $srcfile
1637c478bd9Sstevel@tonic-gate			continue
1647c478bd9Sstevel@tonic-gate		fi
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate		dots=
1677c478bd9Sstevel@tonic-gate		tree=$reltree
168*8bcea973SRichard Lowe		while [[ "${srcfile##$tree}" = "$srcfile" ]]; do
1697c478bd9Sstevel@tonic-gate			dots=../$dots
1707c478bd9Sstevel@tonic-gate			tree=`dirname $tree`
17183d7a252SJohn Beck			[ "$tree" = "." ] && break
1727c478bd9Sstevel@tonic-gate		done
1737c478bd9Sstevel@tonic-gate		echo ${dots}${srcfile##$tree/}
1747c478bd9Sstevel@tonic-gate	done
1757c478bd9Sstevel@tonic-gate}
1767c478bd9Sstevel@tonic-gate
177cdf0c1d5Smjnelsonwhich_scm | read SCM_MODE CODEMGR_WS || exit 1
178cdf0c1d5Smjnelson
179cdf0c1d5Smjnelsonif [[ $SCM_MODE == "unknown" ]]; then
180cdf0c1d5Smjnelson	fail "Unable to determine SCM type currently in use."
181cdf0c1d5Smjnelsonelif [[ $SCM_MODE == "mercurial" ]]; then
182*8bcea973SRichard Lowe	FILELIST=$(hg manifest)
183*8bcea973SRichard Loweelif [[ $SCM_MODE == "git" ]]; then
184*8bcea973SRichard Lowe	FILELIST=$(cd $(dirname $(git rev-parse --git-dir)) && git ls-files)
185cdf0c1d5Smjnelsonelif [[ $SCM_MODE != "teamware" ]]; then
186cdf0c1d5Smjnelson	fail "Unsupported SCM in use: $SCM_MODE"
187cdf0c1d5Smjnelsonfi
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gatewhile getopts r flag; do
1907c478bd9Sstevel@tonic-gate	case $flag in
1917c478bd9Sstevel@tonic-gate	r)
1927c478bd9Sstevel@tonic-gate		RELPATHS=y
1937c478bd9Sstevel@tonic-gate		;;
1947c478bd9Sstevel@tonic-gate	\?)
1957c478bd9Sstevel@tonic-gate		usage
1967c478bd9Sstevel@tonic-gate		;;
1977c478bd9Sstevel@tonic-gate	esac
1987c478bd9Sstevel@tonic-gatedone
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gateshift $((OPTIND - 1))
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate[ $# -gt 1 ] && usage
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gateCURTREE=`/bin/pwd`
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate#
2077c478bd9Sstevel@tonic-gate# Determine the subtree being examined.
2087c478bd9Sstevel@tonic-gate#
209*8bcea973SRichard Loweif [[ $# -eq 0 ]]; then
2107c478bd9Sstevel@tonic-gate	SUBTREE=$CURTREE
211*8bcea973SRichard Loweelif [[ -d $1 ]]; then
2127c478bd9Sstevel@tonic-gate	SUBTREE=$1
213*8bcea973SRichard Loweelif [[ -d $CODEMGR_WS/$1 ]]; then
2147c478bd9Sstevel@tonic-gate	SUBTREE=$CODEMGR_WS/$1
2157c478bd9Sstevel@tonic-gateelse
2167c478bd9Sstevel@tonic-gate	fail "neither \$CODEMGR_WS/$1 nor $1 exists as a directory"
2177c478bd9Sstevel@tonic-gatefi
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate#
2207c478bd9Sstevel@tonic-gate# Get the canonical path to the subtree.
2217c478bd9Sstevel@tonic-gate#
2227c478bd9Sstevel@tonic-gatecd $SUBTREE
2237c478bd9Sstevel@tonic-gateSUBTREE=`/bin/pwd`
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate#
2267c478bd9Sstevel@tonic-gate# Get the canonical path to the current directory.
2277c478bd9Sstevel@tonic-gate#
2287c478bd9Sstevel@tonic-gatecd $CURTREE
2297c478bd9Sstevel@tonic-gateCURTREE=`/bin/pwd`
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate#
2327c478bd9Sstevel@tonic-gate# Get the canonical path to the workspace.
2337c478bd9Sstevel@tonic-gate#
2347c478bd9Sstevel@tonic-gatecd $CODEMGR_WS
2357c478bd9Sstevel@tonic-gateCODEMGR_WS=`/bin/pwd`
2367c478bd9Sstevel@tonic-gate
237*8bcea973SRichard Loweif [[ "${SUBTREE##$CODEMGR_WS}" = "$SUBTREE" ]]; then
2387c478bd9Sstevel@tonic-gate	fail "$SUBTREE is not a subtree of \$CODEMGR_WS"
2397c478bd9Sstevel@tonic-gatefi
2407c478bd9Sstevel@tonic-gate
241*8bcea973SRichard Loweif [[ "${CURTREE##$CODEMGR_WS}" = "$CURTREE" ]]; then
2427c478bd9Sstevel@tonic-gate	fail "$CURTREE is not a subtree of \$CODEMGR_WS"
2437c478bd9Sstevel@tonic-gatefi
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate#
2467c478bd9Sstevel@tonic-gate# Find and execute all inc.flg's below our subtree.
2477c478bd9Sstevel@tonic-gate#
2487c478bd9Sstevel@tonic-gateincflg $SUBTREE
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate#
2517c478bd9Sstevel@tonic-gate# Find and execute all req.flg's at or above our subtree.
2527c478bd9Sstevel@tonic-gate#
2537c478bd9Sstevel@tonic-gateTREE=$SUBTREE
254*8bcea973SRichard Lowewhile [[ $TREE != $CODEMGR_WS ]]; do
255*8bcea973SRichard Lowe	[[ -f $TREE/req.flg ]] && exec_file $TREE/req.flg
2567c478bd9Sstevel@tonic-gate	TREE=`dirname $TREE`
2577c478bd9Sstevel@tonic-gatedone
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gateexit 0
260