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