Files
goca/build.sh
T
2026-08-12 16:23:52 +02:00

71 lines
2.3 KiB
Bash
Executable File

#!/bin/sh
# Build goca for the usual platforms into ./bin, auto-incrementing the patch
# version by 0.0.1 on every build.
#
# version.txt holds the currently built version. Each run increments the patch
# component, then builds every platform with that one version injected via
# -ldflags, and writes it back. So version.txt always reflects the version of
# the binaries just built, and all of them carry the same one. The literal in
# version.go is only ever what a bare `go build` picks up.
#
# The names in ./bin are what selfupdate.go looks for in a release:
# "goca-<goos>-<goarch>", with .exe on Windows. Upload exactly these files to a
# Gitea release whose tag is the bare version number, and --update finds them.
#
# Override the platform list to build just one:
# PLATFORMS="linux/amd64" ./build.sh
#
# A plain run only ever steps the patch. A minor or major step is taken by
# naming the version outright, which is then written back like any other:
# VERSION=1.2.0 ./build.sh
#
# -s -w drops the symbol table and DWARF info, -trimpath keeps build paths out
# of the binary; together they roughly halve it. Neither affects a panic trace.
set -e
cd "$(dirname "$0")"
PLATFORMS=${PLATFORMS:-"darwin/arm64 darwin/amd64 linux/amd64 linux/arm64 windows/amd64"}
if [ -n "$VERSION" ]; then
NV="$VERSION"
else
V=$(cat version.txt 2>/dev/null || echo 1.1.2)
# split MAJOR.MINOR.PATCH and increment PATCH (no carry: 1.1.9 -> 1.1.10)
MAJOR=${V%%.*}
REST=${V#*.}
MINOR=${REST%%.*}
PATCH=${REST#*.}
PATCH=$((PATCH + 1))
NV="$MAJOR.$MINOR.$PATCH"
fi
mkdir -p bin
HOST="$(go env GOOS)/$(go env GOARCH)"
for p in $PLATFORMS; do
os=${p%/*}
arch=${p#*/}
ext=""
if [ "$os" = "windows" ]; then
ext=".exe" # Windows runs nothing without it, and --update knows that
fi
out="bin/goca-$os-$arch$ext"
# CGO_ENABLED=0 throughout: it makes the cross builds work without a
# toolchain per target and the binaries static. goca only needs the network
# and the filesystem, so nothing is lost by dropping cgo.
CGO_ENABLED=0 GOOS="$os" GOARCH="$arch" \
go build -trimpath -ldflags "-s -w -X main.Version=$NV" -o "$out" .
if [ "$p" = "$HOST" ]; then
ln -sf "goca-$os-$arch$ext" bin/goca # the one for this machine
echo " $out -> bin/goca"
else
echo " $out"
fi
done
echo "$NV" > version.txt
echo "built goca v$NV"