Commit 938f9dea37

Luuk de Gram <luuk@degram.dev>
2023-09-01 16:37:07
update linker tests
This updates all linker tests to include `no_entry` as well as changes all tests to executable so they do not need to be updated later when the in-house WebAssembly linker supports dynamic libraries.
1 parent 58618af
Changed files (14)
lib
std
Build
test
link
wasm
archive
basic-features
bss
export
export-data
extern-mangle
function-table
infer-features
producers
segments
shared-memory
stack_pointer
type
lib/std/Build/Step/Compile.zig
@@ -1853,7 +1853,11 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
     if (self.global_base) |global_base| {
         try zig_args.append(b.fmt("--global-base={d}", .{global_base}));
     }
-    try addFlag(&zig_args, "entry", self.no_entry);
+    // invert the value due to naming so when `no_entry` is set to 'true'
+    // we actually emit the flag `-fno_entry`.
+    if (self.no_entry) |no_entry| {
+        try addFlag(&zig_args, "entry", !no_entry);
+    }
 
     if (self.code_model != .default) {
         try zig_args.append("-mcmodel");
test/link/wasm/archive/build.zig
@@ -15,12 +15,13 @@ pub fn build(b: *std.Build) void {
 fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
     // The code in question will pull-in compiler-rt,
     // and therefore link with its archive file.
-    const lib = b.addSharedLibrary(.{
+    const lib = b.addExecutable(.{
         .name = "main",
         .root_source_file = .{ .path = "main.zig" },
         .optimize = optimize,
         .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
     });
+    lib.no_entry = true;
     lib.use_llvm = false;
     lib.use_lld = false;
     lib.strip = false;
test/link/wasm/basic-features/build.zig
@@ -4,7 +4,7 @@ pub const requires_stage2 = true;
 
 pub fn build(b: *std.Build) void {
     // Library with explicitly set cpu features
-    const lib = b.addSharedLibrary(.{
+    const lib = b.addExecutable(.{
         .name = "lib",
         .root_source_file = .{ .path = "main.zig" },
         .optimize = .Debug,
@@ -15,6 +15,7 @@ pub fn build(b: *std.Build) void {
             .os_tag = .freestanding,
         },
     });
+    lib.no_entry = true;
     lib.use_llvm = false;
     lib.use_lld = false;
 
test/link/wasm/bss/build.zig
@@ -14,12 +14,13 @@ pub fn build(b: *std.Build) void {
 
 fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.OptimizeMode, is_safe: bool) void {
     {
-        const lib = b.addSharedLibrary(.{
+        const lib = b.addExecutable(.{
             .name = "lib",
             .root_source_file = .{ .path = "lib.zig" },
             .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
             .optimize = optimize_mode,
         });
+        lib.no_entry = true;
         lib.use_llvm = false;
         lib.use_lld = false;
         lib.strip = false;
@@ -60,12 +61,13 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt
 
     // verify zero'd declaration is stored in bss for all optimization modes.
     {
-        const lib = b.addSharedLibrary(.{
+        const lib = b.addExecutable(.{
             .name = "lib",
             .root_source_file = .{ .path = "lib2.zig" },
             .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
             .optimize = optimize_mode,
         });
+        lib.no_entry = true;
         lib.use_llvm = false;
         lib.use_lld = false;
         lib.strip = false;
test/link/wasm/export/build.zig
@@ -13,31 +13,34 @@ pub fn build(b: *std.Build) void {
 }
 
 fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
-    const no_export = b.addSharedLibrary(.{
+    const no_export = b.addExecutable(.{
         .name = "no-export",
         .root_source_file = .{ .path = "main.zig" },
         .optimize = optimize,
         .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
     });
+    no_export.no_entry = true;
     no_export.use_llvm = false;
     no_export.use_lld = false;
 
-    const dynamic_export = b.addSharedLibrary(.{
+    const dynamic_export = b.addExecutable(.{
         .name = "dynamic",
         .root_source_file = .{ .path = "main.zig" },
         .optimize = optimize,
         .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
     });
+    dynamic_export.no_entry = true;
     dynamic_export.rdynamic = true;
     dynamic_export.use_llvm = false;
     dynamic_export.use_lld = false;
 
-    const force_export = b.addSharedLibrary(.{
+    const force_export = b.addExecutable(.{
         .name = "force",
         .root_source_file = .{ .path = "main.zig" },
         .optimize = optimize,
         .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
     });
+    force_export.no_entry = true;
     force_export.export_symbol_names = &.{"foo"};
     force_export.use_llvm = false;
     force_export.use_lld = false;
test/link/wasm/export-data/build.zig
@@ -9,12 +9,13 @@ pub fn build(b: *std.Build) void {
         return;
     }
 
-    const lib = b.addSharedLibrary(.{
+    const lib = b.addExecutable(.{
         .name = "lib",
         .root_source_file = .{ .path = "lib.zig" },
         .optimize = .ReleaseSafe, // to make the output deterministic in address positions
         .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
     });
+    lib.no_entry = true;
     lib.use_lld = false;
     lib.export_symbol_names = &.{ "foo", "bar" };
     lib.global_base = 0; // put data section at address 0 to make data symbols easier to parse
test/link/wasm/extern-mangle/build.zig
@@ -11,12 +11,13 @@ pub fn build(b: *std.Build) void {
 }
 
 fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
-    const lib = b.addSharedLibrary(.{
+    const lib = b.addExecutable(.{
         .name = "lib",
         .root_source_file = .{ .path = "lib.zig" },
         .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
         .optimize = optimize,
     });
+    lib.no_entry = true;
     lib.import_symbols = true; // import `a` and `b`
     lib.rdynamic = true; // export `foo`
 
test/link/wasm/function-table/build.zig
@@ -13,32 +13,35 @@ pub fn build(b: *std.Build) void {
 }
 
 fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
-    const import_table = b.addSharedLibrary(.{
+    const import_table = b.addExecutable(.{
         .name = "import_table",
         .root_source_file = .{ .path = "lib.zig" },
         .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
         .optimize = optimize,
     });
+    import_table.no_entry = true;
     import_table.use_llvm = false;
     import_table.use_lld = false;
     import_table.import_table = true;
 
-    const export_table = b.addSharedLibrary(.{
+    const export_table = b.addExecutable(.{
         .name = "export_table",
         .root_source_file = .{ .path = "lib.zig" },
         .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
         .optimize = optimize,
     });
+    export_table.no_entry = true;
     export_table.use_llvm = false;
     export_table.use_lld = false;
     export_table.export_table = true;
 
-    const regular_table = b.addSharedLibrary(.{
+    const regular_table = b.addExecutable(.{
         .name = "regular_table",
         .root_source_file = .{ .path = "lib.zig" },
         .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
         .optimize = optimize,
     });
+    regular_table.no_entry = true;
     regular_table.use_llvm = false;
     regular_table.use_lld = false;
 
test/link/wasm/infer-features/build.zig
@@ -17,7 +17,7 @@ pub fn build(b: *std.Build) void {
 
     // Wasm library that doesn't have any features specified. This will
     // infer its featureset from other linked object files.
-    const lib = b.addSharedLibrary(.{
+    const lib = b.addExecutable(.{
         .name = "lib",
         .root_source_file = .{ .path = "main.zig" },
         .optimize = .Debug,
@@ -27,6 +27,7 @@ pub fn build(b: *std.Build) void {
             .os_tag = .freestanding,
         },
     });
+    lib.no_entry = true;
     lib.use_llvm = false;
     lib.use_lld = false;
     lib.addObject(c_obj);
test/link/wasm/producers/build.zig
@@ -14,12 +14,13 @@ pub fn build(b: *std.Build) void {
 }
 
 fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
-    const lib = b.addSharedLibrary(.{
+    const lib = b.addExecutable(.{
         .name = "lib",
         .root_source_file = .{ .path = "lib.zig" },
         .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
         .optimize = optimize,
     });
+    lib.no_entry = true;
     lib.use_llvm = false;
     lib.use_lld = false;
     lib.strip = false;
test/link/wasm/segments/build.zig
@@ -13,12 +13,13 @@ pub fn build(b: *std.Build) void {
 }
 
 fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
-    const lib = b.addSharedLibrary(.{
+    const lib = b.addExecutable(.{
         .name = "lib",
         .root_source_file = .{ .path = "lib.zig" },
         .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
         .optimize = optimize,
     });
+    lib.no_entry = true;
     lib.use_llvm = false;
     lib.use_lld = false;
     lib.strip = false;
test/link/wasm/shared-memory/build.zig
@@ -11,88 +11,87 @@ pub fn build(b: *std.Build) void {
 }
 
 fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.OptimizeMode) void {
-    {
-        const lib = b.addSharedLibrary(.{
-            .name = "lib",
-            .root_source_file = .{ .path = "lib.zig" },
-            .target = .{
-                .cpu_arch = .wasm32,
-                .cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp },
-                .cpu_features_add = std.Target.wasm.featureSet(&.{ .atomics, .bulk_memory }),
-                .os_tag = .freestanding,
-            },
-            .optimize = optimize_mode,
-        });
-        lib.use_lld = false;
-        lib.strip = false;
-        lib.import_memory = true;
-        lib.export_memory = true;
-        lib.shared_memory = true;
-        lib.max_memory = 67108864;
-        lib.single_threaded = false;
-        lib.export_symbol_names = &.{"foo"};
+    const lib = b.addExecutable(.{
+        .name = "lib",
+        .root_source_file = .{ .path = "lib.zig" },
+        .target = .{
+            .cpu_arch = .wasm32,
+            .cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp },
+            .cpu_features_add = std.Target.wasm.featureSet(&.{ .atomics, .bulk_memory }),
+            .os_tag = .freestanding,
+        },
+        .optimize = optimize_mode,
+    });
+    lib.no_entry = true;
+    lib.use_lld = false;
+    lib.strip = false;
+    lib.import_memory = true;
+    lib.export_memory = true;
+    lib.shared_memory = true;
+    lib.max_memory = 67108864;
+    lib.single_threaded = false;
+    lib.export_symbol_names = &.{"foo"};
 
-        const check_lib = lib.checkObject();
+    const check_lib = lib.checkObject();
 
-        check_lib.checkStart("Section import");
-        check_lib.checkNext("entries 1");
-        check_lib.checkNext("module env");
-        check_lib.checkNext("name memory"); // ensure we are importing memory
+    check_lib.checkStart("Section import");
+    check_lib.checkNext("entries 1");
+    check_lib.checkNext("module env");
+    check_lib.checkNext("name memory"); // ensure we are importing memory
 
-        check_lib.checkStart("Section export");
-        check_lib.checkNext("entries 2");
-        check_lib.checkNext("name memory"); // ensure we also export memory again
+    check_lib.checkStart("Section export");
+    check_lib.checkNext("entries 2");
+    check_lib.checkNext("name memory"); // ensure we also export memory again
 
-        // This section *must* be emit as the start function is set to the index
-        // of __wasm_init_memory
-        // release modes will have the TLS segment optimized out in our test-case.
-        // This means we won't have __wasm_init_memory in such case, and therefore
-        // should also not have a section "start"
-        if (optimize_mode == .Debug) {
-            check_lib.checkStart("Section start");
-        }
-
-        // This section is only and *must* be emit when shared-memory is enabled
-        // release modes will have the TLS segment optimized out in our test-case.
-        if (optimize_mode == .Debug) {
-            check_lib.checkStart("Section data_count");
-            check_lib.checkNext("count 3");
-        }
+    // This section *must* be emit as the start function is set to the index
+    // of __wasm_init_memory
+    // release modes will have the TLS segment optimized out in our test-case.
+    // This means we won't have __wasm_init_memory in such case, and therefore
+    // should also not have a section "start"
+    if (optimize_mode == .Debug) {
+        check_lib.checkStart("Section start");
+    }
 
-        check_lib.checkStart("Section custom");
-        check_lib.checkNext("name name");
-        check_lib.checkNext("type function");
-        if (optimize_mode == .Debug) {
-            check_lib.checkNext("name __wasm_init_memory");
-        }
-        check_lib.checkNext("name __wasm_init_tls");
-        check_lib.checkNext("type global");
+    // This section is only and *must* be emit when shared-memory is enabled
+    // release modes will have the TLS segment optimized out in our test-case.
+    if (optimize_mode == .Debug) {
+        check_lib.checkStart("Section data_count");
+        check_lib.checkNext("count 3");
+    }
 
-        // In debug mode the symbol __tls_base is resolved to an undefined symbol
-        // from the object file, hence its placement differs than in release modes
-        // where the entire tls segment is optimized away, and tls_base will have
-        // its original position.
-        if (optimize_mode == .Debug) {
-            check_lib.checkNext("name __tls_size");
-            check_lib.checkNext("name __tls_align");
-            check_lib.checkNext("name __tls_base");
-        } else {
-            check_lib.checkNext("name __tls_base");
-            check_lib.checkNext("name __tls_size");
-            check_lib.checkNext("name __tls_align");
-        }
+    check_lib.checkStart("Section custom");
+    check_lib.checkNext("name name");
+    check_lib.checkNext("type function");
+    if (optimize_mode == .Debug) {
+        check_lib.checkNext("name __wasm_init_memory");
+    }
+    check_lib.checkNext("name __wasm_init_tls");
+    check_lib.checkNext("type global");
 
-        check_lib.checkNext("type data_segment");
-        if (optimize_mode == .Debug) {
-            check_lib.checkNext("names 3");
-            check_lib.checkNext("index 0");
-            check_lib.checkNext("name .rodata");
-            check_lib.checkNext("index 1");
-            check_lib.checkNext("name .bss");
-            check_lib.checkNext("index 2");
-            check_lib.checkNext("name .tdata");
-        }
+    // In debug mode the symbol __tls_base is resolved to an undefined symbol
+    // from the object file, hence its placement differs than in release modes
+    // where the entire tls segment is optimized away, and tls_base will have
+    // its original position.
+    if (optimize_mode == .Debug) {
+        check_lib.checkNext("name __tls_size");
+        check_lib.checkNext("name __tls_align");
+        check_lib.checkNext("name __tls_base");
+    } else {
+        check_lib.checkNext("name __tls_base");
+        check_lib.checkNext("name __tls_size");
+        check_lib.checkNext("name __tls_align");
+    }
 
-        test_step.dependOn(&check_lib.step);
+    check_lib.checkNext("type data_segment");
+    if (optimize_mode == .Debug) {
+        check_lib.checkNext("names 3");
+        check_lib.checkNext("index 0");
+        check_lib.checkNext("name .rodata");
+        check_lib.checkNext("index 1");
+        check_lib.checkNext("name .bss");
+        check_lib.checkNext("index 2");
+        check_lib.checkNext("name .tdata");
     }
+
+    test_step.dependOn(&check_lib.step);
 }
test/link/wasm/stack_pointer/build.zig
@@ -13,12 +13,13 @@ pub fn build(b: *std.Build) void {
 }
 
 fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
-    const lib = b.addSharedLibrary(.{
+    const lib = b.addExecutable(.{
         .name = "lib",
         .root_source_file = .{ .path = "lib.zig" },
         .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
         .optimize = optimize,
     });
+    lib.no_entry = true;
     lib.use_llvm = false;
     lib.use_lld = false;
     lib.strip = false;
test/link/wasm/type/build.zig
@@ -13,12 +13,13 @@ pub fn build(b: *std.Build) void {
 }
 
 fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
-    const lib = b.addSharedLibrary(.{
+    const lib = b.addExecutable(.{
         .name = "lib",
         .root_source_file = .{ .path = "lib.zig" },
         .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
         .optimize = optimize,
     });
+    lib.no_entry = true;
     lib.use_llvm = false;
     lib.use_lld = false;
     lib.strip = false;