Files
sx/library/modules/process.sx
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

119 lines
4.6 KiB
Plaintext

#import "std.sx";
// =====================================================================
// process.sx — subprocess + environment stdlib (POSIX backend).
//
// Scope (Phase 1A): one entry point `run(cmd)` that shells out to
// /bin/sh, captures stdout, returns exit code + stdout. Plus
// `env(name)` / `find_executable(name)`. The bundler uses these to
// invoke `codesign`, `plutil`, `security`, `aapt2`, `javac`, `d8`,
// `keytool`, `apksigner`.
//
// Roadmap: phase 1B replaces `popen` with `posix_spawn` + pipes so
// we can capture stderr separately and pass argv without shell
// quoting. Until then, callers responsible for quoting + use 2>&1
// to fold stderr into the captured stream.
// =====================================================================
libc :: #library "c";
// ── Low-level libc bindings ─────────────────────────────────────────
popen :: (cmd: [:0]u8, mode: [:0]u8) -> *void #foreign libc;
pclose :: (stream: *void) -> s32 #foreign libc;
fread :: (ptr: [*]u8, size: usize, nmemb: usize, stream: *void) -> usize #foreign libc;
feof :: (stream: *void) -> s32 #foreign libc;
getenv :: (name: [:0]u8) -> *u8 #foreign libc;
strlen :: (s: *u8) -> usize #foreign libc;
system :: (cmd: [:0]u8) -> s32 #foreign libc;
// ── Public types ─────────────────────────────────────────────────────
ProcessResult :: struct {
/// Exit code as reported by `WEXITSTATUS(status)`. 0 = success.
/// Note: doesn't distinguish "killed by signal" from "exited
/// non-zero"; phase 1B will return a tagged union.
exit_code: s32;
stdout: string;
}
// ── Public API ───────────────────────────────────────────────────────
// Run a shell command, capture stdout, wait for exit. Returns null if
// the shell itself couldn't be spawned. A non-zero exit_code with
// valid stdout means the command ran and exited non-zero — distinct
// from spawn failure.
//
// `cmd` is interpreted by /bin/sh — callers MUST quote arguments
// containing spaces or shell metacharacters. To capture stderr along
// with stdout, append " 2>&1" to the command.
run :: (cmd: [:0]u8) -> ?ProcessResult {
f := popen(cmd, "r");
if cast(s64) f == 0 { return null; }
out := "";
buf := cstring(4096);
loop := true;
while loop {
n := fread(buf.ptr, 1, 4096, f);
if n == 0 { loop = false; }
if n > 0 {
chunk : string = ---;
chunk.ptr = buf.ptr;
chunk.len = cast(s64) n;
out = concat(out, chunk);
}
}
raw_status := pclose(f);
if raw_status < 0 { return null; }
// POSIX wait(2) status encoding: low byte = signal (if signaled),
// next byte = exit code (if normally exited). For our MVP we just
// surface the exit-code byte; the signal case is folded into the
// non-zero return.
exit_code := (raw_status >> 8) & 0xFF;
if exit_code == 0 {
if (raw_status & 0x7F) != 0 {
// Killed by signal — surface as a non-zero exit.
exit_code = 128 + (raw_status & 0x7F);
}
}
ProcessResult.{ exit_code = exit_code, stdout = out };
}
// Read an environment variable. Returns null if unset; an empty
// string if set to "".
env :: (name: [:0]u8) -> ?string {
p := getenv(name);
addr : s64 = xx p;
if addr == 0 { return null; }
n := strlen(p);
if n == 0 { return ""; }
buf := cstring(cast(s64) n);
memcpy(buf.ptr, xx p, cast(s64) n);
buf;
}
// Locate an executable by walking `$PATH`. Returns the absolute path
// to the first hit, or null if not found anywhere. Uses `command -v`
// under the shell; cheap and matches what the bundler ultimately
// shells out to anyway.
find_executable :: (name: [:0]u8) -> ?string {
// Compose `command -v <name>` — name is assumed shell-safe
// (executable names like `codesign`, `javac`, `aapt2`).
cmd := concat("command -v ", name);
// Need null-terminated for popen.
cmd_z := cstring(cmd.len);
memcpy(cmd_z.ptr, cmd.ptr, cmd.len);
if r := run(cmd_z) {
if r.exit_code != 0 { return null; }
// Strip the trailing newline that `command -v` emits.
out := r.stdout;
if out.len > 0 {
if out[out.len - 1] == 10 { out = substr(out, 0, out.len - 1); }
}
if out.len == 0 { return null; }
return out;
}
null;
}