master
 1const std = @import("std");
 2
 3pub fn build(b: *std.Build) void {
 4    const test_step = b.step("test", "Test it");
 5    b.default_step = test_step;
 6
 7    add(b, test_step, .Debug);
 8    add(b, test_step, .ReleaseFast);
 9    add(b, test_step, .ReleaseSmall);
10    add(b, test_step, .ReleaseSafe);
11}
12
13fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
14    const lib = b.addExecutable(.{
15        .name = "lib",
16        .root_module = b.createModule(.{
17            .root_source_file = b.path("lib.zig"),
18            .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
19            .optimize = optimize,
20            .strip = false,
21        }),
22    });
23    lib.entry = .disabled;
24    lib.use_llvm = false;
25    lib.use_lld = false;
26    lib.stack_size = std.wasm.page_size * 2; // set an explicit stack size
27    lib.link_gc_sections = false;
28    b.installArtifact(lib);
29
30    const check_lib = lib.checkObject();
31
32    // ensure global exists and its initial value is equal to explitic stack size
33    check_lib.checkInHeaders();
34    check_lib.checkExact("Section global");
35    check_lib.checkExact("entries 1");
36    check_lib.checkExact("type i32"); // on wasm32 the stack pointer must be i32
37    check_lib.checkExact("mutable true"); // must be able to mutate the stack pointer
38    check_lib.checkExtract("i32.const {stack_pointer}");
39    check_lib.checkComputeCompare("stack_pointer", .{ .op = .eq, .value = .{ .literal = lib.stack_size.? } });
40
41    // validate memory section starts after virtual stack
42    check_lib.checkInHeaders();
43    check_lib.checkExact("Section data");
44    check_lib.checkExtract("i32.const {data_start}");
45    check_lib.checkComputeCompare("data_start", .{ .op = .eq, .value = .{ .variable = "stack_pointer" } });
46
47    // validate the name of the stack pointer
48    check_lib.checkInHeaders();
49    check_lib.checkExact("Section custom");
50    check_lib.checkExact("type global");
51    check_lib.checkExact("names 1");
52    check_lib.checkExact("index 0");
53    check_lib.checkExact("name __stack_pointer");
54    test_step.dependOn(&check_lib.step);
55}