1 #!/bin/bash
   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, Version 1.0 only
   7 # (the "License").  You may not use this file except in compliance
   8 # with the License.
   9 #
  10 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  11 # or http://www.opensolaris.org/os/licensing.
  12 # See the License for the specific language governing permissions
  13 # and limitations under the License.
  14 #
  15 # When distributing Covered Code, include this CDDL HEADER in each
  16 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  17 # If applicable, add the following below this CDDL HEADER, with the
  18 # fields enclosed by brackets "[]" replaced with your own identifying
  19 # information: Portions Copyright [yyyy] [name of copyright owner]
  20 #
  21 # CDDL HEADER END
  22 #
  23 #
  24 # Copyright 2011-2012 OmniTI Computer Consulting, Inc.  All rights reserved.
  25 # Use is subject to license terms.
  26 # Copyright (c) 2014 by Delphix. All rights reserved.
  27 #
  28 
  29 umask 022
  30 
  31 #############################################################################
  32 # functions.sh
  33 #############################################################################
  34 # Helper functions for building packages that should be common to all build
  35 # scripts
  36 #############################################################################
  37 
  38 #############################################################################
  39 # Process command line options
  40 #############################################################################
  41 process_opts() {
  42     SCREENOUT=
  43     FLAVOR=
  44     OLDFLAVOR=
  45     BUILDARCH=both
  46     OLDBUILDARCH=
  47     BATCH=
  48     AUTOINSTALL=
  49     DEPVER=
  50     SKIP_PKGLINT=
  51     while getopts "bipf:ha:d:lr:" opt; do
  52         case $opt in
  53             h)
  54                 show_usage
  55                 exit
  56                 ;;
  57             \?)
  58                 show_usage
  59                 exit 2
  60                 ;;
  61             l)
  62                 SKIP_PKGLINT=1
  63                 ;;
  64             p)
  65                 SCREENOUT=1
  66                 ;;
  67             b)
  68                 BATCH=1 # Batch mode - exit on error
  69                 ;;
  70             i)
  71                 AUTOINSTALL=1
  72                 ;;
  73             f)
  74                 FLAVOR=$OPTARG
  75                 OLDFLAVOR=$OPTARG # Used to see if the script overrides the
  76                                    # flavor
  77                 ;;
  78             r)
  79                 PKGSRVR=$OPTARG
  80                 ;;
  81             a)
  82                 BUILDARCH=$OPTARG
  83                 OLDBUILDARCH=$OPTARG # Used to see if the script overrides the
  84                                      # BUILDARCH variable
  85                 if [[ "$BUILDARCH" != "32" && "$BUILDARCH" != "64" &&
  86                       "$BUILDARCH" != "both" ]]; then
  87                     echo "Invalid build architecture specified: $BUILDARCH"
  88                     show_usage
  89                     exit 2
  90                 fi
  91                 ;;
  92             d)
  93                 DEPVER=$OPTARG
  94                 ;;
  95         esac
  96     done
  97 }
  98 
  99 #############################################################################
 100 # Show usage information
 101 #############################################################################
 102 show_usage() {
 103     echo "Usage: $0 [-b] [-p] [-f FLAVOR] [-h] [-a 32|64|both] [-d DEPVER]"
 104     echo "  -b        : batch mode (exit on errors without asking)"
 105     echo "  -i        : autoinstall mode (install build deps)"
 106     echo "  -p        : output all commands to the screen as well as log file"
 107     echo "  -l        : skip pkglint check"
 108     echo "  -f FLAVOR : build a specific package flavor"
 109     echo "  -h        : print this help text"
 110     echo "  -a ARCH   : build 32/64 bit only, or both (default: both)"
 111     echo "  -d DEPVER : specify an extra dependency version (no default)"
 112     echo "  -r REPO   : specify the IPS repo to use (default: $PKGSRVR)"
 113 }
 114 
 115 #############################################################################
 116 # Log output of a command to a file
 117 #############################################################################
 118 logcmd() {
 119     if [[ -z "$SCREENOUT" ]]; then
 120         echo Running: "$@" >> $LOGFILE
 121         "$@" >> $LOGFILE 2>&1
 122     else
 123         echo Running: "$@" | tee $LOGFILE
 124         "$@" | tee $LOGFILE 2>&1
 125         return ${PIPESTATUS[0]}
 126     fi
 127 }
 128 logmsg() {
 129     echo "$@" >> $LOGFILE
 130     echo "$@"
 131 }
 132 logerr() {
 133     # Print an error message and ask the user if they wish to continue
 134     logmsg $@
 135     if [[ -z $BATCH ]]; then
 136         ask_to_continue "An Error occured in the build. "
 137     else
 138         exit 1
 139     fi
 140 }
 141 ask_to_continue_() {
 142     MSG=$2
 143     STR=$3
 144     RE=$4
 145     # Ask the user if they want to continue or quit in the event of an error
 146     echo -n "${1}${MSG} ($STR) "
 147     read
 148     while [[ ! "$REPLY" =~ $RE ]]; do
 149         echo -n "continue? ($STR) "
 150         read
 151     done
 152 }
 153 ask_to_continue() {
 154     ask_to_continue_ "${1}" "Do you wish to continue anyway?" "y/n" "[yYnN]"
 155     if [[ "$REPLY" == "n" || "$REPLY" == "N" ]]; then
 156         logmsg "===== Build aborted ====="
 157         exit 1
 158     fi
 159     logmsg "===== User elected to continue after prompt. ====="
 160 }
 161 
 162 ask_to_install() {
 163     PKG=$1
 164     MSG=$2
 165     if [[ -n "$AUTOINSTALL" ]]; then
 166         logmsg "Auto-installing $PKG..."
 167         logcmd sudo pkg install $PKG || logerr "pkg install $PKG failed"
 168         return
 169     fi
 170     if [[ -n "$BATCH" ]]; then
 171         logmsg "===== Build aborted ====="
 172         exit 1
 173     fi
 174     ask_to_continue_ "$MSG " "Install/Abort?" "i/a" "[iIaA]"
 175     if [[ "$REPLY" == "i" || "$REPLY" == "I" ]]; then
 176         logcmd sudo pkg install $PKG || logerr "pkg install failed"
 177     else
 178         logmsg "===== Build aborted ====="
 179         exit 1
 180     fi
 181 }
 182 
 183 ask_to_pkglint() {
 184     local MANIFEST=$1
 185 
 186     ask_to_continue_ "" "Do you want to run pkglint at this time?" "y/n" "[yYnN]"
 187     [[ "$REPLY" == "y" || "$REPLY" == "Y" ]]
 188 }
 189 
 190 #############################################################################
 191 # URL encoding for package names, at least
 192 #############################################################################
 193 # This isn't real URL encoding, just a couple of common substitutions
 194 url_encode() {
 195     [ $# -lt 1 ] && logerr "Not enough arguments to url_encode().  Expecting a string to encode."
 196     local encoded="$1";
 197     encoded=`echo $encoded | sed -e 's!/!%2F!g' -e 's!+!%2B!g'`
 198     encoded=`echo $encoded | sed -e 's/%../_/g;'`
 199     echo $encoded
 200 }
 201 
 202 #############################################################################
 203 # Some initialization
 204 #############################################################################
 205 # Set the LANG to C as the assembler will freak out on unicode in headers
 206 LANG=C
 207 export LANG
 208 # Set the path - This can be overriden/extended in the build script
 209 PATH="/opt/gcc-4.8.1/bin:/usr/ccs/bin:/usr/bin:/usr/sbin:/usr/gnu/bin:/usr/sfw/bin"
 210 export PATH
 211 # The dir where this file is located - used for sourcing further files
 212 MYDIR=$PWD/`dirname $BASH_SOURCE[0]`
 213 # The dir where this file was sourced from - this will be the directory of the
 214 # build script
 215 SRCDIR=$PWD/`dirname $0`
 216 
 217 #############################################################################
 218 # Load configuration options
 219 #############################################################################
 220 . $MYDIR/config.sh
 221 . $MYDIR/site.sh
 222 
 223 # Platform information
 224 SUNOSVER=`uname -r` # e.g. 5.11
 225 
 226 if [[ -f $LOGFILE ]]; then
 227     mv $LOGFILE $LOGFILE.1
 228 fi
 229 process_opts $@
 230 shift $((OPTIND - 1))
 231 
 232 BasicRequirements(){
 233     local needed=""
 234     [[ -x /opt/gcc-4.8.1/bin/gcc ]] || needed+=" developer/gcc48"
 235     [[ -x /usr/bin/ar ]] || needed+=" developer/object-file"
 236     [[ -x /usr/bin/ld ]] || needed+=" developer/linker"
 237     [[ -f /usr/lib/crt1.o ]] || needed+=" developer/library/lint"
 238     [[ -x /usr/bin/gmake ]] || needed+=" developer/build/gnu-make"
 239     [[ -f /usr/include/sys/types.h ]] || needed+=" system/header"
 240     [[ -f /usr/include/math.h ]] || needed+=" system/library/math/header-math"
 241     if [[ -n "$needed" ]]; then
 242         logmsg "You appear to be missing some basic build requirements."
 243         logmsg "To fix this run:"
 244         logmsg " "
 245         logmsg "  sudo pkg install$needed"
 246         if [[ -n "$BATCH" ]]; then
 247             logmsg "===== Build aborted ====="
 248             exit 1
 249         fi
 250         echo
 251         for i in "$needed"; do
 252            ask_to_install $i "--- Build-time dependency $i not found"
 253         done
 254     fi
 255 }
 256 BasicRequirements
 257 
 258 #############################################################################
 259 # Running as root is not safe
 260 #############################################################################
 261 if [[ "$UID" = "0" ]]; then
 262     if [[ -n "$ROOT_OK" ]]; then
 263         logmsg "--- Running as root, but ROOT_OK is set; continuing"
 264     else
 265         logerr "--- You cannot run this as root"
 266     fi
 267 fi
 268 
 269 #############################################################################
 270 # Print startup message
 271 #############################################################################
 272 [[ -z "$NOBANNER" ]] && logmsg "===== Build started at `date` ====="
 273 
 274 
 275 #############################################################################
 276 # Libtool -nostdlib hacking
 277 # libtool doesn't put -nostdlib in the shared archive creation command
 278 # we need it sometimes.
 279 #############################################################################
 280 libtool_nostdlib() {
 281     FILE=$1
 282     EXTRAS=$2
 283     logcmd perl -pi -e 's#(\$CC.*\$compiler_flags)#$1 -nostdlib '"$EXTRAS"'#g;' $FILE ||
 284         logerr "--- Patching libtool:$FILE for -nostdlib support failed"
 285 }
 286 
 287 #############################################################################
 288 # Initialization function
 289 #############################################################################
 290 init() {
 291     # Print out current settings
 292     logmsg "Package name: $PKG"
 293     # Selected flavor
 294     if [[ -z "$FLAVOR" ]]; then
 295         logmsg "Selected flavor: None (use -f to specify a flavor)"
 296     else
 297         logmsg "Selected Flavor: $FLAVOR"
 298     fi
 299     if [[ -n "$OLDFLAVOR" && "$OLDFLAVOR" != "$FLAVOR" ]]; then
 300         logmsg "NOTICE - The flavor was overridden by the build script."
 301         logmsg "The flavor specified on the command line was: $OLDFLAVOR"
 302     fi
 303     # Build arch
 304     logmsg "Selected build arch: $BUILDARCH"
 305     if [[ -n "$OLDBUILDARCH" && "$OLDBUILDARCH" != "$BUILDARCH" ]]; then
 306         logmsg "NOTICE - The build arch was overridden by the build script."
 307         logmsg "The build arch specified on the command line was: $OLDFLAVOR"
 308     fi
 309     # Extra dependency version
 310     if [[ -z "$DEPVER" ]]; then
 311         logmsg "Extra dependency: None (use -d to specify a version)"
 312     else
 313         logmsg "Extra dependency: $DEPVER"
 314     fi
 315     # Ensure SUMMARY and DESC are non-empty
 316     if [[ -z "$SUMMARY" ]]; then
 317         logerr "SUMMARY may not be empty. Please update your build script"
 318     elif [[ -z "$DESC" ]]; then
 319         logerr "DESC may not be empty. Please update your build script"
 320     fi
 321 
 322     # BUILDDIR can be used to manually specify what directory the program is
 323     # built in (i.e. what the tarball extracts to). This defaults to the name
 324     # and version of the program, which works in most cases.
 325     if [[ -z $BUILDDIR ]]; then
 326         BUILDDIR=$PROG-$VER
 327     fi
 328 
 329     RPATH=`echo $PKGSRVR | sed -e 's/^file:\/*/\//'`
 330     if [[ "$RPATH" != "$PKGSRVR" ]]; then
 331         if [[ ! -d $RPATH ]]; then
 332             pkgrepo create $RPATH || \
 333                 logerr "Could not local repo"
 334             pkgrepo add-publisher -s $RPATH $PKGPUBLISHER || \
 335                 logerr "Could not set publisher on repo"
 336         fi
 337     fi
 338     pkgrepo get -s $PKGSRVR > /dev/null 2>&1 || \
 339         logerr "The PKGSRVR ($PKGSRVR) isn't available. All is doomed."
 340     verify_depends
 341 }
 342 
 343 #############################################################################
 344 # Verify any dependencies
 345 #############################################################################
 346 verify_depends() {
 347     logmsg "Verifying dependencies"
 348     # Support old-style runtime deps
 349     if [[ -n "$DEPENDS_IPS" && -n "$RUN_DEPENDS_IPS" ]]; then
 350         # Either old way or new, not both.
 351         logerr "DEPENDS_IPS is deprecated. Please list all runtime dependencies in RUN_DEPENDS_IPS."
 352     elif [[ -n "$DEPENDS_IPS" && -z "$RUN_DEPENDS_IPS" ]]; then
 353         RUN_DEPENDS_IPS=$DEPENDS_IPS
 354     fi
 355     # If only DEPENDS_IPS is used, assume the deps are build-time as well
 356     if [[ -z "$BUILD_DEPENDS_IPS" && -n "$DEPENDS_IPS" ]]; then
 357         BUILD_DEPENDS_IPS=$DEPENDS_IPS
 358     fi
 359     for i in $BUILD_DEPENDS_IPS; do
 360         # Trim indicators to get the true name (see make_package for details)
 361         case ${i:0:1} in
 362             \=|\?)
 363                 i=${i:1}
 364                 ;;
 365             \-)
 366                 # If it's an exclude, we should error if it's installed rather than missing
 367                 i=${i:1}
 368                 pkg info $i > /dev/null 2<&1 &&
 369                     logerr "--- $i cannot be installed while building this package."
 370                 continue
 371                 ;;
 372         esac
 373         pkg info $i > /dev/null 2<&1 ||
 374             ask_to_install "$i" "--- Build-time dependency $i not found"
 375     done
 376 }
 377 
 378 #############################################################################
 379 # People that need this should call it explicitly
 380 #############################################################################
 381 run_autoconf() {
 382     logmsg "Running autoconf"
 383     pushd $TMPDIR/$BUILDDIR > /dev/null
 384     logcmd autoconf || logerr "Failed to run autoconf"
 385     popd > /dev/null
 386 }
 387 
 388 #############################################################################
 389 # Stuff that needs to be done/set before we start building
 390 #############################################################################
 391 prep_build() {
 392     logmsg "Preparing for build"
 393 
 394     # Get the current date/time for the package timestamp
 395     DATETIME=`TZ=UTC /usr/bin/date +"%Y%m%dT%H%M%SZ"`
 396 
 397     logmsg "--- Creating temporary install dir"
 398     # We might need to encode some special chars
 399     PKGE=$(url_encode $PKG)
 400     # For DESTDIR the '%' can cause problems for some install scripts
 401     PKGD=${PKGE//%/_}
 402     DESTDIR=$DTMPDIR/${PKGD}_pkg
 403     if [[ -z $DONT_REMOVE_INSTALL_DIR ]]; then
 404         logcmd chmod -R u+w $DESTDIR > /dev/null 2>&1
 405         logcmd rm -rf $DESTDIR || \
 406             logerr "Failed to remove old temporary install dir"
 407         mkdir -p $DESTDIR || \
 408             logerr "Failed to create temporary install dir"
 409     fi
 410 }
 411 
 412 #############################################################################
 413 # Applies patches contained in $PATCHDIR (default patches/)
 414 #############################################################################
 415 check_for_patches() {
 416     if [[ -z $1 ]]; then
 417         logmsg "Checking for patches in $PATCHDIR/"
 418     else
 419         logmsg "Checking for patches in $PATCHDIR/ ($1)"
 420     fi
 421     if [[ ! -d $SRCDIR/$PATCHDIR ]]; then
 422         logmsg "--- No patches directory found"
 423         return 1
 424     fi
 425     if [[ ! -f $SRCDIR/$PATCHDIR/series ]]; then
 426         logmsg "--- No series file (list of patches) found"
 427         return 1
 428     fi
 429     return 0
 430 }
 431 
 432 patch_source() {
 433     if ! check_for_patches "in order to apply them"; then
 434         logmsg "--- Not applying any patches"
 435     else
 436         logmsg "Applying patches"
 437         # Read the series file for patch filenames
 438         exec 3<"$SRCDIR/$PATCHDIR/series" # Open the series file with handle 3
 439         pushd $TMPDIR/$BUILDDIR > /dev/null
 440         while read LINE <&3 ; do
 441             # Split Line into filename+args
 442             patch_file $LINE
 443         done
 444         popd > /dev/null
 445         exec 3<&- # Close the file
 446     fi
 447 }
 448 
 449 patch_file() {
 450     FILENAME=$1
 451     shift
 452     ARGS=$@
 453     if [[ ! -f $SRCDIR/$PATCHDIR/$FILENAME ]]; then
 454         logmsg "--- Patch file $FILENAME not found. Skipping patch."
 455         return
 456     fi
 457     # Note - if -p is specified more than once, then the last one takes
 458     # precedence, so we can specify -p1 at the beginning to default to -p1.
 459     # -t - don't ask questions
 460     # -N - don't try to apply a reverse patch
 461     if ! logcmd $PATCH -p1 -t -N $ARGS < $SRCDIR/$PATCHDIR/$FILENAME; then
 462         logerr "--- Patch $FILENAME failed"
 463     else
 464         logmsg "--- Applied patch $FILENAME"
 465     fi
 466 }
 467 
 468 #############################################################################
 469 # Attempt to download the given resource to the current directory.
 470 #############################################################################
 471 # Parameters
 472 #   $1 - resource to get
 473 #
 474 get_resource() {
 475     local RESOURCE=$1
 476     case ${MIRROR:0:1} in
 477         /)
 478             logcmd cp $MIRROR/$RESOURCE .
 479             ;;
 480         *)
 481             URLPREFIX=http://$MIRROR
 482             $WGET -a $LOGFILE $URLPREFIX/$RESOURCE
 483             ;;
 484     esac
 485 }
 486 
 487 #############################################################################
 488 # Download source tarball if needed and extract it
 489 #############################################################################
 490 # Parameters
 491 #   $1 - directory name on the server
 492 #   $2 - program name
 493 #   $3 - program version
 494 #   $4 - target directory
 495 #
 496 # E.g.
 497 #       download_source myprog myprog 1.2.3 will try:
 498 #       http://mirrors.omniti.com/myprog/myprog-1.2.3.tar.gz
 499 download_source() {
 500     local DLDIR=$1
 501     local PROG=$2
 502     local VER=$3
 503     local TARGETDIR=$4
 504     if [[ -z $VER ]]; then
 505         local ARCHIVEPREFIX=$PROG
 506     else
 507         local ARCHIVEPREFIX=$PROG-$VER
 508     fi
 509     if [[ -z $TARGETDIR ]]; then
 510         # Default to $TMPDIR if no output dir specified
 511         TARGETDIR=$TMPDIR
 512     fi
 513     # Create TARGETDIR if it doesn't exist
 514     if [[ ! -d $TARGETDIR ]]; then
 515         logmsg "Specified target directory $TARGETDIR does not exist.  Creating it now."
 516         logcmd mkdir -p $TARGETDIR
 517     fi
 518     pushd $TARGETDIR > /dev/null
 519     logmsg "Checking for source directory"
 520     if [ -d $BUILDDIR ]; then
 521         logmsg "--- Source directory found"
 522         if check_for_patches "to see if we need to remove the source dir"; then
 523             logmsg "--- Patches are present, removing source directory"
 524             logcmd rm -rf $BUILDDIR || \
 525                 logerr "Failed to remove source directory"
 526         else
 527             logmsg "--- Patches are not present, keeping source directory"
 528             popd > /dev/null
 529             return
 530         fi
 531     else
 532         logmsg "--- Source directory not found"
 533     fi
 534 
 535     # If we reach this point, the source directory was either not found, or it
 536     # was removed due to patches being present.
 537     logmsg "Checking for $PROG source archive"
 538     find_archive $ARCHIVEPREFIX FILENAME
 539     if [[ "$FILENAME" == "" ]]; then
 540         # Try all possible archive names
 541         logmsg "--- Archive not found."
 542         logmsg "Downloading archive"
 543         get_resource $DLDIR/$ARCHIVEPREFIX.tar.gz || \
 544             get_resource $DLDIR/$ARCHIVEPREFIX.tar.bz2 || \
 545             get_resource $DLDIR/$ARCHIVEPREFIX.tar.xz || \
 546             get_resource $DLDIR/$ARCHIVEPREFIX.tgz || \
 547             get_resource $DLDIR/$ARCHIVEPREFIX.tbz || \
 548             get_resource $DLDIR/$ARCHIVEPREFIX.tar || \
 549             get_resource $DLDIR/$ARCHIVEPREFIX.zip || \
 550             logerr "--- Failed to download file"
 551         find_archive $ARCHIVEPREFIX FILENAME
 552         if [[ "$FILENAME" == "" ]]; then
 553             logerr "Unable to find downloaded file."
 554         fi
 555     else
 556         logmsg "--- $PROG source archive found"
 557     fi
 558     # Extract the archive
 559     logmsg "Extracting archive: $FILENAME"
 560     if ! logcmd extract_archive $FILENAME; then
 561         logerr "--- Unable to extract archive."
 562     fi
 563     # Make sure the archive actually extracted some source where we expect
 564     if [[ ! -d $BUILDDIR ]]; then
 565         logerr "--- Extracted source is not in the expected location" \
 566             " ($BUILDDIR)"
 567     fi
 568     popd > /dev/null
 569 }
 570 
 571 # Finds an existing archive and stores its value in a variable whose name
 572 #   is passed as a second parameter
 573 # Example: find_archive myprog-1.2.3 FILENAME
 574 #   Stores myprog-1.2.3.tar.gz in $FILENAME
 575 find_archive() {
 576     FILES=`ls $1.{tar.bz2,tar.gz,tar.xz,tgz,tbz,tar,zip} 2>/dev/null`
 577     FILES=${FILES%% *} # Take only the first filename returned
 578     # This dereferences the second parameter passed
 579     eval "$2=\"$FILES\""
 580 }
 581 
 582 # Extracts an archive regardless of its extension
 583 extract_archive() {
 584     if [[ ${1: -7} == ".tar.gz" || ${1: -4} == ".tgz" ]]; then
 585         $GZIP -dc $1 | $TAR xvf -
 586     elif [[ ${1: -8} == ".tar.bz2" || ${1: -4} == ".tbz" ]]; then
 587         $BUNZIP2 -dc $1 | $TAR xvf -
 588     elif [[ ${1: -7} == ".tar.xz" ]]; then
 589         $XZCAT $1 | $TAR xvf -
 590     elif [[ ${1: -4} == ".tar" ]]; then
 591         $TAR xvf $1
 592     elif [[ ${1: -4} == ".zip" ]]; then
 593         $UNZIP $1
 594     else
 595         return 1
 596     fi
 597 }
 598 
 599 #############################################################################
 600 # Make the package
 601 #############################################################################
 602 make_package() {
 603     logmsg "Making package"
 604     case $BUILDARCH in
 605         32)
 606             BUILDSTR="32bit-"
 607             ;;
 608         64)
 609             BUILDSTR="64bit-"
 610             ;;
 611         *)
 612             BUILDSTR=""
 613             ;;
 614     esac
 615     # Add the flavor name to the package if it is not the default
 616     case $FLAVOR in
 617         ""|default)
 618             FLAVORSTR=""
 619             ;;
 620         *)
 621             FLAVORSTR="$FLAVOR-"
 622             ;;
 623     esac
 624     DESCSTR="$DESC"
 625     if [[ -n "$FLAVORSTR" ]]; then
 626         DESCSTR="$DESCSTR ($FLAVOR)"
 627     fi
 628     PKGSEND=/usr/bin/pkgsend
 629     PKGLINT=/usr/bin/pkglint
 630     PKGMOGRIFY=/usr/bin/pkgmogrify
 631     PKGFMT=/usr/bin/pkgfmt
 632     P5M_INT=$TMPDIR/${PKGE}.p5m.int
 633     P5M_FINAL=$TMPDIR/${PKGE}.p5m
 634     GLOBAL_MOG_FILE=$MYDIR/global-transforms.mog
 635     MY_MOG_FILE=$TMPDIR/${PKGE}.mog
 636 
 637     ## Strip leading zeros in version components.
 638     VER=`echo $VER | sed -e 's/\.0*\([1-9]\)/.\1/g;'`
 639     if [[ -n "$FLAVOR" ]]; then
 640         # We use FLAVOR instead of FLAVORSTR as we don't want the trailing dash
 641         FMRI="${PKG}-${FLAVOR}@${VER},${SUNOSVER}-${PVER}"
 642     else
 643         FMRI="${PKG}@${VER},${SUNOSVER}-${PVER}"
 644     fi
 645     if [[ -n "$DESTDIR" ]]; then
 646         logmsg "--- Generating package manifest from $DESTDIR"
 647         logmsg "------ Running: $PKGSEND generate $DESTDIR > $P5M_INT"
 648         $PKGSEND generate $DESTDIR > $P5M_INT || \
 649             logerr "------ Failed to generate manifest"
 650     else
 651         logmsg "--- Looks like a meta-package. Creating empty manifest"
 652         logcmd touch $P5M_INT || \
 653             logerr "------ Failed to create empty manifest"
 654     fi
 655     logmsg "--- Generating package metadata"
 656     echo "set name=pkg.fmri value=$FMRI" > $MY_MOG_FILE
 657     # Set human-readable version, if it exists
 658     if [[ -n "$VERHUMAN" ]]; then
 659         logmsg "------ Setting human-readable version"
 660         echo "set name=pkg.human-version value=\"$VERHUMAN\"" >> $MY_MOG_FILE
 661     fi
 662     echo "set name=pkg.summary value=\"$SUMMARY\"" >> $MY_MOG_FILE
 663     echo "set name=pkg.descr value=\"$DESCSTR\"" >> $MY_MOG_FILE
 664     echo "set name=publisher value=\"sa@omniti.com\"" >> $MY_MOG_FILE
 665     if [[ -n "$RUN_DEPENDS_IPS" ]]; then
 666         logmsg "------ Adding dependencies"
 667         for i in $RUN_DEPENDS_IPS; do
 668             # IPS dependencies have multiple types, of which we care about four:
 669             #    require, optional, incorporate, exclude
 670             # For backward compatibility, assume no indicator means type=require
 671             # FMRI attributes are implicitly rooted so we don't have to prefix
 672             # 'pkg:/' or worry about ambiguities in names
 673             local DEPTYPE="require"
 674             case ${i:0:1} in
 675                 \=)
 676                     DEPTYPE="incorporate"
 677                     i=${i:1}
 678                     ;;
 679                 \?)
 680                     DEPTYPE="optional"
 681                     i=${i:1}
 682                     ;;
 683                 \-)
 684                     DEPTYPE="exclude"
 685                     i=${i:1}
 686                     ;;
 687             esac
 688             echo "depend type=$DEPTYPE fmri=${i}" >> $MY_MOG_FILE
 689         done
 690     fi
 691     if [[ -f $SRCDIR/local.mog ]]; then
 692         LOCAL_MOG_FILE=$SRCDIR/local.mog
 693     fi
 694     logmsg "--- Applying transforms"
 695     $PKGMOGRIFY $P5M_INT $MY_MOG_FILE $GLOBAL_MOG_FILE $LOCAL_MOG_FILE $* | $PKGFMT -u > $P5M_FINAL
 696     if [[ -z $SKIP_PKGLINT ]] && ( [[ -n $BATCH ]] ||  ask_to_pkglint ); then
 697         $PKGLINT -c $TMPDIR/lint-cache -r $PKGSRVR $P5M_FINAL || \
 698             logerr "----- pkglint failed"
 699     fi
 700     logmsg "--- Publishing package to $PKGSRVR"
 701     if [[ -z $BATCH ]]; then
 702         logmsg "Intentional pause: Last chance to sanity-check before publication!"
 703         ask_to_continue
 704     fi
 705     if [[ -n "$DESTDIR" ]]; then
 706         logcmd $PKGSEND -s $PKGSRVR publish -d $DESTDIR -d $TMPDIR/$BUILDDIR \
 707             -d $SRCDIR $P5M_FINAL || logerr "------ Failed to publish package"
 708     else
 709         # If we're a metapackage (no DESTDIR) then there are no directories to check
 710         logcmd $PKGSEND -s $PKGSRVR publish $P5M_FINAL || \
 711             logerr "------ Failed to publish package"
 712     fi
 713     logmsg "--- Published $FMRI" 
 714 }
 715 
 716 #############################################################################
 717 # Make isaexec stub binaries
 718 #############################################################################
 719 make_isa_stub() {
 720     logmsg "Making isaexec stub binaries"
 721     [[ -z $ISAEXEC_DIRS ]] && ISAEXEC_DIRS="bin sbin"
 722     for DIR in $ISAEXEC_DIRS; do
 723         if [[ -d $DESTDIR$PREFIX/$DIR ]]; then
 724             logmsg "--- $DIR"
 725             pushd $DESTDIR$PREFIX/$DIR > /dev/null
 726             make_isaexec_stub_arch $ISAPART
 727             make_isaexec_stub_arch $ISAPART64
 728             popd > /dev/null
 729         fi
 730     done
 731 }
 732 
 733 make_isaexec_stub_arch() {
 734     for file in $1/*; do
 735         [[ -f $file ]] || continue # Deals with empty dirs & non-files
 736         # Check to make sure we don't have a script
 737         read -n 5 < $file
 738         file=`echo $file | sed -e "s/$1\///;"`
 739         # Skip if we already made a stub for this file
 740         [[ -f $file ]] && continue
 741         # Only copy non-binaries if we set NOSCRIPTSTUB
 742         if [[ $REPLY != $'\177'ELF && -n "$NOSCRIPTSTUB" ]]; then
 743             logmsg "------ Non-binary file: $file - copying instead"
 744             cp $1/$file .
 745             chmod +x $file
 746             continue
 747         fi
 748         logmsg "------ $file"
 749         # Run the makeisa.sh script
 750         CC=$CC \
 751         logcmd $MYDIR/makeisa.sh $PREFIX/$DIR $file || \
 752             logerr "--- Failed to make isaexec stub for $DIR/$file"
 753     done
 754 }
 755 
 756 #############################################################################
 757 # Build commands
 758 #############################################################################
 759 # Notes:
 760 #   - These methods are designed to work in the general case.
 761 #   - You can set CFLAGS/LDFLAGS (and CFLAGS32/CFLAGS64 for arch specific flags)
 762 #   - Configure flags are set in CONFIGURE_OPTS_32 and CONFIGURE_OPTS_64 with
 763 #     defaults set in config.sh. You can append to these variables or replace
 764 #     them if the defaults don't work for you.
 765 #   - In the normal case, where you just want to add --enable-feature, set
 766 #     CONFIGURE_OPTS. This will be appended to the end of CONFIGURE_CMD
 767 #     for both 32 and 64 bit builds.
 768 #   - Any of these functions can be overriden in your build script, so if
 769 #     anything here doesn't apply to the build process for your application,
 770 #     just override that function with whatever code you need. The build
 771 #     function itself can be overriden if the build process doesn't fit into a
 772 #     configure, make, make install pattern.
 773 #############################################################################
 774 make_clean() {
 775     logmsg "--- make (dist)clean"
 776     logcmd $MAKE distclean || \
 777     logcmd $MAKE clean || \
 778         logmsg "--- *** WARNING *** make (dist)clean Failed"
 779 }
 780 
 781 configure32() {
 782     logmsg "--- configure (32-bit)"
 783     CFLAGS="$CFLAGS $CFLAGS32" \
 784     CXXFLAGS="$CXXFLAGS $CXXFLAGS32" \
 785     CPPFLAGS="$CPPFLAGS $CPPFLAGS32" \
 786     LDFLAGS="$LDFLAGS $LDFLAGS32" \
 787     CC=$CC CXX=$CXX \
 788     logcmd $CONFIGURE_CMD $CONFIGURE_OPTS_32 \
 789     $CONFIGURE_OPTS || \
 790         logerr "--- Configure failed"
 791 }
 792 
 793 configure64() {
 794     logmsg "--- configure (64-bit)"
 795     CFLAGS="$CFLAGS $CFLAGS64" \
 796     CXXFLAGS="$CXXFLAGS $CXXFLAGS64" \
 797     CPPFLAGS="$CPPFLAGS $CPPFLAGS64" \
 798     LDFLAGS="$LDFLAGS $LDFLAGS64" \
 799     CC=$CC CXX=$CXX \
 800     logcmd $CONFIGURE_CMD $CONFIGURE_OPTS_64 \
 801     $CONFIGURE_OPTS || \
 802         logerr "--- Configure failed"
 803 }
 804 
 805 make_prog() {
 806     [[ -n $NO_PARALLEL_MAKE ]] && MAKE_JOBS=""
 807     if [[ -n $LIBTOOL_NOSTDLIB ]]; then
 808         libtool_nostdlib $LIBTOOL_NOSTDLIB $LIBTOOL_NOSTDLIB_EXTRAS
 809     fi
 810     logmsg "--- make"
 811     logcmd $MAKE $MAKE_JOBS || \
 812         logerr "--- Make failed"
 813 }
 814 
 815 make_prog32() {
 816     make_prog
 817 }
 818 
 819 make_prog64() {
 820     make_prog
 821 }
 822 
 823 make_install() {
 824     logmsg "--- make install"
 825     logcmd $MAKE DESTDIR=${DESTDIR} install || \
 826         logerr "--- Make install failed"
 827 }
 828 
 829 make_install32() {
 830     make_install
 831 }
 832 
 833 make_install64() {
 834     make_install
 835 }
 836 
 837 make_pure_install() {
 838     # Make pure_install for perl modules so they don't touch perllocal.pod
 839     logmsg "--- make install (pure)"
 840     logcmd $MAKE DESTDIR=${DESTDIR} pure_install || \
 841         logerr "--- Make pure_install failed"
 842 }
 843 
 844 make_param() {
 845     logmsg "--- make $@"
 846     logcmd $MAKE "$@" || \
 847         logerr "--- $MAKE $1 failed"
 848 }
 849 
 850 # Helper function that can be called by build scripts to make in a specific dir
 851 make_in() {
 852     [[ -z $1 ]] && logerr "------ Make in dir failed - no dir specified"
 853     [[ -n $NO_PARALLEL_MAKE ]] && MAKE_JOBS=""
 854     logmsg "------ make in $1"
 855     logcmd $MAKE $MAKE_JOBS -C $1 || \
 856         logerr "------ Make in $1 failed"
 857 }
 858 
 859 # Helper function that can be called by build scripts to install in a specific
 860 # dir
 861 make_install_in() {
 862     [[ -z $1 ]] && logerr "--- Make install in dir failed - no dir specified"
 863     logmsg "------ make install in $1"
 864     logcmd $MAKE -C $1 DESTDIR=${DESTDIR} install || \
 865         logerr "------ Make install in $1 failed"
 866 }
 867 
 868 build() {
 869     if [[ $BUILDARCH == "32" || $BUILDARCH == "both" ]]; then
 870         build32
 871     fi
 872     if [[ $BUILDARCH == "64" || $BUILDARCH == "both" ]]; then
 873         build64
 874     fi
 875 }
 876 
 877 build32() {
 878     pushd $TMPDIR/$BUILDDIR > /dev/null
 879     logmsg "Building 32-bit"
 880     export ISALIST="$ISAPART"
 881     make_clean
 882     configure32
 883     make_prog32
 884     make_install32
 885     popd > /dev/null
 886     unset ISALIST
 887     export ISALIST
 888 }
 889 
 890 build64() {
 891     pushd $TMPDIR/$BUILDDIR > /dev/null
 892     logmsg "Building 64-bit"
 893     make_clean
 894     configure64
 895     make_prog64
 896     make_install64
 897     popd > /dev/null
 898 }
 899 
 900 #############################################################################
 901 # Build function for python programs
 902 #############################################################################
 903 pre_python_32() {
 904     logmsg "prepping 32bit python build"
 905 }
 906 pre_python_64() {
 907     logmsg "prepping 32bit python build"
 908 }
 909 python_build() {
 910     if [[ -z "$PYTHON" ]]; then logerr "PYTHON not set"; fi
 911     if [[ -z "$PYTHONPATH" ]]; then logerr "PYTHONPATH not set"; fi
 912     if [[ -z "$PYTHONLIB" ]]; then logerr "PYTHONLIB not set"; fi
 913     logmsg "Building using python setup.py"
 914     pushd $TMPDIR/$BUILDDIR > /dev/null
 915 
 916     ISALIST=i386
 917     export ISALIST
 918     pre_python_32
 919     logmsg "--- setup.py (32) build"
 920     logcmd $PYTHON ./setup.py build ||
 921         logerr "--- build failed"
 922     logmsg "--- setup.py (32) install"
 923     logcmd $PYTHON \
 924         ./setup.py install --root=$DESTDIR ||
 925         logerr "--- install failed"
 926 
 927     ISALIST="amd64 i386"
 928     export ISALIST
 929     pre_python_64
 930     logmsg "--- setup.py (64) build"
 931     logcmd $PYTHON ./setup.py build ||
 932         logerr "--- build failed"
 933     logmsg "--- setup.py (64) install"
 934     logcmd $PYTHON \
 935         ./setup.py install --root=$DESTDIR ||
 936         logerr "--- install failed"
 937     popd > /dev/null
 938 
 939     mv $DESTDIR/usr/lib/python2.6/site-packages $DESTDIR/usr/lib/python2.6/vendor-packages ||
 940         logerr "Cannot move from site-packages to vendor-packages"
 941 }
 942 
 943 #############################################################################
 944 # Build/test function for perl modules
 945 #############################################################################
 946 # Detects whether to use Build.PL or Makefile.PL
 947 # Note: Build.PL probably needs Module::Build installed
 948 #############################################################################
 949 vendorizeperl() {
 950     logcmd mv $DESTDIR/usr/perl5/lib/site_perl $DESTDIR/usr/perl5/vendor_perl || logerr "can't move to vendor_perl"
 951     logcmd mkdir -p $DESTDIR/usr/perl5/${DEPVER}
 952     logcmd mv $DESTDIR/usr/perl5/man $DESTDIR/usr/perl5/${DEPVER}/man || logerr "can't move perl man"
 953 }
 954 
 955 buildperl() {
 956     if [[ -f $SRCDIR/${PROG}-${VER}.env ]]; then
 957         logmsg "Sourcing environment file: $SRCDIR/${PROG}-${VER}.env"
 958         source $SRCDIR/${PROG}-${VER}.env
 959     fi
 960     if [[ $BUILDARCH == "32" || $BUILDARCH == "both" ]]; then
 961         buildperl32
 962     fi
 963     if [[ $BUILDARCH == "64" || $BUILDARCH == "both" ]]; then
 964         buildperl64
 965     fi
 966 }
 967 
 968 buildperl32() {
 969     if [[ -f $SRCDIR/${PROG}-${VER}.env32 ]]; then
 970         logmsg "Sourcing environment file: $SRCDIR/${PROG}-${VER}.env32"
 971         source $SRCDIR/${PROG}-${VER}.env32
 972     fi
 973     pushd $TMPDIR/$BUILDDIR > /dev/null
 974     logmsg "Building 32-bit"
 975     export ISALIST="$ISAPART"
 976     local OPTS
 977     OPTS=${MAKEFILE_OPTS//_ARCH_/}
 978     OPTS=${OPTS//_ARCHBIN_/$ISAPART}
 979     if [[ -f Makefile.PL ]]; then
 980         make_clean
 981         makefilepl32 $OPTS
 982         make_prog
 983         [[ -n $PERL_MAKE_TEST ]] && make_param test
 984         make_pure_install
 985     elif [[ -f Build.PL ]]; then
 986         build_clean
 987         buildpl32 $OPTS
 988         build_prog
 989         [[ -n $PERL_MAKE_TEST ]] && build_test
 990         build_install
 991     fi
 992     popd > /dev/null
 993     unset ISALIST
 994     export ISALIST
 995 }
 996 
 997 buildperl64() {
 998     if [[ -f $SRCDIR/${PROG}-${VER}.env64 ]]; then
 999         logmsg "Sourcing environment file: $SRCDIR/${PROG}-${VER}.env64"
1000         source $SRCDIR/${PROG}-${VER}.env64
1001     fi
1002     pushd $TMPDIR/$BUILDDIR > /dev/null
1003     logmsg "Building 64-bit"
1004     local OPTS
1005     OPTS=${MAKEFILE_OPTS//_ARCH_/$ISAPART64}
1006     OPTS=${OPTS//_ARCHBIN_/$ISAPART64}
1007     if [[ -f Makefile.PL ]]; then
1008         make_clean
1009         makefilepl64 $OPTS
1010         make_prog
1011         [[ -n $PERL_MAKE_TEST ]] && make_param test
1012         make_pure_install
1013     elif [[ -f Build.PL ]]; then
1014         build_clean
1015         buildpl64 $OPTS
1016         build_prog
1017         [[ -n $PERL_MAKE_TEST ]] && build_test
1018         build_install
1019     fi
1020     popd > /dev/null
1021 }
1022 
1023 makefilepl32() {
1024     logmsg "--- Makefile.PL 32-bit"
1025     logcmd $PERL32 Makefile.PL PREFIX=$PREFIX $@ ||
1026         logerr "Failed to run Makefile.PL"
1027 }
1028 
1029 makefilepl64() {
1030     logmsg "--- Makefile.PL 64-bit"
1031     logcmd $PERL64 Makefile.PL PREFIX=$PREFIX $@ ||
1032         logerr "Failed to run Makefile.PL"
1033 }
1034 
1035 buildpl32() {
1036     logmsg "--- Build.PL 32-bit"
1037     logcmd $PERL32 Build.PL prefix=$PREFIX $@ ||
1038         logerr "Failed to run Build.PL"
1039 }
1040 
1041 buildpl64() {
1042     logmsg "--- Build.PL 64-bit"
1043     logcmd $PERL64 Build.PL prefix=$PREFIX $@ ||
1044         logerr "Failed to run Build.PL"
1045 }
1046 
1047 build_clean() {
1048     logmsg "--- Build (dist)clean"
1049     logcmd ./Build distclean || \
1050     logcmd ./Build clean || \
1051         logmsg "--- *** WARNING *** make (dist)clean Failed"
1052 }
1053 
1054 build_prog() {
1055     logmsg "--- Build"
1056     logcmd ./Build ||
1057         logerr "Build failed"
1058 }
1059 
1060 build_test() {
1061     logmsg "--- Build test"
1062     logcmd ./Build test ||
1063         logerr "Build test failed"
1064 }
1065 
1066 build_install() {
1067     logmsg "--- Build install"
1068     logcmd ./Build pure_install --destdir=$DESTDIR || \
1069         logmsg "Build install failed"
1070 }
1071 
1072 test_if_core() {
1073     logmsg "Testing whether $MODNAME is in core"
1074     logmsg "--- Ensuring ${PKG} is not installed"
1075     if logcmd pkg info ${PKG}; then
1076         logerr "------ Package ${PKG} appears to be installed.  Please uninstall it."
1077     else
1078         logmsg "------ Not installed, good." 
1079     fi
1080     if logcmd $PERL32 -M$MODNAME -e '1'; then
1081         # Module is in core, don't create a package
1082         logmsg "--- Module is in core for Perl $DEPVER.  Not creating a package."
1083         exit 0
1084     else
1085         logmsg "--- Module is not in core for Perl $DEPVER.  Continuing with build."
1086     fi
1087 }
1088 
1089 #############################################################################
1090 # Scan the destination install and strip the non-stipped ELF objects
1091 #############################################################################
1092 strip_install() {
1093     logmsg "Stripping installation"
1094     pushd $DESTDIR > /dev/null || logerr "Cannot change to installation directory"
1095     while read file
1096     do
1097         if [[ "$1" = "-x" ]]; then
1098             ACTION=$(file $file | grep ELF | egrep -v "(, stripped|debugging)")
1099         else
1100             ACTION=$(file $file | grep ELF | grep "not stripped")
1101         fi
1102         if [[ -n "$ACTION" ]]; then
1103           logmsg "------ stripping $file"
1104           MODE=$(stat -c %a "$file")
1105           logcmd chmod 644 "$file" || logerr "chmod failed: $file"
1106           logcmd strip $* "$file" || logerr "strip failed: $file"
1107           logcmd chmod $MODE "$file" || logerr "chmod failed: $file"
1108         fi
1109     done < <(find . -depth -type f)
1110     popd > /dev/null
1111 }
1112 
1113 #############################################################################
1114 # Clean up and print Done message
1115 #############################################################################
1116 clean_up() {
1117     logmsg "Cleaning up"
1118     if [[ -z $DONT_REMOVE_INSTALL_DIR ]]; then
1119         logmsg "--- Removing temporary install directory $DESTDIR"
1120         logcmd chmod -R u+w $DESTDIR > /dev/null 2>&1
1121         logcmd rm -rf $DESTDIR || \
1122             logerr "Failed to remove temporary install directory"
1123         logmsg "--- Cleaning up temporary manifest and transform files"
1124         logcmd rm -f $P5M_INT $P5M_FINAL $MY_MOG_FILE || \
1125             logerr "Failed to remove temporary manifest and transform files"
1126         logmsg "Done."
1127     fi
1128 }
1129 
1130 #############################################################################
1131 # Helper function that will let you save a predefined function so you can
1132 # override it and call it later
1133 #############################################################################
1134 save_function() {
1135     local ORIG_FUNC=$(declare -f $1)
1136     local NEWNAME_FUNC="$2${ORIG_FUNC#$1}"
1137     eval "$NEWNAME_FUNC"
1138 }
1139 
1140 # Vim hints
1141 # vim:ts=4:sw=4:et: