xref: /titanic_51/usr/src/tools/scripts/bldenv.sh (revision 35bff3c25e305b98f6a6e5317efb1f96e76bba0d)
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 '+AaBCDdFfGIilMmNnOopRrS: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
281export INTERNAL_RELEASE_BUILD=
282
283print 'Build type   is  \c'
284if ${flags.d} ; then
285	print 'DEBUG'
286	unset RELEASE_BUILD
287	unset EXTRA_OPTIONS
288	unset EXTRA_CFLAGS
289else
290	# default is a non-DEBUG build
291	print 'non-DEBUG'
292	export RELEASE_BUILD=
293	unset EXTRA_OPTIONS
294	unset EXTRA_CFLAGS
295fi
296
297if ${flags.O} ; then
298	export MULTI_PROTO="yes"
299	if [[ "$CLOSED_IS_PRESENT" == "yes" ]]; then
300		print "CLOSED_IS_PRESENT is 'no' (because of '-O')"
301	fi
302	export CLOSED_IS_PRESENT=no
303	export ON_CLOSED_BINS="$CODEMGR_WS/closed.skel"
304fi
305
306# update build-type variables
307CPIODIR="${CPIODIR}${SUFFIX}"
308PKGARCHIVE="${PKGARCHIVE}${SUFFIX}"
309
310# Append source version
311if "${flags.s.e}" ; then
312        VERSION+=":EXPORT"
313	SRC="${EXPORT_SRC}/usr/src"
314fi
315
316if "${flags.s.d}" ; then
317        VERSION+=":DOMESTIC"
318	SRC="${EXPORT_SRC}/usr/src"
319fi
320
321if "${flags.s.h}" ; then
322        VERSION+=":HYBRID"
323	SRC="${EXPORT_SRC}/usr/src"
324fi
325
326if "${flags.s.o}" ; then
327        VERSION+=":OPEN_ONLY"
328	SRC="${OPEN_SRCDIR}/usr/src"
329fi
330
331#
332# Keep track of this now, before we manipulate $PATH
333#
334WHICH_SCM=$(dirname $(whence $0))/which_scm
335if [[ ! -x $WHICH_SCM ]]; then
336	WHICH_SCM=which_scm
337fi
338$WHICH_SCM | read SCM_TYPE junk
339
340
341# 	Set PATH for a build
342PATH="/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:."
343if [[ "${SUNWSPRO}" != "" ]]; then
344	export PATH="${SUNWSPRO}/bin:$PATH"
345fi
346
347if [[ -z "$CLOSED_IS_PRESENT" ]]; then
348	if [[ -d $SRC/../closed ]]; then
349		export CLOSED_IS_PRESENT="yes"
350	else
351		export CLOSED_IS_PRESENT="no"
352	fi
353fi
354
355TOOLS="${SRC}/tools"
356TOOLS_PROTO="${TOOLS}/proto"
357
358if "${flags.t}" ; then
359	export ONBLD_TOOLS="${ONBLD_TOOLS:=${TOOLS_PROTO}/opt/onbld}"
360
361	export STABS="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/stabs"
362	export CTFSTABS="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfstabs"
363	export GENOFFSETS="${TOOLS_PROTO}/opt/onbld/bin/genoffsets"
364
365	export CTFCONVERT="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfconvert"
366	export CTFMERGE="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfmerge"
367
368	export CTFCVTPTBL="${TOOLS_PROTO}/opt/onbld/bin/ctfcvtptbl"
369	export CTFFINDMOD="${TOOLS_PROTO}/opt/onbld/bin/ctffindmod"
370
371	PATH="${TOOLS_PROTO}/opt/onbld/bin/${MACH}:${PATH}"
372	PATH="${TOOLS_PROTO}/opt/onbld/bin:${PATH}"
373	export PATH
374fi
375
376export DMAKE_MODE=${DMAKE_MODE:-parallel}
377
378if "${flags.o}" ; then
379	export CH=
380else
381	unset CH
382fi
383POUND_SIGN="#"
384DEF_STRIPFLAG="-s"
385
386TMPDIR="/tmp"
387
388# "o_FLAG" is used by "nightly.sh" and "makebfu.sh" (it may be useful to
389# rename this 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
396unset \
397	CFLAGS \
398	LD_LIBRARY_PATH
399
400# a la ws
401ENVLDLIBS1=
402ENVLDLIBS2=
403ENVLDLIBS3=
404ENVCPPFLAGS1=
405ENVCPPFLAGS2=
406ENVCPPFLAGS3=
407ENVCPPFLAGS4=
408PARENT_ROOT=
409
410"${flags.O}" && export ROOT="$ROOT-open"
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
421ENVLDLIBS1="-L$ROOT/lib -L$ROOT/usr/lib"
422ENVCPPFLAGS1="-I$ROOT/usr/include"
423MAKEFLAGS=e
424
425export \
426        ENVLDLIBS1 \
427        ENVLDLIBS2 \
428        ENVLDLIBS3 \
429	ENVCPPFLAGS1 \
430        ENVCPPFLAGS2 \
431        ENVCPPFLAGS3 \
432	ENVCPPFLAGS4 \
433        MAKEFLAGS \
434        PARENT_ROOT \
435	SCM_TYPE
436
437printf 'RELEASE      is %s\n'   "$RELEASE"
438printf 'VERSION      is %s\n'   "$VERSION"
439printf 'RELEASE_DATE is %s\n\n' "$RELEASE_DATE"
440
441if [[ -f "$SRC/Makefile" ]] && egrep -s '^setup:' "$SRC/Makefile" ; then
442	print "The top-level 'setup' target is available \c"
443	print "to build headers and tools."
444	print ""
445
446elif "${flags.t}" ; then
447	printf \
448	    'The tools can be (re)built with the install target in %s.\n\n' \
449	    "${TOOLS}"
450fi
451
452#
453# place ourselves in a new task, respecting BUILD_PROJECT if set.
454#
455/usr/bin/newtask -c $$ ${BUILD_PROJECT:+-p$BUILD_PROJECT}
456
457if [[ "${flags.c}" == "false" && -x "$SHELL" && \
458    "$(basename "${SHELL}")" != "csh" ]]; then
459	# $SHELL is set, and it's not csh.
460
461	if "${flags.f}" ; then
462		print 'WARNING: -f is ignored when $SHELL is not csh'
463	fi
464
465	printf 'Using %s as shell.\n' "$SHELL"
466	exec "$SHELL" ${@:+-c "$@"}
467
468elif "${flags.f}" ; then
469	print 'Using csh -f as shell.'
470	exec csh -f ${@:+-c "$@"}
471
472else
473	print 'Using csh as shell.'
474	exec csh ${@:+-c "$@"}
475fi
476
477# not reached
478