Commit 4776065036

Luuk de Gram <luuk@degram.dev>
2022-07-10 16:25:16
Use `EmulatableRunStep` for MachO linker tests
1 parent 735b6ee
Changed files (9)
lib
test
link
macho
dylib
entry
needed_library
objc
pagezero
search_strategy
stack_size
weak_library
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),
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);
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);
 }
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);
 }
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);
 }
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();
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");
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);
 }
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);
 }