project-ely/scripts/common-release-build.sh

50 lines
1.2 KiB
Bash

# This is a generalized function to build a statically-linked release binary.
# It specifies all the Go flags needed to make that happen depending on the platform.
build(){
OS=$1
ARCH=$2
# Lowercase
OS=$(echo "$OS" | awk '{ print tolower($0) }')
ARCH=$(echo "$ARCH" | awk '{ print tolower($0) }')
if [ -z "${CC}" ]
then
export CC=gcc
fi
# Add .exe to Windows output
EXE=""
if [ "${OS}" = "windows" ]
then
EXE=".exe"
fi
if [ "${ARCH}" = "x86_64" ]
then
ARCH="amd64"
fi
PACKAGE="$(grep module go.mod | awk '{print $2}')"
TAG="$(git describe --tags --abbrev=0)"
VERSION="$(echo ${TAG} | awk -F '-' '{print $NF}')"
COMMIT_HASH="$(git rev-parse --short HEAD)"
BUILD_TIMESTAMP="$(date -u '+%Y-%m-%dT%H:%M:%SZ')"
OUTPUT="${TAG}-${OS}-${ARCH}${EXE}"
LDFLAGS="${LDFLAGS} -X '${PACKAGE}/internal/version.Version=${VERSION}'"
LDFLAGS="${LDFLAGS} -X '${PACKAGE}/internal/version.CommitHash=${COMMIT_HASH}'"
LDFLAGS="${LDFLAGS} -X '${PACKAGE}/internal/version.BuildTimestamp=${BUILD_TIMESTAMP}'"
export CGO_ENABLED=1
export CC="${CC}"
export GOOS=$OS
export GOARCH=$ARCH
go build -x -v \
-trimpath \
-ldflags="-s -w ${LDFLAGS}" \
-o "${OUTPUT}"
}