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