xref: /titanic_44/usr/src/tools/scripts/nightly.sh (revision 6da5aa940d1e163c6f244b5523c22d545bfb954b)
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#
27# Based on the nightly script from the integration folks,
28# Mostly modified and owned by mike_s.
29# Changes also by kjc, dmk.
30#
31# BRINGOVER_WS may be specified in the env file.
32# The default is the old behavior of CLONE_WS
33#
34# -i on the command line, means fast options, so when it's on the
35# command line (only), lint and check builds are skipped no matter what
36# the setting of their individual flags are in NIGHTLY_OPTIONS.
37#
38# LINTDIRS can be set in the env file, format is a list of:
39#
40#	/dirname-to-run-lint-on flag
41#
42#	Where flag is:	y - enable lint noise diff output
43#			n - disable lint noise diff output
44#
45#	For example: LINTDIRS="$SRC/uts n $SRC/stand y $SRC/psm y"
46#
47# OPTHOME and TEAMWARE may be set in the environment to override /opt
48# and /opt/teamware defaults.
49#
50
51#
52# The CDPATH variable causes ksh's `cd' builtin to emit messages to stdout
53# under certain circumstances, which can really screw things up; unset it.
54#
55unset CDPATH
56
57# Get the absolute path of the nightly script that the user invoked.  This
58# may be a relative path, and we need to do this before changing directory.
59nightly_path=`whence $0`
60nightly_ls="`ls -l $nightly_path`"
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
73#
74# Datestamp for crypto tarballs.  We don't use BUILD_DATE because it
75# doesn't sort right and it uses English abbreviations for the month.
76# We want to guarantee a consistent string, so just invoke date(1)
77# once and save the result in a global variable.  YYYY-MM-DD is easier
78# to parse visually than YYYYMMDD.
79#
80cryptostamp=$(date +%Y-%m-%d)
81
82#
83# Echo the path for depositing a crypto tarball, creating the target
84# directory if it doesn't already exist.
85# usage: cryptodest suffix
86# where "suffix" is "" or "-nd".
87#
88function cryptodest {
89	typeset suffix=$1
90	#
91	# $PKGARCHIVE gets wiped out with each build, so put the
92	# tarball one level up.
93	#
94	typeset dir=$(dirname "$PKGARCHIVE")
95	[ -d "$dir" ] || mkdir -p "$dir" >> "$LOGFILE" 2>&1
96	#
97	# Put the suffix after the datestamp to make it easier for
98	# gatelings to use crypto from a specific date (no need to
99	# copy and rename the gate tarball).
100	#
101	echo "$dir/on-crypto-$cryptostamp$suffix.$MACH.tar"
102}
103
104#
105# Create a non-stamped symlink to the given crypto tarball.
106# Return 0 on success, non-zero on failure.
107#
108function cryptolink {
109	typeset targpath=$1
110	typeset suffix=$2
111	if [ ! -f "$targpath" ]; then
112		echo "no crypto at $targpath"
113		return 1
114	fi
115	typeset dir=$(dirname "$targpath")
116	typeset targfile=$(basename "$targpath")
117	typeset link=on-crypto$suffix.$MACH.tar.bz2
118	(cd "$dir"; rm -f "$link")
119	(cd "$dir"; ln -s "$targfile" "$link")
120	return $?
121}
122
123#
124# Generate a crypto tarball from the proto area and put it in the
125# canonical location, along with the datestamp-free symlink.
126# Sets build_ok to "n" if there is a problem.
127#
128function crypto_from_proto {
129	typeset label=$1
130	typeset suffix=$2
131	typeset -i stat
132	typeset to
133
134	echo "Creating $label crypto tarball..." >> "$LOGFILE"
135
136	#
137	# Generate the crypto THIRDPARTYLICENSE file.  This needs to
138	# be done after the build has finished and before we run
139	# cryptodrop.  We'll generate the file twice if we're building
140	# both DEBUG and non-DEBUG, but it's a cheap operation and not
141	# worth the complexity to only do once.
142	#
143	if [ -d ${ROOT}${suffix}/licenses/usr ]; then
144		( cd ${ROOT}${suffix}/licenses ; \
145		    mktpl -c $SRC/pkg/license-list ) >> "$LOGFILE" 2>&1
146		if (( $? != 0 )) ; then
147			echo "Couldn't create crypto THIRDPARTYLICENSE files" |
148			    tee -a "$mail_msg_file" >> "$LOGFILE"
149			build_ok=n
150			return
151		fi
152	else
153		echo "No licenses found under ${ROOT}${suffix}/licenses" |
154		    tee -a "$mail_msg_file" >> "$LOGFILE"
155	fi
156
157	to=$(cryptodest "$suffix")
158	if [ "$suffix" = "-nd" ]; then
159		cryptodrop -n "$to" >> "$LOGFILE" 2>&1
160	else
161		cryptodrop "$to" >> "$LOGFILE" 2>&1
162	fi
163	if (( $? != 0 )) ; then
164		echo "\nCould not create $label crypto tarball." |
165		   tee -a "$mail_msg_file" >> "$LOGFILE"
166		build_ok=n
167	else
168		cryptolink "$to.bz2" "$suffix" >> "$LOGFILE" 2>&1
169		if (( $? != 0 )) ; then
170			build_ok=n
171		fi
172	fi
173}
174
175#
176# Function to do a DEBUG and non-DEBUG build. Needed because we might
177# need to do another for the source build, and since we only deliver DEBUG or
178# non-DEBUG packages.
179#
180# usage: normal_build
181#
182function normal_build {
183
184	typeset orig_p_FLAG="$p_FLAG"
185	typeset crypto_in="$ON_CRYPTO_BINS"
186	typeset crypto_signer="$CODESIGN_USER"
187	typeset gencrypto=no
188
189	suffix=""
190	[ -n "$CODESIGN_USER" ] && gencrypto=yes
191
192	# non-DEBUG build begins
193
194	if [ "$F_FLAG" = "n" ]; then
195		set_non_debug_build_flags
196		CODESIGN_USER="$crypto_signer" \
197		    build "non-DEBUG" "$suffix-nd" "-nd" "$MULTI_PROTO" \
198		    $(ndcrypto "$crypto_in")
199		if [ "$build_ok" = "y" -a "$X_FLAG" = "y" -a \
200		    "$p_FLAG" = "y" ]; then
201			copy_ihv_pkgs non-DEBUG -nd
202		fi
203
204		if [[ "$gencrypto" = yes && "$build_ok" = y ]]; then
205			crypto_from_proto non-DEBUG -nd
206		fi
207	else
208		echo "\n==== No non-DEBUG $open_only build ====\n" >> "$LOGFILE"
209	fi
210
211	# non-DEBUG build ends
212
213	# DEBUG build begins
214
215	if [ "$D_FLAG" = "y" ]; then
216		set_debug_build_flags
217		CODESIGN_USER="$crypto_signer" \
218		    build "DEBUG" "$suffix" "" "$MULTI_PROTO" "$crypto_in"
219		if [ "$build_ok" = "y" -a "$X_FLAG" = "y" -a \
220		    "$p_FLAG" = "y" ]; then
221			copy_ihv_pkgs DEBUG ""
222		fi
223
224		if [[ "$gencrypto" = yes && "$build_ok" = y ]]; then
225			crypto_from_proto DEBUG ""
226		fi
227	else
228		echo "\n==== No DEBUG $open_only build ====\n" >> "$LOGFILE"
229	fi
230
231	# DEBUG build ends
232
233	p_FLAG="$orig_p_FLAG"
234}
235
236#
237# usage: run_hook HOOKNAME ARGS...
238#
239# If variable "$HOOKNAME" is defined, insert a section header into
240# our logs and then run the command with ARGS
241#
242function run_hook {
243	HOOKNAME=$1
244    	eval HOOKCMD=\$$HOOKNAME
245	shift
246
247	if [ -n "$HOOKCMD" ]; then
248	    	(
249			echo "\n==== Running $HOOKNAME command: $HOOKCMD ====\n"
250		    	( $HOOKCMD "$@" 2>&1 )
251			if [ "$?" -ne 0 ]; then
252			    	# Let exit status propagate up
253			    	touch $TMPDIR/abort
254			fi
255		) | tee -a $mail_msg_file >> $LOGFILE
256
257		if [ -f $TMPDIR/abort ]; then
258			build_ok=n
259			echo "\nAborting at request of $HOOKNAME" |
260				tee -a $mail_msg_file >> $LOGFILE
261			exit 1
262		fi
263	fi
264}
265
266#
267# usage: filelist DESTDIR PATTERN
268#
269function filelist {
270	DEST=$1
271	PATTERN=$2
272	cd ${DEST}
273
274	OBJFILES=${ORIG_SRC}/xmod/obj_files
275	if [ ! -f ${OBJFILES} ]; then
276		return;
277	fi
278	for i in `grep -v '^#' ${OBJFILES} | \
279	    grep ${PATTERN} | cut -d: -f2 | tr -d ' \t'`
280	do
281		# wildcard expansion
282		for j in $i
283		do
284			if [ -f "$j" ]; then
285				echo $j
286			fi
287			if [ -d "$j" ]; then
288				echo $j
289			fi
290		done
291	done | sort | uniq
292}
293
294# function to save off binaries after a full build for later
295# restoration
296function save_binaries {
297	# save off list of binaries
298	echo "\n==== Saving binaries from build at `date` ====\n" | \
299	    tee -a $mail_msg_file >> $LOGFILE
300	rm -f ${BINARCHIVE}
301	cd ${CODEMGR_WS}
302	filelist ${CODEMGR_WS} '^preserve:' >> $LOGFILE
303	filelist ${CODEMGR_WS} '^preserve:' | \
304	    cpio -ocB 2>/dev/null | compress \
305	    > ${BINARCHIVE}
306}
307
308# delete files
309# usage: hybridize_files DESTDIR MAKE_TARGET
310function hybridize_files {
311	DEST=$1
312	MAKETARG=$2
313
314	echo "\n==== Hybridizing files at `date` ====\n" | \
315	    tee -a $mail_msg_file >> $LOGFILE
316	for i in `filelist ${DEST} '^delete:'`
317	do
318		echo "removing ${i}." | tee -a $mail_msg_file >> $LOGFILE
319		rm -rf "${i}"
320	done
321	for i in `filelist ${DEST} '^hybridize:' `
322	do
323		echo "hybridizing ${i}." | tee -a $mail_msg_file >> $LOGFILE
324		rm -f ${i}+
325		sed -e "/^# HYBRID DELETE START/,/^# HYBRID DELETE END/d" \
326		    < ${i} > ${i}+
327		mv ${i}+ ${i}
328	done
329}
330
331# restore binaries into the proper source tree.
332# usage: restore_binaries DESTDIR MAKE_TARGET
333function restore_binaries {
334	DEST=$1
335	MAKETARG=$2
336
337	echo "\n==== Restoring binaries to ${MAKETARG} at `date` ====\n" | \
338	    tee -a $mail_msg_file >> $LOGFILE
339	cd ${DEST}
340	zcat ${BINARCHIVE} | \
341	    cpio -idmucvB 2>/dev/null | tee -a $mail_msg_file >> ${LOGFILE}
342}
343
344# rename files we save binaries of
345# usage: rename_files DESTDIR MAKE_TARGET
346function rename_files {
347	DEST=$1
348	MAKETARG=$2
349	echo "\n==== Renaming source files in ${MAKETARG} at `date` ====\n" | \
350	    tee -a $mail_msg_file >> $LOGFILE
351	for i in `filelist ${DEST} '^rename:'`
352	do
353		echo ${i} | tee -a $mail_msg_file >> ${LOGFILE}
354		rm -f ${i}.export
355		mv ${i} ${i}.export
356	done
357}
358
359#
360# Copy some or all of the source tree.
361#
362# Returns 0 for success, non-zero for failure.
363#
364# usage: copy_source CODEMGR_WS DESTDIR LABEL SRCROOT
365#
366function copy_source {
367	WS=$1
368	DEST=$2
369	label=$3
370	srcroot=$4
371
372	printf "\n==== Creating %s source from %s (%s) ====\n\n" \
373	    "$DEST" "$WS" "$label" | tee -a $mail_msg_file >> $LOGFILE
374
375	printf "cleaning out %s\n" "$DEST." >> $LOGFILE
376	rm -rf "$DEST" >> $LOGFILE 2>&1
377
378	printf "creating %s\n" "$DEST." >> $LOGFILE
379	mkdir -p "$DEST" 2>> $LOGFILE
380
381	if (( $? != 0 )) ; then
382		printf "failed to create %s\n" "$DEST" |
383		    tee -a $mail_msg_file >> $LOGFILE
384		build_ok=n
385		return 1
386	fi
387	cd "$WS"
388
389	printf "populating %s\n" "$DEST." >> $LOGFILE
390
391	case "$SCM_TYPE" in
392	teamware)
393		find $srcroot -name 's\.*' -a -type f -print | \
394		    sed -e 's,SCCS\/s.,,' | \
395		    grep -v '/\.del-*' | \
396		    cpio -pd $DEST >>$LOGFILE 2>&1
397		if (( $? != 0 )) ; then
398		    printf "cpio failed for %s\n" "$DEST" |
399			tee -a $mail_msg_file >> $LOGFILE
400		    build_ok=n
401		    return 1
402		fi
403		;;
404	mercurial)
405		copy_source_mercurial $DEST $srcroot
406		if (( $? != 0 )) ; then
407		    build_ok=n
408		    return 1
409		fi
410		;;
411	*)
412		build_ok=n
413		echo "Tree copy is not supported for workspace type" \
414		    "$SCM_TYPE" | tee -a $mail_msg_file >> $LOGFILE
415		return 1
416		;;
417	esac
418
419	return 0
420}
421
422#
423# Mercurial-specific copy code for copy_source().  Handles the
424# combined open and closed trees.
425#
426# Returns 0 for success, non-zero for failure.
427#
428# usage: copy_source_mercurial destdir srcroot
429#
430function copy_source_mercurial {
431	typeset dest=$1
432	typeset srcroot=$2
433	typeset open_top closed_top
434
435	case $srcroot in
436	usr)
437		open_top=usr
438		if [[ "$CLOSED_IS_PRESENT" = yes ]]; then
439			closed_top=usr/closed
440		fi
441		;;
442	usr/closed*)
443		if [[ "$CLOSED_IS_PRESENT" = no ]]; then
444			printf "can't copy %s: closed tree not present.\n" \
445			    "$srcroot" | tee -a $mail_msg_file >> $LOGFILE
446			return 1
447		fi
448		closed_top="$srcroot"
449		;;
450	*)
451		open_top="$srcroot"
452		;;
453	esac
454
455	if [[ -n "$open_top" ]]; then
456		hg locate -I "$open_top" | cpio -pd "$dest" >>$LOGFILE 2>&1
457		if (( $? != 0 )) ; then
458		    printf "cpio failed for %s\n" "$dest" |
459			tee -a $mail_msg_file >> $LOGFILE
460		    return 1
461		fi
462	fi
463
464	if [[ -n "$closed_top" ]]; then
465		mkdir -p "$dest/usr/closed" || return 1
466		if [[ "$closed_top" = usr/closed ]]; then
467			(cd usr/closed; hg locate |
468			    cpio -pd "$dest/usr/closed") >>$LOGFILE 2>&1
469			if (( $? != 0 )) ; then
470			    printf "cpio failed for %s/usr/closed\n" \
471				"$dest" | tee -a $mail_msg_file >> $LOGFILE
472			    return 1
473			fi
474		else
475			# copy subtree of usr/closed
476			closed_top=${closed_top#usr/closed/}
477			(cd usr/closed; hg locate -I "$closed_top" |
478			    cpio -pd "$dest/usr/closed") >>$LOGFILE 2>&1
479			if (( $? != 0 )) ; then
480			    printf "cpio failed for %s/usr/closed/%s\n" \
481				"$dest" "$closed_top" |
482				tee -a $mail_msg_file >> $LOGFILE
483			    return 1
484			fi
485		fi
486	fi
487
488	return 0
489}
490
491#
492# function to create (but not build) the export/crypt source tree.
493# usage: set_up_source_build CODEMGR_WS DESTDIR MAKE_TARGET
494# Sets SRC to the modified source tree, for use by the caller when it
495# builds the tree.
496#
497function set_up_source_build {
498	WS=$1
499	DEST=$2
500	MAKETARG=$3
501
502	copy_source $WS $DEST $MAKETARG usr
503	if (( $? != 0 )); then
504	    echo "\nCould not copy source tree for source build." |
505		tee -a $mail_msg_file >> $LOGFILE
506	    build_ok=n
507	    return
508	fi
509
510	SRC=${DEST}/usr/src
511
512	cd $SRC
513	rm -f ${MAKETARG}.out
514	echo "making ${MAKETARG} in ${SRC}." >> $LOGFILE
515	/bin/time $MAKE -e ${MAKETARG} 2>&1 | \
516	    tee -a $SRC/${MAKETARG}.out >> $LOGFILE
517	echo "\n==== ${MAKETARG} build errors ====\n" >> $mail_msg_file
518	egrep ":" $SRC/${MAKETARG}.out | \
519		egrep -e "(^${MAKE}:|[ 	]error[: 	\n])" | \
520		egrep -v "Ignoring unknown host" | \
521		egrep -v "warning" >> $mail_msg_file
522
523	echo "clearing state files." >> $LOGFILE
524	find . -name '.make*' -exec rm -f {} \;
525
526	cd ${DEST}
527	if [ "${MAKETARG}" = "CRYPT_SRC" ]; then
528		rm -f ${CODEMGR_WS}/crypt_files.cpio.Z
529		echo "\n==== xmod/cry_files that don't exist ====\n" | \
530		    tee -a $mail_msg_file >> $LOGFILE
531		CRYPT_FILES=${WS}/usr/src/xmod/cry_files
532		for i in `cat ${CRYPT_FILES}`
533		do
534			# make sure the files exist
535			if [ -f "$i" ]; then
536				continue
537			fi
538			if [ -d "$i" ]; then
539				continue
540			fi
541			echo "$i" | tee -a $mail_msg_file >> $LOGFILE
542		done
543		find `cat ${CRYPT_FILES}` -print 2>/dev/null | \
544		    cpio -ocB 2>/dev/null | \
545		    compress > ${CODEMGR_WS}/crypt_files.cpio.Z
546	fi
547
548	if [ "${MAKETARG}" = "EXPORT_SRC" ]; then
549		# rename first, since we might restore a file
550		# of the same name (mapfiles)
551		rename_files ${EXPORT_SRC} EXPORT_SRC
552		if [ "$SH_FLAG" = "y" ]; then
553			hybridize_files ${EXPORT_SRC} EXPORT_SRC
554		fi
555	fi
556
557	# save the cleartext
558	echo "\n==== Creating ${MAKETARG}.cpio.Z ====\n" | \
559	    tee -a $mail_msg_file >> $LOGFILE
560	cd ${DEST}
561	rm -f ${MAKETARG}.cpio.Z
562	find usr -depth -print | \
563	    grep -v usr/src/${MAKETARG}.out | \
564	    cpio -ocB 2>/dev/null | \
565	    compress > ${CODEMGR_WS}/${MAKETARG}.cpio.Z
566	if [ "${MAKETARG}" = "EXPORT_SRC" ]; then
567		restore_binaries ${EXPORT_SRC} EXPORT_SRC
568	fi
569
570	if [ "${MAKETARG}" = "CRYPT_SRC" ]; then
571		restore_binaries ${CRYPT_SRC} CRYPT_SRC
572	fi
573
574}
575
576# Return library search directive as function of given root.
577function myldlibs {
578	echo "-L$1/lib -L$1/usr/lib"
579}
580
581# Return header search directive as function of given root.
582function myheaders {
583	echo "-I$1/usr/include"
584}
585
586#
587# Unpack the crypto tarball into the proto area.  We first extract the
588# tarball into a temp directory so that we can handle the non-DEBUG
589# tarball correctly with MULTI_PROTO=no.
590# Return 0 on success, non-zero on failure.
591#
592function unpack_crypto {
593	typeset tarfile=$1
594	typeset suffix=$2
595	typeset ctop=$(mktemp -d /tmp/crypto.XXXXXX)
596	[ -n "$ctop" ] || return 1
597	typeset croot=$ctop/proto/root_$MACH$suffix
598	echo "Unpacking crypto ($tarfile)..."
599	bzcat "$tarfile" | (cd "$ctop"; tar xfBp -)
600	if [[ $? -ne 0 || ! -d "$croot" ]]; then
601		return 1
602	fi
603	#
604	# We extract with -p so that we maintain permissions on directories.
605	#
606	(cd "$croot"; tar cf - *) | (cd "$ROOT"; tar xfBp -)
607	typeset -i stat=$?
608	rm -rf "$ctop"
609	return $stat
610}
611
612#
613# Function to do the build, including package generation.
614# usage: build LABEL SUFFIX ND MULTIPROTO CRYPTO
615# - LABEL is used to tag build output.
616# - SUFFIX is used to distinguish files (e.g., DEBUG vs non-DEBUG,
617#   open-only vs full tree).
618# - ND is "-nd" (non-DEBUG builds) or "" (DEBUG builds).
619# - If MULTIPROTO is "yes", it means to name the proto area according to
620#   SUFFIX.  Otherwise ("no"), (re)use the standard proto area.
621# - CRYPTO is the path to the crypto tarball, or null.
622#
623function build {
624	LABEL=$1
625	SUFFIX=$2
626	ND=$3
627	MULTIPROTO=$4
628	CRYPTOPATH=$5
629	INSTALLOG=install${SUFFIX}-${MACH}
630	NOISE=noise${SUFFIX}-${MACH}
631	PKGARCHIVE=${PKGARCHIVE_ORIG}${SUFFIX}
632
633	ORIGROOT=$ROOT
634	[ $MULTIPROTO = no ] || export ROOT=$ROOT$SUFFIX
635
636	if [[ "$O_FLAG" = y ]]; then
637		echo "\nSetting CLOSEDROOT= ${ROOT}-closed\n" >> $LOGFILE
638		export CLOSEDROOT=${ROOT}-closed
639	fi
640
641	export ENVLDLIBS1=`myldlibs $ROOT`
642	export ENVCPPFLAGS1=`myheaders $ROOT`
643
644	this_build_ok=y
645	#
646	#	Build OS-Networking source
647	#
648	echo "\n==== Building OS-Net source at `date` ($LABEL) ====\n" \
649		>> $LOGFILE
650
651	rm -f $SRC/${INSTALLOG}.out
652	cd $SRC
653	/bin/time $MAKE -e install 2>&1 | \
654	    tee -a $SRC/${INSTALLOG}.out >> $LOGFILE
655
656	if [[ "$SCM_TYPE" = teamware ]]; then
657		echo "\n==== SCCS Noise ($LABEL) ====\n" >> $mail_msg_file
658		egrep 'sccs(check:| *get)' $SRC/${INSTALLOG}.out >> \
659			$mail_msg_file
660	fi
661
662	echo "\n==== Build errors ($LABEL) ====\n" >> $mail_msg_file
663	egrep ":" $SRC/${INSTALLOG}.out |
664		egrep -e "(^${MAKE}:|[ 	]error[: 	\n])" | \
665		egrep -v "Ignoring unknown host" | \
666		egrep -v "cc .* -o error " | \
667		egrep -v "warning" >> $mail_msg_file
668	if [ "$?" = "0" ]; then
669		build_ok=n
670		this_build_ok=n
671	fi
672	grep "bootblock image is .* bytes too big" $SRC/${INSTALLOG}.out \
673		>> $mail_msg_file
674	if [ "$?" = "0" ]; then
675		build_ok=n
676		this_build_ok=n
677	fi
678
679	if [ -n "$CRYPTOPATH" ]; then
680		unpack_crypto "$CRYPTOPATH" "$ND" >> "$LOGFILE" 2>&1
681		if (( $? != 0 )) ; then
682			echo "Could not unpack crypto ($CRYPTOPATH)" |
683			    tee -a "$mail_msg_file" >> "$LOGFILE"
684			build_ok=n
685			this_build_ok=n
686		fi
687	fi
688
689	if [ "$W_FLAG" = "n" ]; then
690		echo "\n==== Build warnings ($LABEL) ====\n" >>$mail_msg_file
691		egrep -i warning: $SRC/${INSTALLOG}.out \
692			| egrep -v '^tic:' \
693			| egrep -v "symbol (\`|')timezone' has differing types:" \
694		        | egrep -v "parameter <PSTAMP> set to" \
695			| egrep -v "Ignoring unknown host" \
696			| egrep -v "redefining segment flags attribute for" \
697			>> $mail_msg_file
698	fi
699
700	echo "\n==== Ended OS-Net source build at `date` ($LABEL) ====\n" \
701		>> $LOGFILE
702
703	echo "\n==== Elapsed build time ($LABEL) ====\n" >>$mail_msg_file
704	tail -3  $SRC/${INSTALLOG}.out >>$mail_msg_file
705
706	if [ "$i_FLAG" = "n" -a "$W_FLAG" = "n" ]; then
707		rm -f $SRC/${NOISE}.ref
708		if [ -f $SRC/${NOISE}.out ]; then
709			mv $SRC/${NOISE}.out $SRC/${NOISE}.ref
710		fi
711		grep : $SRC/${INSTALLOG}.out \
712			| egrep -v '^/' \
713			| egrep -v '^(Start|Finish|real|user|sys|./bld_awk)' \
714			| egrep -v '^tic:' \
715			| egrep -v '^mcs' \
716			| egrep -v '^LD_LIBRARY_PATH=' \
717			| egrep -v 'ar: creating' \
718			| egrep -v 'ar: writing' \
719			| egrep -v 'conflicts:' \
720			| egrep -v ':saved created' \
721			| egrep -v '^stty.*c:' \
722			| egrep -v '^mfgname.c:' \
723			| egrep -v '^uname-i.c:' \
724			| egrep -v '^volumes.c:' \
725			| egrep -v '^lint library construction:' \
726			| egrep -v 'tsort: INFORM:' \
727			| egrep -v 'stripalign:' \
728			| egrep -v 'chars, width' \
729			| egrep -v "symbol (\`|')timezone' has differing types:" \
730			| egrep -v 'PSTAMP' \
731			| egrep -v '|%WHOANDWHERE%|' \
732			| egrep -v '^Manifying' \
733			| egrep -v 'Ignoring unknown host' \
734			| egrep -v 'Processing method:' \
735			| egrep -v '^Writing' \
736			| egrep -v 'spellin1:' \
737			| egrep -v '^adding:' \
738			| egrep -v "^echo 'msgid" \
739			| egrep -v '^echo ' \
740			| egrep -v '\.c:$' \
741			| egrep -v '^Adding file:' \
742			| egrep -v 'CLASSPATH=' \
743			| egrep -v '\/var\/mail\/:saved' \
744			| egrep -v -- '-DUTS_VERSION=' \
745			| egrep -v '^Running Mkbootstrap' \
746			| egrep -v '^Applet length read:' \
747			| egrep -v 'bytes written:' \
748			| egrep -v '^File:SolarisAuthApplet.bin' \
749			| egrep -v -i 'jibversion' \
750			| egrep -v '^Output size:' \
751			| egrep -v '^Solo size statistics:' \
752			| egrep -v '^Using ROM API Version' \
753			| egrep -v '^Zero Signature length:' \
754			| egrep -v '^Note \(probably harmless\):' \
755			| egrep -v '::' \
756			| egrep -v -- '-xcache' \
757			| egrep -v '^\+' \
758			| egrep -v '^cc1: note: -fwritable-strings' \
759			| egrep -v 'svccfg-native -s svc:/' \
760			| sort | uniq >$SRC/${NOISE}.out
761		if [ ! -f $SRC/${NOISE}.ref ]; then
762			cp $SRC/${NOISE}.out $SRC/${NOISE}.ref
763		fi
764		echo "\n==== Build noise differences ($LABEL) ====\n" \
765			>>$mail_msg_file
766		diff $SRC/${NOISE}.ref $SRC/${NOISE}.out >>$mail_msg_file
767	fi
768
769	#
770	#	Re-sign selected binaries using signing server
771	#	(gatekeeper builds only)
772	#
773	if [ -n "$CODESIGN_USER" -a "$this_build_ok" = "y" ]; then
774		echo "\n==== Signing proto area at `date` ====\n" >> $LOGFILE
775		signing_file="${TMPDIR}/signing"
776		rm -f ${signing_file}
777		export CODESIGN_USER
778		signproto $SRC/tools/codesign/creds 2>&1 | \
779			tee -a ${signing_file} >> $LOGFILE
780		echo "\n==== Finished signing proto area at `date` ====\n" \
781		    >> $LOGFILE
782		echo "\n==== Crypto module signing errors ($LABEL) ====\n" \
783		    >> $mail_msg_file
784		egrep 'WARNING|ERROR' ${signing_file} >> $mail_msg_file
785		if (( $? == 0 )) ; then
786			build_ok=n
787			this_build_ok=n
788		fi
789	fi
790
791	#
792	#	Building Packages
793	#
794	if [ "$p_FLAG" = "y" -a "$this_build_ok" = "y" ]; then
795		if [ -d $SRC/pkg -o -d $SRC/pkgdefs ]; then
796			echo "\n==== Creating $LABEL packages at `date` ====\n" \
797				>> $LOGFILE
798			echo "Clearing out $PKGARCHIVE ..." >> $LOGFILE
799			rm -rf $PKGARCHIVE >> "$LOGFILE" 2>&1
800			mkdir -p $PKGARCHIVE >> "$LOGFILE" 2>&1
801
802			for d in pkg pkgdefs; do
803				if [ ! -f "$SRC/$d/Makefile" ]; then
804					continue
805				fi
806				rm -f $SRC/$d/${INSTALLOG}.out
807				cd $SRC/$d
808				/bin/time $MAKE -e install 2>&1 | \
809					tee -a $SRC/$d/${INSTALLOG}.out >> $LOGFILE
810			done
811
812			echo "\n==== package build errors ($LABEL) ====\n" \
813				>> $mail_msg_file
814
815			for d in pkg pkgdefs; do
816				if [ ! -f "$SRC/$d/Makefile" ]; then
817					continue
818				fi
819
820				egrep "${MAKE}|ERROR|WARNING" $SRC/$d/${INSTALLOG}.out | \
821					grep ':' | \
822					grep -v PSTAMP | \
823					egrep -v "Ignoring unknown host" \
824					>> $mail_msg_file
825			done
826		else
827			#
828			# Handle it gracefully if -p was set but there are
829			# neither pkg nor pkgdefs directories.
830			#
831			echo "\n==== No $LABEL packages to build ====\n" \
832				>> $LOGFILE
833		fi
834	else
835		echo "\n==== Not creating $LABEL packages ====\n" >> $LOGFILE
836	fi
837
838	ROOT=$ORIGROOT
839}
840
841# Usage: dolint /dir y|n
842# Arg. 2 is a flag to turn on/off the lint diff output
843function dolint {
844	if [ ! -d "$1" ]; then
845		echo "dolint error: $1 is not a directory"
846		exit 1
847	fi
848
849	if [ "$2" != "y" -a "$2" != "n" ]; then
850		echo "dolint internal error: $2 should be 'y' or 'n'"
851		exit 1
852	fi
853
854	lintdir=$1
855	dodiff=$2
856	base=`basename $lintdir`
857	LINTOUT=$lintdir/lint-${MACH}.out
858	LINTNOISE=$lintdir/lint-noise-${MACH}
859	export ENVLDLIBS1=`myldlibs $ROOT`
860	export ENVCPPFLAGS1=`myheaders $ROOT`
861
862	set_debug_build_flags
863
864	#
865	#	'$MAKE lint' in $lintdir
866	#
867	echo "\n==== Begin '$MAKE lint' of $base at `date` ====\n" >> $LOGFILE
868
869	# remove old lint.out
870	rm -f $lintdir/lint.out $lintdir/lint-noise.out
871	if [ -f $lintdir/lint-noise.ref ]; then
872		mv $lintdir/lint-noise.ref ${LINTNOISE}.ref
873	fi
874
875	rm -f $LINTOUT
876	cd $lintdir
877	#
878	# Remove all .ln files to ensure a full reference file
879	#
880	rm -f Nothing_to_remove \
881	    `find . \( -name SCCS -o -name .hg -o -name .svn \) \
882	    	-prune -o -type f -name '*.ln' -print `
883
884	/bin/time $MAKE -ek lint 2>&1 | \
885	    tee -a $LINTOUT >> $LOGFILE
886	echo "\n==== '$MAKE lint' of $base ERRORS ====\n" >> $mail_msg_file
887	grep "$MAKE:" $LINTOUT |
888		egrep -v "Ignoring unknown host" \
889		>> $mail_msg_file
890
891	echo "\n==== Ended '$MAKE lint' of $base at `date` ====\n" >> $LOGFILE
892
893	echo "\n==== Elapsed time of '$MAKE lint' of $base ====\n" \
894		>>$mail_msg_file
895	tail -3  $LINTOUT >>$mail_msg_file
896
897	rm -f ${LINTNOISE}.ref
898	if [ -f ${LINTNOISE}.out ]; then
899		mv ${LINTNOISE}.out ${LINTNOISE}.ref
900	fi
901        grep : $LINTOUT | \
902		egrep -v '^(real|user|sys)' |
903		egrep -v '(library construction)' | \
904		egrep -v ': global crosschecks' | \
905		egrep -v 'Ignoring unknown host' | \
906		egrep -v '\.c:$' | \
907		sort | uniq > ${LINTNOISE}.out
908	if [ ! -f ${LINTNOISE}.ref ]; then
909		cp ${LINTNOISE}.out ${LINTNOISE}.ref
910	fi
911	if [ "$dodiff" != "n" ]; then
912		echo "\n==== lint warnings $base ====\n" \
913			>>$mail_msg_file
914		# should be none, though there are a few that were filtered out
915		# above
916		egrep -i '(warning|lint):' ${LINTNOISE}.out \
917			| sort | uniq >> $mail_msg_file
918		echo "\n==== lint noise differences $base ====\n" \
919			>> $mail_msg_file
920		diff ${LINTNOISE}.ref ${LINTNOISE}.out \
921			>> $mail_msg_file
922	fi
923}
924
925# Install proto area from IHV build
926
927function copy_ihv_proto {
928
929	echo "\n==== Installing IHV proto area ====\n" \
930		>> $LOGFILE
931	if [ -d "$IA32_IHV_ROOT" ]; then
932		if [ ! -d "$ROOT" ]; then
933			echo "mkdir -p $ROOT" >> $LOGFILE
934			mkdir -p $ROOT
935		fi
936		echo "copying $IA32_IHV_ROOT to $ROOT\n" >> $LOGFILE
937		cd $IA32_IHV_ROOT
938		tar cf - . | (cd $ROOT; umask 0; tar xpf - ) 2>&1 >> $LOGFILE
939	else
940		echo "$IA32_IHV_ROOT: not found" >> $LOGFILE
941	fi
942
943	if [ "$MULTI_PROTO" = yes ]; then
944		if [ ! -d "$ROOT-nd" ]; then
945			echo "mkdir -p $ROOT-nd" >> $LOGFILE
946			mkdir -p $ROOT-nd
947		fi
948		# If there's a non-DEBUG version of the IHV proto area,
949		# copy it, but copy something if there's not.
950		if [ -d "$IA32_IHV_ROOT-nd" ]; then
951			echo "copying $IA32_IHV_ROOT-nd to $ROOT-nd\n" >> $LOGFILE
952			cd $IA32_IHV_ROOT-nd
953		elif [ -d "$IA32_IHV_ROOT" ]; then
954			echo "copying $IA32_IHV_ROOT to $ROOT-nd\n" >> $LOGFILE
955			cd $IA32_IHV_ROOT
956		else
957			echo "$IA32_IHV_ROOT{-nd,}: not found" >> $LOGFILE
958			return
959		fi
960		tar cf - . | (cd $ROOT-nd; umask 0; tar xpf - ) 2>&1 >> $LOGFILE
961	fi
962}
963
964# Install IHV packages in PKGARCHIVE
965# usage: copy_ihv_pkgs LABEL SUFFIX
966function copy_ihv_pkgs {
967	LABEL=$1
968	SUFFIX=$2
969	# always use non-DEBUG IHV packages
970	IA32_IHV_PKGS=${IA32_IHV_PKGS_ORIG}-nd
971	PKGARCHIVE=${PKGARCHIVE_ORIG}${SUFFIX}
972
973	echo "\n==== Installing IHV packages from $IA32_IHV_PKGS ($LABEL) ====\n" \
974		>> $LOGFILE
975	if [ -d "$IA32_IHV_PKGS" ]; then
976		cd $IA32_IHV_PKGS
977		tar cf - * | \
978		   (cd $PKGARCHIVE; umask 0; tar xpf - ) 2>&1 >> $LOGFILE
979	else
980		echo "$IA32_IHV_PKGS: not found" >> $LOGFILE
981	fi
982
983	echo "\n==== Installing IHV packages from $IA32_IHV_BINARY_PKGS ($LABEL) ====\n" \
984		>> $LOGFILE
985	if [ -d "$IA32_IHV_BINARY_PKGS" ]; then
986		cd $IA32_IHV_BINARY_PKGS
987		tar cf - * | \
988		    (cd $PKGARCHIVE; umask 0; tar xpf - ) 2>&1 >> $LOGFILE
989	else
990		echo "$IA32_IHV_BINARY_PKGS: not found" >> $LOGFILE
991	fi
992}
993
994#
995# Build and install the onbld tools.
996#
997# usage: build_tools DESTROOT
998#
999# returns non-zero status if the build was successful.
1000#
1001function build_tools {
1002	DESTROOT=$1
1003
1004	INSTALLOG=install-${MACH}
1005
1006	echo "\n==== Building tools at `date` ====\n" \
1007		>> $LOGFILE
1008
1009	rm -f ${TOOLS}/${INSTALLOG}.out
1010	cd ${TOOLS}
1011	/bin/time $MAKE TOOLS_PROTO=${DESTROOT} -e install 2>&1 | \
1012	    tee -a ${TOOLS}/${INSTALLOG}.out >> $LOGFILE
1013
1014	echo "\n==== Tools build errors ====\n" >> $mail_msg_file
1015
1016	egrep ":" ${TOOLS}/${INSTALLOG}.out |
1017		egrep -e "(${MAKE}:|[ 	]error[: 	\n])" | \
1018		egrep -v "Ignoring unknown host" | \
1019		egrep -v warning >> $mail_msg_file
1020	return $?
1021}
1022
1023#
1024# Set up to use locally installed tools.
1025#
1026# usage: use_tools TOOLSROOT
1027#
1028function use_tools {
1029	TOOLSROOT=$1
1030
1031	#
1032	# If we're not building ON workspace, then the TOOLSROOT
1033	# settings here are clearly ignored by the workspace
1034	# makefiles, prepending nonexistent directories to PATH is
1035	# harmless, and we clearly do not wish to override
1036	# ONBLD_TOOLS.
1037	#
1038	# If we're building an ON workspace, then the prepended PATH
1039	# elements should supercede the preexisting ONBLD_TOOLS paths,
1040	# and we want to override ONBLD_TOOLS to catch the tools that
1041	# don't have specific path env vars here.
1042	#
1043	# So the only conditional behavior is overriding ONBLD_TOOLS,
1044	# and we check for "an ON workspace" by looking for
1045	# ${TOOLSROOT}/opt/onbld.
1046	#
1047
1048	STABS=${TOOLSROOT}/opt/onbld/bin/${MACH}/stabs
1049	export STABS
1050	CTFSTABS=${TOOLSROOT}/opt/onbld/bin/${MACH}/ctfstabs
1051	export CTFSTABS
1052	GENOFFSETS=${TOOLSROOT}/opt/onbld/bin/genoffsets
1053	export GENOFFSETS
1054
1055	CTFCONVERT=${TOOLSROOT}/opt/onbld/bin/${MACH}/ctfconvert
1056	export CTFCONVERT
1057	CTFMERGE=${TOOLSROOT}/opt/onbld/bin/${MACH}/ctfmerge
1058	export CTFMERGE
1059
1060	CTFCVTPTBL=${TOOLSROOT}/opt/onbld/bin/ctfcvtptbl
1061	export CTFCVTPTBL
1062	CTFFINDMOD=${TOOLSROOT}/opt/onbld/bin/ctffindmod
1063	export CTFFINDMOD
1064
1065	if [ "$VERIFY_ELFSIGN" = "y" ]; then
1066		ELFSIGN=${TOOLSROOT}/opt/onbld/bin/elfsigncmp
1067	else
1068		ELFSIGN=${TOOLSROOT}/opt/onbld/bin/${MACH}/elfsign
1069	fi
1070	export ELFSIGN
1071
1072	PATH="${TOOLSROOT}/opt/onbld/bin/${MACH}:${PATH}"
1073	PATH="${TOOLSROOT}/opt/onbld/bin:${PATH}"
1074	export PATH
1075
1076	if [ -d "${TOOLSROOT}/opt/onbld" ]; then
1077		ONBLD_TOOLS=${TOOLSROOT}/opt/onbld
1078		export ONBLD_TOOLS
1079	fi
1080
1081	echo "\n==== New environment settings. ====\n" >> $LOGFILE
1082	echo "STABS=${STABS}" >> $LOGFILE
1083	echo "CTFSTABS=${CTFSTABS}" >> $LOGFILE
1084	echo "CTFCONVERT=${CTFCONVERT}" >> $LOGFILE
1085	echo "CTFMERGE=${CTFMERGE}" >> $LOGFILE
1086	echo "CTFCVTPTBL=${CTFCVTPTBL}" >> $LOGFILE
1087	echo "CTFFINDMOD=${CTFFINDMOD}" >> $LOGFILE
1088	echo "ELFSIGN=${ELFSIGN}" >> $LOGFILE
1089	echo "PATH=${PATH}" >> $LOGFILE
1090	echo "ONBLD_TOOLS=${ONBLD_TOOLS}" >> $LOGFILE
1091}
1092
1093function staffer {
1094	if [ $ISUSER -ne 0 ]; then
1095		"$@"
1096	else
1097		arg="\"$1\""
1098		shift
1099		for i
1100		do
1101			arg="$arg \"$i\""
1102		done
1103		eval su $STAFFER -c \'$arg\'
1104	fi
1105}
1106
1107#
1108# Verify that the closed tree is present if it needs to be.
1109# Sets CLOSED_IS_PRESENT for future use.
1110#
1111function check_closed_tree {
1112	if [ -z "$CLOSED_IS_PRESENT" ]; then
1113		if [ -d $CODEMGR_WS/usr/closed ]; then
1114			CLOSED_IS_PRESENT="yes"
1115		else
1116			CLOSED_IS_PRESENT="no"
1117		fi
1118		export CLOSED_IS_PRESENT
1119	fi
1120	if [[ "$CLOSED_IS_PRESENT" = no && ! -d "$ON_CLOSED_BINS" ]]; then
1121		#
1122		# If it's an old (pre-split) tree or an empty
1123		# workspace, don't complain.
1124		#
1125		if grep -s CLOSED_BUILD $SRC/Makefile.master > /dev/null; then
1126			echo "If the closed sources are not present," \
1127			    "ON_CLOSED_BINS"
1128			echo "must point to the closed binaries tree."
1129			build_ok=n
1130			exit 1
1131		fi
1132	fi
1133}
1134
1135function obsolete_build {
1136    	echo "WARNING: Obsolete $1 build requested; request will be ignored"
1137}
1138
1139#
1140# wrapper over wsdiff.
1141# usage: do_wsdiff LABEL OLDPROTO NEWPROTO
1142#
1143function do_wsdiff {
1144	label=$1
1145	oldproto=$2
1146	newproto=$3
1147
1148	wsdiff="wsdiff"
1149	[ "$t_FLAG" = y ] && wsdiff="wsdiff -t"
1150
1151	echo "\n==== Getting object changes since last build at `date`" \
1152	    "($label) ====\n" | tee -a $LOGFILE >> $mail_msg_file
1153	$wsdiff -s -r ${TMPDIR}/wsdiff.results $oldproto $newproto 2>&1 | \
1154		    tee -a $LOGFILE >> $mail_msg_file
1155	echo "\n==== Object changes determined at `date` ($label) ====\n" | \
1156	    tee -a $LOGFILE >> $mail_msg_file
1157}
1158
1159#
1160# Functions for setting build flags (DEBUG/non-DEBUG).  Keep them
1161# together.
1162#
1163
1164function set_non_debug_build_flags {
1165	export INTERNAL_RELEASE_BUILD ; INTERNAL_RELEASE_BUILD=
1166	export RELEASE_BUILD ; RELEASE_BUILD=
1167	unset EXTRA_OPTIONS
1168	unset EXTRA_CFLAGS
1169}
1170
1171function set_debug_build_flags {
1172	export INTERNAL_RELEASE_BUILD ; INTERNAL_RELEASE_BUILD=
1173	unset RELEASE_BUILD
1174	unset EXTRA_OPTIONS
1175	unset EXTRA_CFLAGS
1176}
1177
1178
1179MACH=`uname -p`
1180
1181if [ "$OPTHOME" = "" ]; then
1182	OPTHOME=/opt
1183	export OPTHOME
1184fi
1185if [ "$TEAMWARE" = "" ]; then
1186	TEAMWARE=$OPTHOME/teamware
1187	export TEAMWARE
1188fi
1189
1190USAGE='Usage: nightly [-in] [+t] [-V VERS ] [ -S E|D|H|O ] <env_file>
1191
1192Where:
1193	-i	Fast incremental options (no clobber, lint, check)
1194	-n      Do not do a bringover
1195	+t	Use the build tools in $ONBLD_TOOLS/bin
1196	-V VERS set the build version string to VERS
1197	-S	Build a variant of the source product
1198		E - build exportable source
1199		D - build domestic source (exportable + crypt)
1200		H - build hybrid source (binaries + deleted source)
1201		O - build (only) open source
1202
1203	<env_file>  file in Bourne shell syntax that sets and exports
1204	variables that configure the operation of this script and many of
1205	the scripts this one calls. If <env_file> does not exist,
1206	it will be looked for in $OPTHOME/onbld/env.
1207
1208non-DEBUG is the default build type. Build options can be set in the
1209NIGHTLY_OPTIONS variable in the <env_file> as follows:
1210
1211	-A	check for ABI differences in .so files
1212	-C	check for cstyle/hdrchk errors
1213	-D	do a build with DEBUG on
1214	-F	do _not_ do a non-DEBUG build
1215	-G	gate keeper default group of options (-au)
1216	-I	integration engineer default group of options (-ampu)
1217	-M	do not run pmodes (safe file permission checker)
1218	-N	do not run protocmp
1219	-O	generate OpenSolaris deliverables
1220	-R	default group of options for building a release (-mp)
1221	-U	update proto area in the parent
1222	-V VERS set the build version string to VERS
1223	-X	copy x86 IHV proto area
1224	-f	find unreferenced files
1225	-i	do an incremental build (no "make clobber")
1226	-l	do "make lint" in $LINTDIRS (default: $SRC y)
1227	-m	send mail to $MAILTO at end of build
1228	-n      do not do a bringover
1229	-o	build using root privileges to set OWNER/GROUP (old style)
1230	-p	create packages
1231	-r	check ELF runtime attributes in the proto area
1232	-t	build and use the tools in $SRC/tools (default setting)
1233	+t	Use the build tools in $ONBLD_TOOLS/bin
1234	-u	update proto_list_$MACH and friends in the parent workspace;
1235		when used with -f, also build an unrefmaster.out in the parent
1236	-w	report on differences between previous and current proto areas
1237	-z	compress cpio archives with gzip
1238	-W	Do not report warnings (freeware gate ONLY)
1239	-S	Build a variant of the source product
1240		E - build exportable source
1241		D - build domestic source (exportable + crypt)
1242		H - build hybrid source (binaries + deleted source)
1243		O - build (only) open source
1244'
1245#
1246#	-x	less public handling of xmod source for the source product
1247#
1248#	A log file will be generated under the name $LOGFILE
1249#	for partially completed build and log.`date '+%F'`
1250#	in the same directory for fully completed builds.
1251#
1252
1253# default values for low-level FLAGS; G I R are group FLAGS
1254A_FLAG=n
1255C_FLAG=n
1256D_FLAG=n
1257F_FLAG=n
1258f_FLAG=n
1259i_FLAG=n; i_CMD_LINE_FLAG=n
1260l_FLAG=n
1261M_FLAG=n
1262m_FLAG=n
1263N_FLAG=n
1264n_FLAG=n
1265O_FLAG=n
1266o_FLAG=n
1267P_FLAG=n
1268p_FLAG=n
1269r_FLAG=n
1270T_FLAG=n
1271t_FLAG=y
1272U_FLAG=n
1273u_FLAG=n
1274V_FLAG=n
1275W_FLAG=n
1276w_FLAG=n
1277X_FLAG=n
1278SD_FLAG=n
1279SE_FLAG=n
1280SH_FLAG=n
1281SO_FLAG=n
1282#
1283XMOD_OPT=
1284#
1285build_ok=y
1286
1287function is_source_build {
1288	[ "$SE_FLAG" = "y" -o "$SD_FLAG" = "y" -o \
1289	    "$SH_FLAG" = "y" -o "$SO_FLAG" = "y" ]
1290	return $?
1291}
1292
1293#
1294# examine arguments
1295#
1296
1297#
1298# single function for setting -S flag and doing error checking.
1299# usage: set_S_flag <type>
1300# where <type> is the source build type ("E", "D", ...).
1301#
1302function set_S_flag {
1303	if is_source_build; then
1304		echo "Can only build one source variant at a time."
1305		exit 1
1306	fi
1307	if [ "$1" = "E" ]; then
1308		SE_FLAG=y
1309	elif [ "$1" = "D" ]; then
1310		SD_FLAG=y
1311	elif [ "$1" = "H" ]; then
1312		SH_FLAG=y
1313	elif [ "$1" = "O" ]; then
1314		SO_FLAG=y
1315	else
1316		echo "$USAGE"
1317		exit 1
1318	fi
1319}
1320
1321OPTIND=1
1322while getopts +inS:tV: FLAG
1323do
1324	case $FLAG in
1325	  i )	i_FLAG=y; i_CMD_LINE_FLAG=y
1326		;;
1327	  n )	n_FLAG=y
1328		;;
1329	  S )
1330		set_S_flag $OPTARG
1331		;;
1332	 +t )	t_FLAG=n
1333		;;
1334	  V )	V_FLAG=y
1335		V_ARG="$OPTARG"
1336		;;
1337	 \? )	echo "$USAGE"
1338		exit 1
1339		;;
1340	esac
1341done
1342
1343# correct argument count after options
1344shift `expr $OPTIND - 1`
1345
1346# test that the path to the environment-setting file was given
1347if [ $# -ne 1 ]; then
1348	echo "$USAGE"
1349	exit 1
1350fi
1351
1352# check if user is running nightly as root
1353# ISUSER is set non-zero if an ordinary user runs nightly, or is zero
1354# when root invokes nightly.
1355/usr/bin/id | grep '^uid=0(' >/dev/null 2>&1
1356ISUSER=$?;	export ISUSER
1357
1358#
1359# force locale to C
1360LC_COLLATE=C;	export LC_COLLATE
1361LC_CTYPE=C;	export LC_CTYPE
1362LC_MESSAGES=C;	export LC_MESSAGES
1363LC_MONETARY=C;	export LC_MONETARY
1364LC_NUMERIC=C;	export LC_NUMERIC
1365LC_TIME=C;	export LC_TIME
1366
1367# clear environment variables we know to be bad for the build
1368unset LD_OPTIONS
1369unset LD_AUDIT		LD_AUDIT_32		LD_AUDIT_64
1370unset LD_BIND_NOW	LD_BIND_NOW_32		LD_BIND_NOW_64
1371unset LD_BREADTH	LD_BREADTH_32		LD_BREADTH_64
1372unset LD_CONFIG		LD_CONFIG_32		LD_CONFIG_64
1373unset LD_DEBUG		LD_DEBUG_32		LD_DEBUG_64
1374unset LD_DEMANGLE	LD_DEMANGLE_32		LD_DEMANGLE_64
1375unset LD_FLAGS		LD_FLAGS_32		LD_FLAGS_64
1376unset LD_LIBRARY_PATH	LD_LIBRARY_PATH_32	LD_LIBRARY_PATH_64
1377unset LD_LOADFLTR	LD_LOADFLTR_32		LD_LOADFLTR_64
1378unset LD_NOAUDIT	LD_NOAUDIT_32		LD_NOAUDIT_64
1379unset LD_NOAUXFLTR	LD_NOAUXFLTR_32		LD_NOAUXFLTR_64
1380unset LD_NOCONFIG	LD_NOCONFIG_32		LD_NOCONFIG_64
1381unset LD_NODIRCONFIG	LD_NODIRCONFIG_32	LD_NODIRCONFIG_64
1382unset LD_NODIRECT	LD_NODIRECT_32		LD_NODIRECT_64
1383unset LD_NOLAZYLOAD	LD_NOLAZYLOAD_32	LD_NOLAZYLOAD_64
1384unset LD_NOOBJALTER	LD_NOOBJALTER_32	LD_NOOBJALTER_64
1385unset LD_NOVERSION	LD_NOVERSION_32		LD_NOVERSION_64
1386unset LD_ORIGIN		LD_ORIGIN_32		LD_ORIGIN_64
1387unset LD_PRELOAD	LD_PRELOAD_32		LD_PRELOAD_64
1388unset LD_PROFILE	LD_PROFILE_32		LD_PROFILE_64
1389
1390unset CONFIG
1391unset GROUP
1392unset OWNER
1393unset REMOTE
1394unset ENV
1395unset ARCH
1396unset CLASSPATH
1397unset NAME
1398
1399#
1400# To get ONBLD_TOOLS from the environment, it must come from the env file.
1401# If it comes interactively, it is generally TOOLS_PROTO, which will be
1402# clobbered before the compiler version checks, which will therefore fail.
1403#
1404unset ONBLD_TOOLS
1405
1406#
1407#	Setup environmental variables
1408#
1409if [ -f /etc/nightly.conf ]; then
1410	. /etc/nightly.conf
1411fi
1412
1413if [ -f $1 ]; then
1414	if [[ $1 = */* ]]; then
1415		. $1
1416	else
1417		. ./$1
1418	fi
1419else
1420	if [ -f $OPTHOME/onbld/env/$1 ]; then
1421		. $OPTHOME/onbld/env/$1
1422	else
1423		echo "Cannot find env file as either $1 or $OPTHOME/onbld/env/$1"
1424		exit 1
1425	fi
1426fi
1427
1428# contents of stdenv.sh inserted after next line:
1429# STDENV_START
1430# STDENV_END
1431
1432#
1433# place ourselves in a new task, respecting BUILD_PROJECT if set.
1434#
1435if [ -z "$BUILD_PROJECT" ]; then
1436	/usr/bin/newtask -c $$
1437else
1438	/usr/bin/newtask -c $$ -p $BUILD_PROJECT
1439fi
1440
1441ps -o taskid= -p $$ | read build_taskid
1442ps -o project= -p $$ | read build_project
1443
1444#
1445# See if NIGHTLY_OPTIONS is set
1446#
1447if [ "$NIGHTLY_OPTIONS" = "" ]; then
1448	NIGHTLY_OPTIONS="-aBm"
1449fi
1450
1451#
1452# If BRINGOVER_WS was not specified, let it default to CLONE_WS
1453#
1454if [ "$BRINGOVER_WS" = "" ]; then
1455	BRINGOVER_WS=$CLONE_WS
1456fi
1457
1458#
1459# If CLOSED_BRINGOVER_WS was not specified, let it default to CLOSED_CLONE_WS
1460#
1461if [ "$CLOSED_BRINGOVER_WS" = "" ]; then
1462	CLOSED_BRINGOVER_WS=$CLOSED_CLONE_WS
1463fi
1464
1465#
1466# If BRINGOVER_FILES was not specified, default to usr
1467#
1468if [ "$BRINGOVER_FILES" = "" ]; then
1469	BRINGOVER_FILES="usr"
1470fi
1471
1472#
1473# If the closed sources are not present, the closed binaries must be
1474# present for the build to succeed.  If there's no pointer to the
1475# closed binaries, flag that now, rather than forcing the user to wait
1476# a couple hours (or more) to find out.
1477#
1478orig_closed_is_present="$CLOSED_IS_PRESENT"
1479check_closed_tree
1480
1481#
1482# Note: changes to the option letters here should also be applied to the
1483#	bldenv script.  `d' is listed for backward compatibility.
1484#
1485NIGHTLY_OPTIONS=-${NIGHTLY_OPTIONS#-}
1486OPTIND=1
1487while getopts +ABCDdFfGIilMmNnOoPpRrS:TtUuWwXxz FLAG $NIGHTLY_OPTIONS
1488do
1489	case $FLAG in
1490	  A )	A_FLAG=y
1491		#
1492		# If ELF_DATA_BASELINE_DIR is not defined, and we are on SWAN
1493		# (based on CLOSED_IS_PRESENT), then refuse to run. The value
1494		# of ELF version checking is greatly enhanced by including
1495		# the baseline gate comparison.
1496		if [ "$CLOSED_IS_PRESENT" = 'yes' -a \
1497		     "$ELF_DATA_BASELINE_DIR" = '' ]; then
1498			echo "ELF_DATA_BASELINE_DIR must be set if the A" \
1499			    "flag is present in\nNIGHTLY_OPTIONS and closed" \
1500			    "sources are present. Update environment file."
1501			exit 1;
1502		fi
1503		;;
1504	  B )	D_FLAG=y
1505		;; # old version of D
1506	  C )	C_FLAG=y
1507		;;
1508	  D )	D_FLAG=y
1509		;;
1510	  F )	F_FLAG=y
1511		;;
1512	  f )	f_FLAG=y
1513		;;
1514	  G )   u_FLAG=y
1515		;;
1516	  I )	m_FLAG=y
1517		p_FLAG=y
1518		u_FLAG=y
1519		;;
1520	  i )	i_FLAG=y
1521		;;
1522	  l )	l_FLAG=y
1523		;;
1524	  M )	M_FLAG=y
1525		;;
1526	  m )	m_FLAG=y
1527		;;
1528	  N )	N_FLAG=y
1529		;;
1530	  n )	n_FLAG=y
1531		;;
1532	  O )	O_FLAG=y
1533		;;
1534	  o )	o_FLAG=y
1535		;;
1536	  P )	P_FLAG=y
1537		;; # obsolete
1538	  p )	p_FLAG=y
1539		;;
1540	  R )	m_FLAG=y
1541		p_FLAG=y
1542		;;
1543	  r )	r_FLAG=y
1544		;;
1545	  S )
1546		set_S_flag $OPTARG
1547		;;
1548	  T )	T_FLAG=y
1549		;; # obsolete
1550	 +t )	t_FLAG=n
1551		;;
1552	  U )   if [ -z "${PARENT_ROOT}" ]; then
1553			echo "PARENT_ROOT must be set if the U flag is" \
1554			    "present in NIGHTLY_OPTIONS."
1555			exit 1
1556		fi
1557		NIGHTLY_PARENT_ROOT=$PARENT_ROOT
1558		if [ -n "${PARENT_TOOLS_ROOT}" ]; then
1559			NIGHTLY_PARENT_TOOLS_ROOT=$PARENT_TOOLS_ROOT
1560		fi
1561		U_FLAG=y
1562		;;
1563	  u )	u_FLAG=y
1564		;;
1565	  W )	W_FLAG=y
1566		;;
1567
1568	  w )	w_FLAG=y
1569		;;
1570	  X )	# now that we no longer need realmode builds, just
1571		# copy IHV packages.  only meaningful on x86.
1572		if [ "$MACH" = "i386" ]; then
1573			X_FLAG=y
1574		fi
1575		;;
1576	  x )	XMOD_OPT="-x"
1577		;;
1578	 \? )	echo "$USAGE"
1579		exit 1
1580		;;
1581	esac
1582done
1583
1584if [ $ISUSER -ne 0 ]; then
1585	if [ "$o_FLAG" = "y" ]; then
1586		echo "Old-style build requires root permission."
1587		exit 1
1588	fi
1589
1590	# Set default value for STAFFER, if needed.
1591	if [ -z "$STAFFER" -o "$STAFFER" = "nobody" ]; then
1592		STAFFER=`/usr/xpg4/bin/id -un`
1593		export STAFFER
1594	fi
1595fi
1596
1597if [ -z "$MAILTO" -o "$MAILTO" = "nobody" ]; then
1598	MAILTO=$STAFFER
1599	export MAILTO
1600fi
1601
1602PATH="$OPTHOME/onbld/bin:$OPTHOME/onbld/bin/${MACH}:/usr/ccs/bin"
1603PATH="$PATH:$OPTHOME/SUNWspro/bin:$TEAMWARE/bin:/usr/bin:/usr/sbin:/usr/ucb"
1604PATH="$PATH:/usr/openwin/bin:/usr/sfw/bin:/opt/sfw/bin:."
1605export PATH
1606
1607# roots of source trees, both relative to $SRC and absolute.
1608relsrcdirs="."
1609if [[ -d $CODEMGR_WS/usr/closed && "$CLOSED_IS_PRESENT" != no ]]; then
1610	relsrcdirs="$relsrcdirs ../closed"
1611fi
1612abssrcdirs=""
1613for d in $relsrcdirs; do
1614	abssrcdirs="$abssrcdirs $SRC/$d"
1615done
1616
1617unset CH
1618if [ "$o_FLAG" = "y" ]; then
1619# root invoked old-style build -- make sure it works as it always has
1620# by exporting 'CH'.  The current Makefile.master doesn't use this, but
1621# the old ones still do.
1622	PROTOCMPTERSE="protocmp.terse"
1623	CH=
1624	export CH
1625else
1626	PROTOCMPTERSE="protocmp.terse -gu"
1627fi
1628POUND_SIGN="#"
1629# have we set RELEASE_DATE in our env file?
1630if [ -z "$RELEASE_DATE" ]; then
1631	RELEASE_DATE=$(LC_ALL=C date +"%B %Y")
1632fi
1633BUILD_DATE=$(LC_ALL=C date +%Y-%b-%d)
1634BASEWSDIR=$(basename $CODEMGR_WS)
1635DEV_CM="\"@(#)SunOS Internal Development: $LOGNAME $BUILD_DATE [$BASEWSDIR]\""
1636
1637# we export POUND_SIGN, RELEASE_DATE and DEV_CM to speed up the build process
1638# by avoiding repeated shell invocations to evaluate Makefile.master definitions.
1639# we export o_FLAG and X_FLAG for use by makebfu, and by usr/src/pkg/Makefile
1640export o_FLAG X_FLAG POUND_SIGN RELEASE_DATE DEV_CM
1641
1642maketype="distributed"
1643MAKE=dmake
1644# get the dmake version string alone
1645DMAKE_VERSION=$( $MAKE -v )
1646DMAKE_VERSION=${DMAKE_VERSION#*: }
1647# focus in on just the dotted version number alone
1648DMAKE_MAJOR=$( echo $DMAKE_VERSION | \
1649	sed -e 's/.*\<\([^.]*\.[^   ]*\).*$/\1/' )
1650# extract the second (or final) integer
1651DMAKE_MINOR=${DMAKE_MAJOR#*.}
1652DMAKE_MINOR=${DMAKE_MINOR%%.*}
1653# extract the first integer
1654DMAKE_MAJOR=${DMAKE_MAJOR%%.*}
1655CHECK_DMAKE=${CHECK_DMAKE:-y}
1656# x86 was built on the 12th, sparc on the 13th.
1657if [ "$CHECK_DMAKE" = "y" -a \
1658     "$DMAKE_VERSION" != "Sun Distributed Make 7.3 2003/03/12" -a \
1659     "$DMAKE_VERSION" != "Sun Distributed Make 7.3 2003/03/13" -a \( \
1660     "$DMAKE_MAJOR" -lt 7 -o \
1661     "$DMAKE_MAJOR" -eq 7 -a "$DMAKE_MINOR" -lt 4 \) ]; then
1662	if [ -z "$DMAKE_VERSION" ]; then
1663		echo "$MAKE is missing."
1664		exit 1
1665	fi
1666	echo `whence $MAKE`" version is:"
1667	echo "  ${DMAKE_VERSION}"
1668	cat <<EOF
1669
1670This version may not be safe for use.  Either set TEAMWARE to a better
1671path or (if you really want to use this version of dmake anyway), add
1672the following to your environment to disable this check:
1673
1674  CHECK_DMAKE=n
1675EOF
1676	exit 1
1677fi
1678export PATH
1679export MAKE
1680
1681#
1682# Make sure the crypto tarball is available if it's needed.
1683#
1684
1685# Echo the non-DEBUG name corresponding to the given crypto tarball path.
1686function ndcrypto {
1687	typeset dir file
1688
1689	if [ -z "$1" ]; then
1690		echo ""
1691		return
1692	fi
1693
1694	dir=$(dirname "$1")
1695	file=$(basename "$1" ".$MACH.tar.bz2")
1696
1697	echo "$dir/$file-nd.$MACH.tar.bz2"
1698}
1699
1700# Return 0 (success) if the required crypto tarball(s) are present.
1701function crypto_is_present {
1702	if [ -z "$ON_CRYPTO_BINS" ]; then
1703		echo "ON_CRYPTO_BINS is null or not set."
1704		return 1
1705	fi
1706	if [ "$D_FLAG" = y ]; then
1707		if [ ! -f "$ON_CRYPTO_BINS" ]; then
1708			echo "DEBUG crypto tarball is unavailable."
1709			return 1
1710		fi
1711	fi
1712	if [ "$F_FLAG" = n ]; then
1713		if [ ! -f $(ndcrypto "$ON_CRYPTO_BINS") ]; then
1714			echo "Non-DEBUG crypto tarball is unavailable."
1715			return 1
1716		fi
1717	fi
1718
1719	return 0
1720}
1721
1722#
1723# Canonicalize ON_CRYPTO_BINS, just in case it was set to the -nd
1724# tarball.
1725#
1726if [ -n "$ON_CRYPTO_BINS" ]; then
1727	export ON_CRYPTO_BINS=$(echo "$ON_CRYPTO_BINS" |
1728	    sed -e s/-nd.$MACH.tar/.$MACH.tar/)
1729fi
1730
1731if [[ "$O_FLAG" = y && -z "$CODESIGN_USER" ]]; then
1732	if ! crypto_is_present; then
1733		echo "OpenSolaris deliveries need signed crypto."
1734		exit 1
1735	fi
1736fi
1737
1738if [[ "$O_FLAG" = y ]]; then
1739	export TONICBUILD=""
1740else
1741	export TONICBUILD="#"
1742fi
1743
1744if [ "${SUNWSPRO}" != "" ]; then
1745	PATH="${SUNWSPRO}/bin:$PATH"
1746	export PATH
1747fi
1748
1749hostname=$(uname -n)
1750if [[ $DMAKE_MAX_JOBS != +([0-9]) || $DMAKE_MAX_JOBS -eq 0 ]]
1751then
1752	maxjobs=
1753	if [[ -f $HOME/.make.machines ]]
1754	then
1755		# Note: there is a hard tab and space character in the []s
1756		# below.
1757		egrep -i "^[ 	]*$hostname[ 	\.]" \
1758			$HOME/.make.machines | read host jobs
1759		maxjobs=${jobs##*=}
1760	fi
1761
1762	if [[ $maxjobs != +([0-9]) || $maxjobs -eq 0 ]]
1763	then
1764		# default
1765		maxjobs=4
1766	fi
1767
1768	export DMAKE_MAX_JOBS=$maxjobs
1769fi
1770
1771DMAKE_MODE=parallel;
1772export DMAKE_MODE
1773
1774if [ -z "${ROOT}" ]; then
1775	echo "ROOT must be set."
1776	exit 1
1777fi
1778
1779#
1780# if -V flag was given, reset VERSION to V_ARG
1781#
1782if [ "$V_FLAG" = "y" ]; then
1783	VERSION=$V_ARG
1784fi
1785
1786#
1787# Check for IHV root for copying ihv proto area
1788#
1789if [ "$X_FLAG" = "y" ]; then
1790        if [ "$IA32_IHV_ROOT" = "" ]; then
1791		echo "IA32_IHV_ROOT: must be set for copying ihv proto"
1792		args_ok=n
1793        fi
1794        if [ ! -d "$IA32_IHV_ROOT" ]; then
1795                echo "$IA32_IHV_ROOT: not found"
1796                args_ok=n
1797        fi
1798        if [ "$IA32_IHV_WS" = "" ]; then
1799		echo "IA32_IHV_WS: must be set for copying ihv proto"
1800		args_ok=n
1801        fi
1802        if [ ! -d "$IA32_IHV_WS" ]; then
1803                echo "$IA32_IHV_WS: not found"
1804                args_ok=n
1805        fi
1806fi
1807
1808# Append source version
1809if [ "$SE_FLAG" = "y" ]; then
1810	VERSION="${VERSION}:EXPORT"
1811fi
1812
1813if [ "$SD_FLAG" = "y" ]; then
1814	VERSION="${VERSION}:DOMESTIC"
1815fi
1816
1817if [ "$SH_FLAG" = "y" ]; then
1818	VERSION="${VERSION}:MODIFIED_SOURCE_PRODUCT"
1819fi
1820
1821if [ "$SO_FLAG" = "y" ]; then
1822	VERSION="${VERSION}:OPEN_ONLY"
1823fi
1824
1825TMPDIR="/tmp/nightly.tmpdir.$$"
1826export TMPDIR
1827rm -rf ${TMPDIR}
1828mkdir -p $TMPDIR || exit 1
1829chmod 777 $TMPDIR
1830
1831#
1832# Keep elfsign's use of pkcs11_softtoken from looking in the user home
1833# directory, which doesn't always work.   Needed until all build machines
1834# have the fix for 6271754
1835#
1836SOFTTOKEN_DIR=$TMPDIR
1837export SOFTTOKEN_DIR
1838
1839#
1840# Tools should only be built non-DEBUG.  Keep track of the tools proto
1841# area path relative to $TOOLS, because the latter changes in an
1842# export build.
1843#
1844# TOOLS_PROTO is included below for builds other than usr/src/tools
1845# that look for this location.  For usr/src/tools, this will be
1846# overridden on the $MAKE command line in build_tools().
1847#
1848TOOLS=${SRC}/tools
1849TOOLS_PROTO_REL=proto/root_${MACH}-nd
1850TOOLS_PROTO=${TOOLS}/${TOOLS_PROTO_REL};	export TOOLS_PROTO
1851
1852unset   CFLAGS LD_LIBRARY_PATH LDFLAGS
1853
1854# create directories that are automatically removed if the nightly script
1855# fails to start correctly
1856function newdir {
1857	dir=$1
1858	toadd=
1859	while [ ! -d $dir ]; do
1860		toadd="$dir $toadd"
1861		dir=`dirname $dir`
1862	done
1863	torm=
1864	newlist=
1865	for dir in $toadd; do
1866		if staffer mkdir $dir; then
1867			newlist="$ISUSER $dir $newlist"
1868			torm="$dir $torm"
1869		else
1870			[ -z "$torm" ] || staffer rmdir $torm
1871			return 1
1872		fi
1873	done
1874	newdirlist="$newlist $newdirlist"
1875	return 0
1876}
1877newdirlist=
1878
1879[ -d $CODEMGR_WS ] || newdir $CODEMGR_WS || exit 1
1880
1881# since this script assumes the build is from full source, it nullifies
1882# variables likely to have been set by a "ws" script; nullification
1883# confines the search space for headers and libraries to the proto area
1884# built from this immediate source.
1885ENVLDLIBS1=
1886ENVLDLIBS2=
1887ENVLDLIBS3=
1888ENVCPPFLAGS1=
1889ENVCPPFLAGS2=
1890ENVCPPFLAGS3=
1891ENVCPPFLAGS4=
1892PARENT_ROOT=
1893
1894export ENVLDLIBS3 ENVCPPFLAGS1 ENVCPPFLAGS2 ENVCPPFLAGS3 ENVCPPFLAGS4 \
1895	PARENT_ROOT
1896
1897PKGARCHIVE_ORIG=$PKGARCHIVE
1898IA32_IHV_PKGS_ORIG=$IA32_IHV_PKGS
1899
1900#
1901# Juggle the logs and optionally send mail on completion.
1902#
1903
1904function logshuffle {
1905    	LLOG="$ATLOG/log.`date '+%F.%H:%M'`"
1906	if [ -f $LLOG -o -d $LLOG ]; then
1907	    	LLOG=$LLOG.$$
1908	fi
1909	mkdir $LLOG
1910	export LLOG
1911
1912	if [ "$build_ok" = "y" ]; then
1913		mv $ATLOG/proto_list_${MACH} $LLOG
1914
1915		if [ -f $ATLOG/proto_list_tools_${MACH} ]; then
1916			mv $ATLOG/proto_list_tools_${MACH} $LLOG
1917	        fi
1918
1919		if [ -f $TMPDIR/wsdiff.results ]; then
1920		    	mv $TMPDIR/wsdiff.results $LLOG
1921		fi
1922
1923		if [ -f $TMPDIR/wsdiff-nd.results ]; then
1924			mv $TMPDIR/wsdiff-nd.results $LLOG
1925		fi
1926	fi
1927
1928	#
1929	# Now that we're about to send mail, it's time to check the noise
1930	# file.  In the event that an error occurs beyond this point, it will
1931	# be recorded in the nightly.log file, but nowhere else.  This would
1932	# include only errors that cause the copying of the noise log to fail
1933	# or the mail itself not to be sent.
1934	#
1935
1936	exec >>$LOGFILE 2>&1
1937	if [ -s $build_noise_file ]; then
1938	    	echo "\n==== Nightly build noise ====\n" |
1939		    tee -a $LOGFILE >>$mail_msg_file
1940		cat $build_noise_file >>$LOGFILE
1941		cat $build_noise_file >>$mail_msg_file
1942		echo | tee -a $LOGFILE >>$mail_msg_file
1943	fi
1944	rm -f $build_noise_file
1945
1946	case "$build_ok" in
1947		y)
1948			state=Completed
1949			;;
1950		i)
1951			state=Interrupted
1952			;;
1953		*)
1954	    		state=Failed
1955			;;
1956	esac
1957	NIGHTLY_STATUS=$state
1958	export NIGHTLY_STATUS
1959
1960	run_hook POST_NIGHTLY $state
1961	run_hook SYS_POST_NIGHTLY $state
1962
1963	cat $build_time_file $build_environ_file $mail_msg_file \
1964	    > ${LLOG}/mail_msg
1965	if [ "$m_FLAG" = "y" ]; then
1966	    	cat ${LLOG}/mail_msg | /usr/bin/mailx -s \
1967	"Nightly ${MACH} Build of `basename ${CODEMGR_WS}` ${state}." \
1968			${MAILTO}
1969	fi
1970
1971	if [ "$u_FLAG" = "y" -a "$build_ok" = "y" ]; then
1972	    	staffer cp ${LLOG}/mail_msg $PARENT_WS/usr/src/mail_msg-${MACH}
1973		staffer cp $LOGFILE $PARENT_WS/usr/src/nightly-${MACH}.log
1974	fi
1975
1976	mv $LOGFILE $LLOG
1977}
1978
1979#
1980#	Remove the locks and temporary files on any exit
1981#
1982function cleanup {
1983    	logshuffle
1984
1985	[ -z "$lockfile" ] || staffer rm -f $lockfile
1986	[ -z "$atloglockfile" ] || rm -f $atloglockfile
1987	[ -z "$ulockfile" ] || staffer rm -f $ulockfile
1988	[ -z "$Ulockfile" ] || rm -f $Ulockfile
1989
1990	set -- $newdirlist
1991	while [ $# -gt 0 ]; do
1992		ISUSER=$1 staffer rmdir $2
1993		shift; shift
1994	done
1995	rm -rf $TMPDIR
1996}
1997
1998function cleanup_signal {
1999    	build_ok=i
2000	# this will trigger cleanup(), above.
2001	exit 1
2002}
2003
2004trap cleanup 0
2005trap cleanup_signal 1 2 3 15
2006
2007#
2008# Generic lock file processing -- make sure that the lock file doesn't
2009# exist.  If it does, it should name the build host and PID.  If it
2010# doesn't, then make sure we can create it.  Clean up locks that are
2011# known to be stale (assumes host name is unique among build systems
2012# for the workspace).
2013#
2014function create_lock {
2015	lockf=$1
2016	lockvar=$2
2017
2018	ldir=`dirname $lockf`
2019	[ -d $ldir ] || newdir $ldir || exit 1
2020	eval $lockvar=$lockf
2021
2022	while ! staffer ln -s $hostname.$STAFFER.$$ $lockf 2> /dev/null; do
2023		basews=`basename $CODEMGR_WS`
2024		ls -l $lockf | nawk '{print $NF}' | IFS=. read host user pid
2025		if [ "$host" != "$hostname" ]; then
2026			echo "$MACH build of $basews apparently" \
2027			    "already started by $user on $host as $pid."
2028			exit 1
2029		elif kill -s 0 $pid 2>/dev/null; then
2030			echo "$MACH build of $basews already started" \
2031			    "by $user as $pid."
2032			exit 1
2033		else
2034			# stale lock; clear it out and try again
2035			rm -f $lockf
2036		fi
2037	done
2038}
2039
2040#
2041# Return the list of interesting proto areas, depending on the current
2042# options.
2043#
2044function allprotos {
2045	typeset roots="$ROOT"
2046
2047	if [[ "$F_FLAG" = n && "$MULTI_PROTO" = yes ]]; then
2048		roots="$roots $ROOT-nd"
2049	fi
2050
2051	if [[ $O_FLAG = y ]]; then
2052		roots="$roots $ROOT-closed"
2053		[ $MULTI_PROTO = yes ] && roots="$roots $ROOT-nd-closed"
2054	fi
2055
2056	echo $roots
2057}
2058
2059# Ensure no other instance of this script is running on this host.
2060# LOCKNAME can be set in <env_file>, and is by default, but is not
2061# required due to the use of $ATLOG below.
2062if [ -n "$LOCKNAME" ]; then
2063	create_lock /tmp/$LOCKNAME "lockfile"
2064fi
2065#
2066# Create from one, two, or three other locks:
2067#	$ATLOG/nightly.lock
2068#		- protects against multiple builds in same workspace
2069#	$PARENT_WS/usr/src/nightly.$MACH.lock
2070#		- protects against multiple 'u' copy-backs
2071#	$NIGHTLY_PARENT_ROOT/nightly.lock
2072#		- protects against multiple 'U' copy-backs
2073#
2074# Overriding ISUSER to 1 causes the lock to be created as root if the
2075# script is run as root.  The default is to create it as $STAFFER.
2076ISUSER=1 create_lock $ATLOG/nightly.lock "atloglockfile"
2077if [ "$u_FLAG" = "y" ]; then
2078	create_lock $PARENT_WS/usr/src/nightly.$MACH.lock "ulockfile"
2079fi
2080if [ "$U_FLAG" = "y" ]; then
2081	# NIGHTLY_PARENT_ROOT is written as root if script invoked as root.
2082	ISUSER=1 create_lock $NIGHTLY_PARENT_ROOT/nightly.lock "Ulockfile"
2083fi
2084
2085# Locks have been taken, so we're doing a build and we're committed to
2086# the directories we may have created so far.
2087newdirlist=
2088
2089#
2090# Create mail_msg_file
2091#
2092mail_msg_file="${TMPDIR}/mail_msg"
2093touch $mail_msg_file
2094build_time_file="${TMPDIR}/build_time"
2095build_environ_file="${TMPDIR}/build_environ"
2096touch $build_environ_file
2097#
2098#	Move old LOGFILE aside
2099#	ATLOG directory already made by 'create_lock' above
2100#
2101if [ -f $LOGFILE ]; then
2102	mv -f $LOGFILE ${LOGFILE}-
2103fi
2104#
2105#	Build OsNet source
2106#
2107START_DATE=`date`
2108SECONDS=0
2109echo "\n==== Nightly $maketype build started:   $START_DATE ====" \
2110    | tee -a $LOGFILE > $build_time_file
2111
2112echo "\nBuild project:  $build_project\nBuild taskid:   $build_taskid" | \
2113    tee -a $mail_msg_file >> $LOGFILE
2114
2115# make sure we log only to the nightly build file
2116build_noise_file="${TMPDIR}/build_noise"
2117exec </dev/null >$build_noise_file 2>&1
2118
2119run_hook SYS_PRE_NIGHTLY
2120run_hook PRE_NIGHTLY
2121
2122echo "\n==== list of environment variables ====\n" >> $LOGFILE
2123env >> $LOGFILE
2124
2125echo "\n==== Nightly argument issues ====\n" | tee -a $mail_msg_file >> $LOGFILE
2126
2127if [ "$P_FLAG" = "y" ]; then
2128	obsolete_build GPROF | tee -a $mail_msg_file >> $LOGFILE
2129fi
2130
2131if [ "$T_FLAG" = "y" ]; then
2132	obsolete_build TRACE | tee -a $mail_msg_file >> $LOGFILE
2133fi
2134
2135if is_source_build; then
2136	if [ "$i_FLAG" = "y" -o "$i_CMD_LINE_FLAG" = "y" ]; then
2137		echo "WARNING: the -S flags do not support incremental" \
2138		    "builds; forcing clobber\n" | tee -a $mail_msg_file >> $LOGFILE
2139		i_FLAG=n
2140		i_CMD_LINE_FLAG=n
2141	fi
2142	if [ "$N_FLAG" = "n" ]; then
2143		echo "WARNING: the -S flags do not support protocmp;" \
2144		    "protocmp disabled\n" | \
2145		    tee -a $mail_msg_file >> $LOGFILE
2146		N_FLAG=y
2147	fi
2148	if [ "$l_FLAG" = "y" ]; then
2149		echo "WARNING: the -S flags do not support lint;" \
2150		    "lint disabled\n" | tee -a $mail_msg_file >> $LOGFILE
2151		l_FLAG=n
2152	fi
2153	if [ "$C_FLAG" = "y" ]; then
2154		echo "WARNING: the -S flags do not support cstyle;" \
2155		    "cstyle check disabled\n" | tee -a $mail_msg_file >> $LOGFILE
2156		C_FLAG=n
2157	fi
2158else
2159	if [ "$N_FLAG" = "y" ]; then
2160		if [ "$p_FLAG" = "y" ]; then
2161			cat <<EOF | tee -a $mail_msg_file >> $LOGFILE
2162WARNING: the p option (create packages) is set, but so is the N option (do
2163         not run protocmp); this is dangerous; you should unset the N option
2164EOF
2165		else
2166			cat <<EOF | tee -a $mail_msg_file >> $LOGFILE
2167Warning: the N option (do not run protocmp) is set; it probably shouldn't be
2168EOF
2169		fi
2170		echo "" | tee -a $mail_msg_file >> $LOGFILE
2171	fi
2172fi
2173
2174if [ "$D_FLAG" = "n" -a "$l_FLAG" = "y" ]; then
2175	#
2176	# In the past we just complained but went ahead with the lint
2177	# pass, even though the proto area was built non-DEBUG.  It's
2178	# unlikely that non-DEBUG headers will make a difference, but
2179	# rather than assuming it's a safe combination, force the user
2180	# to specify a DEBUG build.
2181	#
2182	echo "WARNING: DEBUG build not requested; disabling lint.\n" \
2183	    | tee -a $mail_msg_file >> $LOGFILE
2184	l_FLAG=n
2185fi
2186
2187if [ "$f_FLAG" = "y" ]; then
2188	if [ "$i_FLAG" = "y" ]; then
2189		echo "WARNING: the -f flag cannot be used during incremental" \
2190		    "builds; ignoring -f\n" | tee -a $mail_msg_file >> $LOGFILE
2191		f_FLAG=n
2192	fi
2193	if [ "${l_FLAG}${p_FLAG}" != "yy" ]; then
2194		echo "WARNING: the -f flag requires -l, and -p;" \
2195		    "ignoring -f\n" | tee -a $mail_msg_file >> $LOGFILE
2196		f_FLAG=n
2197	fi
2198fi
2199
2200if [ "$w_FLAG" = "y" -a ! -d $ROOT ]; then
2201	echo "WARNING: -w specified, but $ROOT does not exist;" \
2202	    "ignoring -w\n" | tee -a $mail_msg_file >> $LOGFILE
2203	w_FLAG=n
2204fi
2205
2206if [ "$t_FLAG" = "n" ]; then
2207	#
2208	# We're not doing a tools build, so make sure elfsign(1) is
2209	# new enough to safely sign non-crypto binaries.  We test
2210	# debugging output from elfsign to detect the old version.
2211	#
2212	newelfsigntest=`SUNW_CRYPTO_DEBUG=stderr /usr/bin/elfsign verify \
2213	    -e /usr/lib/security/pkcs11_softtoken.so.1 2>&1 \
2214	    | egrep algorithmOID`
2215	if [ -z "$newelfsigntest" ]; then
2216		echo "WARNING: /usr/bin/elfsign out of date;" \
2217		    "will only sign crypto modules\n" | \
2218		    tee -a $mail_msg_file >> $LOGFILE
2219		export ELFSIGN_OBJECT=true
2220	elif [ "$VERIFY_ELFSIGN" = "y" ]; then
2221		echo "WARNING: VERIFY_ELFSIGN=y requires" \
2222		    "the -t flag; ignoring VERIFY_ELFSIGN\n" | \
2223		    tee -a $mail_msg_file >> $LOGFILE
2224	fi
2225fi
2226
2227[ "$O_FLAG" = y ] && MULTI_PROTO=yes
2228
2229case $MULTI_PROTO in
2230yes|no)	;;
2231*)
2232	echo "WARNING: MULTI_PROTO is \"$MULTI_PROTO\"; " \
2233	    "should be \"yes\" or \"no\"." | tee -a $mail_msg_file >> $LOGFILE
2234	echo "Setting MULTI_PROTO to \"no\".\n" | \
2235	    tee -a $mail_msg_file >> $LOGFILE
2236	export MULTI_PROTO=no
2237	;;
2238esac
2239
2240# If CODESIGN_USER is set, we'll want the crypto that we just built.
2241if [[ -n "$CODESIGN_USER" && -n "$ON_CRYPTO_BINS" ]]; then
2242	echo "Clearing ON_CRYPTO_BINS for signing build." >> "$LOGFILE"
2243	unset ON_CRYPTO_BINS
2244fi
2245
2246echo "\n==== Build version ====\n" | tee -a $mail_msg_file >> $LOGFILE
2247echo $VERSION | tee -a $mail_msg_file >> $LOGFILE
2248
2249# Save the current proto area if we're comparing against the last build
2250if [ "$w_FLAG" = "y" -a -d "$ROOT" ]; then
2251    if [ -d "$ROOT.prev" ]; then
2252	rm -rf $ROOT.prev
2253    fi
2254    mv $ROOT $ROOT.prev
2255fi
2256
2257# Same for non-DEBUG proto area
2258if [ "$w_FLAG" = "y" -a "$MULTI_PROTO" = yes -a -d "$ROOT-nd" ]; then
2259	if [ -d "$ROOT-nd.prev" ]; then
2260		rm -rf $ROOT-nd.prev
2261	fi
2262	mv $ROOT-nd $ROOT-nd.prev
2263fi
2264
2265#
2266# Echo the SCM type of the parent workspace, this can't just be which_scm
2267# as that does not know how to identify various network repositories.
2268#
2269function parent_wstype {
2270	typeset scm_type junk
2271
2272	CODEMGR_WS="$BRINGOVER_WS" "$WHICH_SCM" 2>/dev/null \
2273	    | read scm_type junk
2274	if [[ -z "$scm_type" || "$scm_type" == unknown ]]; then
2275		# Probe BRINGOVER_WS to determine its type
2276		if [[ $BRINGOVER_WS == svn*://* ]]; then
2277			scm_type="subversion"
2278		elif [[ $BRINGOVER_WS == file://* ]] &&
2279		    egrep -s "This is a Subversion repository" \
2280		    ${BRINGOVER_WS#file://}/README.txt 2> /dev/null; then
2281			scm_type="subversion"
2282		elif [[ $BRINGOVER_WS == ssh://* ]]; then
2283			scm_type="mercurial"
2284		elif [[ $BRINGOVER_WS == http://* ]] && \
2285		    wget -q -O- --save-headers "$BRINGOVER_WS/?cmd=heads" | \
2286		    egrep -s "application/mercurial" 2> /dev/null; then
2287			scm_type="mercurial"
2288		elif svn info $BRINGOVER_WS > /dev/null 2>&1; then
2289			scm_type="subversion"
2290		else
2291			scm_type="none"
2292		fi
2293	fi
2294
2295	# fold both unsupported and unrecognized results into "none"
2296	case "$scm_type" in
2297	none|subversion|teamware|mercurial)
2298		;;
2299	*)	scm_type=none
2300		;;
2301	esac
2302
2303	echo $scm_type
2304}
2305
2306# Echo the SCM types of $CODEMGR_WS and $BRINGOVER_WS
2307function child_wstype {
2308	typeset scm_type junk
2309
2310	# Probe CODEMGR_WS to determine its type
2311	if [[ -d $CODEMGR_WS ]]; then
2312		$WHICH_SCM | read scm_type junk || exit 1
2313	fi
2314
2315	case "$scm_type" in
2316	none|subversion|teamware|mercurial)
2317		;;
2318	*)	scm_type=none
2319		;;
2320	esac
2321
2322	echo $scm_type
2323}
2324
2325export SCM_TYPE=$(child_wstype)
2326
2327#
2328#	Decide whether to clobber
2329#
2330if [ "$i_FLAG" = "n" -a -d "$SRC" ]; then
2331	echo "\n==== Make clobber at `date` ====\n" >> $LOGFILE
2332
2333	cd $SRC
2334	# remove old clobber file
2335	rm -f $SRC/clobber.out
2336	rm -f $SRC/clobber-${MACH}.out
2337
2338	# Remove all .make.state* files, just in case we are restarting
2339	# the build after having interrupted a previous 'make clobber'.
2340	find . \( -name SCCS -o -name .hg -o -name .svn \
2341		-o -name 'interfaces.*' \) -prune \
2342		-o -name '.make.*' -print | xargs rm -f
2343
2344	$MAKE -ek clobber 2>&1 | tee -a $SRC/clobber-${MACH}.out >> $LOGFILE
2345	echo "\n==== Make clobber ERRORS ====\n" >> $mail_msg_file
2346	grep "$MAKE:" $SRC/clobber-${MACH}.out |
2347		egrep -v "Ignoring unknown host" \
2348		>> $mail_msg_file
2349
2350	if [[ "$t_FLAG" = "y" || "$O_FLAG" = "y" ]]; then
2351		echo "\n==== Make tools clobber at `date` ====\n" >> $LOGFILE
2352		cd ${TOOLS}
2353		rm -f ${TOOLS}/clobber-${MACH}.out
2354		$MAKE TOOLS_PROTO=$TOOLS_PROTO -ek clobber 2>&1 | \
2355			tee -a ${TOOLS}/clobber-${MACH}.out >> $LOGFILE
2356		echo "\n==== Make tools clobber ERRORS ====\n" \
2357			>> $mail_msg_file
2358		grep "$MAKE:" ${TOOLS}/clobber-${MACH}.out \
2359			>> $mail_msg_file
2360		rm -rf ${TOOLS_PROTO}
2361		mkdir -p ${TOOLS_PROTO}
2362	fi
2363
2364	typeset roots=$(allprotos)
2365	echo "\n\nClearing $roots" >> "$LOGFILE"
2366	rm -rf $roots
2367
2368	# Get back to a clean workspace as much as possible to catch
2369	# problems that only occur on fresh workspaces.
2370	# Remove all .make.state* files, libraries, and .o's that may
2371	# have been omitted from clobber.  A couple of libraries are
2372	# under source code control, so leave them alone.
2373	# We should probably blow away temporary directories too.
2374	cd $SRC
2375	find $relsrcdirs \( -name SCCS -o -name .hg -o -name .svn \
2376	    -o -name 'interfaces.*' \) -prune -o \
2377	    \( -name '.make.*' -o -name 'lib*.a' -o -name 'lib*.so*' -o \
2378	       -name '*.o' \) -print | \
2379	    grep -v 'tools/ctf/dwarf/.*/libdwarf' | xargs rm -f
2380else
2381	echo "\n==== No clobber at `date` ====\n" >> $LOGFILE
2382fi
2383
2384type bringover_teamware > /dev/null 2>&1 || function bringover_teamware {
2385	# sleep on the parent workspace's lock
2386	while egrep -s write $BRINGOVER_WS/Codemgr_wsdata/locks
2387	do
2388		sleep 120
2389	done
2390
2391	if [[ -z $BRINGOVER ]]; then
2392		BRINGOVER=$TEAMWARE/bin/bringover
2393	fi
2394
2395	staffer $BRINGOVER -c "nightly update" -p $BRINGOVER_WS \
2396	    -w $CODEMGR_WS $BRINGOVER_FILES < /dev/null 2>&1 ||
2397		touch $TMPDIR/bringover_failed
2398
2399        staffer bringovercheck $CODEMGR_WS >$TMPDIR/bringovercheck.out 2>&1
2400	if [ -s $TMPDIR/bringovercheck.out ]; then
2401		echo "\n==== POST-BRINGOVER CLEANUP NOISE ====\n"
2402		cat $TMPDIR/bringovercheck.out
2403	fi
2404}
2405
2406type bringover_mercurial > /dev/null 2>&1 || function bringover_mercurial {
2407	typeset -x PATH=$PATH
2408
2409	# If the repository doesn't exist yet, then we want to populate it.
2410	if [[ ! -d $CODEMGR_WS/.hg ]]; then
2411		staffer hg init $CODEMGR_WS
2412		staffer echo "[paths]" > $CODEMGR_WS/.hg/hgrc
2413		staffer echo "default=$BRINGOVER_WS" >> $CODEMGR_WS/.hg/hgrc
2414		touch $TMPDIR/new_repository
2415	fi
2416
2417	#
2418	# If the user set CLOSED_BRINGOVER_WS and didn't set CLOSED_IS_PRESENT
2419	# to "no," then we'll want to initialise the closed repository
2420	#
2421	# We use $orig_closed_is_present instead of $CLOSED_IS_PRESENT,
2422	# because for newly-created source trees, the latter will be "no"
2423	# until after the bringover completes.
2424	#
2425	if [[ "$orig_closed_is_present" != "no" && \
2426	    -n "$CLOSED_BRINGOVER_WS" && \
2427	    ! -d $CODEMGR_WS/usr/closed/.hg ]]; then
2428		staffer mkdir -p $CODEMGR_WS/usr/closed
2429		staffer hg init $CODEMGR_WS/usr/closed
2430		staffer echo "[paths]" > $CODEMGR_WS/usr/closed/.hg/hgrc
2431		staffer echo "default=$CLOSED_BRINGOVER_WS" >> $CODEMGR_WS/usr/closed/.hg/hgrc
2432		touch $TMPDIR/new_closed
2433		export CLOSED_IS_PRESENT=yes
2434	fi
2435
2436	typeset -x HGMERGE="/bin/false"
2437
2438	#
2439	# If the user has changes, regardless of whether those changes are
2440	# committed, and regardless of whether those changes conflict, then
2441	# we'll attempt to merge them either implicitly (uncommitted) or
2442	# explicitly (committed).
2443	#
2444	# These are the messages we'll use to help clarify mercurial output
2445	# in those cases.
2446	#
2447	typeset mergefailmsg="\
2448***\n\
2449*** nightly was unable to automatically merge your changes.  You should\n\
2450*** redo the full merge manually, following the steps outlined by mercurial\n\
2451*** above, then restart nightly.\n\
2452***\n"
2453	typeset mergepassmsg="\
2454***\n\
2455*** nightly successfully merged your changes.  This means that your working\n\
2456*** directory has been updated, but those changes are not yet committed.\n\
2457*** After nightly completes, you should validate the results of the merge,\n\
2458*** then use hg commit manually.\n\
2459***\n"
2460
2461	#
2462	# For each repository in turn:
2463	#
2464	# 1. Do the pull.  If this fails, dump the output and bail out.
2465	#
2466	# 2. If the pull resulted in an extra head, do an explicit merge.
2467	#    If this fails, dump the output and bail out.
2468	#
2469	# Because we can't rely on Mercurial to exit with a failure code
2470	# when a merge fails (Mercurial issue #186), we must grep the
2471	# output of pull/merge to check for attempted and/or failed merges.
2472	#
2473	# 3. If a merge failed, set the message and fail the bringover.
2474	#
2475	# 4. Otherwise, if a merge succeeded, set the message
2476	#
2477	# 5. Dump the output, and any message from step 3 or 4.
2478	#
2479
2480	typeset HG_SOURCE=$BRINGOVER_WS
2481	if [ ! -f $TMPDIR/new_repository ]; then
2482		HG_SOURCE=$TMPDIR/open_bundle.hg
2483		staffer hg --cwd $CODEMGR_WS incoming --bundle $HG_SOURCE \
2484		    -v $BRINGOVER_WS > $TMPDIR/incoming_open.out
2485
2486		#
2487		# If there are no incoming changesets, then incoming will
2488		# fail, and there will be no bundle file.  Reset the source,
2489		# to allow the remaining logic to complete with no false
2490		# negatives.  (Unlike incoming, pull will return success
2491		# for the no-change case.)
2492		#
2493		if (( $? != 0 )); then
2494			HG_SOURCE=$BRINGOVER_WS
2495		fi
2496	fi
2497
2498	staffer hg --cwd $CODEMGR_WS pull -u $HG_SOURCE \
2499	    > $TMPDIR/pull_open.out 2>&1
2500	if (( $? != 0 )); then
2501		printf "%s: pull failed as follows:\n\n" "$CODEMGR_WS"
2502		cat $TMPDIR/pull_open.out
2503		if grep "^merging.*failed" $TMPDIR/pull_open.out > /dev/null 2>&1; then
2504			printf "$mergefailmsg"
2505		fi
2506		touch $TMPDIR/bringover_failed
2507		return
2508	fi
2509
2510	if grep "not updating" $TMPDIR/pull_open.out > /dev/null 2>&1; then
2511		staffer hg --cwd $CODEMGR_WS merge \
2512		    >> $TMPDIR/pull_open.out 2>&1
2513		if (( $? != 0 )); then
2514			printf "%s: merge failed as follows:\n\n" \
2515			    "$CODEMGR_WS"
2516			cat $TMPDIR/pull_open.out
2517			if grep "^merging.*failed" $TMPDIR/pull_open.out \
2518			    > /dev/null 2>&1; then
2519				printf "$mergefailmsg"
2520			fi
2521			touch $TMPDIR/bringover_failed
2522			return
2523		fi
2524	fi
2525
2526	printf "updated %s with the following results:\n" "$CODEMGR_WS"
2527	cat $TMPDIR/pull_open.out
2528	if grep "^merging" $TMPDIR/pull_open.out >/dev/null 2>&1; then
2529		printf "$mergepassmsg"
2530	fi
2531	printf "\n"
2532
2533	#
2534	# We only want to update usr/closed if it exists, and we haven't been
2535	# told not to via $CLOSED_IS_PRESENT, and we actually know where to
2536	# pull from ($CLOSED_BRINGOVER_WS).
2537	#
2538	if [[ $CLOSED_IS_PRESENT = yes && \
2539	    -d $CODEMGR_WS/usr/closed/.hg && \
2540	    -n $CLOSED_BRINGOVER_WS ]]; then
2541
2542		HG_SOURCE=$CLOSED_BRINGOVER_WS
2543		if [ ! -f $TMPDIR/new_closed ]; then
2544			HG_SOURCE=$TMPDIR/closed_bundle.hg
2545			staffer hg --cwd $CODEMGR_WS/usr/closed incoming \
2546			    --bundle $HG_SOURCE -v $CLOSED_BRINGOVER_WS \
2547			    > $TMPDIR/incoming_closed.out
2548
2549			#
2550			# If there are no incoming changesets, then incoming will
2551			# fail, and there will be no bundle file.  Reset the source,
2552			# to allow the remaining logic to complete with no false
2553			# negatives.  (Unlike incoming, pull will return success
2554			# for the no-change case.)
2555			#
2556			if (( $? != 0 )); then
2557				HG_SOURCE=$CLOSED_BRINGOVER_WS
2558			fi
2559		fi
2560
2561		staffer hg --cwd $CODEMGR_WS/usr/closed pull -u \
2562			$HG_SOURCE > $TMPDIR/pull_closed.out 2>&1
2563		if (( $? != 0 )); then
2564			printf "closed pull failed as follows:\n\n"
2565			cat $TMPDIR/pull_closed.out
2566			if grep "^merging.*failed" $TMPDIR/pull_closed.out \
2567			    > /dev/null 2>&1; then
2568				printf "$mergefailmsg"
2569			fi
2570			touch $TMPDIR/bringover_failed
2571			return
2572		fi
2573
2574		if grep "not updating" $TMPDIR/pull_closed.out > /dev/null 2>&1; then
2575			staffer hg --cwd $CODEMGR_WS/usr/closed merge \
2576			    >> $TMPDIR/pull_closed.out 2>&1
2577			if (( $? != 0 )); then
2578				printf "closed merge failed as follows:\n\n"
2579				cat $TMPDIR/pull_closed.out
2580				if grep "^merging.*failed" $TMPDIR/pull_closed.out > /dev/null 2>&1; then
2581					printf "$mergefailmsg"
2582				fi
2583				touch $TMPDIR/bringover_failed
2584				return
2585			fi
2586		fi
2587
2588		printf "updated %s with the following results:\n" \
2589		    "$CODEMGR_WS/usr/closed"
2590		cat $TMPDIR/pull_closed.out
2591		if grep "^merging" $TMPDIR/pull_closed.out > /dev/null 2>&1; then
2592			printf "$mergepassmsg"
2593		fi
2594	fi
2595
2596	#
2597	# Per-changeset output is neither useful nor manageable for a
2598	# newly-created repository.
2599	#
2600	if [ -f $TMPDIR/new_repository ]; then
2601		return
2602	fi
2603
2604	printf "\nadded the following changesets to open repository:\n"
2605	cat $TMPDIR/incoming_open.out
2606
2607	#
2608	# The closed repository could have been newly created, even though
2609	# the open one previously existed...
2610	#
2611	if [ -f $TMPDIR/new_closed ]; then
2612		return
2613	fi
2614
2615	if [ -f $TMPDIR/incoming_closed.out ]; then
2616		printf "\nadded the following changesets to closed repository:\n"
2617		cat $TMPDIR/incoming_closed.out
2618	fi
2619}
2620
2621type bringover_subversion > /dev/null 2>&1 || function bringover_subversion {
2622	typeset -x PATH=$PATH
2623
2624	if [[ ! -d $CODEMGR_WS/.svn ]]; then
2625		staffer svn checkout $BRINGOVER_WS $CODEMGR_WS ||
2626			touch $TMPDIR/bringover_failed
2627	else
2628		typeset root
2629		root=$(staffer svn info $CODEMGR_WS |
2630			nawk '/^Repository Root:/ {print $NF}')
2631		if [[ $root != $BRINGOVER_WS ]]; then
2632			# We fail here because there's no way to update
2633			# from a named repo.
2634			cat <<-EOF
2635			\$BRINGOVER_WS doesn't match repository root:
2636			  \$BRINGOVER_WS:  $BRINGOVER_WS
2637			  Repository root: $root
2638			EOF
2639			touch $TMPDIR/bringover_failed
2640		else
2641			# If a conflict happens, svn still exits 0.
2642			staffer svn update $CODEMGR_WS | tee $TMPDIR/pull.out ||
2643				touch $TMPDIR/bringover_failed
2644			if grep "^C" $TMPDIR/pull.out > /dev/null 2>&1; then
2645				touch $TMPDIR/bringover_failed
2646			fi
2647		fi
2648	fi
2649}
2650
2651type bringover_none > /dev/null 2>&1 || function bringover_none {
2652	echo "Couldn't figure out what kind of SCM to use for $BRINGOVER_WS."
2653	touch $TMPDIR/bringover_failed
2654}
2655
2656#
2657#	Decide whether to bringover to the codemgr workspace
2658#
2659if [ "$n_FLAG" = "n" ]; then
2660	PARENT_SCM_TYPE=$(parent_wstype)
2661
2662	if [[ $SCM_TYPE != none && $SCM_TYPE != $PARENT_SCM_TYPE ]]; then
2663		echo "cannot bringover from $PARENT_SCM_TYPE to $SCM_TYPE, " \
2664		    "quitting at `date`." | tee -a $mail_msg_file >> $LOGFILE
2665		exit 1
2666	fi
2667
2668	run_hook PRE_BRINGOVER
2669
2670	echo "\n==== bringover to $CODEMGR_WS at `date` ====\n" >> $LOGFILE
2671	echo "\n==== BRINGOVER LOG ====\n" >> $mail_msg_file
2672
2673	eval "bringover_${PARENT_SCM_TYPE}" 2>&1 |
2674		tee -a $mail_msg_file >> $LOGFILE
2675
2676	if [ -f $TMPDIR/bringover_failed ]; then
2677		rm -f $TMPDIR/bringover_failed
2678		build_ok=n
2679		echo "trouble with bringover, quitting at `date`." |
2680			tee -a $mail_msg_file >> $LOGFILE
2681		exit 1
2682	fi
2683
2684	#
2685	# It's possible that we used the bringover above to create
2686	# $CODEMGR_WS.  If so, then SCM_TYPE was previously "none,"
2687	# but should now be the same as $BRINGOVER_WS.
2688	#
2689	[[ $SCM_TYPE = none ]] && SCM_TYPE=$PARENT_SCM_TYPE
2690
2691	run_hook POST_BRINGOVER
2692
2693	#
2694	# Possible transition from pre-split workspace to split
2695	# workspace.  See if the bringover changed anything.
2696	#
2697	CLOSED_IS_PRESENT="$orig_closed_is_present"
2698	check_closed_tree
2699
2700else
2701	echo "\n==== No bringover to $CODEMGR_WS ====\n" >> $LOGFILE
2702fi
2703
2704if [[ "$O_FLAG" = y && "$CLOSED_IS_PRESENT" != "yes" ]]; then
2705	build_ok=n
2706	echo "OpenSolaris binary deliverables need usr/closed." \
2707	    | tee -a "$mail_msg_file" >> $LOGFILE
2708	exit 1
2709fi
2710
2711if [ "$CLOSED_IS_PRESENT" = no ]; then
2712	#
2713	# Not all consolidations have a closed tree, and even if they
2714	# did, they wouldn't necessarily have signed crypto.  But if
2715	# the current source base does have signed crypto and it can't
2716	# be generated, error out, rather than silently building
2717	# unusable binaries.
2718	#
2719	grep -s ELFSIGN_CRYPTO "$SRC/Makefile.master" > /dev/null
2720	if (( $? == 0 )); then
2721		crypto_is_present >> "$LOGFILE"
2722		if (( $? != 0 )); then
2723			build_ok=n
2724			echo "A crypto tarball must be provided when" \
2725			    "there is no closed tree." |
2726			    tee -a "$mail_msg_file" >> "$LOGFILE"
2727			exit 1
2728		fi
2729	fi
2730fi
2731
2732echo "\n==== Build environment ====\n" | tee -a $build_environ_file >> $LOGFILE
2733
2734# System
2735whence uname | tee -a $build_environ_file >> $LOGFILE
2736uname -a 2>&1 | tee -a $build_environ_file >> $LOGFILE
2737echo | tee -a $build_environ_file >> $LOGFILE
2738
2739# nightly
2740echo "$0 $@" | tee -a $build_environ_file >> $LOGFILE
2741if [[ $nightly_path = "/opt/onbld/bin/nightly" ]]; then
2742	# We assume that if you have /opt/onbld/bin/nightly, then
2743	# you have some form of the onbld package installed. If this
2744	# is not true then your nightly is almost definitely out of
2745	# date and should not be used.
2746	/usr/bin/pkg info \*onbld\* | \
2747	    egrep "Name:|Publisher:|Version:|Packaging:|FMRI:"
2748else
2749	echo "$nightly_ls"
2750fi | tee -a $build_environ_file >> $LOGFILE
2751echo | tee -a $build_environ_file >> $LOGFILE
2752
2753# make
2754whence $MAKE | tee -a $build_environ_file >> $LOGFILE
2755$MAKE -v | tee -a $build_environ_file >> $LOGFILE
2756echo "number of concurrent jobs = $DMAKE_MAX_JOBS" |
2757    tee -a $build_environ_file >> $LOGFILE
2758
2759#
2760# Report the compiler versions.
2761#
2762
2763if [[ ! -f $SRC/Makefile ]]; then
2764	build_ok=n
2765	echo "\nUnable to find \"Makefile\" in $SRC." | \
2766	    tee -a $build_environ_file >> $LOGFILE
2767	exit 1
2768fi
2769
2770( cd $SRC
2771  for target in cc-version cc64-version java-version; do
2772	echo
2773	#
2774	# Put statefile somewhere we know we can write to rather than trip
2775	# over a read-only $srcroot.
2776	#
2777	rm -f $TMPDIR/make-state
2778	export SRC
2779	if $MAKE -K $TMPDIR/make-state -e $target 2>/dev/null; then
2780		continue
2781	fi
2782	touch $TMPDIR/nocompiler
2783  done
2784  echo
2785) | tee -a $build_environ_file >> $LOGFILE
2786
2787if [ -f $TMPDIR/nocompiler ]; then
2788	rm -f $TMPDIR/nocompiler
2789	build_ok=n
2790	echo "Aborting due to missing compiler." |
2791		tee -a $build_environ_file >> $LOGFILE
2792	exit 1
2793fi
2794
2795# as
2796whence as | tee -a $build_environ_file >> $LOGFILE
2797as -V 2>&1 | head -1 | tee -a $build_environ_file >> $LOGFILE
2798echo | tee -a $build_environ_file >> $LOGFILE
2799
2800# Check that we're running a capable link-editor
2801whence ld | tee -a $build_environ_file >> $LOGFILE
2802LDVER=`ld -V 2>&1`
2803echo $LDVER | tee -a $build_environ_file >> $LOGFILE
2804LDVER=`echo $LDVER | sed -e "s/.*-1\.//" -e "s/:.*//"`
2805if [ `expr $LDVER \< 422` -eq 1 ]; then
2806	echo "The link-editor needs to be at version 422 or higher to build" | \
2807	    tee -a $build_environ_file >> $LOGFILE
2808	echo "the latest stuff.  Hope your build works." | \
2809	    tee -a $build_environ_file >> $LOGFILE
2810fi
2811
2812#
2813# Build and use the workspace's tools if requested
2814#
2815if [[ "$t_FLAG" = "y" || "$O_FLAG" = y ]]; then
2816	set_non_debug_build_flags
2817
2818	build_tools ${TOOLS_PROTO}
2819	if [[ $? != 0  && "$t_FLAG" = y ]]; then
2820		use_tools $TOOLS_PROTO
2821	fi
2822fi
2823
2824#
2825# copy ihv proto area in addition to the build itself
2826#
2827if [ "$X_FLAG" = "y" ]; then
2828	copy_ihv_proto
2829fi
2830
2831if [ "$i_FLAG" = "y" -a "$SH_FLAG" = "y" ]; then
2832	echo "\n==== NOT Building base OS-Net source ====\n" | \
2833	    tee -a $LOGFILE >> $mail_msg_file
2834else
2835	# timestamp the start of the normal build; the findunref tool uses it.
2836	touch $SRC/.build.tstamp
2837
2838	normal_build
2839fi
2840
2841#
2842# Generate the THIRDPARTYLICENSE files if needed.  This is done after
2843# the build, so that dynamically-created license files are there.
2844# It's done before findunref to help identify license files that need
2845# to be added to tools/opensolaris/license-list.
2846#
2847if [ "$O_FLAG" = y -a "$build_ok" = y ]; then
2848	echo "\n==== Generating THIRDPARTYLICENSE files ====\n" |
2849	    tee -a "$mail_msg_file" >> "$LOGFILE"
2850
2851	if [ -d $ROOT/licenses/usr ]; then
2852		( cd $ROOT/licenses ; \
2853		    mktpl $SRC/pkg/license-list ) >> "$LOGFILE" 2>&1
2854		if (( $? != 0 )) ; then
2855			echo "Couldn't create THIRDPARTYLICENSE files" |
2856			    tee -a "$mail_msg_file" >> "$LOGFILE"
2857		fi
2858	else
2859		echo "No licenses found under $ROOT/licenses" |
2860		    tee -a "$mail_msg_file" >> "$LOGFILE"
2861	fi
2862fi
2863
2864ORIG_SRC=$SRC
2865BINARCHIVE=${CODEMGR_WS}/bin-${MACH}.cpio.Z
2866
2867if [ "$SE_FLAG" = "y" -o "$SD_FLAG" = "y" -o "$SH_FLAG" = "y" ]; then
2868	save_binaries
2869fi
2870
2871
2872# EXPORT_SRC comes after CRYPT_SRC since a domestic build will need
2873# $SRC pointing to the export_source usr/src.
2874
2875if [ "$SE_FLAG" = "y" -o "$SD_FLAG" = "y" -o "$SH_FLAG" = "y" ]; then
2876	if [ "$SD_FLAG" = "y" -a $build_ok = y ]; then
2877	    set_up_source_build ${CODEMGR_WS} ${CRYPT_SRC} CRYPT_SRC
2878	fi
2879
2880	if [ $build_ok = y ]; then
2881	    set_up_source_build ${CODEMGR_WS} ${EXPORT_SRC} EXPORT_SRC
2882	fi
2883fi
2884
2885if [ "$SD_FLAG" = "y" -a $build_ok = y ]; then
2886	# drop the crypt files in place.
2887	cd ${EXPORT_SRC}
2888	echo "\nextracting crypt_files.cpio.Z onto export_source.\n" \
2889	    >> ${LOGFILE}
2890	zcat ${CODEMGR_WS}/crypt_files.cpio.Z | \
2891	    cpio -idmucvB 2>/dev/null >> ${LOGFILE}
2892	if [ "$?" = "0" ]; then
2893		echo "\n==== DOMESTIC extraction succeeded ====\n" \
2894		    >> $mail_msg_file
2895	else
2896		echo "\n==== DOMESTIC extraction failed ====\n" \
2897		    >> $mail_msg_file
2898	fi
2899
2900fi
2901
2902if [ "$SO_FLAG" = "y" -a "$build_ok" = y ]; then
2903	#
2904	# Copy the open sources into their own tree, set up the closed
2905	# binaries, and set up the environment.  The build looks for
2906	# the closed binaries in a location that depends on whether
2907	# it's a DEBUG build, so we might need to make two copies.
2908	#
2909	# If copy_source fails, it will have already generated an
2910	# error message and set build_ok=n, so we don't need to worry
2911	# about that here.
2912	#
2913	copy_source $CODEMGR_WS $OPEN_SRCDIR OPEN_SOURCE usr/src
2914fi
2915
2916if [ "$SO_FLAG" = "y" -a "$build_ok" = y ]; then
2917
2918	echo "\n==== Generating skeleton closed binaries for" \
2919	    "open-only build ====\n" | \
2920	    tee -a $LOGFILE >> $mail_msg_file
2921
2922	rm -rf $CODEMGR_WS/closed.skel
2923	if [ "$D_FLAG" = y ]; then
2924		mkclosed $MACH $ROOT $CODEMGR_WS/closed.skel/root_$MACH \
2925		    >>$LOGFILE 2>&1
2926		if (( $? != 0 )) ; then
2927			echo "Couldn't create skeleton DEBUG closed binaries." |
2928			    tee -a $mail_msg_file >> $LOGFILE
2929		fi
2930	fi
2931	if [ "$F_FLAG" = n ]; then
2932		root=$ROOT
2933		[ "$MULTI_PROTO" = yes ] && root=$ROOT-nd
2934		mkclosed $MACH $root $CODEMGR_WS/closed.skel/root_$MACH-nd \
2935		    >>$LOGFILE 2>&1
2936		if (( $? != 0 )) ; then
2937			echo "Couldn't create skeleton non-DEBUG closed binaries." |
2938			    tee -a $mail_msg_file >> $LOGFILE
2939		fi
2940	fi
2941
2942	SRC=$OPEN_SRCDIR/usr/src
2943	# Try not to clobber any user-provided closed binaries.
2944	export ON_CLOSED_BINS=$CODEMGR_WS/closed.skel
2945	export CLOSED_IS_PRESENT=no
2946fi
2947
2948if is_source_build && [ $build_ok = y ] ; then
2949	# remove proto area(s) here, since we don't clobber
2950	rm -rf `allprotos`
2951	if [ "$t_FLAG" = "y" ]; then
2952		set_non_debug_build_flags
2953		ORIG_TOOLS=$TOOLS
2954		#
2955		# SRC was set earlier to point to the source build
2956		# source tree (e.g., $EXPORT_SRC).
2957		#
2958		TOOLS=${SRC}/tools
2959		TOOLS_PROTO=${TOOLS}/${TOOLS_PROTO_REL}; export TOOLS_PROTO
2960		build_tools ${TOOLS_PROTO}
2961		if [[ $? != 0 ]]; then
2962			use_tools ${TOOLS_PROTO}
2963		fi
2964	fi
2965
2966	export EXPORT_RELEASE_BUILD ; EXPORT_RELEASE_BUILD=#
2967	normal_build
2968fi
2969
2970if [[ "$SO_FLAG" = "y" && "$build_ok" = "y" ]]; then
2971	rm -rf $ON_CLOSED_BINS
2972fi
2973
2974#
2975# There are several checks that need to look at the proto area, but
2976# they only need to look at one, and they don't care whether it's
2977# DEBUG or non-DEBUG.
2978#
2979if [[ "$MULTI_PROTO" = yes && "$D_FLAG" = n ]]; then
2980	checkroot=$ROOT-nd
2981else
2982	checkroot=$ROOT
2983fi
2984
2985if [ "$build_ok" = "y" ]; then
2986	echo "\n==== Creating protolist system file at `date` ====" \
2987		>> $LOGFILE
2988	protolist $checkroot > $ATLOG/proto_list_${MACH}
2989	echo "==== protolist system file created at `date` ====\n" \
2990		>> $LOGFILE
2991
2992	if [ "$N_FLAG" != "y" ]; then
2993
2994		E1=
2995		f1=
2996		if [ -d "$SRC/pkgdefs" ]; then
2997			f1="$SRC/pkgdefs/etc/exception_list_$MACH"
2998			if [ "$X_FLAG" = "y" ]; then
2999				f1="$f1 $IA32_IHV_WS/usr/src/pkgdefs/etc/exception_list_$MACH"
3000			fi
3001		fi
3002
3003		for f in $f1; do
3004			if [ -f "$f" ]; then
3005				E1="$E1 -e $f"
3006			fi
3007		done
3008
3009		E2=
3010		f2=
3011		if [ -d "$SRC/pkg" ]; then
3012			f2="$f2 exceptions/packaging"
3013			if [ "$CLOSED_IS_PRESENT" = "no" ]; then
3014				f2="$f2 exceptions/packaging.open"
3015			else
3016				f2="$f2 exceptions/packaging.closed"
3017			fi
3018		fi
3019
3020		for f in $f2; do
3021			if [ -f "$f" ]; then
3022				E2="$E2 -e $f"
3023			fi
3024		done
3025
3026		if [ -f "$REF_PROTO_LIST" ]; then
3027			#
3028			# For builds that copy the IHV proto area (-X), add the
3029			# IHV proto list to the reference list if the reference
3030			# was built without -X.
3031			#
3032			# For builds that don't copy the IHV proto area, add the
3033			# IHV proto list to the build's proto list if the
3034			# reference was built with -X.
3035			#
3036			# Use the presence of the first file entry of the cached
3037			# IHV proto list in the reference list to determine
3038			# whether it was built with -X or not.
3039			#
3040			IHV_REF_PROTO_LIST=$SRC/pkg/proto_list_ihv_$MACH
3041			grepfor=$(nawk '$1 == "f" { print $2; exit }' \
3042				$IHV_REF_PROTO_LIST 2> /dev/null)
3043			if [ $? = 0 -a -n "$grepfor" ]; then
3044				if [ "$X_FLAG" = "y" ]; then
3045					grep -w "$grepfor" \
3046						$REF_PROTO_LIST > /dev/null
3047					if [ ! "$?" = "0" ]; then
3048						REF_IHV_PROTO="-d $IHV_REF_PROTO_LIST"
3049					fi
3050				else
3051					grep -w "$grepfor" \
3052						$REF_PROTO_LIST > /dev/null
3053					if [ "$?" = "0" ]; then
3054						IHV_PROTO_LIST="$IHV_REF_PROTO_LIST"
3055					fi
3056				fi
3057			fi
3058		fi
3059	fi
3060
3061	if [ "$N_FLAG" != "y" -a -f $SRC/pkgdefs/Makefile ]; then
3062		echo "\n==== Impact on SVr4 packages ====\n" >> $mail_msg_file
3063		#
3064		# Compare the build's proto list with current package
3065		# definitions to audit the quality of package
3066		# definitions and makefile install targets. Use the
3067		# current exception list.
3068		#
3069		PKGDEFS_LIST=""
3070		for d in $abssrcdirs; do
3071			if [ -d $d/pkgdefs ]; then
3072				PKGDEFS_LIST="$PKGDEFS_LIST -d $d/pkgdefs"
3073			fi
3074		done
3075		if [ "$X_FLAG" = "y" -a \
3076		    -d $IA32_IHV_WS/usr/src/pkgdefs ]; then
3077			PKGDEFS_LIST="$PKGDEFS_LIST -d $IA32_IHV_WS/usr/src/pkgdefs"
3078		fi
3079		$PROTOCMPTERSE \
3080		    "Files missing from the proto area:" \
3081		    "Files missing from packages:" \
3082		    "Inconsistencies between pkgdefs and proto area:" \
3083		    ${E1} \
3084		    ${PKGDEFS_LIST} \
3085		    $ATLOG/proto_list_${MACH} \
3086		    >> $mail_msg_file
3087	fi
3088
3089	if [ "$N_FLAG" != "y" -a -d $SRC/pkg ]; then
3090		echo "\n==== Validating manifests against proto area ====\n" \
3091		    >> $mail_msg_file
3092		( cd $SRC/pkg ; $MAKE -e protocmp ROOT="$checkroot" ) \
3093		    >> $mail_msg_file
3094
3095	fi
3096
3097	if [ "$N_FLAG" != "y" -a -f "$REF_PROTO_LIST" ]; then
3098		echo "\n==== Impact on proto area ====\n" >> $mail_msg_file
3099		if [ -n "$E2" ]; then
3100			ELIST=$E2
3101		else
3102			ELIST=$E1
3103		fi
3104		$PROTOCMPTERSE \
3105			"Files in yesterday's proto area, but not today's:" \
3106			"Files in today's proto area, but not yesterday's:" \
3107			"Files that changed between yesterday and today:" \
3108			${ELIST} \
3109			-d $REF_PROTO_LIST \
3110			$REF_IHV_PROTO \
3111			$ATLOG/proto_list_${MACH} \
3112			$IHV_PROTO_LIST \
3113			>> $mail_msg_file
3114	fi
3115fi
3116
3117if [ "$u_FLAG" = "y"  -a "$build_ok" = "y" ]; then
3118	staffer cp $ATLOG/proto_list_${MACH} \
3119		$PARENT_WS/usr/src/proto_list_${MACH}
3120fi
3121
3122# Update parent proto area if necessary. This is done now
3123# so that the proto area has either DEBUG or non-DEBUG kernels.
3124# Note that this clears out the lock file, so we can dispense with
3125# the variable now.
3126if [ "$U_FLAG" = "y" -a "$build_ok" = "y" ]; then
3127	echo "\n==== Copying proto area to $NIGHTLY_PARENT_ROOT ====\n" | \
3128	    tee -a $LOGFILE >> $mail_msg_file
3129	rm -rf $NIGHTLY_PARENT_ROOT/*
3130	unset Ulockfile
3131	mkdir -p $NIGHTLY_PARENT_ROOT
3132	if [[ "$MULTI_PROTO" = no || "$D_FLAG" = y ]]; then
3133		( cd $ROOT; tar cf - . |
3134		    ( cd $NIGHTLY_PARENT_ROOT;  umask 0; tar xpf - ) ) 2>&1 |
3135		    tee -a $mail_msg_file >> $LOGFILE
3136	fi
3137	if [[ "$MULTI_PROTO" = yes && "$F_FLAG" = n ]]; then
3138		rm -rf $NIGHTLY_PARENT_ROOT-nd/*
3139		mkdir -p $NIGHTLY_PARENT_ROOT-nd
3140		cd $ROOT-nd
3141		( tar cf - . |
3142		    ( cd $NIGHTLY_PARENT_ROOT-nd; umask 0; tar xpf - ) ) 2>&1 |
3143		    tee -a $mail_msg_file >> $LOGFILE
3144	fi
3145	if [ -n "${NIGHTLY_PARENT_TOOLS_ROOT}" ]; then
3146		echo "\n==== Copying tools proto area to $NIGHTLY_PARENT_TOOLS_ROOT ====\n" | \
3147		    tee -a $LOGFILE >> $mail_msg_file
3148		rm -rf $NIGHTLY_PARENT_TOOLS_ROOT/*
3149		mkdir -p $NIGHTLY_PARENT_TOOLS_ROOT
3150		if [[ "$MULTI_PROTO" = no || "$D_FLAG" = y ]]; then
3151			( cd $TOOLS_PROTO; tar cf - . |
3152			    ( cd $NIGHTLY_PARENT_TOOLS_ROOT;
3153			    umask 0; tar xpf - ) ) 2>&1 |
3154			    tee -a $mail_msg_file >> $LOGFILE
3155		fi
3156	fi
3157fi
3158
3159#
3160# ELF verification: ABI (-A) and runtime (-r) checks
3161#
3162if [[ ($build_ok = y) && ( ($A_FLAG = y) || ($r_FLAG = y) ) ]]; then
3163	# Directory ELF-data.$MACH holds the files produced by these tests.
3164	elf_ddir=$SRC/ELF-data.$MACH
3165
3166	# If there is a previous ELF-data backup directory, remove it. Then,
3167	# rotate current ELF-data directory into its place and create a new
3168	# empty directory
3169	rm -rf $elf_ddir.ref
3170	if [[ -d $elf_ddir ]]; then
3171		mv $elf_ddir $elf_ddir.ref
3172	fi
3173	mkdir -p $elf_ddir
3174
3175	# Call find_elf to produce a list of the ELF objects in the proto area.
3176	# This list is passed to check_rtime and interface_check, preventing
3177	# them from separately calling find_elf to do the same work twice.
3178	find_elf -fr $checkroot > $elf_ddir/object_list
3179
3180	if [[ $A_FLAG = y ]]; then
3181	       	echo "\n==== Check versioning and ABI information ====\n"  | \
3182		    tee -a $LOGFILE >> $mail_msg_file
3183
3184		# Produce interface description for the proto. Report errors.
3185		interface_check -o -w $elf_ddir -f object_list \
3186			-i interface -E interface.err
3187		if [[ -s $elf_ddir/interface.err ]]; then
3188			tee -a $LOGFILE < $elf_ddir/interface.err \
3189				>> $mail_msg_file
3190		fi
3191
3192		# If ELF_DATA_BASELINE_DIR is defined, compare the new interface
3193		# description file to that from the baseline gate. Issue a
3194		# warning if the baseline is not present, and keep going.
3195		if [[ "$ELF_DATA_BASELINE_DIR" != '' ]]; then
3196			base_ifile="$ELF_DATA_BASELINE_DIR/interface"
3197
3198		       	echo "\n==== Compare versioning and ABI information" \
3199			    "to baseline ====\n"  | \
3200			    tee -a $LOGFILE >> $mail_msg_file
3201		       	echo "Baseline:  $base_ifile\n" >> $LOGFILE
3202
3203			if [[ -f $base_ifile ]]; then
3204				interface_cmp -d -o $base_ifile \
3205				    $elf_ddir/interface > $elf_ddir/interface.cmp
3206				if [[ -s $elf_ddir/interface.cmp ]]; then
3207					echo | tee -a $LOGFILE >> $mail_msg_file
3208					tee -a $LOGFILE < \
3209					    $elf_ddir/interface.cmp \
3210					    >> $mail_msg_file
3211				fi
3212			else
3213			       	echo "baseline not available. comparison" \
3214                                    "skipped" | \
3215				    tee -a $LOGFILE >> $mail_msg_file
3216			fi
3217
3218		fi
3219	fi
3220
3221	if [[ $r_FLAG = y ]]; then
3222		echo "\n==== Check ELF runtime attributes ====\n" | \
3223		    tee -a $LOGFILE >> $mail_msg_file
3224
3225		# If we're doing a DEBUG build the proto area will be left
3226		# with debuggable objects, thus don't assert -s.
3227		if [[ $D_FLAG = y ]]; then
3228			rtime_sflag=""
3229		else
3230			rtime_sflag="-s"
3231		fi
3232		check_rtime -i -m -v $rtime_sflag -o -w $elf_ddir \
3233			-D object_list  -f object_list -E runtime.err \
3234			-I runtime.attr.raw
3235
3236		# check_rtime -I output needs to be sorted in order to
3237		# compare it to that from previous builds.
3238		sort $elf_ddir/runtime.attr.raw > $elf_ddir/runtime.attr
3239		rm $elf_ddir/runtime.attr.raw
3240
3241		# Report errors
3242		if [[ -s $elf_ddir/runtime.err ]]; then
3243			tee -a $LOGFILE < $elf_ddir/runtime.err \
3244				>> $mail_msg_file
3245		fi
3246
3247		# If there is an ELF-data directory from a previous build,
3248		# then diff the attr files. These files contain information
3249		# about dependencies, versioning, and runpaths. There is some
3250		# overlap with the ABI checking done above, but this also
3251		# flushes out non-ABI interface differences along with the
3252		# other information.
3253		echo "\n==== Diff ELF runtime attributes" \
3254		    "(since last build) ====\n" | \
3255		    tee -a $LOGFILE >> $mail_msg_file >> $mail_msg_file
3256
3257		if [[ -f $elf_ddir.ref/runtime.attr ]]; then
3258			diff $elf_ddir.ref/runtime.attr \
3259				$elf_ddir/runtime.attr \
3260				>> $mail_msg_file
3261		fi
3262	fi
3263
3264	# If -u set, copy contents of ELF-data.$MACH to the parent workspace.
3265	if [[ "$u_FLAG" = "y" ]]; then
3266		p_elf_ddir=$PARENT_WS/usr/src/ELF-data.$MACH
3267
3268		# If parent lacks the ELF-data.$MACH directory, create it
3269		if [[ ! -d $p_elf_ddir ]]; then
3270			staffer mkdir -p $p_elf_ddir
3271		fi
3272
3273		# These files are used asynchronously by other builds for ABI
3274		# verification, as above for the -A option. As such, we require
3275		# the file replacement to be atomic. Copy the data to a temp
3276		# file in the same filesystem and then rename into place.
3277		(
3278			cd $elf_ddir
3279			for elf_dfile in *; do
3280				staffer cp $elf_dfile \
3281				    ${p_elf_ddir}/${elf_dfile}.new
3282				staffer mv -f ${p_elf_ddir}/${elf_dfile}.new \
3283				    ${p_elf_ddir}/${elf_dfile}
3284			done
3285		)
3286	fi
3287fi
3288
3289# DEBUG lint of kernel begins
3290
3291if [ "$i_CMD_LINE_FLAG" = "n" -a "$l_FLAG" = "y" ]; then
3292	if [ "$LINTDIRS" = "" ]; then
3293		# LINTDIRS="$SRC/uts y $SRC/stand y $SRC/psm y"
3294		LINTDIRS="$SRC y"
3295	fi
3296	set $LINTDIRS
3297	while [ $# -gt 0 ]; do
3298		dolint $1 $2; shift; shift
3299	done
3300else
3301	echo "\n==== No '$MAKE lint' ====\n" >> $LOGFILE
3302fi
3303
3304# "make check" begins
3305
3306if [ "$i_CMD_LINE_FLAG" = "n" -a "$C_FLAG" = "y" ]; then
3307	# remove old check.out
3308	rm -f $SRC/check.out
3309
3310	rm -f $SRC/check-${MACH}.out
3311	cd $SRC
3312	$MAKE -ek check 2>&1 | tee -a $SRC/check-${MACH}.out >> $LOGFILE
3313	echo "\n==== cstyle/hdrchk errors ====\n" >> $mail_msg_file
3314
3315	grep ":" $SRC/check-${MACH}.out |
3316		egrep -v "Ignoring unknown host" | \
3317		sort | uniq >> $mail_msg_file
3318else
3319	echo "\n==== No '$MAKE check' ====\n" >> $LOGFILE
3320fi
3321
3322echo "\n==== Find core files ====\n" | \
3323    tee -a $LOGFILE >> $mail_msg_file
3324
3325find $abssrcdirs -name core -a -type f -exec file {} \; | \
3326	tee -a $LOGFILE >> $mail_msg_file
3327
3328if [ "$f_FLAG" = "y" -a "$build_ok" = "y" ]; then
3329	echo "\n==== Diff unreferenced files (since last build) ====\n" \
3330	    | tee -a $LOGFILE >>$mail_msg_file
3331	rm -f $SRC/unref-${MACH}.ref
3332	if [ -f $SRC/unref-${MACH}.out ]; then
3333		mv $SRC/unref-${MACH}.out $SRC/unref-${MACH}.ref
3334	fi
3335
3336	findunref -S $SCM_TYPE -t $SRC/.build.tstamp -s usr $CODEMGR_WS \
3337	    ${TOOLS}/findunref/exception_list 2>> $mail_msg_file | \
3338	    sort > $SRC/unref-${MACH}.out
3339
3340	if [ ! -f $SRC/unref-${MACH}.ref ]; then
3341		cp $SRC/unref-${MACH}.out $SRC/unref-${MACH}.ref
3342	fi
3343
3344	diff $SRC/unref-${MACH}.ref $SRC/unref-${MACH}.out >>$mail_msg_file
3345fi
3346
3347#
3348# Generate the OpenSolaris deliverables if requested.  Some of these
3349# steps need to come after findunref and are commented below.
3350#
3351
3352#
3353# Copy an input crypto tarball to the canonical destination (with
3354# datestamp), and point the non-stamped symlink at it.
3355# Usage: copyin_crypto from_path suffix
3356# Returns 0 if successful, non-zero if not.
3357#
3358function copyin_crypto {
3359	typeset from=$1
3360	typeset suffix=$2
3361	typeset to=$(cryptodest "$suffix").bz2
3362	typeset -i stat
3363	cp "$from" "$to"
3364	stat=$?
3365	if (( $stat == 0 )); then
3366		cryptolink "$to" "$suffix"
3367		stat=$?
3368	fi
3369	return $stat
3370}
3371
3372#
3373# Copy a crypto tarball to $CODEMGR_WS to go with the other
3374# OpenSolaris deliverables.
3375# Usage: copyout_crypto suffix
3376# where $suffix is "" or "-nd".
3377#
3378function copyout_crypto {
3379	typeset suffix=$1
3380	typeset cryptof=on-crypto$suffix.$MACH.tar.bz2
3381	[ -f $cryptof ] && rm $cryptof
3382	cp $(cryptodest "$suffix").bz2 $cryptof
3383}
3384
3385#
3386# Pass through the crypto tarball(s) that we were given, putting it in
3387# the same place that crypto_from_proto puts things.
3388# Returns with non-zero status if there is a problem.
3389#
3390function crypto_passthrough {
3391	echo "Reusing $ON_CRYPTO_BINS for crypto tarball(s)..." >> "$LOGFILE"
3392	typeset -i stat=0
3393	if [ "$D_FLAG" = y ]; then
3394		copyin_crypto "$ON_CRYPTO_BINS" "" >> "$LOGFILE" 2>&1
3395		if (( $? != 0 )) ; then
3396			echo "Couldn't create DEBUG crypto tarball." |
3397			    tee -a "$mail_msg_file" >> "$LOGFILE"
3398			stat=1
3399		fi
3400	fi
3401	if [ "$F_FLAG" = n ]; then
3402		copyin_crypto $(ndcrypto "$ON_CRYPTO_BINS") "-nd" \
3403		    >> "$LOGFILE" 2>&1
3404		if (( $? != 0 )) ; then
3405			echo "Couldn't create non-DEBUG crypto tarball." |
3406			    tee -a "$mail_msg_file" >> "$LOGFILE"
3407			stat=1
3408		fi
3409	fi
3410
3411	return $stat
3412}
3413
3414# If we are doing an OpenSolaris _source_ build (-S O) then we do
3415# not have usr/closed available to us to generate closedbins from,
3416# so skip this part.
3417if [ "$SO_FLAG" = n -a "$O_FLAG" = y -a "$build_ok" = y ]; then
3418	echo "\n==== Generating OpenSolaris tarballs ====\n" | \
3419	    tee -a $mail_msg_file >> $LOGFILE
3420
3421	cd $CODEMGR_WS
3422
3423	#
3424	# This step grovels through the package manifests, so it
3425	# must come after findunref.
3426	#
3427	# We assume no DEBUG vs non-DEBUG package content variation
3428	# here; if that changes, then the "make all" in $SRC/pkg will
3429	# need to be moved into the conditionals and repeated for each
3430	# different build.
3431	#
3432	echo "Generating closed binaries tarball(s)..." >> $LOGFILE
3433	closed_basename=on-closed-bins
3434	if [ "$D_FLAG" = y ]; then
3435		bindrop "$closed_basename" >>"$LOGFILE" 2>&1
3436		if (( $? != 0 )) ; then
3437			echo "Couldn't create DEBUG closed binaries." |
3438			    tee -a $mail_msg_file >> $LOGFILE
3439			build_ok=n
3440		fi
3441	fi
3442	if [ "$F_FLAG" = n ]; then
3443		bindrop -n "$closed_basename-nd" >>"$LOGFILE" 2>&1
3444		if (( $? != 0 )) ; then
3445			echo "Couldn't create non-DEBUG closed binaries." |
3446			    tee -a $mail_msg_file >> $LOGFILE
3447			build_ok=n
3448		fi
3449	fi
3450
3451	echo "Generating README.opensolaris..." >> $LOGFILE
3452	cat $SRC/tools/opensolaris/README.opensolaris.tmpl | \
3453	    mkreadme_osol $CODEMGR_WS/README.opensolaris >> $LOGFILE 2>&1
3454	if (( $? != 0 )) ; then
3455		echo "Couldn't create README.opensolaris." |
3456		    tee -a $mail_msg_file >> $LOGFILE
3457		build_ok=n
3458	fi
3459
3460	typeset have_crypto=y
3461	if [ -n "$ON_CRYPTO_BINS" ]; then
3462		crypto_passthrough || have_crypto=n
3463	fi
3464	#
3465	# Make another copy of the crypto so that all the OpenSolaris
3466	# deliverables are in $CODEMGR_WS.
3467	#
3468	if [ "$have_crypto" != y ]; then
3469		build_ok=n
3470	else
3471		echo "Copying crypto tarball to $CODEMGR_WS" >> "$LOGFILE"
3472		if [ "$D_FLAG" = y ]; then
3473			copyout_crypto "" >> "$LOGFILE" 2>&1
3474			if (( $? != 0 )) ; then
3475				echo "Couldn't create DEBUG crypto tarball" |
3476				    tee -a $mail_msg_file >> "$LOGFILE"
3477				build_ok=n
3478			fi
3479		fi
3480		if [ "$F_FLAG" = n ]; then
3481			copyout_crypto "-nd" >> "$LOGFILE" 2>&1
3482			if (( $? != 0 )) ; then
3483				echo "Couldn't create non-DEBUG" \
3484				    "crypto tarball" |
3485				    tee -a $mail_msg_file >> "$LOGFILE"
3486				build_ok=n
3487			fi
3488		fi
3489	fi
3490fi
3491
3492# Verify that the usual lists of files, such as exception lists,
3493# contain only valid references to files.  If the build has failed,
3494# then don't check the proto area.
3495CHECK_PATHS=${CHECK_PATHS:-y}
3496if [ "$CHECK_PATHS" = y -a "$N_FLAG" != y ]; then
3497	echo "\n==== Check lists of files ====\n" | tee -a $LOGFILE \
3498		>>$mail_msg_file
3499	arg=-b
3500	[ "$build_ok" = y ] && arg=
3501	checkpaths $arg $checkroot 2>&1 | tee -a $LOGFILE >>$mail_msg_file
3502fi
3503
3504if [ "$M_FLAG" != "y" -a "$build_ok" = y ]; then
3505	echo "\n==== Impact on file permissions ====\n" \
3506		>> $mail_msg_file
3507
3508	abspkgdefs=
3509	abspkg=
3510	for d in $abssrcdirs; do
3511		if [ -d "$d/pkgdefs" ]; then
3512			abspkgdefs="$abspkgdefs $d"
3513		fi
3514		if [ -d "$d/pkg" ]; then
3515			abspkg="$abspkg $d"
3516		fi
3517	done
3518
3519	if [ -n "$abspkgdefs" ]; then
3520		pmodes -qvdP \
3521		    `find $abspkgdefs -name pkginfo.tmpl -print -o \
3522		    -name .del\* -prune | sed -e 's:/pkginfo.tmpl$::' | \
3523		    sort -u` >> $mail_msg_file
3524	fi
3525
3526	if [ -n "$abspkg" ]; then
3527		for d in "$abspkg"; do
3528			( cd $d/pkg ; $MAKE -e pmodes ) >> $mail_msg_file
3529		done
3530	fi
3531fi
3532
3533if [ "$w_FLAG" = "y" -a "$build_ok" = "y" ]; then
3534	if [[ "$MULTI_PROTO" = no || "$D_FLAG" = y ]]; then
3535		do_wsdiff DEBUG $ROOT.prev $ROOT
3536	fi
3537
3538	if [[ "$MULTI_PROTO" = yes && "$F_FLAG" = n ]]; then
3539		do_wsdiff non-DEBUG $ROOT-nd.prev $ROOT-nd
3540	fi
3541fi
3542
3543END_DATE=`date`
3544echo "==== Nightly $maketype build completed: $END_DATE ====" | \
3545    tee -a $LOGFILE >> $build_time_file
3546
3547typeset -i10 hours
3548typeset -Z2 minutes
3549typeset -Z2 seconds
3550
3551elapsed_time=$SECONDS
3552((hours = elapsed_time / 3600 ))
3553((minutes = elapsed_time / 60  % 60))
3554((seconds = elapsed_time % 60))
3555
3556echo "\n==== Total build time ====" | \
3557    tee -a $LOGFILE >> $build_time_file
3558echo "\nreal    ${hours}:${minutes}:${seconds}" | \
3559    tee -a $LOGFILE >> $build_time_file
3560
3561if [ "$u_FLAG" = "y" -a "$f_FLAG" = "y" -a "$build_ok" = "y" ]; then
3562	staffer cp ${SRC}/unref-${MACH}.out $PARENT_WS/usr/src/
3563
3564	#
3565	# Produce a master list of unreferenced files -- ideally, we'd
3566	# generate the master just once after all of the nightlies
3567	# have finished, but there's no simple way to know when that
3568	# will be.  Instead, we assume that we're the last nightly to
3569	# finish and merge all of the unref-${MACH}.out files in
3570	# $PARENT_WS/usr/src/.  If we are in fact the final ${MACH} to
3571	# finish, then this file will be the authoritative master
3572	# list.  Otherwise, another ${MACH}'s nightly will eventually
3573	# overwrite ours with its own master, but in the meantime our
3574	# temporary "master" will be no worse than any older master
3575	# which was already on the parent.
3576	#
3577
3578	set -- $PARENT_WS/usr/src/unref-*.out
3579	cp "$1" ${TMPDIR}/unref.merge
3580	shift
3581
3582	for unreffile; do
3583		comm -12 ${TMPDIR}/unref.merge "$unreffile" > ${TMPDIR}/unref.$$
3584		mv ${TMPDIR}/unref.$$ ${TMPDIR}/unref.merge
3585	done
3586
3587	staffer cp ${TMPDIR}/unref.merge $PARENT_WS/usr/src/unrefmaster.out
3588fi
3589
3590#
3591# All done save for the sweeping up.
3592# (whichever exit we hit here will trigger the "cleanup" trap which
3593# optionally sends mail on completion).
3594#
3595if [ "$build_ok" = "y" ]; then
3596	exit 0
3597fi
3598exit 1
3599