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