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