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