From 4776065036f9878c86e54fa83341fe8cdbb175f0 Mon Sep 17 00:00:00 2001 From: Luuk de Gram Date: Sun, 10 Jul 2022 16:25:16 +0200 Subject: [PATCH] Use `EmulatableRunStep` for MachO linker tests --- lib/std/build/EmulatableRunStep.zig | 7 ++++++- test/link/macho/dylib/build.zig | 7 ++++--- test/link/macho/entry/build.zig | 5 ++--- test/link/macho/needed_library/build.zig | 6 ++++-- test/link/macho/objc/build.zig | 3 +-- test/link/macho/pagezero/build.zig | 2 ++ test/link/macho/search_strategy/build.zig | 10 ++++++---- test/link/macho/stack_size/build.zig | 5 ++--- test/link/macho/weak_library/build.zig | 7 ++++--- 9 files changed, 31 insertions(+), 21 deletions(-) diff --git a/lib/std/build/EmulatableRunStep.zig b/lib/std/build/EmulatableRunStep.zig index 1f36401599..9745ebb89f 100644 --- a/lib/std/build/EmulatableRunStep.zig +++ b/lib/std/build/EmulatableRunStep.zig @@ -56,7 +56,12 @@ pub const StdIoAction = union(enum) { pub fn create(builder: *Builder, name: []const u8, artifact: *LibExeObjStep) *EmulatableRunStep { std.debug.assert(artifact.kind == .exe or artifact.kind == .test_exe); const self = builder.allocator.create(EmulatableRunStep) catch unreachable; - const hide_warnings = builder.option(bool, "hide-foreign-warnings", "Hide the warning when a foreign binary which is incompatible is skipped") orelse false; + + const option_name = "hide-foreign-warnings"; + const hide_warnings = if (builder.available_options_map.get(option_name) == null) warn: { + break :warn builder.option(bool, option_name, "Hide the warning when a foreign binary which is incompatible is skipped") orelse false; + } else false; + self.* = .{ .builder = builder, .step = Step.init(.emulatable_run, name, builder.allocator, make), diff --git a/test/link/macho/dylib/build.zig b/test/link/macho/dylib/build.zig index 1587def9b8..a5baf255c6 100644 --- a/test/link/macho/dylib/build.zig +++ b/test/link/macho/dylib/build.zig @@ -3,12 +3,14 @@ const Builder = std.build.Builder; pub fn build(b: *Builder) void { const mode = b.standardReleaseOptions(); + const target: std.zig.CrossTarget = .{ .os_tag = .macos }; const test_step = b.step("test", "Test"); test_step.dependOn(b.getInstallStep()); const dylib = b.addSharedLibrary("a", null, b.version(1, 0, 0)); dylib.setBuildMode(mode); + dylib.setTarget(target); dylib.addCSourceFile("a.c", &.{}); dylib.linkLibC(); dylib.install(); @@ -23,6 +25,7 @@ pub fn build(b: *Builder) void { test_step.dependOn(&check_dylib.step); const exe = b.addExecutable("main", null); + exe.setTarget(target); exe.setBuildMode(mode); exe.addCSourceFile("main.c", &.{}); exe.linkSystemLibrary("a"); @@ -40,9 +43,7 @@ pub fn build(b: *Builder) void { check_exe.checkStart("cmd RPATH"); check_exe.checkNext(std.fmt.allocPrint(b.allocator, "path {s}", .{b.pathFromRoot("zig-out/lib")}) catch unreachable); - test_step.dependOn(&check_exe.step); - - const run = exe.run(); + const run = check_exe.runAndCompare(); run.cwd = b.pathFromRoot("."); run.expectStdOutEqual("Hello world"); test_step.dependOn(&run.step); diff --git a/test/link/macho/entry/build.zig b/test/link/macho/entry/build.zig index 1f40b3d8e0..0ecca14aa2 100644 --- a/test/link/macho/entry/build.zig +++ b/test/link/macho/entry/build.zig @@ -8,6 +8,7 @@ pub fn build(b: *Builder) void { test_step.dependOn(b.getInstallStep()); const exe = b.addExecutable("main", null); + exe.setTarget(.{ .os_tag = .macos }); exe.setBuildMode(mode); exe.addCSourceFile("main.c", &.{}); exe.linkLibC(); @@ -26,9 +27,7 @@ pub fn build(b: *Builder) void { check_exe.checkComputeCompare("vmaddr entryoff +", .{ .op = .eq, .value = .{ .variable = "n_value" } }); - test_step.dependOn(&check_exe.step); - - const run = exe.run(); + const run = check_exe.runAndCompare(); run.expectStdOutEqual("42"); test_step.dependOn(&run.step); } diff --git a/test/link/macho/needed_library/build.zig b/test/link/macho/needed_library/build.zig index 708a09dc32..a314fd2201 100644 --- a/test/link/macho/needed_library/build.zig +++ b/test/link/macho/needed_library/build.zig @@ -4,11 +4,13 @@ const LibExeObjectStep = std.build.LibExeObjStep; pub fn build(b: *Builder) void { const mode = b.standardReleaseOptions(); + const target: std.zig.CrossTarget = .{ .os_tag = .macos }; const test_step = b.step("test", "Test the program"); test_step.dependOn(b.getInstallStep()); const dylib = b.addSharedLibrary("a", null, b.version(1, 0, 0)); + dylib.setTarget(target); dylib.setBuildMode(mode); dylib.addCSourceFile("a.c", &.{}); dylib.linkLibC(); @@ -19,6 +21,7 @@ pub fn build(b: *Builder) void { const exe = b.addExecutable("test", null); exe.addCSourceFile("main.c", &[0][]const u8{}); exe.setBuildMode(mode); + exe.setTarget(target); exe.linkLibC(); exe.linkSystemLibraryNeeded("a"); exe.addLibraryPath(b.pathFromRoot("zig-out/lib")); @@ -28,8 +31,7 @@ pub fn build(b: *Builder) void { const check = exe.checkObject(.macho); check.checkStart("cmd LOAD_DYLIB"); check.checkNext("name @rpath/liba.dylib"); - test_step.dependOn(&check.step); - const run_cmd = exe.run(); + const run_cmd = check.runAndCompare(); test_step.dependOn(&run_cmd.step); } diff --git a/test/link/macho/objc/build.zig b/test/link/macho/objc/build.zig index e41fd48e71..d7fd872f77 100644 --- a/test/link/macho/objc/build.zig +++ b/test/link/macho/objc/build.zig @@ -7,7 +7,6 @@ pub fn build(b: *Builder) void { const test_step = b.step("test", "Test the program"); const exe = b.addExecutable("test", null); - b.default_step.dependOn(&exe.step); exe.addIncludePath("."); exe.addCSourceFile("Foo.m", &[0][]const u8{}); exe.addCSourceFile("test.m", &[0][]const u8{}); @@ -17,6 +16,6 @@ pub fn build(b: *Builder) void { // populate paths to the sysroot here. exe.linkFramework("Foundation"); - const run_cmd = exe.run(); + const run_cmd = std.build.EmulatableRunStep.create(b, "run", exe); test_step.dependOn(&run_cmd.step); } diff --git a/test/link/macho/pagezero/build.zig b/test/link/macho/pagezero/build.zig index e858d1f4d8..9dbc0e6473 100644 --- a/test/link/macho/pagezero/build.zig +++ b/test/link/macho/pagezero/build.zig @@ -9,6 +9,7 @@ pub fn build(b: *Builder) void { { const exe = b.addExecutable("pagezero", null); + exe.setTarget(.{ .os_tag = .macos }); exe.setBuildMode(mode); exe.addCSourceFile("main.c", &.{}); exe.linkLibC(); @@ -28,6 +29,7 @@ pub fn build(b: *Builder) void { { const exe = b.addExecutable("no_pagezero", null); + exe.setTarget(.{ .os_tag = .macos }); exe.setBuildMode(mode); exe.addCSourceFile("main.c", &.{}); exe.linkLibC(); diff --git a/test/link/macho/search_strategy/build.zig b/test/link/macho/search_strategy/build.zig index c5e867c8d0..39a82bc6a7 100644 --- a/test/link/macho/search_strategy/build.zig +++ b/test/link/macho/search_strategy/build.zig @@ -1,6 +1,7 @@ const std = @import("std"); const Builder = std.build.Builder; const LibExeObjectStep = std.build.LibExeObjStep; +const target: std.zig.CrossTarget = .{ .os_tag = .macos }; pub fn build(b: *Builder) void { const mode = b.standardReleaseOptions(); @@ -17,9 +18,7 @@ pub fn build(b: *Builder) void { check.checkStart("cmd LOAD_DYLIB"); check.checkNext("name @rpath/liba.dylib"); - test_step.dependOn(&check.step); - - const run = exe.run(); + const run = check.runAndCompare(); run.cwd = b.pathFromRoot("."); run.expectStdOutEqual("Hello world"); test_step.dependOn(&run.step); @@ -30,7 +29,7 @@ pub fn build(b: *Builder) void { const exe = createScenario(b, mode); exe.search_strategy = .paths_first; - const run = exe.run(); + const run = std.build.EmulatableRunStep.create(b, "run", exe); run.cwd = b.pathFromRoot("."); run.expectStdOutEqual("Hello world"); test_step.dependOn(&run.step); @@ -39,6 +38,7 @@ pub fn build(b: *Builder) void { fn createScenario(b: *Builder, mode: std.builtin.Mode) *LibExeObjectStep { const static = b.addStaticLibrary("a", null); + static.setTarget(target); static.setBuildMode(mode); static.addCSourceFile("a.c", &.{}); static.linkLibC(); @@ -48,6 +48,7 @@ fn createScenario(b: *Builder, mode: std.builtin.Mode) *LibExeObjectStep { static.install(); const dylib = b.addSharedLibrary("a", null, b.version(1, 0, 0)); + dylib.setTarget(target); dylib.setBuildMode(mode); dylib.addCSourceFile("a.c", &.{}); dylib.linkLibC(); @@ -57,6 +58,7 @@ fn createScenario(b: *Builder, mode: std.builtin.Mode) *LibExeObjectStep { dylib.install(); const exe = b.addExecutable("main", null); + exe.setTarget(target); exe.setBuildMode(mode); exe.addCSourceFile("main.c", &.{}); exe.linkSystemLibraryName("a"); diff --git a/test/link/macho/stack_size/build.zig b/test/link/macho/stack_size/build.zig index 8da59dcb53..3abf48df7a 100644 --- a/test/link/macho/stack_size/build.zig +++ b/test/link/macho/stack_size/build.zig @@ -8,6 +8,7 @@ pub fn build(b: *Builder) void { test_step.dependOn(b.getInstallStep()); const exe = b.addExecutable("main", null); + exe.setTarget(.{ .os_tag = .macos }); exe.setBuildMode(mode); exe.addCSourceFile("main.c", &.{}); exe.linkLibC(); @@ -17,8 +18,6 @@ pub fn build(b: *Builder) void { check_exe.checkStart("cmd MAIN"); check_exe.checkNext("stacksize 100000000"); - test_step.dependOn(&check_exe.step); - - const run = exe.run(); + const run = check_exe.runAndCompare(); test_step.dependOn(&run.step); } diff --git a/test/link/macho/weak_library/build.zig b/test/link/macho/weak_library/build.zig index f1070f3b2b..8c41e0dfd1 100644 --- a/test/link/macho/weak_library/build.zig +++ b/test/link/macho/weak_library/build.zig @@ -4,11 +4,13 @@ const LibExeObjectStep = std.build.LibExeObjStep; pub fn build(b: *Builder) void { const mode = b.standardReleaseOptions(); + const target: std.zig.CrossTarget = .{ .os_tag = .macos }; const test_step = b.step("test", "Test the program"); test_step.dependOn(b.getInstallStep()); const dylib = b.addSharedLibrary("a", null, b.version(1, 0, 0)); + dylib.setTarget(target); dylib.setBuildMode(mode); dylib.addCSourceFile("a.c", &.{}); dylib.linkLibC(); @@ -16,6 +18,7 @@ pub fn build(b: *Builder) void { const exe = b.addExecutable("test", null); exe.addCSourceFile("main.c", &[0][]const u8{}); + exe.setTarget(target); exe.setBuildMode(mode); exe.linkLibC(); exe.linkSystemLibraryWeak("a"); @@ -30,9 +33,7 @@ pub fn build(b: *Builder) void { check.checkNext("(undefined) weak external _a (from liba)"); check.checkNext("(undefined) weak external _asStr (from liba)"); - test_step.dependOn(&check.step); - - const run_cmd = exe.run(); + const run_cmd = check.runAndCompare(); run_cmd.expectStdOutEqual("42 42"); test_step.dependOn(&run_cmd.step); }