build.sh

3.0 KB · raw

1#!/bin/sh2# Builds one Koh release binary into build/releases/koh-<target>.3#4#   KOH_TARGET=linux-x86_64 sh build.sh5#6# `koh build` sets KOH_TARGET for each platform in koh.build; run by hand, the7# target defaults to the machine running the script. Runs on macOS and Linux.8#9# macOS targets need macOS: they link against the Apple SDK and are ad-hoc10# signed with codesign. Linux targets are static musl binaries built with11# `zig cc`. Every target needs its own OS and CPU: the pinned V disables12# garbage collection when cross-compiling. Linux VMs work from a Mac.13set -eu1415die() {16  echo "build.sh: $*" >&217  exit 118}1920# Paths below are relative to the repository, wherever the script is run from.21cd "$(dirname "$0")"2223# The pin is the only compiler Koh is verified against; see AGENTS.md.24command -v v >/dev/null 2>&1 || die 'V is not on PATH; install V 0.5.2 b9a2d87 (see AGENTS.md)'25[ "$(v version)" = 'V 0.5.2 b9a2d87' ] || die "Koh requires V 0.5.2 b9a2d87, found: $(v version)"2627host_target() {28  case "$(uname -s)" in29    Darwin) os=macos ;;30    Linux) os=linux ;;31    *) die "unsupported host OS: $(uname -s)" ;;32  esac33  case "$(uname -m)" in34    arm64|aarch64) cpu=aarch64 ;;35    x86_64|amd64) cpu=x86_64 ;;36    *) die "unsupported host CPU: $(uname -m)" ;;37  esac38  echo "$os-$cpu"39}4041native=$(host_target)42target=${KOH_TARGET:-$native}43case "$target" in44  macos-aarch64|macos-x86_64|linux-aarch64|linux-x86_64) ;;45  *) die "unsupported release target: $target (want macos-aarch64, macos-x86_64, linux-x86_64 or linux-aarch64)" ;;46esac47[ "$target" = "$native" ] || die "$target requires a native $target host or VM to retain garbage collection (this host is $native)"48out="build/releases/koh-$target"49mkdir -p build/releases5051case "$target" in52  macos-aarch64|macos-x86_64)53    [ "$(uname -s)" = Darwin ] || die "$target needs the Apple SDK; build it on macOS"54    case "$target" in55      macos-aarch64) arch=arm64; cpu=arm64 ;;56      *) arch=x64; cpu=x86_64 ;;57    esac58    v -prod -gc boehm -d use_bundled_libgc -arch "$arch" \59      -cflags "-arch $cpu -mmacosx-version-min=11.0" -o "$out" src/koh.v60    # Apple Silicon refuses to run an unsigned binary.61    codesign -f -s - "$out"62    ;;63  linux-aarch64|linux-x86_64)64    command -v zig >/dev/null 2>&1 || die 'Linux builds need Zig on PATH (zig cc provides the musl toolchain)'65    case "$target" in66      linux-aarch64) arch=arm64; triple=aarch64-linux-musl ;;67      *) arch=x64; triple=x86_64-linux-musl ;;68    esac69    # V's -cc takes a single program, not `zig cc -target …`, so wrap it.70    # musl has neither getcontext nor ARM64's expected __data_start symbol.71    # libgc can scan the stack and discover the data segment without them.72    compiler="$PWD/build/cc-$target"73    printf '#!/bin/sh\nexec zig cc -target %s "$@"\n' "$triple" > "$compiler"74    chmod 755 "$compiler"75    v -prod -gc boehm -d use_bundled_libgc -os linux -arch "$arch" \76      -musl -cc "$compiler" -cflags "-DNO_GETCONTEXT -DSEARCH_FOR_DATA_START" \77      -ldflags "-s" -o "$out" src/koh.v78    ;;79esac8081echo "built $out"