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