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