#!/usr/bin/env bash # usage: scripts/build.sh [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")/.." 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; triple=aarch64-unknown-linux-gnu # musl variant is not in cache.nixos.org (CI would compile gcc), so static glibc instead st=$(nix build --no-link --print-out-paths "nixpkgs#$pkg.glibc.static" | head -1) nimcpu=arm64 nimos=linux; flags=(--passL:-static "--passL:-L$st/lib") ;; 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")" # `nix shell` puts -gcc on PATH. Nim's ..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[@]}"