Fix portable binary cross builds

ober

31d4e9edc0e764652a74fbcced485258261126fb

diff --git a/support/build-binary.sh b/support/build-binary.sh
index d12df73..6993ea8 100755
--- a/support/build-binary.sh
+++ b/support/build-binary.sh
@@ -22,7 +22,8 @@ OUTPUT="$2"
 JERBOA_HOME="${JERBOA_HOME:-$(cd "$(dirname "$0")/.." && pwd)}"
 SCHEME="${SCHEME:-$JERBOA_HOME/.chez/bin/scheme}"
 JERBOA_CHEZ_PREFIX="${JERBOA_CHEZ_PREFIX:-$JERBOA_HOME/.chez}"
-LIBDIRS="${JERBOA_HOME}/lib"
+BINARY_LIBDIRS="${BINARY_LIBDIRS:-${LIBDIRS:-$JERBOA_HOME/lib}}"
+BINARY_STATIC_ENV="${BINARY_STATIC_ENV:-}"
 
 # ── Cross-compilation parameters (all optional; unset = native build) ────────
 # TARGET_MACHINE       Chez machine type to emit (e.g. ta6osx, ta6le).
@@ -104,6 +105,11 @@ CC="${CC:-$CC_DEFAULT}"
 # Allow caller to override link libs entirely (musl-static, etc.).
 OS_LIBS="${OS_LIBS_OVERRIDE:-$OS_LIBS}"
 
+STATIC_CFLAGS=""
+if [ -n "$BINARY_STATIC_ENV" ] && [ "$BINARY_STATIC_ENV" != 0 ]; then
+    STATIC_CFLAGS="-DJERBOA_BINARY_STATIC_ENV=1"
+fi
+
 # ── Determine target machine type ───────────────────────────────────────────
 if [ "$CROSS_BUILD" = yes ]; then
     MACHINE_TYPE="$TARGET_MACHINE"
@@ -143,6 +149,7 @@ else
 fi
 echo "    CC:     $CC"
 echo "    Chez:   $CSV_DIR"
+echo "    Libs:   $BINARY_LIBDIRS"
 echo ""
 
 # ── Step 1: WPO-compile entry script -> program.so ───────────────────────────
@@ -151,7 +158,7 @@ OBJ_DIR=$(mktemp -d "/tmp/jerboa-bin-obj.XXXXXX")
 trap 'rm -rf "$OBJ_DIR" "$WPO_SO" petite_boot.h scheme_boot.h program_boot.h "${OUTPUT}-main.c"' EXIT
 
 echo "==> [1/4] WPO compile"
-JERBOA_XPATCH="$JERBOA_XPATCH" "$SCHEME" --libdirs "$LIBDIRS" \
+JERBOA_XPATCH="$JERBOA_XPATCH" "$SCHEME" --libdirs "$BINARY_LIBDIRS" \
     --script "$JERBOA_HOME/support/build-boot.ss" "$SCRIPT" "$WPO_SO" "$OBJ_DIR"
 echo ""
 
@@ -205,6 +212,10 @@ static const char *write_program_tmpfile(void) {
 }
 
 int main(int argc, const char *argv[]) {
+#ifdef JERBOA_BINARY_STATIC_ENV
+    setenv("JERBOA_STATIC", "1", 1);
+#endif
+
     Sscheme_init(NULL);
     Sregister_boot_file_bytes("petite",
         (void *)petite_boot_data, petite_boot_size);
@@ -213,11 +224,11 @@ int main(int argc, const char *argv[]) {
     Sbuild_heap(NULL, NULL);
 
     const char *prog_path = write_program_tmpfile();
-    Sscheme_program(prog_path, argc, argv);
+    int status = Sscheme_program(prog_path, argc, argv);
     unlink(prog_path);
 
     Sscheme_deinit();
-    return 0;
+    return status;
 }
 CMAIN
 echo ""
@@ -233,7 +244,7 @@ for a in liblz4.a libz.a; do
 done
 
 # shellcheck disable=SC2086
-$CC -I"$CSV_DIR" -O2 \
+$CC -I"$CSV_DIR" -O2 $STATIC_CFLAGS \
     -o "$OUTPUT" \
     "${OUTPUT}-main.c" \
     "$CSV_DIR/libkernel.a" \
diff --git a/support/build-boot.ss b/support/build-boot.ss
index da4e511..04dc408 100644
--- a/support/build-boot.ss
+++ b/support/build-boot.ss
@@ -4,7 +4,8 @@
 ;;; Usage:
 ;;;   scheme --libdirs <libdirs> --script build-boot.ss <entry.ss> <output.so> [<obj-dir>]
 ;;;
-;;; <obj-dir>  Optional writable directory for compiled library .so output.
+;;; <obj-dir>  Optional writable directory for compiled library and entry
+;;;            .so/.wpo output.
 ;;;            Use when the source lib directory is read-only (e.g. Docker bind mounts).
 ;;;            If omitted, compiled output goes alongside the source files.
 ;;;
@@ -65,10 +66,20 @@
     (let* ([base (if (string-suffix? entry-file ".ss")
                    (substring entry-file 0 (- (string-length entry-file) 3))
                    entry-file)]
-           [wpo-file (string-append base ".wpo")])
+           [entry-so (and obj-dir (string-append obj-dir "/program.so"))]
+           [wpo-file (if obj-dir
+                       (string-append obj-dir "/program.wpo")
+                       (string-append base ".wpo"))])
 
-      (display (format "  compile-program ~a ...\n" entry-file) (current-error-port))
-      (compile-program entry-file)
+      (if entry-so
+        (begin
+          (display (format "  compile-program ~a -> ~a ...\n" entry-file entry-so)
+                   (current-error-port))
+          (compile-program entry-file entry-so))
+        (begin
+          (display (format "  compile-program ~a ...\n" entry-file)
+                   (current-error-port))
+          (compile-program entry-file)))
 
       (display (format "  compile-whole-program ~a -> ~a ...\n" wpo-file output-so)
                (current-error-port))
diff --git a/support/cross-cc-freebsd b/support/cross-cc-freebsd
index 7484c20..0cb6e64 100755
--- a/support/cross-cc-freebsd
+++ b/support/cross-cc-freebsd
@@ -22,8 +22,8 @@ REPO_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
 
 self="$(basename "$0")"
 case "$self" in
-    cross-cc-freebsd-amd64) arch=amd64;     triple=x86_64-unknown-freebsd14.0  ;;
-    cross-cc-freebsd-arm64) arch=arm64;     triple=aarch64-unknown-freebsd14.0 ;;
+    cross-cc-freebsd-amd64) arch=amd64;     triple=x86_64-unknown-freebsd14.0;  release_arch=amd64/amd64   ;;
+    cross-cc-freebsd-arm64) arch=arm64;     triple=aarch64-unknown-freebsd14.0; release_arch=arm64/aarch64 ;;
     *)
         echo "ERROR: cross-cc-freebsd must be invoked via cross-cc-freebsd-{amd64,arm64} symlink" >&2
         exit 1
@@ -34,7 +34,7 @@ SYSROOT="${FREEBSD_SYSROOT:-$REPO_DIR/.freebsd-sysroot/$arch}"
 [ -d "$SYSROOT/usr/include" ] || {
     echo "ERROR: FreeBSD sysroot missing at $SYSROOT (expected usr/include + usr/lib)" >&2
     echo "       Download with: curl -L -o .freebsd-sysroot/base.txz \\" >&2
-    echo "         https://download.freebsd.org/releases/$arch/14.4-RELEASE/base.txz" >&2
+    echo "         https://download.freebsd.org/releases/$release_arch/14.4-RELEASE/base.txz" >&2
     echo "       Then: tar -xf .freebsd-sysroot/base.txz -C .freebsd-sysroot/$arch ./usr/include ./usr/lib ./lib" >&2
     exit 1
 }