xref: /titanic_50/usr/src/tools/scripts/bldenv.sh (revision 2d08521bd15501c8370ba2153b9cca4f094979d0)
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
45typeset -r USAGE=$'+
46[-?\n@(#)\$Id: bldenv (OS/Net) 2008-04-06 \$\n]
47[-author?OS/Net community <tools-discuss@opensolaris.org>]
48[+NAME?bldenv - spawn shell for interactive incremental OS-Net
49    consolidation builds]
50[+DESCRIPTION?bldenv is a useful companion to the nightly(1) script for
51    doing interactive and incremental builds in a workspace
52    already built with nightly(1). bldenv spawns a shell set up
53    with the same environment variables taken from an env_file,
54    as prepared for use with nightly(1).]
55[+?In addition to running a shell for interactive use, bldenv
56    can optionally run a single command in the given environment,
57    in the vein of sh -c or su -c. This is useful for
58    scripting, when an interactive shell would not be. If the
59    command is composed of multiple shell words or contains
60    other shell metacharacters, it must be quoted appropriately.]
61[+?bldenv is particularly useful for testing Makefile targets
62    like clobber, install and _msg, which otherwise require digging
63    through large build logs to figure out what is being
64    done.]
65[+?By default, bldenv will invoke the shell specified in
66    $SHELL. If $SHELL is not set or is invalid, csh will be
67    used.]
68[c?force the use of csh, regardless of the  value  of $SHELL.]
69[f?invoke csh with the -f (fast-start) option. This option is valid
70    only if $SHELL is unset or if it points to csh.]
71[d?set up environment for doing DEBUG builds (default is non-DEBUG)]
72[t?set up environment to use the tools in usr/src/tools (this is the
73    default, use +t to use the tools from /opt/onbld)]
74
75<env_file> [command]
76
77[+EXAMPLES]{
78    [+?Example 1: Interactive use]{
79        [+?Use bldenv to spawn a shell to perform  a  DEBUG  build  and
80            testing of the  Makefile  targets  clobber and install for
81            usr/src/cmd/true.]
82        [+\n% rlogin wopr-2 -l gk
83{root::wopr-2::49} bldenv -d /export0/jg/on10-se.env
84Build type   is  DEBUG
85RELEASE      is  5.10
86VERSION      is  wopr-2::on10-se::11/01/2001
87RELEASE_DATE is  May 2004
88The top-level `setup\' target is available to build headers
89and tools.
90Using /usr/bin/tcsh as shell.
91{root::wopr-2::49}
92{root::wopr-2::49} cd $SRC/cmd/true
93{root::wopr-2::50} make
94{root::wopr-2::51} make clobber
95/usr/bin/rm -f true true.po
96{root::wopr-2::52} make
97/usr/bin/rm -f true
98cat true.sh > true
99chmod +x true
100{root::wopr-2::53} make install
101install -s -m 0555 -u root -g bin -f /export0/jg/on10-se/proto/root_sparc/usr/bin true
102`install\' is up to date.]
103    }
104    [+?Example 2: Non-interactive use]{
105        [+?Invoke bldenv to create SUNWonbld with a single command:]
106        [+\nexample% bldenv onnv_06 \'cd $SRC/tools && make pkg\']
107        }
108}
109[+SEE ALSO?\bnightly\b(1)]
110'
111
112# main
113builtin basename
114
115# boolean flags (true/false)
116typeset flags=(
117	typeset c=false
118	typeset f=false
119	typeset d=false
120	typeset O=false
121	typeset o=false
122	typeset t=true
123	typeset s=(
124		typeset e=false
125		typeset h=false
126		typeset d=false
127		typeset o=false
128	)
129)
130
131typeset progname="$(basename -- "${0}")"
132
133OPTIND=1
134SUFFIX="-nd"
135
136while getopts -a "${progname}" "${USAGE}" OPT ; do
137    case ${OPT} in
138	  c)	flags.c=true  ;;
139	  +c)	flags.c=false ;;
140	  f)	flags.f=true  ;;
141	  +f)	flags.f=false ;;
142	  d)	flags.d=true  SUFFIX=""    ;;
143	  +d)	flags.d=false SUFFIX="-nd" ;;
144	  t)	flags.t=true  ;;
145	  +t)	flags.t=false ;;
146	  \?)	usage ;;
147    esac
148done
149shift $((OPTIND-1))
150
151# test that the path to the environment-setting file was given
152if (( $# < 1 )) ; then
153	usage
154fi
155
156# force locale to C
157export \
158	LANG=C \
159	LC_ALL=C \
160	LC_COLLATE=C \
161	LC_CTYPE=C \
162	LC_MESSAGES=C \
163	LC_MONETARY=C \
164	LC_NUMERIC=C \
165	LC_TIME=C
166
167# clear environment variables we know to be bad for the build
168unset \
169	LD_OPTIONS \
170	LD_LIBRARY_PATH \
171	LD_AUDIT \
172	LD_BIND_NOW \
173	LD_BREADTH \
174	LD_CONFIG \
175	LD_DEBUG \
176	LD_FLAGS \
177	LD_LIBRARY_PATH_64 \
178	LD_NOVERSION \
179	LD_ORIGIN \
180	LD_LOADFLTR \
181	LD_NOAUXFLTR \
182	LD_NOCONFIG \
183	LD_NODIRCONFIG \
184	LD_NOOBJALTER \
185	LD_PRELOAD \
186	LD_PROFILE \
187	CONFIG \
188	GROUP \
189	OWNER \
190	REMOTE \
191	ENV \
192	ARCH \
193	CLASSPATH
194
195#
196# Setup environment variables
197#
198if [[ -f /etc/nightly.conf ]]; then
199	source /etc/nightly.conf
200fi
201
202if [[ -f "$1" ]]; then
203	if [[ "$1" == */* ]]; then
204		source "$1"
205	else
206		source "./$1"
207	fi
208else
209	if [[ -f "/opt/onbld/env/$1" ]]; then
210		source "/opt/onbld/env/$1"
211	else
212		printf \
213		    'Cannot find env file as either %s or /opt/onbld/env/%s\n' \
214		    "$1" "$1"
215		exit 1
216	fi
217fi
218shift
219
220# contents of stdenv.sh inserted after next line:
221# STDENV_START
222# STDENV_END
223
224# Check if we have sufficient data to continue...
225[[ -v CODEMGR_WS ]] || fatal_error "Error: Variable CODEMGR_WS not set."
226[[ -d "${CODEMGR_WS}" ]] || fatal_error "Error: ${CODEMGR_WS} is not a directory."
227[[ -f "${CODEMGR_WS}/usr/src/Makefile" ]] || fatal_error "Error: ${CODEMGR_WS}/usr/src/Makefile not found."
228
229# must match the getopts in nightly.sh
230OPTIND=1
231NIGHTLY_OPTIONS="-${NIGHTLY_OPTIONS#-}"
232while getopts '+0ABCDdFfGIilMmNnpRrtUuwW' FLAG "$NIGHTLY_OPTIONS"
233do
234	case "$FLAG" in
235	  t)	flags.t=true  ;;
236	  +t)	flags.t=false ;;
237	  *)	;;
238	esac
239done
240
241POUND_SIGN="#"
242# have we set RELEASE_DATE in our env file?
243if [ -z "$RELEASE_DATE" ]; then
244	RELEASE_DATE=$(LC_ALL=C date +"%B %Y")
245fi
246BUILD_DATE=$(LC_ALL=C date +%Y-%b-%d)
247BASEWSDIR=$(basename -- "${CODEMGR_WS}")
248DEV_CM="\"@(#)SunOS Internal Development: $LOGNAME $BUILD_DATE [$BASEWSDIR]\""
249export DEV_CM RELEASE_DATE POUND_SIGN
250
251print 'Build type   is  \c'
252if ${flags.d} ; then
253	print 'DEBUG'
254	unset RELEASE_BUILD
255	unset EXTRA_OPTIONS
256	unset EXTRA_CFLAGS
257else
258	# default is a non-DEBUG build
259	print 'non-DEBUG'
260	export RELEASE_BUILD=
261	unset EXTRA_OPTIONS
262	unset EXTRA_CFLAGS
263fi
264
265# update build-type variables
266PKGARCHIVE="${PKGARCHIVE}${SUFFIX}"
267
268# 	Set PATH for a build
269PATH="/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:."
270if [[ "${SUNWSPRO}" != "" ]]; then
271	export PATH="${SUNWSPRO}/bin:$PATH"
272fi
273
274if [[ -n "${MAKE}" ]]; then
275	if [[ -x "${MAKE}" ]]; then
276		export PATH="$(dirname -- "${MAKE}"):$PATH"
277	else
278		print "\$MAKE (${MAKE}) is not a valid executible"
279		exit 1
280	fi
281fi
282
283TOOLS="${SRC}/tools"
284TOOLS_PROTO="${TOOLS}/proto/root_${MACH}-nd" ; export TOOLS_PROTO
285
286if "${flags.t}" ; then
287	export ONBLD_TOOLS="${ONBLD_TOOLS:=${TOOLS_PROTO}/opt/onbld}"
288
289	export STABS="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/stabs"
290	export CTFSTABS="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfstabs"
291	export GENOFFSETS="${TOOLS_PROTO}/opt/onbld/bin/genoffsets"
292
293	export CTFCONVERT="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfconvert"
294	export CTFMERGE="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfmerge"
295
296	export CTFCVTPTBL="${TOOLS_PROTO}/opt/onbld/bin/ctfcvtptbl"
297	export CTFFINDMOD="${TOOLS_PROTO}/opt/onbld/bin/ctffindmod"
298
299	PATH="${TOOLS_PROTO}/opt/onbld/bin/${MACH}:${PATH}"
300	PATH="${TOOLS_PROTO}/opt/onbld/bin:${PATH}"
301	export PATH
302fi
303
304export DMAKE_MODE=${DMAKE_MODE:-parallel}
305
306DEF_STRIPFLAG="-s"
307
308TMPDIR="/tmp"
309
310export \
311	PATH TMPDIR \
312	POUND_SIGN \
313	DEF_STRIPFLAG \
314	RELEASE_DATE
315unset \
316	CFLAGS \
317	LD_LIBRARY_PATH
318
319# a la ws
320ENVLDLIBS1=
321ENVLDLIBS2=
322ENVLDLIBS3=
323ENVCPPFLAGS1=
324ENVCPPFLAGS2=
325ENVCPPFLAGS3=
326ENVCPPFLAGS4=
327PARENT_ROOT=
328PARENT_TOOLS_ROOT=
329
330if [[ "$MULTI_PROTO" != "yes" && "$MULTI_PROTO" != "no" ]]; then
331	printf \
332	    'WARNING: invalid value for MULTI_PROTO (%s); setting to "no".\n' \
333	    "$MULTI_PROTO"
334	export MULTI_PROTO="no"
335fi
336
337[[ "$MULTI_PROTO" == "yes" ]] && export ROOT="${ROOT}${SUFFIX}"
338
339ENVLDLIBS1="-L$ROOT/lib -L$ROOT/usr/lib"
340ENVCPPFLAGS1="-I$ROOT/usr/include"
341MAKEFLAGS=e
342
343export \
344        ENVLDLIBS1 \
345        ENVLDLIBS2 \
346        ENVLDLIBS3 \
347	ENVCPPFLAGS1 \
348        ENVCPPFLAGS2 \
349        ENVCPPFLAGS3 \
350	ENVCPPFLAGS4 \
351        MAKEFLAGS \
352        PARENT_ROOT \
353        PARENT_TOOLS_ROOT
354
355printf 'RELEASE      is %s\n'   "$RELEASE"
356printf 'VERSION      is %s\n'   "$VERSION"
357printf 'RELEASE_DATE is %s\n\n' "$RELEASE_DATE"
358
359if [[ -f "$SRC/Makefile" ]] && egrep -s '^setup:' "$SRC/Makefile" ; then
360	print "The top-level 'setup' target is available \c"
361	print "to build headers and tools."
362	print ""
363
364elif "${flags.t}" ; then
365	printf \
366	    'The tools can be (re)built with the install target in %s.\n\n' \
367	    "${TOOLS}"
368fi
369
370#
371# place ourselves in a new task, respecting BUILD_PROJECT if set.
372#
373/usr/bin/newtask -c $$ ${BUILD_PROJECT:+-p$BUILD_PROJECT}
374
375if [[ "${flags.c}" == "false" && -x "$SHELL" && \
376    "$(basename -- "${SHELL}")" != "csh" ]]; then
377	# $SHELL is set, and it's not csh.
378
379	if "${flags.f}" ; then
380		print 'WARNING: -f is ignored when $SHELL is not csh'
381	fi
382
383	printf 'Using %s as shell.\n' "$SHELL"
384	exec "$SHELL" ${@:+-c "$@"}
385
386elif "${flags.f}" ; then
387	print 'Using csh -f as shell.'
388	exec csh -f ${@:+-c "$@"}
389
390else
391	print 'Using csh as shell.'
392	exec csh ${@:+-c "$@"}
393fi
394
395# not reached
396