#!/bin/sh # Build mgsh 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. # # Override the platform list to build just one: # PLATFORMS="linux/amd64" ./build.sh # # Windows is deliberately absent: mgsh shells out to stty and /bin/sh, so it # would compile there and then not work. set -e cd "$(dirname "$0")" PLATFORMS=${PLATFORMS:-"darwin/arm64 darwin/amd64 linux/amd64 linux/arm64"} V=$(cat version.txt 2>/dev/null || echo 4.0.0) # split MAJOR.MINOR.PATCH and increment PATCH (no carry: 4.0.9 -> 4.0.10) MAJOR=${V%%.*} REST=${V#*.} MINOR=${REST%%.*} PATCH=${REST#*.} PATCH=$((PATCH + 1)) NV="$MAJOR.$MINOR.$PATCH" # earlier versions built ./mgsh in the repo root; drop it so nothing keeps # running a stale binary from a path that is no longer written if [ -f mgsh ]; then rm -f mgsh echo "removed stale ./mgsh (the build now writes ./bin)" fi mkdir -p bin HOST="$(go env GOOS)/$(go env GOARCH)" for p in $PLATFORMS; do os=${p%/*} arch=${p#*/} out="bin/mgsh-$os-$arch" # CGO_ENABLED=0 throughout: it makes the cross builds work without a # toolchain per target and the binaries static, and os/user still resolves # the current user without cgo on both darwin and linux. CGO_ENABLED=0 GOOS="$os" GOARCH="$arch" \ go build -ldflags "-X main.VERSION=$NV" -o "$out" . if [ "$p" = "$HOST" ]; then ln -sf "mgsh-$os-$arch" bin/mgsh # the one for this machine echo " $out -> bin/mgsh" else echo " $out" fi done echo "$NV" > version.txt echo "built mgsh v$NV"