Home | History | Annotate | Download | only in scripts
      1 #!/usr/bin/ksh93
      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 #
     24 # Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     25 # Use is subject to license terms.
     26 #
     27 # Uses supplied "env" file, based on /opt/onbld/etc/env, to set shell variables
     28 # before spawning a shell for doing a release-style builds interactively
     29 # and incrementally.
     30 #
     31 
     32 function usage
     33 {
     34     OPTIND=0
     35     getopts -a "${progname}" "${USAGE}" OPT '-?'
     36     exit 2
     37 }
     38 
     39 function is_source_build
     40 {
     41 	"${flags.s.e}" || "${flags.s.d}" || "${flags.s.h}" || "${flags.s.o}"
     42 	return $?
     43 }
     44 
     45 #
     46 # single function for setting -S flag and doing error checking.
     47 # usage: set_S_flag <type>
     48 # where <type> is the source build type ("E", "D", ...).
     49 #
     50 function set_S_flag
     51 {
     52 	if is_source_build; then
     53 		print 'Can only build one source variant at a time.'
     54 		exit 1
     55 	fi
     56 	
     57 	case "$1" in
     58 		"E") flags.s.e=true ;;
     59 		"D") flags.s.d=true ;;
     60 		"H") flags.s.h=true ;;
     61 		"O") flags.s.o=true ;;
     62 		*)   usage ;;
     63 	esac
     64 }
     65 
     66 typeset -r USAGE=$'+
     67 [-?\n@(#)\$Id: bldenv (OS/Net) 2008-04-06 \$\n]
     68 [-author?OS/Net community <tools-discuss (at] opensolaris.org>]
     69 [+NAME?bldenv - spawn shell for interactive incremental OS-Net
     70     consolidation builds]
     71 [+DESCRIPTION?bldenv is a useful companion to the nightly(1) script for
     72     doing interactive and incremental builds in a workspace
     73     already built with nightly(1). bldenv spawns a shell set up
     74     with the same environment variables taken from an env_file,
     75     as prepared for use with nightly(1).]
     76 [+?In addition to running a shell for interactive use, bldenv
     77     can optionally run a single command in the given environment,
     78     in the vein of sh -c or su -c. This is useful for
     79     scripting, when an interactive shell would not be. If the
     80     command is composed of multiple shell words or contains
     81     other shell metacharacters, it must be quoted appropriately.]
     82 [+?bldenv is particularly useful for testing Makefile targets
     83     like clobber, install and _msg, which otherwise require digging
     84     through large build logs to figure out what is being
     85     done.]
     86 [+?bldenv is also useful if you run into build issues with the
     87     source product or when generating OpenSolaris deliverables.
     88     If a source product build is flagged, the environment is set
     89     up for building the indicated source product tree, which is
     90     assumed to have already been created. If the OpenSolaris
     91     deliverables flag (-O) is set in NIGHTLY_OPTIONS, the
     92     environment is set up for building just the open source.
     93     This includes using an alternate proto area, as well as
     94     using the closed binaries in $CODEMGR_WS/closed.skel (which
     95     is assumed to already exist).]
     96 [+?By default, bldenv will invoke the shell specified in
     97     $SHELL. If $SHELL is not set or is invalid, csh will be
     98     used.]
     99 [c?force the use of csh, regardless of the  value  of $SHELL.]
    100 [f?invoke csh with the -f (fast-start) option. This option is valid
    101     only if $SHELL is unset or if it points to csh.]
    102 [d?set up environment for doing DEBUG builds (default is non-DEBUG)]
    103 [t?set up environment to use the tools in usr/src/tools (this is the
    104     default, use +t to use the tools from /opt/onbld)]
    105 [S]:[option?Build a variant of the source product.
    106 The value of \aoption\a must be one of the following:]{
    107        [+E?build the exportable source variant of the source product.]
    108        [+D?build the domestic  source  (exportable + crypt) variant of
    109            the source product.]
    110        [+H?build hybrid source (binaries + deleted source).]
    111        [+O?simulate an OpenSolaris (open source only) build.]
    112 }
    113 
    114 <env_file> [command]
    115 
    116 [+EXAMPLES]{
    117     [+?Example 1: Interactive use]{
    118         [+?Use bldenv to spawn a shell to perform  a  DEBUG  build  and
    119             testing of the  Makefile  targets  clobber and install for
    120             usr/src/cmd/true.]
    121         [+\n% rlogin wopr-2 -l gk
    122 {root::wopr-2::49} bldenv -d /export0/jg/on10-se.env
    123 Build type   is  DEBUG
    124 RELEASE      is  5.10
    125 VERSION      is  wopr-2::on10-se::11/01/2001
    126 RELEASE_DATE is  May 2004
    127 The top-level `setup\' target is available to build headers
    128 and tools.
    129 Using /usr/bin/tcsh as shell.
    130 {root::wopr-2::49}
    131 {root::wopr-2::49} cd $SRC/cmd/true
    132 {root::wopr-2::50} make
    133 {root::wopr-2::51} make clobber
    134 /usr/bin/rm -f true true.po
    135 {root::wopr-2::52} make
    136 /usr/bin/rm -f true
    137 cat true.sh > true
    138 chmod +x true
    139 {root::wopr-2::53} make install
    140 install -s -m 0555 -u root -g bin -f /export0/jg/on10-se/proto/root_sparc/usr/bin true
    141 `install\' is up to date.]
    142     }
    143     [+?Example 2: Non-interactive use]{
    144         [+?Invoke bldenv to create SUNWonbld with a single command:]
    145         [+\nexample% bldenv onnv_06 \'cd $SRC/tools && make pkg\']
    146         }
    147 }
    148 [+SEE ALSO?\bnightly\b(1)]
    149 '
    150 
    151 # main
    152 builtin basename
    153 
    154 # boolean flags (true/false)
    155 typeset flags=(
    156 	typeset c=false
    157 	typeset f=false
    158 	typeset d=false
    159 	typeset O=false
    160 	typeset o=false
    161 	typeset t=true
    162 	typeset s=(
    163 		typeset e=false
    164 		typeset h=false
    165 		typeset d=false
    166 		typeset o=false
    167 	)
    168 )
    169 
    170 typeset progname="$(basename "${0}")"
    171 
    172 OPTIND=1
    173 SUFFIX="-nd"
    174 
    175 while getopts -a "${progname}" "${USAGE}" OPT ; do 
    176     case ${OPT} in
    177 	  c)	flags.c=true  ;;
    178 	  +c)	flags.c=false ;;
    179 	  f)	flags.f=true  ;;
    180 	  +f)	flags.f=false ;;
    181 	  d)	flags.d=true  SUFFIX=""    ;;
    182 	  +d)	flags.d=false SUFFIX="-nd" ;;
    183 	  t)	flags.t=true  ;;
    184 	  +t)	flags.t=false ;;
    185 	  S)	set_S_flag "$OPTARG" ;;
    186 	  \?)	usage ;;
    187     esac
    188 done
    189 shift $((OPTIND-1))
    190 
    191 # test that the path to the environment-setting file was given
    192 if (( $# < 1 )) ; then
    193 	usage
    194 fi
    195 
    196 # force locale to C
    197 export \
    198 	LC_COLLATE=C \
    199 	LC_CTYPE=C \
    200 	LC_MESSAGES=C \
    201 	LC_MONETARY=C \
    202 	LC_NUMERIC=C \
    203 	LC_TIME=C
    204 
    205 # clear environment variables we know to be bad for the build
    206 unset \
    207 	LD_OPTIONS \
    208         LD_LIBRARY_PATH \
    209         LD_AUDIT \
    210         LD_BIND_NOW \
    211         LD_BREADTH \
    212         LD_CONFIG \
    213 	LD_DEBUG \
    214         LD_FLAGS \
    215         LD_LIBRARY_PATH_64 \
    216         LD_NOVERSION \
    217         LD_ORIGIN \
    218 	LD_LOADFLTR \
    219         LD_NOAUXFLTR \
    220         LD_NOCONFIG \
    221         LD_NODIRCONFIG \
    222         LD_NOOBJALTER \
    223 	LD_PRELOAD \
    224         LD_PROFILE \
    225 	CONFIG \
    226 	GROUP \
    227 	OWNER \
    228 	REMOTE \
    229 	ENV \
    230 	ARCH \
    231 	CLASSPATH
    232 
    233 #
    234 # Setup environment variables
    235 #
    236 if [[ -f /etc/nightly.conf ]]; then
    237 	source /etc/nightly.conf
    238 fi
    239 
    240 if [[ -f "$1" ]]; then
    241 	if [[ "$1" == */* ]]; then
    242 		source "$1"
    243 	else
    244 		source "./$1"
    245 	fi
    246 else
    247 	if [[ -f "/opt/onbld/env/$1" ]]; then
    248 		source "/opt/onbld/env/$1"
    249 	else
    250 		printf \
    251 		    'Cannot find env file as either %s or /opt/onbld/env/%s\n' \
    252 		    "$1" "$1"
    253 		exit 1
    254 	fi
    255 fi
    256 shift
    257 
    258 # contents of stdenv.sh inserted after next line:
    259 # STDENV_START
    260 # STDENV_END
    261 
    262 #MACH=$(uname -p)
    263 
    264 # must match the getopts in nightly.sh
    265 OPTIND=1
    266 NIGHTLY_OPTIONS="-${NIGHTLY_OPTIONS#-}"
    267 while getopts '+0AaBCDdFfGIilMmNnOopRrS:tUuWwXxz' FLAG "$NIGHTLY_OPTIONS"
    268 do
    269 	case "$FLAG" in
    270 	  O)	flags.O=true  ;;
    271 	  +O)	flags.O=false ;;
    272 	  o)	flags.o=true  ;;
    273 	  +o)	flags.o=false ;;
    274 	  t)	flags.t=true  ;;
    275 	  +t)	flags.t=false ;;
    276 	  S)	set_S_flag "$OPTARG" ;;
    277 	  *)	;;
    278 	esac
    279 done
    280 
    281 POUND_SIGN="#"
    282 # have we set RELEASE_DATE in our env file?
    283 if [ -z "$RELEASE_DATE" ]; then
    284 	RELEASE_DATE=$(LC_ALL=C date +"%B %Y")
    285 fi
    286 BUILD_DATE=$(LC_ALL=C date +%Y-%b-%d)
    287 BASEWSDIR=$(basename $CODEMGR_WS)
    288 DEV_CM="\"@(#)SunOS Internal Development: $LOGNAME $BUILD_DATE [$BASEWSDIR]\""
    289 export DEV_CM RELEASE_DATE POUND_SIGN
    290 
    291 export INTERNAL_RELEASE_BUILD=
    292 
    293 print 'Build type   is  \c'
    294 if ${flags.d} ; then
    295 	print 'DEBUG'
    296 	unset RELEASE_BUILD
    297 	unset EXTRA_OPTIONS
    298 	unset EXTRA_CFLAGS
    299 else
    300 	# default is a non-DEBUG build
    301 	print 'non-DEBUG'
    302 	export RELEASE_BUILD=
    303 	unset EXTRA_OPTIONS
    304 	unset EXTRA_CFLAGS
    305 fi
    306 
    307 if ${flags.O} ; then
    308 	export MULTI_PROTO="yes"
    309 	if [[ "$CLOSED_IS_PRESENT" == "yes" ]]; then
    310 		print "CLOSED_IS_PRESENT is 'no' (because of '-O')"
    311 	fi
    312 	export CLOSED_IS_PRESENT=no
    313 	export ON_CLOSED_BINS="$CODEMGR_WS/closed.skel"
    314 fi
    315 
    316 # update build-type variables
    317 CPIODIR="${CPIODIR}${SUFFIX}"
    318 PKGARCHIVE="${PKGARCHIVE}${SUFFIX}"
    319 
    320 # Append source version
    321 if "${flags.s.e}" ; then
    322         VERSION+=":EXPORT"
    323 	SRC="${EXPORT_SRC}/usr/src"
    324 fi
    325  
    326 if "${flags.s.d}" ; then
    327         VERSION+=":DOMESTIC"
    328 	SRC="${EXPORT_SRC}/usr/src"
    329 fi
    330 
    331 if "${flags.s.h}" ; then
    332         VERSION+=":HYBRID"
    333 	SRC="${EXPORT_SRC}/usr/src"
    334 fi
    335  
    336 if "${flags.s.o}" ; then
    337         VERSION+=":OPEN_ONLY"
    338 	SRC="${OPEN_SRCDIR}/usr/src"
    339 fi
    340  
    341 #
    342 # Keep track of this now, before we manipulate $PATH
    343 #
    344 WHICH_SCM=$(dirname $(whence $0))/which_scm
    345 if [[ ! -x $WHICH_SCM ]]; then
    346 	WHICH_SCM=which_scm
    347 fi
    348 $WHICH_SCM | read SCM_TYPE junk
    349 
    350 
    351 # 	Set PATH for a build
    352 PATH="/opt/onbld/bin:/opt/onbld/bin/${MACH}:/opt/SUNWspro/bin:/usr/ccs/bin:/usr/bin:/usr/sbin:/usr/ucb:/usr/etc:/usr/openwin/bin:/usr/sfw/bin:/opt/sfw/bin:."
    353 if [[ "${SUNWSPRO}" != "" ]]; then 
    354 	export PATH="${SUNWSPRO}/bin:$PATH" 
    355 fi 
    356 
    357 if [[ -z "$CLOSED_IS_PRESENT" ]]; then
    358 	if [[ -d $SRC/../closed ]]; then
    359 		export CLOSED_IS_PRESENT="yes"
    360 	else
    361 		export CLOSED_IS_PRESENT="no"
    362 	fi
    363 fi
    364 
    365 TOOLS="${SRC}/tools"
    366 TOOLS_PROTO="${TOOLS}/proto"
    367 
    368 if "${flags.t}" ; then
    369 	export ONBLD_TOOLS="${ONBLD_TOOLS:=${TOOLS_PROTO}/opt/onbld}"
    370 
    371 	export STABS="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/stabs"
    372 	export CTFSTABS="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfstabs"
    373 	export GENOFFSETS="${TOOLS_PROTO}/opt/onbld/bin/genoffsets"
    374 
    375 	export CTFCONVERT="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfconvert"
    376 	export CTFMERGE="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfmerge"
    377 
    378 	export CTFCVTPTBL="${TOOLS_PROTO}/opt/onbld/bin/ctfcvtptbl"
    379 	export CTFFINDMOD="${TOOLS_PROTO}/opt/onbld/bin/ctffindmod"
    380 
    381 	PATH="${TOOLS_PROTO}/opt/onbld/bin/${MACH}:${PATH}"
    382 	PATH="${TOOLS_PROTO}/opt/onbld/bin:${PATH}"
    383 	export PATH
    384 fi
    385 
    386 export DMAKE_MODE=${DMAKE_MODE:-parallel}
    387 
    388 if "${flags.o}" ; then
    389 	export CH=
    390 else
    391 	unset CH
    392 fi
    393 DEF_STRIPFLAG="-s"
    394 
    395 TMPDIR="/tmp"
    396 
    397 # "o_FLAG" is used by "nightly.sh" and "makebfu.sh" (it may be useful to
    398 # rename this variable using a more descriptive name later)
    399 export o_FLAG="$(${flags.o} && print 'y' || print 'n')"
    400 
    401 export \
    402 	PATH TMPDIR \
    403 	POUND_SIGN \
    404 	DEF_STRIPFLAG \
    405 	RELEASE_DATE
    406 unset \
    407 	CFLAGS \
    408 	LD_LIBRARY_PATH
    409 
    410 # a la ws
    411 ENVLDLIBS1=
    412 ENVLDLIBS2=
    413 ENVLDLIBS3=
    414 ENVCPPFLAGS1=
    415 ENVCPPFLAGS2=
    416 ENVCPPFLAGS3=
    417 ENVCPPFLAGS4=
    418 PARENT_ROOT=
    419 
    420 "${flags.O}" && export ROOT="$ROOT-open"
    421 
    422 if [[ "$MULTI_PROTO" != "yes" && "$MULTI_PROTO" != "no" ]]; then
    423 	printf \
    424 	    'WARNING: invalid value for MULTI_PROTO (%s);setting to "no".\n' \
    425 	    "$MULTI_PROTO"
    426 	export MULTI_PROTO="no"
    427 fi
    428 
    429 [[ "$MULTI_PROTO" == "yes" ]] && export ROOT="${ROOT}${SUFFIX}"
    430 
    431 ENVLDLIBS1="-L$ROOT/lib -L$ROOT/usr/lib"
    432 ENVCPPFLAGS1="-I$ROOT/usr/include"
    433 MAKEFLAGS=e
    434 
    435 export \
    436         ENVLDLIBS1 \
    437         ENVLDLIBS2 \
    438         ENVLDLIBS3 \
    439 	ENVCPPFLAGS1 \
    440         ENVCPPFLAGS2 \
    441         ENVCPPFLAGS3 \
    442 	ENVCPPFLAGS4 \
    443         MAKEFLAGS \
    444         PARENT_ROOT \
    445 	SCM_TYPE
    446 
    447 printf 'RELEASE      is %s\n'   "$RELEASE"
    448 printf 'VERSION      is %s\n'   "$VERSION"
    449 printf 'RELEASE_DATE is %s\n\n' "$RELEASE_DATE"
    450 
    451 if [[ -f "$SRC/Makefile" ]] && egrep -s '^setup:' "$SRC/Makefile" ; then
    452 	print "The top-level 'setup' target is available \c"
    453 	print "to build headers and tools."
    454 	print ""
    455 
    456 elif "${flags.t}" ; then
    457 	printf \
    458 	    'The tools can be (re)built with the install target in %s.\n\n' \
    459 	    "${TOOLS}"
    460 fi
    461 
    462 #
    463 # place ourselves in a new task, respecting BUILD_PROJECT if set.
    464 #
    465 /usr/bin/newtask -c $$ ${BUILD_PROJECT:+-p$BUILD_PROJECT}
    466 
    467 if [[ "${flags.c}" == "false" && -x "$SHELL" && \
    468     "$(basename "${SHELL}")" != "csh" ]]; then
    469 	# $SHELL is set, and it's not csh.
    470 
    471 	if "${flags.f}" ; then
    472 		print 'WARNING: -f is ignored when $SHELL is not csh'
    473 	fi
    474 
    475 	printf 'Using %s as shell.\n' "$SHELL"
    476 	exec "$SHELL" ${@:+-c "$@"}
    477 
    478 elif "${flags.f}" ; then
    479 	print 'Using csh -f as shell.'
    480 	exec csh -f ${@:+-c "$@"}
    481 
    482 else
    483 	print 'Using csh as shell.'
    484 	exec csh ${@:+-c "$@"}
    485 fi
    486 
    487 # not reached
    488