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