xref: /titanic_51/usr/src/tools/scripts/nightly.sh (revision cf705556833c77102da060256e3d97bd13f6f18a)
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#
24# Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
25# Copyright 2008, 2010, Richard Lowe
26# Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
27#
28# Based on the nightly script from the integration folks,
29# Mostly modified and owned by mike_s.
30# Changes also by kjc, dmk.
31#
32# BRINGOVER_WS may be specified in the env file.
33# The default is the old behavior of CLONE_WS
34#
35# -i on the command line, means fast options, so when it's on the
36# command line (only), lint and check builds are skipped no matter what
37# the setting of their individual flags are in NIGHTLY_OPTIONS.
38#
39# LINTDIRS can be set in the env file, format is a list of:
40#
41#	/dirname-to-run-lint-on flag
42#
43#	Where flag is:	y - enable lint noise diff output
44#			n - disable lint noise diff output
45#
46#	For example: LINTDIRS="$SRC/uts n $SRC/stand y $SRC/psm y"
47#
48# OPTHOME and TEAMWARE may be set in the environment to override /opt
49# and /opt/teamware defaults.
50#
51
52#
53# The CDPATH variable causes ksh's `cd' builtin to emit messages to stdout
54# under certain circumstances, which can really screw things up; unset it.
55#
56unset CDPATH
57
58# Get the absolute path of the nightly script that the user invoked.  This
59# may be a relative path, and we need to do this before changing directory.
60nightly_path=`whence $0`
61
62#
63# Keep track of where we found nightly so we can invoke the matching
64# which_scm script.  If that doesn't work, don't go guessing, just rely
65# on the $PATH settings, which will generally give us either /opt/onbld
66# or the user's workspace.
67#
68WHICH_SCM=$(dirname $nightly_path)/which_scm
69if [[ ! -x $WHICH_SCM ]]; then
70	WHICH_SCM=which_scm
71fi
72
73function fatal_error
74{
75	print -u2 "nightly: $*"
76	exit 1
77}
78
79#
80# Function to do a DEBUG and non-DEBUG build. Needed because we might
81# need to do another for the source build, and since we only deliver DEBUG or
82# non-DEBUG packages.
83#
84# usage: normal_build
85#
86function normal_build {
87
88	typeset orig_p_FLAG="$p_FLAG"
89	typeset crypto_signer="$CODESIGN_USER"
90
91	suffix=""
92
93	# non-DEBUG build begins
94
95	if [ "$F_FLAG" = "n" ]; then
96		set_non_debug_build_flags
97		CODESIGN_USER="$crypto_signer" \
98		    build "non-DEBUG" "$suffix-nd" "-nd" "$MULTI_PROTO"
99		if [ "$build_ok" = "y" -a "$X_FLAG" = "y" -a \
100		    "$p_FLAG" = "y" ]; then
101			copy_ihv_pkgs non-DEBUG -nd
102		fi
103	else
104		echo "\n==== No non-DEBUG $open_only build ====\n" >> "$LOGFILE"
105	fi
106
107	# non-DEBUG build ends
108
109	# DEBUG build begins
110
111	if [ "$D_FLAG" = "y" ]; then
112		set_debug_build_flags
113		CODESIGN_USER="$crypto_signer" \
114		    build "DEBUG" "$suffix" "" "$MULTI_PROTO"
115		if [ "$build_ok" = "y" -a "$X_FLAG" = "y" -a \
116		    "$p_FLAG" = "y" ]; then
117			copy_ihv_pkgs DEBUG ""
118		fi
119	else
120		echo "\n==== No DEBUG $open_only build ====\n" >> "$LOGFILE"
121	fi
122
123	# DEBUG build ends
124
125	p_FLAG="$orig_p_FLAG"
126}
127
128#
129# usage: run_hook HOOKNAME ARGS...
130#
131# If variable "$HOOKNAME" is defined, insert a section header into
132# our logs and then run the command with ARGS
133#
134function run_hook {
135	HOOKNAME=$1
136    	eval HOOKCMD=\$$HOOKNAME
137	shift
138
139	if [ -n "$HOOKCMD" ]; then
140	    	(
141			echo "\n==== Running $HOOKNAME command: $HOOKCMD ====\n"
142		    	( $HOOKCMD "$@" 2>&1 )
143			if [ "$?" -ne 0 ]; then
144			    	# Let exit status propagate up
145			    	touch $TMPDIR/abort
146			fi
147		) | tee -a $mail_msg_file >> $LOGFILE
148
149		if [ -f $TMPDIR/abort ]; then
150			build_ok=n
151			echo "\nAborting at request of $HOOKNAME" |
152				tee -a $mail_msg_file >> $LOGFILE
153			exit 1
154		fi
155	fi
156}
157
158#
159# usage: filelist DESTDIR PATTERN
160#
161function filelist {
162	DEST=$1
163	PATTERN=$2
164	cd ${DEST}
165
166	OBJFILES=${ORIG_SRC}/xmod/obj_files
167	if [ ! -f ${OBJFILES} ]; then
168		return;
169	fi
170	for i in `grep -v '^#' ${OBJFILES} | \
171	    grep ${PATTERN} | cut -d: -f2 | tr -d ' \t'`
172	do
173		# wildcard expansion
174		for j in $i
175		do
176			if [ -f "$j" ]; then
177				echo $j
178			fi
179			if [ -d "$j" ]; then
180				echo $j
181			fi
182		done
183	done | sort | uniq
184}
185
186# function to save off binaries after a full build for later
187# restoration
188function save_binaries {
189	# save off list of binaries
190	echo "\n==== Saving binaries from build at `date` ====\n" | \
191	    tee -a $mail_msg_file >> $LOGFILE
192	rm -f ${BINARCHIVE}
193	cd ${CODEMGR_WS}
194	filelist ${CODEMGR_WS} '^preserve:' >> $LOGFILE
195	filelist ${CODEMGR_WS} '^preserve:' | \
196	    cpio -ocB 2>/dev/null | compress \
197	    > ${BINARCHIVE}
198}
199
200# delete files
201# usage: hybridize_files DESTDIR MAKE_TARGET
202function hybridize_files {
203	DEST=$1
204	MAKETARG=$2
205
206	echo "\n==== Hybridizing files at `date` ====\n" | \
207	    tee -a $mail_msg_file >> $LOGFILE
208	for i in `filelist ${DEST} '^delete:'`
209	do
210		echo "removing ${i}." | tee -a $mail_msg_file >> $LOGFILE
211		rm -rf "${i}"
212	done
213	for i in `filelist ${DEST} '^hybridize:' `
214	do
215		echo "hybridizing ${i}." | tee -a $mail_msg_file >> $LOGFILE
216		rm -f ${i}+
217		sed -e "/^# HYBRID DELETE START/,/^# HYBRID DELETE END/d" \
218		    < ${i} > ${i}+
219		mv ${i}+ ${i}
220	done
221}
222
223# restore binaries into the proper source tree.
224# usage: restore_binaries DESTDIR MAKE_TARGET
225function restore_binaries {
226	DEST=$1
227	MAKETARG=$2
228
229	echo "\n==== Restoring binaries to ${MAKETARG} at `date` ====\n" | \
230	    tee -a $mail_msg_file >> $LOGFILE
231	cd ${DEST}
232	zcat ${BINARCHIVE} | \
233	    cpio -idmucvB 2>/dev/null | tee -a $mail_msg_file >> ${LOGFILE}
234}
235
236# rename files we save binaries of
237# usage: rename_files DESTDIR MAKE_TARGET
238function rename_files {
239	DEST=$1
240	MAKETARG=$2
241	echo "\n==== Renaming source files in ${MAKETARG} at `date` ====\n" | \
242	    tee -a $mail_msg_file >> $LOGFILE
243	for i in `filelist ${DEST} '^rename:'`
244	do
245		echo ${i} | tee -a $mail_msg_file >> ${LOGFILE}
246		rm -f ${i}.export
247		mv ${i} ${i}.export
248	done
249}
250
251#
252# Copy some or all of the source tree.
253#
254# Returns 0 for success, non-zero for failure.
255#
256# usage: copy_source CODEMGR_WS DESTDIR LABEL SRCROOT
257#
258function copy_source {
259	WS=$1
260	DEST=$2
261	label=$3
262	srcroot=$4
263
264	printf "\n==== Creating %s source from %s (%s) ====\n\n" \
265	    "$DEST" "$WS" "$label" | tee -a $mail_msg_file >> $LOGFILE
266
267	printf "cleaning out %s\n" "$DEST." >> $LOGFILE
268	rm -rf "$DEST" >> $LOGFILE 2>&1
269
270	printf "creating %s\n" "$DEST." >> $LOGFILE
271	mkdir -p "$DEST" 2>> $LOGFILE
272
273	if (( $? != 0 )) ; then
274		printf "failed to create %s\n" "$DEST" |
275		    tee -a $mail_msg_file >> $LOGFILE
276		build_ok=n
277		return 1
278	fi
279	cd "$WS"
280
281	printf "populating %s\n" "$DEST." >> $LOGFILE
282
283	case "$SCM_TYPE" in
284	teamware)
285		find $srcroot -name 's\.*' -a -type f -print | \
286		    sed -e 's,SCCS\/s.,,' | \
287		    grep -v '/\.del-*' | \
288		    cpio -pd $DEST >>$LOGFILE 2>&1
289		if (( $? != 0 )) ; then
290		    printf "cpio failed for %s\n" "$DEST" |
291			tee -a $mail_msg_file >> $LOGFILE
292		    build_ok=n
293		    return 1
294		fi
295		;;
296	mercurial)
297		copy_source_mercurial $DEST $srcroot
298		if (( $? != 0 )) ; then
299		    build_ok=n
300		    return 1
301		fi
302		;;
303	*)
304		build_ok=n
305		echo "Tree copy is not supported for workspace type" \
306		    "$SCM_TYPE" | tee -a $mail_msg_file >> $LOGFILE
307		return 1
308		;;
309	esac
310
311	return 0
312}
313
314#
315# Mercurial-specific copy code for copy_source().  Handles the
316# combined open and closed trees.
317#
318# Returns 0 for success, non-zero for failure.
319#
320# usage: copy_source_mercurial destdir srcroot
321#
322function copy_source_mercurial {
323	typeset dest=$1
324	typeset srcroot=$2
325	typeset open_top closed_top
326
327	case $srcroot in
328	usr)
329		open_top=usr
330		if [[ "$CLOSED_IS_PRESENT" = yes ]]; then
331			closed_top=usr/closed
332		fi
333		;;
334	usr/closed*)
335		if [[ "$CLOSED_IS_PRESENT" = no ]]; then
336			printf "can't copy %s: closed tree not present.\n" \
337			    "$srcroot" | tee -a $mail_msg_file >> $LOGFILE
338			return 1
339		fi
340		closed_top="$srcroot"
341		;;
342	*)
343		open_top="$srcroot"
344		;;
345	esac
346
347	if [[ -n "$open_top" ]]; then
348		hg locate -I "$open_top" | cpio -pd "$dest" >>$LOGFILE 2>&1
349		if (( $? != 0 )) ; then
350		    printf "cpio failed for %s\n" "$dest" |
351			tee -a $mail_msg_file >> $LOGFILE
352		    return 1
353		fi
354	fi
355
356	if [[ -n "$closed_top" ]]; then
357		mkdir -p "$dest/usr/closed" || return 1
358		if [[ "$closed_top" = usr/closed ]]; then
359			(cd usr/closed; hg locate |
360			    cpio -pd "$dest/usr/closed") >>$LOGFILE 2>&1
361			if (( $? != 0 )) ; then
362			    printf "cpio failed for %s/usr/closed\n" \
363				"$dest" | tee -a $mail_msg_file >> $LOGFILE
364			    return 1
365			fi
366		else
367			# copy subtree of usr/closed
368			closed_top=${closed_top#usr/closed/}
369			(cd usr/closed; hg locate -I "$closed_top" |
370			    cpio -pd "$dest/usr/closed") >>$LOGFILE 2>&1
371			if (( $? != 0 )) ; then
372			    printf "cpio failed for %s/usr/closed/%s\n" \
373				"$dest" "$closed_top" |
374				tee -a $mail_msg_file >> $LOGFILE
375			    return 1
376			fi
377		fi
378	fi
379
380	return 0
381}
382
383#
384# function to create (but not build) the export/crypt source tree.
385# usage: set_up_source_build CODEMGR_WS DESTDIR MAKE_TARGET
386# Sets SRC to the modified source tree, for use by the caller when it
387# builds the tree.
388#
389function set_up_source_build {
390	WS=$1
391	DEST=$2
392	MAKETARG=$3
393
394	copy_source $WS $DEST $MAKETARG usr
395	if (( $? != 0 )); then
396	    echo "\nCould not copy source tree for source build." |
397		tee -a $mail_msg_file >> $LOGFILE
398	    build_ok=n
399	    return
400	fi
401
402	SRC=${DEST}/usr/src
403
404	cd $SRC
405	rm -f ${MAKETARG}.out
406	echo "making ${MAKETARG} in ${SRC}." >> $LOGFILE
407	/bin/time $MAKE -e ${MAKETARG} 2>&1 | \
408	    tee -a $SRC/${MAKETARG}.out >> $LOGFILE
409	echo "\n==== ${MAKETARG} build errors ====\n" >> $mail_msg_file
410	egrep ":" $SRC/${MAKETARG}.out | \
411		egrep -e "(^${MAKE}:|[ 	]error[: 	\n])" | \
412		egrep -v "Ignoring unknown host" | \
413		egrep -v "warning" >> $mail_msg_file
414
415	echo "clearing state files." >> $LOGFILE
416	find . -name '.make*' -exec rm -f {} \;
417
418	cd ${DEST}
419	if [ "${MAKETARG}" = "CRYPT_SRC" ]; then
420		rm -f ${CODEMGR_WS}/crypt_files.cpio.Z
421		echo "\n==== xmod/cry_files that don't exist ====\n" | \
422		    tee -a $mail_msg_file >> $LOGFILE
423		CRYPT_FILES=${WS}/usr/src/xmod/cry_files
424		for i in `cat ${CRYPT_FILES}`
425		do
426			# make sure the files exist
427			if [ -f "$i" ]; then
428				continue
429			fi
430			if [ -d "$i" ]; then
431				continue
432			fi
433			echo "$i" | tee -a $mail_msg_file >> $LOGFILE
434		done
435		find `cat ${CRYPT_FILES}` -print 2>/dev/null | \
436		    cpio -ocB 2>/dev/null | \
437		    compress > ${CODEMGR_WS}/crypt_files.cpio.Z
438	fi
439
440	if [ "${MAKETARG}" = "EXPORT_SRC" ]; then
441		# rename first, since we might restore a file
442		# of the same name (mapfiles)
443		rename_files ${EXPORT_SRC} EXPORT_SRC
444		if [ "$SH_FLAG" = "y" ]; then
445			hybridize_files ${EXPORT_SRC} EXPORT_SRC
446		fi
447	fi
448
449	# save the cleartext
450	echo "\n==== Creating ${MAKETARG}.cpio.Z ====\n" | \
451	    tee -a $mail_msg_file >> $LOGFILE
452	cd ${DEST}
453	rm -f ${MAKETARG}.cpio.Z
454	find usr -depth -print | \
455	    grep -v usr/src/${MAKETARG}.out | \
456	    cpio -ocB 2>/dev/null | \
457	    compress > ${CODEMGR_WS}/${MAKETARG}.cpio.Z
458	if [ "${MAKETARG}" = "EXPORT_SRC" ]; then
459		restore_binaries ${EXPORT_SRC} EXPORT_SRC
460	fi
461
462	if [ "${MAKETARG}" = "CRYPT_SRC" ]; then
463		restore_binaries ${CRYPT_SRC} CRYPT_SRC
464	fi
465
466}
467
468# Return library search directive as function of given root.
469function myldlibs {
470	echo "-L$1/lib -L$1/usr/lib"
471}
472
473# Return header search directive as function of given root.
474function myheaders {
475	echo "-I$1/usr/include"
476}
477
478#
479# Function to do the build, including package generation.
480# usage: build LABEL SUFFIX ND MULTIPROTO
481# - LABEL is used to tag build output.
482# - SUFFIX is used to distinguish files (e.g., DEBUG vs non-DEBUG,
483#   open-only vs full tree).
484# - ND is "-nd" (non-DEBUG builds) or "" (DEBUG builds).
485# - If MULTIPROTO is "yes", it means to name the proto area according to
486#   SUFFIX.  Otherwise ("no"), (re)use the standard proto area.
487#
488function build {
489	LABEL=$1
490	SUFFIX=$2
491	ND=$3
492	MULTIPROTO=$4
493	INSTALLOG=install${SUFFIX}-${MACH}
494	NOISE=noise${SUFFIX}-${MACH}
495	PKGARCHIVE=${PKGARCHIVE_ORIG}${SUFFIX}
496
497	ORIGROOT=$ROOT
498	[ $MULTIPROTO = no ] || export ROOT=$ROOT$SUFFIX
499
500	if [[ "$O_FLAG" = y ]]; then
501		echo "\nSetting CLOSEDROOT= ${ROOT}-closed\n" >> $LOGFILE
502		export CLOSEDROOT=${ROOT}-closed
503	fi
504
505	export ENVLDLIBS1=`myldlibs $ROOT`
506	export ENVCPPFLAGS1=`myheaders $ROOT`
507
508	this_build_ok=y
509	#
510	#	Build OS-Networking source
511	#
512	echo "\n==== Building OS-Net source at `date` ($LABEL) ====\n" \
513		>> $LOGFILE
514
515	rm -f $SRC/${INSTALLOG}.out
516	cd $SRC
517	/bin/time $MAKE -e install 2>&1 | \
518	    tee -a $SRC/${INSTALLOG}.out >> $LOGFILE
519
520	if [[ "$SCM_TYPE" = teamware ]]; then
521		echo "\n==== SCCS Noise ($LABEL) ====\n" >> $mail_msg_file
522		egrep 'sccs(check:| *get)' $SRC/${INSTALLOG}.out >> \
523			$mail_msg_file
524	fi
525
526	echo "\n==== Build errors ($LABEL) ====\n" >> $mail_msg_file
527	egrep ":" $SRC/${INSTALLOG}.out |
528		egrep -e "(^${MAKE}:|[ 	]error[: 	\n])" | \
529		egrep -v "Ignoring unknown host" | \
530		egrep -v "cc .* -o error " | \
531		egrep -v "warning" >> $mail_msg_file
532	if [ "$?" = "0" ]; then
533		build_ok=n
534		this_build_ok=n
535	fi
536	grep "bootblock image is .* bytes too big" $SRC/${INSTALLOG}.out \
537		>> $mail_msg_file
538	if [ "$?" = "0" ]; then
539		build_ok=n
540		this_build_ok=n
541	fi
542
543	if [ "$W_FLAG" = "n" ]; then
544		echo "\n==== Build warnings ($LABEL) ====\n" >>$mail_msg_file
545		egrep -i warning: $SRC/${INSTALLOG}.out \
546			| egrep -v '^tic:' \
547			| egrep -v "symbol (\`|')timezone' has differing types:" \
548		        | egrep -v "parameter <PSTAMP> set to" \
549			| egrep -v "Ignoring unknown host" \
550			| egrep -v "redefining segment flags attribute for" \
551			>> $mail_msg_file
552	fi
553
554	echo "\n==== Ended OS-Net source build at `date` ($LABEL) ====\n" \
555		>> $LOGFILE
556
557	echo "\n==== Elapsed build time ($LABEL) ====\n" >>$mail_msg_file
558	tail -3  $SRC/${INSTALLOG}.out >>$mail_msg_file
559
560	if [ "$i_FLAG" = "n" -a "$W_FLAG" = "n" ]; then
561		rm -f $SRC/${NOISE}.ref
562		if [ -f $SRC/${NOISE}.out ]; then
563			mv $SRC/${NOISE}.out $SRC/${NOISE}.ref
564		fi
565		grep : $SRC/${INSTALLOG}.out \
566			| egrep -v '^/' \
567			| egrep -v '^(Start|Finish|real|user|sys|./bld_awk)' \
568			| egrep -v '^tic:' \
569			| egrep -v '^mcs' \
570			| egrep -v '^LD_LIBRARY_PATH=' \
571			| egrep -v 'ar: creating' \
572			| egrep -v 'ar: writing' \
573			| egrep -v 'conflicts:' \
574			| egrep -v ':saved created' \
575			| egrep -v '^stty.*c:' \
576			| egrep -v '^mfgname.c:' \
577			| egrep -v '^uname-i.c:' \
578			| egrep -v '^volumes.c:' \
579			| egrep -v '^lint library construction:' \
580			| egrep -v 'tsort: INFORM:' \
581			| egrep -v 'stripalign:' \
582			| egrep -v 'chars, width' \
583			| egrep -v "symbol (\`|')timezone' has differing types:" \
584			| egrep -v 'PSTAMP' \
585			| egrep -v '|%WHOANDWHERE%|' \
586			| egrep -v '^Manifying' \
587			| egrep -v 'Ignoring unknown host' \
588			| egrep -v 'Processing method:' \
589			| egrep -v '^Writing' \
590			| egrep -v 'spellin1:' \
591			| egrep -v '^adding:' \
592			| egrep -v "^echo 'msgid" \
593			| egrep -v '^echo ' \
594			| egrep -v '\.c:$' \
595			| egrep -v '^Adding file:' \
596			| egrep -v 'CLASSPATH=' \
597			| egrep -v '\/var\/mail\/:saved' \
598			| egrep -v -- '-DUTS_VERSION=' \
599			| egrep -v '^Running Mkbootstrap' \
600			| egrep -v '^Applet length read:' \
601			| egrep -v 'bytes written:' \
602			| egrep -v '^File:SolarisAuthApplet.bin' \
603			| egrep -v -i 'jibversion' \
604			| egrep -v '^Output size:' \
605			| egrep -v '^Solo size statistics:' \
606			| egrep -v '^Using ROM API Version' \
607			| egrep -v '^Zero Signature length:' \
608			| egrep -v '^Note \(probably harmless\):' \
609			| egrep -v '::' \
610			| egrep -v -- '-xcache' \
611			| egrep -v '^\+' \
612			| egrep -v '^cc1: note: -fwritable-strings' \
613			| egrep -v 'svccfg-native -s svc:/' \
614			| sort | uniq >$SRC/${NOISE}.out
615		if [ ! -f $SRC/${NOISE}.ref ]; then
616			cp $SRC/${NOISE}.out $SRC/${NOISE}.ref
617		fi
618		echo "\n==== Build noise differences ($LABEL) ====\n" \
619			>>$mail_msg_file
620		diff $SRC/${NOISE}.ref $SRC/${NOISE}.out >>$mail_msg_file
621	fi
622
623	#
624	#	Re-sign selected binaries using signing server
625	#	(gatekeeper builds only)
626	#
627	if [ -n "$CODESIGN_USER" -a "$this_build_ok" = "y" ]; then
628		echo "\n==== Signing proto area at `date` ====\n" >> $LOGFILE
629		signing_file="${TMPDIR}/signing"
630		rm -f ${signing_file}
631		export CODESIGN_USER
632		signproto $SRC/tools/codesign/creds 2>&1 | \
633			tee -a ${signing_file} >> $LOGFILE
634		echo "\n==== Finished signing proto area at `date` ====\n" \
635		    >> $LOGFILE
636		echo "\n==== Crypto module signing errors ($LABEL) ====\n" \
637		    >> $mail_msg_file
638		egrep 'WARNING|ERROR' ${signing_file} >> $mail_msg_file
639		if (( $? == 0 )) ; then
640			build_ok=n
641			this_build_ok=n
642		fi
643	fi
644
645	#
646	#	Building Packages
647	#
648	if [ "$p_FLAG" = "y" -a "$this_build_ok" = "y" ]; then
649		if [ -d $SRC/pkg -o -d $SRC/pkgdefs ]; then
650			echo "\n==== Creating $LABEL packages at `date` ====\n" \
651				>> $LOGFILE
652			echo "Clearing out $PKGARCHIVE ..." >> $LOGFILE
653			rm -rf $PKGARCHIVE >> "$LOGFILE" 2>&1
654			mkdir -p $PKGARCHIVE >> "$LOGFILE" 2>&1
655
656			for d in pkg pkgdefs; do
657				if [ ! -f "$SRC/$d/Makefile" ]; then
658					continue
659				fi
660				rm -f $SRC/$d/${INSTALLOG}.out
661				cd $SRC/$d
662				/bin/time $MAKE -e install 2>&1 | \
663					tee -a $SRC/$d/${INSTALLOG}.out >> $LOGFILE
664			done
665
666			echo "\n==== package build errors ($LABEL) ====\n" \
667				>> $mail_msg_file
668
669			for d in pkg pkgdefs; do
670				if [ ! -f "$SRC/$d/Makefile" ]; then
671					continue
672				fi
673
674				egrep "${MAKE}|ERROR|WARNING" $SRC/$d/${INSTALLOG}.out | \
675					grep ':' | \
676					grep -v PSTAMP | \
677					egrep -v "Ignoring unknown host" \
678					>> $mail_msg_file
679			done
680		else
681			#
682			# Handle it gracefully if -p was set but there are
683			# neither pkg nor pkgdefs directories.
684			#
685			echo "\n==== No $LABEL packages to build ====\n" \
686				>> $LOGFILE
687		fi
688	else
689		echo "\n==== Not creating $LABEL packages ====\n" >> $LOGFILE
690	fi
691
692	ROOT=$ORIGROOT
693}
694
695# Usage: dolint /dir y|n
696# Arg. 2 is a flag to turn on/off the lint diff output
697function dolint {
698	if [ ! -d "$1" ]; then
699		echo "dolint error: $1 is not a directory"
700		exit 1
701	fi
702
703	if [ "$2" != "y" -a "$2" != "n" ]; then
704		echo "dolint internal error: $2 should be 'y' or 'n'"
705		exit 1
706	fi
707
708	lintdir=$1
709	dodiff=$2
710	base=`basename $lintdir`
711	LINTOUT=$lintdir/lint-${MACH}.out
712	LINTNOISE=$lintdir/lint-noise-${MACH}
713	export ENVLDLIBS1=`myldlibs $ROOT`
714	export ENVCPPFLAGS1=`myheaders $ROOT`
715
716	set_debug_build_flags
717
718	#
719	#	'$MAKE lint' in $lintdir
720	#
721	echo "\n==== Begin '$MAKE lint' of $base at `date` ====\n" >> $LOGFILE
722
723	# remove old lint.out
724	rm -f $lintdir/lint.out $lintdir/lint-noise.out
725	if [ -f $lintdir/lint-noise.ref ]; then
726		mv $lintdir/lint-noise.ref ${LINTNOISE}.ref
727	fi
728
729	rm -f $LINTOUT
730	cd $lintdir
731	#
732	# Remove all .ln files to ensure a full reference file
733	#
734	rm -f Nothing_to_remove \
735	    `find . \( -name SCCS -o -name .hg -o -name .svn \) \
736	    	-prune -o -type f -name '*.ln' -print `
737
738	/bin/time $MAKE -ek lint 2>&1 | \
739	    tee -a $LINTOUT >> $LOGFILE
740	echo "\n==== '$MAKE lint' of $base ERRORS ====\n" >> $mail_msg_file
741	grep "$MAKE:" $LINTOUT |
742		egrep -v "Ignoring unknown host" \
743		>> $mail_msg_file
744
745	echo "\n==== Ended '$MAKE lint' of $base at `date` ====\n" >> $LOGFILE
746
747	echo "\n==== Elapsed time of '$MAKE lint' of $base ====\n" \
748		>>$mail_msg_file
749	tail -3  $LINTOUT >>$mail_msg_file
750
751	rm -f ${LINTNOISE}.ref
752	if [ -f ${LINTNOISE}.out ]; then
753		mv ${LINTNOISE}.out ${LINTNOISE}.ref
754	fi
755        grep : $LINTOUT | \
756		egrep -v '^(real|user|sys)' |
757		egrep -v '(library construction)' | \
758		egrep -v ': global crosschecks' | \
759		egrep -v 'Ignoring unknown host' | \
760		egrep -v '\.c:$' | \
761		sort | uniq > ${LINTNOISE}.out
762	if [ ! -f ${LINTNOISE}.ref ]; then
763		cp ${LINTNOISE}.out ${LINTNOISE}.ref
764	fi
765	if [ "$dodiff" != "n" ]; then
766		echo "\n==== lint warnings $base ====\n" \
767			>>$mail_msg_file
768		# should be none, though there are a few that were filtered out
769		# above
770		egrep -i '(warning|lint):' ${LINTNOISE}.out \
771			| sort | uniq >> $mail_msg_file
772		echo "\n==== lint noise differences $base ====\n" \
773			>> $mail_msg_file
774		diff ${LINTNOISE}.ref ${LINTNOISE}.out \
775			>> $mail_msg_file
776	fi
777}
778
779# Install proto area from IHV build
780
781function copy_ihv_proto {
782
783	echo "\n==== Installing IHV proto area ====\n" \
784		>> $LOGFILE
785	if [ -d "$IA32_IHV_ROOT" ]; then
786		if [ ! -d "$ROOT" ]; then
787			echo "mkdir -p $ROOT" >> $LOGFILE
788			mkdir -p $ROOT
789		fi
790		echo "copying $IA32_IHV_ROOT to $ROOT\n" >> $LOGFILE
791		cd $IA32_IHV_ROOT
792		tar cf - . | (cd $ROOT; umask 0; tar xpf - ) 2>&1 >> $LOGFILE
793	else
794		echo "$IA32_IHV_ROOT: not found" >> $LOGFILE
795	fi
796
797	if [ "$MULTI_PROTO" = yes ]; then
798		if [ ! -d "$ROOT-nd" ]; then
799			echo "mkdir -p $ROOT-nd" >> $LOGFILE
800			mkdir -p $ROOT-nd
801		fi
802		# If there's a non-DEBUG version of the IHV proto area,
803		# copy it, but copy something if there's not.
804		if [ -d "$IA32_IHV_ROOT-nd" ]; then
805			echo "copying $IA32_IHV_ROOT-nd to $ROOT-nd\n" >> $LOGFILE
806			cd $IA32_IHV_ROOT-nd
807		elif [ -d "$IA32_IHV_ROOT" ]; then
808			echo "copying $IA32_IHV_ROOT to $ROOT-nd\n" >> $LOGFILE
809			cd $IA32_IHV_ROOT
810		else
811			echo "$IA32_IHV_ROOT{-nd,}: not found" >> $LOGFILE
812			return
813		fi
814		tar cf - . | (cd $ROOT-nd; umask 0; tar xpf - ) 2>&1 >> $LOGFILE
815	fi
816}
817
818# Install IHV packages in PKGARCHIVE
819# usage: copy_ihv_pkgs LABEL SUFFIX
820function copy_ihv_pkgs {
821	LABEL=$1
822	SUFFIX=$2
823	# always use non-DEBUG IHV packages
824	IA32_IHV_PKGS=${IA32_IHV_PKGS_ORIG}-nd
825	PKGARCHIVE=${PKGARCHIVE_ORIG}${SUFFIX}
826
827	echo "\n==== Installing IHV packages from $IA32_IHV_PKGS ($LABEL) ====\n" \
828		>> $LOGFILE
829	if [ -d "$IA32_IHV_PKGS" ]; then
830		cd $IA32_IHV_PKGS
831		tar cf - * | \
832		   (cd $PKGARCHIVE; umask 0; tar xpf - ) 2>&1 >> $LOGFILE
833	else
834		echo "$IA32_IHV_PKGS: not found" >> $LOGFILE
835	fi
836
837	echo "\n==== Installing IHV packages from $IA32_IHV_BINARY_PKGS ($LABEL) ====\n" \
838		>> $LOGFILE
839	if [ -d "$IA32_IHV_BINARY_PKGS" ]; then
840		cd $IA32_IHV_BINARY_PKGS
841		tar cf - * | \
842		    (cd $PKGARCHIVE; umask 0; tar xpf - ) 2>&1 >> $LOGFILE
843	else
844		echo "$IA32_IHV_BINARY_PKGS: not found" >> $LOGFILE
845	fi
846}
847
848#
849# Build and install the onbld tools.
850#
851# usage: build_tools DESTROOT
852#
853# returns non-zero status if the build was successful.
854#
855function build_tools {
856	DESTROOT=$1
857
858	INSTALLOG=install-${MACH}
859
860	echo "\n==== Building tools at `date` ====\n" \
861		>> $LOGFILE
862
863	rm -f ${TOOLS}/${INSTALLOG}.out
864	cd ${TOOLS}
865	/bin/time $MAKE TOOLS_PROTO=${DESTROOT} -e install 2>&1 | \
866	    tee -a ${TOOLS}/${INSTALLOG}.out >> $LOGFILE
867
868	echo "\n==== Tools build errors ====\n" >> $mail_msg_file
869
870	egrep ":" ${TOOLS}/${INSTALLOG}.out |
871		egrep -e "(${MAKE}:|[ 	]error[: 	\n])" | \
872		egrep -v "Ignoring unknown host" | \
873		egrep -v warning >> $mail_msg_file
874	return $?
875}
876
877#
878# Set up to use locally installed tools.
879#
880# usage: use_tools TOOLSROOT
881#
882function use_tools {
883	TOOLSROOT=$1
884
885	#
886	# If we're not building ON workspace, then the TOOLSROOT
887	# settings here are clearly ignored by the workspace
888	# makefiles, prepending nonexistent directories to PATH is
889	# harmless, and we clearly do not wish to override
890	# ONBLD_TOOLS.
891	#
892	# If we're building an ON workspace, then the prepended PATH
893	# elements should supercede the preexisting ONBLD_TOOLS paths,
894	# and we want to override ONBLD_TOOLS to catch the tools that
895	# don't have specific path env vars here.
896	#
897	# So the only conditional behavior is overriding ONBLD_TOOLS,
898	# and we check for "an ON workspace" by looking for
899	# ${TOOLSROOT}/opt/onbld.
900	#
901
902	STABS=${TOOLSROOT}/opt/onbld/bin/${MACH}/stabs
903	export STABS
904	CTFSTABS=${TOOLSROOT}/opt/onbld/bin/${MACH}/ctfstabs
905	export CTFSTABS
906	GENOFFSETS=${TOOLSROOT}/opt/onbld/bin/genoffsets
907	export GENOFFSETS
908
909	CTFCONVERT=${TOOLSROOT}/opt/onbld/bin/${MACH}/ctfconvert
910	export CTFCONVERT
911	CTFMERGE=${TOOLSROOT}/opt/onbld/bin/${MACH}/ctfmerge
912	export CTFMERGE
913
914	CTFCVTPTBL=${TOOLSROOT}/opt/onbld/bin/ctfcvtptbl
915	export CTFCVTPTBL
916	CTFFINDMOD=${TOOLSROOT}/opt/onbld/bin/ctffindmod
917	export CTFFINDMOD
918
919	if [ "$VERIFY_ELFSIGN" = "y" ]; then
920		ELFSIGN=${TOOLSROOT}/opt/onbld/bin/elfsigncmp
921	else
922		ELFSIGN=${TOOLSROOT}/opt/onbld/bin/${MACH}/elfsign
923	fi
924	export ELFSIGN
925
926	PATH="${TOOLSROOT}/opt/onbld/bin/${MACH}:${PATH}"
927	PATH="${TOOLSROOT}/opt/onbld/bin:${PATH}"
928	export PATH
929
930	if [ -d "${TOOLSROOT}/opt/onbld" ]; then
931		ONBLD_TOOLS=${TOOLSROOT}/opt/onbld
932		export ONBLD_TOOLS
933	fi
934
935	echo "\n==== New environment settings. ====\n" >> $LOGFILE
936	echo "STABS=${STABS}" >> $LOGFILE
937	echo "CTFSTABS=${CTFSTABS}" >> $LOGFILE
938	echo "CTFCONVERT=${CTFCONVERT}" >> $LOGFILE
939	echo "CTFMERGE=${CTFMERGE}" >> $LOGFILE
940	echo "CTFCVTPTBL=${CTFCVTPTBL}" >> $LOGFILE
941	echo "CTFFINDMOD=${CTFFINDMOD}" >> $LOGFILE
942	echo "ELFSIGN=${ELFSIGN}" >> $LOGFILE
943	echo "PATH=${PATH}" >> $LOGFILE
944	echo "ONBLD_TOOLS=${ONBLD_TOOLS}" >> $LOGFILE
945}
946
947function staffer {
948	if [ $ISUSER -ne 0 ]; then
949		"$@"
950	else
951		arg="\"$1\""
952		shift
953		for i
954		do
955			arg="$arg \"$i\""
956		done
957		eval su $STAFFER -c \'$arg\'
958	fi
959}
960
961#
962# Verify that the closed tree is present if it needs to be.
963# Sets CLOSED_IS_PRESENT for future use.
964#
965function check_closed_tree {
966	if [ -z "$CLOSED_IS_PRESENT" ]; then
967		if [ -d $CODEMGR_WS/usr/closed ]; then
968			CLOSED_IS_PRESENT="yes"
969		else
970			CLOSED_IS_PRESENT="no"
971		fi
972		export CLOSED_IS_PRESENT
973	fi
974	if [[ "$CLOSED_IS_PRESENT" = no && ! -d "$ON_CLOSED_BINS" ]]; then
975		#
976		# If it's an old (pre-split) tree or an empty
977		# workspace, don't complain.
978		#
979		if grep -s CLOSED_BUILD $SRC/Makefile.master > /dev/null; then
980			echo "If the closed sources are not present," \
981			    "ON_CLOSED_BINS"
982			echo "must point to the closed binaries tree."
983			build_ok=n
984			exit 1
985		fi
986	fi
987}
988
989function obsolete_build {
990    	echo "WARNING: Obsolete $1 build requested; request will be ignored"
991}
992
993#
994# wrapper over wsdiff.
995# usage: do_wsdiff LABEL OLDPROTO NEWPROTO
996#
997function do_wsdiff {
998	label=$1
999	oldproto=$2
1000	newproto=$3
1001
1002	wsdiff="wsdiff"
1003	[ "$t_FLAG" = y ] && wsdiff="wsdiff -t"
1004
1005	echo "\n==== Getting object changes since last build at `date`" \
1006	    "($label) ====\n" | tee -a $LOGFILE >> $mail_msg_file
1007	$wsdiff -s -r ${TMPDIR}/wsdiff.results $oldproto $newproto 2>&1 | \
1008		    tee -a $LOGFILE >> $mail_msg_file
1009	echo "\n==== Object changes determined at `date` ($label) ====\n" | \
1010	    tee -a $LOGFILE >> $mail_msg_file
1011}
1012
1013#
1014# Functions for setting build flags (DEBUG/non-DEBUG).  Keep them
1015# together.
1016#
1017
1018function set_non_debug_build_flags {
1019	export INTERNAL_RELEASE_BUILD ; INTERNAL_RELEASE_BUILD=
1020	export RELEASE_BUILD ; RELEASE_BUILD=
1021	unset EXTRA_OPTIONS
1022	unset EXTRA_CFLAGS
1023}
1024
1025function set_debug_build_flags {
1026	export INTERNAL_RELEASE_BUILD ; INTERNAL_RELEASE_BUILD=
1027	unset RELEASE_BUILD
1028	unset EXTRA_OPTIONS
1029	unset EXTRA_CFLAGS
1030}
1031
1032
1033MACH=`uname -p`
1034
1035if [ "$OPTHOME" = "" ]; then
1036	OPTHOME=/opt
1037	export OPTHOME
1038fi
1039if [ "$TEAMWARE" = "" ]; then
1040	TEAMWARE=$OPTHOME/teamware
1041	export TEAMWARE
1042fi
1043
1044USAGE='Usage: nightly [-in] [+t] [-V VERS ] [ -S E|D|H|O ] <env_file>
1045
1046Where:
1047	-i	Fast incremental options (no clobber, lint, check)
1048	-n      Do not do a bringover
1049	+t	Use the build tools in $ONBLD_TOOLS/bin
1050	-V VERS set the build version string to VERS
1051	-S	Build a variant of the source product
1052		E - build exportable source
1053		D - build domestic source (exportable + crypt)
1054		H - build hybrid source (binaries + deleted source)
1055		O - build (only) open source
1056
1057	<env_file>  file in Bourne shell syntax that sets and exports
1058	variables that configure the operation of this script and many of
1059	the scripts this one calls. If <env_file> does not exist,
1060	it will be looked for in $OPTHOME/onbld/env.
1061
1062non-DEBUG is the default build type. Build options can be set in the
1063NIGHTLY_OPTIONS variable in the <env_file> as follows:
1064
1065	-A	check for ABI differences in .so files
1066	-C	check for cstyle/hdrchk errors
1067	-D	do a build with DEBUG on
1068	-F	do _not_ do a non-DEBUG build
1069	-G	gate keeper default group of options (-au)
1070	-I	integration engineer default group of options (-ampu)
1071	-M	do not run pmodes (safe file permission checker)
1072	-N	do not run protocmp
1073	-O	generate OpenSolaris deliverables
1074	-R	default group of options for building a release (-mp)
1075	-U	update proto area in the parent
1076	-V VERS set the build version string to VERS
1077	-X	copy x86 IHV proto area
1078	-f	find unreferenced files
1079	-i	do an incremental build (no "make clobber")
1080	-l	do "make lint" in $LINTDIRS (default: $SRC y)
1081	-m	send mail to $MAILTO at end of build
1082	-n      do not do a bringover
1083	-o	build using root privileges to set OWNER/GROUP (old style)
1084	-p	create packages
1085	-r	check ELF runtime attributes in the proto area
1086	-t	build and use the tools in $SRC/tools (default setting)
1087	+t	Use the build tools in $ONBLD_TOOLS/bin
1088	-u	update proto_list_$MACH and friends in the parent workspace;
1089		when used with -f, also build an unrefmaster.out in the parent
1090	-w	report on differences between previous and current proto areas
1091	-z	compress cpio archives with gzip
1092	-W	Do not report warnings (freeware gate ONLY)
1093	-S	Build a variant of the source product
1094		E - build exportable source
1095		D - build domestic source (exportable + crypt)
1096		H - build hybrid source (binaries + deleted source)
1097		O - build (only) open source
1098'
1099#
1100#	-x	less public handling of xmod source for the source product
1101#
1102#	A log file will be generated under the name $LOGFILE
1103#	for partially completed build and log.`date '+%F'`
1104#	in the same directory for fully completed builds.
1105#
1106
1107# default values for low-level FLAGS; G I R are group FLAGS
1108A_FLAG=n
1109C_FLAG=n
1110D_FLAG=n
1111F_FLAG=n
1112f_FLAG=n
1113i_FLAG=n; i_CMD_LINE_FLAG=n
1114l_FLAG=n
1115M_FLAG=n
1116m_FLAG=n
1117N_FLAG=n
1118n_FLAG=n
1119O_FLAG=n
1120o_FLAG=n
1121P_FLAG=n
1122p_FLAG=n
1123r_FLAG=n
1124T_FLAG=n
1125t_FLAG=y
1126U_FLAG=n
1127u_FLAG=n
1128V_FLAG=n
1129W_FLAG=n
1130w_FLAG=n
1131X_FLAG=n
1132SD_FLAG=n
1133SE_FLAG=n
1134SH_FLAG=n
1135SO_FLAG=n
1136#
1137XMOD_OPT=
1138#
1139build_ok=y
1140
1141function is_source_build {
1142	[ "$SE_FLAG" = "y" -o "$SD_FLAG" = "y" -o \
1143	    "$SH_FLAG" = "y" -o "$SO_FLAG" = "y" ]
1144	return $?
1145}
1146
1147#
1148# examine arguments
1149#
1150
1151#
1152# single function for setting -S flag and doing error checking.
1153# usage: set_S_flag <type>
1154# where <type> is the source build type ("E", "D", ...).
1155#
1156function set_S_flag {
1157	if is_source_build; then
1158		echo "Can only build one source variant at a time."
1159		exit 1
1160	fi
1161	if [ "$1" = "E" ]; then
1162		SE_FLAG=y
1163	elif [ "$1" = "D" ]; then
1164		SD_FLAG=y
1165	elif [ "$1" = "H" ]; then
1166		SH_FLAG=y
1167	elif [ "$1" = "O" ]; then
1168		SO_FLAG=y
1169	else
1170		echo "$USAGE"
1171		exit 1
1172	fi
1173}
1174
1175OPTIND=1
1176while getopts +inS:tV: FLAG
1177do
1178	case $FLAG in
1179	  i )	i_FLAG=y; i_CMD_LINE_FLAG=y
1180		;;
1181	  n )	n_FLAG=y
1182		;;
1183	  S )
1184		set_S_flag $OPTARG
1185		;;
1186	 +t )	t_FLAG=n
1187		;;
1188	  V )	V_FLAG=y
1189		V_ARG="$OPTARG"
1190		;;
1191	 \? )	echo "$USAGE"
1192		exit 1
1193		;;
1194	esac
1195done
1196
1197# correct argument count after options
1198shift `expr $OPTIND - 1`
1199
1200# test that the path to the environment-setting file was given
1201if [ $# -ne 1 ]; then
1202	echo "$USAGE"
1203	exit 1
1204fi
1205
1206# check if user is running nightly as root
1207# ISUSER is set non-zero if an ordinary user runs nightly, or is zero
1208# when root invokes nightly.
1209/usr/bin/id | grep '^uid=0(' >/dev/null 2>&1
1210ISUSER=$?;	export ISUSER
1211
1212#
1213# force locale to C
1214LC_COLLATE=C;	export LC_COLLATE
1215LC_CTYPE=C;	export LC_CTYPE
1216LC_MESSAGES=C;	export LC_MESSAGES
1217LC_MONETARY=C;	export LC_MONETARY
1218LC_NUMERIC=C;	export LC_NUMERIC
1219LC_TIME=C;	export LC_TIME
1220
1221# clear environment variables we know to be bad for the build
1222unset LD_OPTIONS
1223unset LD_AUDIT		LD_AUDIT_32		LD_AUDIT_64
1224unset LD_BIND_NOW	LD_BIND_NOW_32		LD_BIND_NOW_64
1225unset LD_BREADTH	LD_BREADTH_32		LD_BREADTH_64
1226unset LD_CONFIG		LD_CONFIG_32		LD_CONFIG_64
1227unset LD_DEBUG		LD_DEBUG_32		LD_DEBUG_64
1228unset LD_DEMANGLE	LD_DEMANGLE_32		LD_DEMANGLE_64
1229unset LD_FLAGS		LD_FLAGS_32		LD_FLAGS_64
1230unset LD_LIBRARY_PATH	LD_LIBRARY_PATH_32	LD_LIBRARY_PATH_64
1231unset LD_LOADFLTR	LD_LOADFLTR_32		LD_LOADFLTR_64
1232unset LD_NOAUDIT	LD_NOAUDIT_32		LD_NOAUDIT_64
1233unset LD_NOAUXFLTR	LD_NOAUXFLTR_32		LD_NOAUXFLTR_64
1234unset LD_NOCONFIG	LD_NOCONFIG_32		LD_NOCONFIG_64
1235unset LD_NODIRCONFIG	LD_NODIRCONFIG_32	LD_NODIRCONFIG_64
1236unset LD_NODIRECT	LD_NODIRECT_32		LD_NODIRECT_64
1237unset LD_NOLAZYLOAD	LD_NOLAZYLOAD_32	LD_NOLAZYLOAD_64
1238unset LD_NOOBJALTER	LD_NOOBJALTER_32	LD_NOOBJALTER_64
1239unset LD_NOVERSION	LD_NOVERSION_32		LD_NOVERSION_64
1240unset LD_ORIGIN		LD_ORIGIN_32		LD_ORIGIN_64
1241unset LD_PRELOAD	LD_PRELOAD_32		LD_PRELOAD_64
1242unset LD_PROFILE	LD_PROFILE_32		LD_PROFILE_64
1243
1244unset CONFIG
1245unset GROUP
1246unset OWNER
1247unset REMOTE
1248unset ENV
1249unset ARCH
1250unset CLASSPATH
1251unset NAME
1252
1253#
1254# To get ONBLD_TOOLS from the environment, it must come from the env file.
1255# If it comes interactively, it is generally TOOLS_PROTO, which will be
1256# clobbered before the compiler version checks, which will therefore fail.
1257#
1258unset ONBLD_TOOLS
1259
1260#
1261#	Setup environmental variables
1262#
1263if [ -f /etc/nightly.conf ]; then
1264	. /etc/nightly.conf
1265fi
1266
1267if [ -f $1 ]; then
1268	if [[ $1 = */* ]]; then
1269		. $1
1270	else
1271		. ./$1
1272	fi
1273else
1274	if [ -f $OPTHOME/onbld/env/$1 ]; then
1275		. $OPTHOME/onbld/env/$1
1276	else
1277		echo "Cannot find env file as either $1 or $OPTHOME/onbld/env/$1"
1278		exit 1
1279	fi
1280fi
1281
1282# contents of stdenv.sh inserted after next line:
1283# STDENV_START
1284# STDENV_END
1285
1286# Check if we have sufficient data to continue...
1287[[ -v CODEMGR_WS ]] || fatal_error "Error: Variable CODEMGR_WS not set."
1288[[ -d "${CODEMGR_WS}" ]] || fatal_error "Error: ${CODEMGR_WS} is not a directory."
1289[[ -f "${CODEMGR_WS}/usr/src/Makefile" ]] || fatal_error "Error: ${CODEMGR_WS}/usr/src/Makefile not found."
1290
1291#
1292# place ourselves in a new task, respecting BUILD_PROJECT if set.
1293#
1294if [ -z "$BUILD_PROJECT" ]; then
1295	/usr/bin/newtask -c $$
1296else
1297	/usr/bin/newtask -c $$ -p $BUILD_PROJECT
1298fi
1299
1300ps -o taskid= -p $$ | read build_taskid
1301ps -o project= -p $$ | read build_project
1302
1303#
1304# See if NIGHTLY_OPTIONS is set
1305#
1306if [ "$NIGHTLY_OPTIONS" = "" ]; then
1307	NIGHTLY_OPTIONS="-aBm"
1308fi
1309
1310#
1311# If BRINGOVER_WS was not specified, let it default to CLONE_WS
1312#
1313if [ "$BRINGOVER_WS" = "" ]; then
1314	BRINGOVER_WS=$CLONE_WS
1315fi
1316
1317#
1318# If CLOSED_BRINGOVER_WS was not specified, let it default to CLOSED_CLONE_WS
1319#
1320if [ "$CLOSED_BRINGOVER_WS" = "" ]; then
1321	CLOSED_BRINGOVER_WS=$CLOSED_CLONE_WS
1322fi
1323
1324#
1325# If BRINGOVER_FILES was not specified, default to usr
1326#
1327if [ "$BRINGOVER_FILES" = "" ]; then
1328	BRINGOVER_FILES="usr"
1329fi
1330
1331#
1332# If the closed sources are not present, the closed binaries must be
1333# present for the build to succeed.  If there's no pointer to the
1334# closed binaries, flag that now, rather than forcing the user to wait
1335# a couple hours (or more) to find out.
1336#
1337orig_closed_is_present="$CLOSED_IS_PRESENT"
1338check_closed_tree
1339
1340#
1341# Note: changes to the option letters here should also be applied to the
1342#	bldenv script.  `d' is listed for backward compatibility.
1343#
1344NIGHTLY_OPTIONS=-${NIGHTLY_OPTIONS#-}
1345OPTIND=1
1346while getopts +ABCDdFfGIilMmNnOoPpRrS:TtUuWwXxz FLAG $NIGHTLY_OPTIONS
1347do
1348	case $FLAG in
1349	  A )	A_FLAG=y
1350		#
1351		# If ELF_DATA_BASELINE_DIR is not defined, and we are on SWAN
1352		# (based on CLOSED_IS_PRESENT), then refuse to run. The value
1353		# of ELF version checking is greatly enhanced by including
1354		# the baseline gate comparison.
1355		if [ "$CLOSED_IS_PRESENT" = 'yes' -a \
1356		     "$ELF_DATA_BASELINE_DIR" = '' ]; then
1357			echo "ELF_DATA_BASELINE_DIR must be set if the A" \
1358			    "flag is present in\nNIGHTLY_OPTIONS and closed" \
1359			    "sources are present. Update environment file."
1360			exit 1;
1361		fi
1362		;;
1363	  B )	D_FLAG=y
1364		;; # old version of D
1365	  C )	C_FLAG=y
1366		;;
1367	  D )	D_FLAG=y
1368		;;
1369	  F )	F_FLAG=y
1370		;;
1371	  f )	f_FLAG=y
1372		;;
1373	  G )   u_FLAG=y
1374		;;
1375	  I )	m_FLAG=y
1376		p_FLAG=y
1377		u_FLAG=y
1378		;;
1379	  i )	i_FLAG=y
1380		;;
1381	  l )	l_FLAG=y
1382		;;
1383	  M )	M_FLAG=y
1384		;;
1385	  m )	m_FLAG=y
1386		;;
1387	  N )	N_FLAG=y
1388		;;
1389	  n )	n_FLAG=y
1390		;;
1391	  O )	O_FLAG=y
1392		;;
1393	  o )	o_FLAG=y
1394		;;
1395	  P )	P_FLAG=y
1396		;; # obsolete
1397	  p )	p_FLAG=y
1398		;;
1399	  R )	m_FLAG=y
1400		p_FLAG=y
1401		;;
1402	  r )	r_FLAG=y
1403		;;
1404	  S )
1405		set_S_flag $OPTARG
1406		;;
1407	  T )	T_FLAG=y
1408		;; # obsolete
1409	 +t )	t_FLAG=n
1410		;;
1411	  U )   if [ -z "${PARENT_ROOT}" ]; then
1412			echo "PARENT_ROOT must be set if the U flag is" \
1413			    "present in NIGHTLY_OPTIONS."
1414			exit 1
1415		fi
1416		NIGHTLY_PARENT_ROOT=$PARENT_ROOT
1417		if [ -n "${PARENT_TOOLS_ROOT}" ]; then
1418			NIGHTLY_PARENT_TOOLS_ROOT=$PARENT_TOOLS_ROOT
1419		fi
1420		U_FLAG=y
1421		;;
1422	  u )	u_FLAG=y
1423		;;
1424	  W )	W_FLAG=y
1425		;;
1426
1427	  w )	w_FLAG=y
1428		;;
1429	  X )	# now that we no longer need realmode builds, just
1430		# copy IHV packages.  only meaningful on x86.
1431		if [ "$MACH" = "i386" ]; then
1432			X_FLAG=y
1433		fi
1434		;;
1435	  x )	XMOD_OPT="-x"
1436		;;
1437	 \? )	echo "$USAGE"
1438		exit 1
1439		;;
1440	esac
1441done
1442
1443if [ $ISUSER -ne 0 ]; then
1444	if [ "$o_FLAG" = "y" ]; then
1445		echo "Old-style build requires root permission."
1446		exit 1
1447	fi
1448
1449	# Set default value for STAFFER, if needed.
1450	if [ -z "$STAFFER" -o "$STAFFER" = "nobody" ]; then
1451		STAFFER=`/usr/xpg4/bin/id -un`
1452		export STAFFER
1453	fi
1454fi
1455
1456if [ -z "$MAILTO" -o "$MAILTO" = "nobody" ]; then
1457	MAILTO=$STAFFER
1458	export MAILTO
1459fi
1460
1461PATH="$OPTHOME/onbld/bin:$OPTHOME/onbld/bin/${MACH}:/usr/ccs/bin"
1462PATH="$PATH:$OPTHOME/SUNWspro/bin:$TEAMWARE/bin:/usr/bin:/usr/sbin:/usr/ucb"
1463PATH="$PATH:/usr/openwin/bin:/usr/sfw/bin:/opt/sfw/bin:."
1464export PATH
1465
1466# roots of source trees, both relative to $SRC and absolute.
1467relsrcdirs="."
1468if [[ -d $CODEMGR_WS/usr/closed && "$CLOSED_IS_PRESENT" != no ]]; then
1469	relsrcdirs="$relsrcdirs ../closed"
1470fi
1471abssrcdirs=""
1472for d in $relsrcdirs; do
1473	abssrcdirs="$abssrcdirs $SRC/$d"
1474done
1475
1476unset CH
1477if [ "$o_FLAG" = "y" ]; then
1478# root invoked old-style build -- make sure it works as it always has
1479# by exporting 'CH'.  The current Makefile.master doesn't use this, but
1480# the old ones still do.
1481	PROTOCMPTERSE="protocmp.terse"
1482	CH=
1483	export CH
1484else
1485	PROTOCMPTERSE="protocmp.terse -gu"
1486fi
1487POUND_SIGN="#"
1488# have we set RELEASE_DATE in our env file?
1489if [ -z "$RELEASE_DATE" ]; then
1490	RELEASE_DATE=$(LC_ALL=C date +"%B %Y")
1491fi
1492BUILD_DATE=$(LC_ALL=C date +%Y-%b-%d)
1493BASEWSDIR=$(basename $CODEMGR_WS)
1494DEV_CM="\"@(#)SunOS Internal Development: $LOGNAME $BUILD_DATE [$BASEWSDIR]\""
1495
1496# we export POUND_SIGN, RELEASE_DATE and DEV_CM to speed up the build process
1497# by avoiding repeated shell invocations to evaluate Makefile.master definitions.
1498# we export o_FLAG and X_FLAG for use by makebfu, and by usr/src/pkg/Makefile
1499export o_FLAG X_FLAG POUND_SIGN RELEASE_DATE DEV_CM
1500
1501maketype="distributed"
1502MAKE=dmake
1503# get the dmake version string alone
1504DMAKE_VERSION=$( $MAKE -v )
1505DMAKE_VERSION=${DMAKE_VERSION#*: }
1506# focus in on just the dotted version number alone
1507DMAKE_MAJOR=$( echo $DMAKE_VERSION | \
1508	sed -e 's/.*\<\([^.]*\.[^   ]*\).*$/\1/' )
1509# extract the second (or final) integer
1510DMAKE_MINOR=${DMAKE_MAJOR#*.}
1511DMAKE_MINOR=${DMAKE_MINOR%%.*}
1512# extract the first integer
1513DMAKE_MAJOR=${DMAKE_MAJOR%%.*}
1514CHECK_DMAKE=${CHECK_DMAKE:-y}
1515# x86 was built on the 12th, sparc on the 13th.
1516if [ "$CHECK_DMAKE" = "y" -a \
1517     "$DMAKE_VERSION" != "Sun Distributed Make 7.3 2003/03/12" -a \
1518     "$DMAKE_VERSION" != "Sun Distributed Make 7.3 2003/03/13" -a \( \
1519     "$DMAKE_MAJOR" -lt 7 -o \
1520     "$DMAKE_MAJOR" -eq 7 -a "$DMAKE_MINOR" -lt 4 \) ]; then
1521	if [ -z "$DMAKE_VERSION" ]; then
1522		echo "$MAKE is missing."
1523		exit 1
1524	fi
1525	echo `whence $MAKE`" version is:"
1526	echo "  ${DMAKE_VERSION}"
1527	cat <<EOF
1528
1529This version may not be safe for use.  Either set TEAMWARE to a better
1530path or (if you really want to use this version of dmake anyway), add
1531the following to your environment to disable this check:
1532
1533  CHECK_DMAKE=n
1534EOF
1535	exit 1
1536fi
1537export PATH
1538export MAKE
1539
1540if [[ "$O_FLAG" = y ]]; then
1541	export TONICBUILD=""
1542else
1543	export TONICBUILD="#"
1544fi
1545
1546if [ "${SUNWSPRO}" != "" ]; then
1547	PATH="${SUNWSPRO}/bin:$PATH"
1548	export PATH
1549fi
1550
1551hostname=$(uname -n)
1552if [[ $DMAKE_MAX_JOBS != +([0-9]) || $DMAKE_MAX_JOBS -eq 0 ]]
1553then
1554	maxjobs=
1555	if [[ -f $HOME/.make.machines ]]
1556	then
1557		# Note: there is a hard tab and space character in the []s
1558		# below.
1559		egrep -i "^[ 	]*$hostname[ 	\.]" \
1560			$HOME/.make.machines | read host jobs
1561		maxjobs=${jobs##*=}
1562	fi
1563
1564	if [[ $maxjobs != +([0-9]) || $maxjobs -eq 0 ]]
1565	then
1566		# default
1567		maxjobs=4
1568	fi
1569
1570	export DMAKE_MAX_JOBS=$maxjobs
1571fi
1572
1573DMAKE_MODE=parallel;
1574export DMAKE_MODE
1575
1576if [ -z "${ROOT}" ]; then
1577	echo "ROOT must be set."
1578	exit 1
1579fi
1580
1581#
1582# if -V flag was given, reset VERSION to V_ARG
1583#
1584if [ "$V_FLAG" = "y" ]; then
1585	VERSION=$V_ARG
1586fi
1587
1588#
1589# Check for IHV root for copying ihv proto area
1590#
1591if [ "$X_FLAG" = "y" ]; then
1592        if [ "$IA32_IHV_ROOT" = "" ]; then
1593		echo "IA32_IHV_ROOT: must be set for copying ihv proto"
1594		args_ok=n
1595        fi
1596        if [ ! -d "$IA32_IHV_ROOT" ]; then
1597                echo "$IA32_IHV_ROOT: not found"
1598                args_ok=n
1599        fi
1600        if [ "$IA32_IHV_WS" = "" ]; then
1601		echo "IA32_IHV_WS: must be set for copying ihv proto"
1602		args_ok=n
1603        fi
1604        if [ ! -d "$IA32_IHV_WS" ]; then
1605                echo "$IA32_IHV_WS: not found"
1606                args_ok=n
1607        fi
1608fi
1609
1610# Append source version
1611if [ "$SE_FLAG" = "y" ]; then
1612	VERSION="${VERSION}:EXPORT"
1613fi
1614
1615if [ "$SD_FLAG" = "y" ]; then
1616	VERSION="${VERSION}:DOMESTIC"
1617fi
1618
1619if [ "$SH_FLAG" = "y" ]; then
1620	VERSION="${VERSION}:MODIFIED_SOURCE_PRODUCT"
1621fi
1622
1623if [ "$SO_FLAG" = "y" ]; then
1624	VERSION="${VERSION}:OPEN_ONLY"
1625fi
1626
1627TMPDIR="/tmp/nightly.tmpdir.$$"
1628export TMPDIR
1629rm -rf ${TMPDIR}
1630mkdir -p $TMPDIR || exit 1
1631chmod 777 $TMPDIR
1632
1633#
1634# Keep elfsign's use of pkcs11_softtoken from looking in the user home
1635# directory, which doesn't always work.   Needed until all build machines
1636# have the fix for 6271754
1637#
1638SOFTTOKEN_DIR=$TMPDIR
1639export SOFTTOKEN_DIR
1640
1641#
1642# Tools should only be built non-DEBUG.  Keep track of the tools proto
1643# area path relative to $TOOLS, because the latter changes in an
1644# export build.
1645#
1646# TOOLS_PROTO is included below for builds other than usr/src/tools
1647# that look for this location.  For usr/src/tools, this will be
1648# overridden on the $MAKE command line in build_tools().
1649#
1650TOOLS=${SRC}/tools
1651TOOLS_PROTO_REL=proto/root_${MACH}-nd
1652TOOLS_PROTO=${TOOLS}/${TOOLS_PROTO_REL};	export TOOLS_PROTO
1653
1654unset   CFLAGS LD_LIBRARY_PATH LDFLAGS
1655
1656# create directories that are automatically removed if the nightly script
1657# fails to start correctly
1658function newdir {
1659	dir=$1
1660	toadd=
1661	while [ ! -d $dir ]; do
1662		toadd="$dir $toadd"
1663		dir=`dirname $dir`
1664	done
1665	torm=
1666	newlist=
1667	for dir in $toadd; do
1668		if staffer mkdir $dir; then
1669			newlist="$ISUSER $dir $newlist"
1670			torm="$dir $torm"
1671		else
1672			[ -z "$torm" ] || staffer rmdir $torm
1673			return 1
1674		fi
1675	done
1676	newdirlist="$newlist $newdirlist"
1677	return 0
1678}
1679newdirlist=
1680
1681[ -d $CODEMGR_WS ] || newdir $CODEMGR_WS || exit 1
1682
1683# since this script assumes the build is from full source, it nullifies
1684# variables likely to have been set by a "ws" script; nullification
1685# confines the search space for headers and libraries to the proto area
1686# built from this immediate source.
1687ENVLDLIBS1=
1688ENVLDLIBS2=
1689ENVLDLIBS3=
1690ENVCPPFLAGS1=
1691ENVCPPFLAGS2=
1692ENVCPPFLAGS3=
1693ENVCPPFLAGS4=
1694PARENT_ROOT=
1695
1696export ENVLDLIBS3 ENVCPPFLAGS1 ENVCPPFLAGS2 ENVCPPFLAGS3 ENVCPPFLAGS4 \
1697	PARENT_ROOT
1698
1699PKGARCHIVE_ORIG=$PKGARCHIVE
1700IA32_IHV_PKGS_ORIG=$IA32_IHV_PKGS
1701
1702#
1703# Juggle the logs and optionally send mail on completion.
1704#
1705
1706function logshuffle {
1707    	LLOG="$ATLOG/log.`date '+%F.%H:%M'`"
1708	if [ -f $LLOG -o -d $LLOG ]; then
1709	    	LLOG=$LLOG.$$
1710	fi
1711	mkdir $LLOG
1712	export LLOG
1713
1714	if [ "$build_ok" = "y" ]; then
1715		mv $ATLOG/proto_list_${MACH} $LLOG
1716
1717		if [ -f $ATLOG/proto_list_tools_${MACH} ]; then
1718			mv $ATLOG/proto_list_tools_${MACH} $LLOG
1719	        fi
1720
1721		if [ -f $TMPDIR/wsdiff.results ]; then
1722		    	mv $TMPDIR/wsdiff.results $LLOG
1723		fi
1724
1725		if [ -f $TMPDIR/wsdiff-nd.results ]; then
1726			mv $TMPDIR/wsdiff-nd.results $LLOG
1727		fi
1728	fi
1729
1730	#
1731	# Now that we're about to send mail, it's time to check the noise
1732	# file.  In the event that an error occurs beyond this point, it will
1733	# be recorded in the nightly.log file, but nowhere else.  This would
1734	# include only errors that cause the copying of the noise log to fail
1735	# or the mail itself not to be sent.
1736	#
1737
1738	exec >>$LOGFILE 2>&1
1739	if [ -s $build_noise_file ]; then
1740	    	echo "\n==== Nightly build noise ====\n" |
1741		    tee -a $LOGFILE >>$mail_msg_file
1742		cat $build_noise_file >>$LOGFILE
1743		cat $build_noise_file >>$mail_msg_file
1744		echo | tee -a $LOGFILE >>$mail_msg_file
1745	fi
1746	rm -f $build_noise_file
1747
1748	case "$build_ok" in
1749		y)
1750			state=Completed
1751			;;
1752		i)
1753			state=Interrupted
1754			;;
1755		*)
1756	    		state=Failed
1757			;;
1758	esac
1759	NIGHTLY_STATUS=$state
1760	export NIGHTLY_STATUS
1761
1762	run_hook POST_NIGHTLY $state
1763	run_hook SYS_POST_NIGHTLY $state
1764
1765	cat $build_time_file $build_environ_file $mail_msg_file \
1766	    > ${LLOG}/mail_msg
1767	if [ "$m_FLAG" = "y" ]; then
1768	    	cat ${LLOG}/mail_msg | /usr/bin/mailx -s \
1769	"Nightly ${MACH} Build of `basename ${CODEMGR_WS}` ${state}." \
1770			${MAILTO}
1771	fi
1772
1773	if [ "$u_FLAG" = "y" -a "$build_ok" = "y" ]; then
1774	    	staffer cp ${LLOG}/mail_msg $PARENT_WS/usr/src/mail_msg-${MACH}
1775		staffer cp $LOGFILE $PARENT_WS/usr/src/nightly-${MACH}.log
1776	fi
1777
1778	mv $LOGFILE $LLOG
1779}
1780
1781#
1782#	Remove the locks and temporary files on any exit
1783#
1784function cleanup {
1785    	logshuffle
1786
1787	[ -z "$lockfile" ] || staffer rm -f $lockfile
1788	[ -z "$atloglockfile" ] || rm -f $atloglockfile
1789	[ -z "$ulockfile" ] || staffer rm -f $ulockfile
1790	[ -z "$Ulockfile" ] || rm -f $Ulockfile
1791
1792	set -- $newdirlist
1793	while [ $# -gt 0 ]; do
1794		ISUSER=$1 staffer rmdir $2
1795		shift; shift
1796	done
1797	rm -rf $TMPDIR
1798}
1799
1800function cleanup_signal {
1801    	build_ok=i
1802	# this will trigger cleanup(), above.
1803	exit 1
1804}
1805
1806trap cleanup 0
1807trap cleanup_signal 1 2 3 15
1808
1809#
1810# Generic lock file processing -- make sure that the lock file doesn't
1811# exist.  If it does, it should name the build host and PID.  If it
1812# doesn't, then make sure we can create it.  Clean up locks that are
1813# known to be stale (assumes host name is unique among build systems
1814# for the workspace).
1815#
1816function create_lock {
1817	lockf=$1
1818	lockvar=$2
1819
1820	ldir=`dirname $lockf`
1821	[ -d $ldir ] || newdir $ldir || exit 1
1822	eval $lockvar=$lockf
1823
1824	while ! staffer ln -s $hostname.$STAFFER.$$ $lockf 2> /dev/null; do
1825		basews=`basename $CODEMGR_WS`
1826		ls -l $lockf | nawk '{print $NF}' | IFS=. read host user pid
1827		if [ "$host" != "$hostname" ]; then
1828			echo "$MACH build of $basews apparently" \
1829			    "already started by $user on $host as $pid."
1830			exit 1
1831		elif kill -s 0 $pid 2>/dev/null; then
1832			echo "$MACH build of $basews already started" \
1833			    "by $user as $pid."
1834			exit 1
1835		else
1836			# stale lock; clear it out and try again
1837			rm -f $lockf
1838		fi
1839	done
1840}
1841
1842#
1843# Return the list of interesting proto areas, depending on the current
1844# options.
1845#
1846function allprotos {
1847	typeset roots="$ROOT"
1848
1849	if [[ "$F_FLAG" = n && "$MULTI_PROTO" = yes ]]; then
1850		roots="$roots $ROOT-nd"
1851	fi
1852
1853	if [[ $O_FLAG = y ]]; then
1854		roots="$roots $ROOT-closed"
1855		[ $MULTI_PROTO = yes ] && roots="$roots $ROOT-nd-closed"
1856	fi
1857
1858	echo $roots
1859}
1860
1861# Ensure no other instance of this script is running on this host.
1862# LOCKNAME can be set in <env_file>, and is by default, but is not
1863# required due to the use of $ATLOG below.
1864if [ -n "$LOCKNAME" ]; then
1865	create_lock /tmp/$LOCKNAME "lockfile"
1866fi
1867#
1868# Create from one, two, or three other locks:
1869#	$ATLOG/nightly.lock
1870#		- protects against multiple builds in same workspace
1871#	$PARENT_WS/usr/src/nightly.$MACH.lock
1872#		- protects against multiple 'u' copy-backs
1873#	$NIGHTLY_PARENT_ROOT/nightly.lock
1874#		- protects against multiple 'U' copy-backs
1875#
1876# Overriding ISUSER to 1 causes the lock to be created as root if the
1877# script is run as root.  The default is to create it as $STAFFER.
1878ISUSER=1 create_lock $ATLOG/nightly.lock "atloglockfile"
1879if [ "$u_FLAG" = "y" ]; then
1880	create_lock $PARENT_WS/usr/src/nightly.$MACH.lock "ulockfile"
1881fi
1882if [ "$U_FLAG" = "y" ]; then
1883	# NIGHTLY_PARENT_ROOT is written as root if script invoked as root.
1884	ISUSER=1 create_lock $NIGHTLY_PARENT_ROOT/nightly.lock "Ulockfile"
1885fi
1886
1887# Locks have been taken, so we're doing a build and we're committed to
1888# the directories we may have created so far.
1889newdirlist=
1890
1891#
1892# Create mail_msg_file
1893#
1894mail_msg_file="${TMPDIR}/mail_msg"
1895touch $mail_msg_file
1896build_time_file="${TMPDIR}/build_time"
1897build_environ_file="${TMPDIR}/build_environ"
1898touch $build_environ_file
1899#
1900#	Move old LOGFILE aside
1901#	ATLOG directory already made by 'create_lock' above
1902#
1903if [ -f $LOGFILE ]; then
1904	mv -f $LOGFILE ${LOGFILE}-
1905fi
1906#
1907#	Build OsNet source
1908#
1909START_DATE=`date`
1910SECONDS=0
1911echo "\n==== Nightly $maketype build started:   $START_DATE ====" \
1912    | tee -a $LOGFILE > $build_time_file
1913
1914echo "\nBuild project:  $build_project\nBuild taskid:   $build_taskid" | \
1915    tee -a $mail_msg_file >> $LOGFILE
1916
1917# make sure we log only to the nightly build file
1918build_noise_file="${TMPDIR}/build_noise"
1919exec </dev/null >$build_noise_file 2>&1
1920
1921run_hook SYS_PRE_NIGHTLY
1922run_hook PRE_NIGHTLY
1923
1924echo "\n==== list of environment variables ====\n" >> $LOGFILE
1925env >> $LOGFILE
1926
1927echo "\n==== Nightly argument issues ====\n" | tee -a $mail_msg_file >> $LOGFILE
1928
1929if [ "$P_FLAG" = "y" ]; then
1930	obsolete_build GPROF | tee -a $mail_msg_file >> $LOGFILE
1931fi
1932
1933if [ "$T_FLAG" = "y" ]; then
1934	obsolete_build TRACE | tee -a $mail_msg_file >> $LOGFILE
1935fi
1936
1937if is_source_build; then
1938	if [ "$i_FLAG" = "y" -o "$i_CMD_LINE_FLAG" = "y" ]; then
1939		echo "WARNING: the -S flags do not support incremental" \
1940		    "builds; forcing clobber\n" | tee -a $mail_msg_file >> $LOGFILE
1941		i_FLAG=n
1942		i_CMD_LINE_FLAG=n
1943	fi
1944	if [ "$N_FLAG" = "n" ]; then
1945		echo "WARNING: the -S flags do not support protocmp;" \
1946		    "protocmp disabled\n" | \
1947		    tee -a $mail_msg_file >> $LOGFILE
1948		N_FLAG=y
1949	fi
1950	if [ "$l_FLAG" = "y" ]; then
1951		echo "WARNING: the -S flags do not support lint;" \
1952		    "lint disabled\n" | tee -a $mail_msg_file >> $LOGFILE
1953		l_FLAG=n
1954	fi
1955	if [ "$C_FLAG" = "y" ]; then
1956		echo "WARNING: the -S flags do not support cstyle;" \
1957		    "cstyle check disabled\n" | tee -a $mail_msg_file >> $LOGFILE
1958		C_FLAG=n
1959	fi
1960else
1961	if [ "$N_FLAG" = "y" ]; then
1962		if [ "$p_FLAG" = "y" ]; then
1963			cat <<EOF | tee -a $mail_msg_file >> $LOGFILE
1964WARNING: the p option (create packages) is set, but so is the N option (do
1965         not run protocmp); this is dangerous; you should unset the N option
1966EOF
1967		else
1968			cat <<EOF | tee -a $mail_msg_file >> $LOGFILE
1969Warning: the N option (do not run protocmp) is set; it probably shouldn't be
1970EOF
1971		fi
1972		echo "" | tee -a $mail_msg_file >> $LOGFILE
1973	fi
1974fi
1975
1976if [ "$D_FLAG" = "n" -a "$l_FLAG" = "y" ]; then
1977	#
1978	# In the past we just complained but went ahead with the lint
1979	# pass, even though the proto area was built non-DEBUG.  It's
1980	# unlikely that non-DEBUG headers will make a difference, but
1981	# rather than assuming it's a safe combination, force the user
1982	# to specify a DEBUG build.
1983	#
1984	echo "WARNING: DEBUG build not requested; disabling lint.\n" \
1985	    | tee -a $mail_msg_file >> $LOGFILE
1986	l_FLAG=n
1987fi
1988
1989if [ "$f_FLAG" = "y" ]; then
1990	if [ "$i_FLAG" = "y" ]; then
1991		echo "WARNING: the -f flag cannot be used during incremental" \
1992		    "builds; ignoring -f\n" | tee -a $mail_msg_file >> $LOGFILE
1993		f_FLAG=n
1994	fi
1995	if [ "${l_FLAG}${p_FLAG}" != "yy" ]; then
1996		echo "WARNING: the -f flag requires -l, and -p;" \
1997		    "ignoring -f\n" | tee -a $mail_msg_file >> $LOGFILE
1998		f_FLAG=n
1999	fi
2000fi
2001
2002if [ "$w_FLAG" = "y" -a ! -d $ROOT ]; then
2003	echo "WARNING: -w specified, but $ROOT does not exist;" \
2004	    "ignoring -w\n" | tee -a $mail_msg_file >> $LOGFILE
2005	w_FLAG=n
2006fi
2007
2008if [ "$t_FLAG" = "n" ]; then
2009	#
2010	# We're not doing a tools build, so make sure elfsign(1) is
2011	# new enough to safely sign non-crypto binaries.  We test
2012	# debugging output from elfsign to detect the old version.
2013	#
2014	newelfsigntest=`SUNW_CRYPTO_DEBUG=stderr /usr/bin/elfsign verify \
2015	    -e /usr/lib/security/pkcs11_softtoken.so.1 2>&1 \
2016	    | egrep algorithmOID`
2017	if [ -z "$newelfsigntest" ]; then
2018		echo "WARNING: /usr/bin/elfsign out of date;" \
2019		    "will only sign crypto modules\n" | \
2020		    tee -a $mail_msg_file >> $LOGFILE
2021		export ELFSIGN_OBJECT=true
2022	elif [ "$VERIFY_ELFSIGN" = "y" ]; then
2023		echo "WARNING: VERIFY_ELFSIGN=y requires" \
2024		    "the -t flag; ignoring VERIFY_ELFSIGN\n" | \
2025		    tee -a $mail_msg_file >> $LOGFILE
2026	fi
2027fi
2028
2029[ "$O_FLAG" = y ] && MULTI_PROTO=yes
2030
2031case $MULTI_PROTO in
2032yes|no)	;;
2033*)
2034	echo "WARNING: MULTI_PROTO is \"$MULTI_PROTO\"; " \
2035	    "should be \"yes\" or \"no\"." | tee -a $mail_msg_file >> $LOGFILE
2036	echo "Setting MULTI_PROTO to \"no\".\n" | \
2037	    tee -a $mail_msg_file >> $LOGFILE
2038	export MULTI_PROTO=no
2039	;;
2040esac
2041
2042echo "\n==== Build version ====\n" | tee -a $mail_msg_file >> $LOGFILE
2043echo $VERSION | tee -a $mail_msg_file >> $LOGFILE
2044
2045# Save the current proto area if we're comparing against the last build
2046if [ "$w_FLAG" = "y" -a -d "$ROOT" ]; then
2047    if [ -d "$ROOT.prev" ]; then
2048	rm -rf $ROOT.prev
2049    fi
2050    mv $ROOT $ROOT.prev
2051fi
2052
2053# Same for non-DEBUG proto area
2054if [ "$w_FLAG" = "y" -a "$MULTI_PROTO" = yes -a -d "$ROOT-nd" ]; then
2055	if [ -d "$ROOT-nd.prev" ]; then
2056		rm -rf $ROOT-nd.prev
2057	fi
2058	mv $ROOT-nd $ROOT-nd.prev
2059fi
2060
2061#
2062# Echo the SCM type of the parent workspace, this can't just be which_scm
2063# as that does not know how to identify various network repositories.
2064#
2065function parent_wstype {
2066	typeset scm_type junk
2067
2068	CODEMGR_WS="$BRINGOVER_WS" "$WHICH_SCM" 2>/dev/null \
2069	    | read scm_type junk
2070	if [[ -z "$scm_type" || "$scm_type" == unknown ]]; then
2071		# Probe BRINGOVER_WS to determine its type
2072		if [[ $BRINGOVER_WS == svn*://* ]]; then
2073			scm_type="subversion"
2074		elif [[ $BRINGOVER_WS == file://* ]] &&
2075		    egrep -s "This is a Subversion repository" \
2076		    ${BRINGOVER_WS#file://}/README.txt 2> /dev/null; then
2077			scm_type="subversion"
2078		elif [[ $BRINGOVER_WS == ssh://* ]]; then
2079			scm_type="mercurial"
2080		elif [[ $BRINGOVER_WS == http://* ]] && \
2081		    wget -q -O- --save-headers "$BRINGOVER_WS/?cmd=heads" | \
2082		    egrep -s "application/mercurial" 2> /dev/null; then
2083			scm_type="mercurial"
2084		elif svn info $BRINGOVER_WS > /dev/null 2>&1; then
2085			scm_type="subversion"
2086		else
2087			scm_type="none"
2088		fi
2089	fi
2090
2091	# fold both unsupported and unrecognized results into "none"
2092	case "$scm_type" in
2093	none|subversion|teamware|mercurial)
2094		;;
2095	*)	scm_type=none
2096		;;
2097	esac
2098
2099	echo $scm_type
2100}
2101
2102# Echo the SCM types of $CODEMGR_WS and $BRINGOVER_WS
2103function child_wstype {
2104	typeset scm_type junk
2105
2106	# Probe CODEMGR_WS to determine its type
2107	if [[ -d $CODEMGR_WS ]]; then
2108		$WHICH_SCM | read scm_type junk || exit 1
2109	fi
2110
2111	case "$scm_type" in
2112	none|subversion|teamware|mercurial)
2113		;;
2114	*)	scm_type=none
2115		;;
2116	esac
2117
2118	echo $scm_type
2119}
2120
2121export SCM_TYPE=$(child_wstype)
2122
2123#
2124#	Decide whether to clobber
2125#
2126if [ "$i_FLAG" = "n" -a -d "$SRC" ]; then
2127	echo "\n==== Make clobber at `date` ====\n" >> $LOGFILE
2128
2129	cd $SRC
2130	# remove old clobber file
2131	rm -f $SRC/clobber.out
2132	rm -f $SRC/clobber-${MACH}.out
2133
2134	# Remove all .make.state* files, just in case we are restarting
2135	# the build after having interrupted a previous 'make clobber'.
2136	find . \( -name SCCS -o -name .hg -o -name .svn \
2137		-o -name 'interfaces.*' \) -prune \
2138		-o -name '.make.*' -print | xargs rm -f
2139
2140	$MAKE -ek clobber 2>&1 | tee -a $SRC/clobber-${MACH}.out >> $LOGFILE
2141	echo "\n==== Make clobber ERRORS ====\n" >> $mail_msg_file
2142	grep "$MAKE:" $SRC/clobber-${MACH}.out |
2143		egrep -v "Ignoring unknown host" \
2144		>> $mail_msg_file
2145
2146	if [[ "$t_FLAG" = "y" || "$O_FLAG" = "y" ]]; then
2147		echo "\n==== Make tools clobber at `date` ====\n" >> $LOGFILE
2148		cd ${TOOLS}
2149		rm -f ${TOOLS}/clobber-${MACH}.out
2150		$MAKE TOOLS_PROTO=$TOOLS_PROTO -ek clobber 2>&1 | \
2151			tee -a ${TOOLS}/clobber-${MACH}.out >> $LOGFILE
2152		echo "\n==== Make tools clobber ERRORS ====\n" \
2153			>> $mail_msg_file
2154		grep "$MAKE:" ${TOOLS}/clobber-${MACH}.out \
2155			>> $mail_msg_file
2156		rm -rf ${TOOLS_PROTO}
2157		mkdir -p ${TOOLS_PROTO}
2158	fi
2159
2160	typeset roots=$(allprotos)
2161	echo "\n\nClearing $roots" >> "$LOGFILE"
2162	rm -rf $roots
2163
2164	# Get back to a clean workspace as much as possible to catch
2165	# problems that only occur on fresh workspaces.
2166	# Remove all .make.state* files, libraries, and .o's that may
2167	# have been omitted from clobber.  A couple of libraries are
2168	# under source code control, so leave them alone.
2169	# We should probably blow away temporary directories too.
2170	cd $SRC
2171	find $relsrcdirs \( -name SCCS -o -name .hg -o -name .svn \
2172	    -o -name 'interfaces.*' \) -prune -o \
2173	    \( -name '.make.*' -o -name 'lib*.a' -o -name 'lib*.so*' -o \
2174	       -name '*.o' \) -print | \
2175	    grep -v 'tools/ctf/dwarf/.*/libdwarf' | xargs rm -f
2176else
2177	echo "\n==== No clobber at `date` ====\n" >> $LOGFILE
2178fi
2179
2180type bringover_teamware > /dev/null 2>&1 || function bringover_teamware {
2181	# sleep on the parent workspace's lock
2182	while egrep -s write $BRINGOVER_WS/Codemgr_wsdata/locks
2183	do
2184		sleep 120
2185	done
2186
2187	if [[ -z $BRINGOVER ]]; then
2188		BRINGOVER=$TEAMWARE/bin/bringover
2189	fi
2190
2191	staffer $BRINGOVER -c "nightly update" -p $BRINGOVER_WS \
2192	    -w $CODEMGR_WS $BRINGOVER_FILES < /dev/null 2>&1 ||
2193		touch $TMPDIR/bringover_failed
2194
2195        staffer bringovercheck $CODEMGR_WS >$TMPDIR/bringovercheck.out 2>&1
2196	if [ -s $TMPDIR/bringovercheck.out ]; then
2197		echo "\n==== POST-BRINGOVER CLEANUP NOISE ====\n"
2198		cat $TMPDIR/bringovercheck.out
2199	fi
2200}
2201
2202type bringover_mercurial > /dev/null 2>&1 || function bringover_mercurial {
2203	typeset -x PATH=$PATH
2204
2205	# If the repository doesn't exist yet, then we want to populate it.
2206	if [[ ! -d $CODEMGR_WS/.hg ]]; then
2207		staffer hg init $CODEMGR_WS
2208		staffer echo "[paths]" > $CODEMGR_WS/.hg/hgrc
2209		staffer echo "default=$BRINGOVER_WS" >> $CODEMGR_WS/.hg/hgrc
2210		touch $TMPDIR/new_repository
2211	fi
2212
2213	#
2214	# If the user set CLOSED_BRINGOVER_WS and didn't set CLOSED_IS_PRESENT
2215	# to "no," then we'll want to initialise the closed repository
2216	#
2217	# We use $orig_closed_is_present instead of $CLOSED_IS_PRESENT,
2218	# because for newly-created source trees, the latter will be "no"
2219	# until after the bringover completes.
2220	#
2221	if [[ "$orig_closed_is_present" != "no" && \
2222	    -n "$CLOSED_BRINGOVER_WS" && \
2223	    ! -d $CODEMGR_WS/usr/closed/.hg ]]; then
2224		staffer mkdir -p $CODEMGR_WS/usr/closed
2225		staffer hg init $CODEMGR_WS/usr/closed
2226		staffer echo "[paths]" > $CODEMGR_WS/usr/closed/.hg/hgrc
2227		staffer echo "default=$CLOSED_BRINGOVER_WS" >> $CODEMGR_WS/usr/closed/.hg/hgrc
2228		touch $TMPDIR/new_closed
2229		export CLOSED_IS_PRESENT=yes
2230	fi
2231
2232	typeset -x HGMERGE="/bin/false"
2233
2234	#
2235	# If the user has changes, regardless of whether those changes are
2236	# committed, and regardless of whether those changes conflict, then
2237	# we'll attempt to merge them either implicitly (uncommitted) or
2238	# explicitly (committed).
2239	#
2240	# These are the messages we'll use to help clarify mercurial output
2241	# in those cases.
2242	#
2243	typeset mergefailmsg="\
2244***\n\
2245*** nightly was unable to automatically merge your changes.  You should\n\
2246*** redo the full merge manually, following the steps outlined by mercurial\n\
2247*** above, then restart nightly.\n\
2248***\n"
2249	typeset mergepassmsg="\
2250***\n\
2251*** nightly successfully merged your changes.  This means that your working\n\
2252*** directory has been updated, but those changes are not yet committed.\n\
2253*** After nightly completes, you should validate the results of the merge,\n\
2254*** then use hg commit manually.\n\
2255***\n"
2256
2257	#
2258	# For each repository in turn:
2259	#
2260	# 1. Do the pull.  If this fails, dump the output and bail out.
2261	#
2262	# 2. If the pull resulted in an extra head, do an explicit merge.
2263	#    If this fails, dump the output and bail out.
2264	#
2265	# Because we can't rely on Mercurial to exit with a failure code
2266	# when a merge fails (Mercurial issue #186), we must grep the
2267	# output of pull/merge to check for attempted and/or failed merges.
2268	#
2269	# 3. If a merge failed, set the message and fail the bringover.
2270	#
2271	# 4. Otherwise, if a merge succeeded, set the message
2272	#
2273	# 5. Dump the output, and any message from step 3 or 4.
2274	#
2275
2276	typeset HG_SOURCE=$BRINGOVER_WS
2277	if [ ! -f $TMPDIR/new_repository ]; then
2278		HG_SOURCE=$TMPDIR/open_bundle.hg
2279		staffer hg --cwd $CODEMGR_WS incoming --bundle $HG_SOURCE \
2280		    -v $BRINGOVER_WS > $TMPDIR/incoming_open.out
2281
2282		#
2283		# If there are no incoming changesets, then incoming will
2284		# fail, and there will be no bundle file.  Reset the source,
2285		# to allow the remaining logic to complete with no false
2286		# negatives.  (Unlike incoming, pull will return success
2287		# for the no-change case.)
2288		#
2289		if (( $? != 0 )); then
2290			HG_SOURCE=$BRINGOVER_WS
2291		fi
2292	fi
2293
2294	staffer hg --cwd $CODEMGR_WS pull -u $HG_SOURCE \
2295	    > $TMPDIR/pull_open.out 2>&1
2296	if (( $? != 0 )); then
2297		printf "%s: pull failed as follows:\n\n" "$CODEMGR_WS"
2298		cat $TMPDIR/pull_open.out
2299		if grep "^merging.*failed" $TMPDIR/pull_open.out > /dev/null 2>&1; then
2300			printf "$mergefailmsg"
2301		fi
2302		touch $TMPDIR/bringover_failed
2303		return
2304	fi
2305
2306	if grep "not updating" $TMPDIR/pull_open.out > /dev/null 2>&1; then
2307		staffer hg --cwd $CODEMGR_WS merge \
2308		    >> $TMPDIR/pull_open.out 2>&1
2309		if (( $? != 0 )); then
2310			printf "%s: merge failed as follows:\n\n" \
2311			    "$CODEMGR_WS"
2312			cat $TMPDIR/pull_open.out
2313			if grep "^merging.*failed" $TMPDIR/pull_open.out \
2314			    > /dev/null 2>&1; then
2315				printf "$mergefailmsg"
2316			fi
2317			touch $TMPDIR/bringover_failed
2318			return
2319		fi
2320	fi
2321
2322	printf "updated %s with the following results:\n" "$CODEMGR_WS"
2323	cat $TMPDIR/pull_open.out
2324	if grep "^merging" $TMPDIR/pull_open.out >/dev/null 2>&1; then
2325		printf "$mergepassmsg"
2326	fi
2327	printf "\n"
2328
2329	#
2330	# We only want to update usr/closed if it exists, and we haven't been
2331	# told not to via $CLOSED_IS_PRESENT, and we actually know where to
2332	# pull from ($CLOSED_BRINGOVER_WS).
2333	#
2334	if [[ $CLOSED_IS_PRESENT = yes && \
2335	    -d $CODEMGR_WS/usr/closed/.hg && \
2336	    -n $CLOSED_BRINGOVER_WS ]]; then
2337
2338		HG_SOURCE=$CLOSED_BRINGOVER_WS
2339		if [ ! -f $TMPDIR/new_closed ]; then
2340			HG_SOURCE=$TMPDIR/closed_bundle.hg
2341			staffer hg --cwd $CODEMGR_WS/usr/closed incoming \
2342			    --bundle $HG_SOURCE -v $CLOSED_BRINGOVER_WS \
2343			    > $TMPDIR/incoming_closed.out
2344
2345			#
2346			# If there are no incoming changesets, then incoming will
2347			# fail, and there will be no bundle file.  Reset the source,
2348			# to allow the remaining logic to complete with no false
2349			# negatives.  (Unlike incoming, pull will return success
2350			# for the no-change case.)
2351			#
2352			if (( $? != 0 )); then
2353				HG_SOURCE=$CLOSED_BRINGOVER_WS
2354			fi
2355		fi
2356
2357		staffer hg --cwd $CODEMGR_WS/usr/closed pull -u \
2358			$HG_SOURCE > $TMPDIR/pull_closed.out 2>&1
2359		if (( $? != 0 )); then
2360			printf "closed pull failed as follows:\n\n"
2361			cat $TMPDIR/pull_closed.out
2362			if grep "^merging.*failed" $TMPDIR/pull_closed.out \
2363			    > /dev/null 2>&1; then
2364				printf "$mergefailmsg"
2365			fi
2366			touch $TMPDIR/bringover_failed
2367			return
2368		fi
2369
2370		if grep "not updating" $TMPDIR/pull_closed.out > /dev/null 2>&1; then
2371			staffer hg --cwd $CODEMGR_WS/usr/closed merge \
2372			    >> $TMPDIR/pull_closed.out 2>&1
2373			if (( $? != 0 )); then
2374				printf "closed merge failed as follows:\n\n"
2375				cat $TMPDIR/pull_closed.out
2376				if grep "^merging.*failed" $TMPDIR/pull_closed.out > /dev/null 2>&1; then
2377					printf "$mergefailmsg"
2378				fi
2379				touch $TMPDIR/bringover_failed
2380				return
2381			fi
2382		fi
2383
2384		printf "updated %s with the following results:\n" \
2385		    "$CODEMGR_WS/usr/closed"
2386		cat $TMPDIR/pull_closed.out
2387		if grep "^merging" $TMPDIR/pull_closed.out > /dev/null 2>&1; then
2388			printf "$mergepassmsg"
2389		fi
2390	fi
2391
2392	#
2393	# Per-changeset output is neither useful nor manageable for a
2394	# newly-created repository.
2395	#
2396	if [ -f $TMPDIR/new_repository ]; then
2397		return
2398	fi
2399
2400	printf "\nadded the following changesets to open repository:\n"
2401	cat $TMPDIR/incoming_open.out
2402
2403	#
2404	# The closed repository could have been newly created, even though
2405	# the open one previously existed...
2406	#
2407	if [ -f $TMPDIR/new_closed ]; then
2408		return
2409	fi
2410
2411	if [ -f $TMPDIR/incoming_closed.out ]; then
2412		printf "\nadded the following changesets to closed repository:\n"
2413		cat $TMPDIR/incoming_closed.out
2414	fi
2415}
2416
2417type bringover_subversion > /dev/null 2>&1 || function bringover_subversion {
2418	typeset -x PATH=$PATH
2419
2420	if [[ ! -d $CODEMGR_WS/.svn ]]; then
2421		staffer svn checkout $BRINGOVER_WS $CODEMGR_WS ||
2422			touch $TMPDIR/bringover_failed
2423	else
2424		typeset root
2425		root=$(staffer svn info $CODEMGR_WS |
2426			nawk '/^Repository Root:/ {print $NF}')
2427		if [[ $root != $BRINGOVER_WS ]]; then
2428			# We fail here because there's no way to update
2429			# from a named repo.
2430			cat <<-EOF
2431			\$BRINGOVER_WS doesn't match repository root:
2432			  \$BRINGOVER_WS:  $BRINGOVER_WS
2433			  Repository root: $root
2434			EOF
2435			touch $TMPDIR/bringover_failed
2436		else
2437			# If a conflict happens, svn still exits 0.
2438			staffer svn update $CODEMGR_WS | tee $TMPDIR/pull.out ||
2439				touch $TMPDIR/bringover_failed
2440			if grep "^C" $TMPDIR/pull.out > /dev/null 2>&1; then
2441				touch $TMPDIR/bringover_failed
2442			fi
2443		fi
2444	fi
2445}
2446
2447type bringover_none > /dev/null 2>&1 || function bringover_none {
2448	echo "Couldn't figure out what kind of SCM to use for $BRINGOVER_WS."
2449	touch $TMPDIR/bringover_failed
2450}
2451
2452#
2453#	Decide whether to bringover to the codemgr workspace
2454#
2455if [ "$n_FLAG" = "n" ]; then
2456	PARENT_SCM_TYPE=$(parent_wstype)
2457
2458	if [[ $SCM_TYPE != none && $SCM_TYPE != $PARENT_SCM_TYPE ]]; then
2459		echo "cannot bringover from $PARENT_SCM_TYPE to $SCM_TYPE, " \
2460		    "quitting at `date`." | tee -a $mail_msg_file >> $LOGFILE
2461		exit 1
2462	fi
2463
2464	run_hook PRE_BRINGOVER
2465
2466	echo "\n==== bringover to $CODEMGR_WS at `date` ====\n" >> $LOGFILE
2467	echo "\n==== BRINGOVER LOG ====\n" >> $mail_msg_file
2468
2469	eval "bringover_${PARENT_SCM_TYPE}" 2>&1 |
2470		tee -a $mail_msg_file >> $LOGFILE
2471
2472	if [ -f $TMPDIR/bringover_failed ]; then
2473		rm -f $TMPDIR/bringover_failed
2474		build_ok=n
2475		echo "trouble with bringover, quitting at `date`." |
2476			tee -a $mail_msg_file >> $LOGFILE
2477		exit 1
2478	fi
2479
2480	#
2481	# It's possible that we used the bringover above to create
2482	# $CODEMGR_WS.  If so, then SCM_TYPE was previously "none,"
2483	# but should now be the same as $BRINGOVER_WS.
2484	#
2485	[[ $SCM_TYPE = none ]] && SCM_TYPE=$PARENT_SCM_TYPE
2486
2487	run_hook POST_BRINGOVER
2488
2489	#
2490	# Possible transition from pre-split workspace to split
2491	# workspace.  See if the bringover changed anything.
2492	#
2493	CLOSED_IS_PRESENT="$orig_closed_is_present"
2494	check_closed_tree
2495
2496else
2497	echo "\n==== No bringover to $CODEMGR_WS ====\n" >> $LOGFILE
2498fi
2499
2500if [[ "$O_FLAG" = y && "$CLOSED_IS_PRESENT" != "yes" ]]; then
2501	build_ok=n
2502	echo "OpenSolaris binary deliverables need usr/closed." \
2503	    | tee -a "$mail_msg_file" >> $LOGFILE
2504	exit 1
2505fi
2506
2507echo "\n==== Build environment ====\n" | tee -a $build_environ_file >> $LOGFILE
2508
2509# System
2510whence uname | tee -a $build_environ_file >> $LOGFILE
2511uname -a 2>&1 | tee -a $build_environ_file >> $LOGFILE
2512echo | tee -a $build_environ_file >> $LOGFILE
2513
2514# make
2515whence $MAKE | tee -a $build_environ_file >> $LOGFILE
2516$MAKE -v | tee -a $build_environ_file >> $LOGFILE
2517echo "number of concurrent jobs = $DMAKE_MAX_JOBS" |
2518    tee -a $build_environ_file >> $LOGFILE
2519
2520#
2521# Report the compiler versions.
2522#
2523
2524if [[ ! -f $SRC/Makefile ]]; then
2525	build_ok=n
2526	echo "\nUnable to find \"Makefile\" in $SRC." | \
2527	    tee -a $build_environ_file >> $LOGFILE
2528	exit 1
2529fi
2530
2531( cd $SRC
2532  for target in cc-version cc64-version java-version; do
2533	echo
2534	#
2535	# Put statefile somewhere we know we can write to rather than trip
2536	# over a read-only $srcroot.
2537	#
2538	rm -f $TMPDIR/make-state
2539	export SRC
2540	if $MAKE -K $TMPDIR/make-state -e $target 2>/dev/null; then
2541		continue
2542	fi
2543	touch $TMPDIR/nocompiler
2544  done
2545  echo
2546) | tee -a $build_environ_file >> $LOGFILE
2547
2548if [ -f $TMPDIR/nocompiler ]; then
2549	rm -f $TMPDIR/nocompiler
2550	build_ok=n
2551	echo "Aborting due to missing compiler." |
2552		tee -a $build_environ_file >> $LOGFILE
2553	exit 1
2554fi
2555
2556# as
2557whence as | tee -a $build_environ_file >> $LOGFILE
2558as -V 2>&1 | head -1 | tee -a $build_environ_file >> $LOGFILE
2559echo | tee -a $build_environ_file >> $LOGFILE
2560
2561# Check that we're running a capable link-editor
2562whence ld | tee -a $build_environ_file >> $LOGFILE
2563LDVER=`ld -V 2>&1`
2564echo $LDVER | tee -a $build_environ_file >> $LOGFILE
2565LDVER=`echo $LDVER | sed -e "s/.*-1\.//" -e "s/:.*//"`
2566if [ `expr $LDVER \< 422` -eq 1 ]; then
2567	echo "The link-editor needs to be at version 422 or higher to build" | \
2568	    tee -a $build_environ_file >> $LOGFILE
2569	echo "the latest stuff.  Hope your build works." | \
2570	    tee -a $build_environ_file >> $LOGFILE
2571fi
2572
2573#
2574# Build and use the workspace's tools if requested
2575#
2576if [[ "$t_FLAG" = "y" || "$O_FLAG" = y ]]; then
2577	set_non_debug_build_flags
2578
2579	build_tools ${TOOLS_PROTO}
2580	if [[ $? != 0  && "$t_FLAG" = y ]]; then
2581		use_tools $TOOLS_PROTO
2582	fi
2583fi
2584
2585#
2586# copy ihv proto area in addition to the build itself
2587#
2588if [ "$X_FLAG" = "y" ]; then
2589	copy_ihv_proto
2590fi
2591
2592if [ "$i_FLAG" = "y" -a "$SH_FLAG" = "y" ]; then
2593	echo "\n==== NOT Building base OS-Net source ====\n" | \
2594	    tee -a $LOGFILE >> $mail_msg_file
2595else
2596	# timestamp the start of the normal build; the findunref tool uses it.
2597	touch $SRC/.build.tstamp
2598
2599	normal_build
2600fi
2601
2602#
2603# Generate the THIRDPARTYLICENSE files if needed.  This is done after
2604# the build, so that dynamically-created license files are there.
2605# It's done before findunref to help identify license files that need
2606# to be added to tools/opensolaris/license-list.
2607#
2608if [ "$O_FLAG" = y -a "$build_ok" = y ]; then
2609	echo "\n==== Generating THIRDPARTYLICENSE files ====\n" |
2610	    tee -a "$mail_msg_file" >> "$LOGFILE"
2611
2612	if [ -d $ROOT/licenses/usr ]; then
2613		( cd $ROOT/licenses ; \
2614		    mktpl $SRC/pkg/license-list ) >> "$LOGFILE" 2>&1
2615		if (( $? != 0 )) ; then
2616			echo "Couldn't create THIRDPARTYLICENSE files" |
2617			    tee -a "$mail_msg_file" >> "$LOGFILE"
2618		fi
2619	else
2620		echo "No licenses found under $ROOT/licenses" |
2621		    tee -a "$mail_msg_file" >> "$LOGFILE"
2622	fi
2623fi
2624
2625ORIG_SRC=$SRC
2626BINARCHIVE=${CODEMGR_WS}/bin-${MACH}.cpio.Z
2627
2628if [ "$SE_FLAG" = "y" -o "$SD_FLAG" = "y" -o "$SH_FLAG" = "y" ]; then
2629	save_binaries
2630fi
2631
2632
2633# EXPORT_SRC comes after CRYPT_SRC since a domestic build will need
2634# $SRC pointing to the export_source usr/src.
2635
2636if [ "$SE_FLAG" = "y" -o "$SD_FLAG" = "y" -o "$SH_FLAG" = "y" ]; then
2637	if [ "$SD_FLAG" = "y" -a $build_ok = y ]; then
2638	    set_up_source_build ${CODEMGR_WS} ${CRYPT_SRC} CRYPT_SRC
2639	fi
2640
2641	if [ $build_ok = y ]; then
2642	    set_up_source_build ${CODEMGR_WS} ${EXPORT_SRC} EXPORT_SRC
2643	fi
2644fi
2645
2646if [ "$SD_FLAG" = "y" -a $build_ok = y ]; then
2647	# drop the crypt files in place.
2648	cd ${EXPORT_SRC}
2649	echo "\nextracting crypt_files.cpio.Z onto export_source.\n" \
2650	    >> ${LOGFILE}
2651	zcat ${CODEMGR_WS}/crypt_files.cpio.Z | \
2652	    cpio -idmucvB 2>/dev/null >> ${LOGFILE}
2653	if [ "$?" = "0" ]; then
2654		echo "\n==== DOMESTIC extraction succeeded ====\n" \
2655		    >> $mail_msg_file
2656	else
2657		echo "\n==== DOMESTIC extraction failed ====\n" \
2658		    >> $mail_msg_file
2659	fi
2660
2661fi
2662
2663if [ "$SO_FLAG" = "y" -a "$build_ok" = y ]; then
2664	#
2665	# Copy the open sources into their own tree, set up the closed
2666	# binaries, and set up the environment.  The build looks for
2667	# the closed binaries in a location that depends on whether
2668	# it's a DEBUG build, so we might need to make two copies.
2669	#
2670	# If copy_source fails, it will have already generated an
2671	# error message and set build_ok=n, so we don't need to worry
2672	# about that here.
2673	#
2674	copy_source $CODEMGR_WS $OPEN_SRCDIR OPEN_SOURCE usr/src
2675fi
2676
2677if [ "$SO_FLAG" = "y" -a "$build_ok" = y ]; then
2678
2679	echo "\n==== Generating skeleton closed binaries for" \
2680	    "open-only build ====\n" | \
2681	    tee -a $LOGFILE >> $mail_msg_file
2682
2683	rm -rf $CODEMGR_WS/closed.skel
2684	if [ "$D_FLAG" = y ]; then
2685		mkclosed $MACH $ROOT $CODEMGR_WS/closed.skel/root_$MACH \
2686		    >>$LOGFILE 2>&1
2687		if (( $? != 0 )) ; then
2688			echo "Couldn't create skeleton DEBUG closed binaries." |
2689			    tee -a $mail_msg_file >> $LOGFILE
2690		fi
2691	fi
2692	if [ "$F_FLAG" = n ]; then
2693		root=$ROOT
2694		[ "$MULTI_PROTO" = yes ] && root=$ROOT-nd
2695		mkclosed $MACH $root $CODEMGR_WS/closed.skel/root_$MACH-nd \
2696		    >>$LOGFILE 2>&1
2697		if (( $? != 0 )) ; then
2698			echo "Couldn't create skeleton non-DEBUG closed binaries." |
2699			    tee -a $mail_msg_file >> $LOGFILE
2700		fi
2701	fi
2702
2703	SRC=$OPEN_SRCDIR/usr/src
2704	# Try not to clobber any user-provided closed binaries.
2705	export ON_CLOSED_BINS=$CODEMGR_WS/closed.skel
2706	export CLOSED_IS_PRESENT=no
2707fi
2708
2709if is_source_build && [ $build_ok = y ] ; then
2710	# remove proto area(s) here, since we don't clobber
2711	rm -rf `allprotos`
2712	if [ "$t_FLAG" = "y" ]; then
2713		set_non_debug_build_flags
2714		ORIG_TOOLS=$TOOLS
2715		#
2716		# SRC was set earlier to point to the source build
2717		# source tree (e.g., $EXPORT_SRC).
2718		#
2719		TOOLS=${SRC}/tools
2720		TOOLS_PROTO=${TOOLS}/${TOOLS_PROTO_REL}; export TOOLS_PROTO
2721		build_tools ${TOOLS_PROTO}
2722		if [[ $? != 0 ]]; then
2723			use_tools ${TOOLS_PROTO}
2724		fi
2725	fi
2726
2727	export EXPORT_RELEASE_BUILD ; EXPORT_RELEASE_BUILD=#
2728	normal_build
2729fi
2730
2731if [[ "$SO_FLAG" = "y" && "$build_ok" = "y" ]]; then
2732	rm -rf $ON_CLOSED_BINS
2733fi
2734
2735#
2736# There are several checks that need to look at the proto area, but
2737# they only need to look at one, and they don't care whether it's
2738# DEBUG or non-DEBUG.
2739#
2740if [[ "$MULTI_PROTO" = yes && "$D_FLAG" = n ]]; then
2741	checkroot=$ROOT-nd
2742else
2743	checkroot=$ROOT
2744fi
2745
2746if [ "$build_ok" = "y" ]; then
2747	echo "\n==== Creating protolist system file at `date` ====" \
2748		>> $LOGFILE
2749	protolist $checkroot > $ATLOG/proto_list_${MACH}
2750	echo "==== protolist system file created at `date` ====\n" \
2751		>> $LOGFILE
2752
2753	if [ "$N_FLAG" != "y" ]; then
2754
2755		E1=
2756		f1=
2757		if [ -d "$SRC/pkgdefs" ]; then
2758			f1="$SRC/pkgdefs/etc/exception_list_$MACH"
2759			if [ "$X_FLAG" = "y" ]; then
2760				f1="$f1 $IA32_IHV_WS/usr/src/pkgdefs/etc/exception_list_$MACH"
2761			fi
2762		fi
2763
2764		for f in $f1; do
2765			if [ -f "$f" ]; then
2766				E1="$E1 -e $f"
2767			fi
2768		done
2769
2770		E2=
2771		f2=
2772		if [ -d "$SRC/pkg" ]; then
2773			f2="$f2 exceptions/packaging"
2774		fi
2775
2776		for f in $f2; do
2777			if [ -f "$f" ]; then
2778				E2="$E2 -e $f"
2779			fi
2780		done
2781
2782		if [ -f "$REF_PROTO_LIST" ]; then
2783			#
2784			# For builds that copy the IHV proto area (-X), add the
2785			# IHV proto list to the reference list if the reference
2786			# was built without -X.
2787			#
2788			# For builds that don't copy the IHV proto area, add the
2789			# IHV proto list to the build's proto list if the
2790			# reference was built with -X.
2791			#
2792			# Use the presence of the first file entry of the cached
2793			# IHV proto list in the reference list to determine
2794			# whether it was built with -X or not.
2795			#
2796			IHV_REF_PROTO_LIST=$SRC/pkg/proto_list_ihv_$MACH
2797			grepfor=$(nawk '$1 == "f" { print $2; exit }' \
2798				$IHV_REF_PROTO_LIST 2> /dev/null)
2799			if [ $? = 0 -a -n "$grepfor" ]; then
2800				if [ "$X_FLAG" = "y" ]; then
2801					grep -w "$grepfor" \
2802						$REF_PROTO_LIST > /dev/null
2803					if [ ! "$?" = "0" ]; then
2804						REF_IHV_PROTO="-d $IHV_REF_PROTO_LIST"
2805					fi
2806				else
2807					grep -w "$grepfor" \
2808						$REF_PROTO_LIST > /dev/null
2809					if [ "$?" = "0" ]; then
2810						IHV_PROTO_LIST="$IHV_REF_PROTO_LIST"
2811					fi
2812				fi
2813			fi
2814		fi
2815	fi
2816
2817	if [ "$N_FLAG" != "y" -a -f $SRC/pkgdefs/Makefile ]; then
2818		echo "\n==== Impact on SVr4 packages ====\n" >> $mail_msg_file
2819		#
2820		# Compare the build's proto list with current package
2821		# definitions to audit the quality of package
2822		# definitions and makefile install targets. Use the
2823		# current exception list.
2824		#
2825		PKGDEFS_LIST=""
2826		for d in $abssrcdirs; do
2827			if [ -d $d/pkgdefs ]; then
2828				PKGDEFS_LIST="$PKGDEFS_LIST -d $d/pkgdefs"
2829			fi
2830		done
2831		if [ "$X_FLAG" = "y" -a \
2832		    -d $IA32_IHV_WS/usr/src/pkgdefs ]; then
2833			PKGDEFS_LIST="$PKGDEFS_LIST -d $IA32_IHV_WS/usr/src/pkgdefs"
2834		fi
2835		$PROTOCMPTERSE \
2836		    "Files missing from the proto area:" \
2837		    "Files missing from packages:" \
2838		    "Inconsistencies between pkgdefs and proto area:" \
2839		    ${E1} \
2840		    ${PKGDEFS_LIST} \
2841		    $ATLOG/proto_list_${MACH} \
2842		    >> $mail_msg_file
2843	fi
2844
2845	if [ "$N_FLAG" != "y" -a -d $SRC/pkg ]; then
2846		echo "\n==== Validating manifests against proto area ====\n" \
2847		    >> $mail_msg_file
2848		( cd $SRC/pkg ; $MAKE -e protocmp ROOT="$checkroot" ) \
2849		    >> $mail_msg_file
2850
2851	fi
2852
2853	if [ "$N_FLAG" != "y" -a -f "$REF_PROTO_LIST" ]; then
2854		echo "\n==== Impact on proto area ====\n" >> $mail_msg_file
2855		if [ -n "$E2" ]; then
2856			ELIST=$E2
2857		else
2858			ELIST=$E1
2859		fi
2860		$PROTOCMPTERSE \
2861			"Files in yesterday's proto area, but not today's:" \
2862			"Files in today's proto area, but not yesterday's:" \
2863			"Files that changed between yesterday and today:" \
2864			${ELIST} \
2865			-d $REF_PROTO_LIST \
2866			$REF_IHV_PROTO \
2867			$ATLOG/proto_list_${MACH} \
2868			$IHV_PROTO_LIST \
2869			>> $mail_msg_file
2870	fi
2871fi
2872
2873if [ "$u_FLAG" = "y"  -a "$build_ok" = "y" ]; then
2874	staffer cp $ATLOG/proto_list_${MACH} \
2875		$PARENT_WS/usr/src/proto_list_${MACH}
2876fi
2877
2878# Update parent proto area if necessary. This is done now
2879# so that the proto area has either DEBUG or non-DEBUG kernels.
2880# Note that this clears out the lock file, so we can dispense with
2881# the variable now.
2882if [ "$U_FLAG" = "y" -a "$build_ok" = "y" ]; then
2883	echo "\n==== Copying proto area to $NIGHTLY_PARENT_ROOT ====\n" | \
2884	    tee -a $LOGFILE >> $mail_msg_file
2885	rm -rf $NIGHTLY_PARENT_ROOT/*
2886	unset Ulockfile
2887	mkdir -p $NIGHTLY_PARENT_ROOT
2888	if [[ "$MULTI_PROTO" = no || "$D_FLAG" = y ]]; then
2889		( cd $ROOT; tar cf - . |
2890		    ( cd $NIGHTLY_PARENT_ROOT;  umask 0; tar xpf - ) ) 2>&1 |
2891		    tee -a $mail_msg_file >> $LOGFILE
2892	fi
2893	if [[ "$MULTI_PROTO" = yes && "$F_FLAG" = n ]]; then
2894		rm -rf $NIGHTLY_PARENT_ROOT-nd/*
2895		mkdir -p $NIGHTLY_PARENT_ROOT-nd
2896		cd $ROOT-nd
2897		( tar cf - . |
2898		    ( cd $NIGHTLY_PARENT_ROOT-nd; umask 0; tar xpf - ) ) 2>&1 |
2899		    tee -a $mail_msg_file >> $LOGFILE
2900	fi
2901	if [ -n "${NIGHTLY_PARENT_TOOLS_ROOT}" ]; then
2902		echo "\n==== Copying tools proto area to $NIGHTLY_PARENT_TOOLS_ROOT ====\n" | \
2903		    tee -a $LOGFILE >> $mail_msg_file
2904		rm -rf $NIGHTLY_PARENT_TOOLS_ROOT/*
2905		mkdir -p $NIGHTLY_PARENT_TOOLS_ROOT
2906		if [[ "$MULTI_PROTO" = no || "$D_FLAG" = y ]]; then
2907			( cd $TOOLS_PROTO; tar cf - . |
2908			    ( cd $NIGHTLY_PARENT_TOOLS_ROOT;
2909			    umask 0; tar xpf - ) ) 2>&1 |
2910			    tee -a $mail_msg_file >> $LOGFILE
2911		fi
2912	fi
2913fi
2914
2915#
2916# ELF verification: ABI (-A) and runtime (-r) checks
2917#
2918if [[ ($build_ok = y) && ( ($A_FLAG = y) || ($r_FLAG = y) ) ]]; then
2919	# Directory ELF-data.$MACH holds the files produced by these tests.
2920	elf_ddir=$SRC/ELF-data.$MACH
2921
2922	# If there is a previous ELF-data backup directory, remove it. Then,
2923	# rotate current ELF-data directory into its place and create a new
2924	# empty directory
2925	rm -rf $elf_ddir.ref
2926	if [[ -d $elf_ddir ]]; then
2927		mv $elf_ddir $elf_ddir.ref
2928	fi
2929	mkdir -p $elf_ddir
2930
2931	# Call find_elf to produce a list of the ELF objects in the proto area.
2932	# This list is passed to check_rtime and interface_check, preventing
2933	# them from separately calling find_elf to do the same work twice.
2934	find_elf -fr $checkroot > $elf_ddir/object_list
2935
2936	if [[ $A_FLAG = y ]]; then
2937	       	echo "\n==== Check versioning and ABI information ====\n"  | \
2938		    tee -a $LOGFILE >> $mail_msg_file
2939
2940		# Produce interface description for the proto. Report errors.
2941		interface_check -o -w $elf_ddir -f object_list \
2942			-i interface -E interface.err
2943		if [[ -s $elf_ddir/interface.err ]]; then
2944			tee -a $LOGFILE < $elf_ddir/interface.err \
2945				>> $mail_msg_file
2946		fi
2947
2948		# If ELF_DATA_BASELINE_DIR is defined, compare the new interface
2949		# description file to that from the baseline gate. Issue a
2950		# warning if the baseline is not present, and keep going.
2951		if [[ "$ELF_DATA_BASELINE_DIR" != '' ]]; then
2952			base_ifile="$ELF_DATA_BASELINE_DIR/interface"
2953
2954		       	echo "\n==== Compare versioning and ABI information" \
2955			    "to baseline ====\n"  | \
2956			    tee -a $LOGFILE >> $mail_msg_file
2957		       	echo "Baseline:  $base_ifile\n" >> $LOGFILE
2958
2959			if [[ -f $base_ifile ]]; then
2960				interface_cmp -d -o $base_ifile \
2961				    $elf_ddir/interface > $elf_ddir/interface.cmp
2962				if [[ -s $elf_ddir/interface.cmp ]]; then
2963					echo | tee -a $LOGFILE >> $mail_msg_file
2964					tee -a $LOGFILE < \
2965					    $elf_ddir/interface.cmp \
2966					    >> $mail_msg_file
2967				fi
2968			else
2969			       	echo "baseline not available. comparison" \
2970                                    "skipped" | \
2971				    tee -a $LOGFILE >> $mail_msg_file
2972			fi
2973
2974		fi
2975	fi
2976
2977	if [[ $r_FLAG = y ]]; then
2978		echo "\n==== Check ELF runtime attributes ====\n" | \
2979		    tee -a $LOGFILE >> $mail_msg_file
2980
2981		# If we're doing a DEBUG build the proto area will be left
2982		# with debuggable objects, thus don't assert -s.
2983		if [[ $D_FLAG = y ]]; then
2984			rtime_sflag=""
2985		else
2986			rtime_sflag="-s"
2987		fi
2988		check_rtime -i -m -v $rtime_sflag -o -w $elf_ddir \
2989			-D object_list  -f object_list -E runtime.err \
2990			-I runtime.attr.raw
2991
2992		# check_rtime -I output needs to be sorted in order to
2993		# compare it to that from previous builds.
2994		sort $elf_ddir/runtime.attr.raw > $elf_ddir/runtime.attr
2995		rm $elf_ddir/runtime.attr.raw
2996
2997		# Report errors
2998		if [[ -s $elf_ddir/runtime.err ]]; then
2999			tee -a $LOGFILE < $elf_ddir/runtime.err \
3000				>> $mail_msg_file
3001		fi
3002
3003		# If there is an ELF-data directory from a previous build,
3004		# then diff the attr files. These files contain information
3005		# about dependencies, versioning, and runpaths. There is some
3006		# overlap with the ABI checking done above, but this also
3007		# flushes out non-ABI interface differences along with the
3008		# other information.
3009		echo "\n==== Diff ELF runtime attributes" \
3010		    "(since last build) ====\n" | \
3011		    tee -a $LOGFILE >> $mail_msg_file >> $mail_msg_file
3012
3013		if [[ -f $elf_ddir.ref/runtime.attr ]]; then
3014			diff $elf_ddir.ref/runtime.attr \
3015				$elf_ddir/runtime.attr \
3016				>> $mail_msg_file
3017		fi
3018	fi
3019
3020	# If -u set, copy contents of ELF-data.$MACH to the parent workspace.
3021	if [[ "$u_FLAG" = "y" ]]; then
3022		p_elf_ddir=$PARENT_WS/usr/src/ELF-data.$MACH
3023
3024		# If parent lacks the ELF-data.$MACH directory, create it
3025		if [[ ! -d $p_elf_ddir ]]; then
3026			staffer mkdir -p $p_elf_ddir
3027		fi
3028
3029		# These files are used asynchronously by other builds for ABI
3030		# verification, as above for the -A option. As such, we require
3031		# the file replacement to be atomic. Copy the data to a temp
3032		# file in the same filesystem and then rename into place.
3033		(
3034			cd $elf_ddir
3035			for elf_dfile in *; do
3036				staffer cp $elf_dfile \
3037				    ${p_elf_ddir}/${elf_dfile}.new
3038				staffer mv -f ${p_elf_ddir}/${elf_dfile}.new \
3039				    ${p_elf_ddir}/${elf_dfile}
3040			done
3041		)
3042	fi
3043fi
3044
3045# DEBUG lint of kernel begins
3046
3047if [ "$i_CMD_LINE_FLAG" = "n" -a "$l_FLAG" = "y" ]; then
3048	if [ "$LINTDIRS" = "" ]; then
3049		# LINTDIRS="$SRC/uts y $SRC/stand y $SRC/psm y"
3050		LINTDIRS="$SRC y"
3051	fi
3052	set $LINTDIRS
3053	while [ $# -gt 0 ]; do
3054		dolint $1 $2; shift; shift
3055	done
3056else
3057	echo "\n==== No '$MAKE lint' ====\n" >> $LOGFILE
3058fi
3059
3060# "make check" begins
3061
3062if [ "$i_CMD_LINE_FLAG" = "n" -a "$C_FLAG" = "y" ]; then
3063	# remove old check.out
3064	rm -f $SRC/check.out
3065
3066	rm -f $SRC/check-${MACH}.out
3067	cd $SRC
3068	$MAKE -ek check ROOT="$checkroot" 2>&1 | tee -a $SRC/check-${MACH}.out \
3069	    >> $LOGFILE
3070	echo "\n==== cstyle/hdrchk errors ====\n" >> $mail_msg_file
3071
3072	grep ":" $SRC/check-${MACH}.out |
3073		egrep -v "Ignoring unknown host" | \
3074		sort | uniq >> $mail_msg_file
3075else
3076	echo "\n==== No '$MAKE check' ====\n" >> $LOGFILE
3077fi
3078
3079echo "\n==== Find core files ====\n" | \
3080    tee -a $LOGFILE >> $mail_msg_file
3081
3082find $abssrcdirs -name core -a -type f -exec file {} \; | \
3083	tee -a $LOGFILE >> $mail_msg_file
3084
3085if [ "$f_FLAG" = "y" -a "$build_ok" = "y" ]; then
3086	echo "\n==== Diff unreferenced files (since last build) ====\n" \
3087	    | tee -a $LOGFILE >>$mail_msg_file
3088	rm -f $SRC/unref-${MACH}.ref
3089	if [ -f $SRC/unref-${MACH}.out ]; then
3090		mv $SRC/unref-${MACH}.out $SRC/unref-${MACH}.ref
3091	fi
3092
3093	findunref -S $SCM_TYPE -t $SRC/.build.tstamp -s usr $CODEMGR_WS \
3094	    ${TOOLS}/findunref/exception_list 2>> $mail_msg_file | \
3095	    sort > $SRC/unref-${MACH}.out
3096
3097	if [ ! -f $SRC/unref-${MACH}.ref ]; then
3098		cp $SRC/unref-${MACH}.out $SRC/unref-${MACH}.ref
3099	fi
3100
3101	diff $SRC/unref-${MACH}.ref $SRC/unref-${MACH}.out >>$mail_msg_file
3102fi
3103
3104#
3105# Generate the OpenSolaris deliverables if requested.  Some of these
3106# steps need to come after findunref and are commented below.
3107#
3108
3109# If we are doing an OpenSolaris _source_ build (-S O) then we do
3110# not have usr/closed available to us to generate closedbins from,
3111# so skip this part.
3112if [ "$SO_FLAG" = n -a "$O_FLAG" = y -a "$build_ok" = y ]; then
3113	echo "\n==== Generating OpenSolaris tarballs ====\n" | \
3114	    tee -a $mail_msg_file >> $LOGFILE
3115
3116	cd $CODEMGR_WS
3117
3118	#
3119	# This step grovels through the package manifests, so it
3120	# must come after findunref.
3121	#
3122	# We assume no DEBUG vs non-DEBUG package content variation
3123	# here; if that changes, then the "make all" in $SRC/pkg will
3124	# need to be moved into the conditionals and repeated for each
3125	# different build.
3126	#
3127	echo "Generating closed binaries tarball(s)..." >> $LOGFILE
3128	closed_basename=on-closed-bins
3129	if [ "$D_FLAG" = y ]; then
3130		bindrop "$closed_basename" >>"$LOGFILE" 2>&1
3131		if (( $? != 0 )) ; then
3132			echo "Couldn't create DEBUG closed binaries." |
3133			    tee -a $mail_msg_file >> $LOGFILE
3134			build_ok=n
3135		fi
3136	fi
3137	if [ "$F_FLAG" = n ]; then
3138		bindrop -n "$closed_basename-nd" >>"$LOGFILE" 2>&1
3139		if (( $? != 0 )) ; then
3140			echo "Couldn't create non-DEBUG closed binaries." |
3141			    tee -a $mail_msg_file >> $LOGFILE
3142			build_ok=n
3143		fi
3144	fi
3145
3146	echo "Generating README.opensolaris..." >> $LOGFILE
3147	cat $SRC/tools/opensolaris/README.opensolaris.tmpl | \
3148	    mkreadme_osol $CODEMGR_WS/README.opensolaris >> $LOGFILE 2>&1
3149	if (( $? != 0 )) ; then
3150		echo "Couldn't create README.opensolaris." |
3151		    tee -a $mail_msg_file >> $LOGFILE
3152		build_ok=n
3153	fi
3154fi
3155
3156# Verify that the usual lists of files, such as exception lists,
3157# contain only valid references to files.  If the build has failed,
3158# then don't check the proto area.
3159CHECK_PATHS=${CHECK_PATHS:-y}
3160if [ "$CHECK_PATHS" = y -a "$N_FLAG" != y ]; then
3161	echo "\n==== Check lists of files ====\n" | tee -a $LOGFILE \
3162		>>$mail_msg_file
3163	arg=-b
3164	[ "$build_ok" = y ] && arg=
3165	checkpaths $arg $checkroot 2>&1 | tee -a $LOGFILE >>$mail_msg_file
3166fi
3167
3168if [ "$M_FLAG" != "y" -a "$build_ok" = y ]; then
3169	echo "\n==== Impact on file permissions ====\n" \
3170		>> $mail_msg_file
3171
3172	abspkgdefs=
3173	abspkg=
3174	for d in $abssrcdirs; do
3175		if [ -d "$d/pkgdefs" ]; then
3176			abspkgdefs="$abspkgdefs $d"
3177		fi
3178		if [ -d "$d/pkg" ]; then
3179			abspkg="$abspkg $d"
3180		fi
3181	done
3182
3183	if [ -n "$abspkgdefs" ]; then
3184		pmodes -qvdP \
3185		    `find $abspkgdefs -name pkginfo.tmpl -print -o \
3186		    -name .del\* -prune | sed -e 's:/pkginfo.tmpl$::' | \
3187		    sort -u` >> $mail_msg_file
3188	fi
3189
3190	if [ -n "$abspkg" ]; then
3191		for d in "$abspkg"; do
3192			( cd $d/pkg ; $MAKE -e pmodes ) >> $mail_msg_file
3193		done
3194	fi
3195fi
3196
3197if [ "$w_FLAG" = "y" -a "$build_ok" = "y" ]; then
3198	if [[ "$MULTI_PROTO" = no || "$D_FLAG" = y ]]; then
3199		do_wsdiff DEBUG $ROOT.prev $ROOT
3200	fi
3201
3202	if [[ "$MULTI_PROTO" = yes && "$F_FLAG" = n ]]; then
3203		do_wsdiff non-DEBUG $ROOT-nd.prev $ROOT-nd
3204	fi
3205fi
3206
3207END_DATE=`date`
3208echo "==== Nightly $maketype build completed: $END_DATE ====" | \
3209    tee -a $LOGFILE >> $build_time_file
3210
3211typeset -i10 hours
3212typeset -Z2 minutes
3213typeset -Z2 seconds
3214
3215elapsed_time=$SECONDS
3216((hours = elapsed_time / 3600 ))
3217((minutes = elapsed_time / 60  % 60))
3218((seconds = elapsed_time % 60))
3219
3220echo "\n==== Total build time ====" | \
3221    tee -a $LOGFILE >> $build_time_file
3222echo "\nreal    ${hours}:${minutes}:${seconds}" | \
3223    tee -a $LOGFILE >> $build_time_file
3224
3225if [ "$u_FLAG" = "y" -a "$f_FLAG" = "y" -a "$build_ok" = "y" ]; then
3226	staffer cp ${SRC}/unref-${MACH}.out $PARENT_WS/usr/src/
3227
3228	#
3229	# Produce a master list of unreferenced files -- ideally, we'd
3230	# generate the master just once after all of the nightlies
3231	# have finished, but there's no simple way to know when that
3232	# will be.  Instead, we assume that we're the last nightly to
3233	# finish and merge all of the unref-${MACH}.out files in
3234	# $PARENT_WS/usr/src/.  If we are in fact the final ${MACH} to
3235	# finish, then this file will be the authoritative master
3236	# list.  Otherwise, another ${MACH}'s nightly will eventually
3237	# overwrite ours with its own master, but in the meantime our
3238	# temporary "master" will be no worse than any older master
3239	# which was already on the parent.
3240	#
3241
3242	set -- $PARENT_WS/usr/src/unref-*.out
3243	cp "$1" ${TMPDIR}/unref.merge
3244	shift
3245
3246	for unreffile; do
3247		comm -12 ${TMPDIR}/unref.merge "$unreffile" > ${TMPDIR}/unref.$$
3248		mv ${TMPDIR}/unref.$$ ${TMPDIR}/unref.merge
3249	done
3250
3251	staffer cp ${TMPDIR}/unref.merge $PARENT_WS/usr/src/unrefmaster.out
3252fi
3253
3254#
3255# All done save for the sweeping up.
3256# (whichever exit we hit here will trigger the "cleanup" trap which
3257# optionally sends mail on completion).
3258#
3259if [ "$build_ok" = "y" ]; then
3260	exit 0
3261fi
3262exit 1
3263