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