-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
1510 lines (1369 loc) · 71 KB
/
build.zig
File metadata and controls
1510 lines (1369 loc) · 71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const std = @import("std");
const fuzz_seed_wasms = [_][]const u8{
"diamond_call_indirect_then_load.wasm",
"loop_backedge_barrier.wasm",
"loop_body_call.wasm",
"triangle_store.wasm",
"nested_ifs_mixed.wasm",
"indirect_vs_direct_call.wasm",
"atomic_barrier.wasm",
"bulk_memory_barrier.wasm",
"tail_call_barrier.wasm",
"multi_memory_forwarding.wasm",
};
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Whether the selected target CPU can execute AOT code natively.
// Test/bench binaries tied to native AOT support are only installed on
// these arches so cross-compiled release builds for e.g. riscv64 don't
// try to compile or run the AOT execution path.
const target_arch = target.result.cpu.arch;
const aot_executable_target = switch (target_arch) {
.x86_64, .aarch64 => true,
else => false,
};
// Subset of `aot_executable_target` where the host-import trampoline
// pool is supported. Mirrors `supports_pool` in
// `src/runtime/aot/host_trampolines.zig:341`: Windows can't mmap RWX
// pages the same way, and macOS aarch64 needs MAP_JIT +
// `pthread_jit_write_protect_np` plumbing that isn't wired. The
// canon-lower (aot) dispatcher path — and therefore any
// component-importing-host-fn end-to-end test — needs this.
const aot_trampoline_pool_target = aot_executable_target and
target.result.os.tag != .windows and
!(target.result.os.tag == .macos and target_arch == .aarch64);
// codegen-bench emits x86-64 machine code but does not execute it. It can
// run on the native AOT arches with a portable timer fallback.
const bench_target = aot_executable_target;
// ── Build flags ────────────────────────────────────────────────────
const strip = b.option(bool, "strip", "Strip debug info from binaries") orelse false;
const stack_protector = b.option(bool, "stack-protector", "Enable stack protector (requires libc)") orelse false;
const link_libc = b.option(bool, "link-libc", "Link libc") orelse
(stack_protector or target.result.os.tag == .wasi);
const version_string = b.option([]const u8, "version", "Version string") orelse "dev";
const ir_property_iterations = b.option(u32, "ir-property-iters", "IR optimizer property-test iterations per shape/pass") orelse 8;
// ── Feature flags ──────────────────────────────────────────────────
const options = b.addOptions();
options.addOption([]const u8, "version", version_string);
const interp = b.option(bool, "interp", "Enable interpreter") orelse true;
options.addOption(bool, "interp", interp);
const aot = b.option(bool, "aot", "Enable AOT support") orelse true;
options.addOption(bool, "aot", aot);
const fast_interp = b.option(bool, "fast_interp", "Enable fast interpreter") orelse true;
options.addOption(bool, "fast_interp", fast_interp);
const jit = b.option(bool, "jit", "Enable LLVM JIT") orelse false;
options.addOption(bool, "jit", jit);
const fast_jit = b.option(bool, "fast_jit", "Enable fast JIT") orelse false;
options.addOption(bool, "fast_jit", fast_jit);
const wamr_compiler = b.option(bool, "wamr_compiler", "Enable AOT compiler") orelse false;
options.addOption(bool, "wamr_compiler", wamr_compiler);
const libc_builtin = b.option(bool, "libc_builtin", "Enable built-in libc") orelse true;
options.addOption(bool, "libc_builtin", libc_builtin);
const libc_wasi = b.option(bool, "libc_wasi", "Enable WASI libc") orelse true;
options.addOption(bool, "libc_wasi", libc_wasi);
const simd = b.option(bool, "simd", "Enable SIMD support") orelse true;
options.addOption(bool, "simd", simd);
const ref_types = b.option(bool, "ref_types", "Enable reference types") orelse true;
options.addOption(bool, "ref_types", ref_types);
const multi_module = b.option(bool, "multi_module", "Enable multi-module") orelse false;
options.addOption(bool, "multi_module", multi_module);
const lib_pthread = b.option(bool, "lib_pthread", "Enable pthread library") orelse false;
options.addOption(bool, "lib_pthread", lib_pthread);
const lib_wasi_threads = b.option(bool, "lib_wasi_threads", "Enable WASI threads") orelse false;
options.addOption(bool, "lib_wasi_threads", lib_wasi_threads);
const thread_mgr = b.option(bool, "thread_mgr", "Enable thread manager") orelse false;
options.addOption(bool, "thread_mgr", thread_mgr);
const debug_interp = b.option(bool, "debug_interp", "Enable interpreter debugging") orelse false;
options.addOption(bool, "debug_interp", debug_interp);
const bulk_memory = b.option(bool, "bulk_memory", "Enable bulk memory ops") orelse false;
options.addOption(bool, "bulk_memory", bulk_memory);
const shared_memory = b.option(bool, "shared_memory", "Enable shared memory") orelse false;
options.addOption(bool, "shared_memory", shared_memory);
const tail_call = b.option(bool, "tail_call", "Enable tail call") orelse false;
options.addOption(bool, "tail_call", tail_call);
const gc = b.option(bool, "gc", "Enable garbage collection") orelse false;
options.addOption(bool, "gc", gc);
const memory64 = b.option(bool, "memory64", "Enable memory64") orelse false;
options.addOption(bool, "memory64", memory64);
const multi_memory = b.option(bool, "multi_memory", "Enable multi-memory") orelse false;
options.addOption(bool, "multi_memory", multi_memory);
const exce_handling = b.option(bool, "exce_handling", "Enable exception handling") orelse false;
options.addOption(bool, "exce_handling", exce_handling);
const shared_heap = b.option(bool, "shared_heap", "Enable shared heap") orelse false;
options.addOption(bool, "shared_heap", shared_heap);
const wasi_nn = b.option(bool, "wasi_nn", "Enable WASI neural network") orelse false;
options.addOption(bool, "wasi_nn", wasi_nn);
const component_model = b.option(bool, "component_model", "Enable Component Model") orelse false;
options.addOption(bool, "component_model", component_model);
const network_tests = b.option(
bool,
"network_tests",
"Enable opt-in unit tests that perform real outbound HTTPS requests (#521)",
) orelse false;
options.addOption(bool, "network_tests", network_tests);
const skip_coldstart = b.option(bool, "skip-coldstart", "Skip cold-start budget tests (issue #395)") orelse false;
const verify_ir_triage = b.option(bool, "verify-ir-triage", "Run differential tests with the IR verifier enabled and print per-test verifier failures (issue #627)") orelse false;
const wamr_strict_canon = b.option(bool, "wamr-strict-canon", "Enable strict canonical ABI ptr/len diagnostics") orelse switch (optimize) {
.ReleaseFast => false,
else => true,
};
options.addOption(bool, "wamr_strict_canon", wamr_strict_canon);
const aot_broken_components = b.option(
bool,
"aot-broken-components",
"Run `component-examples-run` / `wasi-p2-testsuite` fixtures that currently fail under AOT-only `wamr run` (#662). Defaults true so local `zig build component-examples-run` still exercises them; CI sets to false to keep the gate green.",
) orelse true;
const config_module = options.createModule();
// ── TLS library (cataggar/tls.zig, zig16 branch) ──────────────────
// Pure-Zig TLS 1.2/1.3 client + server. Used for `wasi:http@0.3`
// incoming-handler HTTPS termination (#609): upstream Zig std ships
// only `std.crypto.tls.Client`, so the server-side handshake comes
// from this dependency. Exposes the `tls` module via `addModule`.
const tls_dep = b.dependency("tls", .{
.target = target,
.optimize = optimize,
});
const tls_module = tls_dep.module("tls");
// ── Root module for the library ────────────────────────────────────
const lib_module = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
lib_module.addImport("config", config_module);
lib_module.addImport("tls", tls_module);
// ── Static library ─────────────────────────────────────────────────
const lib = b.addLibrary(.{
.name = "wamr",
.root_module = lib_module,
});
b.installArtifact(lib);
// ── wamr executable ────────────────────────────────────────────────
const exe_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.strip = if (strip) true else null,
.stack_protector = if (stack_protector) true else null,
.link_libc = if (link_libc) true else null,
});
exe_module.addImport("config", config_module);
exe_module.addImport("wamr", lib_module);
const exe = b.addExecutable(.{
.name = "wamr",
.root_module = exe_module,
});
b.installArtifact(exe);
// ── wamrc AOT compiler ────────────────────────────────────────────
const wamrc_module = b.createModule(.{
.root_source_file = b.path("src/compiler/main.zig"),
.target = target,
.optimize = optimize,
.strip = if (strip) true else null,
.stack_protector = if (stack_protector) true else null,
.link_libc = if (link_libc) true else null,
});
wamrc_module.addImport("config", config_module);
wamrc_module.addImport("wamr", lib_module);
const wamrc = b.addExecutable(.{
.name = "wamrc",
.root_module = wamrc_module,
});
b.installArtifact(wamrc);
// ── Spec test runner ─────────────────────────────────────────────
// wabt v3.0.0-dev.4 ships both a library and a CLI exe both named "wabt",
// so `wabt_dep.artifact("wabt")` panics with "artifact name 'wabt' is
// ambiguous", and the package no longer calls `b.addModule(...)` either.
// Build the wabt library module ourselves from the dep's `src/root.zig`,
// wired with a synthetic `build_options.version` (the only build option
// wabt's root.zig consumes).
const wabt_dep = b.dependency("wabt", .{
.target = target,
.optimize = .ReleaseSafe,
});
const wabt_build_options = b.addOptions();
wabt_build_options.addOption([]const u8, "version", "v3.0.0-dev.4");
const wabt_module = b.createModule(.{
.root_source_file = wabt_dep.path("src/root.zig"),
.target = target,
.optimize = .ReleaseSafe,
});
wabt_module.addImport("build_options", wabt_build_options.createModule());
const spec_runner_module = b.createModule(.{
.root_source_file = b.path("src/tests/run_spec_tests.zig"),
.target = target,
.optimize = .ReleaseSafe,
});
spec_runner_module.addImport("config", config_module);
spec_runner_module.addImport("wamr", lib_module);
spec_runner_module.addImport("wabt", wabt_module);
const spec_runner_exe = b.addExecutable(.{
.name = "spec-test-runner",
.root_module = spec_runner_module,
});
if (aot_executable_target) b.installArtifact(spec_runner_exe);
// Run the spec suite through the AOT pipeline. Non-blocking convenience
// step; not wired into the default `test` aggregate while codegen gaps
// and the skiplist stabilize (see src/tests/aot_skiplist.zig).
const run_spec_aot = b.addRunArtifact(spec_runner_exe);
run_spec_aot.addArg("--mode=aot");
run_spec_aot.addArg("tests/spec-json");
const spec_aot_step = b.step("spec-tests-aot", "Run the spec-json suite through the AOT pipeline");
spec_aot_step.dependOn(&run_spec_aot.step);
// ── WASI conformance suite ────────────────────────────────────────
// Drives the vendored `WebAssembly/wasi-testsuite` against the just-built
// `wamr` CLI through the in-tree adapter. Skiplist entries must each
// carry a rationale + follow-up issue number — see
// `tests/wasi-testsuite-skip.json`. Not wired into the default `test`
// aggregate (it requires Python 3 + the runner's deps), but the CI job
// gates regressions on every PR. Run locally with `zig build wasi-testsuite`.
// The runner entry point is wrapped through
// `tests/wasi-testsuite-runner-patch/wasi_test_runner.py`, which
// imports the upstream package unmodified and monkey-patches
// `TestCaseRunner.do_wait` to honour the `WAMR_TESTSUITE_TIMEOUT`
// env var (seconds; defaults to upstream's hard-coded 5s when
// unset). Without that override, slow developer VMs flake on
// `http-fields` and other fixtures whose runtime drifts past 5s.
// See #583 A7 + the wrapper's module docstring for rationale.
const wasi_runner = b.addSystemCommand(&.{
"python3",
"tests/wasi-testsuite-runner-patch/wasi_test_runner.py",
"--test-suite",
"tests/wasi-testsuite/tests/c/testsuite/wasm32-wasip1",
"tests/wasi-testsuite/tests/rust/testsuite/wasm32-wasip1",
"tests/wasi-testsuite/tests/assemblyscript/testsuite/wasm32-wasip1",
"--runtime-adapter",
"tests/wasi-testsuite-adapter/wamr-zig.py",
"--exclude-filter",
"tests/wasi-testsuite-skip.json",
});
// Point the adapter at the freshly-installed wamr binary so we don't pick
// up a stale system iwasm.
wasi_runner.setEnvironmentVariable("WAMR", b.getInstallPath(.bin, "wamr"));
wasi_runner.setEnvironmentVariable("WAMRC", b.getInstallPath(.bin, "wamrc"));
wasi_runner.step.dependOn(b.getInstallStep());
const wasi_testsuite_step = b.step(
"wasi-testsuite",
"Run the WebAssembly/wasi-testsuite conformance suite",
);
wasi_testsuite_step.dependOn(&wasi_runner.step);
// ── WASI Preview 3 conformance gate (#489) ────────────────────────
// Drives the vendored `wasm32-wasip3` fixtures at
// `tests/wasi-testsuite/tests/rust/testsuite/wasm32-wasip3/` through
// the just-built `wamr` CLI via the same in-tree adapter as the
// Preview 1 gate. Acts as a CI gate against regressions in the WASI
// Preview 3 adapter surface (`src/component/wasi_cli_adapter.zig`,
// P3 wave A–C: #481–#487). Skip-list entries must each carry a
// rationale + tracking issue — see `tests/wasi-p3-testsuite-skip.json`.
// Not wired into the default `test` aggregate (it requires Python 3
// + the runner's deps); CI gates regressions on every PR. Run
// locally with `zig build wasi-p3-testsuite`.
const wasi_p3_runner = b.addSystemCommand(&.{
"python3",
"tests/wasi-testsuite-runner-patch/wasi_test_runner.py",
"--test-suite",
"tests/wasi-testsuite/tests/rust/testsuite/wasm32-wasip3",
"--runtime-adapter",
"tests/wasi-testsuite-adapter/wamr-zig.py",
"--exclude-filter",
"tests/wasi-p3-testsuite-skip.json",
});
wasi_p3_runner.setEnvironmentVariable("WAMR", b.getInstallPath(.bin, "wamr"));
wasi_p3_runner.setEnvironmentVariable("WAMRC", b.getInstallPath(.bin, "wamrc"));
wasi_p3_runner.step.dependOn(b.getInstallStep());
const wasi_p3_testsuite_step = b.step(
"wasi-p3-testsuite",
"Run the WASI Preview 3 conformance gate (wasm32-wasip3 fixtures)",
);
wasi_p3_testsuite_step.dependOn(&wasi_p3_runner.step);
// ── Wasmtime parity gate (#583 C1, original #489 proposal) ────────
// Runs the *same* `wasm32-wasip3` fixtures through upstream Wasmtime
// (CI pin: v44.0.1, the first release with `-Sp3` support — see the
// `component-examples-wasmtime` job in `.github/workflows/ci.yml`).
// The wasm test corpus is identical, so a wamr regression that
// Wasmtime *also* exhibits flags as a fixture bug rather than a
// wamr bug (see `scripts/diff-testsuite-reports.py`).
//
// Resolves the Wasmtime binary from the `WASMTIME` env var (set by
// CI; defaults to `wasmtime` on `PATH`). The `WASMTIME` env var is
// intentionally not pinned in-tree so a developer can point the
// step at a locally-built wasmtime for triage. The CI workflow at
// `.github/workflows/wasi-p3-parity.yml` is the source of truth
// for the pinned version.
const wasi_p3_runner_wasmtime = b.addSystemCommand(&.{
"python3",
"tests/wasi-testsuite-runner-patch/wasi_test_runner.py",
"--test-suite",
"tests/wasi-testsuite/tests/rust/testsuite/wasm32-wasip3",
"--runtime-adapter",
"tests/wasi-testsuite-adapter/wasmtime.py",
"--exclude-filter",
"tests/wasi-p3-testsuite-skip.json",
});
// No `WAMR` env var needed here; the wasmtime adapter reads
// `WASMTIME` instead (the test binaries themselves are the same
// `*.wasm` files that the wamr-side gate exercises).
const wasi_p3_testsuite_wasmtime_step = b.step(
"wasi-p3-testsuite-wasmtime",
"Run the WASI Preview 3 fixtures through Wasmtime (#583 C1 parity gate)",
);
wasi_p3_testsuite_wasmtime_step.dependOn(&wasi_p3_runner_wasmtime.step);
// ── Wasmtime parity diff ──────────────────────────────────────────
// Convenience step that runs both runtimes through
// `scripts/wasi-p3-parity.py` (which writes JSON reports + a
// classifier summary, then forwards the diff exit code). The
// orchestrator is a Python script because Wasmtime can exit
// non-zero on its own (e.g. v44.0.1 fails 4 / 40 fixtures —
// `http-service`, `sockets-tcp-{connect,listen}`,
// `sockets-udp-send`); we need to keep going through the diff
// step to classify the deltas as regressions vs fixture/runtime
// bugs. Output JSONs live under `zig-out/test-reports/` so CI
// can upload them as artifacts on failure.
const reports_dir = b.pathJoin(&.{ b.install_path, "test-reports" });
const parity_orchestrator = b.addSystemCommand(&.{
"python3",
"scripts/wasi-p3-parity.py",
"--output-dir",
reports_dir,
});
parity_orchestrator.setEnvironmentVariable("WAMR", b.getInstallPath(.bin, "wamr"));
parity_orchestrator.step.dependOn(b.getInstallStep());
const wasi_p3_parity_step = b.step(
"wasi-p3-parity",
"Run wamr + Wasmtime against wasm32-wasip3 fixtures and diff (#583 C1)",
);
wasi_p3_parity_step.dependOn(&parity_orchestrator.step);
// ── Tests ──────────────────────────────────────────────────────────
const test_module = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
test_module.addImport("config", config_module);
test_module.addImport("tls", tls_module);
const lib_unit_tests = b.addTest(.{
.root_module = test_module,
});
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
const exe_test_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
exe_test_module.addImport("config", config_module);
exe_test_module.addImport("wamr", lib_module);
const exe_unit_tests = b.addTest(.{
.root_module = exe_test_module,
});
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
test_step.dependOn(&run_exe_unit_tests.step);
// wamrc unit tests (subcommand parsing, deriveOutputPath).
const wamrc_test_module = b.createModule(.{
.root_source_file = b.path("src/compiler/main.zig"),
.target = target,
.optimize = optimize,
});
wamrc_test_module.addImport("config", config_module);
wamrc_test_module.addImport("wamr", lib_module);
const wamrc_unit_tests = b.addTest(.{
.root_module = wamrc_test_module,
});
const run_wamrc_unit_tests = b.addRunArtifact(wamrc_unit_tests);
test_step.dependOn(&run_wamrc_unit_tests.step);
// CLI smoke assertions: subcommand layout, exit codes, version stdout.
{
const wamr_version_line = b.fmt("wamr {s}\n", .{version_string});
const wamrc_version_line = b.fmt("wamrc {s}\n", .{version_string});
const wamr_version_run = b.addRunArtifact(exe);
wamr_version_run.addArg("version");
wamr_version_run.expectExitCode(0);
wamr_version_run.expectStdOutEqual(wamr_version_line);
test_step.dependOn(&wamr_version_run.step);
const wamr_long_version_run = b.addRunArtifact(exe);
wamr_long_version_run.addArg("--version");
wamr_long_version_run.expectExitCode(0);
wamr_long_version_run.expectStdOutEqual(wamr_version_line);
test_step.dependOn(&wamr_long_version_run.step);
const wamrc_version_run = b.addRunArtifact(wamrc);
wamrc_version_run.addArg("version");
wamrc_version_run.expectExitCode(0);
wamrc_version_run.expectStdOutEqual(wamrc_version_line);
test_step.dependOn(&wamrc_version_run.step);
const wamr_help_run = b.addRunArtifact(exe);
wamr_help_run.addArgs(&.{ "help", "run" });
wamr_help_run.expectExitCode(0);
test_step.dependOn(&wamr_help_run.step);
const wamrc_help_compile = b.addRunArtifact(wamrc);
wamrc_help_compile.addArgs(&.{ "help", "compile" });
wamrc_help_compile.expectExitCode(0);
test_step.dependOn(&wamrc_help_compile.step);
const wamrc_help_run = b.addRunArtifact(wamrc);
wamrc_help_run.addArgs(&.{ "help", "run" });
wamrc_help_run.expectExitCode(0);
test_step.dependOn(&wamrc_help_run.step);
}
// `wamrc run` smoke (#665): compile + spawn the just-built `wamr`
// against the 36-byte noop core wasm. Exercises the full subprocess
// pipeline (sibling-output → freshness sidecar → wamr discovery via
// `WAMR_BIN` → exit-code propagation). Output goes to a build-cache
// file via `-o` so the source tree stays clean.
if (aot_executable_target) {
const wamrc_run_smoke = b.addRunArtifact(wamrc);
wamrc_run_smoke.addArg("run");
wamrc_run_smoke.addArg("-o");
const smoke_out = wamrc_run_smoke.addOutputFileArg("noop.cwasm");
_ = smoke_out;
wamrc_run_smoke.addFileArg(b.path("tests/coldstart/noop.wasm"));
wamrc_run_smoke.setEnvironmentVariable("WAMR_BIN", b.getInstallPath(.bin, "wamr"));
wamrc_run_smoke.step.dependOn(b.getInstallStep());
wamrc_run_smoke.expectExitCode(0);
test_step.dependOn(&wamrc_run_smoke.step);
}
// #760 regression: AOT `wasi:cli/exit.exit` must terminate the host
// process with the requested discriminant rather than returning the
// post-#714 sentinel through the canon-lower(aot) trampoline. Two
// hand-rolled WAT fixtures (`tests/regressions/760-aot-cli-exit/`)
// import `wasi:cli/exit@0.2.0` directly (no preview1 → preview2
// adapter, so unrelated to the cross-instance #662 blocker that
// gates the `zig-exit` example) and exit with the ok / err
// discriminants. Before the fix both crashed the host with SIGSEGV
// (exit 139) after the wit-bindgen adapter's "host exit
// implementation didn't exit!" assertion fired on the sentinel
// return; after the fix the host exits 0 / 1 respectively.
//
// Gated on `aot_trampoline_pool_target` (not just
// `aot_executable_target`) because canon-lower for a host import
// requires the host-import trampoline pool, which isn't supported
// on Windows / macOS-aarch64 — running there fails with
// `[aot reject] ... UnsupportedPlatform` regardless of #760.
if (aot_trampoline_pool_target) {
const fixtures = [_]struct { name: []const u8, expected_exit: u8 }{
.{ .name = "exit-ok.wasm", .expected_exit = 0 },
.{ .name = "exit-with-code-7.wasm", .expected_exit = 1 },
};
for (fixtures) |f| {
const run = b.addRunArtifact(wamrc);
run.addArg("run");
run.addArg("-o");
_ = run.addOutputFileArg(b.fmt("760-{s}.cwasm", .{f.name}));
run.addFileArg(b.path(b.fmt("tests/regressions/760-aot-cli-exit/{s}", .{f.name})));
run.setEnvironmentVariable("WAMR_BIN", b.getInstallPath(.bin, "wamr"));
run.step.dependOn(b.getInstallStep());
run.expectExitCode(f.expected_exit);
test_step.dependOn(&run.step);
}
}
// #794: load-forwarding soundness gate. Compiles the shipped hand-written
// wasm corpus with the dedicated Check-11 verify mode
// (`--verify-ir=load-forwarding`) and requires a clean exit. Check 11 is a
// SOUND OVER-APPROXIMATION: it never misses an unsound forward but can
// false-positive on legitimate "snapshot" loads (a load value reused across
// an aliasing store), which are pervasive in LLVM-optimised wasm. The gated
// corpus is therefore deliberately limited to hand-written fixtures. The
// strong, false-positive-free regression net for the #743 / #793 bug class
// is the differential property fuzzer
// (`src/compiler/ir/property_test.zig`, `loop_forwarded_load` shape), which
// executes original vs optimised IR and compares observables.
const verify_ir_soundness_step = b.step(
"verify-ir-soundness",
"Compile the hand-written wasm corpus with the load-forwarding soundness check (#794)",
);
if (aot_executable_target) {
const soundness_fixtures = [_][]const u8{
"tests/spec-json/linking.trap401.wasm",
"tests/spec-json/linking.trap413.wasm",
"tests/spec-json/linking.trap554.wasm",
"tests/spec-json/linking.trap566.wasm",
"tests/spec-json/linking.trap592.wasm",
"tests/coldstart/noop.wasm",
};
for (soundness_fixtures) |fixture| {
const run = b.addRunArtifact(wamrc);
run.addArgs(&.{ "compile", "--verify-ir=load-forwarding", "-o" });
_ = run.addOutputFileArg("verified.cwasm");
run.addFileArg(b.path(fixture));
run.expectExitCode(0);
verify_ir_soundness_step.dependOn(&run.step);
test_step.dependOn(&run.step);
}
}
// #757 `wamrc verify` smoke (no wasmtime required):
// * `verify help` → exit 0 + non-empty stdout.
// * `verify --wasmtime-bin=/dev/null/nope <wasm>` → spawn fails →
// exit 2 (setup error). Exercises the binary-resolution +
// "spawn failed = setup error" wiring without needing
// wasmtime on the test runner's PATH.
// Reuses the 339-byte `tests/regressions/760-aot-cli-exit/exit-ok.wasm`
// (a valid component the AOT precompile accepts) so the smoke
// gets past the precompile step before the wasmtime spawn fails.
if (aot_trampoline_pool_target) {
const verify_help = b.addRunArtifact(wamrc);
verify_help.addArgs(&.{ "verify", "help" });
verify_help.expectExitCode(0);
test_step.dependOn(&verify_help.step);
const verify_no_wasmtime = b.addRunArtifact(wamrc);
verify_no_wasmtime.addArgs(&.{ "verify", "--wasmtime-bin=/dev/null/nope-wamrc-757" });
verify_no_wasmtime.addFileArg(b.path("tests/regressions/760-aot-cli-exit/exit-ok.wasm"));
verify_no_wasmtime.setEnvironmentVariable("WAMR_BIN", b.getInstallPath(.bin, "wamr"));
verify_no_wasmtime.step.dependOn(b.getInstallStep());
verify_no_wasmtime.expectExitCode(2);
test_step.dependOn(&verify_no_wasmtime.step);
}
// Compiler IR passes tests (separate module to avoid root/wamr conflict)
const passes_test_module = b.createModule(.{
.root_source_file = b.path("src/compiler/ir/passes.zig"),
.target = target,
.optimize = optimize,
});
const passes_tests = b.addTest(.{
.root_module = passes_test_module,
});
const run_passes_tests = b.addRunArtifact(passes_tests);
test_step.dependOn(&run_passes_tests.step);
// Compiler IR analysis tests
const analysis_test_module = b.createModule(.{
.root_source_file = b.path("src/compiler/ir/analysis.zig"),
.target = target,
.optimize = optimize,
});
const analysis_tests = b.addTest(.{
.root_module = analysis_test_module,
});
const run_analysis_tests = b.addRunArtifact(analysis_tests);
test_step.dependOn(&run_analysis_tests.step);
// Compiler local-init analysis tests
const local_init_test_module = b.createModule(.{
.root_source_file = b.path("src/compiler/ir/local_init.zig"),
.target = target,
.optimize = optimize,
});
const local_init_tests = b.addTest(.{
.root_module = local_init_test_module,
});
const run_local_init_tests = b.addRunArtifact(local_init_tests);
test_step.dependOn(&run_local_init_tests.step);
// Compiler register allocator tests
const regalloc_test_module = b.createModule(.{
.root_source_file = b.path("src/compiler/ir/regalloc.zig"),
.target = target,
.optimize = optimize,
});
const regalloc_tests = b.addTest(.{
.root_module = regalloc_test_module,
});
const run_regalloc_tests = b.addRunArtifact(regalloc_tests);
test_step.dependOn(&run_regalloc_tests.step);
// Compiler loop-aware live-range splitting tests (#383 / #524). The
// file's tests were previously not wired into any module and so never
// ran; this module makes `zig build test` cover them.
const range_split_test_module = b.createModule(.{
.root_source_file = b.path("src/compiler/ir/range_split.zig"),
.target = target,
.optimize = optimize,
});
const range_split_tests = b.addTest(.{
.root_module = range_split_test_module,
});
const run_range_split_tests = b.addRunArtifact(range_split_tests);
test_step.dependOn(&run_range_split_tests.step);
// Compiler IR printer tests
const ir_print_test_module = b.createModule(.{
.root_source_file = b.path("src/compiler/ir/print_test.zig"),
.target = target,
.optimize = optimize,
});
const ir_print_tests = b.addTest(.{
.root_module = ir_print_test_module,
});
const run_ir_print_tests = b.addRunArtifact(ir_print_tests);
test_step.dependOn(&run_ir_print_tests.step);
// IR verifier tests (#624).
const verifier_test_module = b.createModule(.{
.root_source_file = b.path("src/compiler/ir/verifier.zig"),
.target = target,
.optimize = optimize,
});
const verifier_tests = b.addTest(.{
.root_module = verifier_test_module,
});
const run_verifier_tests = b.addRunArtifact(verifier_tests);
test_step.dependOn(&run_verifier_tests.step);
// IR interpreter tests (#736).
const ir_interp_test_module = b.createModule(.{
.root_source_file = b.path("src/compiler/ir/interp.zig"),
.target = target,
.optimize = optimize,
});
const ir_interp_tests = b.addTest(.{
.root_module = ir_interp_test_module,
});
const run_ir_interp_tests = b.addRunArtifact(ir_interp_tests);
test_step.dependOn(&run_ir_interp_tests.step);
// IR deterministic generator tests (#736).
const ir_fuzz_test_module = b.createModule(.{
.root_source_file = b.path("src/compiler/ir/fuzz.zig"),
.target = target,
.optimize = optimize,
});
const ir_fuzz_tests = b.addTest(.{
.root_module = ir_fuzz_test_module,
});
const run_ir_fuzz_tests = b.addRunArtifact(ir_fuzz_tests);
test_step.dependOn(&run_ir_fuzz_tests.step);
// IR optimizer property tests (#736).
const ir_property_test_module = b.createModule(.{
.root_source_file = b.path("src/compiler/ir/property_test.zig"),
.target = target,
.optimize = optimize,
});
const ir_property_options = b.addOptions();
ir_property_options.addOption(u32, "iterations", ir_property_iterations);
ir_property_test_module.addImport("ir_property_options", ir_property_options.createModule());
const ir_property_tests = b.addTest(.{
.root_module = ir_property_test_module,
});
const run_ir_property_tests = b.addRunArtifact(ir_property_tests);
test_step.dependOn(&run_ir_property_tests.step);
// Dominator-aware redundant-load forwarder tests (#391).
const dom_frl_test_module = b.createModule(.{
.root_source_file = b.path("src/compiler/ir/forward_redundant_loads_dominator.zig"),
.target = target,
.optimize = optimize,
});
const dom_frl_tests = b.addTest(.{
.root_module = dom_frl_test_module,
});
const run_dom_frl_tests = b.addRunArtifact(dom_frl_tests);
test_step.dependOn(&run_dom_frl_tests.step);
// Interp-vs-AOT differential tests. Own module (with its own `wamr`
// alias) so `aot_harness.zig` — which `differential.zig` imports — is
// reached through the `wamr` module and not duplicated into it. The
// standalone `aot_harness` module below (used by fuzz targets) must
// own the file exclusively; pulling it in via the main `wamr` lib
// module would trigger Zig's "file exists in modules X and Y" error.
const differential_test_module = b.createModule(.{
.root_source_file = b.path("src/tests/differential.zig"),
.target = target,
.optimize = optimize,
});
differential_test_module.addImport("wamr", lib_module);
const differential_options = b.addOptions();
differential_options.addOption(bool, "verify_ir_triage", verify_ir_triage);
differential_test_module.addImport("differential_options", differential_options.createModule());
const differential_tests = b.addTest(.{
.root_module = differential_test_module,
});
const run_differential_tests = b.addRunArtifact(differential_tests);
test_step.dependOn(&run_differential_tests.step);
// #694: regression test for active elem segments referencing
// funcidx ≥ 256 (was capped by a fixed 256-entry buffer in
// `mapCodeExecutable`, silently dropping both the native pointer
// and the sig_id update for any high-funcidx slot).
const aot_high_funcidx_module = b.createModule(.{
.root_source_file = b.path("src/tests/aot_high_funcidx_test.zig"),
.target = target,
.optimize = optimize,
});
aot_high_funcidx_module.addImport("wamr", lib_module);
const aot_high_funcidx_tests = b.addTest(.{
.root_module = aot_high_funcidx_module,
});
const run_aot_high_funcidx_tests = b.addRunArtifact(aot_high_funcidx_tests);
test_step.dependOn(&run_aot_high_funcidx_tests.step);
// #625 phase 1: AOT-backed component-core smoke test. Lives in its
// own test step for the same reason `differential.zig` does:
// `aot_harness.zig` cannot be pulled into the `wamr` lib module
// (it's already owned by the test runners that route through it).
const component_aot_smoke_module = b.createModule(.{
.root_source_file = b.path("src/tests/component_aot_smoke_test.zig"),
.target = target,
.optimize = optimize,
});
component_aot_smoke_module.addImport("wamr", lib_module);
const component_aot_smoke_tests = b.addTest(.{
.root_module = component_aot_smoke_module,
});
const run_component_aot_smoke_tests = b.addRunArtifact(component_aot_smoke_tests);
test_step.dependOn(&run_component_aot_smoke_tests.step);
// Phase 2: precompile → manifest → loadManifest → instantiate
// round-trip test (#625). Uses the same separate-module pattern
// as the phase 1 smoke test above.
const component_precompile_module = b.createModule(.{
.root_source_file = b.path("src/tests/component_precompile_test.zig"),
.target = target,
.optimize = optimize,
});
component_precompile_module.addImport("wamr", lib_module);
const component_precompile_tests = b.addTest(.{
.root_module = component_precompile_module,
});
const run_component_precompile_tests = b.addRunArtifact(component_precompile_tests);
test_step.dependOn(&run_component_precompile_tests.step);
// #676: wamrc compile-component must recurse into nested
// sub-components (the dominant shape of `wabt component
// compose -d` / `wasm-tools compose` output). Companion to the
// single-core round-trip above.
const component_precompile_nested_module = b.createModule(.{
.root_source_file = b.path("src/tests/component_precompile_nested_test.zig"),
.target = target,
.optimize = optimize,
});
component_precompile_nested_module.addImport("wamr", lib_module);
const component_precompile_nested_tests = b.addTest(.{
.root_module = component_precompile_nested_module,
});
const run_component_precompile_nested_tests = b.addRunArtifact(component_precompile_nested_tests);
test_step.dependOn(&run_component_precompile_nested_tests.step);
// Phase 3: canon.lift dispatches onto AOT cores (#625).
const component_aot_canonlift_module = b.createModule(.{
.root_source_file = b.path("src/tests/component_aot_canonlift_test.zig"),
.target = target,
.optimize = optimize,
});
component_aot_canonlift_module.addImport("wamr", lib_module);
const component_aot_canonlift_tests = b.addTest(.{
.root_module = component_aot_canonlift_module,
});
const run_component_aot_canonlift_tests = b.addRunArtifact(component_aot_canonlift_tests);
test_step.dependOn(&run_component_aot_canonlift_tests.step);
// Cold-start budget tests (issue #395). In-process timing companion
// to the subprocess harness in #394. Compile a 36-byte noop wasm
// through the just-built `wamrc` to produce a `.cwasm` fixture, then
// run two timing tests asserting WAMR-internal load+invoke stays
// under fixed budgets. Disable with `-Dskip-coldstart=true`.
if (aot_executable_target) {
const wamrc_compile_noop = b.addRunArtifact(wamrc);
wamrc_compile_noop.addArg("compile");
wamrc_compile_noop.addFileArg(b.path("tests/coldstart/noop.wasm"));
wamrc_compile_noop.addArg("-o");
const noop_cwasm = wamrc_compile_noop.addOutputFileArg("noop.cwasm");
const coldstart_options = b.addOptions();
coldstart_options.addOption(bool, "skip", skip_coldstart);
const coldstart_test_module = b.createModule(.{
.root_source_file = b.path("src/tests/coldstart_test.zig"),
.target = target,
.optimize = .ReleaseFast,
});
coldstart_test_module.addImport("wamr", lib_module);
coldstart_test_module.addImport("coldstart_options", coldstart_options.createModule());
coldstart_test_module.addAnonymousImport("noop_wasm", .{
.root_source_file = b.path("tests/coldstart/noop.wasm"),
});
coldstart_test_module.addAnonymousImport("noop_cwasm", .{
.root_source_file = noop_cwasm,
});
const coldstart_tests = b.addTest(.{
.root_module = coldstart_test_module,
});
const run_coldstart_tests = b.addRunArtifact(coldstart_tests);
test_step.dependOn(&run_coldstart_tests.step);
}
// ── WASI sockets (#437) end-to-end ────────────────────────────────
// Previously compiled a tiny `wasm32-wasi` echo server and drove
// `wamr run --listen=…` against it. Removed in #644 alongside the
// AOT-only CLI policy: plain core wasm is no longer accepted by
// `wamr run`, and `--listen` on core wasm depended on the
// interpreter `runWasm` path which no longer exists. The
// socket-preopen plumbing it exercised lives in the library
// (`WasiCtx.addPreopenSocket`) and is covered by lower-level
// tests in `src/wasi/`.
// ── Benchmark ─────────────────────────────────────────────────────
const bench_module = b.createModule(.{
.root_source_file = b.path("src/compiler/bench_codegen.zig"),
.target = target,
.optimize = .ReleaseFast,
// Darwin's clock_gettime lives in libSystem; the timer needs libc
// linked. Linux uses a raw syscall and Windows uses ntdll, so libc
// is only required here.
.link_libc = if (target.result.os.tag.isDarwin()) true else null,
});
const bench_exe = b.addExecutable(.{
.name = "codegen-bench",
.root_module = bench_module,
});
if (bench_target) b.installArtifact(bench_exe);
const run_bench = b.addRunArtifact(bench_exe);
const bench_step = b.step("bench", "Run codegen benchmarks");
bench_step.dependOn(&run_bench.step);
// ── Fuzz harnesses ────────────────────────────────────────────────
// CLI binaries that replay corpus inputs through a specific
// pipeline (core loader / component loader / interp / aot /
// interp-vs-aot diff) and leave
// a reproducer at <crashes>/in-flight.wasm if the process aborts.
// See src/tests/fuzz/common.zig, tests/fuzz/README.md, and
// .github/workflows/fuzz.yml.
const aot_harness_module = b.createModule(.{
.root_source_file = b.path("src/tests/aot_harness.zig"),
.target = target,
.optimize = optimize,
});
aot_harness_module.addImport("wamr", lib_module);
// ── CoreMark AOT runner ────────────────────────────────────────────
// Loads a CoreMark wasi `.wasm` and executes it through the Zig AOT
// backend (same pipeline as differential tests). Replaces the old
// C-based standalone coremark runner for gating the Zig backend on
// real CoreMark workloads.
const coremark_module = b.createModule(.{
.root_source_file = b.path("src/tests/coremark_aot_runner.zig"),
.target = target,
.optimize = .ReleaseFast,
});
coremark_module.addImport("wamr", lib_module);
const coremark_exe = b.addExecutable(.{
.name = "coremark-aot-runner",
.root_module = coremark_module,
});
if (aot_executable_target) b.installArtifact(coremark_exe);
const run_coremark_nofp = b.addRunArtifact(coremark_exe);
run_coremark_nofp.addArg("tests/benchmarks/coremark/coremark_wasi_nofp.wasm");
const run_coremark_fp = b.addRunArtifact(coremark_exe);
run_coremark_fp.addArg("tests/benchmarks/coremark/coremark_wasi.wasm");
const coremark_step = b.step(
"coremark-aot",
"Run the CoreMark wasi benchmarks through the Zig AOT backend",
);
coremark_step.dependOn(&run_coremark_nofp.step);
coremark_step.dependOn(&run_coremark_fp.step);
// ── CoreMark profile runner ────────────────────────────────────────
// SIGPROF-based sampling profiler for the CoreMark AOT, plus
// disassembly of the top-3 hot functions. Linux + aarch64/x86_64
// only — see src/tests/profile/sigprof.zig. See
// tests/benchmarks/coremark/README.md "Profiling" section.
const coremark_profile_module = b.createModule(.{
.root_source_file = b.path("src/tests/coremark_profile_runner.zig"),
.target = target,
.optimize = .ReleaseFast,
});
coremark_profile_module.addImport("wamr", lib_module);
const coremark_profile_exe = b.addExecutable(.{
.name = "coremark-profile-runner",
.root_module = coremark_profile_module,
});
if (aot_executable_target) b.installArtifact(coremark_profile_exe);
const run_coremark_profile = b.addRunArtifact(coremark_profile_exe);
run_coremark_profile.addArg("tests/benchmarks/coremark/coremark_wasi_nofp.wasm");
const coremark_profile_step = b.step(
"coremark-profile",
"Sampling-profile the CoreMark AOT and dump top-3 hot-function disassembly",
);
coremark_profile_step.dependOn(&run_coremark_profile.step);
// ── SIMD benchmark runner ───────────────────────────────────────────
// Builds small in-memory SIMD modules and reports interpreter vs AOT
// status/timing. Optional runner args after `--` can enable external
// baselines such as Wasmtime.
const simd_bench_module = b.createModule(.{
.root_source_file = b.path("src/tests/simd_bench_runner.zig"),
.target = target,
.optimize = .ReleaseFast,
});
simd_bench_module.addImport("wamr", lib_module);
const simd_bench_exe = b.addExecutable(.{
.name = "simd-bench-runner",
.root_module = simd_bench_module,
});
if (aot_executable_target) b.installArtifact(simd_bench_exe);
const simd_bench_step = b.step(
"simd-bench",
"Run SIMD interpreter/AOT benchmark status probes",
);
if (aot_executable_target) {
const run_simd_bench = b.addRunArtifact(simd_bench_exe);
run_simd_bench.addArg("--iterations");
run_simd_bench.addArg("10000");
if (b.args) |args| run_simd_bench.addArgs(args);
simd_bench_step.dependOn(&run_simd_bench.step);
}
// ── WASI stream zero-copy microbench (#583 B2) ──────────────────────
// Drives the executor's `stream.read` rendezvous against a synthetic
// host_driver to compare scratch-buffer vs zero-copy specialisation
// wall-clock + allocation cost. See
// `tests/benchmarks/wasi-streams/microbench.zig`.
const wasi_streams_bench_module = b.createModule(.{
.root_source_file = b.path("tests/benchmarks/wasi-streams/microbench.zig"),
.target = target,
.optimize = .ReleaseFast,
});
wasi_streams_bench_module.addImport("wamr", lib_module);
const wasi_streams_bench_exe = b.addExecutable(.{
.name = "wasi-streams-bench",
.root_module = wasi_streams_bench_module,
});
b.installArtifact(wasi_streams_bench_exe);
const wasi_streams_bench_step = b.step(
"wasi-streams-bench",