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