github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/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  # Determine the arch/os combos we're building for
    14  XC_ARCH=${XC_ARCH:-"386 amd64 arm"}
    15  XC_OS=${XC_OS:-linux darwin windows freebsd openbsd solaris}
    16  XC_EXCLUDE_OSARCH="!darwin/arm !darwin/386"
    17  
    18  # Delete the old dir
    19  echo "==> Removing old directory..."
    20  rm -f bin/*
    21  rm -rf pkg/*
    22  mkdir -p bin/
    23  
    24  # If its dev mode, only build for ourself
    25  if [[ -n "${TF_DEV}" ]]; then
    26      XC_OS=$(go env GOOS)
    27      XC_ARCH=$(go env GOARCH)
    28  fi
    29  
    30  if ! which gox > /dev/null; then
    31      echo "==> Installing gox..."
    32      go install github.com/mitchellh/gox
    33  fi
    34  
    35  # Instruct gox to build statically linked binaries
    36  export CGO_ENABLED=0
    37  
    38  # Set module download mode to readonly to not implicitly update go.mod
    39  export GOFLAGS="-mod=readonly"
    40  
    41  # In release mode we don't want debug information in the binary
    42  if [[ -n "${TF_RELEASE}" ]]; then
    43      LD_FLAGS="-s -w"
    44  fi
    45  
    46  # Ensure all remote modules are downloaded and cached before build so that
    47  # the concurrent builds launched by gox won't race to redundantly download them.
    48  go mod download
    49  
    50  # Build!
    51  echo "==> Building..."
    52  gox \
    53      -os="${XC_OS}" \
    54      -arch="${XC_ARCH}" \
    55      -osarch="${XC_EXCLUDE_OSARCH}" \
    56      -ldflags "${LD_FLAGS}" \
    57      -output "pkg/{{.OS}}_{{.Arch}}/terraform" \
    58      .
    59  
    60  # Move all the compiled things to the $GOPATH/bin
    61  GOPATH=${GOPATH:-$(go env GOPATH)}
    62  case $(uname) in
    63      CYGWIN*)
    64          GOPATH="$(cygpath $GOPATH)"
    65          ;;
    66  esac
    67  OLDIFS=$IFS
    68  IFS=: MAIN_GOPATH=($GOPATH)
    69  IFS=$OLDIFS
    70  
    71  # Create GOPATH/bin if it's doesn't exists
    72  if [ ! -d $MAIN_GOPATH/bin ]; then
    73      echo "==> Creating GOPATH/bin directory..."
    74      mkdir -p $MAIN_GOPATH/bin
    75  fi
    76  
    77  # Copy our OS/Arch to the bin/ directory
    78  DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)"
    79  if [[ -d "${DEV_PLATFORM}" ]]; then
    80      for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do
    81          cp ${F} bin/
    82          cp ${F} ${MAIN_GOPATH}/bin/
    83      done
    84  fi
    85  
    86  if [ "${TF_DEV}x" = "x" ]; then
    87      # Zip and copy to the dist dir
    88      echo "==> Packaging..."
    89      for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do
    90          OSARCH=$(basename ${PLATFORM})
    91          echo "--> ${OSARCH}"
    92  
    93          pushd $PLATFORM >/dev/null 2>&1
    94          zip ../${OSARCH}.zip ./*
    95          popd >/dev/null 2>&1
    96      done
    97  fi
    98  
    99  # Done!
   100  echo
   101  echo "==> Results:"
   102  ls -hl bin/