github.com/hooklift/terraform@v0.11.0-beta1.0.20171117000744-6786c1361ffe/scripts/build.sh (about)

     1  #!/usr/bin/env bash
     2  #
     3  # This script builds the application from source for multiple platforms.
     4  
     5  # Get the parent directory of where this script is.
     6  SOURCE="${BASH_SOURCE[0]}"
     7  while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
     8  DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"
     9  
    10  # Change into that directory
    11  cd "$DIR"
    12  
    13  # Get the git commit
    14  GIT_COMMIT=$(git rev-parse HEAD)
    15  GIT_DIRTY=$(test -n "`git status --porcelain`" && echo "+CHANGES" || true)
    16  
    17  # Determine the arch/os combos we're building for
    18  XC_ARCH=${XC_ARCH:-"386 amd64 arm"}
    19  XC_OS=${XC_OS:-linux darwin windows freebsd openbsd solaris}
    20  XC_EXCLUDE_OSARCH="!darwin/arm !darwin/386"
    21  
    22  # Delete the old dir
    23  echo "==> Removing old directory..."
    24  rm -f bin/*
    25  rm -rf pkg/*
    26  mkdir -p bin/
    27  
    28  # If its dev mode, only build for ourself
    29  if [ "${TF_DEV}x" != "x" ]; then
    30      XC_OS=$(go env GOOS)
    31      XC_ARCH=$(go env GOARCH)
    32  fi
    33  
    34  if ! which gox > /dev/null; then
    35      echo "==> Installing gox..."
    36      go get -u github.com/mitchellh/gox
    37  fi
    38  
    39  # instruct gox to build statically linked binaries
    40  export CGO_ENABLED=0
    41  
    42  # Allow LD_FLAGS to be appended during development compilations
    43  LD_FLAGS="-X main.GitCommit=${GIT_COMMIT}${GIT_DIRTY} $LD_FLAGS"
    44  
    45  # In release mode we don't want debug information in the binary
    46  if [[ -n "${TF_RELEASE}" ]]; then
    47      LD_FLAGS="-X main.GitCommit=${GIT_COMMIT}${GIT_DIRTY} -X github.com/hashicorp/terraform/version.Prerelease= -s -w"
    48  fi
    49  
    50  # Build!
    51  echo "==> Building..."
    52  gox \
    53      -os="${XC_OS}" \
    54      -arch="${XC_ARCH}" \
    55      -osarch="${XC_EXCLUDE_OSARCH}" \
    56      -ldflags "${LD_FLAGS}" \
    57      -output "pkg/{{.OS}}_{{.Arch}}/${PWD##*/}" \
    58      .
    59  
    60  # Move all the compiled things to the $GOPATH/bin
    61  GOPATH=${GOPATH:-$(go env GOPATH)}
    62  case $(uname) in
    63      CYGWIN*)
    64          GOPATH="$(cygpath $GOPATH)"
    65          ;;
    66  esac
    67  OLDIFS=$IFS
    68  IFS=: MAIN_GOPATH=($GOPATH)
    69  IFS=$OLDIFS
    70  
    71  # Create GOPATH/bin if it's doesn't exists
    72  if [ ! -d $MAIN_GOPATH/bin ]; then
    73      echo "==> Creating GOPATH/bin directory..."
    74      mkdir -p $MAIN_GOPATH/bin
    75  fi
    76  
    77  # Copy our OS/Arch to the bin/ directory
    78  DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)"
    79  if [[ -d "${DEV_PLATFORM}" ]]; then
    80      for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do
    81          cp ${F} bin/
    82          cp ${F} ${MAIN_GOPATH}/bin/
    83      done
    84  fi
    85  
    86  if [ "${TF_DEV}x" = "x" ]; then
    87      # Zip and copy to the dist dir
    88      echo "==> Packaging..."
    89      for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do
    90          OSARCH=$(basename ${PLATFORM})
    91          echo "--> ${OSARCH}"
    92  
    93          pushd $PLATFORM >/dev/null 2>&1
    94          zip ../${OSARCH}.zip ./*
    95          popd >/dev/null 2>&1
    96      done
    97  fi
    98  
    99  # Done!
   100  echo
   101  echo "==> Results:"
   102  ls -hl bin/