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