Merge pull request #12059 from Luukdegram/linker-tests-run-step

Implement EmulatableRunStep for linker tests
This commit is contained in:
Luuk de Gram 2022-07-25 06:33:01 +02:00 committed by GitHub
commit 7c13bdb1c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 400 additions and 112 deletions

View File

@ -67,6 +67,7 @@ $STAGE1_ZIG build test-cli -fqemu -fwasmtime
$STAGE1_ZIG build test-run-translated-c -fqemu -fwasmtime
$STAGE1_ZIG build docs -fqemu -fwasmtime
$STAGE1_ZIG build test-cases -fqemu -fwasmtime
$STAGE1_ZIG build test-link -fqemu -fwasmtime
# Produce the experimental std lib documentation.
mkdir -p "$RELEASE_STAGING/docs/std"

View File

@ -26,6 +26,7 @@ pub const CheckFileStep = @import("build/CheckFileStep.zig");
pub const CheckObjectStep = @import("build/CheckObjectStep.zig");
pub const InstallRawStep = @import("build/InstallRawStep.zig");
pub const OptionsStep = @import("build/OptionsStep.zig");
pub const EmulatableRunStep = @import("build/EmulatableRunStep.zig");
pub const Builder = struct {
install_tls: TopLevelStep,
@ -1890,6 +1891,21 @@ pub const LibExeObjStep = struct {
return run_step;
}
/// Creates an `EmulatableRunStep` with an executable built with `addExecutable`.
/// Allows running foreign binaries through emulation platforms such as Qemu or Rosetta.
/// When a binary cannot be ran through emulation or the option is disabled, a warning
/// will be printed and the binary will *NOT* be ran.
pub fn runEmulatable(exe: *LibExeObjStep) *EmulatableRunStep {
assert(exe.kind == .exe or exe.kind == .text_exe);
const run_step = EmulatableRunStep.create(exe.builder.fmt("run {s}", .{exe.step.name}), exe);
if (exe.vcpkg_bin_path) |path| {
run_step.addPathDir(path);
}
return run_step;
}
pub fn checkObject(self: *LibExeObjStep, obj_format: std.Target.ObjectFormat) *CheckObjectStep {
return CheckObjectStep.create(self.builder, self.getOutputSource(), obj_format);
}
@ -3604,6 +3620,7 @@ pub const Step = struct {
translate_c,
write_file,
run,
emulatable_run,
check_file,
check_object,
install_raw,

View File

@ -12,6 +12,7 @@ const CheckObjectStep = @This();
const Allocator = mem.Allocator;
const Builder = build.Builder;
const Step = build.Step;
const EmulatableRunStep = build.EmulatableRunStep;
pub const base_id = .check_obj;
@ -37,6 +38,16 @@ pub fn create(builder: *Builder, source: build.FileSource, obj_format: std.Targe
return self;
}
/// Runs and (optionally) compares the output of a binary.
/// Asserts `self` was generated from an executable step.
pub fn runAndCompare(self: *CheckObjectStep) *EmulatableRunStep {
const dependencies_len = self.step.dependencies.items.len;
assert(dependencies_len > 0);
const exe_step = self.step.dependencies.items[dependencies_len - 1];
const exe = exe_step.cast(std.build.LibExeObjStep).?;
return EmulatableRunStep.create(self.builder, "EmulatableRun", exe);
}
/// There two types of actions currently suported:
/// * `.match` - is the main building block of standard matchers with optional eat-all token `{*}`
/// and extractors by name such as `{n_value}`. Please note this action is very simplistic in nature

View File

@ -0,0 +1,215 @@
//! Unlike `RunStep` this step will provide emulation, when enabled, to run foreign binaries.
//! When a binary is foreign, but emulation for the target is disabled, the specified binary
//! will not be run and therefore also not validated against its output.
//! This step can be useful when wishing to run a built binary on multiple platforms,
//! without having to verify if it's possible to be ran against.
const std = @import("../std.zig");
const build = std.build;
const Step = std.build.Step;
const Builder = std.build.Builder;
const LibExeObjStep = std.build.LibExeObjStep;
const RunStep = std.build.RunStep;
const fs = std.fs;
const process = std.process;
const EnvMap = process.EnvMap;
const EmulatableRunStep = @This();
pub const base_id = .emulatable_run;
const max_stdout_size = 1 * 1024 * 1024; // 1 MiB
step: Step,
builder: *Builder,
/// The artifact (executable) to be run by this step
exe: *LibExeObjStep,
/// Set this to `null` to ignore the exit code for the purpose of determining a successful execution
expected_exit_code: ?u8 = 0,
/// Override this field to modify the environment
env_map: ?*EnvMap,
/// Set this to modify the current working directory
cwd: ?[]const u8,
stdout_action: RunStep.StdIoAction = .inherit,
stderr_action: RunStep.StdIoAction = .inherit,
/// When set to true, hides the warning of skipping a foreign binary which cannot be run on the host
/// or through emulation.
hide_foreign_binaries_warning: bool,
/// Creates a step that will execute the given artifact. This step will allow running the
/// binary through emulation when any of the emulation options such as `enable_rosetta` are set to true.
/// When set to false, and the binary is foreign, running the executable is skipped.
/// Asserts given artifact is an executable.
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 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),
.exe = artifact,
.env_map = null,
.cwd = null,
.hide_foreign_binaries_warning = hide_warnings,
};
self.step.dependOn(&artifact.step);
return self;
}
fn make(step: *Step) !void {
const self = @fieldParentPtr(EmulatableRunStep, "step", step);
const host_info = self.builder.host;
var argv_list = std.ArrayList([]const u8).init(self.builder.allocator);
defer argv_list.deinit();
const need_cross_glibc = self.exe.target.isGnuLibC() and self.exe.is_linking_libc;
switch (host_info.getExternalExecutor(self.exe.target_info, .{
.qemu_fixes_dl = need_cross_glibc and self.builder.glibc_runtimes_dir != null,
.link_libc = self.exe.is_linking_libc,
})) {
.native => {},
.rosetta => if (!self.builder.enable_rosetta) return warnAboutForeignBinaries(self),
.wine => |bin_name| if (self.builder.enable_wine) {
try argv_list.append(bin_name);
} else return,
.qemu => |bin_name| if (self.builder.enable_qemu) {
const glibc_dir_arg = if (need_cross_glibc)
self.builder.glibc_runtimes_dir orelse return
else
null;
try argv_list.append(bin_name);
if (glibc_dir_arg) |dir| {
// TODO look into making this a call to `linuxTriple`. This
// needs the directory to be called "i686" rather than
// "i386" which is why we do it manually here.
const fmt_str = "{s}" ++ fs.path.sep_str ++ "{s}-{s}-{s}";
const cpu_arch = self.exe.target.getCpuArch();
const os_tag = self.exe.target.getOsTag();
const abi = self.exe.target.getAbi();
const cpu_arch_name: []const u8 = if (cpu_arch == .i386)
"i686"
else
@tagName(cpu_arch);
const full_dir = try std.fmt.allocPrint(self.builder.allocator, fmt_str, .{
dir, cpu_arch_name, @tagName(os_tag), @tagName(abi),
});
try argv_list.append("-L");
try argv_list.append(full_dir);
}
} else return warnAboutForeignBinaries(self),
.darling => |bin_name| if (self.builder.enable_darling) {
try argv_list.append(bin_name);
} else return warnAboutForeignBinaries(self),
.wasmtime => |bin_name| if (self.builder.enable_wasmtime) {
try argv_list.append(bin_name);
try argv_list.append("--dir=.");
} else return warnAboutForeignBinaries(self),
else => return warnAboutForeignBinaries(self),
}
if (self.exe.target.isWindows()) {
// On Windows we don't have rpaths so we have to add .dll search paths to PATH
RunStep.addPathForDynLibsInternal(&self.step, self.builder, self.exe);
}
const executable_path = self.exe.installed_path orelse self.exe.getOutputSource().getPath(self.builder);
try argv_list.append(executable_path);
try RunStep.runCommand(
argv_list.items,
self.builder,
self.expected_exit_code,
self.stdout_action,
self.stderr_action,
.Inherit,
self.env_map,
self.cwd,
false,
);
}
pub fn expectStdErrEqual(self: *EmulatableRunStep, bytes: []const u8) void {
self.stderr_action = .{ .expect_exact = self.builder.dupe(bytes) };
}
pub fn expectStdOutEqual(self: *EmulatableRunStep, bytes: []const u8) void {
self.stdout_action = .{ .expect_exact = self.builder.dupe(bytes) };
}
fn warnAboutForeignBinaries(step: *EmulatableRunStep) void {
if (step.hide_foreign_binaries_warning) return;
const builder = step.builder;
const artifact = step.exe;
const host_name = builder.host.target.zigTriple(builder.allocator) catch unreachable;
const foreign_name = artifact.target.zigTriple(builder.allocator) catch unreachable;
const target_info = std.zig.system.NativeTargetInfo.detect(builder.allocator, artifact.target) catch unreachable;
const need_cross_glibc = artifact.target.isGnuLibC() and artifact.is_linking_libc;
switch (builder.host.getExternalExecutor(target_info, .{
.qemu_fixes_dl = need_cross_glibc and builder.glibc_runtimes_dir != null,
.link_libc = artifact.is_linking_libc,
})) {
.native => unreachable,
.bad_dl => |foreign_dl| {
const host_dl = builder.host.dynamic_linker.get() orelse "(none)";
std.debug.print("the host system does not appear to be capable of executing binaries from the target because the host dynamic linker is '{s}', while the target dynamic linker is '{s}'. Consider setting the dynamic linker as '{s}'.\n", .{
host_dl, foreign_dl, host_dl,
});
},
.bad_os_or_cpu => {
std.debug.print("the host system ({s}) does not appear to be capable of executing binaries from the target ({s}).\n", .{
host_name, foreign_name,
});
},
.darling => if (!builder.enable_darling) {
std.debug.print(
"the host system ({s}) does not appear to be capable of executing binaries " ++
"from the target ({s}). Consider enabling darling.\n",
.{ host_name, foreign_name },
);
},
.rosetta => if (!builder.enable_rosetta) {
std.debug.print(
"the host system ({s}) does not appear to be capable of executing binaries " ++
"from the target ({s}). Consider enabling rosetta.\n",
.{ host_name, foreign_name },
);
},
.wine => if (!builder.enable_wine) {
std.debug.print(
"the host system ({s}) does not appear to be capable of executing binaries " ++
"from the target ({s}). Consider enabling wine.\n",
.{ host_name, foreign_name },
);
},
.qemu => if (!builder.enable_qemu) {
std.debug.print(
"the host system ({s}) does not appear to be capable of executing binaries " ++
"from the target ({s}). Consider enabling qemu.\n",
.{ host_name, foreign_name },
);
},
.wasmtime => {
std.debug.print(
"the host system ({s}) does not appear to be capable of executing binaries " ++
"from the target ({s}). Consider enabling wasmtime.\n",
.{ host_name, foreign_name },
);
},
}
}

View File

@ -97,24 +97,42 @@ pub fn clearEnvironment(self: *RunStep) void {
}
pub fn addPathDir(self: *RunStep, search_path: []const u8) void {
const env_map = self.getEnvMap();
addPathDirInternal(&self.step, self.builder, search_path);
}
/// For internal use only, users of `RunStep` should use `addPathDir` directly.
fn addPathDirInternal(step: *Step, builder: *Builder, search_path: []const u8) void {
const env_map = getEnvMapInternal(step, builder.allocator);
const key = "PATH";
var prev_path = env_map.get(key);
if (prev_path) |pp| {
const new_path = self.builder.fmt("{s}" ++ [1]u8{fs.path.delimiter} ++ "{s}", .{ pp, search_path });
const new_path = builder.fmt("{s}" ++ [1]u8{fs.path.delimiter} ++ "{s}", .{ pp, search_path });
env_map.put(key, new_path) catch unreachable;
} else {
env_map.put(key, self.builder.dupePath(search_path)) catch unreachable;
env_map.put(key, builder.dupePath(search_path)) catch unreachable;
}
}
pub fn getEnvMap(self: *RunStep) *EnvMap {
return self.env_map orelse {
const env_map = self.builder.allocator.create(EnvMap) catch unreachable;
env_map.* = process.getEnvMap(self.builder.allocator) catch unreachable;
self.env_map = env_map;
return getEnvMapInternal(&self.step, self.builder.allocator);
}
fn getEnvMapInternal(step: *Step, allocator: Allocator) *EnvMap {
const maybe_env_map = switch (step.id) {
.run => step.cast(RunStep).?.env_map,
.emulatable_run => step.cast(build.EmulatableRunStep).?.env_map,
else => unreachable,
};
return maybe_env_map orelse {
const env_map = allocator.create(EnvMap) catch unreachable;
env_map.* = process.getEnvMap(allocator) catch unreachable;
switch (step.id) {
.run => step.cast(RunStep).?.env_map = env_map,
.emulatable_run => step.cast(RunStep).?.env_map = env_map,
else => unreachable,
}
return env_map;
};
}
@ -146,10 +164,7 @@ fn stdIoActionToBehavior(action: StdIoAction) std.ChildProcess.StdIo {
fn make(step: *Step) !void {
const self = @fieldParentPtr(RunStep, "step", step);
const cwd = if (self.cwd) |cwd| self.builder.pathFromRoot(cwd) else self.builder.build_root;
var argv_list = ArrayList([]const u8).init(self.builder.allocator);
for (self.argv.items) |arg| {
switch (arg) {
.bytes => |bytes| try argv_list.append(bytes),
@ -165,24 +180,48 @@ fn make(step: *Step) !void {
}
}
const argv = argv_list.items;
try runCommand(
argv_list.items,
self.builder,
self.expected_exit_code,
self.stdout_action,
self.stderr_action,
self.stdin_behavior,
self.env_map,
self.cwd,
self.print,
);
}
pub fn runCommand(
argv: []const []const u8,
builder: *Builder,
expected_exit_code: ?u8,
stdout_action: StdIoAction,
stderr_action: StdIoAction,
stdin_behavior: std.ChildProcess.StdIo,
env_map: ?*EnvMap,
maybe_cwd: ?[]const u8,
print: bool,
) !void {
const cwd = if (maybe_cwd) |cwd| builder.pathFromRoot(cwd) else builder.build_root;
if (!std.process.can_spawn) {
const cmd = try std.mem.join(self.builder.allocator, " ", argv);
const cmd = try std.mem.join(builder.addInstallDirectory, " ", argv);
std.debug.print("the following command cannot be executed ({s} does not support spawning a child process):\n{s}", .{ @tagName(builtin.os.tag), cmd });
self.builder.allocator.free(cmd);
builder.allocator.free(cmd);
return ExecError.ExecNotSupported;
}
var child = std.ChildProcess.init(argv, self.builder.allocator);
var child = std.ChildProcess.init(argv, builder.allocator);
child.cwd = cwd;
child.env_map = self.env_map orelse self.builder.env_map;
child.env_map = env_map orelse builder.env_map;
child.stdin_behavior = self.stdin_behavior;
child.stdout_behavior = stdIoActionToBehavior(self.stdout_action);
child.stderr_behavior = stdIoActionToBehavior(self.stderr_action);
child.stdin_behavior = stdin_behavior;
child.stdout_behavior = stdIoActionToBehavior(stdout_action);
child.stderr_behavior = stdIoActionToBehavior(stderr_action);
if (self.print)
if (print)
printCmd(cwd, argv);
child.spawn() catch |err| {
@ -193,21 +232,21 @@ fn make(step: *Step) !void {
// TODO need to poll to read these streams to prevent a deadlock (or rely on evented I/O).
var stdout: ?[]const u8 = null;
defer if (stdout) |s| self.builder.allocator.free(s);
defer if (stdout) |s| builder.allocator.free(s);
switch (self.stdout_action) {
switch (stdout_action) {
.expect_exact, .expect_matches => {
stdout = child.stdout.?.reader().readAllAlloc(self.builder.allocator, max_stdout_size) catch unreachable;
stdout = child.stdout.?.reader().readAllAlloc(builder.allocator, max_stdout_size) catch unreachable;
},
.inherit, .ignore => {},
}
var stderr: ?[]const u8 = null;
defer if (stderr) |s| self.builder.allocator.free(s);
defer if (stderr) |s| builder.allocator.free(s);
switch (self.stderr_action) {
switch (stderr_action) {
.expect_exact, .expect_matches => {
stderr = child.stderr.?.reader().readAllAlloc(self.builder.allocator, max_stdout_size) catch unreachable;
stderr = child.stderr.?.reader().readAllAlloc(builder.allocator, max_stdout_size) catch unreachable;
},
.inherit, .ignore => {},
}
@ -219,18 +258,18 @@ fn make(step: *Step) !void {
switch (term) {
.Exited => |code| blk: {
const expected_exit_code = self.expected_exit_code orelse break :blk;
const expected_code = expected_exit_code orelse break :blk;
if (code != expected_exit_code) {
if (self.builder.prominent_compile_errors) {
if (code != expected_code) {
if (builder.prominent_compile_errors) {
std.debug.print("Run step exited with error code {} (expected {})\n", .{
code,
expected_exit_code,
expected_code,
});
} else {
std.debug.print("The following command exited with error code {} (expected {}):\n", .{
code,
expected_exit_code,
expected_code,
});
printCmd(cwd, argv);
}
@ -245,7 +284,7 @@ fn make(step: *Step) !void {
},
}
switch (self.stderr_action) {
switch (stderr_action) {
.inherit, .ignore => {},
.expect_exact => |expected_bytes| {
if (!mem.eql(u8, expected_bytes, stderr.?)) {
@ -277,7 +316,7 @@ fn make(step: *Step) !void {
},
}
switch (self.stdout_action) {
switch (stdout_action) {
.inherit, .ignore => {},
.expect_exact => |expected_bytes| {
if (!mem.eql(u8, expected_bytes, stdout.?)) {
@ -319,12 +358,18 @@ fn printCmd(cwd: ?[]const u8, argv: []const []const u8) void {
}
fn addPathForDynLibs(self: *RunStep, artifact: *LibExeObjStep) void {
addPathForDynLibsInternal(&self.step, self.builder, artifact);
}
/// This should only be used for internal usage, this is called automatically
/// for the user.
pub fn addPathForDynLibsInternal(step: *Step, builder: *Builder, artifact: *LibExeObjStep) void {
for (artifact.link_objects.items) |link_object| {
switch (link_object) {
.other_step => |other| {
if (other.target.isWindows() and other.isDynamicLibrary()) {
self.addPathDir(fs.path.dirname(other.getOutputSource().getPath(self.builder)).?);
self.addPathForDynLibs(other);
addPathDirInternal(step, builder, fs.path.dirname(other.getOutputSource().getPath(builder)).?);
addPathForDynLibsInternal(step, builder, other);
}
},
else => {},

View File

@ -47,69 +47,67 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
.requires_stage2 = true,
});
if (builtin.os.tag == .macos) {
cases.addBuildFile("test/link/macho/entry/build.zig", .{
.build_modes = true,
});
cases.addBuildFile("test/link/macho/entry/build.zig", .{
.build_modes = true,
});
cases.addBuildFile("test/link/macho/pagezero/build.zig", .{
.build_modes = false,
});
cases.addBuildFile("test/link/macho/pagezero/build.zig", .{
.build_modes = false,
});
cases.addBuildFile("test/link/macho/dylib/build.zig", .{
.build_modes = true,
});
cases.addBuildFile("test/link/macho/dylib/build.zig", .{
.build_modes = true,
});
cases.addBuildFile("test/link/macho/dead_strip/build.zig", .{
.build_modes = false,
});
cases.addBuildFile("test/link/macho/dead_strip/build.zig", .{
.build_modes = false,
});
cases.addBuildFile("test/link/macho/dead_strip_dylibs/build.zig", .{
.build_modes = true,
.requires_macos_sdk = true,
});
cases.addBuildFile("test/link/macho/dead_strip_dylibs/build.zig", .{
.build_modes = true,
.requires_macos_sdk = true,
});
cases.addBuildFile("test/link/macho/needed_library/build.zig", .{
.build_modes = true,
});
cases.addBuildFile("test/link/macho/needed_library/build.zig", .{
.build_modes = true,
});
cases.addBuildFile("test/link/macho/weak_library/build.zig", .{
.build_modes = true,
});
cases.addBuildFile("test/link/macho/weak_library/build.zig", .{
.build_modes = true,
});
cases.addBuildFile("test/link/macho/needed_framework/build.zig", .{
.build_modes = true,
.requires_macos_sdk = true,
});
cases.addBuildFile("test/link/macho/needed_framework/build.zig", .{
.build_modes = true,
.requires_macos_sdk = true,
});
cases.addBuildFile("test/link/macho/weak_framework/build.zig", .{
.build_modes = true,
.requires_macos_sdk = true,
});
cases.addBuildFile("test/link/macho/weak_framework/build.zig", .{
.build_modes = true,
.requires_macos_sdk = true,
});
// Try to build and run an Objective-C executable.
cases.addBuildFile("test/link/macho/objc/build.zig", .{
.build_modes = true,
.requires_macos_sdk = true,
});
// Try to build and run an Objective-C executable.
cases.addBuildFile("test/link/macho/objc/build.zig", .{
.build_modes = true,
.requires_macos_sdk = true,
});
// Try to build and run an Objective-C++ executable.
cases.addBuildFile("test/link/macho/objcpp/build.zig", .{
.build_modes = true,
.requires_macos_sdk = true,
});
// Try to build and run an Objective-C++ executable.
cases.addBuildFile("test/link/macho/objcpp/build.zig", .{
.build_modes = true,
.requires_macos_sdk = true,
});
cases.addBuildFile("test/link/macho/stack_size/build.zig", .{
.build_modes = true,
});
cases.addBuildFile("test/link/macho/stack_size/build.zig", .{
.build_modes = true,
});
cases.addBuildFile("test/link/macho/search_strategy/build.zig", .{
.build_modes = true,
});
cases.addBuildFile("test/link/macho/search_strategy/build.zig", .{
.build_modes = true,
});
cases.addBuildFile("test/link/macho/headerpad/build.zig", .{
.build_modes = true,
.requires_macos_sdk = true,
});
}
cases.addBuildFile("test/link/macho/headerpad/build.zig", .{
.build_modes = true,
.requires_macos_sdk = true,
});
}

View File

@ -16,9 +16,7 @@ pub fn build(b: *Builder) void {
check.checkInSymtab();
check.checkNext("{*} (__TEXT,__text) external _iAmUnused");
test_step.dependOn(&check.step);
const run_cmd = exe.run();
const run_cmd = check.runAndCompare();
run_cmd.expectStdOutEqual("Hello!\n");
test_step.dependOn(&run_cmd.step);
}
@ -32,9 +30,7 @@ pub fn build(b: *Builder) void {
check.checkInSymtab();
check.checkNotPresent("{*} (__TEXT,__text) external _iAmUnused");
test_step.dependOn(&check.step);
const run_cmd = exe.run();
const run_cmd = check.runAndCompare();
run_cmd.expectStdOutEqual("Hello!\n");
test_step.dependOn(&run_cmd.step);
}

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