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