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