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