xref: /illumos-gate/usr/src/tools/scripts/nightly (revision 244a25b4a79e6f5ff390ef3ed83b7a39fb1b1006)
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}"$':|[\t ]error[:\t ]|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[\t ]*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}"$':|[\t ]error[:\t ])' | \
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}"$':|[\t ]error[:\t ])' | \
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		egrep -i $'^[\t ]*'"${hostname}"$'[\t .]' \
857			$HOME/.make.machines | read host jobs
858		maxjobs=${jobs##*=}
859	fi
860
861	if [[ $maxjobs != +([0-9]) || $maxjobs == 0 ]]
862	then
863		# default
864		maxjobs=4
865	fi
866
867	export DMAKE_MAX_JOBS=$maxjobs
868fi
869
870DMAKE_MODE=parallel;
871export DMAKE_MODE
872
873if [ -z "${ROOT}" ]; then
874	echo "ROOT must be set."
875	exit 1
876fi
877
878#
879# if -V flag was given, reset VERSION to V_ARG
880#
881if [ "$V_FLAG" = "y" ]; then
882	VERSION=$V_ARG
883fi
884
885TMPDIR="/tmp/nightly.tmpdir.$$"
886export TMPDIR
887rm -rf ${TMPDIR}
888mkdir -p $TMPDIR || exit 1
889chmod 777 $TMPDIR
890
891#
892# Work around folks who have historically used GCC_ROOT and convert it to
893# GNUC_ROOT. We leave GCC_ROOT in the environment for now (though this could
894# mess up the case where multiple different gcc versions are being used to
895# shadow).
896#
897if [[ -n "${GCC_ROOT}" ]]; then
898	export GNUC_ROOT=${GCC_ROOT}
899fi
900
901#
902# Tools should only be built non-DEBUG.  Keep track of the tools proto
903# area path relative to $TOOLS, because the latter changes in an
904# export build.
905#
906# TOOLS_PROTO is included below for builds other than usr/src/tools
907# that look for this location.  For usr/src/tools, this will be
908# overridden on the $MAKE command line in build_tools().
909#
910TOOLS=${SRC}/tools
911TOOLS_PROTO_REL=proto/root_${MACH}-nd
912TOOLS_PROTO=${TOOLS}/${TOOLS_PROTO_REL};	export TOOLS_PROTO
913
914unset   CFLAGS LD_LIBRARY_PATH LDFLAGS
915
916# create directories that are automatically removed if the nightly script
917# fails to start correctly
918function newdir {
919	dir=$1
920	toadd=
921	while [ ! -d $dir ]; do
922		toadd="$dir $toadd"
923		dir=`dirname $dir`
924	done
925	torm=
926	newlist=
927	for dir in $toadd; do
928		if staffer mkdir $dir; then
929			newlist="$ISUSER $dir $newlist"
930			torm="$dir $torm"
931		else
932			[ -z "$torm" ] || staffer rmdir $torm
933			return 1
934		fi
935	done
936	newdirlist="$newlist $newdirlist"
937	return 0
938}
939newdirlist=
940
941[ -d $CODEMGR_WS ] || newdir $CODEMGR_WS || exit 1
942
943# since this script assumes the build is from full source, it nullifies
944# variables likely to have been set by a "ws" script; nullification
945# confines the search space for headers and libraries to the proto area
946# built from this immediate source.
947ENVLDLIBS1=
948ENVLDLIBS2=
949ENVLDLIBS3=
950ENVCPPFLAGS1=
951ENVCPPFLAGS2=
952ENVCPPFLAGS3=
953ENVCPPFLAGS4=
954PARENT_ROOT=
955
956export ENVLDLIBS3 ENVCPPFLAGS1 ENVCPPFLAGS2 ENVCPPFLAGS3 ENVCPPFLAGS4 \
957	ENVLDLIBS1 ENVLDLIBS2 PARENT_ROOT
958
959PKGARCHIVE_ORIG=$PKGARCHIVE
960
961#
962# Juggle the logs and optionally send mail on completion.
963#
964
965function logshuffle {
966	LLOG="$ATLOG/log.`date '+%F.%H:%M'`"
967	if [ -f $LLOG -o -d $LLOG ]; then
968		LLOG=$LLOG.$$
969	fi
970
971	rm -f "$ATLOG/latest" 2>/dev/null
972	mkdir -p $LLOG
973	export LLOG
974
975	if [ "$build_ok" = "y" ]; then
976		mv $ATLOG/proto_list_${MACH} $LLOG
977
978		if [ -f $ATLOG/proto_list_tools_${MACH} ]; then
979			mv $ATLOG/proto_list_tools_${MACH} $LLOG
980	        fi
981
982		if [ -f $TMPDIR/wsdiff.results ]; then
983			mv $TMPDIR/wsdiff.results $LLOG
984		fi
985
986		if [ -f $TMPDIR/wsdiff-nd.results ]; then
987			mv $TMPDIR/wsdiff-nd.results $LLOG
988		fi
989	fi
990
991	#
992	# Now that we're about to send mail, it's time to check the noise
993	# file.  In the event that an error occurs beyond this point, it will
994	# be recorded in the nightly.log file, but nowhere else.  This would
995	# include only errors that cause the copying of the noise log to fail
996	# or the mail itself not to be sent.
997	#
998
999	exec >>$LOGFILE 2>&1
1000	if [ -s $build_noise_file ]; then
1001		echo "\n==== Nightly build noise ====\n" |
1002		    tee -a $LOGFILE >>$mail_msg_file
1003		cat $build_noise_file >>$LOGFILE
1004		cat $build_noise_file >>$mail_msg_file
1005		echo | tee -a $LOGFILE >>$mail_msg_file
1006	fi
1007	rm -f $build_noise_file
1008
1009	case "$build_ok" in
1010		y)
1011			state=Completed
1012			;;
1013		i)
1014			state=Interrupted
1015			;;
1016		*)
1017			state=Failed
1018			;;
1019	esac
1020
1021	if [[ $state != "Interrupted" && $build_extras_ok != "y" ]]; then
1022		state=Failed
1023	fi
1024
1025	NIGHTLY_STATUS=$state
1026	export NIGHTLY_STATUS
1027
1028	run_hook POST_NIGHTLY $state
1029	run_hook SYS_POST_NIGHTLY $state
1030
1031	#
1032	# mailx(1) sets From: based on the -r flag
1033	# if it is given.
1034	#
1035	mailx_r=
1036	if [[ -n "${MAILFROM}" ]]; then
1037		mailx_r="-r ${MAILFROM}"
1038	fi
1039
1040	cat $build_time_file $build_environ_file $mail_msg_file \
1041	    > ${LLOG}/mail_msg
1042	if [ "$m_FLAG" = "y" ]; then
1043		/usr/bin/mailx ${mailx_r} -s \
1044		    "Nightly ${MACH} Build of ${basews} ${state}." \
1045		    "${MAILTO}" < "${LLOG}/mail_msg"
1046	fi
1047
1048	if [ "$u_FLAG" = "y" -a "$build_ok" = "y" ]; then
1049		staffer cp ${LLOG}/mail_msg $PARENT_WS/usr/src/mail_msg-${MACH}
1050		staffer cp $LOGFILE $PARENT_WS/usr/src/nightly-${MACH}.log
1051	fi
1052
1053	mv $LOGFILE $LLOG
1054
1055	ln -s "$LLOG" "$ATLOG/latest"
1056}
1057
1058#
1059#	Remove the locks and temporary files on any exit
1060#
1061function cleanup {
1062	logshuffle
1063
1064	[ -z "$lockfile" ] || staffer rm -f $lockfile
1065	[ -z "$atloglockfile" ] || rm -f $atloglockfile
1066	[ -z "$ulockfile" ] || staffer rm -f $ulockfile
1067	[ -z "$Ulockfile" ] || rm -f $Ulockfile
1068
1069	set -- $newdirlist
1070	while [ $# -gt 0 ]; do
1071		ISUSER=$1 staffer rmdir $2
1072		shift; shift
1073	done
1074	rm -rf $TMPDIR
1075}
1076
1077function cleanup_signal {
1078	build_ok=i
1079	# this will trigger cleanup(), above.
1080	exit 1
1081}
1082
1083trap cleanup 0
1084trap cleanup_signal 1 2 3 15
1085
1086#
1087# Generic lock file processing -- make sure that the lock file doesn't
1088# exist.  If it does, it should name the build host and PID.  If it
1089# doesn't, then make sure we can create it.  Clean up locks that are
1090# known to be stale (assumes host name is unique among build systems
1091# for the workspace).
1092#
1093function create_lock {
1094	lockf=$1
1095	lockvar=$2
1096
1097	ldir=`dirname $lockf`
1098	[ -d $ldir ] || newdir $ldir || exit 1
1099	eval $lockvar=$lockf
1100
1101	while ! staffer ln -s $hostname.$STAFFER.$$ $lockf 2> /dev/null; do
1102		ls -l $lockf | nawk '{print $NF}' | IFS=. read host user pid
1103		if [ "$host" != "$hostname" ]; then
1104			echo "$MACH build of $basews apparently" \
1105			    "already started by $user on $host as $pid."
1106			exit 1
1107		elif kill -s 0 $pid 2>/dev/null; then
1108			echo "$MACH build of $basews already started" \
1109			    "by $user as $pid."
1110			exit 1
1111		else
1112			# stale lock; clear it out and try again
1113			rm -f $lockf
1114		fi
1115	done
1116}
1117
1118#
1119# Return the list of interesting proto areas, depending on the current
1120# options.
1121#
1122function allprotos {
1123	typeset roots="$ROOT"
1124
1125	if [[ "$F_FLAG" == n && "$MULTI_PROTO" == yes ]]; then
1126		roots="$roots $ROOT-nd"
1127	fi
1128
1129	echo $roots
1130}
1131
1132# Ensure no other instance of this script is running on this host.
1133# LOCKNAME can be set in <env_file>, and is by default, but is not
1134# required due to the use of $ATLOG below.
1135if [ -n "$LOCKNAME" ]; then
1136	create_lock /tmp/$LOCKNAME "lockfile"
1137fi
1138#
1139# Create from one, two, or three other locks:
1140#	$ATLOG/nightly.lock
1141#		- protects against multiple builds in same workspace
1142#	$PARENT_WS/usr/src/nightly.$MACH.lock
1143#		- protects against multiple 'u' copy-backs
1144#	$NIGHTLY_PARENT_ROOT/nightly.lock
1145#		- protects against multiple 'U' copy-backs
1146#
1147# Overriding ISUSER to 1 causes the lock to be created as root if the
1148# script is run as root.  The default is to create it as $STAFFER.
1149ISUSER=1 create_lock $ATLOG/nightly.lock "atloglockfile"
1150if [ "$u_FLAG" = "y" ]; then
1151	create_lock $PARENT_WS/usr/src/nightly.$MACH.lock "ulockfile"
1152fi
1153if [ "$U_FLAG" = "y" ]; then
1154	# NIGHTLY_PARENT_ROOT is written as root if script invoked as root.
1155	ISUSER=1 create_lock $NIGHTLY_PARENT_ROOT/nightly.lock "Ulockfile"
1156fi
1157
1158# Locks have been taken, so we're doing a build and we're committed to
1159# the directories we may have created so far.
1160newdirlist=
1161
1162#
1163# Create mail_msg_file
1164#
1165mail_msg_file="${TMPDIR}/mail_msg"
1166touch $mail_msg_file
1167build_time_file="${TMPDIR}/build_time"
1168build_environ_file="${TMPDIR}/build_environ"
1169touch $build_environ_file
1170#
1171#	Move old LOGFILE aside
1172#	ATLOG directory already made by 'create_lock' above
1173#
1174if [ -f $LOGFILE ]; then
1175	mv -f $LOGFILE ${LOGFILE}-
1176fi
1177#
1178#	Build OsNet source
1179#
1180START_DATE=`date`
1181SECONDS=0
1182echo "\n==== Nightly $maketype build started:   $START_DATE ====" \
1183    | tee -a $LOGFILE > $build_time_file
1184
1185echo "\nBuild project:  $build_project\nBuild taskid:   $build_taskid" | \
1186    tee -a $mail_msg_file >> $LOGFILE
1187
1188# make sure we log only to the nightly build file
1189build_noise_file="${TMPDIR}/build_noise"
1190exec </dev/null >$build_noise_file 2>&1
1191
1192run_hook SYS_PRE_NIGHTLY
1193run_hook PRE_NIGHTLY
1194
1195echo "\n==== list of environment variables ====\n" >> $LOGFILE
1196env >> $LOGFILE
1197
1198echo "\n==== Nightly argument issues ====\n" | tee -a $mail_msg_file >> $LOGFILE
1199
1200if [ "$N_FLAG" = "y" ]; then
1201	if [ "$p_FLAG" = "y" ]; then
1202		cat <<EOF | tee -a $mail_msg_file >> $LOGFILE
1203WARNING: the p option (create packages) is set, but so is the N option (do
1204         not run protocmp); this is dangerous; you should unset the N option
1205EOF
1206	else
1207		cat <<EOF | tee -a $mail_msg_file >> $LOGFILE
1208Warning: the N option (do not run protocmp) is set; it probably shouldn't be
1209EOF
1210	fi
1211	echo "" | tee -a $mail_msg_file >> $LOGFILE
1212fi
1213
1214if [ "$f_FLAG" = "y" ]; then
1215	if [ "$i_FLAG" = "y" ]; then
1216		echo "WARNING: the -f flag cannot be used during incremental" \
1217		    "builds; ignoring -f\n" | tee -a $mail_msg_file >> $LOGFILE
1218		f_FLAG=n
1219	fi
1220	if [ "${p_FLAG}" != "y" ]; then
1221		echo "WARNING: the -f flag requires -p;" \
1222		    "ignoring -f\n" | tee -a $mail_msg_file >> $LOGFILE
1223		f_FLAG=n
1224	fi
1225fi
1226
1227if [ "$w_FLAG" = "y" -a ! -d $ROOT ]; then
1228	echo "WARNING: -w specified, but $ROOT does not exist;" \
1229	    "ignoring -w\n" | tee -a $mail_msg_file >> $LOGFILE
1230	w_FLAG=n
1231fi
1232
1233case $MULTI_PROTO in
1234yes|no)	;;
1235*)
1236	echo "WARNING: MULTI_PROTO is \"$MULTI_PROTO\"; " \
1237	    "should be \"yes\" or \"no\"." | tee -a $mail_msg_file >> $LOGFILE
1238	echo "Setting MULTI_PROTO to \"no\".\n" | \
1239	    tee -a $mail_msg_file >> $LOGFILE
1240	export MULTI_PROTO=no
1241	;;
1242esac
1243
1244echo "\n==== Build version ====\n" | tee -a $mail_msg_file >> $LOGFILE
1245echo $VERSION | tee -a $mail_msg_file >> $LOGFILE
1246
1247# Save the current proto area if we're comparing against the last build
1248if [ "$w_FLAG" = "y" -a -d "$ROOT" ]; then
1249    if [ -d "$ROOT.prev" ]; then
1250	rm -rf $ROOT.prev
1251    fi
1252    mv $ROOT $ROOT.prev
1253fi
1254
1255# Same for non-DEBUG proto area
1256if [ "$w_FLAG" = "y" -a "$MULTI_PROTO" = yes -a -d "$ROOT-nd" ]; then
1257	if [ -d "$ROOT-nd.prev" ]; then
1258		rm -rf $ROOT-nd.prev
1259	fi
1260	mv $ROOT-nd $ROOT-nd.prev
1261fi
1262
1263#
1264# Echo the SCM type of the parent workspace, this can't just be which_scm
1265# as that does not know how to identify various network repositories.
1266#
1267function parent_wstype {
1268	typeset scm_type junk
1269
1270	CODEMGR_WS="$BRINGOVER_WS" "$WHICH_SCM" 2>/dev/null \
1271	    | read scm_type junk
1272	if [[ -z "$scm_type" || "$scm_type" == unknown ]]; then
1273		# Probe BRINGOVER_WS to determine its type
1274		if [[ $BRINGOVER_WS == ssh://* ]]; then
1275			scm_type="mercurial"
1276		elif [[ $BRINGOVER_WS == http://* ]] && \
1277		    wget -q -O- --save-headers "$BRINGOVER_WS/?cmd=heads" | \
1278		    egrep -s "application/mercurial" 2> /dev/null; then
1279			scm_type="mercurial"
1280		else
1281			scm_type="none"
1282		fi
1283	fi
1284
1285	# fold both unsupported and unrecognized results into "none"
1286	case "$scm_type" in
1287	mercurial)
1288		;;
1289	*)	scm_type=none
1290		;;
1291	esac
1292
1293	echo $scm_type
1294}
1295
1296# Echo the SCM types of $CODEMGR_WS and $BRINGOVER_WS
1297function child_wstype {
1298	typeset scm_type junk
1299
1300	# Probe CODEMGR_WS to determine its type
1301	if [[ -d $CODEMGR_WS ]]; then
1302		$WHICH_SCM | read scm_type junk || exit 1
1303	fi
1304
1305	case "$scm_type" in
1306	none|git|mercurial)
1307		;;
1308	*)	scm_type=none
1309		;;
1310	esac
1311
1312	echo $scm_type
1313}
1314
1315SCM_TYPE=$(child_wstype)
1316
1317#
1318#	Decide whether to clobber
1319#
1320if [ "$i_FLAG" = "n" -a -d "$SRC" ]; then
1321	echo "\n==== Make clobber at `date` ====\n" >> $LOGFILE
1322
1323	cd $SRC
1324	# remove old clobber file
1325	rm -f $SRC/clobber.out
1326	rm -f $SRC/clobber-${MACH}.out
1327
1328	# Remove all .make.state* files, just in case we are restarting
1329	# the build after having interrupted a previous 'make clobber'.
1330	find . \( -name SCCS -o -name .hg -o -name .svn -o -name .git \
1331		-o -name 'interfaces.*' \) -prune \
1332		-o -name '.make.*' -print | xargs rm -f
1333
1334	$MAKE -ek clobber 2>&1 | tee -a $SRC/clobber-${MACH}.out >> $LOGFILE
1335	echo "\n==== Make clobber ERRORS ====\n" >> $mail_msg_file
1336	grep "$MAKE:" $SRC/clobber-${MACH}.out |
1337		egrep -v ": (Entering|Leaving) directory " | \
1338		egrep -v "Ignoring unknown host" | \
1339		tee $TMPDIR/clobber_errs >> $mail_msg_file
1340
1341	if [[ -s $TMPDIR/clobber_errs ]]; then
1342		build_extras_ok=n
1343	fi
1344
1345	if [[ "$t_FLAG" == "y" ]]; then
1346		echo "\n==== Make tools clobber at `date` ====\n" >> $LOGFILE
1347		cd ${TOOLS}
1348		rm -f ${TOOLS}/clobber-${MACH}.out
1349		$MAKE TOOLS_PROTO=$TOOLS_PROTO -ek clobber 2>&1 | \
1350			tee -a ${TOOLS}/clobber-${MACH}.out >> $LOGFILE
1351		echo "\n==== Make tools clobber ERRORS ====\n" \
1352			>> $mail_msg_file
1353		grep "$MAKE:" ${TOOLS}/clobber-${MACH}.out \
1354			| egrep -v ": (Entering|Leaving) directory " \
1355			>> $mail_msg_file
1356		if (( $? == 0 )); then
1357			build_extras_ok=n
1358		fi
1359		rm -rf ${TOOLS_PROTO}
1360		mkdir -p ${TOOLS_PROTO}
1361	fi
1362
1363	typeset roots=$(allprotos)
1364	echo "\n\nClearing $roots" >> "$LOGFILE"
1365	rm -rf $roots
1366
1367	# Get back to a clean workspace as much as possible to catch
1368	# problems that only occur on fresh workspaces.
1369	# Remove all .make.state* files, libraries, and .o's that may
1370	# have been omitted from clobber.  A couple of libraries are
1371	# under source code control, so leave them alone.
1372	# We should probably blow away temporary directories too.
1373	cd $SRC
1374	find $relsrcdirs \( -name SCCS -o -name .hg -o -name .svn \
1375	    -o -name .git -o -name 'interfaces.*' \) -prune -o \
1376	    \( -name '.make.*' -o -name 'lib*.a' -o -name 'lib*.so*' -o \
1377	       -name '*.o' \) -print | \
1378	    grep -v 'tools/ctf/dwarf/.*/libdwarf' | xargs rm -f
1379else
1380	echo "\n==== No clobber at `date` ====\n" >> $LOGFILE
1381fi
1382
1383type bringover_mercurial > /dev/null 2>&1 || function bringover_mercurial {
1384	typeset -x PATH=$PATH
1385
1386	# If the repository doesn't exist yet, then we want to populate it.
1387	if [[ ! -d $CODEMGR_WS/.hg ]]; then
1388		staffer hg init $CODEMGR_WS
1389		staffer echo "[paths]" > $CODEMGR_WS/.hg/hgrc
1390		staffer echo "default=$BRINGOVER_WS" >> $CODEMGR_WS/.hg/hgrc
1391		touch $TMPDIR/new_repository
1392	fi
1393
1394	typeset -x HGMERGE="/bin/false"
1395
1396	#
1397	# If the user has changes, regardless of whether those changes are
1398	# committed, and regardless of whether those changes conflict, then
1399	# we'll attempt to merge them either implicitly (uncommitted) or
1400	# explicitly (committed).
1401	#
1402	# These are the messages we'll use to help clarify mercurial output
1403	# in those cases.
1404	#
1405	typeset mergefailmsg="\
1406***\n\
1407*** nightly was unable to automatically merge your changes.  You should\n\
1408*** redo the full merge manually, following the steps outlined by mercurial\n\
1409*** above, then restart nightly.\n\
1410***\n"
1411	typeset mergepassmsg="\
1412***\n\
1413*** nightly successfully merged your changes.  This means that your working\n\
1414*** directory has been updated, but those changes are not yet committed.\n\
1415*** After nightly completes, you should validate the results of the merge,\n\
1416*** then use hg commit manually.\n\
1417***\n"
1418
1419	#
1420	# For each repository in turn:
1421	#
1422	# 1. Do the pull.  If this fails, dump the output and bail out.
1423	#
1424	# 2. If the pull resulted in an extra head, do an explicit merge.
1425	#    If this fails, dump the output and bail out.
1426	#
1427	# Because we can't rely on Mercurial to exit with a failure code
1428	# when a merge fails (Mercurial issue #186), we must grep the
1429	# output of pull/merge to check for attempted and/or failed merges.
1430	#
1431	# 3. If a merge failed, set the message and fail the bringover.
1432	#
1433	# 4. Otherwise, if a merge succeeded, set the message
1434	#
1435	# 5. Dump the output, and any message from step 3 or 4.
1436	#
1437
1438	typeset HG_SOURCE=$BRINGOVER_WS
1439	if [ ! -f $TMPDIR/new_repository ]; then
1440		HG_SOURCE=$TMPDIR/open_bundle.hg
1441		staffer hg --cwd $CODEMGR_WS incoming --bundle $HG_SOURCE \
1442		    -v $BRINGOVER_WS > $TMPDIR/incoming_open.out
1443
1444		#
1445		# If there are no incoming changesets, then incoming will
1446		# fail, and there will be no bundle file.  Reset the source,
1447		# to allow the remaining logic to complete with no false
1448		# negatives.  (Unlike incoming, pull will return success
1449		# for the no-change case.)
1450		#
1451		if (( $? != 0 )); then
1452			HG_SOURCE=$BRINGOVER_WS
1453		fi
1454	fi
1455
1456	staffer hg --cwd $CODEMGR_WS pull -u $HG_SOURCE \
1457	    > $TMPDIR/pull_open.out 2>&1
1458	if (( $? != 0 )); then
1459		printf "%s: pull failed as follows:\n\n" "$CODEMGR_WS"
1460		cat $TMPDIR/pull_open.out
1461		if grep "^merging.*failed" $TMPDIR/pull_open.out > /dev/null 2>&1; then
1462			printf "$mergefailmsg"
1463		fi
1464		touch $TMPDIR/bringover_failed
1465		return
1466	fi
1467
1468	if grep "not updating" $TMPDIR/pull_open.out > /dev/null 2>&1; then
1469		staffer hg --cwd $CODEMGR_WS merge \
1470		    >> $TMPDIR/pull_open.out 2>&1
1471		if (( $? != 0 )); then
1472			printf "%s: merge failed as follows:\n\n" \
1473			    "$CODEMGR_WS"
1474			cat $TMPDIR/pull_open.out
1475			if grep "^merging.*failed" $TMPDIR/pull_open.out \
1476			    > /dev/null 2>&1; then
1477				printf "$mergefailmsg"
1478			fi
1479			touch $TMPDIR/bringover_failed
1480			return
1481		fi
1482	fi
1483
1484	printf "updated %s with the following results:\n" "$CODEMGR_WS"
1485	cat $TMPDIR/pull_open.out
1486	if grep "^merging" $TMPDIR/pull_open.out >/dev/null 2>&1; then
1487		printf "$mergepassmsg"
1488	fi
1489	printf "\n"
1490
1491	#
1492	# Per-changeset output is neither useful nor manageable for a
1493	# newly-created repository.
1494	#
1495	if [ -f $TMPDIR/new_repository ]; then
1496		return
1497	fi
1498
1499	printf "\nadded the following changesets to open repository:\n"
1500	cat $TMPDIR/incoming_open.out
1501}
1502
1503type bringover_none > /dev/null 2>&1 || function bringover_none {
1504	echo "Couldn't figure out what kind of SCM to use for $BRINGOVER_WS."
1505	touch $TMPDIR/bringover_failed
1506}
1507
1508#
1509#	Decide whether to bringover to the codemgr workspace
1510#
1511if [ "$n_FLAG" = "n" ]; then
1512	PARENT_SCM_TYPE=$(parent_wstype)
1513
1514	if [[ $SCM_TYPE != none && $SCM_TYPE != $PARENT_SCM_TYPE ]]; then
1515		echo "cannot bringover from $PARENT_SCM_TYPE to $SCM_TYPE, " \
1516		    "quitting at `date`." | tee -a $mail_msg_file >> $LOGFILE
1517		exit 1
1518	fi
1519
1520	run_hook PRE_BRINGOVER
1521
1522	echo "\n==== bringover to $CODEMGR_WS at `date` ====\n" >> $LOGFILE
1523	echo "\n==== BRINGOVER LOG ====\n" >> $mail_msg_file
1524
1525	eval "bringover_${PARENT_SCM_TYPE}" 2>&1 |
1526		tee -a $mail_msg_file >> $LOGFILE
1527
1528	if [ -f $TMPDIR/bringover_failed ]; then
1529		rm -f $TMPDIR/bringover_failed
1530		build_ok=n
1531		echo "trouble with bringover, quitting at `date`." |
1532			tee -a $mail_msg_file >> $LOGFILE
1533		exit 1
1534	fi
1535
1536	#
1537	# It's possible that we used the bringover above to create
1538	# $CODEMGR_WS.  If so, then SCM_TYPE was previously "none,"
1539	# but should now be the same as $BRINGOVER_WS.
1540	#
1541	[[ $SCM_TYPE == none ]] && SCM_TYPE=$PARENT_SCM_TYPE
1542
1543	run_hook POST_BRINGOVER
1544
1545	check_closed_bins
1546
1547else
1548	echo "\n==== No bringover to $CODEMGR_WS ====\n" >> $LOGFILE
1549fi
1550
1551# Safeguards
1552[[ -v CODEMGR_WS ]] || fatal_error "Error: Variable CODEMGR_WS not set."
1553[[ -d "${CODEMGR_WS}" ]] || fatal_error "Error: ${CODEMGR_WS} is not a directory."
1554[[ -f "${CODEMGR_WS}/usr/src/Makefile" ]] || fatal_error "Error: ${CODEMGR_WS}/usr/src/Makefile not found."
1555
1556if [[ "$t_FLAG" == "y" ]]; then
1557	set_non_debug_build_flags
1558	# Switch ONBLD_TOOLS early if -t is specified so that
1559	# we could use bootstrapped cw for compiler checks.
1560	ONBLD_TOOLS=${TOOLS_PROTO}/opt/onbld
1561	export ONBLD_TOOLS
1562
1563	bootstrap_tools || fatal_error "Error: could not bootstrap tools"
1564fi
1565
1566echo "\n==== Build environment ====\n" | tee -a $build_environ_file >> $LOGFILE
1567
1568# System
1569whence uname | tee -a $build_environ_file >> $LOGFILE
1570uname -a 2>&1 | tee -a $build_environ_file >> $LOGFILE
1571echo | tee -a $build_environ_file >> $LOGFILE
1572
1573# make
1574whence $MAKE | tee -a $build_environ_file >> $LOGFILE
1575$MAKE -v | tee -a $build_environ_file >> $LOGFILE
1576echo "number of concurrent jobs = $DMAKE_MAX_JOBS" |
1577    tee -a $build_environ_file >> $LOGFILE
1578
1579#
1580# Report the compiler versions.
1581#
1582
1583if [[ ! -f $SRC/Makefile ]]; then
1584	build_ok=n
1585	echo "\nUnable to find \"Makefile\" in $SRC." | \
1586	    tee -a $build_environ_file >> $LOGFILE
1587	exit 1
1588fi
1589
1590( cd $SRC
1591  for target in cc-version java-version openssl-version; do
1592	echo
1593	#
1594	# Put statefile somewhere we know we can write to rather than trip
1595	# over a read-only $srcroot.
1596	#
1597	rm -f $TMPDIR/make-state
1598	export SRC
1599	if $MAKE -K $TMPDIR/make-state -e $target 2>/dev/null; then
1600		continue
1601	fi
1602	touch $TMPDIR/nocompiler
1603  done
1604  echo
1605) | egrep -v "${MAKE}: (Entering|Leaving) directory " \
1606    | tee -a $build_environ_file >> $LOGFILE
1607
1608if [ -f $TMPDIR/nocompiler ]; then
1609	rm -f $TMPDIR/nocompiler
1610	build_ok=n
1611	echo "Aborting due to missing compiler." |
1612		tee -a $build_environ_file >> $LOGFILE
1613	exit 1
1614fi
1615
1616# Check that we're running a capable link-editor
1617whence ld | tee -a $build_environ_file >> $LOGFILE
1618LDVER=`ld -V 2>&1`
1619echo $LDVER | tee -a $build_environ_file >> $LOGFILE
1620LDVER=`echo $LDVER | sed -e "s/.*-1\.\([0-9]*\).*/\1/"`
1621if [ `expr $LDVER \< 422` -eq 1 ]; then
1622	echo "The link-editor needs to be at version 422 or higher to build" | \
1623	    tee -a $build_environ_file >> $LOGFILE
1624	echo "the latest stuff.  Hope your build works." | \
1625	    tee -a $build_environ_file >> $LOGFILE
1626fi
1627
1628#
1629# Build and use the workspace's tools if requested
1630#
1631if [[ "$t_FLAG" == "y" ]]; then
1632	set_non_debug_build_flags
1633
1634	build_tools ${TOOLS_PROTO}
1635	if (( $? != 0 )); then
1636		build_ok=n
1637		exit 1
1638	fi
1639
1640	STABS=${TOOLS_PROTO}/opt/onbld/bin/${MACH}/stabs
1641	export STABS
1642	CTFSTABS=${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfstabs
1643	export CTFSTABS
1644	GENOFFSETS=${TOOLS_PROTO}/opt/onbld/bin/genoffsets
1645	export GENOFFSETS
1646
1647	CTFCONVERT=${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfconvert
1648	export CTFCONVERT
1649	CTFMERGE=${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfmerge
1650	export CTFMERGE
1651
1652	PATH="${TOOLS_PROTO}/opt/onbld/bin/${MACH}:${PATH}"
1653	PATH="${TOOLS_PROTO}/opt/onbld/bin:${PATH}"
1654	export PATH
1655
1656	echo "\n==== New environment settings. ====\n" >> $LOGFILE
1657	echo "STABS=${STABS}" >> $LOGFILE
1658	echo "CTFSTABS=${CTFSTABS}" >> $LOGFILE
1659	echo "CTFCONVERT=${CTFCONVERT}" >> $LOGFILE
1660	echo "CTFMERGE=${CTFMERGE}" >> $LOGFILE
1661	echo "PATH=${PATH}" >> $LOGFILE
1662	echo "ONBLD_TOOLS=${ONBLD_TOOLS}" >> $LOGFILE
1663fi
1664
1665# timestamp the start of the normal build; the findunref tool uses it.
1666touch $SRC/.build.tstamp
1667
1668normal_build
1669
1670ORIG_SRC=$SRC
1671BINARCHIVE=${CODEMGR_WS}/bin-${MACH}.cpio.Z
1672
1673abspkg=
1674for d in $abssrcdirs; do
1675	if [ -d "$d/pkg" ]; then
1676		abspkg="$abspkg $d"
1677	fi
1678done
1679
1680if [ "$L_FLAG" != "y" -a "$build_ok" = y ]; then
1681	echo "\n==== Linting packages ====\n" | \
1682	    tee -a $LOGFILE >> $mail_msg_file
1683
1684	if [ -n "$abspkg" ]; then
1685		for d in "$abspkg"; do
1686			( cd $d/pkg ; $MAKE -e pkglint ) | \
1687			    tee -a $LOGFILE | \
1688			    egrep -v ": (Entering|Leaving) directory " | \
1689			    egrep -v 'Lint engine setup|Starting lint run'
1690		done 2>&1 | tee $TMPDIR/pkglint_noise >> $mail_msg_file
1691		if [[ -s $TMPDIR/pkglint_noise ]]; then
1692			build_extras_ok=n
1693		fi
1694	fi
1695fi
1696
1697#
1698# There are several checks that need to look at the proto area, but
1699# they only need to look at one, and they don't care whether it's
1700# DEBUG or non-DEBUG.
1701#
1702if [[ "$MULTI_PROTO" == yes && "$D_FLAG" == n ]]; then
1703	checkroot=$ROOT-nd
1704else
1705	checkroot=$ROOT
1706fi
1707
1708if [ "$build_ok" = "y" ]; then
1709	echo "\n==== Creating protolist system file at `date` ====" \
1710		>> $LOGFILE
1711	protolist $checkroot > $ATLOG/proto_list_${MACH}
1712	echo "==== protolist system file created at `date` ====\n" \
1713		>> $LOGFILE
1714
1715	if [ "$N_FLAG" != "y" ]; then
1716
1717		E1=
1718		f1=
1719		for f in $f1; do
1720			if [ -f "$f" ]; then
1721				E1="$E1 -e $f"
1722			fi
1723		done
1724
1725		E2=
1726		f2=
1727		if [ -d "$SRC/pkg" ]; then
1728			f2="$f2 exceptions/packaging"
1729		fi
1730
1731		for f in $f2; do
1732			if [ -f "$f" ]; then
1733				E2="$E2 -e $f"
1734			fi
1735		done
1736	fi
1737
1738	if [ "$N_FLAG" != "y" -a -d $SRC/pkg ]; then
1739		echo "\n==== Validating manifests against proto area ====\n" \
1740		    >> $mail_msg_file
1741		( cd $SRC/pkg ; $MAKE -e protocmp ROOT="$checkroot" ) | \
1742		    egrep -v ": (Entering|Leaving) directory " | \
1743		    tee $TMPDIR/protocmp_noise >> $mail_msg_file
1744		if [[ -s $TMPDIR/protocmp_noise ]]; then
1745			build_extras_ok=n
1746		fi
1747	fi
1748
1749	if [ "$N_FLAG" != "y" -a -f "$REF_PROTO_LIST" ]; then
1750		echo "\n==== Impact on proto area ====\n" >> $mail_msg_file
1751		if [ -n "$E2" ]; then
1752			ELIST=$E2
1753		else
1754			ELIST=$E1
1755		fi
1756		$PROTOCMPTERSE \
1757			"Files in yesterday's proto area, but not today's:" \
1758			"Files in today's proto area, but not yesterday's:" \
1759			"Files that changed between yesterday and today:" \
1760			${ELIST} \
1761			-d $REF_PROTO_LIST \
1762			$ATLOG/proto_list_${MACH} \
1763			>> $mail_msg_file
1764	fi
1765fi
1766
1767if [[ "$u_FLAG" == "y" && "$build_ok" == "y" && \
1768    "$build_extras_ok" == "y" ]]; then
1769	staffer cp $ATLOG/proto_list_${MACH} \
1770		$PARENT_WS/usr/src/proto_list_${MACH}
1771fi
1772
1773# Update parent proto area if necessary. This is done now
1774# so that the proto area has either DEBUG or non-DEBUG kernels.
1775# Note that this clears out the lock file, so we can dispense with
1776# the variable now.
1777if [ "$U_FLAG" = "y" -a "$build_ok" = "y" ]; then
1778	echo "\n==== Copying proto area to $NIGHTLY_PARENT_ROOT ====\n" | \
1779	    tee -a $LOGFILE >> $mail_msg_file
1780	rm -rf $NIGHTLY_PARENT_ROOT/*
1781	unset Ulockfile
1782	mkdir -p $NIGHTLY_PARENT_ROOT
1783	if [[ "$MULTI_PROTO" == no || "$D_FLAG" == y ]]; then
1784		( cd $ROOT; tar cf - . |
1785		    ( cd $NIGHTLY_PARENT_ROOT;  umask 0; tar xpf - ) ) 2>&1 |
1786		    tee -a $mail_msg_file >> $LOGFILE
1787	fi
1788	if [[ "$MULTI_PROTO" == yes && "$F_FLAG" == n ]]; then
1789		rm -rf $NIGHTLY_PARENT_ROOT-nd/*
1790		mkdir -p $NIGHTLY_PARENT_ROOT-nd
1791		cd $ROOT-nd
1792		( tar cf - . |
1793		    ( cd $NIGHTLY_PARENT_ROOT-nd; umask 0; tar xpf - ) ) 2>&1 |
1794		    tee -a $mail_msg_file >> $LOGFILE
1795	fi
1796	if [ -n "${NIGHTLY_PARENT_TOOLS_ROOT}" ]; then
1797		echo "\n==== Copying tools proto area to $NIGHTLY_PARENT_TOOLS_ROOT ====\n" | \
1798		    tee -a $LOGFILE >> $mail_msg_file
1799		rm -rf $NIGHTLY_PARENT_TOOLS_ROOT/*
1800		mkdir -p $NIGHTLY_PARENT_TOOLS_ROOT
1801		if [[ "$MULTI_PROTO" == no || "$D_FLAG" == y ]]; then
1802			( cd $TOOLS_PROTO; tar cf - . |
1803			    ( cd $NIGHTLY_PARENT_TOOLS_ROOT;
1804			    umask 0; tar xpf - ) ) 2>&1 |
1805			    tee -a $mail_msg_file >> $LOGFILE
1806		fi
1807	fi
1808fi
1809
1810#
1811# ELF verification: ABI (-A) and runtime (-r) checks
1812#
1813if [[ ($build_ok == y) && (($A_FLAG == y) || ($r_FLAG == y)) ]]; then
1814	# Directory ELF-data.$MACH holds the files produced by these tests.
1815	elf_ddir=$SRC/ELF-data.$MACH
1816
1817	# If there is a previous ELF-data backup directory, remove it. Then,
1818	# rotate current ELF-data directory into its place and create a new
1819	# empty directory
1820	rm -rf $elf_ddir.ref
1821	if [[ -d $elf_ddir ]]; then
1822		mv $elf_ddir $elf_ddir.ref
1823	fi
1824	mkdir -p $elf_ddir
1825
1826	# Call find_elf to produce a list of the ELF objects in the proto area.
1827	# This list is passed to check_rtime and interface_check, preventing
1828	# them from separately calling find_elf to do the same work twice.
1829	find_elf -fr $checkroot > $elf_ddir/object_list
1830
1831	if [[ $A_FLAG == y ]]; then
1832		echo "\n==== Check versioning and ABI information ====\n"  | \
1833		    tee -a $LOGFILE >> $mail_msg_file
1834
1835		# Produce interface description for the proto. Report errors.
1836		interface_check -o -w $elf_ddir -f object_list \
1837			-i interface -E interface.err
1838		if [[ -s $elf_ddir/interface.err ]]; then
1839			tee -a $LOGFILE < $elf_ddir/interface.err \
1840			    >> $mail_msg_file
1841			build_extras_ok=n
1842		fi
1843
1844		# If ELF_DATA_BASELINE_DIR is defined, compare the new interface
1845		# description file to that from the baseline gate. Issue a
1846		# warning if the baseline is not present, and keep going.
1847		if [[ "$ELF_DATA_BASELINE_DIR" != '' ]]; then
1848			base_ifile="$ELF_DATA_BASELINE_DIR/interface"
1849
1850			echo "\n==== Compare versioning and ABI information" \
1851			    "to baseline ====\n"  | \
1852			    tee -a $LOGFILE >> $mail_msg_file
1853			echo "Baseline:	 $base_ifile\n" >> $LOGFILE
1854
1855			if [[ -f $base_ifile ]]; then
1856				interface_cmp -d -o $base_ifile \
1857				    $elf_ddir/interface > $elf_ddir/interface.cmp
1858				if [[ -s $elf_ddir/interface.cmp ]]; then
1859					echo | tee -a $LOGFILE >> $mail_msg_file
1860					tee -a $LOGFILE < \
1861					    $elf_ddir/interface.cmp \
1862					    >> $mail_msg_file
1863					build_extras_ok=n
1864				fi
1865			else
1866				echo "baseline not available. comparison" \
1867                                    "skipped" | \
1868				    tee -a $LOGFILE >> $mail_msg_file
1869			fi
1870
1871		fi
1872	fi
1873
1874	if [[ $r_FLAG == y ]]; then
1875		echo "\n==== Check ELF runtime attributes ====\n" | \
1876		    tee -a $LOGFILE >> $mail_msg_file
1877
1878		# If we're doing a DEBUG build the proto area will be left
1879		# with debuggable objects, thus don't assert -s.
1880		if [[ $D_FLAG == y ]]; then
1881			rtime_sflag=""
1882		else
1883			rtime_sflag="-s"
1884		fi
1885		check_rtime -i -m -v $rtime_sflag -o -w $elf_ddir \
1886			-D object_list  -f object_list -E runtime.err \
1887			-I runtime.attr.raw
1888		if (( $? != 0 )); then
1889			build_extras_ok=n
1890		fi
1891
1892		# check_rtime -I output needs to be sorted in order to
1893		# compare it to that from previous builds.
1894		sort $elf_ddir/runtime.attr.raw > $elf_ddir/runtime.attr
1895		rm $elf_ddir/runtime.attr.raw
1896
1897		# Report errors
1898		if [[ -s $elf_ddir/runtime.err ]]; then
1899			tee -a $LOGFILE < $elf_ddir/runtime.err \
1900				>> $mail_msg_file
1901			build_extras_ok=n
1902		fi
1903
1904		# If there is an ELF-data directory from a previous build,
1905		# then diff the attr files. These files contain information
1906		# about dependencies, versioning, and runpaths. There is some
1907		# overlap with the ABI checking done above, but this also
1908		# flushes out non-ABI interface differences along with the
1909		# other information.
1910		echo "\n==== Diff ELF runtime attributes" \
1911		    "(since last build) ====\n" | \
1912		    tee -a $LOGFILE >> $mail_msg_file >> $mail_msg_file
1913
1914		if [[ -f $elf_ddir.ref/runtime.attr ]]; then
1915			diff $elf_ddir.ref/runtime.attr \
1916				$elf_ddir/runtime.attr \
1917				>> $mail_msg_file
1918		fi
1919	fi
1920
1921	# If -u set, copy contents of ELF-data.$MACH to the parent workspace.
1922	if [[ "$u_FLAG" == "y" ]]; then
1923		p_elf_ddir=$PARENT_WS/usr/src/ELF-data.$MACH
1924
1925		# If parent lacks the ELF-data.$MACH directory, create it
1926		if [[ ! -d $p_elf_ddir ]]; then
1927			staffer mkdir -p $p_elf_ddir
1928		fi
1929
1930		# These files are used asynchronously by other builds for ABI
1931		# verification, as above for the -A option. As such, we require
1932		# the file replacement to be atomic. Copy the data to a temp
1933		# file in the same filesystem and then rename into place.
1934		(
1935			cd $elf_ddir
1936			for elf_dfile in *; do
1937				staffer cp $elf_dfile \
1938				    ${p_elf_ddir}/${elf_dfile}.new
1939				staffer mv -f ${p_elf_ddir}/${elf_dfile}.new \
1940				    ${p_elf_ddir}/${elf_dfile}
1941			done
1942		)
1943	fi
1944fi
1945
1946# "make check" begins
1947
1948if [ "$i_CMD_LINE_FLAG" = "n" -a "$C_FLAG" = "y" ]; then
1949	# remove old check.out
1950	rm -f $SRC/check.out
1951
1952	rm -f $SRC/check-${MACH}.out
1953	cd $SRC
1954	$MAKE -ek check ROOT="$checkroot" 2>&1 | tee -a $SRC/check-${MACH}.out \
1955	    >> $LOGFILE
1956	echo "\n==== cstyle/hdrchk errors ====\n" >> $mail_msg_file
1957
1958	grep ":" $SRC/check-${MACH}.out |
1959		egrep -v ": (Entering|Leaving) directory " | \
1960		egrep -v "Ignoring unknown host" | \
1961		sort | uniq | tee $TMPDIR/check_errors >> $mail_msg_file
1962
1963	if [[ -s $TMPDIR/check_errors ]]; then
1964		build_extras_ok=n
1965	fi
1966else
1967	echo "\n==== No '$MAKE check' ====\n" >> $LOGFILE
1968fi
1969
1970echo "\n==== Find core files ====\n" | \
1971    tee -a $LOGFILE >> $mail_msg_file
1972
1973find $abssrcdirs -name core -a -type f -exec file {} \; | \
1974	tee -a $LOGFILE >> $mail_msg_file
1975
1976if [ "$f_FLAG" = "y" -a "$build_ok" = "y" ]; then
1977	echo "\n==== Diff unreferenced files (since last build) ====\n" \
1978	    | tee -a $LOGFILE >>$mail_msg_file
1979	rm -f $SRC/unref-${MACH}.ref
1980	if [ -f $SRC/unref-${MACH}.out ]; then
1981		mv $SRC/unref-${MACH}.out $SRC/unref-${MACH}.ref
1982	fi
1983
1984	findunref -S $SCM_TYPE -t $SRC/.build.tstamp -s usr $CODEMGR_WS \
1985	    ${TOOLS}/findunref/exception_list 2>> $mail_msg_file | \
1986	    sort > $SRC/unref-${MACH}.out
1987
1988	if [ ! -f $SRC/unref-${MACH}.ref ]; then
1989		cp $SRC/unref-${MACH}.out $SRC/unref-${MACH}.ref
1990	fi
1991
1992	diff $SRC/unref-${MACH}.ref $SRC/unref-${MACH}.out >>$mail_msg_file
1993fi
1994
1995# Verify that the usual lists of files, such as exception lists,
1996# contain only valid references to files.  If the build has failed,
1997# then don't check the proto area.
1998CHECK_PATHS=${CHECK_PATHS:-y}
1999if [ "$CHECK_PATHS" = y -a "$N_FLAG" != y ]; then
2000	echo "\n==== Check lists of files ====\n" | tee -a $LOGFILE \
2001		>>$mail_msg_file
2002	arg=-b
2003	[ "$build_ok" = y ] && arg=
2004	checkpaths $arg $checkroot > $SRC/check-paths.out 2>&1
2005	if [[ -s $SRC/check-paths.out ]]; then
2006		tee -a $LOGFILE < $SRC/check-paths.out >> $mail_msg_file
2007		build_extras_ok=n
2008	fi
2009fi
2010
2011if [ "$M_FLAG" != "y" -a "$build_ok" = y ]; then
2012	echo "\n==== Impact on file permissions ====\n" \
2013		>> $mail_msg_file
2014
2015	if [ -n "$abspkg" ]; then
2016		for d in "$abspkg"; do
2017			( cd $d/pkg ; $MAKE -e pmodes ) | \
2018			egrep -v ": (Entering|Leaving) directory " \
2019			>> $mail_msg_file
2020		done
2021	fi
2022fi
2023
2024if [ "$w_FLAG" = "y" -a "$build_ok" = "y" ]; then
2025	if [[ "$MULTI_PROTO" == no || "$D_FLAG" == y ]]; then
2026		do_wsdiff DEBUG $ROOT.prev $ROOT
2027	fi
2028
2029	if [[ "$MULTI_PROTO" == yes && "$F_FLAG" == n ]]; then
2030		do_wsdiff non-DEBUG $ROOT-nd.prev $ROOT-nd
2031	fi
2032fi
2033
2034END_DATE=`date`
2035echo "==== Nightly $maketype build completed: $END_DATE ====" | \
2036    tee -a $LOGFILE >> $build_time_file
2037
2038typeset -i10 hours
2039typeset -Z2 minutes
2040typeset -Z2 seconds
2041
2042elapsed_time=$SECONDS
2043((hours = elapsed_time / 3600 ))
2044((minutes = elapsed_time / 60  % 60))
2045((seconds = elapsed_time % 60))
2046
2047echo "\n==== Total build time ====" | \
2048    tee -a $LOGFILE >> $build_time_file
2049echo "\nreal    ${hours}:${minutes}:${seconds}" | \
2050    tee -a $LOGFILE >> $build_time_file
2051
2052if [ "$u_FLAG" = "y" -a "$f_FLAG" = "y" -a "$build_ok" = "y" ]; then
2053	staffer cp ${SRC}/unref-${MACH}.out $PARENT_WS/usr/src/
2054
2055	#
2056	# Produce a master list of unreferenced files -- ideally, we'd
2057	# generate the master just once after all of the nightlies
2058	# have finished, but there's no simple way to know when that
2059	# will be.  Instead, we assume that we're the last nightly to
2060	# finish and merge all of the unref-${MACH}.out files in
2061	# $PARENT_WS/usr/src/.  If we are in fact the final ${MACH} to
2062	# finish, then this file will be the authoritative master
2063	# list.  Otherwise, another ${MACH}'s nightly will eventually
2064	# overwrite ours with its own master, but in the meantime our
2065	# temporary "master" will be no worse than any older master
2066	# which was already on the parent.
2067	#
2068
2069	set -- $PARENT_WS/usr/src/unref-*.out
2070	cp "$1" ${TMPDIR}/unref.merge
2071	shift
2072
2073	for unreffile; do
2074		comm -12 ${TMPDIR}/unref.merge "$unreffile" > ${TMPDIR}/unref.$$
2075		mv ${TMPDIR}/unref.$$ ${TMPDIR}/unref.merge
2076	done
2077
2078	staffer cp ${TMPDIR}/unref.merge $PARENT_WS/usr/src/unrefmaster.out
2079fi
2080
2081#
2082# All done save for the sweeping up.
2083# (whichever exit we hit here will trigger the "cleanup" trap which
2084# optionally sends mail on completion).
2085#
2086if [[ "$build_ok" == "y" ]]; then
2087	if [[ "$W_FLAG" == "y" || "$build_extras_ok" == "y" ]]; then
2088		exit 0
2089	fi
2090fi
2091
2092exit 1
2093