xref: /titanic_51/usr/src/tools/scripts/ws.sh (revision ff17c8bf86c3e567734be83f90267edee20f580f)
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
109
110#
111# No workspace/repository path was given, so try and detect one from our
112# current directory we're in
113#
114if [ $# -lt 1 ]; then
115	if $WHICH_SCM | read SCM_MODE tmpwsname && \
116	    [[ $SCM_MODE != unknown ]]; then
117		echo "Defaulting to $SCM_MODE repository $tmpwsname"
118	else
119		echo "usage: ws [-e] [workspace_name]" >&2
120		if $setenv; then
121			cleanup_env
122			return 1
123		else
124			exit 1
125		fi
126	fi
127else
128	#
129	# A workspace/repository path was passed in, grab it and pop
130	# it off the stack
131	#
132	tmpwsname=$1
133	shift
134fi
135
136#
137#	This variable displays the nested activations of workspaces.
138#	This is done here to get the exact name the user entered.
139#
140WS_STACK="$tmpwsname $WS_STACK"; export WS_STACK
141
142#
143# Set the workspace name and unset tmpwsname (as we reuse it later)
144#
145wsname=`echo $tmpwsname|fmtwsname`
146unset tmpwsname
147
148#
149# Checking for CODEMGR_WSPATH
150#
151if [ "(" "${CODEMGR_WSPATH}x" != "x" ")" -a "(" ! -d $wsname ")" -a \
152     "(" `expr "$wsname" : "\/"` = "0" ")" ]
153then
154	ofs=$IFS
155	IFS=": 	"
156	for i in $CODEMGR_WSPATH
157	do
158		if [ -d ${i}/${wsname} ]; then
159			wsname=${i}/${wsname}
160			break
161		fi
162	done
163	IFS=$ofs
164fi
165
166#
167# to translate it to an absolute pathname.  We need an
168# absolute pathname in order to set CODEMGR_WS.
169#
170if [ `expr "$wsname" : "\/"` = "0" ]
171then
172	pwd=`pwd`
173	wsname="$pwd/$wsname"
174fi
175
176#
177#	Check to see if this is a valid workspace
178#
179if [ ! -d $wsname ]; then
180	echo "$wsname . . . no such directory" >&2
181	if $setenv; then
182		cleanup_env
183		return 1
184	else
185		exit 1
186	fi
187fi
188
189#
190# This catches the case of a passed in workspace path
191# Check which type of SCM is in use by $wsname.
192#
193(cd $wsname && $WHICH_SCM) | read SCM_MODE tmpwsname
194if [[ $? != 0 || "$SCM_MODE" == unknown ]]; then
195	echo "Error: Unable to detect a supported SCM repository in $wsname"
196	if $setenv; then
197		cleanup_env
198		return 1
199	else
200		exit 1
201	fi
202fi
203
204wsname=$tmpwsname
205CODEMGR_WS=$wsname ; export CODEMGR_WS
206SRC=$wsname/usr/src; export SRC
207TSRC=$wsname/usr/ontest; export TSRC
208
209if [ "$SCM_MODE" = "teamware" -a -d ${wsname}/Codemgr_wsdata ]; then
210	CM_DATA="Codemgr_wsdata"
211	wsosdir=$CODEMGR_WS/$CM_DATA/sunos
212	protofile=$wsosdir/protodefs
213elif [ "$SCM_MODE" = "mercurial" -a -d ${wsname}/.hg ]; then
214	CM_DATA=".hg"
215	wsosdir=$CODEMGR_WS/$CM_DATA
216	protofile=$wsosdir/org.opensolaris.protodefs
217else
218	echo "$wsname is not a supported workspace; type is $SCM_MODE" >&2
219	if $setenv; then
220		cleanup_env
221		return 1
222	else
223		exit 1
224	fi
225fi
226
227if [ ! -f $protofile ]; then
228	if [ ! -w $CODEMGR_WS/$CM_DATA ]; then
229		#
230		# The workspace doesn't have a protodefs file and I am
231		# unable to create one.  Tell user and use /tmp instead.
232		#
233		echo "Unable to create the proto defaults file ($protofile)."
234
235		# Just make one in /tmp
236		wsosdir=/tmp
237		protofile=$wsosdir/protodefs
238	fi
239
240	if [ ! -d $wsosdir ]; then
241		mkdir $wsosdir
242	fi
243
244	cat << PROTOFILE_EoF > $protofile
245#!/bin/sh
246#
247#	Set default proto areas for this workspace
248#	NOTE: This file was initially automatically generated.
249#
250#	Feel free to edit this file.  If this file is removed
251#	it will be rebuilt containing default values.
252#
253#	The variable CODEMGR_WS is available to this script.
254#
255#	PROTO1 is the first proto area searched and is typically set
256#	to a proto area associated with the workspace.  The ROOT
257#	environment variable is set to the same as PROTO1.  If you
258#	will be doing make installs this proto area needs to be writable.
259#
260#	PROTO2 and PROTO3 are set to proto areas to search before the
261#	search proceeds to the local machine or the proto area specified by
262#	TERMPROTO.
263#
264#	TERMPROTO (if specified) is the last place searched.  If
265#	TERMPROTO is not specified the search will end at the local
266#	machine.
267#
268
269PROTO1=\$CODEMGR_WS/proto
270PROTOFILE_EoF
271
272	if [ "$SCM_MODE" = "teamware" ]; then
273		cat << PROTOFILE_EoF >> $protofile
274if [ -f "\$CODEMGR_WS/Codemgr_wsdata/parent" ]; then
275   #
276   # If this workspace has an codemgr parent then set PROTO2 to
277   # point to the parents proto space.
278   #
279   parent=\`workspace parent \$CODEMGR_WS\`
280   if [[ -n \$parent ]]; then
281	   PROTO2=\$parent/proto
282   fi
283fi
284PROTOFILE_EoF
285	elif [ "$SCM_MODE" = "mercurial" ]; then
286		cat << PROTOFILE_EoF >> $protofile
287parent=\`(cd \$CODEMGR_WS && hg path default 2>/dev/null)\`
288if [[ \$? -eq 0 && -n \$parent ]]; then
289   PROTO2=\$(check_proto \$parent/proto)
290fi
291PROTOFILE_EoF
292	fi
293fi
294
295. $protofile
296
297# This means you don't have to type make -e all of the time
298
299MAKEFLAGS=e; export MAKEFLAGS
300
301#
302#	Set up the environment variables
303#
304MACH=`uname -p`
305ROOT=/proto/root_${MACH}	# default
306
307ENVCPPFLAGS1=
308ENVCPPFLAGS2=
309ENVCPPFLAGS3=
310ENVCPPFLAGS4=
311ENVLDLIBS1=
312ENVLDLIBS2=
313ENVLDLIBS3=
314
315PROTO1=`check_proto $PROTO1`
316if [[ -n "$PROTO1" ]]; then	# first proto area specifed
317	ROOT=$PROTO1
318	ENVCPPFLAGS1=-I$ROOT/usr/include
319	export ENVCPPFLAGS1
320	ENVLDLIBS1="-L$ROOT/lib -L$ROOT/usr/lib"
321	export ENVLDLIBS1
322
323	PROTO2=`check_proto $PROTO2`
324	if [[ -n "$PROTO2" ]]; then	# second proto area specifed
325		ENVCPPFLAGS2=-I$PROTO2/usr/include
326		export ENVCPPFLAGS2
327		ENVLDLIBS2="-L$PROTO2/lib -L$PROTO2/usr/lib"
328		export ENVLDLIBS2
329
330		PROTO3=`check_proto $PROTO3`
331		if [[ -n "$PROTO3" ]]; then	# third proto area specifed
332			PROTO3=`check_proto $PROTO3`
333			ENVCPPFLAGS3=-I$PROTO3/usr/include
334			export ENVCPPFLAGS3
335			ENVLDLIBS3="-L$PROTO3/lib -L$PROTO3/usr/lib"
336			export ENVLDLIBS3
337		fi
338	fi
339fi
340
341export ROOT
342
343if [[ -n "$TERMPROTO" ]]; then	# fallback area specifed
344	TERMPROTO=`check_proto $TERMPROTO`
345	ENVCPPFLAGS4="-Y I,$TERMPROTO/usr/include"
346	export ENVCPPFLAGS4
347	ENVLDLIBS3="$ENVLDLIBS3 -Y P,$TERMPROTO/lib:$TERMPROTO/usr/lib"
348	export ENVLDLIBS3
349fi
350
351osbld_flag=0
352
353if [[ -z "$ONBLD_DIR" ]]; then
354	ONBLD_DIR=$(dirname $(whence $0))
355fi
356
357if ! echo ":$PATH:" | grep ":${ONBLD_DIR}:" > /dev/null; then
358	PATH="${ONBLD_DIR}:${ONBLD_DIR}/${MACH}:${PATH}"
359	osbld_flag=1
360fi
361
362export PATH
363
364if [[ -n "$PROTO2" ]]; then
365   # This should point to the parent's proto
366   PARENT_ROOT=$PROTO2
367   export PARENT_ROOT
368else
369   # Clear it in case it's already in the env.
370   PARENT_ROOT=
371fi
372export ONBLD_DIR
373export MACH
374
375os_rev=`uname -r`
376os_name=`uname -s`
377
378if [[ $os_name != "SunOS" || `expr $os_rev : "5\."` != "2" ]]; then
379   #
380   # This is not a SunOS 5.x machine - something is wrong
381   #
382   echo "***WARNING: this script is meant to be run on SunOS 5.x."
383   echo "            This machine appears to be running: $os_name $os_rev"
384fi
385
386echo ""
387echo "Workspace                    : $wsname"
388if [ -n "$parent" ]; then
389   echo "Workspace Parent             : $parent"
390fi
391echo "Proto area (\$ROOT)           : $ROOT"
392if [ -n "$PARENT_ROOT" ]; then
393   echo "Parent proto area (\$PARENT_ROOT) : $PARENT_ROOT"
394fi
395echo "Root of source (\$SRC)        : $SRC"
396echo "Root of test source (\$TSRC)  : $TSRC"
397if [ $osbld_flag = "1" ]; then
398   echo "Prepended to PATH            : $ONBLD_DIR"
399fi
400echo "Current directory (\$PWD)     : $wsname"
401echo ""
402
403cd $wsname
404
405if $setenv; then
406	cleanup_env
407else
408	exec ${SHELL:-sh} "$@"
409fi
410