| ... | @@ -6,38 +6,78 @@ pub fn build(b: *std.Build) void { | ... | @@ -6,38 +6,78 @@ pub fn build(b: *std.Build) void { |
| 6 | const test_step = b.step("test", "Test"); | 6 | const test_step = b.step("test", "Test"); |
| 7 | b.default_step = test_step; | 7 | b.default_step = test_step; |
| 8 | | 8 | |
| 9 | const lib = b.addSharedLibrary(.{ | 9 | add(b, test_step, .Debug, true); |
| 10 | .name = "lib", | 10 | add(b, test_step, .ReleaseFast, false); |
| 11 | .root_source_file = .{ .path = "lib.zig" }, | 11 | add(b, test_step, .ReleaseSmall, false); |
| 12 | .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding }, | 12 | add(b, test_step, .ReleaseSafe, true); |
| 13 | .optimize = .Debug, | 13 | } |
| 14 | }); | 14 | |
| 15 | lib.use_llvm = false; | 15 | fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.OptimizeMode, is_safe: bool) void { |
| 16 | lib.use_lld = false; | 16 | { |
| 17 | lib.strip = false; | 17 | const lib = b.addSharedLibrary(.{ |
| 18 | // to make sure the bss segment is emitted, we must import memory | 18 | .name = "lib", |
| 19 | lib.import_memory = true; | 19 | .root_source_file = .{ .path = "lib.zig" }, |
| 20 | lib.install(); | 20 | .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding }, |
| 21 | | 21 | .optimize = optimize_mode, |
| 22 | const check_lib = lib.checkObject(); | 22 | }); |
| 23 | | 23 | lib.use_llvm = false; |
| 24 | // since we import memory, make sure it exists with the correct naming | 24 | lib.use_lld = false; |
| 25 | check_lib.checkStart("Section import"); | 25 | lib.strip = false; |
| 26 | check_lib.checkNext("entries 1"); | 26 | // to make sure the bss segment is emitted, we must import memory |
| 27 | check_lib.checkNext("module env"); // default module name is "env" | 27 | lib.import_memory = true; |
| 28 | check_lib.checkNext("name memory"); // as per linker specification | 28 | |
| 29 | | 29 | const check_lib = lib.checkObject(); |
| 30 | // since we are importing memory, ensure it's not exported | 30 | |
| 31 | check_lib.checkNotPresent("Section export"); | 31 | // since we import memory, make sure it exists with the correct naming |
| 32 | | 32 | check_lib.checkStart("Section import"); |
| 33 | // validate the name of the stack pointer | 33 | check_lib.checkNext("entries 1"); |
| 34 | check_lib.checkStart("Section custom"); | 34 | check_lib.checkNext("module env"); // default module name is "env" |
| 35 | check_lib.checkNext("type data_segment"); | 35 | check_lib.checkNext("name memory"); // as per linker specification |
| 36 | check_lib.checkNext("names 2"); | 36 | |
| 37 | check_lib.checkNext("index 0"); | 37 | // since we are importing memory, ensure it's not exported |
| 38 | check_lib.checkNext("name .rodata"); | 38 | check_lib.checkNotPresent("Section export"); |
| 39 | check_lib.checkNext("index 1"); // bss section always last | 39 | |
| 40 | check_lib.checkNext("name .bss"); | 40 | // validate the name of the stack pointer |
| 41 | | 41 | check_lib.checkStart("Section custom"); |
| 42 | test_step.dependOn(&check_lib.step); | 42 | check_lib.checkNext("type data_segment"); |
| | 43 | check_lib.checkNext("names 2"); |
| | 44 | check_lib.checkNext("index 0"); |
| | 45 | check_lib.checkNext("name .rodata"); |
| | 46 | // for safe optimization modes `undefined` is stored in data instead of bss. |
| | 47 | if (is_safe) { |
| | 48 | check_lib.checkNext("index 1"); |
| | 49 | check_lib.checkNext("name .data"); |
| | 50 | check_lib.checkNotPresent("name .bss"); |
| | 51 | } else { |
| | 52 | check_lib.checkNext("index 1"); // bss section always last |
| | 53 | check_lib.checkNext("name .bss"); |
| | 54 | } |
| | 55 | test_step.dependOn(&check_lib.step); |
| | 56 | } |
| | 57 | |
| | 58 | // verify zero'd declaration is stored in bss for all optimization modes. |
| | 59 | { |
| | 60 | const lib = b.addSharedLibrary(.{ |
| | 61 | .name = "lib", |
| | 62 | .root_source_file = .{ .path = "lib2.zig" }, |
| | 63 | .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding }, |
| | 64 | .optimize = optimize_mode, |
| | 65 | }); |
| | 66 | lib.use_llvm = false; |
| | 67 | lib.use_lld = false; |
| | 68 | lib.strip = false; |
| | 69 | // to make sure the bss segment is emitted, we must import memory |
| | 70 | lib.import_memory = true; |
| | 71 | |
| | 72 | const check_lib = lib.checkObject(); |
| | 73 | check_lib.checkStart("Section custom"); |
| | 74 | check_lib.checkNext("type data_segment"); |
| | 75 | check_lib.checkNext("names 2"); |
| | 76 | check_lib.checkNext("index 0"); |
| | 77 | check_lib.checkNext("name .rodata"); |
| | 78 | check_lib.checkNext("index 1"); |
| | 79 | check_lib.checkNext("name .bss"); |
| | 80 | |
| | 81 | test_step.dependOn(&check_lib.step); |
| | 82 | } |
| 43 | } | 83 | } |