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