xref: /titanic_50/usr/src/tools/scripts/bldenv.sh (revision dc0f59e5324b0cb0a8b1062e2d385e5c016661b2)
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
309CPIODIR="${CPIODIR}${SUFFIX}"
310PKGARCHIVE="${PKGARCHIVE}${SUFFIX}"
311
312# Append source version
313if "${flags.s.e}" ; then
314        VERSION+=":EXPORT"
315	SRC="${EXPORT_SRC}/usr/src"
316fi
317
318if "${flags.s.d}" ; then
319        VERSION+=":DOMESTIC"
320	SRC="${EXPORT_SRC}/usr/src"
321fi
322
323if "${flags.s.h}" ; then
324        VERSION+=":HYBRID"
325	SRC="${EXPORT_SRC}/usr/src"
326fi
327
328if "${flags.s.o}" ; then
329        VERSION+=":OPEN_ONLY"
330	SRC="${OPEN_SRCDIR}/usr/src"
331fi
332
333#
334# Keep track of this now, before we manipulate $PATH
335#
336WHICH_SCM=$(dirname $(whence $0))/which_scm
337if [[ ! -x $WHICH_SCM ]]; then
338	WHICH_SCM=which_scm
339fi
340$WHICH_SCM | read SCM_TYPE junk
341
342
343# 	Set PATH for a build
344PATH="/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:."
345if [[ "${SUNWSPRO}" != "" ]]; then
346	export PATH="${SUNWSPRO}/bin:$PATH"
347fi
348
349if [[ -z "$CLOSED_IS_PRESENT" ]]; then
350	if [[ -d $SRC/../closed ]]; then
351		export CLOSED_IS_PRESENT="yes"
352	else
353		export CLOSED_IS_PRESENT="no"
354	fi
355fi
356
357TOOLS="${SRC}/tools"
358TOOLS_PROTO="${TOOLS}/proto/root_${MACH}-nd" ; export TOOLS_PROTO
359
360if "${flags.t}" ; then
361	export ONBLD_TOOLS="${ONBLD_TOOLS:=${TOOLS_PROTO}/opt/onbld}"
362
363	export STABS="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/stabs"
364	export CTFSTABS="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfstabs"
365	export GENOFFSETS="${TOOLS_PROTO}/opt/onbld/bin/genoffsets"
366
367	export CTFCONVERT="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfconvert"
368	export CTFMERGE="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfmerge"
369
370	export CTFCVTPTBL="${TOOLS_PROTO}/opt/onbld/bin/ctfcvtptbl"
371	export CTFFINDMOD="${TOOLS_PROTO}/opt/onbld/bin/ctffindmod"
372
373	PATH="${TOOLS_PROTO}/opt/onbld/bin/${MACH}:${PATH}"
374	PATH="${TOOLS_PROTO}/opt/onbld/bin:${PATH}"
375	export PATH
376fi
377
378export DMAKE_MODE=${DMAKE_MODE:-parallel}
379
380if "${flags.o}" ; then
381	export CH=
382else
383	unset CH
384fi
385DEF_STRIPFLAG="-s"
386
387TMPDIR="/tmp"
388
389# "o_FLAG" is used by "nightly.sh" and "makebfu.sh" (it may be useful to
390# rename this variable using a more descriptive name later)
391export o_FLAG="$(${flags.o} && print 'y' || print 'n')"
392
393export \
394	PATH TMPDIR \
395	POUND_SIGN \
396	DEF_STRIPFLAG \
397	RELEASE_DATE
398unset \
399	CFLAGS \
400	LD_LIBRARY_PATH
401
402# a la ws
403ENVLDLIBS1=
404ENVLDLIBS2=
405ENVLDLIBS3=
406ENVCPPFLAGS1=
407ENVCPPFLAGS2=
408ENVCPPFLAGS3=
409ENVCPPFLAGS4=
410PARENT_ROOT=
411PARENT_TOOLS_ROOT=
412
413if [[ "$MULTI_PROTO" != "yes" && "$MULTI_PROTO" != "no" ]]; then
414	printf \
415	    'WARNING: invalid value for MULTI_PROTO (%s); setting to "no".\n' \
416	    "$MULTI_PROTO"
417	export MULTI_PROTO="no"
418fi
419
420[[ "$MULTI_PROTO" == "yes" ]] && export ROOT="${ROOT}${SUFFIX}"
421
422export TONICBUILD="#"
423
424if "${flags.O}" ; then
425	if [[ "$CLOSED_IS_PRESENT" != "yes" ]]; then
426		print "OpenSolaris closed binary generation requires "
427		print "closed tree"
428		exit 1
429	fi
430	print "Generating OpenSolaris deliverables"
431	# We only need CLOSEDROOT in the env for convenience. Makefile.master
432	# figures out what it needs when it matters.
433	export CLOSEDROOT="${ROOT}-closed"
434	export TONICBUILD=""
435fi
436
437ENVLDLIBS1="-L$ROOT/lib -L$ROOT/usr/lib"
438ENVCPPFLAGS1="-I$ROOT/usr/include"
439MAKEFLAGS=e
440
441export \
442        ENVLDLIBS1 \
443        ENVLDLIBS2 \
444        ENVLDLIBS3 \
445	ENVCPPFLAGS1 \
446        ENVCPPFLAGS2 \
447        ENVCPPFLAGS3 \
448	ENVCPPFLAGS4 \
449        MAKEFLAGS \
450        PARENT_ROOT \
451        PARENT_TOOLS_ROOT \
452	SCM_TYPE
453
454printf 'RELEASE      is %s\n'   "$RELEASE"
455printf 'VERSION      is %s\n'   "$VERSION"
456printf 'RELEASE_DATE is %s\n\n' "$RELEASE_DATE"
457
458if [[ -f "$SRC/Makefile" ]] && egrep -s '^setup:' "$SRC/Makefile" ; then
459	print "The top-level 'setup' target is available \c"
460	print "to build headers and tools."
461	print ""
462
463elif "${flags.t}" ; then
464	printf \
465	    'The tools can be (re)built with the install target in %s.\n\n' \
466	    "${TOOLS}"
467fi
468
469#
470# place ourselves in a new task, respecting BUILD_PROJECT if set.
471#
472/usr/bin/newtask -c $$ ${BUILD_PROJECT:+-p$BUILD_PROJECT}
473
474if [[ "${flags.c}" == "false" && -x "$SHELL" && \
475    "$(basename "${SHELL}")" != "csh" ]]; then
476	# $SHELL is set, and it's not csh.
477
478	if "${flags.f}" ; then
479		print 'WARNING: -f is ignored when $SHELL is not csh'
480	fi
481
482	printf 'Using %s as shell.\n' "$SHELL"
483	exec "$SHELL" ${@:+-c "$@"}
484
485elif "${flags.f}" ; then
486	print 'Using csh -f as shell.'
487	exec csh -f ${@:+-c "$@"}
488
489else
490	print 'Using csh as shell.'
491	exec csh ${@:+-c "$@"}
492fi
493
494# not reached
495