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