#!/bin/ksh -p # # CDDL HEADER START # # The contents of this file are subject to the terms of the # Common Development and Distribution License (the "License"). # You may not use this file except in compliance with the License. # # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE # or http://www.opensolaris.org/os/licensing. # See the License for the specific language governing permissions # and limitations under the License. # # When distributing Covered Code, include this CDDL HEADER in each # file and include the License file at usr/src/OPENSOLARIS.LICENSE. # If applicable, add the following below this CDDL HEADER, with the # fields enclosed by brackets "[]" replaced with your own identifying # information: Portions Copyright [yyyy] [name of copyright owner] # # CDDL HEADER END # # # Copyright 2008 Sun Microsystems, Inc. All rights reserved. # Use is subject to license terms. # # Copyright 2011 Nexenta Systems, Inc. All rights reserved. # # This script sets up the environment variables for a SunOS # codemgr workspace and spawns a shell with the environment # setup. # # The following Environment variables are set: # CODEMGR_WS # ONBLD_DIR # SRC # TSRC # ROOT # PARENT_ROOT # MACH # MAKEFLAGS # ENVCPPFLAGS{1-4} # ENVLDLIBS{1-3} # # The MAKEFLAGS environment variable is set to force make # to read default make variables from the environment. # # Workspace names can be specified in two forms: pathname # and hostname:pathname. If the hostname:pathname form is used # the script accesses the environment through the /net automounter # map. # # # function to produce a pathname from a workspace name or subdirectory. # The workspace name can have hostname:pathname format. # proc fmtwsname { awk -F: '$1 != $0 { print "/net/"$1$2 } \ $1 == $0 { print $0 }' } # # Return a valid proto area, if one exists. # proc check_proto { if [[ -z $1 ]] { return } if [[ "$SCM_MODE" = "teamware" ]] { # Check for problematic parent specification and adjust setvar proto = $(echo $1|fmtwsname) echo "${proto}/root_${MACH}" } elif [[ "$SCM_MODE" = "mercurial" ]] { setvar proto = "$1" # # If the proto is a local repository then we can use it # to point to the parents proto area. Don't bother to # check if it exists or not, we never did for Teamware, # since it might appear later anyway. # if [[ "${proto##ssh://}" == "$proto" && \ "${proto##http://}" == "$proto" && \ "${proto##https://}" == "$proto" ]] { echo "${proto}/root_${MACH}" } } elif [[ "$SCM_MODE" = "git" ]] { # # For git, we make no attempt to deal with the possibility of # remote parent workspaces because, in the protodefs file, we # don't actually acknowledge the concept of a parent workspace # at all, in keeping with the rest of our git support. # echo "${1}/root_${MACH}" } } proc cleanup_env { # keep the env. clean when returning unset setenv osbld_flag os_rev wsosdir protofile wsname ofs proto \ pwd parent PROTO1 PROTO2 PROTO3 tmpwsname return 0 } if [[ "$1" = "-e" ]] { setvar setenv = 'true' shift } else { setvar setenv = 'false' } setvar WHICH_SCM = "$(/bin/dirname $(whence $0))/which_scm" if [[ ! -x $WHICH_SCM ]] { setvar WHICH_SCM = 'which_scm' } # # No workspace/repository path was given, so try and detect one from our # current directory we're in # if [[ $# -lt 1 ]] { if env CODEMGR_WS="" $WHICH_SCM | read SCM_MODE tmpwsname && \ [[ $SCM_MODE != unknown ]] { echo "Defaulting to $SCM_MODE repository $tmpwsname" } else { echo "usage: ws [-e] [workspace_name]" >&2 if $setenv { cleanup_env return 1 } else { exit 1 } } } else { # # A workspace/repository path was passed in, grab it and pop # it off the stack # setvar tmpwsname = "$1" shift } # # This variable displays the nested activations of workspaces. # This is done here to get the exact name the user entered. # setvar WS_STACK = ""$tmpwsname $WS_STACK""; export WS_STACK # # Set the workspace name and unset tmpwsname (as we reuse it later) # setvar wsname = $(echo $tmpwsname|fmtwsname) unset tmpwsname # # Checking for CODEMGR_WSPATH # if [[ -n ${CODEMGR_WSPATH} && ( ! -d $wsname ) && \ ( `expr "$wsname" : "\/"` = "0" ) ]] { setvar ofs = "$IFS" setvar IFS = "": "" for i in [$CODEMGR_WSPATH] { if [[ -d ${i}/${wsname} ]] { setvar wsname = "${i}/${wsname}" break } } setvar IFS = "$ofs" } # # to translate it to an absolute pathname. We need an # absolute pathname in order to set CODEMGR_WS. # if [[ `expr "$wsname" : "\/"` = "0" ]] { setvar pwd = $(pwd) setvar wsname = ""$pwd/$wsname"" } # # Check to see if this is a valid workspace # if [[ ! -d $wsname ]] { echo "$wsname . . . no such directory" >&2 if $setenv { cleanup_env return 1 } else { exit 1 } } # # This catches the case of a passed in workspace path # Check which type of SCM is in use by $wsname. # shell {cd $wsname && env CODEMGR_WS="" $WHICH_SCM} | read SCM_MODE tmpwsname if [[ $? != 0 || "$SCM_MODE" == unknown ]] { echo "Error: Unable to detect a supported SCM repository in $wsname" if $setenv { cleanup_env return 1 } else { exit 1 } } setvar wsname = "$tmpwsname" setvar CODEMGR_WS = "$wsname" ; export CODEMGR_WS setvar SRC = "$wsname/usr/src"; export SRC setvar TSRC = "$wsname/usr/ontest"; export TSRC if [[ "$SCM_MODE" = "teamware" && -d ${wsname}/Codemgr_wsdata ]] { setvar CM_DATA = ""Codemgr_wsdata"" setvar wsosdir = "$CODEMGR_WS/$CM_DATA/sunos" setvar protofile = "$wsosdir/protodefs" } elif [[ "$SCM_MODE" = "mercurial" && -d ${wsname}/.hg ]] { setvar CM_DATA = "".hg"" setvar wsosdir = "$CODEMGR_WS/$CM_DATA" setvar protofile = "$wsosdir/org.opensolaris.protodefs" } elif [[ "$SCM_MODE" = "git" && -d ${wsname}/.git ]] { setvar CM_DATA = "".git"" setvar wsosdir = "$CODEMGR_WS/$CM_DATA" setvar protofile = "$wsosdir/org.opensolaris.protodefs" } else { echo "$wsname is not a supported workspace; type is $SCM_MODE" >&2 if $setenv { cleanup_env return 1 } else { exit 1 } } setvar MACH = $(uname -p) if [[ ! -f $protofile ]] { if [[ ! -w $CODEMGR_WS/$CM_DATA ]] { # # The workspace doesn't have a protodefs file and I am # unable to create one. Tell user and use /tmp instead. # echo "Unable to create the proto defaults file ($protofile)." # Just make one in /tmp setvar wsosdir = '/tmp' setvar protofile = "$wsosdir/protodefs" } if [[ ! -d $wsosdir ]] { mkdir $wsosdir } cat <<< """ > $protofile #!/bin/sh # # Set default proto areas for this workspace # NOTE: This file was initially automatically generated. # # Feel free to edit this file. If this file is removed # it will be rebuilt containing default values. # # The variable CODEMGR_WS is available to this script. # # PROTO1 is the first proto area searched and is typically set # to a proto area associated with the workspace. The ROOT # environment variable is set to the same as PROTO1. If you # will be doing make installs this proto area needs to be writable. # # PROTO2 and PROTO3 are set to proto areas to search before the # search proceeds to the local machine or the proto area specified by # TERMPROTO. # # TERMPROTO (if specified) is the last place searched. If # TERMPROTO is not specified the search will end at the local # machine. # PROTO1='$'CODEMGR_WS/proto """ > $protofile #!/bin/sh # # Set default proto areas for this workspace # NOTE: This file was initially automatically generated. # # Feel free to edit this file. If this file is removed # it will be rebuilt containing default values. # # The variable CODEMGR_WS is available to this script. # # PROTO1 is the first proto area searched and is typically set # to a proto area associated with the workspace. The ROOT # environment variable is set to the same as PROTO1. If you # will be doing make installs this proto area needs to be writable. # # PROTO2 and PROTO3 are set to proto areas to search before the # search proceeds to the local machine or the proto area specified by # TERMPROTO. # # TERMPROTO (if specified) is the last place searched. If # TERMPROTO is not specified the search will end at the local # machine. # PROTO1=\$CODEMGR_WS/proto PROTOFILE_EoF if [[ "$SCM_MODE" = "teamware" ]] { cat <<< """ >> $protofile if [[ -f "'$'CODEMGR_WS/Codemgr_wsdata/parent" ]]; then # # If this workspace has an codemgr parent then set PROTO2 to # point to the parents proto space. # parent='`'workspace parent '$'CODEMGR_WS'`' if [[ -n '$'parent ]]; then PROTO2='$'parent/proto fi fi """ >> $protofile if [[ -f "\$CODEMGR_WS/Codemgr_wsdata/parent" ]]; then # # If this workspace has an codemgr parent then set PROTO2 to # point to the parents proto space. # parent=\`workspace parent \$CODEMGR_WS\` if [[ -n \$parent ]]; then PROTO2=\$parent/proto fi fi PROTOFILE_EoF } elif [[ "$SCM_MODE" = "mercurial" ]] { cat <<< """ >> $protofile parent='`'(cd '$'CODEMGR_WS && hg path default 2>/dev/null)'`' if [[ '$'? -eq 0 && -n '$'parent ]]; then [[ -n '$'(check_proto '$'parent/proto) ]] && PROTO2='$'parent/proto fi """ >> $protofile parent=\`(cd \$CODEMGR_WS && hg path default 2>/dev/null)\` if [[ \$? -eq 0 && -n \$parent ]]; then [[ -n \$(check_proto \$parent/proto) ]] && PROTO2=\$parent/proto fi PROTOFILE_EoF } } source $protofile # This means you don't have to type make -e all of the time setvar MAKEFLAGS = 'e'; export MAKEFLAGS # # Set up the environment variables # setvar ROOT = "/proto/root_${MACH}" # default setvar ENVCPPFLAGS1 = '' setvar ENVCPPFLAGS2 = '' setvar ENVCPPFLAGS3 = '' setvar ENVCPPFLAGS4 = '' setvar ENVLDLIBS1 = '' setvar ENVLDLIBS2 = '' setvar ENVLDLIBS3 = '' setvar PROTO1 = $(check_proto $PROTO1) if [[ -n "$PROTO1" ]] { # first proto area specifed setvar ROOT = "$PROTO1" setvar ENVCPPFLAGS1 = "-I$ROOT/usr/include" export ENVCPPFLAGS1 setvar ENVLDLIBS1 = ""-L$ROOT/lib -L$ROOT/usr/lib"" export ENVLDLIBS1 setvar PROTO2 = $(check_proto $PROTO2) if [[ -n "$PROTO2" ]] { # second proto area specifed setvar ENVCPPFLAGS2 = "-I$PROTO2/usr/include" export ENVCPPFLAGS2 setvar ENVLDLIBS2 = ""-L$PROTO2/lib -L$PROTO2/usr/lib"" export ENVLDLIBS2 setvar PROTO3 = $(check_proto $PROTO3) if [[ -n "$PROTO3" ]] { # third proto area specifed setvar ENVCPPFLAGS3 = "-I$PROTO3/usr/include" export ENVCPPFLAGS3 setvar ENVLDLIBS3 = ""-L$PROTO3/lib -L$PROTO3/usr/lib"" export ENVLDLIBS3 } } } export ROOT if [[ -n "$TERMPROTO" ]] { # fallback area specifed setvar TERMPROTO = $(check_proto $TERMPROTO) setvar ENVCPPFLAGS4 = ""-Y I,$TERMPROTO/usr/include"" export ENVCPPFLAGS4 setvar ENVLDLIBS3 = ""$ENVLDLIBS3 -Y P,$TERMPROTO/lib:$TERMPROTO/usr/lib"" export ENVLDLIBS3 } setvar osbld_flag = '0' if [[ -z "$ONBLD_DIR" ]] { setvar ONBLD_DIR = $(/bin/dirname $(whence $0)) } if ! echo ":$PATH:" | grep ":${ONBLD_DIR}:" > /dev/null { setvar PATH = ""${ONBLD_DIR}:${ONBLD_DIR}/${MACH}:${PATH}"" setvar osbld_flag = '1' } export PATH if [[ -n "$PROTO2" ]] { # This should point to the parent's proto setvar PARENT_ROOT = "$PROTO2" export PARENT_ROOT } else { # Clear it in case it's already in the env. setvar PARENT_ROOT = '' } export ONBLD_DIR export MACH setvar os_rev = $(uname -r) setvar os_name = $(uname -s) if [[ $os_name != "SunOS" || `expr $os_rev : "5\."` != "2" ]] { # # This is not a SunOS 5.x machine - something is wrong # echo "***WARNING: this script is meant to be run on SunOS 5.x." echo " This machine appears to be running: $os_name $os_rev" } echo "" echo "Workspace : $wsname" if [[ -n "$parent" ]] { echo "Workspace Parent : $parent" } echo "Proto area (\$ROOT) : $ROOT" if [[ -n "$PARENT_ROOT" ]] { echo "Parent proto area (\$PARENT_ROOT) : $PARENT_ROOT" } echo "Root of source (\$SRC) : $SRC" echo "Root of test source (\$TSRC) : $TSRC" if [[ $osbld_flag = "1" ]] { echo "Prepended to PATH : $ONBLD_DIR" } echo "Current directory (\$PWD) : $wsname" echo "" cd $wsname if $setenv { cleanup_env } else { exec ${SHELL:-sh} @ARGV }