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