ajhahn.de
← eeco
Shell 82 lines
#!/bin/sh
# Cross-builds eeco for the six release targets.
# Writes archives + SHA256SUMS into dist/.
#
# Inputs (env, with defaults):
#   VERSION     git describe --tags --dirty --always, or "dev"
#   COMMIT      git short SHA, or "unknown"
#   BUILD_DATE  ISO-8601 UTC, or current date
#
# Single static binary: CGO_ENABLED=0 across the matrix.
set -eu

VERSION="${VERSION:-$(git describe --tags --dirty --always 2>/dev/null || echo dev)}"
COMMIT="${COMMIT:-$(git rev-parse --short HEAD 2>/dev/null || echo unknown)}"
BUILD_DATE="${BUILD_DATE:-$(date -u +%Y-%m-%dT%H:%M:%SZ)}"

LDFLAGS="-s -w -X main.version=${VERSION} -X main.commit=${COMMIT} -X main.buildDate=${BUILD_DATE}"

# Resolve repo root from this script's location so the build works from any cwd.
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
cd "${ROOT}"

DIST="${ROOT}/dist"
STAGE="${DIST}/_stage"
rm -rf "${STAGE}"
mkdir -p "${DIST}" "${STAGE}"

build_one() {
	GOOS="$1"
	GOARCH="$2"
	EXT=""
	ARCHIVE_EXT="tar.gz"
	if [ "${GOOS}" = "windows" ]; then
		EXT=".exe"
		ARCHIVE_EXT="zip"
	fi

	OUT_DIR="${STAGE}/${GOOS}_${GOARCH}"
	mkdir -p "${OUT_DIR}"

	printf 'build %s/%s\n' "${GOOS}" "${GOARCH}"
	CGO_ENABLED=0 GOOS="${GOOS}" GOARCH="${GOARCH}" \
		go build -trimpath -ldflags "${LDFLAGS}" \
		-o "${OUT_DIR}/eeco${EXT}" ./cmd/eeco

	cp README.md LICENSE "${OUT_DIR}/"

	# Bundle the Unix man page into non-windows archives so brew's
	# `man1.install "eeco.1"` (scripts/gen-packaging.sh) can pick it up
	# from the extracted tarball. Generated upstream by gen-manpage.sh;
	# `make release` depends on `manpage` so the file always exists.
	if [ "${GOOS}" != "windows" ] && [ -f "${DIST}/eeco.1" ]; then
		cp "${DIST}/eeco.1" "${OUT_DIR}/"
	fi

	ARCHIVE="${DIST}/eeco_${VERSION}_${GOOS}_${GOARCH}.${ARCHIVE_EXT}"
	rm -f "${ARCHIVE}"
	if [ "${ARCHIVE_EXT}" = "zip" ]; then
		( cd "${STAGE}" && zip -q -r "${ARCHIVE}" "${GOOS}_${GOARCH}" )
	else
		( cd "${STAGE}" && tar -czf "${ARCHIVE}" "${GOOS}_${GOARCH}" )
	fi
}

build_one darwin  amd64
build_one darwin  arm64
build_one linux   amd64
build_one linux   arm64
build_one windows amd64
build_one windows arm64

rm -rf "${STAGE}"

# Write SHA256SUMS over every archive in dist/ plus the standalone
# eeco.1 man page (shipped as its own release asset so direct
# downloaders can verify it).
( cd "${DIST}" && shasum -a 256 eeco_${VERSION}_*.tar.gz eeco_${VERSION}_*.zip eeco.1 > SHA256SUMS )

printf '\nartifacts:\n'
ls -1 "${DIST}"