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