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