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