Files
sx/tests/cross_compile.sh
agra 5cc62e63c3 bundling: fs/process stdlib + post-link callback + Apple .app in sx
Campaign Weeks 3-6 of /Users/agra/.claude/plans/lets-plan-to-move-splendid-pumpkin.md
land in one push: the bundling pipeline that used to live in
src/target.zig (createBundle, embedFramework, extractEntitlements,
buildInfoPlist, codesign) now lives in
library/modules/platform/bundle.sx and runs in the IR interpreter
after target.link() returns.

New language-side surface:
- library/modules/fs.sx — POSIX libc bindings (open/read/write/close,
  mkdir/unlink/rmdir, chmod, rename, access, basename/dirname). Variadic
  open() lowers to C's varargs via the new args: ..T form. Direct libc
  calls bypass *File method dispatch so they work from the post-link
  IR interpreter.
- library/modules/process.sx — popen-based run(cmd) returning
  ProcessResult{ exit_code, stdout }, plus env() and find_executable().
- library/modules/std.sx — xml_escape(s) and variadic path_join(parts).
- library/modules/compiler.sx — BuildOptions grows
  set_post_link_callback / set_post_link_module / binary_path
  accessors; bundle_path/bundle_id/codesign_identity/provisioning_profile
  setters + accessors; per-target predicates is_macos/is_ios/
  is_ios_device/is_ios_simulator + target_triple; framework_count /
  framework_at(i) / framework_path_count / framework_path_at(i);
  add_asset_dir(src, dest) + asset_dir_count / src_at / dest_at.

Compiler-side wiring:
- src/ir/compiler_hooks.zig — BuildConfig now carries post_link_callback_fn,
  post_link_module, binary_path, bundle_*, target_triple,
  target_frameworks, target_framework_paths, asset_dirs. Hook registry
  exposes every accessor; getters return "" / 0 for unset fields so
  bundle.sx can treat absent values uniformly.
- src/ir/host_ffi.zig (new) — dlsym(RTLD_DEFAULT) + arity-switched cdecl
  trampolines so #foreign("c") declarations resolve through the host
  libc during #run / post-link interpretation.
- src/ir/interp.zig — callForeign dispatch; build_config pointer
  injection so accessor hooks see live state during re-entry.
- src/core.zig — keeps the IR module alive past generateCode; exposes
  invokeByName / invokeByFuncId so main.zig can re-enter the
  interpreter after linking.
- src/main.zig — wires bundle/codesign/provisioning CLI flags +
  target_triple + framework lists into BuildConfig; invokes the
  post-link callback (by FuncId or by <module>.bundle_main lookup) once
  target.link() returns. When --bundle is set but no callback is
  registered, auto-falls-back to post_link_module = "platform.bundle"
  so the legacy --bundle CLI keeps working for any program that imports
  modules/platform/bundle.sx.

Apple .app bundler (library/modules/platform/bundle.sx):
- Single bundle_main entry covers macOS, iOS simulator, iOS device.
  Per-target Info.plist switch keys off is_ios()/is_ios_simulator() —
  iOS emits UIDeviceFamily / LSRequiresIPhoneOS /
  UIApplicationSceneManifest / DTPlatformName (iPhoneOS or
  iPhoneSimulator); macOS emits the minimal CFBundle* set.
- iOS-only steps:
  - Provisioning embed: fs.read_file + fs.write_file to
    <bundle>/embedded.mobileprovision.
  - Framework embed: recursive cp -R per -F search path into
    <bundle>/Frameworks/<Name>.framework/ (until fs.sx grows list_dir).
  - Entitlements extraction: four process.run calls (security cms -D,
    plutil -extract Entitlements xml1, plutil -extract
    ApplicationIdentifierPrefix.0, plutil -replace application-identifier)
    resolving the wildcard <TEAM>.* -> <TEAM>.<bundle_id>.
  - Real codesign with --entitlements when present.
- Asset dirs (add_asset_dir): recursive cp -R src/. into <bundle>/dest/.
  Missing src is treated as "nothing to do" so projects can register
  add_asset_dir("assets", "assets") unconditionally.

Parser:
- parseStmt() now accepts #import \"path\"; and #framework \"Name\"; as
  statement-position tokens. Needed for top-level
  inline if OS == .android { #import \"modules/platform/android.sx\"; }
  blocks (issue-0042 flatten pass surfaces them); chess's
  inline-if-with-#import was rejected at parse time before this fix.

Removals from src/target.zig:
- createBundle, embedFramework, extractEntitlements, buildInfoPlist,
  codesign (~210 lines). main.zig no longer calls createBundle after
  link(); the sx callback is the single entry point.

Tests / regression markers (all run under sx run host JIT):
- examples/115-post-link-callback.sx — callback registration round-trip.
- examples/116-fs-roundtrip.sx — fs.write_file -> fs.read_file -> exists.
- examples/117-process-roundtrip.sx — process.run + env + find_executable.
- examples/118-macos-bundle.sx — macOS .app via bundle_main callback.
- examples/119-interp-cast-ptr-cmp.sx — cast(T) val under interpreter.
- examples/120-interp-variadic-any.sx — variadic ..Any indexing in IR
  interpreter.
- examples/121-ios-sim-bundle.sx — iOS-sim cross-compile + .app with
  iOS-shaped Info.plist (added to tests/cross_compile.sh as the
  ios-sim tuple).
- examples/122-ios-device-bundle.sx — iOS device cross-compile +
  full codesign pipeline (provisioning embed + entitlements
  extraction + --entitlements codesign). Manually verified end-to-end:
  installed via xcrun devicectl device install app + launched
  successfully on iPhone 17 Pro.
- examples/123-inline-if-import-in-body.sx — locks in the parser fix.

zig build && zig build test && bash tests/run_examples.sh => 141 passed,
0 failed; bash tests/cross_compile.sh => 7 passed, 0 failed.
2026-05-22 19:03:31 +03:00

102 lines
3.7 KiB
Bash
Executable File

#!/bin/bash
# Cross-compile regression runner.
#
# For each (target, example) tuple, runs `./sx build --target <t> <example>`
# and asserts (a) exit 0 and (b) the expected output file was produced.
# Compile correctness only — these examples can't be executed on the host
# (iOS Obj-C runtime / Android NDK).
#
# Tuple list starts empty and grows as Phase 0 / 1 / 2 / 3 of the FFI plan
# add cross-only examples. Skips with a warning (still exits 0) when the
# required toolchain isn't installed, so contributors without the iOS SDK
# or Android NDK aren't blocked.
#
# Usage: ./tests/cross_compile.sh
set -uo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
SX="$ROOT_DIR/zig-out/bin/sx"
TMP_DIR="${TMPDIR:-/tmp}/sx-cross-compile"
mkdir -p "$TMP_DIR"
# Tuple format: "<target>|<example_path>"
# Add entries as cross-only examples land. Verifies the example
# compiles cleanly for the target's NDK / SDK without needing the
# host to actually run it.
TUPLES=(
"android|examples/ffi-objc-call-10-os-gate.sx"
"android|examples/ffi-jni-call-02-void.sx"
# Step 1.24: verify the inverse OS gate — `inline if OS == .android
# { #jni_call(...) }` must strip its body before lowering on iOS so
# emit_llvm doesn't try to use libjvm symbols the iOS SDK lacks.
"ios-sim|examples/ffi-jni-call-02-void.sx"
# #jni_main pipeline slice 2: an example carrying a `#jni_main
# #jni_class(...)` decl must continue to lower + link cleanly for
# android even without an APK build (compile-only check).
"android|examples/ffi-jni-main-01-emit.sx"
# `super.method(args)` dispatch: lowers to JNI CallNonvirtualVoidMethod
# against the parent class (Activity by default). Compile-only check
# — runtime correctness is verified by on-device chess deploy.
"android|examples/ffi-jni-main-02-super.sx"
# `Alias.new(args)` constructor dispatch: lowers to FindClass +
# GetMethodID("<init>") + NewObject. Compile-only — runtime via chess.
"android|examples/ffi-jni-main-03-ctor.sx"
# Week 6: iOS-simulator branch of platform.bundle. Cross-compiles
# against the iPhoneSimulator SDK; the post-link callback then
# writes an `.app` with the iOS-shaped Info.plist (UIDeviceFamily,
# LSRequiresIPhoneOS, UIApplicationSceneManifest, DTPlatformName).
"ios-sim|examples/121-ios-sim-bundle.sx"
)
PASS=0
FAIL=0
SKIP=0
toolchain_available() {
local target="$1"
case "$target" in
ios|ios-sim)
xcrun --sdk iphonesimulator --show-sdk-path >/dev/null 2>&1
;;
android|android-arm64)
# discoverAndroidNdk in target.zig accepts $ANDROID_NDK_HOME,
# $ANDROID_NDK_ROOT, or a scan of $HOME/Library/Android/sdk/ndk.
[[ -n "${ANDROID_NDK_HOME:-}" || -n "${ANDROID_NDK_ROOT:-}" ]] \
|| [[ -d "$HOME/Library/Android/sdk/ndk" ]]
;;
*)
return 1
;;
esac
}
for tuple in "${TUPLES[@]:-}"; do
[[ -z "$tuple" ]] && continue
target="${tuple%%|*}"
example="${tuple#*|}"
label="$target / $(basename "$example" .sx)"
if ! toolchain_available "$target"; then
SKIP=$((SKIP + 1))
printf " %-50s SKIP (no toolchain)\n" "$label"
continue
fi
out_obj="$TMP_DIR/$(basename "$example" .sx).$target.o"
printf " %-50s" "$label"
"$SX" build --target "$target" -o "$out_obj" "$ROOT_DIR/$example" >/dev/null 2>&1
rc=$?
if [[ $rc -eq 0 && -s "$out_obj" ]]; then
PASS=$((PASS + 1))
echo "ok"
else
FAIL=$((FAIL + 1))
echo "FAIL (exit=$rc, output=$out_obj)"
fi
done
echo "$PASS passed, $FAIL failed, $SKIP skipped"
[[ $FAIL -eq 0 ]]