@native-sdk/cli 0.9.3 → 0.9.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/app.zig CHANGED
@@ -6,6 +6,94 @@
6
6
  const std = @import("std");
7
7
  const builtin = @import("builtin");
8
8
 
9
+ /// Canonicalize a generated file by its CONTENT before another build step
10
+ /// consumes it. `std.Build.Step.Run` normally places outputs under a cache
11
+ /// directory keyed by every declared input. That is correct for the producer,
12
+ /// but it means an unrelated input can change the output PATH even when the
13
+ /// file's bytes are identical; downstream Run steps hash that path and miss
14
+ /// their own caches. TypeScript's combined frontend is exactly that shape:
15
+ /// service implementation edits re-run the checker while often leaving the
16
+ /// core ABI contract byte-identical.
17
+ ///
18
+ /// This narrow adapter gives equal bytes one immutable cache path. Consumers
19
+ /// still invalidate whenever the bytes change, while producer-only churn
20
+ /// stops here. It is public so the repository's fixture graph can exercise
21
+ /// the same boundary as app builds.
22
+ pub fn stabilizeGeneratedFile(b: *std.Build, source: std.Build.LazyPath, basename: []const u8, trace: bool) std.Build.LazyPath {
23
+ return StableGeneratedFile.create(b, source, basename, trace).lazyPath();
24
+ }
25
+
26
+ const StableGeneratedFile = struct {
27
+ step: std.Build.Step,
28
+ source: std.Build.LazyPath,
29
+ basename: []const u8,
30
+ trace: bool,
31
+ generated: std.Build.GeneratedFile,
32
+
33
+ fn create(b: *std.Build, source: std.Build.LazyPath, basename: []const u8, trace: bool) *StableGeneratedFile {
34
+ const stable = b.allocator.create(StableGeneratedFile) catch @panic("OOM");
35
+ stable.* = .{
36
+ .step = std.Build.Step.init(.{
37
+ .id = .custom,
38
+ .name = b.fmt("stabilize generated {s}", .{basename}),
39
+ .owner = b,
40
+ .makeFn = make,
41
+ }),
42
+ .source = source.dupe(b),
43
+ .basename = b.dupePath(basename),
44
+ .trace = trace,
45
+ .generated = undefined,
46
+ };
47
+ stable.generated = .{ .step = &stable.step };
48
+ source.addStepDependencies(&stable.step);
49
+ return stable;
50
+ }
51
+
52
+ fn lazyPath(self: *StableGeneratedFile) std.Build.LazyPath {
53
+ return .{ .generated = .{ .file = &self.generated } };
54
+ }
55
+
56
+ fn make(step: *std.Build.Step, options: std.Build.Step.MakeOptions) !void {
57
+ _ = options;
58
+ const self: *StableGeneratedFile = @fieldParentPtr("step", step);
59
+ const b = step.owner;
60
+ const io = b.graph.io;
61
+ const arena = b.allocator;
62
+ const source_path = self.source.getPath3(b, step);
63
+ const bytes = source_path.root_dir.handle.readFileAlloc(io, source_path.sub_path, arena, .limited(64 * 1024 * 1024)) catch |err|
64
+ return step.fail("cannot stabilize generated {s}: {t}", .{ self.basename, err });
65
+
66
+ var digest: [std.crypto.hash.sha2.Sha256.digest_length]u8 = undefined;
67
+ std.crypto.hash.sha2.Sha256.hash(bytes, &digest, .{});
68
+ const hex = std.fmt.bytesToHex(digest, .lower);
69
+ const output_dir = b.pathJoin(&.{ "o", "native-stable", &hex });
70
+ const output_path = b.pathJoin(&.{ output_dir, self.basename });
71
+ self.generated.path = try b.cache_root.join(arena, &.{output_path});
72
+
73
+ var reused = false;
74
+ if (b.cache_root.handle.readFileAlloc(io, output_path, arena, .limited(64 * 1024 * 1024))) |existing| {
75
+ reused = std.mem.eql(u8, existing, bytes);
76
+ } else |_| {}
77
+ if (!reused) {
78
+ b.cache_root.handle.createDirPath(io, output_dir) catch |err|
79
+ return step.fail("cannot create the stable generated-output directory for {s}: {t}", .{ self.basename, err });
80
+ var atomic = b.cache_root.handle.createFileAtomic(io, output_path, .{ .replace = true }) catch |err|
81
+ return step.fail("cannot stage stable generated {s}: {t}", .{ self.basename, err });
82
+ defer atomic.deinit(io);
83
+ atomic.file.writeStreamingAll(io, bytes) catch |err|
84
+ return step.fail("cannot write stable generated {s}: {t}", .{ self.basename, err });
85
+ atomic.replace(io) catch |err|
86
+ return step.fail("cannot publish stable generated {s}: {t}", .{ self.basename, err });
87
+ }
88
+ step.result_cached = reused;
89
+ if (self.trace) std.debug.print("native build trace: {s} content {s} {s}\n", .{
90
+ self.basename,
91
+ &hex,
92
+ if (reused) "reused" else "changed",
93
+ });
94
+ }
95
+ };
96
+
9
97
  /// The shared web-layer inference contract: this build graph is one thin
10
98
  /// adapter over it (the CLI's manifest tooling and the app runner are the
11
99
  /// others), feeding it the inputs only this boundary sees — the
@@ -239,15 +327,14 @@ const core_compiler_teaching =
239
327
  " that is the default.\nDelete the setting (or spell it \"external\").\n";
240
328
 
241
329
  /// The staged TypeScript-core wiring: one generated directory holding the
242
- /// core module (the generated mirror with its staged shim runtime), the
243
- /// app's markup, and the SDK's generated-wiring entry (ts_core_main.zig
244
- /// as main.zig) — plus the compiled-core archive the app module links
245
- /// behind the mirror. Built once per app build and shared by the exe
246
- /// and test modules.
330
+ /// core module (the generated mirror with its staged shim runtime) and the
331
+ /// SDK's generated-wiring entry (ts_core_main.zig as main.zig) — plus the
332
+ /// compiled-core archive and isolated markup data object the app module
333
+ /// links. Built once per app build and shared by the exe and test modules.
247
334
  const TsCoreStage = struct {
248
335
  main_root: std.Build.LazyPath,
249
336
  /// The staged mobile wiring (ts_core_mobile.zig beside the same
250
- /// mirror/markup/registry files): the embed static library's `app`
337
+ /// mirror/registry files): the embed static library's `app`
251
338
  /// module roots here on iOS/Android targets.
252
339
  mobile_root: std.Build.LazyPath,
253
340
  /// The compiled-core archive: the app module links it (with libc,
@@ -262,6 +349,9 @@ const TsCoreStage = struct {
262
349
  /// carrier is selected. The app module links it beside the core's
263
350
  /// archive.
264
351
  service_archive: ?std.Build.LazyPath = null,
352
+ /// C source containing the primary markup bytes under stable external
353
+ /// symbols. Compiled as a tiny data object by the final app artifact.
354
+ markup_c: std.Build.LazyPath,
265
355
  migrations: std.Build.LazyPath,
266
356
  };
267
357
 
@@ -586,20 +676,20 @@ fn tsParseQuotedManifestValue(manifest_json: []const u8, comptime key: []const u
586
676
  return suffix;
587
677
  }
588
678
 
589
- /// The external core compiler's entry module (scriptc's dist/main.js),
679
+ /// The external compiler's published `scriptc` binary entrypoint,
590
680
  /// resolved by node's ancestor node_modules walk from the SDK's
591
681
  /// packages/core — the same origin the frontend toolchain resolves from
592
682
  /// (tsAliasedCompilerVersion's walk, kept in lockstep). Repo checkouts
593
683
  /// install it there with `npm ci`; the npm-installed CLI carries the
594
684
  /// compiler as a regular dependency, nested under the package on global
595
685
  /// prefixes and hoisted to the project root on local ones.
596
- fn tsExternalCompilerJs(b: *std.Build, dep: *std.Build.Dependency) ?[]const u8 {
686
+ fn tsExternalCompilerBin(b: *std.Build, dep: *std.Build.Dependency) ?[]const u8 {
597
687
  const io = b.graph.io;
598
688
  const sdk_root = tsSdkRoot(b.allocator, io, dep);
599
689
  var dir: []const u8 = b.pathJoin(&.{ sdk_root, "packages", "core" });
600
690
  while (true) {
601
691
  if (!std.mem.eql(u8, std.fs.path.basename(dir), "node_modules")) {
602
- const candidate = b.pathJoin(&.{ dir, "node_modules", "scriptc", "dist", "main.js" });
692
+ const candidate = b.pathJoin(&.{ dir, "node_modules", ".bin", if (builtin.os.tag == .windows) "scriptc.cmd" else "scriptc" });
603
693
  found: {
604
694
  std.Io.Dir.cwd().access(io, candidate, .{}) catch break :found;
605
695
  return candidate;
@@ -609,8 +699,8 @@ fn tsExternalCompilerJs(b: *std.Build, dep: *std.Build.Dependency) ?[]const u8 {
609
699
  }
610
700
  }
611
701
 
612
- fn requireTsExternalCompilerJs(b: *std.Build, dep: *std.Build.Dependency) []const u8 {
613
- return tsExternalCompilerJs(b, dep) orelse {
702
+ fn requireTsExternalCompilerBin(b: *std.Build, dep: *std.Build.Dependency) []const u8 {
703
+ return tsExternalCompilerBin(b, dep) orelse {
614
704
  const sdk_root = tsSdkRoot(dep.builder.allocator, dep.builder.graph.io, dep);
615
705
  std.debug.print(
616
706
  \\
@@ -628,6 +718,27 @@ fn requireTsExternalCompilerJs(b: *std.Build, dep: *std.Build.Dependency) []cons
628
718
  };
629
719
  }
630
720
 
721
+ /// PR 166 adds both the published compile-cache bootstrap and the optional
722
+ /// library-profile optimization field. The installed bootstrap is therefore
723
+ /// the capability marker: keep older exact pins byte-for-byte compatible;
724
+ /// once that release is installed, Debug/native-dev gets `dev` and
725
+ /// release/package artifacts get `release` automatically.
726
+ fn scriptcProfileOptimization(b: *std.Build, dep: *std.Build.Dependency, optimize: std.builtin.OptimizeMode) ?[]const u8 {
727
+ const sdk_root = tsSdkRoot(b.allocator, b.graph.io, dep);
728
+ var dir: []const u8 = b.pathJoin(&.{ sdk_root, "packages", "core" });
729
+ while (true) {
730
+ if (!std.mem.eql(u8, std.fs.path.basename(dir), "node_modules")) {
731
+ const marker = b.pathJoin(&.{ dir, "node_modules", "scriptc", "dist", "bootstrap.js" });
732
+ std.Io.Dir.cwd().access(b.graph.io, marker, .{}) catch {
733
+ dir = std.fs.path.dirname(dir) orelse return null;
734
+ continue;
735
+ };
736
+ return if (optimize == .Debug) "dev" else "release";
737
+ }
738
+ dir = std.fs.path.dirname(dir) orelse return null;
739
+ }
740
+ }
741
+
631
742
  /// The SDK dependency's real root, resolved the way both the toolchain
632
743
  /// check and its teaching name it.
633
744
  fn tsSdkRoot(allocator: std.mem.Allocator, io: std.Io, dep: *std.Build.Dependency) []const u8 {
@@ -643,11 +754,11 @@ const TsToolingConsumer = enum { app_core, sqlite_schema };
643
754
  fn tsToolingPreflight(b: *std.Build, dep: *std.Build.Dependency, consumer: TsToolingConsumer) []const u8 {
644
755
  const node = b.findProgram(&.{"node"}, &.{}) catch switch (consumer) {
645
756
  .app_core => @panic("\nbuilding a TypeScript app core needs node on PATH (the @native-sdk/core frontend checks the" ++
646
- " core at build time; the binary you ship carries no JS runtime).\nInstall Node.js 22.15+ (on the 23" ++
647
- " line: 23.5+) — https://nodejs.org or `brew install node` — and re-run.\n"),
757
+ " core at build time; the binary you ship carries no JS runtime).\nInstall Node.js 24+" ++
758
+ " — https://nodejs.org or `brew install node` — and re-run.\n"),
648
759
  .sqlite_schema => @panic("\nbuilding relational SQLite migrations needs node on PATH (the schema checker and" ++
649
760
  " migration generator run at build time; the binary you ship carries no JS runtime).\nInstall Node.js" ++
650
- " 22.15+ (on the 23 line: 23.5+) — https://nodejs.org or `brew install node` — and re-run.\n"),
761
+ " 24+ — https://nodejs.org or `brew install node` — and re-run.\n"),
651
762
  };
652
763
  switch (tsToolchainResolution(b, dep)) {
653
764
  .resolved => {},
@@ -737,7 +848,7 @@ fn tsCorePreflight(b: *std.Build, dep: *std.Build.Dependency, app_root: []const
737
848
  /// contract sidecar, and corewire generates the mirror module from THAT
738
849
  /// document — so the boot identity fence always pairs the mirror with
739
850
  /// its own compile. The staged module directory carries core.zig (the
740
- /// mirror) + its staged runtime + app.native + main.zig, so the
851
+ /// mirror) + its staged runtime + main.zig, so the
741
852
  /// generated wiring imports one fixed shape.
742
853
  fn tsCoreStage(
743
854
  b: *std.Build,
@@ -754,6 +865,8 @@ fn tsCoreStage(
754
865
  service_packages: []const ServicePackageConfig,
755
866
  service_carrier_choice: ServiceCarrierOption,
756
867
  service_pool_workers: ?u8,
868
+ build_trace: bool,
869
+ scriptc_optimization: ?[]const u8,
757
870
  ) TsCoreStage {
758
871
  const node = tsCorePreflight(b, dep, app_root);
759
872
  const window_views = collectTsWindowViews(b, app_root);
@@ -819,17 +932,20 @@ fn tsCoreStage(
819
932
  // of the reachable imports: over-approximation only re-runs the
820
933
  // check, never misses a stale input).
821
934
  const check = b.addSystemCommand(&.{node});
935
+ check.setName("native ts frontend (core + service contracts)");
822
936
  check.addFileArg(dep.path("build/ts_run.mjs"));
823
937
  check.addFileArg(dep.path("packages/core/src/cli.ts"));
824
938
  check.addFileArg(b.path(appPath(b, app_root, "src/core.ts")));
825
939
  check.addArg("--contract");
826
- const contract = check.addOutputFileArg("core.contract.json");
940
+ const contract_raw = check.addOutputFileArg("core.contract.json");
941
+ const contract = stabilizeGeneratedFile(b, contract_raw, "core.contract.json", build_trace);
827
942
  // The document's entry spelling is app-relative (the sidecar/facade
828
943
  // contract carries no machine paths).
829
944
  check.addArgs(&.{ "--contract-entry", "src/core.ts" });
830
945
  const services_contract: ?std.Build.LazyPath = if (has_services) services_contract: {
831
946
  check.addArg("--services-contract");
832
- break :services_contract check.addOutputFileArg("services.contract.json");
947
+ const raw = check.addOutputFileArg("services.contract.json");
948
+ break :services_contract stabilizeGeneratedFile(b, raw, "services.contract.json", build_trace);
833
949
  } else null;
834
950
  if (persist_capability) check.addArgs(&.{ "--capability", "persist" });
835
951
  for (service_packages) |package_entry| {
@@ -869,12 +985,14 @@ fn tsCoreStage(
869
985
  // so the profile's entry spelling and the facade file can never skew.
870
986
  const corewire_exe = corewireExe(b, dep);
871
987
  const project = b.addRunArtifact(corewire_exe);
988
+ project.setName("native corewire (core facade + profile)");
872
989
  project.addArg("--sidecar");
873
990
  project.addFileArg(contract);
874
991
  project.addArg("--facade");
875
992
  const facade = project.addOutputFileArg("core_facade.ts");
876
993
  project.addArg("--profile");
877
994
  const profile = project.addOutputFileArg("core_profile.json");
995
+ if (scriptc_optimization) |value| project.addArgs(&.{ "--optimization", value });
878
996
 
879
997
  // The fourth corewire projection is intentionally driven only by the
880
998
  // service sidecar: the generated TypeScript dispatch loop and Zig
@@ -885,14 +1003,17 @@ fn tsCoreStage(
885
1003
  var service_client: ?std.Build.LazyPath = null;
886
1004
  if (services_contract) |service_contract| {
887
1005
  const service_project = b.addRunArtifact(corewire_exe);
1006
+ service_project.setName("native corewire (service host + ABI projections)");
888
1007
  service_project.addArg("--services-sidecar");
889
1008
  service_project.addFileArg(service_contract);
890
1009
  service_project.addArg("--service-host-main");
891
1010
  const service_host_main = service_project.addOutputFileArg("service_host_main.ts");
892
1011
  service_project.addArg("--service-registry");
893
- service_registry = service_project.addOutputFileArg("services.zig");
1012
+ const service_registry_raw = service_project.addOutputFileArg("services.zig");
1013
+ service_registry = stabilizeGeneratedFile(b, service_registry_raw, "services.zig", build_trace);
894
1014
  service_project.addArg("--service-client");
895
- service_client = service_project.addOutputFileArg("services.gen.ts");
1015
+ const service_client_raw = service_project.addOutputFileArg("services.gen.ts");
1016
+ service_client = stabilizeGeneratedFile(b, service_client_raw, "services.gen.ts", build_trace);
896
1017
  const service_inproc_main: ?std.Build.LazyPath = if (service_carrier == .in_process) inproc: {
897
1018
  service_project.addArg("--service-inproc-main");
898
1019
  break :inproc service_project.addOutputFileArg("service_inproc_main.ts");
@@ -907,6 +1028,7 @@ fn tsCoreStage(
907
1028
  // throw into the tagged Error shape the pinned compiler can catch from an
908
1029
  // imported op; no deterministic profile fences participate here.
909
1030
  const service_stage_run = b.addSystemCommand(&.{node});
1031
+ service_stage_run.setName("native stage TypeScript services");
910
1032
  service_stage_run.addFileArg(dep.path("packages/core/scripts/stage_external_services.mjs"));
911
1033
  service_stage_run.addArg("--src");
912
1034
  service_stage_run.addDirectoryArg(b.path(appPath(b, app_root, "src")));
@@ -920,6 +1042,7 @@ fn tsCoreStage(
920
1042
  service_stage_run.addArg("--inproc-profile");
921
1043
  service_stage_run.addFileArg(inproc_profile);
922
1044
  }
1045
+ if (scriptc_optimization) |value| service_project.addArgs(&.{ "--optimization", value });
923
1046
  service_stage_run.addArg("--contract");
924
1047
  service_stage_run.addFileArg(service_contract);
925
1048
  service_stage_run.addArg("--out");
@@ -929,7 +1052,10 @@ fn tsCoreStage(
929
1052
  service_stage_run.has_side_effects = true;
930
1053
 
931
1054
  const service_compile = b.addSystemCommand(&.{node});
1055
+ service_compile.setName("native scriptc service compile");
1056
+ if (build_trace) service_compile.setEnvironmentVariable("SCRIPTC_TIMING", "1");
932
1057
  service_compile.addFileArg(dep.path("packages/core/scripts/run_external_service_compiler.mjs"));
1058
+ service_compile.addFileInput(dep.path("packages/core/scripts/compiler_command.mjs"));
933
1059
  service_compile.addArg("--stage");
934
1060
  service_compile.addDirectoryArg(service_stage_dir);
935
1061
  service_compile.addArg("--manifest");
@@ -961,15 +1087,15 @@ fn tsCoreStage(
961
1087
  if (b.graph.environ_map.get("NATIVE_SDK_CORE_COMPILER")) |override| {
962
1088
  service_compile.addArgs(&.{ "--compiler", override });
963
1089
  } else {
964
- const compiler_js = requireTsExternalCompilerJs(b, dep);
965
- service_compile.addArg("--compiler-js");
966
- service_compile.addFileArg(.{ .cwd_relative = compiler_js });
1090
+ const compiler_bin = requireTsExternalCompilerBin(b, dep);
1091
+ service_compile.addArgs(&.{ "--compiler", compiler_bin });
967
1092
  }
968
1093
  }
969
1094
 
970
1095
  // The compile stage: author sources + staged SDK + static surface +
971
1096
  // generated entry/profile, one scratch tree.
972
1097
  const stage_run = b.addSystemCommand(&.{node});
1098
+ stage_run.setName("native stage TypeScript core");
973
1099
  stage_run.addFileArg(dep.path("packages/core/scripts/stage_external_core.mjs"));
974
1100
  stage_run.addArg("--src");
975
1101
  stage_run.addDirectoryArg(b.path(appPath(b, app_root, "src")));
@@ -985,6 +1111,15 @@ fn tsCoreStage(
985
1111
  stage_run.addArg("--services-client");
986
1112
  stage_run.addFileArg(client);
987
1113
  }
1114
+ // `addDirectoryArg` orders the stager after a generated directory but
1115
+ // does not hash a source directory's discovered contents. The old graph
1116
+ // accidentally got core-source invalidation from changing producer cache
1117
+ // paths. Now that generated ABI files are stabilized by content, state
1118
+ // every source the stager copies explicitly: authored core files plus the
1119
+ // two SDK implementation modules. Services are excluded because they have
1120
+ // their own compile lane.
1121
+ addAppCoreTsDirInputs(b, stage_run, appPath(b, app_root, "src"));
1122
+ addStagedCoreSdkInputs(b, dep.builder, stage_run);
988
1123
  stage_run.addArg("--out");
989
1124
  const stage_dir = stage_run.addOutputDirectoryArg("stage");
990
1125
 
@@ -992,7 +1127,10 @@ fn tsCoreStage(
992
1127
  // exact pin, archive normalized, the co-emitted sidecar captured.
993
1128
  const symbol_name = externalCoreSymbolName(b, app_name);
994
1129
  const compile = b.addSystemCommand(&.{node});
1130
+ compile.setName("native scriptc core compile");
1131
+ if (build_trace) compile.setEnvironmentVariable("SCRIPTC_TIMING", "1");
995
1132
  compile.addFileArg(dep.path("packages/core/scripts/run_external_core_compiler.mjs"));
1133
+ compile.addFileInput(dep.path("packages/core/scripts/compiler_command.mjs"));
996
1134
  compile.addArg("--stage");
997
1135
  compile.addDirectoryArg(stage_dir);
998
1136
  compile.addArgs(&.{ "--name", symbol_name });
@@ -1020,13 +1158,13 @@ fn tsCoreStage(
1020
1158
  // driver still refuses a release other than the SDK's pin.
1021
1159
  compile.addArgs(&.{ "--compiler", override });
1022
1160
  } else {
1023
- const compiler_js = requireTsExternalCompilerJs(b, dep);
1024
- compile.addArg("--compiler-js");
1025
- compile.addFileArg(.{ .cwd_relative = compiler_js });
1161
+ const compiler_bin = requireTsExternalCompilerBin(b, dep);
1162
+ compile.addArgs(&.{ "--compiler", compiler_bin });
1026
1163
  }
1027
1164
 
1028
1165
  // The mirror, generated from the archive's OWN co-emitted contract.
1029
1166
  const mirror = b.addRunArtifact(corewire_exe);
1167
+ mirror.setName("native corewire (compiled-core mirror)");
1030
1168
  mirror.addArg("--sidecar");
1031
1169
  mirror.addFileArg(compiled_sidecar);
1032
1170
  mirror.addArg("--out");
@@ -1051,7 +1189,14 @@ fn tsCoreStage(
1051
1189
  \\
1052
1190
  , .{ service_carrier, service_pool_workers }));
1053
1191
  _ = staged.addCopyFile(migrations_zig, "migrations.zig");
1054
- _ = staged.addCopyFile(b.path(appPath(b, app_root, "src/app.native")), "app.native");
1192
+ // Primary markup bytes live in their own tiny C translation unit. They
1193
+ // no longer participate in Zig source analysis, so a markup edit reuses
1194
+ // the compiled app/SDK graph and performs only C data compile + relink.
1195
+ const markup_embed = b.addSystemCommand(&.{node});
1196
+ markup_embed.setName("native embed primary markup data");
1197
+ markup_embed.addFileArg(dep.path("packages/core/scripts/embed_markup_c.mjs"));
1198
+ markup_embed.addFileArg(b.path(appPath(b, app_root, "src/app.native")));
1199
+ const markup_c = markup_embed.addOutputFileArg("app_markup.c");
1055
1200
  for (window_views.sources) |source| {
1056
1201
  _ = staged.addCopyFile(b.path(appPath(b, app_root, source.source_path)), source.staged_path);
1057
1202
  }
@@ -1067,6 +1212,7 @@ fn tsCoreStage(
1067
1212
  .archive = archive,
1068
1213
  .service_exe = service_exe,
1069
1214
  .service_archive = service_archive,
1215
+ .markup_c = markup_c,
1070
1216
  .migrations = migrations_zig,
1071
1217
  };
1072
1218
  }
@@ -1156,6 +1302,37 @@ fn addAppTsDirInputs(b: *std.Build, transpile: *std.Build.Step.Run, src_path: []
1156
1302
  }
1157
1303
  }
1158
1304
 
1305
+ /// Declare exactly the author TypeScript files copied into the core compiler's
1306
+ /// scratch tree. `src/services/` is a separate compiler class and `.d.ts`
1307
+ /// files are declarations for editor/provider use, not scriptc source inputs.
1308
+ fn addAppCoreTsDirInputs(b: *std.Build, stage: *std.Build.Step.Run, src_path: []const u8) void {
1309
+ var dir = b.build_root.handle.openDir(b.graph.io, src_path, .{ .iterate = true }) catch return;
1310
+ defer dir.close(b.graph.io);
1311
+ var walker = dir.walk(b.allocator) catch return;
1312
+ defer walker.deinit();
1313
+ while (walker.next(b.graph.io) catch null) |entry| {
1314
+ if (entry.kind != .file or !std.mem.endsWith(u8, entry.basename, ".ts") or std.mem.endsWith(u8, entry.basename, ".d.ts")) continue;
1315
+ const normalized = b.dupe(entry.path);
1316
+ for (normalized) |*char| if (char.* == '\\') {
1317
+ char.* = '/';
1318
+ };
1319
+ if (std.mem.startsWith(u8, normalized, "services/")) continue;
1320
+ stage.addFileInput(b.path(b.fmt("{s}/{s}", .{ src_path, entry.path })));
1321
+ }
1322
+ }
1323
+
1324
+ /// Declare the SDK implementation modules copied by
1325
+ /// stage_external_core.mjs. A directory LazyPath supplies ordering only for
1326
+ /// this source tree; these file inputs make implementation-only SDK edits
1327
+ /// invalidate the staged tree and compiled core archive even when the public
1328
+ /// contract remains byte-identical. Public for the repository fixture graph,
1329
+ /// which exercises the same compiler boundary as app builds.
1330
+ pub fn addStagedCoreSdkInputs(b: *std.Build, sdk_builder: *std.Build, stage: *std.Build.Step.Run) void {
1331
+ for ([_][]const u8{ "text.ts", "events.ts" }) |source| {
1332
+ stage.addFileInput(sdk_builder.path(b.fmt("packages/core/sdk/{s}", .{source})));
1333
+ }
1334
+ }
1335
+
1159
1336
  fn appHasServiceFiles(b: *std.Build, app_root: []const u8) bool {
1160
1337
  const services_path = appPath(b, app_root, "src/services");
1161
1338
  var dir = b.build_root.handle.openDir(b.graph.io, services_path, .{ .iterate = true }) catch return false;
@@ -1274,6 +1451,7 @@ pub const MobileTsCore = struct {
1274
1451
  archive: std.Build.LazyPath,
1275
1452
  /// The in-process service archive, when src/services exists.
1276
1453
  service_archive: ?std.Build.LazyPath,
1454
+ markup_c: std.Build.LazyPath,
1277
1455
  /// The app.zon module the mobile wiring reads scene chrome, identity,
1278
1456
  /// and theme from (`app_manifest_zon`).
1279
1457
  manifest_mod: *std.Build.Module,
@@ -1352,6 +1530,7 @@ fn addMobileLibWithTarget(b: *std.Build, dep: *std.Build.Dependency, target: std
1352
1530
  // host link already passes).
1353
1531
  exports_mod.link_libc = true;
1354
1532
  exports_mod.addObjectFile(ts.archive);
1533
+ exports_mod.addObjectFile(markupDataObject(b, target, optimize, ts.markup_c).getEmittedBin());
1355
1534
  if (ts.service_archive) |service_archive| exports_mod.addObjectFile(service_archive);
1356
1535
  }
1357
1536
  if (options.store_capability or options.relational_capability) {
@@ -1432,6 +1611,8 @@ pub fn addAppArtifacts(b: *std.Build, dep: *std.Build.Dependency, app_options: A
1432
1611
  const optimize_request = b.option(std.builtin.OptimizeMode, "optimize", "Prioritize performance, safety, or binary size");
1433
1612
  const optimize = exampleOptimizeMode(b, optimize_request, .Debug);
1434
1613
  const app_optimize = exampleOptimizeMode(b, optimize_request, .ReleaseFast);
1614
+ const build_trace = b.option(bool, "build-trace", "Trace generated TypeScript ABI artifacts and cache reuse") orelse false;
1615
+ const scriptc_optimization = scriptcProfileOptimization(b, dep, app_optimize);
1435
1616
 
1436
1617
  // The core role is detected from the tree (never a flag or config):
1437
1618
  // builds with a custom `main` entry declared their core explicitly and
@@ -1491,6 +1672,8 @@ pub fn addAppArtifacts(b: *std.Build, dep: *std.Build.Dependency, app_options: A
1491
1672
  app_config.service_packages,
1492
1673
  service_carrier_choice,
1493
1674
  service_pool_workers,
1675
+ build_trace,
1676
+ scriptc_optimization,
1494
1677
  )
1495
1678
  else
1496
1679
  null;
@@ -1524,6 +1707,7 @@ pub fn addAppArtifacts(b: *std.Build, dep: *std.Build.Dependency, app_options: A
1524
1707
  .main_root = stage.mobile_root,
1525
1708
  .archive = stage.archive,
1526
1709
  .service_archive = stage.service_archive,
1710
+ .markup_c = stage.markup_c,
1527
1711
  .manifest_mod = b.createModule(.{ .root_source_file = b.path(appPath(b, app_options.app_root, "app.zon")) }),
1528
1712
  } else null,
1529
1713
  });
@@ -1575,9 +1759,31 @@ pub fn addAppArtifacts(b: *std.Build, dep: *std.Build.Dependency, app_options: A
1575
1759
  const options_mod = options.createModule();
1576
1760
 
1577
1761
  const app_mod = appModule(b, dep, target, app_optimize, app_options, options_mod, ts_stage, relational_migrations, app_config);
1762
+ // TypeScript app code and platform hosts are expensive Zig/Clang semantic
1763
+ // work but do not depend on primary markup bytes. Compile them once into
1764
+ // an object, then make the executable a link-only artifact over that
1765
+ // cached object plus the tiny generated markup-data object. A markup edit
1766
+ // therefore avoids re-analyzing the SDK and app runner entirely.
1767
+ const exe_root = if (ts_stage) |stage| root: {
1768
+ const app_code = b.addObject(.{
1769
+ .name = b.fmt("{s}-app-code", .{app_options.name}),
1770
+ .root_module = app_mod,
1771
+ .use_llvm = useLlvmWorkaround(target),
1772
+ });
1773
+ const link_mod = b.createModule(.{ .target = target, .optimize = app_optimize });
1774
+ link_mod.addObject(app_code);
1775
+ link_mod.addObject(markupDataObject(b, target, app_optimize, stage.markup_c));
1776
+ // Object dependencies propagate framework/system-library NAMES, but
1777
+ // Zig does not propagate the search paths or rpaths recorded on the
1778
+ // module that produced an object. Restate those path-only facts on
1779
+ // the final link module so the cached app-code split remains link-
1780
+ // equivalent to compiling the app module directly.
1781
+ addPlatformLinkSearchPaths(b, selected_platform, web_engine, cef_dir, link_mod);
1782
+ break :root link_mod;
1783
+ } else app_mod;
1578
1784
  const exe = b.addExecutable(.{
1579
1785
  .name = app_options.name,
1580
- .root_module = app_mod,
1786
+ .root_module = exe_root,
1581
1787
  // The app executable crosses the platform C seam on every host
1582
1788
  // call (the GTK host's `native_sdk_gtk_create_view` is a
1583
1789
  // 22-parameter mix of pointers, sizes, ints, and doubles), and
@@ -1629,7 +1835,14 @@ pub fn addAppArtifacts(b: *std.Build, dep: *std.Build.Dependency, app_options: A
1629
1835
  const run_step = b.step("run", "Run the app");
1630
1836
  run_step.dependOn(&run.step);
1631
1837
 
1632
- const test_app_mod = if (app_optimize == optimize) app_mod else appModule(b, dep, target, optimize, app_options, options_mod, ts_stage, relational_migrations, app_config);
1838
+ // TypeScript tests need their own module even when optimize modes match:
1839
+ // the production app module feeds the cached app-code object and must not
1840
+ // absorb the separately linked markup data object.
1841
+ const test_app_mod = if (ts_stage != null or app_optimize != optimize)
1842
+ appModule(b, dep, target, optimize, app_options, options_mod, ts_stage, relational_migrations, app_config)
1843
+ else
1844
+ app_mod;
1845
+ if (ts_stage) |stage| test_app_mod.addObject(markupDataObject(b, target, optimize, stage.markup_c));
1633
1846
  const tests = b.addTest(.{ .root_module = test_app_mod, .use_llvm = useLlvmWorkaround(target) });
1634
1847
  const test_step = b.step("test", "Run tests");
1635
1848
  test_step.dependOn(&b.addRunArtifact(tests).step);
@@ -1820,7 +2033,7 @@ fn appModule(b: *std.Build, dep: *std.Build.Dependency, target: std.Build.Resolv
1820
2033
  const app_mod = if (ts_stage) |stage|
1821
2034
  // TypeScript core: the app module roots at the staged generated
1822
2035
  // wiring (ts_core_main.zig beside the mirror core.zig, its shim
1823
- // runtime, and the app's markup).
2036
+ // runtime; primary markup bytes link as a separate data object).
1824
2037
  b.createModule(.{
1825
2038
  .root_source_file = stage.main_root,
1826
2039
  .target = target,
@@ -1859,6 +2072,15 @@ fn appModule(b: *std.Build, dep: *std.Build.Dependency, target: std.Build.Resolv
1859
2072
  return app_mod;
1860
2073
  }
1861
2074
 
2075
+ /// Compile primary markup bytes independently from the Zig app graph. The
2076
+ /// final artifact sees this as an object-file input, so a markup edit dirties
2077
+ /// only this tiny C compile and the final link.
2078
+ fn markupDataObject(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, source: std.Build.LazyPath) *std.Build.Step.Compile {
2079
+ const mod = b.createModule(.{ .target = target, .optimize = optimize });
2080
+ mod.addCSourceFile(.{ .file = source, .flags = &.{} });
2081
+ return b.addObject(.{ .name = "native-app-markup-data", .root_module = mod });
2082
+ }
2083
+
1862
2084
  /// Bare Mach-O executables launched by the dev loop do not have an app
1863
2085
  /// bundle's external Info.plist. Embed launch policy plus capture usage
1864
2086
  /// strings in the conventional section so LaunchServices starts accessory
@@ -2216,6 +2438,7 @@ fn externalModule(b: *std.Build, dep: *std.Build.Dependency, target: std.Build.R
2216
2438
  // 26.0 SDKs. Release builds never hit it (no UBSan), which is why only
2217
2439
  // Debug-built examples (standardOptimizeOption default) crashed.
2218
2440
  fn linkPlatform(b: *std.Build, dep: *std.Build.Dependency, target: std.Build.ResolvedTarget, app_mod: *std.Build.Module, exe: *std.Build.Step.Compile, platform: PlatformOption, web_engine: WebEngineOption, web_layer: bool, cef_dir: []const u8, cef_auto_install: bool) void {
2441
+ addPlatformLinkSearchPaths(b, platform, web_engine, cef_dir, app_mod);
2219
2442
  if (platform == .macos) {
2220
2443
  switch (web_engine) {
2221
2444
  .system => {
@@ -2240,14 +2463,9 @@ fn linkPlatform(b: *std.Build, dep: *std.Build.Dependency, target: std.Build.Res
2240
2463
  const flags: []const []const u8 = if (b.sysroot) |sysroot| &.{ "-fobjc-arc", "-fno-sanitize=builtin", "-ObjC++", "-std=c++17", "-stdlib=libc++", "-mmacosx-version-min=11.0", "-isysroot", sysroot, sdk_include, include_arg, define_arg } else &.{ "-fobjc-arc", "-fno-sanitize=builtin", "-ObjC++", "-std=c++17", "-stdlib=libc++", "-mmacosx-version-min=11.0", include_arg, define_arg };
2241
2464
  app_mod.addCSourceFile(.{ .file = dep.path("src/platform/macos/cef_host.mm"), .flags = flags });
2242
2465
  app_mod.addObjectFile(b.path(b.fmt("{s}/libcef_dll_wrapper/libcef_dll_wrapper.a", .{cef_dir})));
2243
- app_mod.addFrameworkPath(b.path(b.fmt("{s}/Release", .{cef_dir})));
2244
2466
  app_mod.linkFramework("Chromium Embedded Framework", .{});
2245
- app_mod.addRPath(.{ .cwd_relative = "@executable_path/Frameworks" });
2246
2467
  },
2247
2468
  }
2248
- if (b.sysroot) |sysroot| {
2249
- app_mod.addFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sysroot, "System/Library/Frameworks" }) });
2250
- }
2251
2469
  app_mod.linkFramework("AppKit", .{});
2252
2470
  // The audio playback service (the AppKit host's single AVPlayer).
2253
2471
  app_mod.linkFramework("AVFoundation", .{});
@@ -2308,9 +2526,7 @@ fn linkPlatform(b: *std.Build, dep: *std.Build.Dependency, target: std.Build.Res
2308
2526
  const define_arg = b.fmt("-DNATIVE_SDK_CEF_DIR=\"{s}\"", .{cef_dir});
2309
2527
  app_mod.addCSourceFile(.{ .file = dep.path("src/platform/linux/cef_host.cpp"), .flags = &.{ "-std=c++17", include_arg, define_arg } });
2310
2528
  app_mod.addObjectFile(b.path(b.fmt("{s}/libcef_dll_wrapper/libcef_dll_wrapper.a", .{cef_dir})));
2311
- app_mod.addLibraryPath(b.path(b.fmt("{s}/Release", .{cef_dir})));
2312
2529
  app_mod.linkSystemLibrary("cef", .{});
2313
- app_mod.addRPath(.{ .cwd_relative = "$ORIGIN" });
2314
2530
  },
2315
2531
  }
2316
2532
  app_mod.linkSystemLibrary("c", .{});
@@ -2369,7 +2585,6 @@ fn linkPlatform(b: *std.Build, dep: *std.Build.Dependency, target: std.Build.Res
2369
2585
  app_mod.addCSourceFile(.{ .file = dep.path("src/platform/windows/cef_host.cpp"), .flags = &.{ "-std=c++17", include_arg, define_arg } });
2370
2586
  app_mod.addCSourceFile(.{ .file = dep.path("src/platform/windows/gpu_surface_renderer.cpp"), .flags = &.{"-std=c++17"} });
2371
2587
  app_mod.addObjectFile(b.path(b.fmt("{s}/libcef_dll_wrapper/libcef_dll_wrapper.lib", .{cef_dir})));
2372
- app_mod.addLibraryPath(b.path(b.fmt("{s}/Release", .{cef_dir})));
2373
2588
  },
2374
2589
  }
2375
2590
  app_mod.linkSystemLibrary("c", .{});
@@ -2402,6 +2617,27 @@ fn linkPlatform(b: *std.Build, dep: *std.Build.Dependency, target: std.Build.Res
2402
2617
  }
2403
2618
  }
2404
2619
 
2620
+ /// Link-search metadata is not inherited through a compiled object in Zig's
2621
+ /// build graph. Keep every path-only platform setting in this one helper so a
2622
+ /// TypeScript app's cached app-code object and its final link receive the same
2623
+ /// framework/library lookup and runtime-search policy.
2624
+ fn addPlatformLinkSearchPaths(b: *std.Build, platform: PlatformOption, web_engine: WebEngineOption, cef_dir: []const u8, mod: *std.Build.Module) void {
2625
+ if (platform == .macos) {
2626
+ if (b.sysroot) |sysroot| {
2627
+ mod.addFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sysroot, "System/Library/Frameworks" }) });
2628
+ }
2629
+ if (web_engine == .chromium) {
2630
+ mod.addFrameworkPath(b.path(b.fmt("{s}/Release", .{cef_dir})));
2631
+ mod.addRPath(.{ .cwd_relative = "@executable_path/Frameworks" });
2632
+ }
2633
+ } else if (platform == .linux and web_engine == .chromium) {
2634
+ mod.addLibraryPath(b.path(b.fmt("{s}/Release", .{cef_dir})));
2635
+ mod.addRPath(.{ .cwd_relative = "$ORIGIN" });
2636
+ } else if (platform == .windows and web_engine == .chromium) {
2637
+ mod.addLibraryPath(b.path(b.fmt("{s}/Release", .{cef_dir})));
2638
+ }
2639
+ }
2640
+
2405
2641
  /// The vendored WebView2Loader.dll for the target architecture, relative
2406
2642
  /// to the framework root.
2407
2643
  fn webView2LoaderSubPath(target: std.Build.ResolvedTarget) []const u8 {