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