github.com/ggriffiths/terraform@v0.9.0-beta1.0.20170222213024-79c4935604cb/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  # In relase mode we don't want debug information in the binary
    45  if [[ -n "${TF_RELEASE}" ]]; then
    46      LD_FLAGS="-X main.GitCommit=${GIT_COMMIT}${GIT_DIRTY} -s -w"
    47  fi
    48  
    49  # Build!
    50  echo "==> Building..."
    51  gox \
    52      -os="${XC_OS}" \
    53      -arch="${XC_ARCH}" \
    54      -osarch="${XC_EXCLUDE_OSARCH}" \
    55      -ldflags "${LD_FLAGS}" \
    56      -output "pkg/{{.OS}}_{{.Arch}}/terraform" \
    57      .
    58  
    59  # Move all the compiled things to the $GOPATH/bin
    60  GOPATH=${GOPATH:-$(go env GOPATH)}
    61  case $(uname) in
    62      CYGWIN*)
    63          GOPATH="$(cygpath $GOPATH)"
    64          ;;
    65  esac
    66  OLDIFS=$IFS
    67  IFS=: MAIN_GOPATH=($GOPATH)
    68  IFS=$OLDIFS
    69  
    70  # Create GOPATH/bin if it's doesn't exists
    71  if [ ! -d $MAIN_GOPATH/bin ]; then
    72      echo "==> Creating GOPATH/bin directory..."
    73      mkdir -p $MAIN_GOPATH/bin
    74  fi
    75  
    76  # Copy our OS/Arch to the bin/ directory
    77  DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)"
    78  if [[ -d "${DEV_PLATFORM}" ]]; then
    79      for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do
    80          cp ${F} bin/
    81          cp ${F} ${MAIN_GOPATH}/bin/
    82      done
    83  fi
    84  
    85  if [ "${TF_DEV}x" = "x" ]; then
    86      # Zip and copy to the dist dir
    87      echo "==> Packaging..."
    88      for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do
    89          OSARCH=$(basename ${PLATFORM})
    90          echo "--> ${OSARCH}"
    91  
    92          pushd $PLATFORM >/dev/null 2>&1
    93          zip ../${OSARCH}.zip ./*
    94          popd >/dev/null 2>&1
    95      done
    96  fi
    97  
    98  # Done!
    99  echo
   100  echo "==> Results:"
   101  ls -hl bin/