Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update build.sh #36241

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 50 additions & 53 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,105 +2,102 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: BUSL-1.1

#
# This script builds the application from source for multiple platforms.

# Get the parent directory of where this script is.
# Resolve the script's directory
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"
while [ -h "$SOURCE" ]; do
SOURCE="$(readlink "$SOURCE")"
done
DIR="$(cd -P "$(dirname "$SOURCE")/.." && pwd)"

# Change into that directory
cd "$DIR" || exit
# Navigate to the script's directory
cd "$DIR" || exit 1

# Determine the arch/os combos we're building for
# Determine the architectures and operating systems to build for
XC_ARCH=${XC_ARCH:-"386 amd64 arm"}
XC_OS=${XC_OS:-linux darwin windows freebsd openbsd solaris}
XC_OS=${XC_OS:-"linux darwin windows freebsd openbsd solaris"}
XC_EXCLUDE_OSARCH="!darwin/arm !darwin/386"

# Delete the old dir
echo "==> Removing old directory..."
rm -f bin/*
rm -rf pkg/*
# Clean up old directories
echo "==> Removing old directories..."
rm -rf bin/* pkg/*
mkdir -p bin/

# If its dev mode, only build for ourself
if [[ -n "${TF_DEV}" ]]; then
# Use dev mode settings if TF_DEV is set
if [[ -n "$TF_DEV" ]]; then
XC_OS=$(go env GOOS)
XC_ARCH=$(go env GOARCH)
fi

if ! which gox > /dev/null; then
# Install gox if not available
if ! command -v gox &>/dev/null; then
echo "==> Installing gox..."
go install github.com/mitchellh/gox
fi

# Instruct gox to build statically linked binaries
# Configure Go environment
export CGO_ENABLED=0

# Set module download mode to readonly to not implicitly update go.mod
export GOFLAGS="-mod=readonly"

# In release mode we don't want debug information in the binary and we don't
# want the -dev version marker
if [[ -n "${TF_RELEASE}" ]]; then
# Configure release mode settings
LD_FLAGS=""
if [[ -n "$TF_RELEASE" ]]; then
LD_FLAGS="-s -w -X 'github.com/hashicorp/terraform/version.dev=no'"
fi

# Ensure all remote modules are downloaded and cached before build so that
# the concurrent builds launched by gox won't race to redundantly download them.
# Pre-download all modules to prevent concurrency issues
go mod download

# Build!
# Build the binaries
echo "==> Building..."
gox \
-os="${XC_OS}" \
-arch="${XC_ARCH}" \
-osarch="${XC_EXCLUDE_OSARCH}" \
-ldflags "${LD_FLAGS}" \
-os="$XC_OS" \
-arch="$XC_ARCH" \
-osarch="$XC_EXCLUDE_OSARCH" \
-ldflags "$LD_FLAGS" \
-output "pkg/{{.OS}}_{{.Arch}}/terraform" \
.

# Move all the compiled things to the $GOPATH/bin
# Determine GOPATH
GOPATH=${GOPATH:-$(go env GOPATH)}
case $(uname) in
CYGWIN*)
GOPATH="$(cygpath $GOPATH)"
;;
CYGWIN*) GOPATH="$(cygpath "$GOPATH")" ;;
esac
OLDIFS=$IFS
IFS=: MAIN_GOPATH=($GOPATH)
IFS=$OLDIFS

# Create GOPATH/bin if it's doesn't exists
if [ ! -d $MAIN_GOPATH/bin ]; then
# Get the main GOPATH directory
IFS=: read -r MAIN_GOPATH _ <<< "$GOPATH"

# Ensure GOPATH/bin exists
if [[ ! -d "$MAIN_GOPATH/bin" ]]; then
echo "==> Creating GOPATH/bin directory..."
mkdir -p $MAIN_GOPATH/bin
mkdir -p "$MAIN_GOPATH/bin"
fi

# Copy our OS/Arch to the bin/ directory
# Copy built binaries to bin/ and GOPATH/bin
DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)"
if [[ -d "${DEV_PLATFORM}" ]]; then
for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do
cp ${F} bin/
cp ${F} ${MAIN_GOPATH}/bin/
if [[ -d "$DEV_PLATFORM" ]]; then
for FILE in "$DEV_PLATFORM"/*; do
cp "$FILE" bin/
cp "$FILE" "$MAIN_GOPATH/bin/"
done
fi

if [ "${TF_DEV}x" = "x" ]; then
# Zip and copy to the dist dir
# Package binaries for non-dev builds
if [[ -z "$TF_DEV" ]]; then
echo "==> Packaging..."
for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do
OSARCH=$(basename ${PLATFORM})
echo "--> ${OSARCH}"

pushd $PLATFORM >/dev/null 2>&1
zip ../${OSARCH}.zip ./*
popd >/dev/null 2>&1
for PLATFORM in ./pkg/*; do
if [[ -d "$PLATFORM" ]]; then
OSARCH=$(basename "$PLATFORM")
echo "--> $OSARCH"
pushd "$PLATFORM" >/dev/null
zip -r "../${OSARCH}.zip" ./*
popd >/dev/null
fi
done
fi

# Done!
# Display results
echo
echo "==> Results:"
ls -hl bin/