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