xref: /titanic_41/usr/src/tools/scripts/ws.sh (revision e4f5a11d4a234623168c1558fcdf4341e11769e1)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26# ident	"%Z%%M%	%I%	%E% SMI"
27#
28#	This script sets up the environment variables for a SunOS
29#	codemgr workspace and spawns a shell with the environment
30#	setup.
31#
32#	The following Environment variables are set:
33#		CODEMGR_WS
34#		ONBLD_DIR
35#		SRC
36#		TSRC
37#		ROOT
38#		PARENT_ROOT
39#		MACH
40#		MAKEFLAGS
41#		ENVCPPFLAGS{1-4}
42#		ENVLDLIBS{1-3}
43#
44#	The MAKEFLAGS environment variable is set to force make
45#	to read default make variables from the environment.
46#
47#	Workspace names can be specified in two forms: pathname
48#	and hostname:pathname.  If the hostname:pathname form is used
49#	the script accesses the environment through the /net automounter
50#	map.
51#
52
53#
54# function to produce a pathname from a workspace name or subdirectory.
55# The workspace name can have hostname:pathname format.
56#
57
58fmtwsname()
59{
60	awk -F: '$1 != $0 { print "/net/"$1$2 } \
61		 $1 == $0 { print $0 }'
62}
63
64#
65# Return a valid proto area, if one exists.
66#
67check_proto()
68{
69	if [[ -z $1 ]]; then
70		return
71	fi
72
73	if [ "$SCM_MODE" = "teamware" ]; then
74		# Check for problematic parent specification and adjust
75		proto=`echo $1|fmtwsname`
76		echo "${proto}/root_${MACH}"
77	elif [ "$SCM_MODE" = "mercurial" ]; then
78		proto=$1
79		#
80		# If the proto is a local repository then we can use it
81		# to point to the parents proto area. Don't bother to
82		# check if it exists or not, we never did for Teamware,
83		# since it might appear later anyway.
84		#
85		if [ "${proto##ssh://}" == "$proto" -a \
86		     "${proto##http://}" == "$proto" -a \
87		     "${proto##https://}" == "$proto" ]; then
88			echo "${proto}/root_${MACH}"
89		fi
90	fi
91}
92
93cleanup_env()
94{
95	# keep the env. clean when returning
96	unset setenv osbld_flag os_rev wsosdir protofile wsname ofs proto \
97		pwd parent PROTO1 PROTO2 PROTO3 tmpwsname
98	return 0
99}
100
101if [ "$1" = "-e" ]; then
102	setenv=true
103	shift
104else
105	setenv=false
106fi
107
108WHICH_SCM=$(dirname $(whence $0))/which_scm
109if [[ ! -x $WHICH_SCM ]]; then
110	WHICH_SCM=which_scm
111fi
112
113#
114# No workspace/repository path was given, so try and detect one from our
115# current directory we're in
116#
117if [ $# -lt 1 ]; then
118	if env CODEMGR_WS="" $WHICH_SCM | read SCM_MODE tmpwsname && \
119	    [[ $SCM_MODE != unknown ]]; then
120		echo "Defaulting to $SCM_MODE repository $tmpwsname"
121	else
122		echo "usage: ws [-e] [workspace_name]" >&2
123		if $setenv; then
124			cleanup_env
125			return 1
126		else
127			exit 1
128		fi
129	fi
130else
131	#
132	# A workspace/repository path was passed in, grab it and pop
133	# it off the stack
134	#
135	tmpwsname=$1
136	shift
137fi
138
139#
140#	This variable displays the nested activations of workspaces.
141#	This is done here to get the exact name the user entered.
142#
143WS_STACK="$tmpwsname $WS_STACK"; export WS_STACK
144
145#
146# Set the workspace name and unset tmpwsname (as we reuse it later)
147#
148wsname=`echo $tmpwsname|fmtwsname`
149unset tmpwsname
150
151#
152# Checking for CODEMGR_WSPATH
153#
154if [ "(" "${CODEMGR_WSPATH}x" != "x" ")" -a "(" ! -d $wsname ")" -a \
155     "(" `expr "$wsname" : "\/"` = "0" ")" ]
156then
157	ofs=$IFS
158	IFS=": 	"
159	for i in $CODEMGR_WSPATH
160	do
161		if [ -d ${i}/${wsname} ]; then
162			wsname=${i}/${wsname}
163			break
164		fi
165	done
166	IFS=$ofs
167fi
168
169#
170# to translate it to an absolute pathname.  We need an
171# absolute pathname in order to set CODEMGR_WS.
172#
173if [ `expr "$wsname" : "\/"` = "0" ]
174then
175	pwd=`pwd`
176	wsname="$pwd/$wsname"
177fi
178
179#
180#	Check to see if this is a valid workspace
181#
182if [ ! -d $wsname ]; then
183	echo "$wsname . . . no such directory" >&2
184	if $setenv; then
185		cleanup_env
186		return 1
187	else
188		exit 1
189	fi
190fi
191
192#
193# This catches the case of a passed in workspace path
194# Check which type of SCM is in use by $wsname.
195#
196(cd $wsname && env CODEMGR_WS="" $WHICH_SCM) | read SCM_MODE tmpwsname
197if [[ $? != 0 || "$SCM_MODE" == unknown ]]; then
198	echo "Error: Unable to detect a supported SCM repository in $wsname"
199	if $setenv; then
200		cleanup_env
201		return 1
202	else
203		exit 1
204	fi
205fi
206
207wsname=$tmpwsname
208CODEMGR_WS=$wsname ; export CODEMGR_WS
209SRC=$wsname/usr/src; export SRC
210TSRC=$wsname/usr/ontest; export TSRC
211
212if [ "$SCM_MODE" = "teamware" -a -d ${wsname}/Codemgr_wsdata ]; then
213	CM_DATA="Codemgr_wsdata"
214	wsosdir=$CODEMGR_WS/$CM_DATA/sunos
215	protofile=$wsosdir/protodefs
216elif [ "$SCM_MODE" = "mercurial" -a -d ${wsname}/.hg ]; then
217	CM_DATA=".hg"
218	wsosdir=$CODEMGR_WS/$CM_DATA
219	protofile=$wsosdir/org.opensolaris.protodefs
220else
221	echo "$wsname is not a supported workspace; type is $SCM_MODE" >&2
222	if $setenv; then
223		cleanup_env
224		return 1
225	else
226		exit 1
227	fi
228fi
229
230MACH=`uname -p`
231
232if [ ! -f $protofile ]; then
233	if [ ! -w $CODEMGR_WS/$CM_DATA ]; then
234		#
235		# The workspace doesn't have a protodefs file and I am
236		# unable to create one.  Tell user and use /tmp instead.
237		#
238		echo "Unable to create the proto defaults file ($protofile)."
239
240		# Just make one in /tmp
241		wsosdir=/tmp
242		protofile=$wsosdir/protodefs
243	fi
244
245	if [ ! -d $wsosdir ]; then
246		mkdir $wsosdir
247	fi
248
249	cat << PROTOFILE_EoF > $protofile
250#!/bin/sh
251#
252#	Set default proto areas for this workspace
253#	NOTE: This file was initially automatically generated.
254#
255#	Feel free to edit this file.  If this file is removed
256#	it will be rebuilt containing default values.
257#
258#	The variable CODEMGR_WS is available to this script.
259#
260#	PROTO1 is the first proto area searched and is typically set
261#	to a proto area associated with the workspace.  The ROOT
262#	environment variable is set to the same as PROTO1.  If you
263#	will be doing make installs this proto area needs to be writable.
264#
265#	PROTO2 and PROTO3 are set to proto areas to search before the
266#	search proceeds to the local machine or the proto area specified by
267#	TERMPROTO.
268#
269#	TERMPROTO (if specified) is the last place searched.  If
270#	TERMPROTO is not specified the search will end at the local
271#	machine.
272#
273
274PROTO1=\$CODEMGR_WS/proto
275PROTOFILE_EoF
276
277	if [ "$SCM_MODE" = "teamware" ]; then
278		cat << PROTOFILE_EoF >> $protofile
279if [ -f "\$CODEMGR_WS/Codemgr_wsdata/parent" ]; then
280   #
281   # If this workspace has an codemgr parent then set PROTO2 to
282   # point to the parents proto space.
283   #
284   parent=\`workspace parent \$CODEMGR_WS\`
285   if [[ -n \$parent ]]; then
286	   PROTO2=\$parent/proto
287   fi
288fi
289PROTOFILE_EoF
290	elif [ "$SCM_MODE" = "mercurial" ]; then
291		cat << PROTOFILE_EoF >> $protofile
292parent=\`(cd \$CODEMGR_WS && hg path default 2>/dev/null)\`
293if [[ \$? -eq 0 && -n \$parent ]]; then
294   [[ -n \$(check_proto \$parent/proto) ]] && PROTO2=\$parent/proto
295fi
296PROTOFILE_EoF
297	fi
298fi
299
300. $protofile
301
302# This means you don't have to type make -e all of the time
303
304MAKEFLAGS=e; export MAKEFLAGS
305
306#
307#	Set up the environment variables
308#
309ROOT=/proto/root_${MACH}	# default
310
311ENVCPPFLAGS1=
312ENVCPPFLAGS2=
313ENVCPPFLAGS3=
314ENVCPPFLAGS4=
315ENVLDLIBS1=
316ENVLDLIBS2=
317ENVLDLIBS3=
318
319PROTO1=`check_proto $PROTO1`
320if [[ -n "$PROTO1" ]]; then	# first proto area specifed
321	ROOT=$PROTO1
322	ENVCPPFLAGS1=-I$ROOT/usr/include
323	export ENVCPPFLAGS1
324	ENVLDLIBS1="-L$ROOT/lib -L$ROOT/usr/lib"
325	export ENVLDLIBS1
326
327	PROTO2=`check_proto $PROTO2`
328	if [[ -n "$PROTO2" ]]; then	# second proto area specifed
329		ENVCPPFLAGS2=-I$PROTO2/usr/include
330		export ENVCPPFLAGS2
331		ENVLDLIBS2="-L$PROTO2/lib -L$PROTO2/usr/lib"
332		export ENVLDLIBS2
333
334		PROTO3=`check_proto $PROTO3`
335		if [[ -n "$PROTO3" ]]; then	# third proto area specifed
336			ENVCPPFLAGS3=-I$PROTO3/usr/include
337			export ENVCPPFLAGS3
338			ENVLDLIBS3="-L$PROTO3/lib -L$PROTO3/usr/lib"
339			export ENVLDLIBS3
340		fi
341	fi
342fi
343
344export ROOT
345
346if [[ -n "$TERMPROTO" ]]; then	# fallback area specifed
347	TERMPROTO=`check_proto $TERMPROTO`
348	ENVCPPFLAGS4="-Y I,$TERMPROTO/usr/include"
349	export ENVCPPFLAGS4
350	ENVLDLIBS3="$ENVLDLIBS3 -Y P,$TERMPROTO/lib:$TERMPROTO/usr/lib"
351	export ENVLDLIBS3
352fi
353
354osbld_flag=0
355
356if [[ -z "$ONBLD_DIR" ]]; then
357	ONBLD_DIR=$(dirname $(whence $0))
358fi
359
360if ! echo ":$PATH:" | grep ":${ONBLD_DIR}:" > /dev/null; then
361	PATH="${ONBLD_DIR}:${ONBLD_DIR}/${MACH}:${PATH}"
362	osbld_flag=1
363fi
364
365export PATH
366
367if [[ -n "$PROTO2" ]]; then
368   # This should point to the parent's proto
369   PARENT_ROOT=$PROTO2
370   export PARENT_ROOT
371else
372   # Clear it in case it's already in the env.
373   PARENT_ROOT=
374fi
375export ONBLD_DIR
376export MACH
377
378os_rev=`uname -r`
379os_name=`uname -s`
380
381if [[ $os_name != "SunOS" || `expr $os_rev : "5\."` != "2" ]]; then
382   #
383   # This is not a SunOS 5.x machine - something is wrong
384   #
385   echo "***WARNING: this script is meant to be run on SunOS 5.x."
386   echo "            This machine appears to be running: $os_name $os_rev"
387fi
388
389echo ""
390echo "Workspace                    : $wsname"
391if [ -n "$parent" ]; then
392   echo "Workspace Parent             : $parent"
393fi
394echo "Proto area (\$ROOT)           : $ROOT"
395if [ -n "$PARENT_ROOT" ]; then
396   echo "Parent proto area (\$PARENT_ROOT) : $PARENT_ROOT"
397fi
398echo "Root of source (\$SRC)        : $SRC"
399echo "Root of test source (\$TSRC)  : $TSRC"
400if [ $osbld_flag = "1" ]; then
401   echo "Prepended to PATH            : $ONBLD_DIR"
402fi
403echo "Current directory (\$PWD)     : $wsname"
404echo ""
405
406cd $wsname
407
408if $setenv; then
409	cleanup_env
410else
411	exec ${SHELL:-sh} "$@"
412fi
413