github.com/kimor79/packer@v0.8.7-0.20151221212622-d507b18eb4cf/scripts/build.sh (about)

     1  #!/usr/bin/env bash
     2  #
     3  # This script builds the application from source for multiple platforms.
     4  set -e
     5  
     6  # Get the parent directory of where this script is.
     7  SOURCE="${BASH_SOURCE[0]}"
     8  while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
     9  DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"
    10  
    11  # Change into that directory
    12  cd $DIR
    13  
    14  # Get the git commit
    15  GIT_COMMIT=$(git rev-parse HEAD)
    16  GIT_DIRTY=$(test -n "`git status --porcelain`" && echo "+CHANGES" || true)
    17  
    18  # If its dev mode, only build for ourself
    19  if [ "${PACKER_DEV}x" != "x" ]; then
    20      XC_OS=${XC_OS:-$(go env GOOS)}
    21      XC_ARCH=${XC_ARCH:-$(go env GOARCH)}
    22  fi
    23  
    24  # Determine the arch/os combos we're building for
    25  XC_ARCH=${XC_ARCH:-"386 amd64 arm"}
    26  XC_OS=${XC_OS:-linux darwin windows freebsd openbsd}
    27  
    28  # Install dependencies
    29  echo "==> Getting dependencies..."
    30  go get -d ./...
    31  
    32  # Delete the old dir
    33  echo "==> Removing old directory..."
    34  rm -f bin/*
    35  rm -rf pkg/*
    36  mkdir -p bin/
    37  
    38  # Build!
    39  echo "==> Building..."
    40  set +e
    41  gox \
    42      -os="${XC_OS}" \
    43      -arch="${XC_ARCH}" \
    44      -ldflags "-X main.GitCommit ${GIT_COMMIT}${GIT_DIRTY}" \
    45      -output "pkg/{{.OS}}_{{.Arch}}/packer" \
    46      .
    47  set -e
    48  
    49  # Move all the compiled things to the $GOPATH/bin
    50  GOPATH=${GOPATH:-$(go env GOPATH)}
    51  case $(uname) in
    52      CYGWIN*)
    53          GOPATH="$(cygpath $GOPATH)"
    54          ;;
    55  esac
    56  OLDIFS=$IFS
    57  IFS=:
    58  case $(uname) in
    59      MINGW*)
    60          IFS=";"
    61          ;;
    62      MSYS*)
    63          IFS=";"
    64          ;;
    65  esac
    66  MAIN_GOPATH=($GOPATH)
    67  IFS=$OLDIFS
    68  
    69  # Copy our OS/Arch to the bin/ directory
    70  echo "==> Copying binaries for this platform..."
    71  DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)"
    72  for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do
    73      cp ${F} bin/
    74      cp ${F} ${MAIN_GOPATH}/bin/
    75  done
    76  
    77  # Done!
    78  echo
    79  echo "==> Results:"
    80  ls -hl bin/