github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/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 [[ -n "${TF_DEV}" ]]; then 30 XC_OS=$(go env GOOS) 31 XC_ARCH=$(go env GOARCH) 32 33 # Allow LD_FLAGS to be appended during development compilations 34 LD_FLAGS="-X main.GitCommit=${GIT_COMMIT}${GIT_DIRTY} $LD_FLAGS" 35 fi 36 37 if ! which gox > /dev/null; then 38 echo "==> Installing gox..." 39 go get -u github.com/mitchellh/gox 40 fi 41 42 # Instruct gox to build statically linked binaries 43 export CGO_ENABLED=0 44 45 # In release mode we don't want debug information in the binary 46 if [[ -n "${TF_RELEASE}" ]]; then 47 LD_FLAGS="-s -w" 48 fi 49 50 # Ensure all remote modules are downloaded and cached before build so that 51 # the concurrent builds launched by gox won't race to redundantly download them. 52 go mod download 53 54 # Build! 55 echo "==> Building..." 56 gox \ 57 -os="${XC_OS}" \ 58 -arch="${XC_ARCH}" \ 59 -osarch="${XC_EXCLUDE_OSARCH}" \ 60 -ldflags "${LD_FLAGS}" \ 61 -output "pkg/{{.OS}}_{{.Arch}}/${PWD##*/}" \ 62 . 63 64 # Move all the compiled things to the $GOPATH/bin 65 GOPATH=${GOPATH:-$(go env GOPATH)} 66 case $(uname) in 67 CYGWIN*) 68 GOPATH="$(cygpath $GOPATH)" 69 ;; 70 esac 71 OLDIFS=$IFS 72 IFS=: MAIN_GOPATH=($GOPATH) 73 IFS=$OLDIFS 74 75 # Create GOPATH/bin if it's doesn't exists 76 if [ ! -d $MAIN_GOPATH/bin ]; then 77 echo "==> Creating GOPATH/bin directory..." 78 mkdir -p $MAIN_GOPATH/bin 79 fi 80 81 # Copy our OS/Arch to the bin/ directory 82 DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)" 83 if [[ -d "${DEV_PLATFORM}" ]]; then 84 for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do 85 cp ${F} bin/ 86 cp ${F} ${MAIN_GOPATH}/bin/ 87 done 88 fi 89 90 if [ "${TF_DEV}x" = "x" ]; then 91 # Zip and copy to the dist dir 92 echo "==> Packaging..." 93 for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do 94 OSARCH=$(basename ${PLATFORM}) 95 echo "--> ${OSARCH}" 96 97 pushd $PLATFORM >/dev/null 2>&1 98 zip ../${OSARCH}.zip ./* 99 popd >/dev/null 2>&1 100 done 101 fi 102 103 # Done! 104 echo 105 echo "==> Results:" 106 ls -hl bin/