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