xref: /illumos-gate/usr/src/tools/scripts/bldenv (revision 1702ab7ec82985a87bbbab682ca7d9a9ccb11e7a)
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# set variables we want environment file to be able to override
201MAKEFLAGS=e
202
203#
204# Setup environment variables
205#
206if [[ -f /etc/nightly.conf ]]; then
207	source /etc/nightly.conf
208fi
209
210if [[ -f "$1" ]]; then
211	if [[ "$1" == */* ]]; then
212		source "$1"
213	else
214		source "./$1"
215	fi
216else
217	if [[ -f "/opt/onbld/env/$1" ]]; then
218		source "/opt/onbld/env/$1"
219	else
220		printf \
221		    'Cannot find env file as either %s or /opt/onbld/env/%s\n' \
222		    "$1" "$1"
223		exit 1
224	fi
225fi
226shift
227
228# Check if we have sufficient data to continue...
229[[ -v CODEMGR_WS ]] || fatal_error "Error: Variable CODEMGR_WS not set."
230[[ -d "${CODEMGR_WS}" ]] || fatal_error "Error: ${CODEMGR_WS} is not a directory."
231[[ -f "${CODEMGR_WS}/usr/src/Makefile" ]] || fatal_error "Error: ${CODEMGR_WS}/usr/src/Makefile not found."
232
233# must match the getopts in nightly
234OPTIND=1
235NIGHTLY_OPTIONS="-${NIGHTLY_OPTIONS#-}"
236while getopts '+0ABCDdFfGIilMmNnpRrtUuwW' FLAG $NIGHTLY_OPTIONS
237do
238	case "$FLAG" in
239	  t)	flags.t=true  ;;
240	  +t)	flags.t=false ;;
241	  F)	flags.DF_build=true ;;
242	  *)	;;
243	esac
244done
245
246# DEBUG is a little bit complicated.  First, bldenv -d/+d over-rides
247# the env file.  Otherwise, we'll default to DEBUG iff we are *not*
248# building non-DEBUG bits at all.
249if [ "${flags.d_set}" != "true" ] && "${flags.DF_build}"; then
250	flags.d=true
251fi
252
253POUND_SIGN="#"
254basews=$(basename -- "$CODEMGR_WS")
255# have we set RELEASE_DATE in our env file?
256if [ -z "$RELEASE_DATE" ]; then
257	RELEASE_DATE=$(LC_ALL=C date +"%B %Y")
258fi
259now=$(LC_ALL=C date +%Y-%b-%d)
260DEV_CM_TAIL="development build: $LOGNAME $now [$basews]"
261
262#
263# We export POUND_SIGN, RELEASE_DATE and DEV_CM_TAIL to speed up the build
264# process by avoiding repeated shell invocations to evaluate Makefile.master
265# definitions.
266#
267export POUND_SIGN RELEASE_DATE DEV_CM_TAIL
268
269print 'Build type   is  \c'
270if ${flags.d} ; then
271	print 'DEBUG'
272	SUFFIX=""
273	unset RELEASE_BUILD
274	unset EXTRA_OPTIONS
275	unset EXTRA_CFLAGS
276
277	if [ -n "$DEBUG_CONSOLE_COLOR" ]; then
278		export DEFAULT_CONSOLE_COLOR="$DEBUG_CONSOLE_COLOR"
279	fi
280else
281	# default is a non-DEBUG build
282	print 'non-DEBUG'
283	SUFFIX="-nd"
284	export RELEASE_BUILD=
285	unset EXTRA_OPTIONS
286	unset EXTRA_CFLAGS
287
288	if [ -n "$RELEASE_CONSOLE_COLOR" ]; then
289		export DEFAULT_CONSOLE_COLOR="$RELEASE_CONSOLE_COLOR"
290	fi
291fi
292
293# update build-type variables
294PKGARCHIVE="${PKGARCHIVE}${SUFFIX}"
295
296#	Set PATH for a build
297PATH="/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:."
298if [[ "${SUNWSPRO}" != "" ]]; then
299	export PATH="${SUNWSPRO}/bin:$PATH"
300fi
301
302if [[ -n "${MAKE}" ]]; then
303	if [[ -x "${MAKE}" ]]; then
304		export PATH="$(dirname -- "${MAKE}"):$PATH"
305	else
306		print "\$MAKE (${MAKE}) is not a valid executible"
307		exit 1
308	fi
309fi
310
311TOOLS="${SRC}/tools"
312TOOLS_PROTO="${TOOLS}/proto/root_${MACH}-nd" ; export TOOLS_PROTO
313
314if "${flags.t}" ; then
315	export ONBLD_TOOLS="${ONBLD_TOOLS:=${TOOLS_PROTO}/opt/onbld}"
316
317	export STABS="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/stabs"
318	export CTFSTABS="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfstabs"
319	export GENOFFSETS="${TOOLS_PROTO}/opt/onbld/bin/genoffsets"
320
321	export CTFCONVERT="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfconvert"
322	export CTFMERGE="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfmerge"
323	export NDRGEN="${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ndrgen"
324
325	PATH="${TOOLS_PROTO}/opt/onbld/bin/${MACH}:${PATH}"
326	PATH="${TOOLS_PROTO}/opt/onbld/bin:${PATH}"
327	export PATH
328fi
329
330export DMAKE_MODE=${DMAKE_MODE:-parallel}
331
332#
333# Work around folks who have historically used GCC_ROOT and convert it to
334# GNUC_ROOT. We leave GCC_ROOT in the environment for now (though this could
335# mess up the case where multiple different gcc versions are being used to
336# shadow).
337#
338if [[ -n "${GCC_ROOT}" ]]; then
339	export GNUC_ROOT=${GCC_ROOT}
340fi
341
342DEF_STRIPFLAG="-s"
343
344TMPDIR="/tmp"
345
346export \
347	PATH TMPDIR \
348	POUND_SIGN \
349	DEF_STRIPFLAG \
350	RELEASE_DATE
351unset \
352	CFLAGS \
353	LD_LIBRARY_PATH
354
355# a la ws
356ENVLDLIBS1=
357ENVLDLIBS2=
358ENVLDLIBS3=
359ENVCPPFLAGS1=
360ENVCPPFLAGS2=
361ENVCPPFLAGS3=
362ENVCPPFLAGS4=
363PARENT_ROOT=
364PARENT_TOOLS_ROOT=
365
366if [[ "$MULTI_PROTO" != "yes" && "$MULTI_PROTO" != "no" ]]; then
367	printf \
368	    'WARNING: invalid value for MULTI_PROTO (%s); setting to "no".\n' \
369	    "$MULTI_PROTO"
370	export MULTI_PROTO="no"
371fi
372
373[[ "$MULTI_PROTO" == "yes" ]] && export ROOT="${ROOT}${SUFFIX}"
374
375ENVLDLIBS1="-L$ROOT/lib -L$ROOT/usr/lib"
376ENVCPPFLAGS1="-I$ROOT/usr/include"
377
378export \
379        ENVLDLIBS1 \
380        ENVLDLIBS2 \
381        ENVLDLIBS3 \
382	ENVCPPFLAGS1 \
383        ENVCPPFLAGS2 \
384        ENVCPPFLAGS3 \
385	ENVCPPFLAGS4 \
386        MAKEFLAGS \
387        PARENT_ROOT \
388        PARENT_TOOLS_ROOT
389
390printf 'RELEASE      is %s\n'   "$RELEASE"
391printf 'VERSION      is %s\n'   "$VERSION"
392printf 'RELEASE_DATE is %s\n\n' "$RELEASE_DATE"
393
394if [[ -f "$SRC/Makefile" ]] && egrep -s '^setup:' "$SRC/Makefile" ; then
395	print "The top-level 'setup' target is available \c"
396	print "to build headers and tools."
397	print ""
398
399elif "${flags.t}" ; then
400	printf \
401	    'The tools can be (re)built with the install target in %s.\n\n' \
402	    "${TOOLS}"
403fi
404
405#
406# place ourselves in a new task, respecting BUILD_PROJECT if set.
407#
408/usr/bin/newtask -c $$ ${BUILD_PROJECT:+-p$BUILD_PROJECT}
409
410if [[ "${flags.c}" == "false" && -x "$SHELL" && \
411    "$(basename -- "${SHELL}")" != "csh" ]]; then
412	# $SHELL is set, and it's not csh.
413
414	if "${flags.f}" ; then
415		print 'WARNING: -f is ignored when $SHELL is not csh'
416	fi
417
418	printf 'Using %s as shell.\n' "$SHELL"
419	exec "$SHELL" ${@:+-c "$@"}
420
421elif "${flags.f}" ; then
422	print 'Using csh -f as shell.'
423	exec csh -f ${@:+-c "$@"}
424
425else
426	print 'Using csh as shell.'
427	exec csh ${@:+-c "$@"}
428fi
429
430# not reached
431