Use EmulatableRunStep for MachO linker tests

This commit is contained in:
Luuk de Gram 2022-07-10 16:25:16 +02:00
parent 735b6eefe9
commit 4776065036
No known key found for this signature in database
GPG Key ID: A8CFE58E4DC7D664
9 changed files with 31 additions and 21 deletions

View File

@ -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),

View File

@ -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);

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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();

View File

@ -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");

View File

@ -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);
}

View File

@ -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);
}