xref: /illumos-gate/usr/src/tools/scripts/nightly (revision e49b90fb462012f1ca373a15a9e16657b22a36e6)
1#!/bin/ksh -p
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 2008, 2010, Richard Lowe
26# Copyright 2022 Tintri by DDN, Inc. All rights reserved.
27# Copyright 2012 Joshua M. Clulow <josh@sysmgr.org>
28# Copyright (c) 2017 by Delphix. All rights reserved.
29# Copyright 2020 Joyent, Inc.
30# Copyright 2019 Peter Trible.
31# Copyright 2020 Oxide Computer Company
32# Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
33# Copyright 2024 Bill Sommerfeld <sommerfeld@hamachi.org>
34#
35# Based on the nightly script from the integration folks,
36# Mostly modified and owned by mike_s.
37# Changes also by kjc, dmk.
38#
39# BRINGOVER_WS may be specified in the env file.
40# The default is the old behavior of CLONE_WS
41#
42# -i on the command line, means fast options, so when it's on the
43# command line (only), check builds are skipped no matter what
44# the setting of their individual flags are in NIGHTLY_OPTIONS.
45#
46# OPTHOME  may be set in the environment to override /opt
47#
48
49#
50# The CDPATH variable causes ksh's `cd' builtin to emit messages to stdout
51# under certain circumstances, which can really screw things up; unset it.
52#
53unset CDPATH
54
55# Get the absolute path of the nightly script that the user invoked.  This
56# may be a relative path, and we need to do this before changing directory.
57nightly_path=`whence $0`
58
59#
60# Keep track of where we found nightly so we can invoke the matching
61# which_scm script.  If that doesn't work, don't go guessing, just rely
62# on the $PATH settings, which will generally give us either /opt/onbld
63# or the user's workspace.
64#
65WHICH_SCM=$(dirname $nightly_path)/which_scm
66if [[ ! -x $WHICH_SCM ]]; then
67	WHICH_SCM=which_scm
68fi
69
70function fatal_error
71{
72	print -u2 "nightly: $*"
73	exit 1
74}
75
76#
77# Function to do a DEBUG and non-DEBUG build. Needed because we might
78# need to do another for the source build, and since we only deliver DEBUG or
79# non-DEBUG packages.
80#
81# usage: normal_build
82#
83function normal_build {
84
85	typeset orig_p_FLAG="$p_FLAG"
86	typeset crypto_signer="$CODESIGN_USER"
87
88	suffix=""
89
90	# non-DEBUG build begins
91
92	if [ "$F_FLAG" = "n" ]; then
93		set_non_debug_build_flags
94		CODESIGN_USER="$crypto_signer" \
95		    build "non-DEBUG" "$suffix-nd" "-nd" "$MULTI_PROTO"
96	else
97		echo "\n==== No non-DEBUG $open_only build ====\n" >> "$LOGFILE"
98	fi
99
100	# non-DEBUG build ends
101
102	# DEBUG build begins
103
104	if [ "$D_FLAG" = "y" ]; then
105		set_debug_build_flags
106		CODESIGN_USER="$crypto_signer" \
107		    build "DEBUG" "$suffix" "" "$MULTI_PROTO"
108	else
109		echo "\n==== No DEBUG $open_only build ====\n" >> "$LOGFILE"
110	fi
111
112	# DEBUG build ends
113
114	p_FLAG="$orig_p_FLAG"
115}
116
117#
118# usage: run_hook HOOKNAME ARGS...
119#
120# If variable "$HOOKNAME" is defined, insert a section header into
121# our logs and then run the command with ARGS
122#
123function run_hook {
124	HOOKNAME=$1
125	eval HOOKCMD=\$$HOOKNAME
126	shift
127
128	if [ -n "$HOOKCMD" ]; then
129		(
130			echo "\n==== Running $HOOKNAME command: $HOOKCMD ====\n"
131			( $HOOKCMD "$@" 2>&1 )
132			if [ "$?" -ne 0 ]; then
133				# Let exit status propagate up
134				touch $TMPDIR/abort
135			fi
136		) | tee -a $mail_msg_file >> $LOGFILE
137
138		if [ -f $TMPDIR/abort ]; then
139			build_ok=n
140			echo "\nAborting at request of $HOOKNAME" |
141				tee -a $mail_msg_file >> $LOGFILE
142			exit 1
143		fi
144	fi
145}
146
147# Return library search directive as function of given root.
148function myldlibs {
149	echo "-L$1/lib -L$1/usr/lib"
150}
151
152# Return header search directive as function of given root.
153function myheaders {
154	echo "-I$1/usr/include"
155}
156
157#
158# Function to do the build, including package generation.
159# usage: build LABEL SUFFIX ND MULTIPROTO
160# - LABEL is used to tag build output.
161# - SUFFIX is used to distinguish files (e.g., DEBUG vs non-DEBUG,
162#   open-only vs full tree).
163# - ND is "-nd" (non-DEBUG builds) or "" (DEBUG builds).
164# - If MULTIPROTO is "yes", it means to name the proto area according to
165#   SUFFIX.  Otherwise ("no"), (re)use the standard proto area.
166#
167function build {
168	LABEL=$1
169	SUFFIX=$2
170	ND=$3
171	MULTIPROTO=$4
172	INSTALLOG=install${SUFFIX}-${MACH}
173	NOISE=noise${SUFFIX}-${MACH}
174	PKGARCHIVE=${PKGARCHIVE_ORIG}${SUFFIX}
175
176	ORIGROOT=$ROOT
177	[ $MULTIPROTO = no ] || export ROOT=$ROOT$SUFFIX
178
179	export ENVLDLIBS1=`myldlibs $ROOT`
180	export ENVCPPFLAGS1=`myheaders $ROOT`
181
182	this_build_ok=y
183	#
184	#	Build OS-Networking source
185	#
186	echo "\n==== Building OS-Net source at `date` ($LABEL) ====\n" \
187		>> $LOGFILE
188
189	rm -f $SRC/${INSTALLOG}.out
190	cd $SRC
191	/bin/time $MAKE -e install 2>&1 | \
192	    tee -a $SRC/${INSTALLOG}.out >> $LOGFILE
193
194	echo "\n==== Build errors ($LABEL) ====\n" >> $mail_msg_file
195	egrep ":" $SRC/${INSTALLOG}.out |
196	    egrep -e "(^${MAKE}"$':|[\t ]error[:\t ]|ld: guidance:)' | \
197	    egrep -v ": (Entering|Leaving) directory " | \
198	    egrep -v "Ignoring unknown host" | \
199	    egrep -v "cc .* -o error " | \
200	    egrep -v "warning" | tee $TMPDIR/build_errs${SUFFIX} \
201	    >> $mail_msg_file
202	    sed -n $'/^Undefined[\t ]*first referenced$/,/^ld: fatal:/p' \
203	    < $SRC/${INSTALLOG}.out >> $mail_msg_file
204	if [[ -s $TMPDIR/build_errs${SUFFIX} ]]; then
205		build_ok=n
206		this_build_ok=n
207	fi
208	grep "bootblock image is .* bytes too big" $SRC/${INSTALLOG}.out \
209		>> $mail_msg_file
210	if [ "$?" = "0" ]; then
211		build_ok=n
212		this_build_ok=n
213	fi
214
215	echo "\n==== Build warnings ($LABEL) ====\n" >>$mail_msg_file
216	egrep -i 'warn(ing)?:' $SRC/${INSTALLOG}.out \
217		| egrep -v '^tic:' \
218		| egrep -v "symbol (\`|')timezone' has differing types:" \
219		| egrep -v "parameter <PSTAMP> set to" \
220		| egrep -v "Ignoring unknown host" \
221		| egrep -v "redefining segment flags attribute for" \
222		| tee $TMPDIR/build_warnings${SUFFIX} >> $mail_msg_file
223	if [[ -s $TMPDIR/build_warnings${SUFFIX} ]]; then
224		build_ok=n
225		this_build_ok=n
226	fi
227
228	echo "\n==== Ended OS-Net source build at `date` ($LABEL) ====\n" \
229		>> $LOGFILE
230
231	echo "\n==== Elapsed build time ($LABEL) ====\n" >>$mail_msg_file
232	tail -3  $SRC/${INSTALLOG}.out >>$mail_msg_file
233
234	if [ "$i_FLAG" = "n" ]; then
235		rm -f $SRC/${NOISE}.ref
236		if [ -f $SRC/${NOISE}.out ]; then
237			mv $SRC/${NOISE}.out $SRC/${NOISE}.ref
238		fi
239		grep : $SRC/${INSTALLOG}.out \
240			| egrep -v '^/' \
241			| egrep -v '^(Start|Finish|real|user|sys|./bld_awk)' \
242			| egrep -v '^tic:' \
243			| egrep -v '^mcs' \
244			| egrep -v '^LD_LIBRARY_PATH=' \
245			| egrep -v 'ar: creating' \
246			| egrep -v 'ar: writing' \
247			| egrep -v 'conflicts:' \
248			| egrep -v ':saved created' \
249			| egrep -v '^stty.*c:' \
250			| egrep -v '^mfgname.c:' \
251			| egrep -v '^uname-i.c:' \
252			| egrep -v '^volumes.c:' \
253			| egrep -v '^lint library construction:' \
254			| egrep -v 'tsort: INFORM:' \
255			| egrep -v 'stripalign:' \
256			| egrep -v 'chars, width' \
257			| egrep -v "symbol (\`|')timezone' has differing types:" \
258			| egrep -v 'PSTAMP' \
259			| egrep -v '^Manifying' \
260			| egrep -v 'Ignoring unknown host' \
261			| egrep -v 'Processing method:' \
262			| egrep -v '^Writing' \
263			| egrep -v 'spellin1:' \
264			| egrep -v '^adding:' \
265			| egrep -v "^echo 'msgid" \
266			| egrep -v '^echo ' \
267			| egrep -v '\.c:$' \
268			| egrep -v '^Adding file:' \
269			| egrep -v 'CLASSPATH=' \
270			| egrep -v '\/var\/mail\/:saved' \
271			| egrep -v -- '-DUTS_VERSION=' \
272			| egrep -v '^Running Mkbootstrap' \
273			| egrep -v '^Applet length read:' \
274			| egrep -v 'bytes written:' \
275			| egrep -v '^File:SolarisAuthApplet.bin' \
276			| egrep -v -i 'jibversion' \
277			| egrep -v '^Output size:' \
278			| egrep -v '^Solo size statistics:' \
279			| egrep -v '^Using ROM API Version' \
280			| egrep -v '^Zero Signature length:' \
281			| egrep -v '^Note \(probably harmless\):' \
282			| egrep -v '::' \
283			| egrep -v '^\+' \
284			| egrep -v 'svccfg-native -s svc:/' \
285			| egrep -v ": (Entering|Leaving) directory " \
286			| sort | uniq >$SRC/${NOISE}.out
287		if [ ! -f $SRC/${NOISE}.ref ]; then
288			cp $SRC/${NOISE}.out $SRC/${NOISE}.ref
289		fi
290		echo "\n==== Build noise differences ($LABEL) ====\n" \
291			>>$mail_msg_file
292		diff $SRC/${NOISE}.ref $SRC/${NOISE}.out >>$mail_msg_file
293	fi
294
295	#
296	#	Re-sign selected binaries using signing server
297	#	(gatekeeper builds only)
298	#
299	if [ -n "$CODESIGN_USER" -a "$this_build_ok" = "y" ]; then
300		echo "\n==== Signing proto area at `date` ====\n" >> $LOGFILE
301		signing_file="${TMPDIR}/signing"
302		rm -f ${signing_file}
303		export CODESIGN_USER
304		signproto $SRC/tools/codesign/creds 2>&1 | \
305			tee -a ${signing_file} >> $LOGFILE
306		echo "\n==== Finished signing proto area at `date` ====\n" \
307		    >> $LOGFILE
308		echo "\n==== Crypto module signing errors ($LABEL) ====\n" \
309		    >> $mail_msg_file
310		egrep 'WARNING|ERROR' ${signing_file} >> $mail_msg_file
311		if (( $? == 0 )) ; then
312			build_ok=n
313			this_build_ok=n
314		fi
315	fi
316
317	#
318	#	Building Packages
319	#
320	if [ "$p_FLAG" = "y" -a "$this_build_ok" = "y" ]; then
321		if [ -d $SRC/pkg ]; then
322			echo "\n==== Creating $LABEL packages at `date` ====\n" \
323				>> $LOGFILE
324			echo "Clearing out $PKGARCHIVE ..." >> $LOGFILE
325			rm -rf $PKGARCHIVE >> "$LOGFILE" 2>&1
326			mkdir -p $PKGARCHIVE >> "$LOGFILE" 2>&1
327
328			rm -f $SRC/pkg/${INSTALLOG}.out
329			cd $SRC/pkg
330			/bin/time $MAKE -e install 2>&1 | \
331			    tee -a $SRC/pkg/${INSTALLOG}.out >> $LOGFILE
332
333			echo "\n==== package build errors ($LABEL) ====\n" \
334				>> $mail_msg_file
335
336			egrep "${MAKE}|ERROR|WARNING" $SRC/pkg/${INSTALLOG}.out | \
337				grep ':' | \
338				grep -v PSTAMP | \
339				egrep -v "${MAKE}: (Entering|Leaving) directory " | \
340				egrep -v "Ignoring unknown host" | \
341				tee $TMPDIR/package >> $mail_msg_file
342			if [[ -s $TMPDIR/package ]]; then
343				build_extras_ok=n
344				this_build_ok=n
345			fi
346		else
347			#
348			# Handle it gracefully if -p was set but there so
349			# no pkg directory.
350			#
351			echo "\n==== No $LABEL packages to build ====\n" \
352				>> $LOGFILE
353		fi
354	else
355		echo "\n==== Not creating $LABEL packages ====\n" >> $LOGFILE
356	fi
357
358	ROOT=$ORIGROOT
359}
360
361#
362# Bootstrap build tools which are pre-requisites for the rest of the build.
363#
364function bootstrap_tools {
365	echo "\n==== Bootstrapping tools at `date` ====\n" >> $LOGFILE
366
367	typeset INSTALLOG=install-bootstrap-${MACH}
368
369	rm -f $TMPDIR/make-state ${TOOLS}/$INSTALLOG.out
370	cd ${TOOLS}
371	/bin/time $MAKE -K $TMPDIR/make-state -e TARGET=install bootstrap \
372	    2>&1 | tee -a ${TOOLS}/$INSTALLOG.out >> $LOGFILE
373
374	echo "\n==== Bootstrap build errors ====\n" >> $mail_msg_file
375
376	egrep ":" ${TOOLS}/$INSTALLOG.out |
377	    egrep -e "(${MAKE}"$':|[\t ]error[:\t ])' | \
378	    egrep -v ": (Entering|Leaving) directory " | \
379	    egrep -v warning | tee $TMPDIR/bootstrap_errors >> $mail_msg_file
380
381	[[ -s $TMPDIR/bootstrap_errors ]] && return 1
382	return 0
383}
384
385#
386# Build and install the tools.
387#
388# usage: build_tools DESTROOT
389#
390# returns zero status if the build was successful.
391#
392function build_tools {
393	DESTROOT=$1
394
395	INSTALLOG=install-${MACH}
396
397	echo "\n==== Building tools at `date` ====\n" \
398		>> $LOGFILE
399
400	rm -f ${TOOLS}/${INSTALLOG}.out
401	cd ${TOOLS}
402	/bin/time $MAKE TOOLS_PROTO=${DESTROOT} -e install 2>&1 | \
403	    tee -a ${TOOLS}/${INSTALLOG}.out >> $LOGFILE
404
405	echo "\n==== Tools build errors ====\n" >> $mail_msg_file
406
407	egrep ":" ${TOOLS}/${INSTALLOG}.out |
408		egrep -e "(${MAKE}"$':|[\t ]error[:\t ])' | \
409		egrep -v ": (Entering|Leaving) directory " | \
410		egrep -v "Ignoring unknown host" | \
411		egrep -v warning | tee $TMPDIR/tools_errors >> $mail_msg_file
412
413	if [[ -s $TMPDIR/tools_errors ]]; then
414		return 1
415	fi
416	return 0
417}
418
419function staffer {
420	if [ $ISUSER -ne 0 ]; then
421		"$@"
422	else
423		arg="\"$1\""
424		shift
425		for i
426		do
427			arg="$arg \"$i\""
428		done
429		eval su $STAFFER -c \'$arg\'
430	fi
431}
432
433#
434# Verify that the closed bins are present
435#
436function check_closed_bins {
437	if [[ -n "$ON_CLOSED_BINS" && ! -d "$ON_CLOSED_BINS" ]]; then
438		echo "ON_CLOSED_BINS must point to the closed binaries tree."
439		build_ok=n
440		exit 1
441	fi
442}
443
444#
445# wrapper over wsdiff.
446# usage: do_wsdiff LABEL OLDPROTO NEWPROTO
447#
448function do_wsdiff {
449	label=$1
450	oldproto=$2
451	newproto=$3
452
453	wsdiff="wsdiff"
454	[ "$t_FLAG" = y ] && wsdiff="wsdiff -t"
455
456	echo "\n==== Getting object changes since last build at `date`" \
457	    "($label) ====\n" | tee -a $LOGFILE >> $mail_msg_file
458	$wsdiff -s -r ${TMPDIR}/wsdiff.results $oldproto $newproto 2>&1 | \
459		    tee -a $LOGFILE >> $mail_msg_file
460	echo "\n==== Object changes determined at `date` ($label) ====\n" | \
461	    tee -a $LOGFILE >> $mail_msg_file
462}
463
464#
465# Functions for setting build flags (DEBUG/non-DEBUG).  Keep them
466# together.
467#
468
469function set_non_debug_build_flags {
470	export RELEASE_BUILD ; RELEASE_BUILD=
471	unset EXTRA_OPTIONS
472	unset EXTRA_CFLAGS
473	if [ -n "$RELEASE_CONSOLE_COLOR" ]; then
474		export DEFAULT_CONSOLE_COLOR="$RELEASE_CONSOLE_COLOR"
475	fi
476}
477
478function set_debug_build_flags {
479	unset RELEASE_BUILD
480	unset EXTRA_OPTIONS
481	unset EXTRA_CFLAGS
482
483	if [ -n "$DEBUG_CONSOLE_COLOR" ]; then
484		export DEFAULT_CONSOLE_COLOR="$DEBUG_CONSOLE_COLOR"
485	fi
486}
487
488
489MACH=`uname -p`
490
491if [ "$OPTHOME" = "" ]; then
492	OPTHOME=/opt
493	export OPTHOME
494fi
495
496USAGE='Usage: nightly [-in] [+t] [-B WS] [-b BR] [-d WS] [-V VERS] <env_file>
497
498Where:
499	-i	Fast incremental options (no clobber, check)
500	-n      Do not do a bringover
501	+t	Use the build tools in $ONBLD_TOOLS/bin
502	-B WS	bringover from WS (directory or URL)
503	-b BR   bringover branch BR
504	-d WS	do build in WS (directory)
505	-V VERS set the build version string to VERS
506
507	<env_file>  file in Bourne shell syntax that sets and exports
508	variables that configure the operation of this script and many of
509	the scripts this one calls. If <env_file> does not exist,
510	it will be looked for in $OPTHOME/onbld/env.
511
512non-DEBUG is the default build type. Build options must be set in the
513NIGHTLY_OPTIONS variable in the <env_file> as follows:
514
515	-A	check for ABI differences in .so files
516	-C	check for cstyle/hdrchk errors
517	-D	do a build with DEBUG on
518	-F	do _not_ do a non-DEBUG build
519	-G	gate keeper default group of options (-au)
520	-I	integration engineer default group of options (-ampu)
521	-L	do not run pkglint
522	-M	do not run pmodes (safe file permission checker)
523	-N	do not run protocmp
524	-R	default group of options for building a release (-mp)
525	-U	update proto area in the parent
526	-V VERS set the build version string to VERS
527	-f	find unreferenced files
528	-i	do an incremental build (no "make clobber")
529	-m	send mail to $MAILTO at end of build
530	-n      do not do a bringover
531	-p	create packages
532	-r	check ELF runtime attributes in the proto area
533	-t	build and use the tools in $SRC/tools (default setting)
534	+t	Use the build tools in $ONBLD_TOOLS/bin
535	-u	update proto_list_$MACH and friends in the parent workspace;
536		when used with -f, also build an unrefmaster.out in the parent
537	-w	report on differences between previous and current proto areas
538'
539#
540#	A log file will be generated under the name $LOGFILE
541#	for partially completed build and log.`date '+%F'`
542#	in the same directory for fully completed builds.
543#
544
545# default values for low-level FLAGS; G I R are group FLAGS
546A_FLAG=n
547C_FLAG=n
548D_FLAG=n
549F_FLAG=n
550f_FLAG=n
551i_FLAG=n; i_CMD_LINE_FLAG=n
552L_FLAG=n
553M_FLAG=n
554m_FLAG=n
555N_FLAG=n
556n_FLAG=n
557p_FLAG=n
558r_FLAG=n
559t_FLAG=y
560U_FLAG=n
561u_FLAG=n
562V_FLAG=n
563w_FLAG=n
564W_FLAG=n
565#
566build_ok=y
567build_extras_ok=y
568
569#
570# examine arguments
571#
572
573OPTIND=1
574while getopts +b:B:d:intV:W FLAG
575do
576	case $FLAG in
577	  b )	BRINGOVER_BRANCH="$OPTARG"
578		;;
579	  B )	BRINGOVER_WS="$OPTARG"
580		;;
581	  d )	CODEMGR_WS="$OPTARG"
582		;;
583	  i )	i_FLAG=y; i_CMD_LINE_FLAG=y
584		;;
585	  n )	n_FLAG=y
586		;;
587	 +t )	t_FLAG=n
588		;;
589	  V )	V_FLAG=y
590		V_ARG="$OPTARG"
591		;;
592	  W )   W_FLAG=y
593		;;
594	 \? )	echo "$USAGE"
595		exit 1
596		;;
597	esac
598done
599
600# correct argument count after options
601shift `expr $OPTIND - 1`
602
603# test that the path to the environment-setting file was given
604if [ $# -ne 1 ]; then
605	echo "$USAGE"
606	exit 1
607fi
608
609# check if user is running nightly as root
610# ISUSER is set non-zero if an ordinary user runs nightly, or is zero
611# when root invokes nightly.
612/usr/bin/id | grep '^uid=0(' >/dev/null 2>&1
613ISUSER=$?;	export ISUSER
614
615#
616# force locale to C
617LANG=C;		export LANG
618LC_ALL=C;	export LC_ALL
619LC_COLLATE=C;	export LC_COLLATE
620LC_CTYPE=C;	export LC_CTYPE
621LC_MESSAGES=C;	export LC_MESSAGES
622LC_MONETARY=C;	export LC_MONETARY
623LC_NUMERIC=C;	export LC_NUMERIC
624LC_TIME=C;	export LC_TIME
625
626# clear environment variables we know to be bad for the build
627unset LD_OPTIONS
628unset LD_AUDIT		LD_AUDIT_32		LD_AUDIT_64
629unset LD_BIND_NOW	LD_BIND_NOW_32		LD_BIND_NOW_64
630unset LD_BREADTH	LD_BREADTH_32		LD_BREADTH_64
631unset LD_CONFIG		LD_CONFIG_32		LD_CONFIG_64
632unset LD_DEBUG		LD_DEBUG_32		LD_DEBUG_64
633unset LD_DEMANGLE	LD_DEMANGLE_32		LD_DEMANGLE_64
634unset LD_FLAGS		LD_FLAGS_32		LD_FLAGS_64
635unset LD_LIBRARY_PATH	LD_LIBRARY_PATH_32	LD_LIBRARY_PATH_64
636unset LD_LOADFLTR	LD_LOADFLTR_32		LD_LOADFLTR_64
637unset LD_NOAUDIT	LD_NOAUDIT_32		LD_NOAUDIT_64
638unset LD_NOAUXFLTR	LD_NOAUXFLTR_32		LD_NOAUXFLTR_64
639unset LD_NOCONFIG	LD_NOCONFIG_32		LD_NOCONFIG_64
640unset LD_NODIRCONFIG	LD_NODIRCONFIG_32	LD_NODIRCONFIG_64
641unset LD_NODIRECT	LD_NODIRECT_32		LD_NODIRECT_64
642unset LD_NOLAZYLOAD	LD_NOLAZYLOAD_32	LD_NOLAZYLOAD_64
643unset LD_NOOBJALTER	LD_NOOBJALTER_32	LD_NOOBJALTER_64
644unset LD_NOVERSION	LD_NOVERSION_32		LD_NOVERSION_64
645unset LD_ORIGIN		LD_ORIGIN_32		LD_ORIGIN_64
646unset LD_PRELOAD	LD_PRELOAD_32		LD_PRELOAD_64
647unset LD_PROFILE	LD_PROFILE_32		LD_PROFILE_64
648
649unset CONFIG
650unset GROUP
651unset OWNER
652unset REMOTE
653unset ENV
654unset ARCH
655unset CLASSPATH
656unset NAME
657
658#
659# To get ONBLD_TOOLS from the environment, it must come from the env file.
660# If it comes interactively, it is generally TOOLS_PROTO, which will be
661# clobbered before the compiler version checks, which will therefore fail.
662#
663unset ONBLD_TOOLS
664
665#
666#	Setup environmental variables
667#
668if [ -f /etc/nightly.conf ]; then
669	. /etc/nightly.conf
670fi
671
672if [ -f $1 ]; then
673	if [[ $1 == */* ]]; then
674		. $1
675	else
676		. ./$1
677	fi
678else
679	if [ -f $OPTHOME/onbld/env/$1 ]; then
680		. $OPTHOME/onbld/env/$1
681	else
682		echo "Cannot find env file as either $1 or $OPTHOME/onbld/env/$1"
683		exit 1
684	fi
685fi
686
687# Check if we have sufficient data to continue...
688[[ -v CODEMGR_WS ]] || fatal_error "Error: Variable CODEMGR_WS not set."
689if  [[ "${NIGHTLY_OPTIONS}" == ~(F)n ]] ; then
690	# Check if the gate data are valid if we don't do a "bringover" below
691	[[ -d "${CODEMGR_WS}" ]] || \
692		fatal_error "Error: ${CODEMGR_WS} is not a directory."
693	[[ -f "${CODEMGR_WS}/usr/src/Makefile" ]] || \
694		fatal_error "Error: ${CODEMGR_WS}/usr/src/Makefile not found."
695fi
696
697#
698# place ourselves in a new task, respecting BUILD_PROJECT if set.
699#
700if [ -z "$BUILD_PROJECT" ]; then
701	/usr/bin/newtask -c $$
702else
703	/usr/bin/newtask -c $$ -p $BUILD_PROJECT
704fi
705
706ps -o taskid= -p $$ | read build_taskid
707ps -o project= -p $$ | read build_project
708
709#
710# See if NIGHTLY_OPTIONS is set
711#
712if [ "$NIGHTLY_OPTIONS" = "" ]; then
713	print -u2 "NIGHTLY_OPTIONS must be set in environment file"
714	echo "$USAGE"
715	exit 1
716fi
717
718#
719# If BRINGOVER_WS was not specified, let it default to CLONE_WS
720#
721if [ "$BRINGOVER_WS" = "" ]; then
722	BRINGOVER_WS=$CLONE_WS
723fi
724
725if [ "$BRINGOVER_REMOTE" = "" ]; then
726	BRINGOVER_REMOTE=nightly_bringover_ws
727fi
728
729if [ "$BRINGOVER_SCM" = "" ]; then
730	BRINGOVER_SCM=git
731fi
732
733check_closed_bins
734
735#
736# Note: changes to the option letters here should also be applied to the
737#	bldenv script.  `d' is listed for backward compatibility.
738#
739NIGHTLY_OPTIONS=-${NIGHTLY_OPTIONS#-}
740OPTIND=1
741while getopts +ABCDdFfGIiLMmNnpRrtUuwW FLAG $NIGHTLY_OPTIONS
742do
743	case $FLAG in
744	  A )	A_FLAG=y
745		;;
746	  B )	D_FLAG=y
747		;; # old version of D
748	  C )	C_FLAG=y
749		;;
750	  D )	D_FLAG=y
751		;;
752	  F )	F_FLAG=y
753		;;
754	  f )	f_FLAG=y
755		;;
756	  G )   u_FLAG=y
757		;;
758	  I )	m_FLAG=y
759		p_FLAG=y
760		u_FLAG=y
761		;;
762	  i )	i_FLAG=y
763		;;
764	  L )	L_FLAG=y
765		;;
766	  M )	M_FLAG=y
767		;;
768	  m )	m_FLAG=y
769		;;
770	  N )	N_FLAG=y
771		;;
772	  n )	n_FLAG=y
773		;;
774	  p )	p_FLAG=y
775		;;
776	  R )	m_FLAG=y
777		p_FLAG=y
778		;;
779	  r )	r_FLAG=y
780		;;
781	 +t )	t_FLAG=n
782		;;
783	  U )   if [ -z "${PARENT_ROOT}" ]; then
784			echo "PARENT_ROOT must be set if the U flag is" \
785			    "present in NIGHTLY_OPTIONS."
786			exit 1
787		fi
788		NIGHTLY_PARENT_ROOT=$PARENT_ROOT
789		if [ -n "${PARENT_TOOLS_ROOT}" ]; then
790			NIGHTLY_PARENT_TOOLS_ROOT=$PARENT_TOOLS_ROOT
791		fi
792		U_FLAG=y
793		;;
794	  u )	u_FLAG=y
795		;;
796	  w )	w_FLAG=y
797		;;
798	  W )   W_FLAG=y
799		;;
800	 \? )	echo "$USAGE"
801		exit 1
802		;;
803	esac
804done
805
806# Skip pkglint if packages aren't being built
807[ $p_FLAG = n ] && L_FLAG=y
808
809if [ $ISUSER -ne 0 ]; then
810	# Set default value for STAFFER, if needed.
811	if [ -z "$STAFFER" -o "$STAFFER" = "nobody" ]; then
812		STAFFER=`/usr/xpg4/bin/id -un`
813		export STAFFER
814	fi
815fi
816
817if [ -z "$MAILTO" -o "$MAILTO" = "nobody" ]; then
818	MAILTO=$STAFFER
819	export MAILTO
820fi
821
822PATH="$OPTHOME/onbld/bin:$OPTHOME/onbld/bin/${MACH}:/usr/ccs/bin"
823PATH="$PATH:$OPTHOME/SUNWspro/bin:/usr/bin:/usr/sbin:/usr/ucb"
824PATH="$PATH:/usr/openwin/bin:/usr/sfw/bin:/opt/sfw/bin:."
825export PATH
826
827# roots of source trees, both relative to $SRC and absolute.
828relsrcdirs="."
829abssrcdirs="$SRC"
830
831PROTOCMPTERSE="protocmp.terse -gu"
832POUND_SIGN="#"
833basews="$(basename "$CODEMGR_WS")"
834# have we set RELEASE_DATE in our env file?
835if [ -z "$RELEASE_DATE" ]; then
836	RELEASE_DATE=$(LC_ALL=C date +"%B %Y")
837fi
838now=$(LC_ALL=C date +%Y-%b-%d)
839DEV_CM_TAIL="development build: $LOGNAME $now [$basews]"
840
841#
842# We export POUND_SIGN, RELEASE_DATE and DEV_CM_TAIL to speed up the build
843# process by avoiding repeated shell invocations to evaluate Makefile.master
844# definitions.
845#
846export POUND_SIGN RELEASE_DATE DEV_CM_TAIL
847
848maketype="distributed"
849if [[ -z "$MAKE" ]]; then
850	MAKE=dmake
851elif [[ ! -x "$MAKE" ]]; then
852	echo "\$MAKE is set to garbage in the environment"
853	exit 1
854fi
855export PATH
856export MAKE
857
858if [ "${SUNWSPRO}" != "" ]; then
859	PATH="${SUNWSPRO}/bin:$PATH"
860	export PATH
861fi
862
863hostname=$(uname -n)
864if [[ $DMAKE_MAX_JOBS != +([0-9]) || $DMAKE_MAX_JOBS == 0 ]]
865then
866	maxjobs=
867	if [[ -f $HOME/.make.machines ]]
868	then
869		egrep -i $'^[\t ]*'"${hostname}"$'[\t .]' \
870			$HOME/.make.machines | read host jobs
871		maxjobs=${jobs##*=}
872	fi
873
874	if [[ $maxjobs != +([0-9]) || $maxjobs == 0 ]]
875	then
876		# default
877		maxjobs=4
878	fi
879
880	export DMAKE_MAX_JOBS=$maxjobs
881fi
882
883DMAKE_MODE=parallel;
884export DMAKE_MODE
885
886if [ -z "${ROOT}" ]; then
887	echo "ROOT must be set."
888	exit 1
889fi
890
891TMPDIR="/tmp/nightly.tmpdir.$$"
892export TMPDIR
893rm -rf ${TMPDIR}
894mkdir -p $TMPDIR || exit 1
895chmod 777 $TMPDIR
896
897#
898# Work around folks who have historically used GCC_ROOT and convert it to
899# GNUC_ROOT. We leave GCC_ROOT in the environment for now (though this could
900# mess up the case where multiple different gcc versions are being used to
901# shadow).
902#
903if [[ -n "${GCC_ROOT}" ]]; then
904	export GNUC_ROOT=${GCC_ROOT}
905fi
906
907#
908# Tools should only be built non-DEBUG.  Keep track of the tools proto
909# area path relative to $TOOLS, because the latter changes in an
910# export build.
911#
912# TOOLS_PROTO is included below for builds other than usr/src/tools
913# that look for this location.  For usr/src/tools, this will be
914# overridden on the $MAKE command line in build_tools().
915#
916TOOLS=${SRC}/tools
917TOOLS_PROTO_REL=proto/root_${MACH}-nd
918TOOLS_PROTO=${TOOLS}/${TOOLS_PROTO_REL};	export TOOLS_PROTO
919
920unset   CFLAGS LD_LIBRARY_PATH LDFLAGS
921
922#
923# Echo the SCM type of the parent workspace, this can't just be which_scm
924# as that does not know how to identify various network repositories.
925#
926function parent_wstype {
927	typeset scm_type junk
928
929	CODEMGR_WS="$BRINGOVER_WS" "$WHICH_SCM" 2>/dev/null \
930	    | read scm_type junk
931	if [[ -z "$scm_type" || "$scm_type" == unknown ]]; then
932		# Probe BRINGOVER_WS to determine its type
933		case "$BRINGOVER_WS" in
934		git://* | http://*.git | https://*.git)
935			scm_type="git"
936			;;
937		ssh://* | http://* | https://* )
938			scm_type="${BRINGOVER_SCM}"
939			;;
940		*)	scm_type="none"
941			;;
942		esac
943	fi
944
945	# fold both unsupported and unrecognized results into "none"
946	case "$scm_type" in
947	mercurial|git)
948		;;
949	*)	scm_type=none
950		;;
951	esac
952
953	echo $scm_type
954}
955
956# Echo the SCM types of $CODEMGR_WS and $BRINGOVER_WS
957function child_wstype {
958	typeset scm_type junk
959
960	# Probe CODEMGR_WS to determine its type
961	if [[ -d $CODEMGR_WS ]]; then
962		$WHICH_SCM | read scm_type junk || exit 1
963	fi
964
965	case "$scm_type" in
966	none|git|mercurial)
967		;;
968	*)	scm_type=none
969		;;
970	esac
971
972	echo $scm_type
973}
974
975# create directories that are automatically removed if the nightly script
976# fails to start correctly
977function newdir {
978	dir=$1
979	toadd=
980	while [ ! -d $dir ]; do
981		toadd="$dir $toadd"
982		dir=`dirname $dir`
983	done
984	torm=
985	newlist=
986	for dir in $toadd; do
987		if staffer mkdir $dir; then
988			newlist="$ISUSER $dir $newlist"
989			torm="$dir $torm"
990		else
991			[ -z "$torm" ] || staffer rmdir $torm
992			return 1
993		fi
994	done
995	newdirlist="$newlist $newdirlist"
996	return 0
997}
998newdirlist=
999
1000# Initialize the git repo before creating the log subdir; "git clone" insists
1001# that a preexisting directory be empty.
1002# Use --reference-if-able to share most of the parent's .git tree.
1003type init_git > /dev/null 2>&1 || function init_git {
1004	if [ -d "${BRINGOVER_WS}" ]; then
1005		REF_WS="--reference-if-able $(git -C ${BRINGOVER_WS} rev-parse --path-format=absolute --git-common-dir)"
1006	fi
1007	staffer git clone \
1008		--no-checkout \
1009		${CLONE_OPTIONS} \
1010		--origin ${BRINGOVER_REMOTE} \
1011		${REF_WS} \
1012		${BRINGOVER_WS} ${CODEMGR_WS}
1013}
1014
1015# All mercurial initialization is done in bringover_mercurial
1016type init_mercurial > /dev/null 2>&1 || function init_mercurial {
1017	newdir $CODEMGR_WS
1018}
1019
1020type init_none > /dev/null 2>&1 || function init_none {
1021	newdir $CODEMGR_WS
1022}
1023
1024function create_build_ws {
1025	PARENT_SCM_TYPE=$(parent_wstype)
1026
1027	eval "init_${PARENT_SCM_TYPE}" 2>&1
1028}
1029
1030[ -d $CODEMGR_WS ] || create_build_ws || exit 1
1031
1032# since this script assumes the build is from full source, it nullifies
1033# variables likely to have been set by a "ws" script; nullification
1034# confines the search space for headers and libraries to the proto area
1035# built from this immediate source.
1036ENVLDLIBS1=
1037ENVLDLIBS2=
1038ENVLDLIBS3=
1039ENVCPPFLAGS1=
1040ENVCPPFLAGS2=
1041ENVCPPFLAGS3=
1042ENVCPPFLAGS4=
1043PARENT_ROOT=
1044
1045export ENVLDLIBS3 ENVCPPFLAGS1 ENVCPPFLAGS2 ENVCPPFLAGS3 ENVCPPFLAGS4 \
1046	ENVLDLIBS1 ENVLDLIBS2 PARENT_ROOT
1047
1048PKGARCHIVE_ORIG=$PKGARCHIVE
1049
1050#
1051# Juggle the logs and optionally send mail on completion.
1052#
1053
1054function logshuffle {
1055	LLOG="$ATLOG/log.`date '+%F.%H:%M'`"
1056	if [ -f $LLOG -o -d $LLOG ]; then
1057		LLOG=$LLOG.$$
1058	fi
1059
1060	rm -f "$ATLOG/latest" 2>/dev/null
1061	mkdir -p $LLOG
1062	export LLOG
1063
1064	if [ "$build_ok" = "y" ]; then
1065		mv $ATLOG/proto_list_${MACH} $LLOG
1066
1067		if [ -f $ATLOG/proto_list_tools_${MACH} ]; then
1068			mv $ATLOG/proto_list_tools_${MACH} $LLOG
1069	        fi
1070
1071		if [ -f $TMPDIR/wsdiff.results ]; then
1072			mv $TMPDIR/wsdiff.results $LLOG
1073		fi
1074
1075		if [ -f $TMPDIR/wsdiff-nd.results ]; then
1076			mv $TMPDIR/wsdiff-nd.results $LLOG
1077		fi
1078	fi
1079
1080	#
1081	# Now that we're about to send mail, it's time to check the noise
1082	# file.  In the event that an error occurs beyond this point, it will
1083	# be recorded in the nightly.log file, but nowhere else.  This would
1084	# include only errors that cause the copying of the noise log to fail
1085	# or the mail itself not to be sent.
1086	#
1087
1088	exec >>$LOGFILE 2>&1
1089	if [ -s $build_noise_file ]; then
1090		echo "\n==== Nightly build noise ====\n" |
1091		    tee -a $LOGFILE >>$mail_msg_file
1092		cat $build_noise_file >>$LOGFILE
1093		cat $build_noise_file >>$mail_msg_file
1094		echo | tee -a $LOGFILE >>$mail_msg_file
1095	fi
1096	rm -f $build_noise_file
1097
1098	case "$build_ok" in
1099		y)
1100			state=Completed
1101			;;
1102		i)
1103			state=Interrupted
1104			;;
1105		*)
1106			state=Failed
1107			;;
1108	esac
1109
1110	if [[ $state != "Interrupted" && $build_extras_ok != "y" ]]; then
1111		state=Failed
1112	fi
1113
1114	NIGHTLY_STATUS=$state
1115	export NIGHTLY_STATUS
1116
1117	run_hook POST_NIGHTLY $state
1118	run_hook SYS_POST_NIGHTLY $state
1119
1120	#
1121	# mailx(1) sets From: based on the -r flag
1122	# if it is given.
1123	#
1124	mailx_r=
1125	if [[ -n "${MAILFROM}" ]]; then
1126		mailx_r="-r ${MAILFROM}"
1127	fi
1128
1129	cat $build_time_file $build_environ_file $mail_msg_file \
1130	    > ${LLOG}/mail_msg
1131	if [ "$m_FLAG" = "y" ]; then
1132		/usr/bin/mailx ${mailx_r} -s \
1133		    "Nightly ${MACH} Build of ${basews} ${state}." \
1134		    "${MAILTO}" < "${LLOG}/mail_msg"
1135	fi
1136
1137	if [ "$u_FLAG" = "y" -a "$build_ok" = "y" ]; then
1138		staffer cp ${LLOG}/mail_msg $PARENT_WS/usr/src/mail_msg-${MACH}
1139		staffer cp $LOGFILE $PARENT_WS/usr/src/nightly-${MACH}.log
1140	fi
1141
1142	mv $LOGFILE $LLOG
1143
1144	ln -s "$LLOG" "$ATLOG/latest"
1145}
1146
1147#
1148#	Remove the locks and temporary files on any exit
1149#
1150function cleanup {
1151	logshuffle
1152
1153	[ -z "$lockfile" ] || staffer rm -f $lockfile
1154	[ -z "$atloglockfile" ] || rm -f $atloglockfile
1155	[ -z "$ulockfile" ] || staffer rm -f $ulockfile
1156	[ -z "$Ulockfile" ] || rm -f $Ulockfile
1157
1158	set -- $newdirlist
1159	while [ $# -gt 0 ]; do
1160		ISUSER=$1 staffer rmdir $2
1161		shift; shift
1162	done
1163	rm -rf $TMPDIR
1164}
1165
1166function cleanup_signal {
1167	build_ok=i
1168	# this will trigger cleanup(), above.
1169	exit 1
1170}
1171
1172trap cleanup 0
1173trap cleanup_signal 1 2 3 15
1174
1175#
1176# Generic lock file processing -- make sure that the lock file doesn't
1177# exist.  If it does, it should name the build host and PID.  If it
1178# doesn't, then make sure we can create it.  Clean up locks that are
1179# known to be stale (assumes host name is unique among build systems
1180# for the workspace).
1181#
1182function create_lock {
1183	lockf=$1
1184	lockvar=$2
1185
1186	ldir=`dirname $lockf`
1187	[ -d $ldir ] || newdir $ldir || exit 1
1188	eval $lockvar=$lockf
1189
1190	while ! staffer ln -s $hostname.$STAFFER.$$ $lockf 2> /dev/null; do
1191		ls -l $lockf | nawk '{print $NF}' | IFS=. read host user pid
1192		if [ "$host" != "$hostname" ]; then
1193			echo "$MACH build of $basews apparently" \
1194			    "already started by $user on $host as $pid."
1195			exit 1
1196		elif kill -s 0 $pid 2>/dev/null; then
1197			echo "$MACH build of $basews already started" \
1198			    "by $user as $pid."
1199			exit 1
1200		else
1201			# stale lock; clear it out and try again
1202			rm -f $lockf
1203		fi
1204	done
1205}
1206
1207#
1208# Return the list of interesting proto areas, depending on the current
1209# options.
1210#
1211function allprotos {
1212	typeset roots="$ROOT"
1213
1214	if [[ "$F_FLAG" == n && "$MULTI_PROTO" == yes ]]; then
1215		roots="$roots $ROOT-nd"
1216	fi
1217
1218	echo $roots
1219}
1220
1221# Ensure no other instance of this script is running on this host.
1222# LOCKNAME can be set in <env_file>, and is by default, but is not
1223# required due to the use of $ATLOG below.
1224if [ -n "$LOCKNAME" ]; then
1225	create_lock /tmp/$LOCKNAME "lockfile"
1226fi
1227#
1228# Create from one, two, or three other locks:
1229#	$ATLOG/nightly.lock
1230#		- protects against multiple builds in same workspace
1231#	$PARENT_WS/usr/src/nightly.$MACH.lock
1232#		- protects against multiple 'u' copy-backs
1233#	$NIGHTLY_PARENT_ROOT/nightly.lock
1234#		- protects against multiple 'U' copy-backs
1235#
1236# Overriding ISUSER to 1 causes the lock to be created as root if the
1237# script is run as root.  The default is to create it as $STAFFER.
1238ISUSER=1 create_lock $ATLOG/nightly.lock "atloglockfile"
1239if [ "$u_FLAG" = "y" ]; then
1240	create_lock $PARENT_WS/usr/src/nightly.$MACH.lock "ulockfile"
1241fi
1242if [ "$U_FLAG" = "y" ]; then
1243	# NIGHTLY_PARENT_ROOT is written as root if script invoked as root.
1244	ISUSER=1 create_lock $NIGHTLY_PARENT_ROOT/nightly.lock "Ulockfile"
1245fi
1246
1247# Locks have been taken, so we're doing a build and we're committed to
1248# the directories we may have created so far.
1249newdirlist=
1250
1251#
1252# Create mail_msg_file
1253#
1254mail_msg_file="${TMPDIR}/mail_msg"
1255touch $mail_msg_file
1256build_time_file="${TMPDIR}/build_time"
1257build_environ_file="${TMPDIR}/build_environ"
1258touch $build_environ_file
1259#
1260#	Move old LOGFILE aside
1261#	ATLOG directory already made by 'create_lock' above
1262#
1263if [ -f $LOGFILE ]; then
1264	mv -f $LOGFILE ${LOGFILE}-
1265fi
1266#
1267#	Build OsNet source
1268#
1269START_DATE=`date`
1270SECONDS=0
1271echo "\n==== Nightly $maketype build started:   $START_DATE ====" \
1272    | tee -a $LOGFILE > $build_time_file
1273
1274echo "\nBuild project:  $build_project\nBuild taskid:   $build_taskid" | \
1275    tee -a $mail_msg_file >> $LOGFILE
1276
1277# make sure we log only to the nightly build file
1278build_noise_file="${TMPDIR}/build_noise"
1279exec </dev/null >$build_noise_file 2>&1
1280
1281run_hook SYS_PRE_NIGHTLY
1282run_hook PRE_NIGHTLY
1283
1284echo "\n==== list of environment variables ====\n" >> $LOGFILE
1285env >> $LOGFILE
1286
1287echo "\n==== Nightly argument issues ====\n" | tee -a $mail_msg_file >> $LOGFILE
1288
1289if [ "$N_FLAG" = "y" ]; then
1290	if [ "$p_FLAG" = "y" ]; then
1291		cat <<EOF | tee -a $mail_msg_file >> $LOGFILE
1292WARNING: the p option (create packages) is set, but so is the N option (do
1293         not run protocmp); this is dangerous; you should unset the N option
1294EOF
1295	else
1296		cat <<EOF | tee -a $mail_msg_file >> $LOGFILE
1297Warning: the N option (do not run protocmp) is set; it probably shouldn't be
1298EOF
1299	fi
1300	echo "" | tee -a $mail_msg_file >> $LOGFILE
1301fi
1302
1303if [ "$f_FLAG" = "y" ]; then
1304	if [ "$i_FLAG" = "y" ]; then
1305		echo "WARNING: the -f flag cannot be used during incremental" \
1306		    "builds; ignoring -f\n" | tee -a $mail_msg_file >> $LOGFILE
1307		f_FLAG=n
1308	fi
1309	if [ "${p_FLAG}" != "y" ]; then
1310		echo "WARNING: the -f flag requires -p;" \
1311		    "ignoring -f\n" | tee -a $mail_msg_file >> $LOGFILE
1312		f_FLAG=n
1313	fi
1314fi
1315
1316if [ "$w_FLAG" = "y" -a ! -d $ROOT ]; then
1317	echo "WARNING: -w specified, but $ROOT does not exist;" \
1318	    "ignoring -w\n" | tee -a $mail_msg_file >> $LOGFILE
1319	w_FLAG=n
1320fi
1321
1322case $MULTI_PROTO in
1323yes|no)	;;
1324*)
1325	echo "WARNING: MULTI_PROTO is \"$MULTI_PROTO\"; " \
1326	    "should be \"yes\" or \"no\"." | tee -a $mail_msg_file >> $LOGFILE
1327	echo "Setting MULTI_PROTO to \"no\".\n" | \
1328	    tee -a $mail_msg_file >> $LOGFILE
1329	export MULTI_PROTO=no
1330	;;
1331esac
1332
1333# Save the current proto area if we're comparing against the last build
1334if [ "$w_FLAG" = "y" -a -d "$ROOT" ]; then
1335    if [ -d "$ROOT.prev" ]; then
1336	rm -rf $ROOT.prev
1337    fi
1338    mv $ROOT $ROOT.prev
1339fi
1340
1341# Same for non-DEBUG proto area
1342if [ "$w_FLAG" = "y" -a "$MULTI_PROTO" = yes -a -d "$ROOT-nd" ]; then
1343	if [ -d "$ROOT-nd.prev" ]; then
1344		rm -rf $ROOT-nd.prev
1345	fi
1346	mv $ROOT-nd $ROOT-nd.prev
1347fi
1348
1349SCM_TYPE=$(child_wstype)
1350
1351#
1352#	Decide whether to clobber
1353#
1354if [ "$i_FLAG" = "n" -a -d "$SRC" ]; then
1355	echo "\n==== Make clobber at `date` ====\n" >> $LOGFILE
1356
1357	cd $SRC
1358	# remove old clobber file
1359	rm -f $SRC/clobber.out
1360	rm -f $SRC/clobber-${MACH}.out
1361
1362	# Remove all .make.state* files, just in case we are restarting
1363	# the build after having interrupted a previous 'make clobber'.
1364	find . \( -name SCCS -o -name .hg -o -name .svn -o -name .git \
1365		-o -name 'interfaces.*' \) -prune \
1366		-o -name '.make.*' -print | xargs rm -f
1367
1368	$MAKE -ek clobber 2>&1 | tee -a $SRC/clobber-${MACH}.out >> $LOGFILE
1369	echo "\n==== Make clobber ERRORS ====\n" >> $mail_msg_file
1370	grep "$MAKE:" $SRC/clobber-${MACH}.out |
1371		egrep -v ": (Entering|Leaving) directory " | \
1372		egrep -v "Ignoring unknown host" | \
1373		tee $TMPDIR/clobber_errs >> $mail_msg_file
1374
1375	if [[ -s $TMPDIR/clobber_errs ]]; then
1376		build_extras_ok=n
1377	fi
1378
1379	if [[ "$t_FLAG" == "y" ]]; then
1380		echo "\n==== Make tools clobber at `date` ====\n" >> $LOGFILE
1381		cd ${TOOLS}
1382		rm -f ${TOOLS}/clobber-${MACH}.out
1383		$MAKE TOOLS_PROTO=$TOOLS_PROTO -ek clobber 2>&1 | \
1384			tee -a ${TOOLS}/clobber-${MACH}.out >> $LOGFILE
1385		echo "\n==== Make tools clobber ERRORS ====\n" \
1386			>> $mail_msg_file
1387		grep "$MAKE:" ${TOOLS}/clobber-${MACH}.out \
1388			| egrep -v ": (Entering|Leaving) directory " \
1389			>> $mail_msg_file
1390		if (( $? == 0 )); then
1391			build_extras_ok=n
1392		fi
1393		rm -rf ${TOOLS_PROTO}
1394		mkdir -p ${TOOLS_PROTO}
1395	fi
1396
1397	typeset roots=$(allprotos)
1398	echo "\n\nClearing $roots" >> "$LOGFILE"
1399	rm -rf $roots
1400
1401	# Get back to a clean workspace as much as possible to catch
1402	# problems that only occur on fresh workspaces.
1403	# Remove all .make.state* files, libraries, and .o's that may
1404	# have been omitted from clobber.  A couple of libraries are
1405	# under source code control, so leave them alone.
1406	# We should probably blow away temporary directories too.
1407	cd $SRC
1408	find $relsrcdirs \( -name SCCS -o -name .hg -o -name .svn \
1409	    -o -name .git -o -name 'interfaces.*' \) -prune -o \
1410	    \( -name '.make.*' -o -name 'lib*.a' -o -name 'lib*.so*' -o \
1411	       -name '*.o' \) -print | \
1412	    grep -v 'tools/ctf/dwarf/.*/libdwarf' | xargs rm -f
1413else
1414	echo "\n==== No clobber at `date` ====\n" >> $LOGFILE
1415fi
1416
1417type bringover_git > /dev/null 2>&1 || function bringover_git {
1418	typeset -x PATH=$PATH
1419
1420	if [ "$BRINGOVER_BRANCH" = "" ]; then
1421		if [ -d "$BRINGOVER_WS" ]; then
1422			BRINGOVER_BRANCH=$(cd "${BRINGOVER_WS}"; git rev-parse --abbrev-ref HEAD)
1423			if [ "$BRINGOVER_BRANCH" = "HEAD" ]; then
1424				printf "%s: can't determine BRINGOVER_BRANCH from repo in 'detached HEAD' state\n" "$BRINGOVER_WS"
1425				touch $TMPDIR/bringover_failed
1426				return
1427			fi
1428		else
1429			printf "%s: can't determine BRINGOVER_BRANCH\n" "$BRINGOVER_WS"
1430			touch $TMPDIR/bringover_failed
1431			return
1432		fi
1433	fi
1434	(cd ${CODEMGR_WS} &&
1435		staffer git remote set-url ${BRINGOVER_REMOTE} ${BRINGOVER_WS} &&
1436		staffer git fetch ${BRINGOVER_REMOTE} ${BRINGOVER_BRANCH} &&
1437		staffer git switch --force-create ${BRINGOVER_BRANCH} \
1438			${BRINGOVER_REMOTE}/${BRINGOVER_BRANCH}) \
1439		    >$TMPDIR/bringover.out 2>&1
1440	if (( $? != 0 )); then
1441		printf "%s: update failed as follows:\n\n" "$CODEMGR_WS"
1442		cat $TMPDIR/bringover.out
1443		touch $TMPDIR/bringover_failed
1444		return
1445	fi
1446
1447	printf "%s: local branch '%s' updated to commit %s\n"	\
1448		"$CODEMGR_WS" "$BRINGOVER_BRANCH"		\
1449		$(cd ${CODEMGR_WS}; git rev-parse HEAD)
1450
1451	staffer git status --no-short --branch --untracked-files=no
1452}
1453
1454type bringover_mercurial > /dev/null 2>&1 || function bringover_mercurial {
1455	typeset -x PATH=$PATH
1456
1457	# If the repository doesn't exist yet, then we want to populate it.
1458	if [[ ! -d $CODEMGR_WS/.hg ]]; then
1459		staffer hg init $CODEMGR_WS
1460		staffer echo "[paths]" > $CODEMGR_WS/.hg/hgrc
1461		staffer echo "default=$BRINGOVER_WS" >> $CODEMGR_WS/.hg/hgrc
1462		touch $TMPDIR/new_repository
1463	fi
1464
1465	typeset -x HGMERGE="/bin/false"
1466
1467	#
1468	# If the user has changes, regardless of whether those changes are
1469	# committed, and regardless of whether those changes conflict, then
1470	# we'll attempt to merge them either implicitly (uncommitted) or
1471	# explicitly (committed).
1472	#
1473	# These are the messages we'll use to help clarify mercurial output
1474	# in those cases.
1475	#
1476	typeset mergefailmsg="\
1477***\n\
1478*** nightly was unable to automatically merge your changes.  You should\n\
1479*** redo the full merge manually, following the steps outlined by mercurial\n\
1480*** above, then restart nightly.\n\
1481***\n"
1482	typeset mergepassmsg="\
1483***\n\
1484*** nightly successfully merged your changes.  This means that your working\n\
1485*** directory has been updated, but those changes are not yet committed.\n\
1486*** After nightly completes, you should validate the results of the merge,\n\
1487*** then use hg commit manually.\n\
1488***\n"
1489
1490	#
1491	# For each repository in turn:
1492	#
1493	# 1. Do the pull.  If this fails, dump the output and bail out.
1494	#
1495	# 2. If the pull resulted in an extra head, do an explicit merge.
1496	#    If this fails, dump the output and bail out.
1497	#
1498	# Because we can't rely on Mercurial to exit with a failure code
1499	# when a merge fails (Mercurial issue #186), we must grep the
1500	# output of pull/merge to check for attempted and/or failed merges.
1501	#
1502	# 3. If a merge failed, set the message and fail the bringover.
1503	#
1504	# 4. Otherwise, if a merge succeeded, set the message
1505	#
1506	# 5. Dump the output, and any message from step 3 or 4.
1507	#
1508
1509	typeset HG_SOURCE=$BRINGOVER_WS
1510	if [ ! -f $TMPDIR/new_repository ]; then
1511		HG_SOURCE=$TMPDIR/open_bundle.hg
1512		staffer hg --cwd $CODEMGR_WS incoming --bundle $HG_SOURCE \
1513		    -v $BRINGOVER_WS > $TMPDIR/incoming_open.out
1514
1515		#
1516		# If there are no incoming changesets, then incoming will
1517		# fail, and there will be no bundle file.  Reset the source,
1518		# to allow the remaining logic to complete with no false
1519		# negatives.  (Unlike incoming, pull will return success
1520		# for the no-change case.)
1521		#
1522		if (( $? != 0 )); then
1523			HG_SOURCE=$BRINGOVER_WS
1524		fi
1525	fi
1526
1527	staffer hg --cwd $CODEMGR_WS pull -u $HG_SOURCE \
1528	    > $TMPDIR/pull_open.out 2>&1
1529	if (( $? != 0 )); then
1530		printf "%s: pull failed as follows:\n\n" "$CODEMGR_WS"
1531		cat $TMPDIR/pull_open.out
1532		if grep "^merging.*failed" $TMPDIR/pull_open.out > /dev/null 2>&1; then
1533			printf "$mergefailmsg"
1534		fi
1535		touch $TMPDIR/bringover_failed
1536		return
1537	fi
1538
1539	if grep "not updating" $TMPDIR/pull_open.out > /dev/null 2>&1; then
1540		staffer hg --cwd $CODEMGR_WS merge \
1541		    >> $TMPDIR/pull_open.out 2>&1
1542		if (( $? != 0 )); then
1543			printf "%s: merge failed as follows:\n\n" \
1544			    "$CODEMGR_WS"
1545			cat $TMPDIR/pull_open.out
1546			if grep "^merging.*failed" $TMPDIR/pull_open.out \
1547			    > /dev/null 2>&1; then
1548				printf "$mergefailmsg"
1549			fi
1550			touch $TMPDIR/bringover_failed
1551			return
1552		fi
1553	fi
1554
1555	printf "updated %s with the following results:\n" "$CODEMGR_WS"
1556	cat $TMPDIR/pull_open.out
1557	if grep "^merging" $TMPDIR/pull_open.out >/dev/null 2>&1; then
1558		printf "$mergepassmsg"
1559	fi
1560	printf "\n"
1561
1562	#
1563	# Per-changeset output is neither useful nor manageable for a
1564	# newly-created repository.
1565	#
1566	if [ -f $TMPDIR/new_repository ]; then
1567		return
1568	fi
1569
1570	printf "\nadded the following changesets to open repository:\n"
1571	cat $TMPDIR/incoming_open.out
1572}
1573
1574type bringover_none > /dev/null 2>&1 || function bringover_none {
1575	echo "Couldn't figure out what kind of SCM to use for $BRINGOVER_WS."
1576	touch $TMPDIR/bringover_failed
1577}
1578
1579#
1580#	Decide whether to bringover to the codemgr workspace
1581#
1582if [ "$n_FLAG" = "n" ]; then
1583	PARENT_SCM_TYPE=$(parent_wstype)
1584
1585	if [[ $SCM_TYPE != none && $SCM_TYPE != $PARENT_SCM_TYPE ]]; then
1586		echo "cannot bringover from $PARENT_SCM_TYPE to $SCM_TYPE, " \
1587		    "quitting at `date`." | tee -a $mail_msg_file >> $LOGFILE
1588		exit 1
1589	fi
1590
1591	run_hook PRE_BRINGOVER
1592
1593	echo "\n==== bringover to $CODEMGR_WS at `date` ====\n" >> $LOGFILE
1594	echo "\n==== BRINGOVER LOG ====\n" >> $mail_msg_file
1595
1596	eval "bringover_${PARENT_SCM_TYPE}" 2>&1 |
1597		tee -a $mail_msg_file >> $LOGFILE
1598
1599	if [ -f $TMPDIR/bringover_failed ]; then
1600		rm -f $TMPDIR/bringover_failed
1601		build_ok=n
1602		echo "trouble with bringover, quitting at `date`." |
1603			tee -a $mail_msg_file >> $LOGFILE
1604		exit 1
1605	fi
1606
1607	#
1608	# It's possible that we used the bringover above to create
1609	# $CODEMGR_WS.  If so, then SCM_TYPE was previously "none,"
1610	# but should now be the same as $BRINGOVER_WS.
1611	#
1612	[[ $SCM_TYPE == none ]] && SCM_TYPE=$PARENT_SCM_TYPE
1613
1614	run_hook POST_BRINGOVER
1615
1616	check_closed_bins
1617
1618else
1619	echo "\n==== No bringover to $CODEMGR_WS ====\n" >> $LOGFILE
1620fi
1621
1622# Safeguards
1623[[ -v CODEMGR_WS ]] || fatal_error "Error: Variable CODEMGR_WS not set."
1624[[ -d "${CODEMGR_WS}" ]] || fatal_error "Error: ${CODEMGR_WS} is not a directory."
1625[[ -f "${CODEMGR_WS}/usr/src/Makefile" ]] || fatal_error "Error: ${CODEMGR_WS}/usr/src/Makefile not found."
1626
1627#
1628# if -V flag was given, reset VERSION to V_ARG
1629#
1630if [[ "$V_FLAG" == "y" ]]; then
1631	export VERSION=$V_ARG
1632elif [[ -z "$VERSION" ]]; then
1633	export VERSION=$(git -C "${CODEMGR_WS}" describe --long --all --dirty |
1634		cut -d/ -f2-)
1635fi
1636
1637[[ -n "$VERSION" ]] || fatal_error "Error: VERSION not set"
1638
1639echo "\n==== Build version ====\n" | tee -a $mail_msg_file >> $LOGFILE
1640echo $VERSION | tee -a $mail_msg_file >> $LOGFILE
1641
1642if [[ "$t_FLAG" == "y" ]]; then
1643	set_non_debug_build_flags
1644	# Switch ONBLD_TOOLS early if -t is specified so that
1645	# we could use bootstrapped cw for compiler checks.
1646	ONBLD_TOOLS=${TOOLS_PROTO}/opt/onbld
1647	export ONBLD_TOOLS
1648
1649	bootstrap_tools || fatal_error "Error: could not bootstrap tools"
1650fi
1651
1652echo "\n==== Build environment ====\n" | tee -a $build_environ_file >> $LOGFILE
1653
1654# System
1655whence uname | tee -a $build_environ_file >> $LOGFILE
1656uname -a 2>&1 | tee -a $build_environ_file >> $LOGFILE
1657echo | tee -a $build_environ_file >> $LOGFILE
1658
1659# make
1660whence $MAKE | tee -a $build_environ_file >> $LOGFILE
1661$MAKE -v | tee -a $build_environ_file >> $LOGFILE
1662echo "number of concurrent jobs = $DMAKE_MAX_JOBS" |
1663    tee -a $build_environ_file >> $LOGFILE
1664
1665#
1666# Report the compiler versions.
1667#
1668
1669if [[ ! -f $SRC/Makefile ]]; then
1670	build_ok=n
1671	echo "\nUnable to find \"Makefile\" in $SRC." | \
1672	    tee -a $build_environ_file >> $LOGFILE
1673	exit 1
1674fi
1675
1676( cd $SRC
1677  for target in cc-version java-version openssl-version; do
1678	echo
1679	#
1680	# Put statefile somewhere we know we can write to rather than trip
1681	# over a read-only $srcroot.
1682	#
1683	rm -f $TMPDIR/make-state
1684	export SRC
1685	if $MAKE -K $TMPDIR/make-state -e $target 2>/dev/null; then
1686		continue
1687	fi
1688	touch $TMPDIR/nocompiler
1689  done
1690  echo
1691) | egrep -v "${MAKE}: (Entering|Leaving) directory " \
1692    | tee -a $build_environ_file >> $LOGFILE
1693
1694if [ -f $TMPDIR/nocompiler ]; then
1695	rm -f $TMPDIR/nocompiler
1696	build_ok=n
1697	echo "Aborting due to missing compiler." |
1698		tee -a $build_environ_file >> $LOGFILE
1699	exit 1
1700fi
1701
1702# Check that we're running a capable link-editor
1703whence ld | tee -a $build_environ_file >> $LOGFILE
1704LDVER=`ld -V 2>&1`
1705echo $LDVER | tee -a $build_environ_file >> $LOGFILE
1706LDVER=`echo $LDVER | sed -e "s/.*-1\.\([0-9]*\).*/\1/"`
1707if [ `expr $LDVER \< 422` -eq 1 ]; then
1708	echo "The link-editor needs to be at version 422 or higher to build" | \
1709	    tee -a $build_environ_file >> $LOGFILE
1710	echo "the latest stuff.  Hope your build works." | \
1711	    tee -a $build_environ_file >> $LOGFILE
1712fi
1713
1714#
1715# Build and use the workspace's tools if requested
1716#
1717if [[ "$t_FLAG" == "y" ]]; then
1718	set_non_debug_build_flags
1719
1720	build_tools ${TOOLS_PROTO}
1721	if (( $? != 0 )); then
1722		build_ok=n
1723		exit 1
1724	fi
1725
1726	STABS=${TOOLS_PROTO}/opt/onbld/bin/${MACH}/stabs
1727	export STABS
1728	CTFSTABS=${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfstabs
1729	export CTFSTABS
1730	GENOFFSETS=${TOOLS_PROTO}/opt/onbld/bin/genoffsets
1731	export GENOFFSETS
1732
1733	CTFCONVERT=${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfconvert
1734	export CTFCONVERT
1735	CTFMERGE=${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfmerge
1736	export CTFMERGE
1737
1738	PATH="${TOOLS_PROTO}/opt/onbld/bin/${MACH}:${PATH}"
1739	PATH="${TOOLS_PROTO}/opt/onbld/bin:${PATH}"
1740	export PATH
1741
1742	echo "\n==== New environment settings. ====\n" >> $LOGFILE
1743	echo "STABS=${STABS}" >> $LOGFILE
1744	echo "CTFSTABS=${CTFSTABS}" >> $LOGFILE
1745	echo "CTFCONVERT=${CTFCONVERT}" >> $LOGFILE
1746	echo "CTFMERGE=${CTFMERGE}" >> $LOGFILE
1747	echo "PATH=${PATH}" >> $LOGFILE
1748	echo "ONBLD_TOOLS=${ONBLD_TOOLS}" >> $LOGFILE
1749fi
1750
1751# timestamp the start of the normal build; the findunref tool uses it.
1752touch $SRC/.build.tstamp
1753
1754normal_build
1755
1756ORIG_SRC=$SRC
1757BINARCHIVE=${CODEMGR_WS}/bin-${MACH}.cpio.Z
1758
1759abspkg=
1760for d in $abssrcdirs; do
1761	if [ -d "$d/pkg" ]; then
1762		abspkg="$abspkg $d"
1763	fi
1764done
1765
1766if [ "$L_FLAG" != "y" -a "$build_ok" = y ]; then
1767	echo "\n==== Linting packages ====\n" | \
1768	    tee -a $LOGFILE >> $mail_msg_file
1769
1770	if [ -n "$abspkg" ]; then
1771		for d in "$abspkg"; do
1772			( cd $d/pkg ; $MAKE -e pkglint ) | \
1773			    tee -a $LOGFILE | \
1774			    egrep -v ": (Entering|Leaving) directory " | \
1775			    egrep -v 'Lint engine setup|Starting lint run'
1776		done 2>&1 | tee $TMPDIR/pkglint_noise >> $mail_msg_file
1777		if [[ -s $TMPDIR/pkglint_noise ]]; then
1778			build_extras_ok=n
1779		fi
1780	fi
1781fi
1782
1783#
1784# There are several checks that need to look at the proto area, but
1785# they only need to look at one, and they don't care whether it's
1786# DEBUG or non-DEBUG.
1787#
1788if [[ "$MULTI_PROTO" == yes && "$D_FLAG" == n ]]; then
1789	checkroot=$ROOT-nd
1790else
1791	checkroot=$ROOT
1792fi
1793
1794if [ "$build_ok" = "y" ]; then
1795	echo "\n==== Creating protolist system file at `date` ====" \
1796		>> $LOGFILE
1797	protolist $checkroot > $ATLOG/proto_list_${MACH}
1798	echo "==== protolist system file created at `date` ====\n" \
1799		>> $LOGFILE
1800
1801	if [ "$N_FLAG" != "y" ]; then
1802
1803		E1=
1804		f1=
1805		for f in $f1; do
1806			if [ -f "$f" ]; then
1807				E1="$E1 -e $f"
1808			fi
1809		done
1810
1811		E2=
1812		f2=
1813		if [ -d "$SRC/pkg" ]; then
1814			f2="$f2 exceptions/packaging"
1815		fi
1816
1817		for f in $f2; do
1818			if [ -f "$f" ]; then
1819				E2="$E2 -e $f"
1820			fi
1821		done
1822	fi
1823
1824	if [ "$N_FLAG" != "y" -a -d $SRC/pkg ]; then
1825		echo "\n==== Validating manifests against proto area ====\n" \
1826		    >> $mail_msg_file
1827		( cd $SRC/pkg ; $MAKE -e protocmp ROOT="$checkroot" ) | \
1828		    egrep -v ": (Entering|Leaving) directory " | \
1829		    tee $TMPDIR/protocmp_noise >> $mail_msg_file
1830		if [[ -s $TMPDIR/protocmp_noise ]]; then
1831			build_extras_ok=n
1832		fi
1833	fi
1834
1835	if [ "$N_FLAG" != "y" -a -f "$REF_PROTO_LIST" ]; then
1836		echo "\n==== Impact on proto area ====\n" >> $mail_msg_file
1837		if [ -n "$E2" ]; then
1838			ELIST=$E2
1839		else
1840			ELIST=$E1
1841		fi
1842		$PROTOCMPTERSE \
1843			"Files in yesterday's proto area, but not today's:" \
1844			"Files in today's proto area, but not yesterday's:" \
1845			"Files that changed between yesterday and today:" \
1846			${ELIST} \
1847			-d $REF_PROTO_LIST \
1848			$ATLOG/proto_list_${MACH} \
1849			>> $mail_msg_file
1850	fi
1851fi
1852
1853if [[ "$u_FLAG" == "y" && "$build_ok" == "y" && \
1854    "$build_extras_ok" == "y" ]]; then
1855	staffer cp $ATLOG/proto_list_${MACH} \
1856		$PARENT_WS/usr/src/proto_list_${MACH}
1857fi
1858
1859# Update parent proto area if necessary. This is done now
1860# so that the proto area has either DEBUG or non-DEBUG kernels.
1861# Note that this clears out the lock file, so we can dispense with
1862# the variable now.
1863if [ "$U_FLAG" = "y" -a "$build_ok" = "y" ]; then
1864	echo "\n==== Copying proto area to $NIGHTLY_PARENT_ROOT ====\n" | \
1865	    tee -a $LOGFILE >> $mail_msg_file
1866	rm -rf $NIGHTLY_PARENT_ROOT/*
1867	unset Ulockfile
1868	mkdir -p $NIGHTLY_PARENT_ROOT
1869	if [[ "$MULTI_PROTO" == no || "$D_FLAG" == y ]]; then
1870		( cd $ROOT; tar cf - . |
1871		    ( cd $NIGHTLY_PARENT_ROOT;  umask 0; tar xpf - ) ) 2>&1 |
1872		    tee -a $mail_msg_file >> $LOGFILE
1873	fi
1874	if [[ "$MULTI_PROTO" == yes && "$F_FLAG" == n ]]; then
1875		rm -rf $NIGHTLY_PARENT_ROOT-nd/*
1876		mkdir -p $NIGHTLY_PARENT_ROOT-nd
1877		cd $ROOT-nd
1878		( tar cf - . |
1879		    ( cd $NIGHTLY_PARENT_ROOT-nd; umask 0; tar xpf - ) ) 2>&1 |
1880		    tee -a $mail_msg_file >> $LOGFILE
1881	fi
1882	if [ -n "${NIGHTLY_PARENT_TOOLS_ROOT}" ]; then
1883		echo "\n==== Copying tools proto area to $NIGHTLY_PARENT_TOOLS_ROOT ====\n" | \
1884		    tee -a $LOGFILE >> $mail_msg_file
1885		rm -rf $NIGHTLY_PARENT_TOOLS_ROOT/*
1886		mkdir -p $NIGHTLY_PARENT_TOOLS_ROOT
1887		if [[ "$MULTI_PROTO" == no || "$D_FLAG" == y ]]; then
1888			( cd $TOOLS_PROTO; tar cf - . |
1889			    ( cd $NIGHTLY_PARENT_TOOLS_ROOT;
1890			    umask 0; tar xpf - ) ) 2>&1 |
1891			    tee -a $mail_msg_file >> $LOGFILE
1892		fi
1893	fi
1894fi
1895
1896#
1897# ELF verification: ABI (-A) and runtime (-r) checks
1898#
1899if [[ ($build_ok == y) && (($A_FLAG == y) || ($r_FLAG == y)) ]]; then
1900	# Directory ELF-data.$MACH holds the files produced by these tests.
1901	elf_ddir=$SRC/ELF-data.$MACH
1902
1903	# If there is a previous ELF-data backup directory, remove it. Then,
1904	# rotate current ELF-data directory into its place and create a new
1905	# empty directory
1906	rm -rf $elf_ddir.ref
1907	if [[ -d $elf_ddir ]]; then
1908		mv $elf_ddir $elf_ddir.ref
1909	fi
1910	mkdir -p $elf_ddir
1911
1912	# Call find_elf to produce a list of the ELF objects in the proto area.
1913	# This list is passed to check_rtime and interface_check, preventing
1914	# them from separately calling find_elf to do the same work twice.
1915	find_elf -fr $checkroot > $elf_ddir/object_list
1916
1917	if [[ $A_FLAG == y ]]; then
1918		echo "\n==== Check versioning and ABI information ====\n"  | \
1919		    tee -a $LOGFILE >> $mail_msg_file
1920
1921		# Produce interface description for the proto. Report errors.
1922		interface_check -o -w $elf_ddir -f object_list \
1923			-i interface -E interface.err
1924		if [[ -s $elf_ddir/interface.err ]]; then
1925			tee -a $LOGFILE < $elf_ddir/interface.err \
1926			    >> $mail_msg_file
1927			build_extras_ok=n
1928		fi
1929
1930		# If ELF_DATA_BASELINE_DIR is defined, compare the new interface
1931		# description file to that from the baseline gate. Issue a
1932		# warning if the baseline is not present, and keep going.
1933		if [[ "$ELF_DATA_BASELINE_DIR" != '' ]]; then
1934			base_ifile="$ELF_DATA_BASELINE_DIR/interface"
1935
1936			echo "\n==== Compare versioning and ABI information" \
1937			    "to baseline ====\n"  | \
1938			    tee -a $LOGFILE >> $mail_msg_file
1939			echo "Baseline:	 $base_ifile\n" >> $LOGFILE
1940
1941			if [[ -f $base_ifile ]]; then
1942				interface_cmp -d -o $base_ifile \
1943				    $elf_ddir/interface > $elf_ddir/interface.cmp
1944				if [[ -s $elf_ddir/interface.cmp ]]; then
1945					echo | tee -a $LOGFILE >> $mail_msg_file
1946					tee -a $LOGFILE < \
1947					    $elf_ddir/interface.cmp \
1948					    >> $mail_msg_file
1949					build_extras_ok=n
1950				fi
1951			else
1952				echo "baseline not available. comparison" \
1953                                    "skipped" | \
1954				    tee -a $LOGFILE >> $mail_msg_file
1955			fi
1956
1957		fi
1958	fi
1959
1960	if [[ $r_FLAG == y ]]; then
1961		echo "\n==== Check ELF runtime attributes ====\n" | \
1962		    tee -a $LOGFILE >> $mail_msg_file
1963
1964		# If we're doing a DEBUG build the proto area will be left
1965		# with debuggable objects, thus don't assert -s.
1966		if [[ $D_FLAG == y ]]; then
1967			rtime_sflag=""
1968		else
1969			rtime_sflag="-s"
1970		fi
1971		check_rtime -i -m -v $rtime_sflag -o -w $elf_ddir \
1972			-D object_list  -f object_list -E runtime.err \
1973			-I runtime.attr.raw
1974		if (( $? != 0 )); then
1975			build_extras_ok=n
1976		fi
1977
1978		# check_rtime -I output needs to be sorted in order to
1979		# compare it to that from previous builds.
1980		sort $elf_ddir/runtime.attr.raw > $elf_ddir/runtime.attr
1981		rm $elf_ddir/runtime.attr.raw
1982
1983		# Report errors
1984		if [[ -s $elf_ddir/runtime.err ]]; then
1985			tee -a $LOGFILE < $elf_ddir/runtime.err \
1986				>> $mail_msg_file
1987			build_extras_ok=n
1988		fi
1989
1990		# If there is an ELF-data directory from a previous build,
1991		# then diff the attr files. These files contain information
1992		# about dependencies, versioning, and runpaths. There is some
1993		# overlap with the ABI checking done above, but this also
1994		# flushes out non-ABI interface differences along with the
1995		# other information.
1996		echo "\n==== Diff ELF runtime attributes" \
1997		    "(since last build) ====\n" | \
1998		    tee -a $LOGFILE >> $mail_msg_file >> $mail_msg_file
1999
2000		if [[ -f $elf_ddir.ref/runtime.attr ]]; then
2001			diff $elf_ddir.ref/runtime.attr \
2002				$elf_ddir/runtime.attr \
2003				>> $mail_msg_file
2004		fi
2005	fi
2006
2007	# If -u set, copy contents of ELF-data.$MACH to the parent workspace.
2008	if [[ "$u_FLAG" == "y" ]]; then
2009		p_elf_ddir=$PARENT_WS/usr/src/ELF-data.$MACH
2010
2011		# If parent lacks the ELF-data.$MACH directory, create it
2012		if [[ ! -d $p_elf_ddir ]]; then
2013			staffer mkdir -p $p_elf_ddir
2014		fi
2015
2016		# These files are used asynchronously by other builds for ABI
2017		# verification, as above for the -A option. As such, we require
2018		# the file replacement to be atomic. Copy the data to a temp
2019		# file in the same filesystem and then rename into place.
2020		(
2021			cd $elf_ddir
2022			for elf_dfile in *; do
2023				staffer cp $elf_dfile \
2024				    ${p_elf_ddir}/${elf_dfile}.new
2025				staffer mv -f ${p_elf_ddir}/${elf_dfile}.new \
2026				    ${p_elf_ddir}/${elf_dfile}
2027			done
2028		)
2029	fi
2030fi
2031
2032# "make check" begins
2033
2034if [ "$i_CMD_LINE_FLAG" = "n" -a "$C_FLAG" = "y" ]; then
2035	# remove old check.out
2036	rm -f $SRC/check.out
2037
2038	rm -f $SRC/check-${MACH}.out
2039	cd $SRC
2040	$MAKE -ek check ROOT="$checkroot" 2>&1 | tee -a $SRC/check-${MACH}.out \
2041	    >> $LOGFILE
2042	echo "\n==== cstyle/hdrchk errors ====\n" >> $mail_msg_file
2043
2044	grep ":" $SRC/check-${MACH}.out |
2045		egrep -v ": (Entering|Leaving) directory " | \
2046		egrep -v "Ignoring unknown host" | \
2047		sort | uniq | tee $TMPDIR/check_errors >> $mail_msg_file
2048
2049	if [[ -s $TMPDIR/check_errors ]]; then
2050		build_extras_ok=n
2051	fi
2052else
2053	echo "\n==== No '$MAKE check' ====\n" >> $LOGFILE
2054fi
2055
2056echo "\n==== Find core files ====\n" | \
2057    tee -a $LOGFILE >> $mail_msg_file
2058
2059find $abssrcdirs -name core -a -type f -exec file {} \; | \
2060	tee -a $LOGFILE >> $mail_msg_file
2061
2062if [ "$f_FLAG" = "y" -a "$build_ok" = "y" ]; then
2063	echo "\n==== Diff unreferenced files (since last build) ====\n" \
2064	    | tee -a $LOGFILE >>$mail_msg_file
2065	rm -f $SRC/unref-${MACH}.ref
2066	if [ -f $SRC/unref-${MACH}.out ]; then
2067		mv $SRC/unref-${MACH}.out $SRC/unref-${MACH}.ref
2068	fi
2069
2070	findunref -S $SCM_TYPE -t $SRC/.build.tstamp -s usr $CODEMGR_WS \
2071	    ${TOOLS}/findunref/exception_list 2>> $mail_msg_file | \
2072	    sort > $SRC/unref-${MACH}.out
2073
2074	if [ ! -f $SRC/unref-${MACH}.ref ]; then
2075		cp $SRC/unref-${MACH}.out $SRC/unref-${MACH}.ref
2076	fi
2077
2078	diff $SRC/unref-${MACH}.ref $SRC/unref-${MACH}.out >>$mail_msg_file
2079fi
2080
2081# Verify that the usual lists of files, such as exception lists,
2082# contain only valid references to files.  If the build has failed,
2083# then don't check the proto area.
2084CHECK_PATHS=${CHECK_PATHS:-y}
2085if [ "$CHECK_PATHS" = y -a "$N_FLAG" != y ]; then
2086	echo "\n==== Check lists of files ====\n" | tee -a $LOGFILE \
2087		>>$mail_msg_file
2088	arg=-b
2089	[ "$build_ok" = y ] && arg=
2090	checkpaths $arg $checkroot > $SRC/check-paths.out 2>&1
2091	if [[ -s $SRC/check-paths.out ]]; then
2092		tee -a $LOGFILE < $SRC/check-paths.out >> $mail_msg_file
2093		build_extras_ok=n
2094	fi
2095fi
2096
2097if [ "$M_FLAG" != "y" -a "$build_ok" = y ]; then
2098	echo "\n==== Impact on file permissions ====\n" \
2099		>> $mail_msg_file
2100
2101	if [ -n "$abspkg" ]; then
2102		for d in "$abspkg"; do
2103			( cd $d/pkg ; $MAKE -e pmodes ) | \
2104			egrep -v ": (Entering|Leaving) directory " \
2105			>> $mail_msg_file
2106		done
2107	fi
2108fi
2109
2110if [ "$w_FLAG" = "y" -a "$build_ok" = "y" ]; then
2111	if [[ "$MULTI_PROTO" == no || "$D_FLAG" == y ]]; then
2112		do_wsdiff DEBUG $ROOT.prev $ROOT
2113	fi
2114
2115	if [[ "$MULTI_PROTO" == yes && "$F_FLAG" == n ]]; then
2116		do_wsdiff non-DEBUG $ROOT-nd.prev $ROOT-nd
2117	fi
2118fi
2119
2120END_DATE=`date`
2121echo "==== Nightly $maketype build completed: $END_DATE ====" | \
2122    tee -a $LOGFILE >> $build_time_file
2123
2124typeset -i10 hours
2125typeset -Z2 minutes
2126typeset -Z2 seconds
2127
2128elapsed_time=$SECONDS
2129((hours = elapsed_time / 3600 ))
2130((minutes = elapsed_time / 60  % 60))
2131((seconds = elapsed_time % 60))
2132
2133echo "\n==== Total build time ====" | \
2134    tee -a $LOGFILE >> $build_time_file
2135echo "\nreal    ${hours}:${minutes}:${seconds}" | \
2136    tee -a $LOGFILE >> $build_time_file
2137
2138if [ "$u_FLAG" = "y" -a "$f_FLAG" = "y" -a "$build_ok" = "y" ]; then
2139	staffer cp ${SRC}/unref-${MACH}.out $PARENT_WS/usr/src/
2140
2141	#
2142	# Produce a master list of unreferenced files -- ideally, we'd
2143	# generate the master just once after all of the nightlies
2144	# have finished, but there's no simple way to know when that
2145	# will be.  Instead, we assume that we're the last nightly to
2146	# finish and merge all of the unref-${MACH}.out files in
2147	# $PARENT_WS/usr/src/.  If we are in fact the final ${MACH} to
2148	# finish, then this file will be the authoritative master
2149	# list.  Otherwise, another ${MACH}'s nightly will eventually
2150	# overwrite ours with its own master, but in the meantime our
2151	# temporary "master" will be no worse than any older master
2152	# which was already on the parent.
2153	#
2154
2155	set -- $PARENT_WS/usr/src/unref-*.out
2156	cp "$1" ${TMPDIR}/unref.merge
2157	shift
2158
2159	for unreffile; do
2160		comm -12 ${TMPDIR}/unref.merge "$unreffile" > ${TMPDIR}/unref.$$
2161		mv ${TMPDIR}/unref.$$ ${TMPDIR}/unref.merge
2162	done
2163
2164	staffer cp ${TMPDIR}/unref.merge $PARENT_WS/usr/src/unrefmaster.out
2165fi
2166
2167#
2168# All done save for the sweeping up.
2169# (whichever exit we hit here will trigger the "cleanup" trap which
2170# optionally sends mail on completion).
2171#
2172if [[ "$build_ok" == "y" ]]; then
2173	if [[ "$W_FLAG" == "y" || "$build_extras_ok" == "y" ]]; then
2174		exit 0
2175	fi
2176fi
2177
2178exit 1
2179