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