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