Files
sx/library/modules/build.sx
agra ba28488d99 P5.5: migrate the 35 BuildOptions accessors off #compiler to VM-native abi(.compiler)
`BuildOptions :: struct #compiler { ...35 methods... }` becomes
`BuildOptions :: struct { }` (an opaque null-sentinel handle) plus 35 free
`ufcs (self: BuildOptions, …) abi(.compiler)` decls in build.sx, each serviced
by a new `comptime_vm.callBuildOptionFn` arm (off `callCompilerFn`). No legacy
`compiler_lib` handler: the names are registered in `bound_fns` with a single
bailing stub only so `weldedCompilerFn` accepts them.

- String lifetime: setters dupe the arg into the persistent `Vm.gpa` (the
  Compilation allocator, threaded into both `tryEval` and `runBuildCallback` —
  not the per-eval VM arena) and write/append to the threaded `BuildConfig`.
  Getters read the field/slice or compute the target predicate from the triple.
- Dispatch routing (Option B): a `#run`/const-init entry that directly calls a
  compiler-domain/welded fn (`emit_llvm.entryNeedsVm`) runs on the VM with no
  legacy fallback regardless of the `-Dcomptime-flat` gate, so gate-OFF stays
  green without a legacy BuildOptions handler (P5.7 retires the legacy interp).
- Mark the 5 `platform/bundle.sx` getter-calling helpers `abi(.compiler)` (they
  are comptime-only bundler code; otherwise their now-welded getter calls trip
  the runtime-call gate).
- 37 `.ir` snapshots regenerated (std transitively imports build.sx → string-
  pool/type-table indices shift); verified `.ir`-only, zero behavior-stream diffs.

BuildOptions `compiler_call` strict bails gone (1609/1614/1615 strict-clean);
1616 now bails on a separate, pre-existing unported bitwise/shift VM gap (`shr`),
to port first in P5.6. 703/0 both gates.

Also sweep the outdated "flat memory" terminology to "comptime/byte-addressable"
across comptime_vm + the plan/checkpoint/CLAUDE docs: the comptime VM is
arena-backed, byte-addressable memory where `Addr` is a real host pointer, not a
flat contiguous address space (flag names `-Dcomptime-flat`/`SX_COMPTIME_FLAT` kept).
2026-06-19 13:21:09 +03:00

130 lines
7.3 KiB
Plaintext

// `List` (for the default build script) + the compiler-API build primitives
// (`emit_object`/`link`/`c_object_paths`/…). The std↔build import cycle (std's
// tail imports cli.sx which imports this file) is handled by the resolver.
#import "modules/std.sx";
#import "modules/compiler.sx";
OperatingSystem :: enum { macos; linux; windows; wasm; ios; android; unknown; }
Architecture :: enum { aarch64; x86_64; wasm32; wasm64; unknown; }
OS : OperatingSystem = .unknown;
ARCH : Architecture = .unknown;
POINTER_SIZE : i64 = 8;
// An opaque compile-time build-configuration handle. `build_options()` hands one
// back; its real state lives on the compiler's threaded `BuildConfig`. The handle
// itself is a null-sentinel word (never dereferenced) — every accessor below takes
// it as an ignored `self` and reads/writes the `BuildConfig` instead. The accessors
// are free `abi(.compiler)` functions (resolved on `opt.method(...)` via UFCS) and
// serviced by `comptime_vm.callCompilerFn` — migrated off the old `struct #compiler`
// hook surface (Phase 5.5).
BuildOptions :: struct { }
build_options :: () -> BuildOptions abi(.compiler);
// ── BuildOptions accessors (free `abi(.compiler)` functions, UFCS) ───────────
add_link_flag :: ufcs (self: BuildOptions, flag: [:0]u8) abi(.compiler);
add_framework :: ufcs (self: BuildOptions, name: [:0]u8) abi(.compiler);
set_output_path :: ufcs (self: BuildOptions, path: [:0]u8) abi(.compiler);
set_wasm_shell :: ufcs (self: BuildOptions, path: [:0]u8) abi(.compiler);
// Register a directory of runtime assets to bundle alongside the
// binary. `src` is the path on disk (relative to the CWD at build
// time); `dest` is the relative location inside the bundle / APK.
// Apple .app: copied to `<bundle>/<dest>/`. Android APK: zipped under
// `<dest>/` at the APK root. Idiomatic form is
// `opts.add_asset_dir("assets", "assets")`.
add_asset_dir :: ufcs (self: BuildOptions, src: [:0]u8, dest: [:0]u8) abi(.compiler);
asset_dir_count :: ufcs (self: BuildOptions) -> i64 abi(.compiler);
asset_dir_src_at :: ufcs (self: BuildOptions, i: i64) -> string abi(.compiler);
asset_dir_dest_at :: ufcs (self: BuildOptions, i: i64) -> string abi(.compiler);
// Name-based alternative to `on_build`. The compiler resolves
// `<module_name>.bundle_main` after linking.
set_post_link_module :: ufcs (self: BuildOptions, module_name: [:0]u8) abi(.compiler);
// Path of the freshly-linked binary, only meaningful while a
// post-link callback is running. Returns "" before linking.
binary_path :: ufcs (self: BuildOptions) -> string abi(.compiler);
// Apple `.app` / Android `.apk` bundling parameters. Accessors
// return "" when unset so the bundler can use a single string
// type. macOS bundling needs `bundle_path` and `bundle_id`;
// codesign / provisioning are iOS-device-only.
set_bundle_path :: ufcs (self: BuildOptions, path: [:0]u8) abi(.compiler);
set_bundle_id :: ufcs (self: BuildOptions, id: [:0]u8) abi(.compiler);
set_codesign_identity :: ufcs (self: BuildOptions, identity: [:0]u8) abi(.compiler);
set_provisioning_profile :: ufcs (self: BuildOptions, path: [:0]u8) abi(.compiler);
bundle_path :: ufcs (self: BuildOptions) -> string abi(.compiler);
bundle_id :: ufcs (self: BuildOptions) -> string abi(.compiler);
codesign_identity :: ufcs (self: BuildOptions) -> string abi(.compiler);
provisioning_profile :: ufcs (self: BuildOptions) -> string abi(.compiler);
// Target accessors. Empty triple before linking; predicates mirror
// TargetConfig.is{MacOS,IOS,IOSDevice,IOSSimulator}() on the Zig
// side. Used by the sx bundler to switch Info.plist shape and
// codesigning ceremony per platform.
target_triple :: ufcs (self: BuildOptions) -> string abi(.compiler);
is_macos :: ufcs (self: BuildOptions) -> bool abi(.compiler);
is_ios :: ufcs (self: BuildOptions) -> bool abi(.compiler);
is_ios_device :: ufcs (self: BuildOptions) -> bool abi(.compiler);
is_ios_simulator :: ufcs (self: BuildOptions) -> bool abi(.compiler);
is_android :: ufcs (self: BuildOptions) -> bool abi(.compiler);
// Framework list accessors. The bundler walks `framework_count() *
// framework_at(i)` to find each `-framework` name and recursively
// copies its `<Name>.framework` directory from one of
// `framework_path_at(0..framework_path_count())` into
// `<bundle>/Frameworks/`. Indexed form (slice returns are expressible
// now via `List`, but the bundler still walks these by index).
framework_count :: ufcs (self: BuildOptions) -> i64 abi(.compiler);
framework_at :: ufcs (self: BuildOptions, i: i64) -> string abi(.compiler);
framework_path_count :: ufcs (self: BuildOptions) -> i64 abi(.compiler);
framework_path_at :: ufcs (self: BuildOptions, i: i64) -> string abi(.compiler);
// Android APK bundling parameters. `manifest_path` overrides the
// bundler's auto-generated AndroidManifest.xml; `keystore_path`
// overrides the default `$HOME/.android/debug.keystore`. Accessors
// return "" when unset.
set_manifest_path :: ufcs (self: BuildOptions, path: [:0]u8) abi(.compiler);
set_keystore_path :: ufcs (self: BuildOptions, path: [:0]u8) abi(.compiler);
manifest_path :: ufcs (self: BuildOptions) -> string abi(.compiler);
keystore_path :: ufcs (self: BuildOptions) -> string abi(.compiler);
// `#jni_main #jni_class("path") { ... }` decls collected during
// lowering. The Android bundler walks `0..jni_main_count()` and
// for each entry writes a `.java` file at
// `<stage>/java/<runtime_path>.java`, compiles via javac + d8, and
// bundles the resulting classes.dex into the APK.
jni_main_count :: ufcs (self: BuildOptions) -> i64 abi(.compiler);
jni_main_runtime_path_at :: ufcs (self: BuildOptions, i: i64) -> string abi(.compiler);
jni_main_java_source_at :: ufcs (self: BuildOptions, i: i64) -> string abi(.compiler);
// The build callback registrar (Phase 5). Registers the sx function that drives
// the build — the compiler invokes it after codegen with the `BuildOptions`
// handle. `#run on_build(build);` (from inside a `#run` block) is the override
// form, where `build :: (opt: BuildOptions) -> bool abi(.compiler) { … }`; the
// last registration wins. With no override the compiler auto-invokes the stdlib
// `default_pipeline` (below). This is the sole post-build callback mechanism.
on_build :: (cb: (opt: BuildOptions) -> bool abi(.compiler)) abi(.compiler);
// ── The default build script ────────────────────────────────────────────────
//
// `default_pipeline` is the stdlib build driver: the compiler invokes it after
// codegen (everything is sx-driven — there is no auto-emit/auto-link). It emits
// the sx object, gathers the C companion objects, and links them into the output
// with the build's libraries / frameworks / flags / target. A user overrides the
// whole pipeline with their own `#run on_build(custom);` in main.sx (last-wins).
// The compiler FORCE-LOWERS this well-known name and auto-invokes it after
// codegen when no `#run on_build(custom);` override was registered (no library
// `#run` needed). A user override takes over entirely.
default_pipeline :: (opt: BuildOptions) -> bool abi(.compiler) {
obj := emit_object();
objs := c_object_paths();
objs.append(obj);
link(objs, build_output(), link_libraries(), build_frameworks(), build_flags(), build_target());
return true;
}