build: cross-compile with Nix gcc toolchains instead of zig; drop macOS; v0.1.1
ci / release (push) Canceled after 47m21s

This commit is contained in:
2026-11-22 17:56:42 +01:00
parent 82ffcaf783
commit 32ec81ff6c
8 changed files with 118 additions and 112 deletions
+29 -20
View File
@@ -1,24 +1,33 @@
#!/usr/bin/env bash
# usage: scripts/build.sh <nim-os> <nim-cpu> <zig-target> <output>
# e.g. scripts/build.sh windows amd64 x86_64-windows-gnu dist/sirmem-windows-x86_64.exe
# usage: scripts/build.sh <linux-x86_64|linux-aarch64|windows-x86_64> [output]
# Cross-compiles following https://nim-lang.org/docs/nimc.html#crossminuscompilation
# with the cross gcc from nixpkgs, fetched on demand through `nix shell`.
set -euo pipefail
cd "$(dirname "$0")/.."
os=$1 cpu=$2 target=$3 out=$4
target=${1:?target: linux-x86_64 | linux-aarch64 | windows-x86_64}
case $target in
linux-x86_64)
out=${2:-dist/sirmem-linux-x86_64}; pkg=pkgsCross.musl64; triple=x86_64-unknown-linux-musl
nimcpu=amd64 nimos=linux; flags=(--passL:-static) ;;
linux-aarch64)
out=${2:-dist/sirmem-linux-aarch64}; pkg=pkgsCross.aarch64-multiplatform-musl; triple=aarch64-unknown-linux-musl
nimcpu=arm64 nimos=linux; flags=(--passL:-static) ;;
windows-x86_64)
out=${2:-dist/sirmem-windows-x86_64.exe}; pkg=pkgsCross.mingwW64; triple=x86_64-w64-mingw32
nimcpu=amd64 nimos=windows
# gcc 15 mingw threads via mcfgthread: its import lib is not on gcc's default search path
mcf=$(nix build --no-link --print-out-paths "nixpkgs#$pkg.windows.mcfgthreads")
flags=(-d:mingw --passL:-static "--passL:-L$mcf/lib") ;;
*) echo "unknown target $target" >&2; exit 2 ;;
esac
mkdir -p "$(dirname "$out")"
export ZIG_TARGET=$target
# Notes from trial and error with zig 0.16: --opt:size breaks linking (empty undefined
# symbols); -d:useMalloc is needed on linux-musl; aarch64-linux needs --tlsEmulation:on
# (TLS relocs fail to link otherwise) but x86_64-linux fails *with* it.
extra=()
if [ "$os" = linux ] && [ "$cpu" = arm64 ]; then extra+=(--tlsEmulation:on); fi
# zig cc 0.16 sporadically fails to link aarch64-linux ("undefined symbol:" with an empty
# name, also seen as CacheCheckFailed); a retry succeeds, so retry up to 5 times.
for attempt in 1 2 3 4 5; do
rm -rf "nimcache/$target"
if nim c -d:release -d:strip -d:useMalloc --parallelBuild:1 --hints:off \
"${extra[@]}" --os:"$os" --cpu:"$cpu" \
--cc:clang --clang.exe:"$PWD/scripts/zigcc" --clang.linkerexe:"$PWD/scripts/zigcc" \
--nimcache:"nimcache/$target" -o:"$out" src/sirmem.nim ; then exit 0; fi
echo "build attempt $attempt failed, retrying" >&2
done
exit 1
# `nix shell` puts <triple>-gcc on PATH. Nim's <cpu>.<os>.gcc.* settings (as in the Nim
# cross-compilation docs) are what a cross build needs; they are ignored when target == host,
# so plain gcc.* is set too. nim.cfg pins the mingw gcc.path to /usr/bin, so path is set too.
export NIMCPU=$nimcpu NIMOS=$nimos TRIPLE=$triple OUT=$out TARGET=$target
exec nix shell "nixpkgs#$pkg.stdenv.cc" --command bash -c '
gcc=$TRIPLE-gcc; dir=$(dirname "$(command -v "$gcc")")
exec nim c -d:release -d:strip --hints:off --os:$NIMOS --cpu:$NIMCPU "$@" \
--gcc.exe:"$gcc" --gcc.linkerexe:"$gcc" \
--$NIMCPU.$NIMOS.gcc.path:"$dir" --$NIMCPU.$NIMOS.gcc.exe:"$gcc" --$NIMCPU.$NIMOS.gcc.linkerexe:"$gcc" \
--nimcache:"nimcache/$TARGET" -o:"$OUT" src/sirmem.nim' _ "${flags[@]}"