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