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