xref: /titanic_51/usr/src/tools/scripts/bldenv.sh (revision e18306b13ed357bd545696aa96b53617b64db4a3)
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# Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
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 fatal_error
33{
34	print -u2 "${progname}: $*"
35	exit 1
36}
37
38function usage
39{
40    OPTIND=0
41    getopts -a "${progname}" "${USAGE}" OPT '-?'
42    exit 2
43}
44
45function is_source_build
46{
47	"${flags.s.e}" || "${flags.s.d}" || "${flags.s.h}" || "${flags.s.o}"
48	return $?
49}
50
51#
52# single function for setting -S flag and doing error checking.
53# usage: set_S_flag <type>
54# where <type> is the source build type ("E", "D", ...).
55#
56function set_S_flag
57{
58	if is_source_build; then
59		print 'Can only build one source variant at a time.'
60		exit 1
61	fi
62
63	case "$1" in
64		"E") flags.s.e=true ;;
65		"D") flags.s.d=true ;;
66		"H") flags.s.h=true ;;
67		"O") flags.s.o=true ;;
68		*)   usage ;;
69	esac
70}
71
72typeset -r USAGE=$'+
73[-?\n@(#)\$Id: bldenv (OS/Net) 2008-04-06 \$\n]
74[-author?OS/Net community <tools-discuss@opensolaris.org>]
75[+NAME?bldenv - spawn shell for interactive incremental OS-Net
76    consolidation builds]
77[+DESCRIPTION?bldenv is a useful companion to the nightly(1) script for
78    doing interactive and incremental builds in a workspace
79    already built with nightly(1). bldenv spawns a shell set up
80    with the same environment variables taken from an env_file,
81    as prepared for use with nightly(1).]
82[+?In addition to running a shell for interactive use, bldenv
83    can optionally run a single command in the given environment,
84    in the vein of sh -c or su -c. This is useful for
85    scripting, when an interactive shell would not be. If the
86    command is composed of multiple shell words or contains
87    other shell metacharacters, it must be quoted appropriately.]
88[+?bldenv is particularly useful for testing Makefile targets
89    like clobber, install and _msg, which otherwise require digging
90    through large build logs to figure out what is being
91    done.]
92[+?bldenv is also useful if you run into build issues with the
93    source product or when generating OpenSolaris deliverables.
94    If a source product build is flagged, the environment is set
95    up for building the indicated source product tree, which is
96    assumed to have already been created. If the OpenSolaris
97    deliverables flag (-O) is set in NIGHTLY_OPTIONS, the
98    environment is set up for building just the open source.
99    This includes using an alternate proto area, as well as
100    using the closed binaries in $CODEMGR_WS/closed.skel (which
101    is assumed to already exist).]
102[+?By default, bldenv will invoke the shell specified in
103    $SHELL. If $SHELL is not set or is invalid, csh will be
104    used.]
105[c?force the use of csh, regardless of the  value  of $SHELL.]
106[f?invoke csh with the -f (fast-start) option. This option is valid
107    only if $SHELL is unset or if it points to csh.]
108[d?set up environment for doing DEBUG builds (default is non-DEBUG)]
109[t?set up environment to use the tools in usr/src/tools (this is the
110    default, use +t to use the tools from /opt/onbld)]
111[S]:[option?Build a variant of the source product.
112The value of \aoption\a must be one of the following:]{
113       [+E?build the exportable source variant of the source product.]
114       [+D?build the domestic  source  (exportable + crypt) variant of
115           the source product.]
116       [+H?build hybrid source (binaries + deleted source).]
117       [+O?simulate an OpenSolaris (open source only) build.]
118}
119
120<env_file> [command]
121
122[+EXAMPLES]{
123    [+?Example 1: Interactive use]{
124        [+?Use bldenv to spawn a shell to perform  a  DEBUG  build  and
125            testing of the  Makefile  targets  clobber and install for
126            usr/src/cmd/true.]
127        [+\n% rlogin wopr-2 -l gk
128{root::wopr-2::49} bldenv -d /export0/jg/on10-se.env
129Build type   is  DEBUG
130RELEASE      is  5.10
131VERSION      is  wopr-2::on10-se::11/01/2001
132RELEASE_DATE is  May 2004
133The top-level `setup\' target is available to build headers
134and tools.
135Using /usr/bin/tcsh as shell.
136{root::wopr-2::49}
137{root::wopr-2::49} cd $SRC/cmd/true
138{root::wopr-2::50} make
139{root::wopr-2::51} make clobber
140/usr/bin/rm -f true true.po
141{root::wopr-2::52} make
142/usr/bin/rm -f true
143cat true.sh > true
144chmod +x true
145{root::wopr-2::53} make install
146install -s -m 0555 -u root -g bin -f /export0/jg/on10-se/proto/root_sparc/usr/bin true
147`install\' is up to date.]
148    }
149    [+?Example 2: Non-interactive use]{
150        [+?Invoke bldenv to create SUNWonbld with a single command:]
151        [+\nexample% bldenv onnv_06 \'cd $SRC/tools && make pkg\']
152        }
153}
154[+SEE ALSO?\bnightly\b(1)]
155'
156
157# main
158builtin basename
159
160# boolean flags (true/false)
161typeset flags=(
162	typeset c=false
163	typeset f=false
164	typeset d=false
165	typeset O=false
166	typeset o=false
167	typeset t=true
168	typeset s=(
169		typeset e=false
170		typeset h=false
171		typeset d=false
172		typeset o=false
173	)
174)
175
176typeset progname="$(basename -- "${0}")"
177
178OPTIND=1
179SUFFIX="-nd"
180
181while getopts -a "${progname}" "${USAGE}" OPT ; do
182    case ${OPT} in
183	  c)	flags.c=true  ;;
184	  +c)	flags.c=false ;;
185	  f)	flags.f=true  ;;
186	  +f)	flags.f=false ;;
187	  d)	flags.d=true  SUFFIX=""    ;;
188	  +d)	flags.d=false SUFFIX="-nd" ;;
189	  t)	flags.t=true  ;;
190	  +t)	flags.t=false ;;
191	  S)	set_S_flag "$OPTARG" ;;
192	  \?)	usage ;;
193    esac
194done
195shift $((OPTIND-1))
196
197# test that the path to the environment-setting file was given
198if (( $# < 1 )) ; then
199	usage
200fi
201
202# force locale to C
203export \
204	LC_COLLATE=C \
205	LC_CTYPE=C \
206	LC_MESSAGES=C \
207	LC_MONETARY=C \
208	LC_NUMERIC=C \
209	LC_TIME=C
210
211# clear environment variables we know to be bad for the build
212unset \
213	LD_OPTIONS \
214        LD_LIBRARY_PATH \
215        LD_AUDIT \
216        LD_BIND_NOW \
217        LD_BREADTH \
218        LD_CONFIG \
219	LD_DEBUG \
220        LD_FLAGS \
221        LD_LIBRARY_PATH_64 \
222        LD_NOVERSION \
223        LD_ORIGIN \
224	LD_LOADFLTR \
225        LD_NOAUXFLTR \
226        LD_NOCONFIG \
227        LD_NODIRCONFIG \
228        LD_NOOBJALTER \
229	LD_PRELOAD \
230        LD_PROFILE \
231	CONFIG \
232	GROUP \
233	OWNER \
234	REMOTE \
235	ENV \
236	ARCH \
237	CLASSPATH
238
239#
240# Setup environment variables
241#
242if [[ -f /etc/nightly.conf ]]; then
243	source /etc/nightly.conf
244fi
245
246if [[ -f "$1" ]]; then
247	if [[ "$1" == */* ]]; then
248		source "$1"
249	else
250		source "./$1"
251	fi
252else
253	if [[ -f "/opt/onbld/env/$1" ]]; then
254		source "/opt/onbld/env/$1"
255	else
256		printf \
257		    'Cannot find env file as either %s or /opt/onbld/env/%s\n' \
258		    "$1" "$1"
259		exit 1
260	fi
261fi
262shift
263
264# contents of stdenv.sh inserted after next line:
265# STDENV_START
266# STDENV_END
267
268# Check if we have sufficient data to continue...
269[[ -v CODEMGR_WS ]] || fatal_error "Error: Variable CODEMGR_WS not set."
270[[ -d "${CODEMGR_WS}" ]] || fatal_error "Error: ${CODEMGR_WS} is not a directory."
271[[ -f "${CODEMGR_WS}/usr/src/Makefile" ]] || fatal_error "Error: ${CODEMGR_WS}/usr/src/Makefile not found."
272
273# must match the getopts in nightly.sh
274OPTIND=1
275NIGHTLY_OPTIONS="-${NIGHTLY_OPTIONS#-}"
276while getopts '+0AaBCDdFfGIilMmNnOopRrS:tUuWwXxz' FLAG "$NIGHTLY_OPTIONS"
277do
278	case "$FLAG" in
279	  O)    flags.O=true  ;;
280	  +O)   flags.O=false ;;
281	  o)	flags.o=true  ;;
282	  +o)	flags.o=false ;;
283	  t)	flags.t=true  ;;
284	  +t)	flags.t=false ;;
285	  S)	set_S_flag "$OPTARG" ;;
286	  *)	;;
287	esac
288done
289
290POUND_SIGN="#"
291# have we set RELEASE_DATE in our env file?
292if [ -z "$RELEASE_DATE" ]; then
293	RELEASE_DATE=$(LC_ALL=C date +"%B %Y")
294fi
295BUILD_DATE=$(LC_ALL=C date +%Y-%b-%d)
296BASEWSDIR=$(basename -- "${CODEMGR_WS}")
297DEV_CM="\"@(#)SunOS Internal Development: $LOGNAME $BUILD_DATE [$BASEWSDIR]\""
298export DEV_CM RELEASE_DATE POUND_SIGN
299
300export INTERNAL_RELEASE_BUILD=
301
302print 'Build type   is  \c'
303if ${flags.d} ; then
304	print 'DEBUG'
305	unset RELEASE_BUILD
306	unset EXTRA_OPTIONS
307	unset EXTRA_CFLAGS
308else
309	# default is a non-DEBUG build
310	print 'non-DEBUG'
311	export RELEASE_BUILD=
312	unset EXTRA_OPTIONS
313	unset EXTRA_CFLAGS
314fi
315
316[[ "${flags.O}" == "true" ]] && export MULTI_PROTO="yes"
317
318# update build-type variables
319PKGARCHIVE="${PKGARCHIVE}${SUFFIX}"
320
321# Append source version
322if "${flags.s.e}" ; then
323        VERSION+=":EXPORT"
324	SRC="${EXPORT_SRC}/usr/src"
325fi
326
327if "${flags.s.d}" ; then
328        VERSION+=":DOMESTIC"
329	SRC="${EXPORT_SRC}/usr/src"
330fi
331
332if "${flags.s.h}" ; then
333        VERSION+=":HYBRID"
334	SRC="${EXPORT_SRC}/usr/src"
335fi
336
337if "${flags.s.o}" ; then
338        VERSION+=":OPEN_ONLY"
339	SRC="${OPEN_SRCDIR}/usr/src"
340fi
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
452printf 'RELEASE      is %s\n'   "$RELEASE"
453printf 'VERSION      is %s\n'   "$VERSION"
454printf 'RELEASE_DATE is %s\n\n' "$RELEASE_DATE"
455
456if [[ -f "$SRC/Makefile" ]] && egrep -s '^setup:' "$SRC/Makefile" ; then
457	print "The top-level 'setup' target is available \c"
458	print "to build headers and tools."
459	print ""
460
461elif "${flags.t}" ; then
462	printf \
463	    'The tools can be (re)built with the install target in %s.\n\n' \
464	    "${TOOLS}"
465fi
466
467#
468# place ourselves in a new task, respecting BUILD_PROJECT if set.
469#
470/usr/bin/newtask -c $$ ${BUILD_PROJECT:+-p$BUILD_PROJECT}
471
472if [[ "${flags.c}" == "false" && -x "$SHELL" && \
473    "$(basename -- "${SHELL}")" != "csh" ]]; then
474	# $SHELL is set, and it's not csh.
475
476	if "${flags.f}" ; then
477		print 'WARNING: -f is ignored when $SHELL is not csh'
478	fi
479
480	printf 'Using %s as shell.\n' "$SHELL"
481	exec "$SHELL" ${@:+-c "$@"}
482
483elif "${flags.f}" ; then
484	print 'Using csh -f as shell.'
485	exec csh -f ${@:+-c "$@"}
486
487else
488	print 'Using csh as shell.'
489	exec csh ${@:+-c "$@"}
490fi
491
492# not reached
493