github.com/serbaut/terraform@v0.6.12-0.20160607213102-ac2d195cc560/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 21 # Delete the old dir 22 echo "==> Removing old directory..." 23 rm -f bin/* 24 rm -rf pkg/* 25 mkdir -p bin/ 26 27 # If its dev mode, only build for ourself 28 if [ "${TF_DEV}x" != "x" ]; then 29 XC_OS=$(go env GOOS) 30 XC_ARCH=$(go env GOARCH) 31 fi 32 33 if ! which gox > /dev/null; then 34 echo "==> Installing gox..." 35 go get -u github.com/mitchellh/gox 36 fi 37 38 LD_FLAGS="-X main.GitCommit=${GIT_COMMIT}${GIT_DIRTY}" 39 # In relase mode we don't want debug information in the binary 40 if [[ -n "${TF_RELEASE}" ]]; then 41 LD_FLAGS="-X main.GitCommit=${GIT_COMMIT}${GIT_DIRTY} -s -w" 42 fi 43 44 # Build! 45 echo "==> Building..." 46 gox \ 47 -os="${XC_OS}" \ 48 -arch="${XC_ARCH}" \ 49 -ldflags "${LD_FLAGS}" \ 50 -output "pkg/{{.OS}}_{{.Arch}}/terraform" \ 51 . 52 53 # Move all the compiled things to the $GOPATH/bin 54 GOPATH=${GOPATH:-$(go env GOPATH)} 55 case $(uname) in 56 CYGWIN*) 57 GOPATH="$(cygpath $GOPATH)" 58 ;; 59 esac 60 OLDIFS=$IFS 61 IFS=: MAIN_GOPATH=($GOPATH) 62 IFS=$OLDIFS 63 64 # Create GOPATH/bin if it's doesn't exists 65 if [ ! -d $MAIN_GOPATH/bin ]; then 66 echo "==> Creating GOPATH/bin directory..." 67 mkdir -p $MAIN_GOPATH/bin 68 fi 69 70 # Copy our OS/Arch to the bin/ directory 71 DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)" 72 if [[ -d "${DEV_PLATFORM}" ]]; then 73 for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do 74 cp ${F} bin/ 75 cp ${F} ${MAIN_GOPATH}/bin/ 76 done 77 fi 78 79 if [ "${TF_DEV}x" = "x" ]; then 80 # Zip and copy to the dist dir 81 echo "==> Packaging..." 82 for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do 83 OSARCH=$(basename ${PLATFORM}) 84 echo "--> ${OSARCH}" 85 86 pushd $PLATFORM >/dev/null 2>&1 87 zip ../${OSARCH}.zip ./* 88 popd >/dev/null 2>&1 89 done 90 fi 91 92 # Done! 93 echo 94 echo "==> Results:" 95 ls -hl bin/