xref: /titanic_41/usr/src/tools/scripts/nightly.sh (revision b0fc0e77220f1fa4c933fd58a4e1dedcd650b0f1)
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# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26# ident	"%Z%%M%	%I%	%E% SMI"
27#
28# Based on the nightly script from the integration folks,
29# Mostly modified and owned by mike_s.
30# Changes also by kjc, dmk.
31#
32# BRINGOVER_WS may be specified in the env file.
33# The default is the old behavior of CLONE_WS
34#
35# -i on the command line, means fast options, so when it's on the
36# command line (only), lint and check builds are skipped no matter what
37# the setting of their individual flags are in NIGHTLY_OPTIONS.
38#
39# LINTDIRS can be set in the env file, format is a list of:
40#
41#	/dirname-to-run-lint-on flag
42#
43#	Where flag is:	y - enable lint noise diff output
44#			n - disable lint noise diff output
45#
46#	For example: LINTDIRS="$SRC/uts n $SRC/stand y $SRC/psm y"
47#
48# -A flag in NIGHTLY_OPTIONS checks ABI diffs in .so files
49# This option requires a couple of scripts.
50#
51# OPTHOME and TEAMWARE may be set in the environment to override /opt
52# and /opt/teamware defaults.
53#
54
55#
56# The CDPATH variable causes ksh's `cd' builtin to emit messages to stdout
57# under certain circumstances, which can really screw things up; unset it.
58#
59unset CDPATH
60
61# function to do a DEBUG and non-DEBUG build. Needed because we might
62# need to do another for the source build, and since we only deliver DEBUG or
63# non-DEBUG packages.
64
65normal_build() {
66
67	# timestamp the start of a nightly build; the findunref tool uses it.
68	touch $SRC/.build.tstamp
69
70	# non-DEBUG build begins
71
72	if [ "$F_FLAG" = "n" ]; then
73		export INTERNAL_RELEASE_BUILD ; INTERNAL_RELEASE_BUILD=
74		export RELEASE_BUILD ; RELEASE_BUILD=
75		unset EXTRA_OPTIONS
76		unset EXTRA_CFLAGS
77
78		build non-DEBUG -nd
79
80		if [ "$build_ok" = "y" -a "$X_FLAG" = "y" -a "$p_FLAG" = "y" ]; then
81			copy_ihv_pkgs non-DEBUG -nd
82		fi
83	else
84		echo "\n==== No non-DEBUG build ====\n" >> $LOGFILE
85	fi
86
87	# non-DEBUG build ends
88
89	# DEBUG build begins
90
91	if [ "$D_FLAG" = "y" ]; then
92
93		export INTERNAL_RELEASE_BUILD ; INTERNAL_RELEASE_BUILD=
94		unset RELEASE_BUILD
95		unset EXTRA_OPTIONS
96		unset EXTRA_CFLAGS
97
98		build DEBUG ""
99
100		if [ "$build_ok" = "y" -a "$X_FLAG" = "y" -a "$p_FLAG" = "y" ]; then
101			copy_ihv_pkgs DEBUG ""
102		fi
103
104	else
105		echo "\n==== No DEBUG build ====\n" >> $LOGFILE
106	fi
107
108	# DEBUG build ends
109}
110
111#
112# usage: filelist DESTDIR PATTERN
113#
114filelist() {
115	DEST=$1
116	PATTERN=$2
117	cd ${DEST}
118
119	OBJFILES=${ORIG_SRC}/xmod/obj_files
120	if [ ! -f ${OBJFILES} ]; then
121		return;
122	fi
123	for i in `grep -v '^#' ${OBJFILES} | \
124	    grep ${PATTERN} | cut -d: -f2 | tr -d ' \t'`
125	do
126		# wildcard expansion
127		for j in $i
128		do
129			if [ -f "$j" ]; then
130				echo $j
131			fi
132			if [ -d "$j" ]; then
133				echo $j
134			fi
135		done
136	done | sort | uniq
137}
138
139# function to save off binaries after a full build for later
140# restoration
141save_binaries() {
142	# save off list of binaries
143	echo "\n==== Saving binaries from build at `date` ====\n" | \
144	    tee -a $mail_msg_file >> $LOGFILE
145	rm -f ${BINARCHIVE}
146	cd ${CODEMGR_WS}
147	filelist ${CODEMGR_WS} '^preserve:' >> $LOGFILE
148	filelist ${CODEMGR_WS} '^preserve:' | \
149	    cpio -ocB 2>/dev/null | compress \
150	    > ${BINARCHIVE}
151}
152
153# delete files
154# usage: hybridize_files DESTDIR MAKE_TARGET
155hybridize_files() {
156	DEST=$1
157	MAKETARG=$2
158
159	echo "\n==== Hybridizing files at `date` ====\n" | \
160	    tee -a $mail_msg_file >> $LOGFILE
161	for i in `filelist ${DEST} '^delete:'`
162	do
163		echo "removing ${i}." | tee -a $mail_msg_file >> $LOGFILE
164		rm -rf "${i}"
165	done
166	for i in `filelist ${DEST} '^hybridize:' `
167	do
168		echo "hybridizing ${i}." | tee -a $mail_msg_file >> $LOGFILE
169		rm -f ${i}+
170		sed -e "/^# HYBRID DELETE START/,/^# HYBRID DELETE END/d" \
171		    < ${i} > ${i}+
172		mv ${i}+ ${i}
173	done
174}
175
176# restore binaries into the proper source tree.
177# usage: restore_binaries DESTDIR MAKE_TARGET
178restore_binaries() {
179	DEST=$1
180	MAKETARG=$2
181
182	echo "\n==== Restoring binaries to ${MAKETARG} at `date` ====\n" | \
183	    tee -a $mail_msg_file >> $LOGFILE
184	cd ${DEST}
185	zcat ${BINARCHIVE} | \
186	    cpio -idmucvB 2>/dev/null | tee -a $mail_msg_file >> ${LOGFILE}
187}
188
189# rename files we save binaries of
190# usage: rename_files DESTDIR MAKE_TARGET
191rename_files() {
192	DEST=$1
193	MAKETARG=$2
194	echo "\n==== Renaming source files in ${MAKETARG} at `date` ====\n" | \
195	    tee -a $mail_msg_file >> $LOGFILE
196	for i in `filelist ${DEST} '^rename:'`
197	do
198		echo ${i} | tee -a $mail_msg_file >> ${LOGFILE}
199		rm -f ${i}.export
200		mv ${i} ${i}.export
201	done
202}
203
204#
205# Copy some or all of the source tree.
206# usage: copy_source CODEMGR_WS DESTDIR LABEL SRCROOT
207#
208copy_source() {
209	WS=$1
210	DEST=$2
211	label=$3
212	srcroot=$4
213
214	echo "\n==== Creating ${DEST} source from ${WS} ($label) ====\n" | \
215	    tee -a $mail_msg_file >> $LOGFILE
216
217	echo "cleaning out ${DEST}." >> $LOGFILE
218	rm -rf "${DEST}" >> $LOGFILE 2>&1
219
220	mkdir -p ${DEST}
221	cd ${WS}
222
223	echo "creating ${DEST}." >> $LOGFILE
224	find $srcroot -name 's\.*' -a -type f -print | \
225	    sed -e 's,SCCS\/s.,,' | \
226	    grep -v '/\.del-*' | \
227	    cpio -pd ${DEST} >>$LOGFILE 2>&1
228}
229
230#
231# function to create (but not build) the export/crypt source tree.
232# usage: set_up_source_build CODEMGR_WS DESTDIR MAKE_TARGET
233# Sets SRC to the modified source tree, for use by the caller when it
234# builds the tree.
235#
236set_up_source_build() {
237	WS=$1
238	DEST=$2
239	MAKETARG=$3
240
241	copy_source $WS $DEST $MAKETARG usr
242	SRC=${DEST}/usr/src
243
244	cd $SRC
245	rm -f ${MAKETARG}.out
246	echo "making ${MAKETARG} in ${SRC}." >> $LOGFILE
247	/bin/time $MAKE -e ${MAKETARG} 2>&1 | \
248	    tee -a $SRC/${MAKETARG}.out >> $LOGFILE
249	echo "\n==== ${MAKETARG} build errors ====\n" >> $mail_msg_file
250	egrep ":" $SRC/${MAKETARG}.out | \
251		egrep -e "(^${MAKE}:|[ 	]error[: 	\n])" | \
252		egrep -v "Ignoring unknown host" | \
253		egrep -v "warning" >> $mail_msg_file
254
255	echo "clearing state files." >> $LOGFILE
256	find . -name '.make*' -exec rm -f {} \;
257
258	cd ${DEST}
259	if [ "${MAKETARG}" = "CRYPT_SRC" ]; then
260		rm -f ${CODEMGR_WS}/crypt_files.cpio.Z
261		echo "\n==== xmod/cry_files that don't exist ====\n" | \
262		    tee -a $mail_msg_file >> $LOGFILE
263		CRYPT_FILES=${WS}/usr/src/xmod/cry_files
264		for i in `cat ${CRYPT_FILES}`
265		do
266			# make sure the files exist
267			if [ -f "$i" ]; then
268				continue
269			fi
270			if [ -d "$i" ]; then
271				continue
272			fi
273			echo "$i" | tee -a $mail_msg_file >> $LOGFILE
274		done
275		find `cat ${CRYPT_FILES}` -print 2>/dev/null | \
276		    cpio -ocB 2>/dev/null | \
277		    compress > ${CODEMGR_WS}/crypt_files.cpio.Z
278	fi
279
280	if [ "${MAKETARG}" = "EXPORT_SRC" ]; then
281		# rename first, since we might restore a file
282		# of the same name (mapfiles)
283		rename_files ${EXPORT_SRC} EXPORT_SRC
284		if [ "$SH_FLAG" = "y" ]; then
285			hybridize_files ${EXPORT_SRC} EXPORT_SRC
286		fi
287	fi
288
289	# save the cleartext
290	echo "\n==== Creating ${MAKETARG}.cpio.Z ====\n" | \
291	    tee -a $mail_msg_file >> $LOGFILE
292	cd ${DEST}
293	rm -f ${MAKETARG}.cpio.Z
294	find usr -depth -print | \
295	    grep -v usr/src/${MAKETARG}.out | \
296	    cpio -ocB 2>/dev/null | \
297	    compress > ${CODEMGR_WS}/${MAKETARG}.cpio.Z
298	if [ "${MAKETARG}" = "EXPORT_SRC" ]; then
299		restore_binaries ${EXPORT_SRC} EXPORT_SRC
300	fi
301
302	if [ "${MAKETARG}" = "CRYPT_SRC" ]; then
303		restore_binaries ${CRYPT_SRC} CRYPT_SRC
304	fi
305
306}
307
308# function to do the build.
309# usage: build LABEL SUFFIX
310
311build() {
312	LABEL=$1
313	SUFFIX=$2
314	INSTALLOG=install${SUFFIX}-${MACH}
315	NOISE=noise${SUFFIX}-${MACH}
316	CPIODIR=${CPIODIR_ORIG}${SUFFIX}
317	PKGARCHIVE=${PKGARCHIVE_ORIG}${SUFFIX}
318	if [ "$SPARC_RM_PKGARCHIVE_ORIG" ]; then
319		SPARC_RM_PKGARCHIVE=${SPARC_RM_PKGARCHIVE_ORIG}${SUFFIX}
320	fi
321
322	#remove old logs
323	OLDINSTALLOG=install${SUFFIX}
324	OLDNOISE=noise${SUFFIX}
325	rm -f $SRC/${OLDINSTALLOG}.out
326	rm -f $SRC/${OLDNOISE}.ref
327	if [ -f $SRC/${OLDNOISE}.out ]; then
328		mv $SRC/${OLDNOISE}.out $SRC/${NOISE}.ref
329	fi
330
331	this_build_ok=y
332	#
333	#	Build OS-Networking source
334	#
335	echo "\n==== Building OS-Net source at `date` ($LABEL) ====\n" \
336		>> $LOGFILE
337
338	rm -f $SRC/${INSTALLOG}.out
339	cd $SRC
340	/bin/time $MAKE -e install 2>&1 | \
341	    tee -a $SRC/${INSTALLOG}.out >> $LOGFILE
342
343	echo "\n==== SCCS Noise ($LABEL) ====\n" >> $mail_msg_file
344
345	egrep 'sccs(check|  get)' $SRC/${INSTALLOG}.out >> $mail_msg_file
346
347	echo "\n==== Build errors ($LABEL) ====\n" >> $mail_msg_file
348	egrep ":" $SRC/${INSTALLOG}.out |
349		egrep -e "(^${MAKE}:|[ 	]error[: 	\n])" | \
350		egrep -v "Ignoring unknown host" | \
351		egrep -v "cc .* -o error " | \
352		egrep -v "warning" >> $mail_msg_file
353	if [ "$?" = "0" ]; then
354		build_ok=n
355		this_build_ok=n
356	fi
357	grep "bootblock image is .* bytes too big" $SRC/${INSTALLOG}.out \
358		>> $mail_msg_file
359	if [ "$?" = "0" ]; then
360		build_ok=n
361		this_build_ok=n
362	fi
363
364	if [ "$W_FLAG" = "n" ]; then
365		echo "\n==== Build warnings ($LABEL) ====\n" >>$mail_msg_file
366		egrep -i warning: $SRC/${INSTALLOG}.out \
367			| egrep -v '^tic:' \
368			| egrep -v "symbol \`timezone' has differing types:" \
369		        | egrep -v "parameter <PSTAMP> set to" \
370			| egrep -v "Ignoring unknown host" \
371			| egrep -v "redefining segment flags attribute for" \
372			>> $mail_msg_file
373	fi
374
375	echo "\n==== Ended OS-Net source build at `date` ($LABEL) ====\n" \
376		>> $LOGFILE
377
378	echo "\n==== Elapsed build time ($LABEL) ====\n" >>$mail_msg_file
379	tail -3  $SRC/${INSTALLOG}.out >>$mail_msg_file
380
381	if [ "$i_FLAG" = "n" -a "$W_FLAG" = "n" ]; then
382		rm -f $SRC/${NOISE}.ref
383		if [ -f $SRC/${NOISE}.out ]; then
384			mv $SRC/${NOISE}.out $SRC/${NOISE}.ref
385		fi
386		grep : $SRC/${INSTALLOG}.out \
387			| egrep -v '^/' \
388			| egrep -v '^(Start|Finish|real|user|sys|./bld_awk)' \
389			| egrep -v '^tic:' \
390			| egrep -v '^mcs' \
391			| egrep -v '^LD_LIBRARY_PATH=' \
392			| egrep -v 'ar: creating' \
393			| egrep -v 'ar: writing' \
394			| egrep -v 'conflicts:' \
395			| egrep -v ':saved created' \
396			| egrep -v '^stty.*c:' \
397			| egrep -v '^mfgname.c:' \
398			| egrep -v '^uname-i.c:' \
399			| egrep -v '^volumes.c:' \
400			| egrep -v '^lint library construction:' \
401			| egrep -v 'tsort: INFORM:' \
402			| egrep -v 'stripalign:' \
403			| egrep -v 'chars, width' \
404			| egrep -v "symbol \`timezone' has differing types:" \
405			| egrep -v 'PSTAMP' \
406			| egrep -v '|%WHOANDWHERE%|' \
407			| egrep -v '^Manifying' \
408			| egrep -v 'Ignoring unknown host' \
409			| egrep -v 'Processing method:' \
410			| egrep -v '^Writing' \
411			| egrep -v 'spellin1:' \
412			| egrep -v '^adding:' \
413			| egrep -v "^echo 'msgid" \
414			| egrep -v '^echo ' \
415			| egrep -v '\.c:$' \
416			| egrep -v '^Adding file:' \
417			| egrep -v 'CLASSPATH=' \
418			| egrep -v '\/var\/mail\/:saved' \
419			| egrep -v -- '-DUTS_VERSION=' \
420			| egrep -v '^Running Mkbootstrap' \
421			| egrep -v '^Applet length read:' \
422			| egrep -v 'bytes written:' \
423			| egrep -v '^File:SolarisAuthApplet.bin' \
424			| egrep -v -i 'jibversion' \
425			| egrep -v '^Output size:' \
426			| egrep -v '^Solo size statistics:' \
427			| egrep -v '^Using ROM API Version' \
428			| egrep -v '^Zero Signature length:' \
429			| egrep -v '^Note \(probably harmless\):' \
430			| egrep -v '::' \
431			| egrep -v -- '-xcache' \
432			| egrep -v '^\+' \
433			| egrep -v '^cc1: note: -fwritable-strings' \
434			| egrep -v 'svccfg-native -s svc:/' \
435			| sort | uniq >$SRC/${NOISE}.out
436		if [ ! -f $SRC/${NOISE}.ref ]; then
437			cp $SRC/${NOISE}.out $SRC/${NOISE}.ref
438		fi
439		echo "\n==== Build noise differences ($LABEL) ====\n" \
440			>>$mail_msg_file
441		diff $SRC/${NOISE}.ref $SRC/${NOISE}.out >>$mail_msg_file
442	fi
443
444	#
445	#	Create cpio archives for preintegration testing (PIT)
446	#
447	if [ "$a_FLAG" = "y" -a "$this_build_ok" = "y" ]; then
448		echo "\n==== Creating $LABEL cpio archives at `date` ====\n" \
449			>> $LOGFILE
450		makebfu_file="${TMPDIR}/makebfu"
451		rm -f ${makebfu_file}
452		makebfu 2>&1 | \
453			tee -a ${makebfu_file} >> $LOGFILE
454		echo "\n==== cpio archives build errors ($LABEL) ====\n" \
455			>> $mail_msg_file
456		grep -v "^Creating .* archive:" ${makebfu_file} | \
457			grep -v "^Making" | \
458			grep -v "^$" | \
459			sort | uniq >> $mail_msg_file
460		rm -f ${makebfu_file}
461		# hack for test folks
462		if [ -z "`echo $PARENT_WS|egrep '^\/ws\/'`" ]; then
463			X=/net/`uname -n`${CPIODIR}
464		else
465			X=${CPIODIR}
466		fi
467		echo "Archive_directory: ${X}" >${TMPDIR}/f
468		cp ${TMPDIR}/f ${CPIODIR}/../../.${MACH}_wgtrun
469		rm -f ${TMPDIR}/f
470
471	else
472		echo "\n==== Not creating $LABEL cpio archives ====\n" \
473			>> $LOGFILE
474	fi
475
476	#
477	#	Building Packages
478	#
479	if [ "$p_FLAG" = "y" -a "$this_build_ok" = "y" ]; then
480		echo "\n==== Creating $LABEL packages at `date` ====\n" \
481			>> $LOGFILE
482		rm -f $SRC/pkgdefs/${INSTALLOG}.out
483		echo "Clearing out $PKGARCHIVE ..." >> $LOGFILE
484		rm -rf $PKGARCHIVE
485		mkdir -p $PKGARCHIVE
486
487		#
488		# Optional build of sparc realmode on i386
489		#
490		if [ "$MACH" = "i386" ] && [ "${SPARC_RM_PKGARCHIVE}" ]; then
491			echo "Clearing out ${SPARC_RM_PKGARCHIVE} ..." \
492				>> $LOGFILE
493			rm -rf ${SPARC_RM_PKGARCHIVE}
494			mkdir -p ${SPARC_RM_PKGARCHIVE}
495		fi
496
497		cd $SRC/pkgdefs
498		$MAKE -e install 2>&1 | \
499			tee -a $SRC/pkgdefs/${INSTALLOG}.out >> $LOGFILE
500		echo "\n==== Package build errors ($LABEL) ====\n" \
501			>> $mail_msg_file
502		egrep "${MAKE}|ERROR|WARNING" $SRC/pkgdefs/${INSTALLOG}.out | \
503			grep ':' | \
504			grep -v PSTAMP | \
505			egrep -v "Ignoring unknown host" \
506			>> $mail_msg_file
507	else
508		echo "\n==== Not creating $LABEL packages ====\n" >> $LOGFILE
509	fi
510}
511
512# Usage: dolint /dir y|n
513# Arg. 2 is a flag to turn on/off the lint diff output
514dolint() {
515	if [ ! -d "$1" ]; then
516		echo "dolint error: $1 is not a directory"
517		exit 1
518	fi
519
520	if [ "$2" != "y" -a "$2" != "n" ]; then
521		echo "dolint internal error: $2 should be 'y' or 'n'"
522		exit 1
523	fi
524
525	lintdir=$1
526	dodiff=$2
527	base=`basename $lintdir`
528	LINTOUT=$lintdir/lint-${MACH}.out
529	LINTNOISE=$lintdir/lint-noise-${MACH}
530
531	export INTERNAL_RELEASE_BUILD ; INTERNAL_RELEASE_BUILD=
532	unset RELEASE_BUILD
533	unset EXTRA_OPTIONS
534	unset EXTRA_CFLAGS
535
536	#
537	#	'$MAKE lint' in $lintdir
538	#
539	echo "\n==== Begin '$MAKE lint' of $base at `date` ====\n" >> $LOGFILE
540
541	# remove old lint.out
542	rm -f $lintdir/lint.out $lintdir/lint-noise.out
543	if [ -f $lintdir/lint-noise.ref ]; then
544		mv $lintdir/lint-noise.ref ${LINTNOISE}.ref
545	fi
546
547	rm -f $LINTOUT
548	cd $lintdir
549	#
550	# Remove all .ln files to ensure a full reference file
551	#
552	rm -f Nothing_to_remove \
553	    `find . -name SCCS -prune -o -type f -name '*.ln' -print `
554
555	/bin/time $MAKE -ek lint 2>&1 | \
556	    tee -a $LINTOUT >> $LOGFILE
557	echo "\n==== '$MAKE lint' of $base ERRORS ====\n" >> $mail_msg_file
558	grep "$MAKE:" $LINTOUT |
559		egrep -v "Ignoring unknown host" \
560		>> $mail_msg_file
561
562	echo "\n==== Ended '$MAKE lint' of $base at `date` ====\n" >> $LOGFILE
563
564	echo "\n==== Elapsed time of '$MAKE lint' of $base ====\n" \
565		>>$mail_msg_file
566	tail -3  $LINTOUT >>$mail_msg_file
567
568	rm -f ${LINTNOISE}.ref
569	if [ -f ${LINTNOISE}.out ]; then
570		mv ${LINTNOISE}.out ${LINTNOISE}.ref
571	fi
572        grep : $LINTOUT | \
573		egrep -v '^(real|user|sys)' |
574		egrep -v '(library construction)' | \
575		egrep -v ': global crosschecks' | \
576		egrep -v 'Ignoring unknown host' | \
577		egrep -v '\.c:$' | \
578		sort | uniq > ${LINTNOISE}.out
579	if [ ! -f ${LINTNOISE}.ref ]; then
580		cp ${LINTNOISE}.out ${LINTNOISE}.ref
581	fi
582	if [ "$dodiff" != "n" ]; then
583		echo "\n==== lint warnings $base ====\n" \
584			>>$mail_msg_file
585		# should be none, though there are a few that were filtered out
586		# above
587		egrep -i '(warning|lint):' ${LINTNOISE}.out \
588			| sort | uniq >> $mail_msg_file
589		echo "\n==== lint noise differences $base ====\n" \
590			>> $mail_msg_file
591		diff ${LINTNOISE}.ref ${LINTNOISE}.out \
592			>> $mail_msg_file
593	fi
594}
595
596# Install proto area from IHV build
597
598copy_ihv_proto() {
599
600	echo "\n==== Installing IHV proto area ====\n" \
601		>> $LOGFILE
602	if [ -d "$IA32_IHV_ROOT" ]; then
603		if [ ! -d "$ROOT" ]; then
604			echo "mkdir -p $ROOT" >> $LOGFILE
605			mkdir -p $ROOT
606		fi
607		echo "copying $IA32_IHV_ROOT to $ROOT\n" >> $LOGFILE
608		cd $IA32_IHV_ROOT
609		tar -cf - . | (cd $ROOT; umask 0; tar xpf - ) 2>&1 >> $LOGFILE
610	else
611		echo "$IA32_IHV_ROOT: not found" >> $LOGFILE
612	fi
613}
614
615# Install IHV packages in PKGARCHIVE
616# usage: copy_ihv_pkgs LABEL SUFFIX
617copy_ihv_pkgs() {
618	LABEL=$1
619	SUFFIX=$2
620	# always use non-DEBUG IHV packages
621	IA32_IHV_PKGS=${IA32_IHV_PKGS_ORIG}-nd
622	PKGARCHIVE=${PKGARCHIVE_ORIG}${SUFFIX}
623
624	echo "\n==== Installing IHV packages from $IA32_IHV_PKGS ($LABEL) ====\n" \
625		>> $LOGFILE
626	if [ -d "$IA32_IHV_PKGS" ]; then
627		cd $IA32_IHV_PKGS
628		tar -cf - * | \
629		   (cd $PKGARCHIVE; umask 0; tar xpf - ) 2>&1 >> $LOGFILE
630	else
631		echo "$IA32_IHV_PKGS: not found" >> $LOGFILE
632	fi
633
634	echo "\n==== Installing IHV packages from $IA32_IHV_BINARY_PKGS ($LABEL) ====\n" \
635		>> $LOGFILE
636	if [ -d "$IA32_IHV_BINARY_PKGS" ]; then
637		cd $IA32_IHV_BINARY_PKGS
638		tar -cf - * | \
639		    (cd $PKGARCHIVE; umask 0; tar xpf - ) 2>&1 >> $LOGFILE
640	else
641		echo "$IA32_IHV_BINARY_PKGS: not found" >> $LOGFILE
642	fi
643}
644
645# usage: build_tools DESTROOT
646build_tools() {
647	DESTROOT=$1
648
649	INSTALLOG=install-${MACH}
650
651	echo "\n==== Building tools at `date` ====\n" \
652		>> $LOGFILE
653
654	rm -f ${TOOLS}/${INSTALLOG}.out
655	cd ${TOOLS}
656	/bin/time $MAKE ROOT=${DESTROOT} -e install 2>&1 | \
657	    tee -a ${TOOLS}/${INSTALLOG}.out >> $LOGFILE
658
659	echo "\n==== Tools build errors ====\n" >> $mail_msg_file
660
661	egrep ":" ${TOOLS}/${INSTALLOG}.out |
662		egrep -e "(${MAKE}:|[ 	]error[: 	\n])" | \
663		egrep -v "Ignoring unknown host" | \
664		egrep -v warning >> $mail_msg_file
665	if [ "$?" != "0" ]; then
666		STABS=${DESTROOT}/opt/onbld/bin/${MACH}/stabs
667		export STABS
668		CTFSTABS=${DESTROOT}/opt/onbld/bin/${MACH}/ctfstabs
669		export CTFSTABS
670		GENOFFSETS=${DESTROOT}/opt/onbld/bin/genoffsets
671		export GENOFFSETS
672
673		CTFCONVERT=${DESTROOT}/opt/onbld/bin/${MACH}/ctfconvert
674		export CTFCONVERT
675		CTFMERGE=${DESTROOT}/opt/onbld/bin/${MACH}/ctfmerge
676		export CTFMERGE
677
678		CTFCVTPTBL=${DESTROOT}/opt/onbld/bin/ctfcvtptbl
679		export CTFCVTPTBL
680		CTFFINDMOD=${DESTROOT}/opt/onbld/bin/ctffindmod
681		export CTFFINDMOD
682
683		if [ "$VERIFY_ELFSIGN" = "y" ]; then
684			ELFSIGN=${DESTROOT}/opt/onbld/bin/elfsigncmp
685		else
686			ELFSIGN=${DESTROOT}/opt/onbld/bin/${MACH}/elfsign
687		fi
688		export ELFSIGN
689
690		PATH="${DESTROOT}/opt/onbld/bin/${MACH}:${PATH}"
691		PATH="${DESTROOT}/opt/onbld/bin:${PATH}"
692		export PATH
693
694		echo "\n==== New environment settings. ====\n" >> $LOGFILE
695		echo "STABS=${STABS}" >> $LOGFILE
696		echo "CTFSTABS=${CTFSTABS}" >> $LOGFILE
697		echo "CTFCONVERT=${CTFCONVERT}" >> $LOGFILE
698		echo "CTFMERGE=${CTFMERGE}" >> $LOGFILE
699		echo "CTFCVTPTBL=${CTFCVTPTBL}" >> $LOGFILE
700		echo "CTFFINDMOD=${CTFFINDMOD}" >> $LOGFILE
701		echo "ELFSIGN=${ELFSIGN}" >> $LOGFILE
702		echo "PATH=${PATH}" >> $LOGFILE
703	fi
704}
705
706staffer() {
707	if [ $ISUSER -ne 0 ]; then
708		"$@"
709	else
710		arg="\"$1\""
711		shift
712		for i
713		do
714			arg="$arg \"$i\""
715		done
716		eval su $STAFFER -c \'$arg\'
717	fi
718}
719
720#
721# Verify that the closed tree is present if it needs to be.
722# Sets CLOSED_IS_PRESENT for future use.
723#
724check_closed_tree() {
725	if [ -z "$CLOSED_IS_PRESENT" ]; then
726		if [ -d $SRC/../closed ]; then
727			CLOSED_IS_PRESENT="yes"
728		else
729			CLOSED_IS_PRESENT="no"
730		fi
731		export CLOSED_IS_PRESENT
732	fi
733	if [[ "$CLOSED_IS_PRESENT" = no && ! -d "$ON_CLOSED_BINS" ]]; then
734		#
735		# If it's an old (pre-split) tree or an empty
736		# workspace, don't complain.
737		#
738		if grep -s CLOSED_BUILD $SRC/Makefile.master > /dev/null; then
739			echo "If the closed sources are not present," \
740			    "ON_CLOSED_BINS"
741			echo "must point to the closed binaries tree."
742			exit 1
743		fi
744	fi
745}
746
747obsolete_build() {
748    	echo "WARNING: Obsolete $1 build requested; request will be ignored"
749}
750
751
752MACH=`uname -p`
753
754if [ "$OPTHOME" = "" ]; then
755	OPTHOME=/opt
756	export OPTHOME
757fi
758if [ "$TEAMWARE" = "" ]; then
759	TEAMWARE=$OPTHOME/teamware
760	export TEAMWARE
761fi
762
763USAGE='Usage: nightly [-in] [-V VERS ] [ -S E|D|H|O ] <env_file>
764
765Where:
766	-i	Fast incremental options (no clobber, lint, check)
767	-n      Do not do a bringover
768	-V VERS set the build version string to VERS
769	-S	Build a variant of the source product
770		E - build exportable source
771		D - build domestic source (exportable + crypt)
772		H - build hybrid source (binaries + deleted source)
773		O - build (only) open source
774
775	<env_file>  file in Bourne shell syntax that sets and exports
776	variables that configure the operation of this script and many of
777	the scripts this one calls. If <env_file> does not exist,
778	it will be looked for in $OPTHOME/onbld/env.
779
780non-DEBUG is the default build type. Build options can be set in the
781NIGHTLY_OPTIONS variable in the <env_file> as follows:
782
783	-A	check for ABI differences in .so files
784	-C	check for cstyle/hdrchk errors
785	-D	do a build with DEBUG on
786	-F	do _not_ do a non-DEBUG build
787	-G	gate keeper default group of options (-au)
788	-I	integration engineer default group of options (-ampu)
789	-M	do not run pmodes (safe file permission checker)
790	-N	do not run protocmp
791	-R	default group of options for building a release (-mp)
792	-U	update proto area in the parent
793	-V VERS set the build version string to VERS
794	-X	copy x86 IHV proto area
795	-a	create cpio archives
796	-f	find unreferenced files
797	-i	do an incremental build (no "make clobber")
798	-l	do "make lint" in $LINTDIRS (default: $SRC y)
799	-m	send mail to $MAILTO at end of build
800	-n      do not do a bringover
801	-o	build using root privileges to set OWNER/GROUP (old style)
802	-p	create packages
803	-r	check ELF runtime attributes in the proto area
804	-t	build and use the tools in $SRC/tools
805	-u	update proto_list_$MACH and friends in the parent workspace;
806		when used with -f, also build an unrefmaster.out in the parent
807	-w	report on differences between previous and current proto areas
808	-z	compress cpio archives with gzip
809	-W	Do not report warnings (freeware gate ONLY)
810	-S	Build a variant of the source product
811		E - build exportable source
812		D - build domestic source (exportable + crypt)
813		H - build hybrid source (binaries + deleted source)
814		O - build (only) open source
815'
816#
817#	-x	less public handling of xmod source for the source product
818#
819#	A log file will be generated under the name $LOGFILE
820#	for partially completed build and log.`date '+%F'`
821#	in the same directory for fully completed builds.
822#
823
824# default values for low-level FLAGS; G I R are group FLAGS
825A_FLAG=n
826a_FLAG=n
827C_FLAG=n
828F_FLAG=n
829f_FLAG=n
830D_FLAG=n
831P_FLAG=n
832T_FLAG=n
833n_FLAG=n
834o_FLAG=n
835i_FLAG=n; i_CMD_LINE_FLAG=n
836l_FLAG=n
837m_FLAG=n
838p_FLAG=n
839r_FLAG=n
840t_FLAG=n
841u_FLAG=n
842U_FLAG=n
843V_FLAG=n
844M_FLAG=n
845N_FLAG=n
846z_FLAG=n
847w_FLAG=n
848W_FLAG=n
849SE_FLAG=n
850SD_FLAG=n
851SH_FLAG=n
852SO_FLAG=n
853X_FLAG=n
854#
855XMOD_OPT=
856#
857build_ok=y
858
859is_source_build() {
860	[ "$SE_FLAG" = "y" -o "$SD_FLAG" = "y" -o \
861	    "$SH_FLAG" = "y" -o "$SO_FLAG" = "y" ]
862	return $?
863}
864
865#
866# examine arguments
867#
868
869#
870# single function for setting -S flag and doing error checking.
871# usage: set_S_flag <type>
872# where <type> is the source build type ("E", "D", ...).
873#
874set_S_flag() {
875	if is_source_build; then
876		echo "Can only build one source variant at a time."
877		exit 1
878	fi
879	if [ "$1" = "E" ]; then
880		SE_FLAG=y
881	elif [ "$1" = "D" ]; then
882		SD_FLAG=y
883	elif [ "$1" = "H" ]; then
884		SH_FLAG=y
885	elif [ "$1" = "O" ]; then
886		SO_FLAG=y
887	else
888		echo "$USAGE"
889		exit 1
890	fi
891}
892
893OPTIND=1
894while getopts inV:S:t FLAG
895do
896	case $FLAG in
897	  i )	i_FLAG=y; i_CMD_LINE_FLAG=y
898		;;
899	  n )	n_FLAG=y
900		;;
901	  V )	V_FLAG=y
902		V_ARG="$OPTARG"
903		;;
904	  S )
905		set_S_flag $OPTARG
906		;;
907	  t )	t_FLAG=y
908		;;
909	 \? )	echo "$USAGE"
910		exit 1
911		;;
912	esac
913done
914
915# correct argument count after options
916shift `expr $OPTIND - 1`
917
918# test that the path to the environment-setting file was given
919if [ $# -ne 1 ]; then
920	echo "$USAGE"
921	exit 1
922fi
923
924# check if user is running nightly as root
925# ISUSER is set non-zero if an ordinary user runs nightly, or is zero
926# when root invokes nightly.
927/usr/bin/id | grep '^uid=0(' >/dev/null 2>&1
928ISUSER=$?;	export ISUSER
929
930#
931# force locale to C
932LC_COLLATE=C;	export LC_COLLATE
933LC_CTYPE=C;	export LC_CTYPE
934LC_MESSAGES=C;	export LC_MESSAGES
935LC_MONETARY=C;	export LC_MONETARY
936LC_NUMERIC=C;	export LC_NUMERIC
937LC_TIME=C;	export LC_TIME
938
939# clear environment variables we know to be bad for the build
940unset LD_OPTIONS
941unset LD_AUDIT		LD_AUDIT_32		LD_AUDIT_64
942unset LD_BIND_NOW	LD_BIND_NOW_32		LD_BIND_NOW_64
943unset LD_BREADTH	LD_BREADTH_32		LD_BREADTH_64
944unset LD_CONFIG		LD_CONFIG_32		LD_CONFIG_64
945unset LD_DEBUG		LD_DEBUG_32		LD_DEBUG_64
946unset LD_DEMANGLE	LD_DEMANGLE_32		LD_DEMANGLE_64
947unset LD_FLAGS		LD_FLAGS_32		LD_FLAGS_64
948unset LD_LIBRARY_PATH	LD_LIBRARY_PATH_32	LD_LIBRARY_PATH_64
949unset LD_LOADFLTR	LD_LOADFLTR_32		LD_LOADFLTR_64
950unset LD_NOAUDIT	LD_NOAUDIT_32		LD_NOAUDIT_64
951unset LD_NOAUXFLTR	LD_NOAUXFLTR_32		LD_NOAUXFLTR_64
952unset LD_NOCONFIG	LD_NOCONFIG_32		LD_NOCONFIG_64
953unset LD_NODIRCONFIG	LD_NODIRCONFIG_32	LD_NODIRCONFIG_64
954unset LD_NODIRECT	LD_NODIRECT_32		LD_NODIRECT_64
955unset LD_NOLAZYLOAD	LD_NOLAZYLOAD_32	LD_NOLAZYLOAD_64
956unset LD_NOOBJALTER	LD_NOOBJALTER_32	LD_NOOBJALTER_64
957unset LD_NOVERSION	LD_NOVERSION_32		LD_NOVERSION_64
958unset LD_ORIGIN		LD_ORIGIN_32		LD_ORIGIN_64
959unset LD_PRELOAD	LD_PRELOAD_32		LD_PRELOAD_64
960unset LD_PROFILE	LD_PROFILE_32		LD_PROFILE_64
961
962unset CONFIG
963unset GROUP
964unset OWNER
965unset REMOTE
966unset ENV
967unset ARCH
968unset CLASSPATH
969unset NAME
970
971#
972#	Setup environmental variables
973#
974if [ -f $1 ]; then
975	if [[ $1 = */* ]]; then
976		. $1
977	else
978		. ./$1
979	fi
980else
981	if [ -f $OPTHOME/onbld/env/$1 ]; then
982		. $OPTHOME/onbld/env/$1
983	else
984		echo "Cannot find env file as either $1 or $OPTHOME/onbld/env/$1"
985		exit 1
986	fi
987fi
988
989# contents of stdenv.sh inserted after next line:
990# STDENV_START
991# STDENV_END
992
993#
994# place ourselves in a new task, respecting BUILD_PROJECT if set.
995#
996if [ -z "$BUILD_PROJECT" ]; then
997	/usr/bin/newtask -c $$
998else
999	/usr/bin/newtask -c $$ -p $BUILD_PROJECT
1000fi
1001
1002ps -o taskid= -p $$ | read build_taskid
1003ps -o project= -p $$ | read build_project
1004
1005#
1006# See if NIGHTLY_OPTIONS is set
1007#
1008if [ "$NIGHTLY_OPTIONS" = "" ]; then
1009	NIGHTLY_OPTIONS="-aBm"
1010fi
1011
1012#
1013# If BRINGOVER_WS was not specified, let it default to CLONE_WS
1014#
1015if [ "$BRINGOVER_WS" = "" ]; then
1016	BRINGOVER_WS=$CLONE_WS
1017fi
1018
1019#
1020# If BRINGOVER_FILES was not specified, default to usr
1021#
1022if [ "$BRINGOVER_FILES" = "" ]; then
1023	BRINGOVER_FILES="usr"
1024fi
1025
1026#
1027# If the closed sources are not present, the closed binaries must be
1028# present for the build to succeed.  If there's no pointer to the
1029# closed binaries, flag that now, rather than forcing the user to wait
1030# a couple hours (or more) to find out.
1031#
1032orig_closed_is_present="$CLOSED_IS_PRESENT"
1033check_closed_tree
1034
1035#
1036# Note: changes to the option letters here should also be applied to the
1037#	bldenv script.  `d' is listed for backward compatibility.
1038#
1039NIGHTLY_OPTIONS=-${NIGHTLY_OPTIONS#-}
1040OPTIND=1
1041while getopts ABDFNMPTCGIRafinlmoptuUxdrtwzWS:X FLAG $NIGHTLY_OPTIONS
1042do
1043	case $FLAG in
1044	  A )	A_FLAG=y
1045		;;
1046	  B )	D_FLAG=y
1047		;; # old version of D
1048	  F )	F_FLAG=y
1049		;;
1050	  D )	D_FLAG=y
1051		;;
1052	  P )	P_FLAG=y
1053		;; # obsolete
1054	  T )	T_FLAG=y
1055		;; # obsolete
1056	  C )	C_FLAG=y
1057		;;
1058	  M )	M_FLAG=y
1059		;;
1060	  N )	N_FLAG=y
1061		;;
1062	  G )	a_FLAG=y
1063		u_FLAG=y
1064		;;
1065	  I )	a_FLAG=y
1066		m_FLAG=y
1067		p_FLAG=y
1068		u_FLAG=y
1069		;;
1070	  R )	m_FLAG=y
1071		p_FLAG=y
1072		;;
1073	  a )	a_FLAG=y
1074		;;
1075	  f )	f_FLAG=y
1076		;;
1077	  i )	i_FLAG=y
1078		;;
1079	  n )	n_FLAG=y
1080		;;
1081	  o )	o_FLAG=y
1082		;;
1083	  l )	l_FLAG=y
1084		;;
1085	  m )	m_FLAG=y
1086		;;
1087	  p )	p_FLAG=y
1088		;;
1089	  r )	r_FLAG=y
1090		;;
1091	  t )	t_FLAG=y
1092		;;
1093	  u )	u_FLAG=y
1094		;;
1095	  w )	w_FLAG=y
1096		;;
1097	  z )	z_FLAG=y
1098		;;
1099	  U )
1100		if [ -z "${PARENT_ROOT}" ]; then
1101			echo "PARENT_ROOT must be set if the U flag is" \
1102			    "present in NIGHTLY_OPTIONS."
1103			exit 1
1104		fi
1105		U_FLAG=y
1106		NIGHTLY_PARENT_ROOT=$PARENT_ROOT
1107		;;
1108	  x )	XMOD_OPT="-x"
1109		;;
1110	  W )	W_FLAG=y
1111		;;
1112	  S )
1113		set_S_flag $OPTARG
1114		;;
1115	  X )	# now that we no longer need realmode builds, just
1116		# copy IHV packages.  only meaningful on x86.
1117		if [ "$MACH" = "i386" ]; then
1118			X_FLAG=y
1119		fi
1120		;;
1121	 \? )	echo "$USAGE"
1122		exit 1
1123		;;
1124	esac
1125done
1126
1127if [ $ISUSER -ne 0 ]; then
1128	if [ "$o_FLAG" = "y" ]; then
1129		echo "Old-style build requires root permission."
1130		exit 1
1131	fi
1132
1133	# Set default value for STAFFER, if needed.
1134	if [ -z "$STAFFER" -o "$STAFFER" = "nobody" ]; then
1135		STAFFER=`/usr/xpg4/bin/id -un`
1136		export STAFFER
1137	fi
1138fi
1139
1140if [ -z "$MAILTO" -o "$MAILTO" = "nobody" ]; then
1141	MAILTO=$STAFFER
1142	export MAILTO
1143fi
1144
1145PATH="$OPTHOME/onbld/bin:$OPTHOME/onbld/bin/${MACH}:/usr/ccs/bin"
1146PATH="$PATH:$OPTHOME/SUNWspro/bin:$TEAMWARE/bin:/usr/bin:/usr/sbin:/usr/ucb"
1147PATH="$PATH:/usr/openwin/bin:/usr/sfw/bin:/opt/sfw/bin:."
1148export PATH
1149
1150# roots of source trees, both relative to $SRC and absolute.
1151relsrcdirs="."
1152if [[ -d $SRC/../closed && "$CLOSED_IS_PRESENT" != no ]]; then
1153	relsrcdirs="$relsrcdirs ../closed"
1154fi
1155abssrcdirs=""
1156for d in $relsrcdirs; do
1157	abssrcdirs="$abssrcdirs $SRC/$d"
1158done
1159
1160unset CH
1161if [ "$o_FLAG" = "y" ]; then
1162# root invoked old-style build -- make sure it works as it always has
1163# by exporting 'CH'.  The current Makefile.master doesn't use this, but
1164# the old ones still do.
1165	PROTOCMPTERSE="protocmp.terse"
1166	CH=
1167	export CH
1168else
1169	PROTOCMPTERSE="protocmp.terse -gu"
1170fi
1171POUND_SIGN="#"
1172
1173# we export POUND_SIGN to speed up the build process -- prevents evaluation of
1174# the Makefile.master definitions.
1175export o_FLAG X_FLAG POUND_SIGN
1176
1177maketype="distributed"
1178MAKE=dmake
1179# get the dmake version string alone
1180DMAKE_VERSION=$( $MAKE -v )
1181DMAKE_VERSION=${DMAKE_VERSION#*: }
1182# focus in on just the dotted version number alone
1183DMAKE_MAJOR=$( echo $DMAKE_VERSION | \
1184	sed -e 's/.*\<\([^.]*\.[^   ]*\).*$/\1/' )
1185# extract the second (or final) integer
1186DMAKE_MINOR=${DMAKE_MAJOR#*.}
1187DMAKE_MINOR=${DMAKE_MINOR%%.*}
1188# extract the first integer
1189DMAKE_MAJOR=${DMAKE_MAJOR%%.*}
1190CHECK_DMAKE=${CHECK_DMAKE:-y}
1191# x86 was built on the 12th, sparc on the 13th.
1192if [ "$CHECK_DMAKE" = "y" -a \
1193     "$DMAKE_VERSION" != "Sun Distributed Make 7.3 2003/03/12" -a \
1194     "$DMAKE_VERSION" != "Sun Distributed Make 7.3 2003/03/13" -a \( \
1195     "$DMAKE_MAJOR" -lt 7 -o \
1196     "$DMAKE_MAJOR" -eq 7 -a "$DMAKE_MINOR" -lt 4 \) ]; then
1197	if [ -z "$DMAKE_VERSION" ]; then
1198		echo "$MAKE is missing."
1199		exit 1
1200	fi
1201	echo `whence $MAKE`" version is:"
1202	echo "  ${DMAKE_VERSION}"
1203	cat <<EOF
1204
1205This version may not be safe for use.  Either set TEAMWARE to a better
1206path or (if you really want to use this version of dmake anyway), add
1207the following to your environment to disable this check:
1208
1209  CHECK_DMAKE=n
1210EOF
1211	exit 1
1212fi
1213export PATH
1214export MAKE
1215
1216if [ "${SUNWSPRO}" != "" ]; then
1217	PATH="${SUNWSPRO}/bin:$PATH"
1218	export PATH
1219fi
1220
1221hostname=`uname -n`
1222if [ ! -f $HOME/.make.machines ]; then
1223	DMAKE_MAX_JOBS=4
1224else
1225	DMAKE_MAX_JOBS="`grep $hostname $HOME/.make.machines | \
1226	    tail -1 | awk -F= '{print $ 2;}'`"
1227	if [ "$DMAKE_MAX_JOBS" = "" ]; then
1228		DMAKE_MAX_JOBS=4
1229	fi
1230fi
1231DMAKE_MODE=parallel;
1232export DMAKE_MODE
1233export DMAKE_MAX_JOBS
1234
1235if [ -z "${ROOT}" ]; then
1236	echo "ROOT must be set."
1237	exit 1
1238fi
1239
1240#
1241# if -V flag was given, reset VERSION to V_ARG
1242#
1243if [ "$V_FLAG" = "y" ]; then
1244	VERSION=$V_ARG
1245fi
1246
1247#
1248# Check for IHV root for copying ihv proto area
1249#
1250if [ "$X_FLAG" = "y" ]; then
1251        if [ "$IA32_IHV_ROOT" = "" ]; then
1252		echo "IA32_IHV_ROOT: must be set for copying ihv proto"
1253		args_ok=n
1254        fi
1255        if [ ! -d "$IA32_IHV_ROOT" ]; then
1256                echo "$IA32_IHV_ROOT: not found"
1257                args_ok=n
1258        fi
1259        if [ "$IA32_IHV_WS" = "" ]; then
1260		echo "IA32_IHV_WS: must be set for copying ihv proto"
1261		args_ok=n
1262        fi
1263        if [ ! -d "$IA32_IHV_WS" ]; then
1264                echo "$IA32_IHV_WS: not found"
1265                args_ok=n
1266        fi
1267fi
1268
1269# Append source version
1270if [ "$SE_FLAG" = "y" ]; then
1271	VERSION="${VERSION}:EXPORT"
1272fi
1273
1274if [ "$SD_FLAG" = "y" ]; then
1275	VERSION="${VERSION}:DOMESTIC"
1276fi
1277
1278if [ "$SH_FLAG" = "y" ]; then
1279	VERSION="${VERSION}:MODIFIED_SOURCE_PRODUCT"
1280fi
1281
1282if [ "$SO_FLAG" = "y" ]; then
1283	VERSION="${VERSION}:OPEN_ONLY"
1284fi
1285
1286TMPDIR="/tmp/nightly.tmpdir.$$"
1287export TMPDIR
1288rm -rf ${TMPDIR}
1289mkdir -p $TMPDIR || exit 1
1290
1291#
1292# Keep elfsign's use of pkcs11_softtoken from looking in the user home
1293# directory, which doesn't always work.   Needed until all build machines
1294# have the fix for 6271754
1295#
1296SOFTTOKEN_DIR=$TMPDIR
1297export SOFTTOKEN_DIR
1298
1299TOOLS=${SRC}/tools
1300TOOLS_PROTO=${TOOLS}/proto
1301
1302unset   CFLAGS LD_LIBRARY_PATH LDFLAGS
1303
1304# create directories that are automatically removed if the nightly script
1305# fails to start correctly
1306newdir() {
1307	dir=$1
1308	toadd=
1309	while [ ! -d $dir ]; do
1310		toadd="$dir $toadd"
1311		dir=`dirname $dir`
1312	done
1313	torm=
1314	newlist=
1315	for dir in $toadd; do
1316		if staffer mkdir $dir; then
1317			newlist="$ISUSER $dir $newlist"
1318			torm="$dir $torm"
1319		else
1320			[ -z "$torm" ] || staffer rmdir $torm
1321			return 1
1322		fi
1323	done
1324	newdirlist="$newlist $newdirlist"
1325	return 0
1326}
1327newdirlist=
1328
1329[ -d $CODEMGR_WS ] || newdir $CODEMGR_WS || exit 1
1330
1331# since this script assumes the build is from full source, it nullifies
1332# variables likely to have been set by a "ws" script; nullification
1333# confines the search space for headers and libraries to the proto area
1334# built from this immediate source.
1335ENVLDLIBS1=
1336ENVLDLIBS2=
1337ENVLDLIBS3=
1338ENVCPPFLAGS1=
1339ENVCPPFLAGS2=
1340ENVCPPFLAGS3=
1341ENVCPPFLAGS4=
1342PARENT_ROOT=
1343
1344export ENVLDLIBS3 ENVCPPFLAGS1 ENVCPPFLAGS2 ENVCPPFLAGS3 ENVCPPFLAGS4 \
1345	PARENT_ROOT
1346
1347ENVLDLIBS1="-L$ROOT/lib -L$ROOT/usr/lib"
1348ENVCPPFLAGS1="-I$ROOT/usr/include"
1349
1350export ENVLDLIBS1 ENVLDLIBS2
1351
1352CPIODIR_ORIG=$CPIODIR
1353PKGARCHIVE_ORIG=$PKGARCHIVE
1354IA32_IHV_PKGS_ORIG=$IA32_IHV_PKGS
1355if [ "$SPARC_RM_PKGARCHIVE" ]; then
1356	SPARC_RM_PKGARCHIVE_ORIG=$SPARC_RM_PKGARCHIVE
1357fi
1358
1359#
1360# Juggle the logs and optionally send mail on completion.
1361#
1362
1363logshuffle() {
1364    	LLOG="$ATLOG/log.`date '+%F'`"
1365	rm -rf $ATLOG/log.??`date '+%d'`
1366	rm -rf $ATLOG/log.????-??-`date '+%d'`
1367	if [ -f $LLOG -o -d $LLOG ]; then
1368	    	LLOG=$LLOG.$$
1369	fi
1370	mkdir $LLOG
1371	export LLOG
1372
1373	if [ "$build_ok" = "y" ]; then
1374		mv $ATLOG/proto_list_${MACH} $LLOG
1375
1376		if [ -f $TMPDIR/wsdiff.results ]; then
1377		    mv $TMPDIR/wsdiff.results $LLOG
1378		fi
1379	fi
1380
1381	#
1382	# Now that we're about to send mail, it's time to check the noise
1383	# file.  In the event that an error occurs beyond this point, it will
1384	# be recorded in the nightly.log file, but nowhere else.  This would
1385	# include only errors that cause the copying of the noise log to fail
1386	# or the mail itself not to be sent.
1387	#
1388
1389	exec >>$LOGFILE 2>&1
1390	if [ -s $build_noise_file ]; then
1391	    	echo "\n==== Nightly build noise ====\n" |
1392		    tee -a $LOGFILE >>$mail_msg_file
1393		cat $build_noise_file >>$LOGFILE
1394		cat $build_noise_file >>$mail_msg_file
1395		echo | tee -a $LOGFILE >>$mail_msg_file
1396	fi
1397	rm -f $build_noise_file
1398
1399	case "$build_ok" in
1400		y)
1401			state=Completed
1402			;;
1403		i)
1404			state=Interrupted
1405			;;
1406		*)
1407	    		state=Failed
1408			;;
1409	esac
1410	NIGHTLY_STATUS=$state
1411	export NIGHTLY_STATUS
1412
1413	if [ -n "$POST_NIGHTLY" ]; then
1414		echo "\n==== Running POST_NIGHTLY command:" \
1415		    "$POST_NIGHTLY ====\n" | tee -a $mail_msg_file >> $LOGFILE
1416		$POST_NIGHTLY $state 2>&1 | tee -a $mail_msg_file >> $LOGFILE
1417	fi
1418
1419	cat $build_time_file $mail_msg_file > ${LLOG}/mail_msg
1420	if [ "$m_FLAG" = "y" ]; then
1421	    	cat $build_time_file $mail_msg_file |
1422		    /usr/bin/mailx -s \
1423	"Nightly ${MACH} Build of `basename ${CODEMGR_WS}` ${state}." \
1424			${MAILTO}
1425	fi
1426
1427	if [ "$u_FLAG" = "y" -a "$build_ok" = "y" ]; then
1428	    	staffer cp ${LLOG}/mail_msg $PARENT_WS/usr/src/mail_msg-${MACH}
1429		staffer cp $LOGFILE $PARENT_WS/usr/src/nightly-${MACH}.log
1430	fi
1431
1432	mv $LOGFILE $LLOG
1433}
1434
1435#
1436#	Remove the locks and temporary files on any exit
1437#
1438cleanup() {
1439    	logshuffle
1440
1441	[ -z "$lockfile" ] || staffer rm -f $lockfile
1442	[ -z "$atloglockfile" ] || rm -f $atloglockfile
1443	[ -z "$ulockfile" ] || staffer rm -f $ulockfile
1444	[ -z "$Ulockfile" ] || rm -f $Ulockfile
1445
1446	set -- $newdirlist
1447	while [ $# -gt 0 ]; do
1448		ISUSER=$1 staffer rmdir $2
1449		shift; shift
1450	done
1451	rm -rf $TMPDIR
1452}
1453
1454cleanup_signal() {
1455    	build_ok=i
1456	# this will trigger cleanup(), above.
1457	exit 1
1458}
1459
1460trap cleanup 0
1461trap cleanup_signal 1 2 3 15
1462
1463#
1464# Generic lock file processing -- make sure that the lock file doesn't
1465# exist.  If it does, it should name the build host and PID.  If it
1466# doesn't, then make sure we can create it.  Clean up locks that are
1467# known to be stale (assumes host name is unique among build systems
1468# for the workspace).
1469create_lock() {
1470	lockf=$1
1471	lockvar=$2
1472	if [ -f $lockf ]; then
1473		basews=`basename $CODEMGR_WS`
1474		if read host user pid < $lockf; then
1475			if [ "$host" != "$hostname" ]; then
1476				echo "$MACH build of $basews apparently" \
1477				    "already started by $user on $host as $pid."
1478			elif kill -s 0 $pid 2>/dev/null; then
1479				echo "$MACH build of $basews already started" \
1480				    "by $user as $pid."
1481			else
1482				# stale lock; clear it out and continue
1483				rm -f $lockf
1484			fi
1485		else
1486			echo "$MACH build of $basews already running."
1487		fi
1488	fi
1489	if [ -f $lockf ]; then
1490		echo "Lock file is $lockf."
1491		exit 1
1492	fi
1493	ldir=`dirname $lockf`
1494	[ -d $ldir ] || newdir $ldir || exit 1
1495	eval $lockvar=$lockf
1496	staffer sh -c "echo $hostname $STAFFER $$ > $lockf" || exit 1
1497}
1498
1499# Ensure no other instance of this script is running on this host.
1500# LOCKNAME can be set in <env_file>, and is by default, but is not
1501# required due to the use of $ATLOG below.
1502if [ -n "$LOCKNAME" ]; then
1503	create_lock /tmp/$LOCKNAME "lockfile"
1504fi
1505#
1506# Create from one, two, or three other locks:
1507#	$ATLOG/nightly.lock
1508#		- protects against multiple builds in same workspace
1509#	$PARENT_WS/usr/src/nightly.$MACH.lock
1510#		- protects against multiple 'u' copy-backs
1511#	$NIGHTLY_PARENT_ROOT/nightly.lock
1512#		- protects against multiple 'U' copy-backs
1513#
1514# Overriding ISUSER to 1 causes the lock to be created as root if the
1515# script is run as root.  The default is to create it as $STAFFER.
1516ISUSER=1 create_lock $ATLOG/nightly.lock "atloglockfile"
1517if [ "$u_FLAG" = "y" ]; then
1518	create_lock $PARENT_WS/usr/src/nightly.$MACH.lock "ulockfile"
1519fi
1520if [ "$U_FLAG" = "y" ]; then
1521	# NIGHTLY_PARENT_ROOT is written as root if script invoked as root.
1522	ISUSER=1 create_lock $NIGHTLY_PARENT_ROOT/nightly.lock "Ulockfile"
1523fi
1524
1525# Locks have been taken, so we're doing a build and we're committed to
1526# the directories we may have created so far.
1527newdirlist=
1528
1529#
1530# Create mail_msg_file
1531#
1532mail_msg_file="${TMPDIR}/mail_msg"
1533touch $mail_msg_file
1534build_time_file="${TMPDIR}/build_time"
1535#
1536#	Move old LOGFILE aside
1537#	ATLOG directory already made by 'create_lock' above
1538#
1539if [ -f $LOGFILE ]; then
1540	mv -f $LOGFILE ${LOGFILE}-
1541fi
1542#
1543#	Build OsNet source
1544#
1545START_DATE=`date`
1546SECONDS=0
1547echo "\n==== Nightly $maketype build started:   $START_DATE ====" \
1548    | tee -a $LOGFILE > $build_time_file
1549
1550# make sure we log only to the nightly build file
1551build_noise_file="${TMPDIR}/build_noise"
1552exec </dev/null >$build_noise_file 2>&1
1553
1554echo "\n==== list of environment variables ====\n" >> $LOGFILE
1555env >> $LOGFILE
1556
1557echo "\n==== Nightly argument issues ====\n" | tee -a $mail_msg_file >> $LOGFILE
1558
1559if [ "$P_FLAG" = "y" ]; then
1560	obsolete_build GPROF | tee -a $mail_msg_file >> $LOGFILE
1561fi
1562
1563if [ "$T_FLAG" = "y" ]; then
1564	obsolete_build TRACE | tee -a $mail_msg_file >> $LOGFILE
1565fi
1566
1567if is_source_build; then
1568	if [ "$i_FLAG" = "y" -o "$i_CMD_LINE_FLAG" = "y" ]; then
1569		echo "WARNING: the -S flags do not support incremental" \
1570		    "builds; forcing clobber\n" | tee -a $mail_msg_file >> $LOGFILE
1571		i_FLAG=n
1572		i_CMD_LINE_FLAG=n
1573	fi
1574	if [ "$N_FLAG" = "n" ]; then
1575		echo "WARNING: the -S flags do not support protocmp;" \
1576		    "protocmp disabled\n" | \
1577		    tee -a $mail_msg_file >> $LOGFILE
1578		N_FLAG=y
1579	fi
1580	if [ "$l_FLAG" = "y" ]; then
1581		echo "WARNING: the -S flags do not support lint;" \
1582		    "lint disabled\n" | tee -a $mail_msg_file >> $LOGFILE
1583		l_FLAG=n
1584	fi
1585	if [ "$C_FLAG" = "y" ]; then
1586		echo "WARNING: the -S flags do not support cstyle;" \
1587		    "cstyle check disabled\n" | tee -a $mail_msg_file >> $LOGFILE
1588		C_FLAG=n
1589	fi
1590else
1591	if [ "$N_FLAG" = "y" ]; then
1592		if [ "$p_FLAG" = "y" ]; then
1593			cat <<EOF | tee -a $mail_msg_file >> $LOGFILE
1594WARNING: the p option (create packages) is set, but so is the N option (do
1595         not run protocmp); this is dangerous; you should unset the N option
1596EOF
1597		else
1598			cat <<EOF | tee -a $mail_msg_file >> $LOGFILE
1599Warning: the N option (do not run protocmp) is set; it probably shouldn't be
1600EOF
1601		fi
1602		echo "" | tee -a $mail_msg_file >> $LOGFILE
1603	fi
1604fi
1605
1606if [ "$a_FLAG" = "y" -a "$D_FLAG" = "n" -a "$F_FLAG" = "y" ]; then
1607	echo "WARNING: Neither DEBUG nor non-DEBUG build requested, but the" \
1608	    "'a' option was set." | tee -a $mail_msg_file >> $LOGFILE
1609fi
1610
1611if [ "$D_FLAG" = "n" -a "$l_FLAG" = "y" ]; then
1612	echo "WARNING: DEBUG build not requested, but lint will be with" \
1613	    "DEBUG enabled.\n" \
1614	    | tee -a $mail_msg_file >> $LOGFILE
1615fi
1616
1617if [ "$f_FLAG" = "y" ]; then
1618	if [ "$i_FLAG" = "y" ]; then
1619		echo "WARNING: the -f flag cannot be used during incremental" \
1620		    "builds; ignoring -f\n" | tee -a $mail_msg_file >> $LOGFILE
1621		f_FLAG=n
1622	fi
1623	if [ "$p_FLAG" != "y" -o "$l_FLAG" != "y" ]; then
1624		echo "WARNING: the -f flag requires -l and -p; ignoring -f\n" | \
1625		    tee -a $mail_msg_file >> $LOGFILE
1626		f_FLAG=n
1627	fi
1628fi
1629
1630if [ "$w_FLAG" = "y" -a ! -d $ROOT ]; then
1631	echo "WARNING: -w specified, but no pre-existing proto area found;" \
1632	    "ignoring -w\n" | tee -a $mail_msg_file >> $LOGFILE
1633	w_FLAG=n
1634fi
1635
1636if [ "$t_FLAG" = "n" ]; then
1637	#
1638	# We're not doing a tools build, so make sure elfsign(1) is
1639	# new enough to safely sign non-crypto binaries.  We test
1640	# debugging output from elfsign to detect the old version.
1641	#
1642	newelfsigntest=`SUNW_CRYPTO_DEBUG=stderr /usr/bin/elfsign verify \
1643	    -e /usr/lib/security/pkcs11_softtoken.so.1 2>&1 \
1644	    | egrep algorithmOID`
1645	if [ -z "$newelfsigntest" ]; then
1646		echo "WARNING: /usr/bin/elfsign out of date;" \
1647		    "will only sign crypto modules\n" | \
1648		    tee -a $mail_msg_file >> $LOGFILE
1649		export ELFSIGN_OBJECT=true
1650	elif [ "$VERIFY_ELFSIGN" = "y" ]; then
1651		echo "WARNING: VERIFY_ELFSIGN=y requires" \
1652		    "the -t flag; ignoring VERIFY_ELFSIGN\n" | \
1653		    tee -a $mail_msg_file >> $LOGFILE
1654	fi
1655fi
1656
1657echo "==== Build environment ====\n" | tee -a $mail_msg_file >> $LOGFILE
1658
1659# System
1660whence uname | tee -a $mail_msg_file >> $LOGFILE
1661uname -a 2>&1 | tee -a $mail_msg_file >> $LOGFILE
1662echo | tee -a $mail_msg_file >> $LOGFILE
1663
1664# nightly (will fail in year 2100 due to SCCS flaw)
1665echo "$0 $@" | tee -a $mail_msg_file >> $LOGFILE
1666echo "%M% version %I% 20%E%\n" | tee -a $mail_msg_file >> $LOGFILE
1667
1668# make
1669whence $MAKE | tee -a $mail_msg_file >> $LOGFILE
1670$MAKE -v | tee -a $mail_msg_file >> $LOGFILE
1671echo "number of concurrent jobs = $DMAKE_MAX_JOBS" |
1672    tee -a $mail_msg_file >> $LOGFILE
1673
1674#
1675# Report the compiler versions.
1676#
1677if [ -f $SRC/Makefile ]; then
1678	srcroot=$SRC
1679elif [ -f $BRINGOVER_WS/usr/src/Makefile ]; then
1680	srcroot=$BRINGOVER_WS/usr/src
1681else
1682	echo "\nUnable to find \"Makefile\" in $BRINGOVER_WS/usr/src or $SRC." |
1683	    tee -a $mail_msg_file >> $LOGFILE
1684	exit 1
1685fi
1686
1687( cd $srcroot
1688  for target in cc-version cc64-version java-version; do
1689	echo
1690	#
1691	# Put statefile somewhere we know we can write to rather than trip
1692	# over a read-only $srcroot.
1693	#
1694	rm -f $TMPDIR/make-state
1695	export SRC=$srcroot
1696	if $MAKE -K $TMPDIR/make-state -e $target 2>/dev/null; then
1697		continue
1698	fi
1699	touch $TMPDIR/nocompiler
1700  done
1701  echo
1702) | tee -a $mail_msg_file >> $LOGFILE
1703
1704if [ -f $TMPDIR/nocompiler ]; then
1705	rm -f $TMPDIR/nocompiler
1706	build_ok=n
1707	echo "Aborting due to missing compiler." |
1708		tee -a $mail_msg_file >> $LOGFILE
1709	exit 1
1710fi
1711
1712# as
1713whence as | tee -a $mail_msg_file >> $LOGFILE
1714as -V 2>&1 | head -1 | tee -a $mail_msg_file >> $LOGFILE
1715echo | tee -a $mail_msg_file >> $LOGFILE
1716
1717# Check that we're running a capable link-editor
1718whence ld | tee -a $mail_msg_file >> $LOGFILE
1719LDVER=`ld -V 2>&1`
1720echo $LDVER | tee -a $mail_msg_file >> $LOGFILE
1721LDVER=`echo $LDVER | sed -e "s/.*-1\.//" -e "s/:.*//"`
1722if [ `expr $LDVER \< 422` -eq 1 ]; then
1723	echo "The link-editor needs to be at version 422 or higher to build" | \
1724	    tee -a $mail_msg_file >> $LOGFILE
1725	echo "the latest stuff, hope your build works." | \
1726	    tee -a $mail_msg_file >> $LOGFILE
1727fi
1728
1729echo "\nBuild project:  $build_project\nBuild taskid:   $build_taskid" | \
1730    tee -a $mail_msg_file >> $LOGFILE
1731
1732echo "\n==== Build version ====\n" | tee -a $mail_msg_file >> $LOGFILE
1733echo $VERSION | tee -a $mail_msg_file >> $LOGFILE
1734
1735# Save the current proto area if we're comparing against the last build
1736if [ "$w_FLAG" = "y" -a -d "$ROOT" ]; then
1737    if [ -d "$ROOT.prev" ]; then
1738	rm -rf $ROOT.prev
1739    fi
1740    mv $ROOT $ROOT.prev
1741fi
1742
1743#
1744#	Decide whether to clobber
1745#
1746if [ "$i_FLAG" = "n" -a -d "$SRC" ]; then
1747	echo "\n==== Make clobber at `date` ====\n" >> $LOGFILE
1748
1749	cd $SRC
1750	# remove old clobber file
1751	rm -f $SRC/clobber.out
1752	rm -f $SRC/clobber-${MACH}.out
1753
1754	# Remove all .make.state* files, just in case we are restarting
1755	# the build after having interrupted a previous 'make clobber'.
1756	find . \( -name SCCS -o -name 'interfaces.*' \) -prune \
1757	    -o -name '.make.*' -print | xargs rm -f
1758
1759	$MAKE -ek clobber 2>&1 | tee -a $SRC/clobber-${MACH}.out >> $LOGFILE
1760	echo "\n==== Make clobber ERRORS ====\n" >> $mail_msg_file
1761	grep "$MAKE:" $SRC/clobber-${MACH}.out |
1762		egrep -v "Ignoring unknown host" \
1763		>> $mail_msg_file
1764
1765	if [ "$t_FLAG" = "y" ]; then
1766		echo "\n==== Make tools clobber at `date` ====\n" >> $LOGFILE
1767		cd ${TOOLS}
1768		rm -f ${TOOLS}/clobber-${MACH}.out
1769		$MAKE -ek clobber 2>&1 | \
1770			tee -a ${TOOLS}/clobber-${MACH}.out >> $LOGFILE
1771		echo "\n==== Make tools clobber ERRORS ====\n" \
1772			>> $mail_msg_file
1773		grep "$MAKE:" ${TOOLS}/clobber-${MACH}.out \
1774			>> $mail_msg_file
1775		rm -rf ${TOOLS_PROTO}
1776		mkdir -p ${TOOLS_PROTO}
1777	fi
1778
1779	rm -rf $ROOT
1780
1781	# Get back to a clean workspace as much as possible to catch
1782	# problems that only occur on fresh workspaces.
1783	# Remove all .make.state* files, libraries, and .o's that may
1784	# have been omitted from clobber.  A couple of libraries are
1785	# under SCCS, so leave them alone.
1786	# We should probably blow away temporary directories too.
1787	cd $SRC
1788	find $relsrcdirs \( -name SCCS -o -name 'interfaces.*' \) -prune -o \
1789	    \( -name '.make.*' -o -name 'lib*.a' -o -name 'lib*.so*' -o \
1790	       -name '*.o' \) -print | \
1791	    grep -v 'tools/ctf/dwarf/.*/libdwarf' | xargs rm -f
1792else
1793	echo "\n==== No clobber at `date` ====\n" >> $LOGFILE
1794fi
1795
1796#
1797#	Decide whether to bringover to the codemgr workspace
1798#
1799if [ "$n_FLAG" = "n" ]; then
1800	echo "\n==== bringover to $CODEMGR_WS at `date` ====\n" >> $LOGFILE
1801	# sleep on the parent workspace's lock
1802	while egrep -s write $BRINGOVER_WS/Codemgr_wsdata/locks
1803	do
1804		sleep 120
1805	done
1806
1807	echo "\n==== BRINGOVER LOG ====\n" >> $mail_msg_file
1808
1809	(staffer $TEAMWARE/bin/bringover -c "nightly update" -p $BRINGOVER_WS \
1810	    -w $CODEMGR_WS $BRINGOVER_FILES < /dev/null 2>&1 ||
1811		touch $TMPDIR/bringover_failed
1812
1813         staffer bringovercheck $CODEMGR_WS >$TMPDIR/bringovercheck.out 2>&1
1814
1815	 if [ -s $TMPDIR/bringovercheck.out ]; then
1816		echo "\n==== POST-BRINGOVER CLEANUP NOISE ====\n"
1817		cat $TMPDIR/bringovercheck.out
1818	 fi
1819
1820	) | tee -a  $mail_msg_file >> $LOGFILE
1821
1822	if [ -f $TMPDIR/bringover_failed ]; then
1823		rm -f $TMPDIR/bringover_failed
1824		build_ok=n
1825		echo "trouble with bringover, quitting at `date`." |
1826			tee -a $mail_msg_file >> $LOGFILE
1827		exit 1
1828	fi
1829
1830	#
1831	# Possible transition from pre-split workspace to split
1832	# workspace.  See if the bringover changed anything.
1833	#
1834	CLOSED_IS_PRESENT="$orig_closed_is_present"
1835	check_closed_tree
1836else
1837	echo "\n==== No bringover to $CODEMGR_WS ====\n" >> $LOGFILE
1838fi
1839
1840#
1841# Build tools if requested
1842#
1843if [ "$t_FLAG" = "y" ]; then
1844	export INTERNAL_RELEASE_BUILD ; INTERNAL_RELEASE_BUILD=
1845	export RELEASE_BUILD ; RELEASE_BUILD=
1846	unset EXTRA_OPTIONS
1847	unset EXTRA_CFLAGS
1848
1849	export ONBLD_TOOLS=${ONBLD_TOOLS:=${TOOLS_PROTO}/opt/onbld}
1850	build_tools ${TOOLS_PROTO}
1851fi
1852
1853#
1854# copy ihv proto area in addition to the build itself
1855#
1856if [ "$X_FLAG" = "y" ]; then
1857	copy_ihv_proto
1858fi
1859
1860if [ "$i_FLAG" = "y" -a "$SH_FLAG" = "y" ]; then
1861	echo "\n==== NOT Building base OS-Net source ====\n" | \
1862	    tee -a $LOGFILE >> $mail_msg_file
1863else
1864	normal_build
1865fi
1866
1867ORIG_SRC=$SRC
1868BINARCHIVE=${CODEMGR_WS}/bin-${MACH}.cpio.Z
1869
1870#
1871# For the "open" build, we don't mung any source files, so skip this
1872# step.
1873#
1874if [ "$SE_FLAG" = "y" -o "$SD_FLAG" = "y" -o "$SH_FLAG" = "y" ]; then
1875	save_binaries
1876
1877	echo "\n==== Retrieving SCCS files at `date` ====\n" >> $LOGFILE
1878	SCCSHELPER=${TMPDIR}/sccs-helper
1879	rm -f ${SCCSHELPER}
1880cat >${SCCSHELPER} <<EOF
1881#!/bin/ksh
1882cd \$1
1883cd ..
1884sccs get SCCS >/dev/null 2>&1
1885EOF
1886	cd $SRC
1887	chmod +x ${SCCSHELPER}
1888	find $relsrcdirs -name SCCS | xargs -L 1 ${SCCSHELPER}
1889	rm -f ${SCCSHELPER}
1890fi
1891
1892if [ "$SD_FLAG" = "y" ]; then
1893	set_up_source_build ${CODEMGR_WS} ${CRYPT_SRC} CRYPT_SRC
1894fi
1895
1896# EXPORT_SRC comes after CRYPT_SRC since a domestic build will need
1897# $SRC pointing to the export_source usr/src.
1898if [ "$SE_FLAG" = "y" -o "$SD_FLAG" = "y" -o "$SH_FLAG" = "y" ]; then
1899	set_up_source_build ${CODEMGR_WS} ${EXPORT_SRC} EXPORT_SRC
1900fi
1901
1902if [ "$SD_FLAG" = "y" ]; then
1903	# drop the crypt files in place.
1904	cd ${EXPORT_SRC}
1905	echo "\nextracting crypt_files.cpio.Z onto export_source.\n" \
1906	    >> ${LOGFILE}
1907	zcat ${CODEMGR_WS}/crypt_files.cpio.Z | \
1908	    cpio -idmucvB 2>/dev/null >> ${LOGFILE}
1909	if [ "$?" = "0" ]; then
1910		echo "\n==== DOMESTIC extraction succeeded ====\n" \
1911		    >> $mail_msg_file
1912	else
1913		echo "\n==== DOMESTIC extraction failed ====\n" \
1914		    >> $mail_msg_file
1915	fi
1916
1917fi
1918
1919if [ "$SO_FLAG" = "y" ]; then
1920	#
1921	# Copy the open sources into their own tree, set up the closed
1922	# binaries, and set up the environment.
1923	#
1924	copy_source $CODEMGR_WS $OPEN_SRCDIR OPEN_SOURCE usr/src
1925	SRC=$OPEN_SRCDIR/usr/src
1926
1927	# Try not to clobber any user-provided closed binaries.
1928	export ON_CLOSED_BINS=$CODEMGR_WS/closed$$
1929	echo "\n==== Copying skeleton closed binaries to $ON_CLOSED_BINS ====\n" | \
1930	    tee -a $mail_msg_file >> $LOGFILE
1931	mkclosed $MACH $CODEMGR_WS/proto $ON_CLOSED_BINS >>$LOGFILE 2>&1
1932	if [ $? -ne 0 ]; then
1933		build_ok=n
1934		echo "Aborting (couldn't create closed binaries)." |
1935			tee -a $mail_msg_file >> $LOGFILE
1936	fi
1937	CLOSED_IS_PRESENT=no
1938fi
1939
1940if is_source_build && [ $build_ok = y ] ; then
1941	# remove proto area here, since we don't clobber
1942	rm -rf "$ROOT"
1943	if [ "$t_FLAG" = "y" ]; then
1944		export INTERNAL_RELEASE_BUILD ; INTERNAL_RELEASE_BUILD=
1945		export RELEASE_BUILD ; RELEASE_BUILD=
1946		unset EXTRA_OPTIONS
1947		unset EXTRA_CFLAGS
1948		ORIG_TOOLS=$TOOLS
1949		#
1950		# SRC was set earlier to point to the source build
1951		# source tree (e.g., $EXPORT_SRC).
1952		#
1953		TOOLS=${SRC}/tools
1954		build_tools ${SRC}/tools/proto
1955		TOOLS=$ORIG_TOOLS
1956	fi
1957
1958	export EXPORT_RELEASE_BUILD ; EXPORT_RELEASE_BUILD=#
1959	normal_build
1960fi
1961
1962if [[ "$SO_FLAG" = "y" && "$build_ok" = "y" ]]; then
1963	rm -rf $ON_CLOSED_BINS
1964fi
1965
1966if [ "$build_ok" = "y" ]; then
1967	echo "\n==== Creating protolist system file at `date` ====" \
1968		>> $LOGFILE
1969	protolist $ROOT > $ATLOG/proto_list_${MACH}
1970	echo "==== protolist system file created at `date` ====\n" \
1971		>> $LOGFILE
1972
1973	if [ "$N_FLAG" != "y" ]; then
1974		echo "\n==== Impact on packages ====\n" >> $mail_msg_file
1975
1976		# If there is a reference proto list, compare the build's proto
1977		# list with the reference to see changes in proto areas.
1978		# Use the current exception list.
1979		exc=etc/exception_list_$MACH
1980		if [ -f $SRC/pkgdefs/$exc ]; then
1981			ELIST="-e $SRC/pkgdefs/$exc"
1982		fi
1983		if [ "$X_FLAG" = "y" -a -f $IA32_IHV_WS/usr/src/pkgdefs/$exc ]; then
1984			ELIST="$ELIST -e $IA32_IHV_WS/usr/src/pkgdefs/$exc"
1985		fi
1986
1987		if [ -f "$REF_PROTO_LIST" ]; then
1988			# For builds that copy the IHV proto area (-X), add the
1989			# IHV proto list to the reference list if the reference
1990			# was built without -X.
1991			#
1992			# For builds that don't copy the IHV proto area, add the
1993			# IHV proto list to the build's proto list if the
1994			# reference was built with -X.
1995			#
1996			# Use the presence of the first file entry of the cached
1997			# IHV proto list in the reference list to determine
1998			# whether it was build with -X or not.
1999			IHV_REF_PROTO_LIST=$SRC/pkgdefs/etc/proto_list_ihv_$MACH
2000			grepfor=$(nawk '$1 == "f" { print $2; exit }' \
2001				$IHV_REF_PROTO_LIST 2> /dev/null)
2002			if [ $? = 0 -a -n "$grepfor" ]; then
2003				if [ "$X_FLAG" = "y" ]; then
2004					grep -w "$grepfor" \
2005						$REF_PROTO_LIST > /dev/null
2006					if [ ! "$?" = "0" ]; then
2007						REF_IHV_PROTO="-d $IHV_REF_PROTO_LIST"
2008					fi
2009				else
2010					grep -w "$grepfor" \
2011						$REF_PROTO_LIST > /dev/null
2012					if [ "$?" = "0" ]; then
2013						IHV_PROTO_LIST="$IHV_REF_PROTO_LIST"
2014					fi
2015				fi
2016			fi
2017
2018			$PROTOCMPTERSE \
2019			  "Files in yesterday's proto area, but not today's:" \
2020			  "Files in today's proto area, but not yesterday's:" \
2021			  "Files that changed between yesterday and today:" \
2022			  ${ELIST} \
2023			  -d $REF_PROTO_LIST \
2024			  $REF_IHV_PROTO \
2025			  $ATLOG/proto_list_${MACH} \
2026			  $IHV_PROTO_LIST \
2027				>> $mail_msg_file
2028		fi
2029		# Compare the build's proto list with current package
2030		# definitions to audit the quality of package definitions
2031		# and makefile install targets. Use the current exception list.
2032		PKGDEFS_LIST=""
2033		for d in $abssrcdirs; do
2034			if [ -d $d/pkgdefs ]; then
2035				PKGDEFS_LIST="$PKGDEFS_LIST -d $d/pkgdefs"
2036			fi
2037		done
2038		if [ "$X_FLAG" = "y" -a -d $IA32_IHV_WS/usr/src/pkgdefs ]; then
2039			PKGDEFS_LIST="$PKGDEFS_LIST -d $IA32_IHV_WS/usr/src/pkgdefs"
2040		fi
2041
2042		$PROTOCMPTERSE \
2043		    "Files missing from the proto area:" \
2044		    "Files missing from packages:" \
2045		    "Inconsistencies between pkgdefs and proto area:" \
2046		    ${ELIST} \
2047		    ${PKGDEFS_LIST} \
2048		    $ATLOG/proto_list_${MACH} \
2049		    >> $mail_msg_file
2050	fi
2051fi
2052
2053if [ "$u_FLAG" = "y"  -a "$build_ok" = "y" ]; then
2054	staffer cp $ATLOG/proto_list_${MACH} \
2055		$PARENT_WS/usr/src/proto_list_${MACH}
2056fi
2057
2058# Update parent proto area if necessary. This is done now
2059# so that the proto area has either DEBUG or non-DEBUG kernels.
2060# Note that this clears out the lock file, so we can dispense with
2061# the variable now.
2062if [ "$U_FLAG" = "y" -a "$build_ok" = "y" ]; then
2063	echo "\n==== Copying proto area to $NIGHTLY_PARENT_ROOT ====\n" | \
2064	    tee -a $LOGFILE >> $mail_msg_file
2065	# The rm -rf command below produces predictable errors if
2066	# nightly is invoked from the parent's $ROOT/opt/onbld/bin,
2067	# and that directory is accessed via NFS.  This is because
2068	# deleted-but-still-open files don't actually disappear as
2069	# expected, but rather turn into .nfsXXXX junk files, leaving
2070	# the directory non-empty.  Since this is a not-unusual usage
2071	# pattern, and we still want to catch other errors here, we
2072	# take the unusal step of moving aside 'nightly' from that
2073	# directory (if we're using it).
2074	mypath=${0##*/root_$MACH/}
2075	if [ "$mypath" = $0 ]; then
2076		mypath=opt/onbld/bin/${0##*/}
2077	fi
2078	if [ $0 -ef $PARENT_WS/proto/root_$MACH/$mypath ]; then
2079		mv -f $0 $PARENT_WS/proto/root_$MACH
2080	fi
2081	rm -rf $PARENT_WS/proto/root_$MACH/*
2082	unset Ulockfile
2083	mkdir -p $NIGHTLY_PARENT_ROOT
2084	cd $ROOT
2085	( tar cf - . | ( cd $NIGHTLY_PARENT_ROOT;  umask 0; tar xpf - ) ) 2>&1 |
2086		tee -a $mail_msg_file >> $LOGFILE
2087fi
2088
2089#
2090# do shared library interface verification
2091#
2092
2093if [ "$A_FLAG" = "y" -a "$build_ok" = "y" ]; then
2094	echo "\n==== Check versioning and ABI information ====\n"  | \
2095	    tee -a $LOGFILE >> $mail_msg_file
2096
2097	rm -rf $SRC/interfaces.ref
2098	if [ -d $SRC/interfaces.out ]; then
2099		mv $SRC/interfaces.out $SRC/interfaces.ref
2100	fi
2101	rm -rf $SRC/interfaces.out
2102	mkdir -p $SRC/interfaces.out
2103
2104	intf_check -V -m -o -b $SRC/tools/abi/etc \
2105		-d $SRC/interfaces.out $ROOT 2>&1 | sort \
2106		> $SRC/interfaces.out/log
2107
2108	# report any ERROR found in log file
2109	fgrep 'ERROR' $SRC/interfaces.out/log | sed 's/^ERROR: //' | \
2110		tee -a $LOGFILE >> $mail_msg_file
2111
2112	if [ ! -d $SRC/interfaces.ref ] ; then
2113		mkdir -p $SRC/interfaces.ref
2114		if [ -d  $SRC/interfaces.out ]; then
2115			cp -r $SRC/interfaces.out/* $SRC/interfaces.ref
2116		fi
2117	fi
2118
2119	echo "\n==== Diff versioning warnings (since last build) ====\n" | \
2120	    tee -a $LOGFILE >> $mail_msg_file
2121
2122	out_vers=`grep ^VERSION $SRC/interfaces.out/log`;
2123	ref_vers=`grep ^VERSION $SRC/interfaces.ref/log`;
2124
2125	# Report any differences in WARNING messages between last
2126	# and current build.
2127	if [ "$out_vers" = "$ref_vers" ]; then
2128		diff $SRC/interfaces.ref/log $SRC/interfaces.out/log | \
2129		    fgrep 'WARNING' | sed 's/WARNING: //' | \
2130		    tee -a $LOGFILE >> $mail_msg_file
2131	fi
2132fi
2133
2134if [ "$r_FLAG" = "y" -a "$build_ok" = "y" ]; then
2135	echo "\n==== Check ELF runtime attributes ====\n" | \
2136	    tee -a $LOGFILE >> $mail_msg_file
2137
2138	LDDUSAGE="^ldd: does not support -e"
2139	LDDWRONG="wrong class"
2140	CRLERROR="^crle:"
2141	CRLECONF="^crle: configuration file:"
2142
2143	rm -f $SRC/runtime.ref
2144	if [ -f $SRC/runtime.out ]; then
2145		egrep -v "$LDDUSAGE|$LDDWRONG|$CRLERROR|$CRLECONF" \
2146			$SRC/runtime.out > $SRC/runtime.ref
2147	fi
2148
2149	# If we're doing a debug build the proto area will be left with
2150	# debuggable objects, thus don't assert -s.
2151	if [ "$D_FLAG" = "y" ]; then
2152		rtime_sflag=""
2153	else
2154		rtime_sflag="-s"
2155	fi
2156	check_rtime -d $ROOT -i -m -o $rtime_sflag $ROOT 2>&1 | \
2157	    egrep -v ": unreferenced object=$ROOT/.*/lib(w|intl|thread|pthread).so" | \
2158	    egrep -v ": unused object=$ROOT/.*/lib(w|intl|thread|pthread).so" | \
2159	    sort >$SRC/runtime.out
2160
2161	# Determine any processing errors that will affect the final output
2162	# and display these first.
2163	grep -l "$LDDUSAGE" $SRC/runtime.out > /dev/null
2164	if [ $? -eq 0 ]; then
2165	    echo "WARNING: ldd(1) does not support -e.  The version of ldd(1)" | \
2166		tee -a $LOGFILE >> $mail_msg_file
2167	    echo "on your system is old - 4390308 (s81_30) is required.\n" | \
2168		tee -a $LOGFILE >> $mail_msg_file
2169	fi
2170	grep -l "$LDDWRONG" $SRC/runtime.out > /dev/null
2171	if [ $? -eq 0 ]; then
2172	    echo "WARNING: wrong class message detected.  ldd(1) was unable" | \
2173		tee -a $LOGFILE >> $mail_msg_file
2174	    echo "to execute an object, thus it could not be checked fully." | \
2175		tee -a $LOGFILE >> $mail_msg_file
2176	    echo "Perhaps a 64-bit object was encountered on a 32-bit system," | \
2177		tee -a $LOGFILE >> $mail_msg_file
2178	    echo "or an i386 object was encountered on a sparc system?\n" | \
2179		tee -a $LOGFILE >> $mail_msg_file
2180	fi
2181	grep -l "$CRLECONF" $SRC/runtime.out > /dev/null
2182	if [ $? -eq 0 ]; then
2183	    echo "WARNING: creation of an alternative dependency cache failed." | \
2184		tee -a $LOGFILE >> $mail_msg_file
2185	    echo "Dependencies will bind to the base system libraries.\n" | \
2186		tee -a $LOGFILE >> $mail_msg_file
2187	    grep "$CRLECONF" $SRC/runtime.out | \
2188		tee -a $LOGFILE >> $mail_msg_file
2189	    grep "$CRLERROR" $SRC/runtime.out | grep -v "$CRLECONF" | \
2190		tee -a $LOGFILE >> $mail_msg_file
2191	    echo "\n" | tee -a $LOGFILE >> $mail_msg_file
2192	fi
2193
2194	egrep '<dependency no longer necessary>' $SRC/runtime.out | \
2195	    tee -a $LOGFILE >> $mail_msg_file
2196
2197	# NEEDED= and RPATH= are informational; report anything else that we
2198	# haven't already.
2199	egrep -v "NEEDED=|RPATH=|$LDDUSAGE|$LDDWRONG|$CRLERROR|$CRLECONF" \
2200	    $SRC/runtime.out | tee -a $LOGFILE >> $mail_msg_file
2201
2202	# probably should compare against a 'known ok runpaths' list
2203	if [ ! -f $SRC/runtime.ref ]; then
2204		egrep -v "$LDDUSAGE|$LDDWRONG|$CRLERROR|$CRLECONF" \
2205			$SRC/runtime.out >  $SRC/runtime.ref
2206	fi
2207
2208	echo "\n==== Diff ELF runtime attributes (since last build) ====\n" \
2209	    >> $mail_msg_file
2210
2211	egrep -v "$LDDUSAGE|$LDDWRONG|$CRLERROR|$CRLECONF" $SRC/runtime.out | \
2212	    diff $SRC/runtime.ref - >> $mail_msg_file
2213fi
2214
2215# DEBUG lint of kernel begins
2216
2217if [ "$i_CMD_LINE_FLAG" = "n" -a "$l_FLAG" = "y" ]; then
2218	if [ "$LINTDIRS" = "" ]; then
2219		# LINTDIRS="$SRC/uts y $SRC/stand y $SRC/psm y"
2220		LINTDIRS="$SRC y"
2221	fi
2222	set $LINTDIRS
2223	while [ $# -gt 0 ]; do
2224		dolint $1 $2; shift; shift
2225	done
2226else
2227	echo "\n==== No '$MAKE lint' ====\n" >> $LOGFILE
2228fi
2229
2230# "make check" begins
2231
2232if [ "$i_CMD_LINE_FLAG" = "n" -a "$C_FLAG" = "y" ]; then
2233	# remove old check.out
2234	rm -f $SRC/check.out
2235
2236	rm -f $SRC/check-${MACH}.out
2237	cd $SRC
2238	$MAKE -ek check 2>&1 | tee -a $SRC/check-${MACH}.out >> $LOGFILE
2239	echo "\n==== cstyle/hdrchk errors ====\n" >> $mail_msg_file
2240
2241	grep ":" $SRC/check-${MACH}.out |
2242		egrep -v "Ignoring unknown host" | \
2243		sort | uniq >> $mail_msg_file
2244else
2245	echo "\n==== No '$MAKE check' ====\n" >> $LOGFILE
2246fi
2247
2248echo "\n==== Find core files ====\n" | \
2249    tee -a $LOGFILE >> $mail_msg_file
2250
2251find $abssrcdirs -name core -a -type f -exec file {} \; | \
2252	tee -a $LOGFILE >> $mail_msg_file
2253
2254if [ "$f_FLAG" = "y" -a "$build_ok" = "y" ]; then
2255	echo "\n==== Diff unreferenced files (since last build) ====\n" \
2256	    | tee -a $LOGFILE >>$mail_msg_file
2257	rm -f $SRC/unref-${MACH}.ref
2258	if [ -f $SRC/unref-${MACH}.out ]; then
2259		mv $SRC/unref-${MACH}.out $SRC/unref-${MACH}.ref
2260	fi
2261
2262	findunref -t $SRC/.build.tstamp $SRC/.. \
2263	    ${TOOLS}/findunref/exception_list \
2264	    2>> $mail_msg_file | sort | \
2265	    sed -e s=^./src/=./= -e s=^./closed/=../closed/= \
2266	    > $SRC/unref-${MACH}.out
2267
2268	if [ ! -f $SRC/unref-${MACH}.ref ]; then
2269		cp $SRC/unref-${MACH}.out $SRC/unref-${MACH}.ref
2270	fi
2271
2272	diff $SRC/unref-${MACH}.ref $SRC/unref-${MACH}.out >>$mail_msg_file
2273fi
2274
2275# Verify that the usual lists of files, such as exception lists,
2276# contain only valid references to files.  If the build has failed,
2277# then don't check the proto area.
2278CHECK_PATHS=${CHECK_PATHS:-y}
2279if [ "$CHECK_PATHS" = y -a "$N_FLAG" != y ]; then
2280	echo "\n==== Check lists of files ====\n" | tee -a $LOGFILE \
2281		>>$mail_msg_file
2282	arg=-b
2283	[ "$build_ok" = y ] && arg=
2284	checkpaths $arg $ROOT 2>&1 | tee -a $LOGFILE >>$mail_msg_file
2285fi
2286
2287if [ "$M_FLAG" != "y" -a "$build_ok" = y ]; then
2288	echo "\n==== Impact on file permissions ====\n" \
2289		>> $mail_msg_file
2290	#
2291	# Get pkginfo files from usr/src/pkgdefs
2292	#
2293	pmodes -qvdP \
2294	`for d in $abssrcdirs; do
2295		if [ -d "$d/pkgdefs" ]
2296		then
2297			find $d/pkgdefs -name pkginfo.tmpl -print -o -name .del\* -prune
2298		fi
2299	 done | sed -e 's:/pkginfo.tmpl$::' | sort -u ` >> $mail_msg_file
2300fi
2301
2302if [ "$w_FLAG" = "y" -a "$build_ok" = "y" ]; then
2303	echo "\n==== Objects that differ since last build ====\n" | \
2304	    tee -a $LOGFILE >> $mail_msg_file
2305
2306	if [ "$t_FLAG" = "y" ]; then
2307	    wsdiff -t -r ${TMPDIR}/wsdiff.results $ROOT.prev $ROOT | \
2308		tee -a $LOGFILE >> $mail_msg_file
2309	else
2310	    wsdiff -r ${TMPDIR}/wsdiff.results $ROOT.prev $ROOT  | \
2311		tee -a $LOGFILE >> $mail_msg_file
2312	fi
2313fi
2314
2315END_DATE=`date`
2316echo "==== Nightly $maketype build completed: $END_DATE ====" | \
2317    tee -a $LOGFILE >> $build_time_file
2318
2319typeset -Z2 minutes
2320typeset -Z2 seconds
2321
2322elapsed_time=$SECONDS
2323((hours = elapsed_time / 3600 ))
2324((minutes = elapsed_time / 60  % 60))
2325((seconds = elapsed_time % 60))
2326
2327echo "\n==== Total build time ====" | \
2328    tee -a $LOGFILE >> $build_time_file
2329echo "\nreal    ${hours}:${minutes}:${seconds}" | \
2330    tee -a $LOGFILE >> $build_time_file
2331
2332if [ "$u_FLAG" = "y" -a "$f_FLAG" = "y" -a "$build_ok" = "y" ]; then
2333	staffer cp ${SRC}/unref-${MACH}.out $PARENT_WS/usr/src/
2334
2335	#
2336	# Produce a master list of unreferenced files -- ideally, we'd
2337	# generate the master just once after all of the nightlies
2338	# have finished, but there's no simple way to know when that
2339	# will be.  Instead, we assume that we're the last nightly to
2340	# finish and merge all of the unref-${MACH}.out files in
2341	# $PARENT_WS/usr/src/.  If we are in fact the final ${MACH} to
2342	# finish, then this file will be the authoritative master
2343	# list.  Otherwise, another ${MACH}'s nightly will eventually
2344	# overwrite ours with its own master, but in the meantime our
2345	# temporary "master" will be no worse than any older master
2346	# which was already on the parent.
2347	#
2348
2349	set -- $PARENT_WS/usr/src/unref-*.out
2350	cp "$1" ${TMPDIR}/unref.merge
2351	shift
2352
2353	for unreffile; do
2354		comm -12 ${TMPDIR}/unref.merge "$unreffile" > ${TMPDIR}/unref.$$
2355		mv ${TMPDIR}/unref.$$ ${TMPDIR}/unref.merge
2356	done
2357
2358	staffer cp ${TMPDIR}/unref.merge $PARENT_WS/usr/src/unrefmaster.out
2359fi
2360
2361#
2362# All done save for the sweeping up.
2363# (whichever exit we hit here will trigger the "cleanup" trap which
2364# optionally sends mail on completion).
2365#
2366if [ "$build_ok" = "y" ]; then
2367	exit 0
2368fi
2369exit 1
2370