xref: /titanic_41/usr/src/tools/scripts/bldenv.sh (revision edafac1f8203a7de1490688213c118b03215c89d)
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
32function usage
33{
34    OPTIND=0
35    getopts -a "${progname}" "${USAGE}" OPT '-?'
36    exit 2
37}
38
39function 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#
50function 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
66typeset -r USAGE=$'+
67[-?\n@(#)\$Id: bldenv (OS/Net) 2008-04-06 \$\n]
68[-author?OS/Net community <tools-discuss@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.
106The 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
123Build type   is  DEBUG
124RELEASE      is  5.10
125VERSION      is  wopr-2::on10-se::11/01/2001
126RELEASE_DATE is  May 2004
127The top-level `setup\' target is available to build headers
128and tools.
129Using /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
137cat true.sh > true
138chmod +x true
139{root::wopr-2::53} make install
140install -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
152builtin basename
153
154# boolean flags (true/false)
155typeset 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
170typeset progname="$(basename "${0}")"
171
172OPTIND=1
173SUFFIX="-nd"
174
175while 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
188done
189shift $((OPTIND-1))
190
191# test that the path to the environment-setting file was given
192if (( $# < 1 )) ; then
193	usage
194fi
195
196# force locale to C
197export \
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
206unset \
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#
236if [[ -f /etc/nightly.conf ]]; then
237	source /etc/nightly.conf
238fi
239
240if [[ -f "$1" ]]; then
241	if [[ "$1" == */* ]]; then
242		source "$1"
243	else
244		source "./$1"
245	fi
246else
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
255fi
256shift
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
265OPTIND=1
266NIGHTLY_OPTIONS="-${NIGHTLY_OPTIONS#-}"
267while getopts '+0AaBCDdFfGIilMmNnOopRrS:tUuWwXxz' FLAG "$NIGHTLY_OPTIONS"
268do
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
279done
280
281POUND_SIGN="#"
282# have we set RELEASE_DATE in our env file?
283if [ -z "$RELEASE_DATE" ]; then
284	RELEASE_DATE=$(LC_ALL=C date +"%B %Y")
285fi
286BUILD_DATE=$(LC_ALL=C date +%Y-%b-%d)
287BASEWSDIR=$(basename $CODEMGR_WS)
288DEV_CM="\"@(#)SunOS Internal Development: $LOGNAME $BUILD_DATE [$BASEWSDIR]\""
289export DEV_CM RELEASE_DATE POUND_SIGN
290
291export INTERNAL_RELEASE_BUILD=
292
293print 'Build type   is  \c'
294if ${flags.d} ; then
295	print 'DEBUG'
296	unset RELEASE_BUILD
297	unset EXTRA_OPTIONS
298	unset EXTRA_CFLAGS
299else
300	# default is a non-DEBUG build
301	print 'non-DEBUG'
302	export RELEASE_BUILD=
303	unset EXTRA_OPTIONS
304	unset EXTRA_CFLAGS
305fi
306
307if ${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"
314fi
315
316# update build-type variables
317CPIODIR="${CPIODIR}${SUFFIX}"
318PKGARCHIVE="${PKGARCHIVE}${SUFFIX}"
319
320# Append source version
321if "${flags.s.e}" ; then
322        VERSION+=":EXPORT"
323	SRC="${EXPORT_SRC}/usr/src"
324fi
325
326if "${flags.s.d}" ; then
327        VERSION+=":DOMESTIC"
328	SRC="${EXPORT_SRC}/usr/src"
329fi
330
331if "${flags.s.h}" ; then
332        VERSION+=":HYBRID"
333	SRC="${EXPORT_SRC}/usr/src"
334fi
335
336if "${flags.s.o}" ; then
337        VERSION+=":OPEN_ONLY"
338	SRC="${OPEN_SRCDIR}/usr/src"
339fi
340
341#
342# Keep track of this now, before we manipulate $PATH
343#
344WHICH_SCM=$(dirname $(whence $0))/which_scm
345if [[ ! -x $WHICH_SCM ]]; then
346	WHICH_SCM=which_scm
347fi
348$WHICH_SCM | read SCM_TYPE junk
349
350
351# 	Set PATH for a build
352PATH="/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:."
353if [[ "${SUNWSPRO}" != "" ]]; then
354	export PATH="${SUNWSPRO}/bin:$PATH"
355fi
356
357if [[ -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
363fi
364
365TOOLS="${SRC}/tools"
366TOOLS_PROTO="${TOOLS}/proto"
367
368if "${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
384fi
385
386export DMAKE_MODE=${DMAKE_MODE:-parallel}
387
388if "${flags.o}" ; then
389	export CH=
390else
391	unset CH
392fi
393DEF_STRIPFLAG="-s"
394
395TMPDIR="/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)
399export o_FLAG="$(${flags.o} && print 'y' || print 'n')"
400
401export \
402	PATH TMPDIR \
403	POUND_SIGN \
404	DEF_STRIPFLAG \
405	RELEASE_DATE
406unset \
407	CFLAGS \
408	LD_LIBRARY_PATH
409
410# a la ws
411ENVLDLIBS1=
412ENVLDLIBS2=
413ENVLDLIBS3=
414ENVCPPFLAGS1=
415ENVCPPFLAGS2=
416ENVCPPFLAGS3=
417ENVCPPFLAGS4=
418PARENT_ROOT=
419
420"${flags.O}" && export ROOT="$ROOT-open"
421
422if [[ "$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"
427fi
428
429[[ "$MULTI_PROTO" == "yes" ]] && export ROOT="${ROOT}${SUFFIX}"
430
431ENVLDLIBS1="-L$ROOT/lib -L$ROOT/usr/lib"
432ENVCPPFLAGS1="-I$ROOT/usr/include"
433MAKEFLAGS=e
434
435export \
436        ENVLDLIBS1 \
437        ENVLDLIBS2 \
438        ENVLDLIBS3 \
439	ENVCPPFLAGS1 \
440        ENVCPPFLAGS2 \
441        ENVCPPFLAGS3 \
442	ENVCPPFLAGS4 \
443        MAKEFLAGS \
444        PARENT_ROOT \
445	SCM_TYPE
446
447printf 'RELEASE      is %s\n'   "$RELEASE"
448printf 'VERSION      is %s\n'   "$VERSION"
449printf 'RELEASE_DATE is %s\n\n' "$RELEASE_DATE"
450
451if [[ -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
456elif "${flags.t}" ; then
457	printf \
458	    'The tools can be (re)built with the install target in %s.\n\n' \
459	    "${TOOLS}"
460fi
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
467if [[ "${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
478elif "${flags.f}" ; then
479	print 'Using csh -f as shell.'
480	exec csh -f ${@:+-c "$@"}
481
482else
483	print 'Using csh as shell.'
484	exec csh ${@:+-c "$@"}
485fi
486
487# not reached
488