github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/scripts/build.sh (about) 1 #!/usr/bin/env bash 2 # Copyright (c) HashiCorp, Inc. 3 # SPDX-License-Identifier: MPL-2.0 4 5 # 6 # This script builds the application from source for multiple platforms. 7 8 # Get the parent directory of where this script is. 9 SOURCE="${BASH_SOURCE[0]}" 10 while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done 11 DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )" 12 13 # Change into that directory 14 cd "$DIR" 15 16 # Determine the arch/os combos we're building for 17 XC_ARCH=${XC_ARCH:-"386 amd64 arm"} 18 XC_OS=${XC_OS:-linux darwin windows freebsd openbsd solaris} 19 XC_EXCLUDE_OSARCH="!darwin/arm !darwin/386" 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 [[ -n "${TF_DEV}" ]]; 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 install github.com/mitchellh/gox 36 fi 37 38 # Instruct gox to build statically linked binaries 39 export CGO_ENABLED=0 40 41 # Set module download mode to readonly to not implicitly update go.mod 42 export GOFLAGS="-mod=readonly" 43 44 # In release mode we don't want debug information in the binary and we don't 45 # want the -dev version marker 46 if [[ -n "${TF_RELEASE}" ]]; then 47 LD_FLAGS="-s -w -X 'github.com/terramate-io/tf/version.dev=no'" 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}}/terraform" \ 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/