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