authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-07-25 06:33:01+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-07-25 06:33:01+02:00
log7c13bdb1c9c7f61def9b58b1f24072a4eddc54c1
treecf48ca1d4072ed9b6454d935fac42e4fd05abcbf
parent546c75ca46eff73e5f038e2b894ff96c65cd8960
parent72c0cebe5c5e6954ae9992d36f8164ae9433df9e
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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

Implement EmulatableRunStep for linker tests

15 files changed, 413 insertions(+), 125 deletions(-)

ci/zinc/linux_test.sh+1
......@@ -67,6 +67,7 @@ $STAGE1_ZIG build test-cli -fqemu -fwasmtime
6767$STAGE1_ZIG build test-run-translated-c -fqemu -fwasmtime
6868$STAGE1_ZIG build docs -fqemu -fwasmtime
6969$STAGE1_ZIG build test-cases -fqemu -fwasmtime
70$STAGE1_ZIG build test-link -fqemu -fwasmtime
7071
7172# Produce the experimental std lib documentation.
7273mkdir -p "$RELEASE_STAGING/docs/std"
lib/std/build.zig+17
......@@ -26,6 +26,7 @@ pub const CheckFileStep = @import("build/CheckFileStep.zig");
2626pub const CheckObjectStep = @import("build/CheckObjectStep.zig");
2727pub const InstallRawStep = @import("build/InstallRawStep.zig");
2828pub const OptionsStep = @import("build/OptionsStep.zig");
29pub const EmulatableRunStep = @import("build/EmulatableRunStep.zig");
2930
3031pub const Builder = struct {
3132 install_tls: TopLevelStep,
......@@ -1890,6 +1891,21 @@ pub const LibExeObjStep = struct {
18901891 return run_step;
18911892 }
18921893
1894 /// Creates an `EmulatableRunStep` with an executable built with `addExecutable`.
1895 /// Allows running foreign binaries through emulation platforms such as Qemu or Rosetta.
1896 /// When a binary cannot be ran through emulation or the option is disabled, a warning
1897 /// will be printed and the binary will *NOT* be ran.
1898 pub fn runEmulatable(exe: *LibExeObjStep) *EmulatableRunStep {
1899 assert(exe.kind == .exe or exe.kind == .text_exe);
1900
1901 const run_step = EmulatableRunStep.create(exe.builder.fmt("run {s}", .{exe.step.name}), exe);
1902 if (exe.vcpkg_bin_path) |path| {
1903 run_step.addPathDir(path);
1904 }
1905
1906 return run_step;
1907 }
1908
18931909 pub fn checkObject(self: *LibExeObjStep, obj_format: std.Target.ObjectFormat) *CheckObjectStep {
18941910 return CheckObjectStep.create(self.builder, self.getOutputSource(), obj_format);
18951911 }
......@@ -3604,6 +3620,7 @@ pub const Step = struct {
36043620 translate_c,
36053621 write_file,
36063622 run,
3623 emulatable_run,
36073624 check_file,
36083625 check_object,
36093626 install_raw,
lib/std/build/CheckObjectStep.zig+11
......@@ -12,6 +12,7 @@ const CheckObjectStep = @This();
1212const Allocator = mem.Allocator;
1313const Builder = build.Builder;
1414const Step = build.Step;
15const EmulatableRunStep = build.EmulatableRunStep;
1516
1617pub const base_id = .check_obj;
1718
......@@ -37,6 +38,16 @@ pub fn create(builder: *Builder, source: build.FileSource, obj_format: std.Targe
3738 return self;
3839}
3940
41/// Runs and (optionally) compares the output of a binary.
42/// Asserts `self` was generated from an executable step.
43pub fn runAndCompare(self: *CheckObjectStep) *EmulatableRunStep {
44 const dependencies_len = self.step.dependencies.items.len;
45 assert(dependencies_len > 0);
46 const exe_step = self.step.dependencies.items[dependencies_len - 1];
47 const exe = exe_step.cast(std.build.LibExeObjStep).?;
48 return EmulatableRunStep.create(self.builder, "EmulatableRun", exe);
49}
50
4051/// There two types of actions currently suported:
4152/// * `.match` - is the main building block of standard matchers with optional eat-all token `{*}`
4253/// and extractors by name such as `{n_value}`. Please note this action is very simplistic in nature
lib/std/build/EmulatableRunStep.zig created+215
......@@ -0,0 +1,215 @@
1//! Unlike `RunStep` this step will provide emulation, when enabled, to run foreign binaries.
2//! When a binary is foreign, but emulation for the target is disabled, the specified binary
3//! will not be run and therefore also not validated against its output.
4//! This step can be useful when wishing to run a built binary on multiple platforms,
5//! without having to verify if it's possible to be ran against.
6
7const std = @import("../std.zig");
8const build = std.build;
9const Step = std.build.Step;
10const Builder = std.build.Builder;
11const LibExeObjStep = std.build.LibExeObjStep;
12const RunStep = std.build.RunStep;
13
14const fs = std.fs;
15const process = std.process;
16const EnvMap = process.EnvMap;
17
18const EmulatableRunStep = @This();
19
20pub const base_id = .emulatable_run;
21
22const max_stdout_size = 1 * 1024 * 1024; // 1 MiB
23
24step: Step,
25builder: *Builder,
26
27/// The artifact (executable) to be run by this step
28exe: *LibExeObjStep,
29
30/// Set this to `null` to ignore the exit code for the purpose of determining a successful execution
31expected_exit_code: ?u8 = 0,
32
33/// Override this field to modify the environment
34env_map: ?*EnvMap,
35
36/// Set this to modify the current working directory
37cwd: ?[]const u8,
38
39stdout_action: RunStep.StdIoAction = .inherit,
40stderr_action: RunStep.StdIoAction = .inherit,
41
42/// When set to true, hides the warning of skipping a foreign binary which cannot be run on the host
43/// or through emulation.
44hide_foreign_binaries_warning: bool,
45
46/// Creates a step that will execute the given artifact. This step will allow running the
47/// binary through emulation when any of the emulation options such as `enable_rosetta` are set to true.
48/// When set to false, and the binary is foreign, running the executable is skipped.
49/// Asserts given artifact is an executable.
50pub fn create(builder: *Builder, name: []const u8, artifact: *LibExeObjStep) *EmulatableRunStep {
51 std.debug.assert(artifact.kind == .exe or artifact.kind == .test_exe);
52 const self = builder.allocator.create(EmulatableRunStep) catch unreachable;
53
54 const option_name = "hide-foreign-warnings";
55 const hide_warnings = if (builder.available_options_map.get(option_name) == null) warn: {
56 break :warn builder.option(bool, option_name, "Hide the warning when a foreign binary which is incompatible is skipped") orelse false;
57 } else false;
58
59 self.* = .{
60 .builder = builder,
61 .step = Step.init(.emulatable_run, name, builder.allocator, make),
62 .exe = artifact,
63 .env_map = null,
64 .cwd = null,
65 .hide_foreign_binaries_warning = hide_warnings,
66 };
67 self.step.dependOn(&artifact.step);
68
69 return self;
70}
71
72fn make(step: *Step) !void {
73 const self = @fieldParentPtr(EmulatableRunStep, "step", step);
74 const host_info = self.builder.host;
75
76 var argv_list = std.ArrayList([]const u8).init(self.builder.allocator);
77 defer argv_list.deinit();
78
79 const need_cross_glibc = self.exe.target.isGnuLibC() and self.exe.is_linking_libc;
80 switch (host_info.getExternalExecutor(self.exe.target_info, .{
81 .qemu_fixes_dl = need_cross_glibc and self.builder.glibc_runtimes_dir != null,
82 .link_libc = self.exe.is_linking_libc,
83 })) {
84 .native => {},
85 .rosetta => if (!self.builder.enable_rosetta) return warnAboutForeignBinaries(self),
86 .wine => |bin_name| if (self.builder.enable_wine) {
87 try argv_list.append(bin_name);
88 } else return,
89 .qemu => |bin_name| if (self.builder.enable_qemu) {
90 const glibc_dir_arg = if (need_cross_glibc)
91 self.builder.glibc_runtimes_dir orelse return
92 else
93 null;
94 try argv_list.append(bin_name);
95 if (glibc_dir_arg) |dir| {
96 // TODO look into making this a call to `linuxTriple`. This
97 // needs the directory to be called "i686" rather than
98 // "i386" which is why we do it manually here.
99 const fmt_str = "{s}" ++ fs.path.sep_str ++ "{s}-{s}-{s}";
100 const cpu_arch = self.exe.target.getCpuArch();
101 const os_tag = self.exe.target.getOsTag();
102 const abi = self.exe.target.getAbi();
103 const cpu_arch_name: []const u8 = if (cpu_arch == .i386)
104 "i686"
105 else
106 @tagName(cpu_arch);
107 const full_dir = try std.fmt.allocPrint(self.builder.allocator, fmt_str, .{
108 dir, cpu_arch_name, @tagName(os_tag), @tagName(abi),
109 });
110
111 try argv_list.append("-L");
112 try argv_list.append(full_dir);
113 }
114 } else return warnAboutForeignBinaries(self),
115 .darling => |bin_name| if (self.builder.enable_darling) {
116 try argv_list.append(bin_name);
117 } else return warnAboutForeignBinaries(self),
118 .wasmtime => |bin_name| if (self.builder.enable_wasmtime) {
119 try argv_list.append(bin_name);
120 try argv_list.append("--dir=.");
121 } else return warnAboutForeignBinaries(self),
122 else => return warnAboutForeignBinaries(self),
123 }
124
125 if (self.exe.target.isWindows()) {
126 // On Windows we don't have rpaths so we have to add .dll search paths to PATH
127 RunStep.addPathForDynLibsInternal(&self.step, self.builder, self.exe);
128 }
129
130 const executable_path = self.exe.installed_path orelse self.exe.getOutputSource().getPath(self.builder);
131 try argv_list.append(executable_path);
132
133 try RunStep.runCommand(
134 argv_list.items,
135 self.builder,
136 self.expected_exit_code,
137 self.stdout_action,
138 self.stderr_action,
139 .Inherit,
140 self.env_map,
141 self.cwd,
142 false,
143 );
144}
145
146pub fn expectStdErrEqual(self: *EmulatableRunStep, bytes: []const u8) void {
147 self.stderr_action = .{ .expect_exact = self.builder.dupe(bytes) };
148}
149
150pub fn expectStdOutEqual(self: *EmulatableRunStep, bytes: []const u8) void {
151 self.stdout_action = .{ .expect_exact = self.builder.dupe(bytes) };
152}
153
154fn warnAboutForeignBinaries(step: *EmulatableRunStep) void {
155 if (step.hide_foreign_binaries_warning) return;
156 const builder = step.builder;
157 const artifact = step.exe;
158
159 const host_name = builder.host.target.zigTriple(builder.allocator) catch unreachable;
160 const foreign_name = artifact.target.zigTriple(builder.allocator) catch unreachable;
161 const target_info = std.zig.system.NativeTargetInfo.detect(builder.allocator, artifact.target) catch unreachable;
162 const need_cross_glibc = artifact.target.isGnuLibC() and artifact.is_linking_libc;
163 switch (builder.host.getExternalExecutor(target_info, .{
164 .qemu_fixes_dl = need_cross_glibc and builder.glibc_runtimes_dir != null,
165 .link_libc = artifact.is_linking_libc,
166 })) {
167 .native => unreachable,
168 .bad_dl => |foreign_dl| {
169 const host_dl = builder.host.dynamic_linker.get() orelse "(none)";
170 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", .{
171 host_dl, foreign_dl, host_dl,
172 });
173 },
174 .bad_os_or_cpu => {
175 std.debug.print("the host system ({s}) does not appear to be capable of executing binaries from the target ({s}).\n", .{
176 host_name, foreign_name,
177 });
178 },
179 .darling => if (!builder.enable_darling) {
180 std.debug.print(
181 "the host system ({s}) does not appear to be capable of executing binaries " ++
182 "from the target ({s}). Consider enabling darling.\n",
183 .{ host_name, foreign_name },
184 );
185 },
186 .rosetta => if (!builder.enable_rosetta) {
187 std.debug.print(
188 "the host system ({s}) does not appear to be capable of executing binaries " ++
189 "from the target ({s}). Consider enabling rosetta.\n",
190 .{ host_name, foreign_name },
191 );
192 },
193 .wine => if (!builder.enable_wine) {
194 std.debug.print(
195 "the host system ({s}) does not appear to be capable of executing binaries " ++
196 "from the target ({s}). Consider enabling wine.\n",
197 .{ host_name, foreign_name },
198 );
199 },
200 .qemu => if (!builder.enable_qemu) {
201 std.debug.print(
202 "the host system ({s}) does not appear to be capable of executing binaries " ++
203 "from the target ({s}). Consider enabling qemu.\n",
204 .{ host_name, foreign_name },
205 );
206 },
207 .wasmtime => {
208 std.debug.print(
209 "the host system ({s}) does not appear to be capable of executing binaries " ++
210 "from the target ({s}). Consider enabling wasmtime.\n",
211 .{ host_name, foreign_name },
212 );
213 },
214 }
215}
lib/std/build/RunStep.zig+79-34
......@@ -97,24 +97,42 @@ pub fn clearEnvironment(self: *RunStep) void {
9797}
9898
9999pub fn addPathDir(self: *RunStep, search_path: []const u8) void {
100 const env_map = self.getEnvMap();
100 addPathDirInternal(&self.step, self.builder, search_path);
101}
102
103/// For internal use only, users of `RunStep` should use `addPathDir` directly.
104fn addPathDirInternal(step: *Step, builder: *Builder, search_path: []const u8) void {
105 const env_map = getEnvMapInternal(step, builder.allocator);
101106
102107 const key = "PATH";
103108 var prev_path = env_map.get(key);
104109
105110 if (prev_path) |pp| {
106 const new_path = self.builder.fmt("{s}" ++ [1]u8{fs.path.delimiter} ++ "{s}", .{ pp, search_path });
111 const new_path = builder.fmt("{s}" ++ [1]u8{fs.path.delimiter} ++ "{s}", .{ pp, search_path });
107112 env_map.put(key, new_path) catch unreachable;
108113 } else {
109 env_map.put(key, self.builder.dupePath(search_path)) catch unreachable;
114 env_map.put(key, builder.dupePath(search_path)) catch unreachable;
110115 }
111116}
112117
113118pub fn getEnvMap(self: *RunStep) *EnvMap {
114 return self.env_map orelse {
115 const env_map = self.builder.allocator.create(EnvMap) catch unreachable;
116 env_map.* = process.getEnvMap(self.builder.allocator) catch unreachable;
117 self.env_map = env_map;
119 return getEnvMapInternal(&self.step, self.builder.allocator);
120}
121
122fn getEnvMapInternal(step: *Step, allocator: Allocator) *EnvMap {
123 const maybe_env_map = switch (step.id) {
124 .run => step.cast(RunStep).?.env_map,
125 .emulatable_run => step.cast(build.EmulatableRunStep).?.env_map,
126 else => unreachable,
127 };
128 return maybe_env_map orelse {
129 const env_map = allocator.create(EnvMap) catch unreachable;
130 env_map.* = process.getEnvMap(allocator) catch unreachable;
131 switch (step.id) {
132 .run => step.cast(RunStep).?.env_map = env_map,
133 .emulatable_run => step.cast(RunStep).?.env_map = env_map,
134 else => unreachable,
135 }
118136 return env_map;
119137 };
120138}
......@@ -146,10 +164,7 @@ fn stdIoActionToBehavior(action: StdIoAction) std.ChildProcess.StdIo {
146164fn make(step: *Step) !void {
147165 const self = @fieldParentPtr(RunStep, "step", step);
148166
149 const cwd = if (self.cwd) |cwd| self.builder.pathFromRoot(cwd) else self.builder.build_root;
150
151167 var argv_list = ArrayList([]const u8).init(self.builder.allocator);
152
153168 for (self.argv.items) |arg| {
154169 switch (arg) {
155170 .bytes => |bytes| try argv_list.append(bytes),
......@@ -165,24 +180,48 @@ fn make(step: *Step) !void {
165180 }
166181 }
167182
168 const argv = argv_list.items;
183 try runCommand(
184 argv_list.items,
185 self.builder,
186 self.expected_exit_code,
187 self.stdout_action,
188 self.stderr_action,
189 self.stdin_behavior,
190 self.env_map,
191 self.cwd,
192 self.print,
193 );
194}
195
196pub fn runCommand(
197 argv: []const []const u8,
198 builder: *Builder,
199 expected_exit_code: ?u8,
200 stdout_action: StdIoAction,
201 stderr_action: StdIoAction,
202 stdin_behavior: std.ChildProcess.StdIo,
203 env_map: ?*EnvMap,
204 maybe_cwd: ?[]const u8,
205 print: bool,
206) !void {
207 const cwd = if (maybe_cwd) |cwd| builder.pathFromRoot(cwd) else builder.build_root;
169208
170209 if (!std.process.can_spawn) {
171 const cmd = try std.mem.join(self.builder.allocator, " ", argv);
210 const cmd = try std.mem.join(builder.addInstallDirectory, " ", argv);
172211 std.debug.print("the following command cannot be executed ({s} does not support spawning a child process):\n{s}", .{ @tagName(builtin.os.tag), cmd });
173 self.builder.allocator.free(cmd);
212 builder.allocator.free(cmd);
174213 return ExecError.ExecNotSupported;
175214 }
176215
177 var child = std.ChildProcess.init(argv, self.builder.allocator);
216 var child = std.ChildProcess.init(argv, builder.allocator);
178217 child.cwd = cwd;
179 child.env_map = self.env_map orelse self.builder.env_map;
218 child.env_map = env_map orelse builder.env_map;
180219
181 child.stdin_behavior = self.stdin_behavior;
182 child.stdout_behavior = stdIoActionToBehavior(self.stdout_action);
183 child.stderr_behavior = stdIoActionToBehavior(self.stderr_action);
220 child.stdin_behavior = stdin_behavior;
221 child.stdout_behavior = stdIoActionToBehavior(stdout_action);
222 child.stderr_behavior = stdIoActionToBehavior(stderr_action);
184223
185 if (self.print)
224 if (print)
186225 printCmd(cwd, argv);
187226
188227 child.spawn() catch |err| {
......@@ -193,21 +232,21 @@ fn make(step: *Step) !void {
193232 // TODO need to poll to read these streams to prevent a deadlock (or rely on evented I/O).
194233
195234 var stdout: ?[]const u8 = null;
196 defer if (stdout) |s| self.builder.allocator.free(s);
235 defer if (stdout) |s| builder.allocator.free(s);
197236
198 switch (self.stdout_action) {
237 switch (stdout_action) {
199238 .expect_exact, .expect_matches => {
200 stdout = child.stdout.?.reader().readAllAlloc(self.builder.allocator, max_stdout_size) catch unreachable;
239 stdout = child.stdout.?.reader().readAllAlloc(builder.allocator, max_stdout_size) catch unreachable;
201240 },
202241 .inherit, .ignore => {},
203242 }
204243
205244 var stderr: ?[]const u8 = null;
206 defer if (stderr) |s| self.builder.allocator.free(s);
245 defer if (stderr) |s| builder.allocator.free(s);
207246
208 switch (self.stderr_action) {
247 switch (stderr_action) {
209248 .expect_exact, .expect_matches => {
210 stderr = child.stderr.?.reader().readAllAlloc(self.builder.allocator, max_stdout_size) catch unreachable;
249 stderr = child.stderr.?.reader().readAllAlloc(builder.allocator, max_stdout_size) catch unreachable;
211250 },
212251 .inherit, .ignore => {},
213252 }
......@@ -219,18 +258,18 @@ fn make(step: *Step) !void {
219258
220259 switch (term) {
221260 .Exited => |code| blk: {
222 const expected_exit_code = self.expected_exit_code orelse break :blk;
261 const expected_code = expected_exit_code orelse break :blk;
223262
224 if (code != expected_exit_code) {
225 if (self.builder.prominent_compile_errors) {
263 if (code != expected_code) {
264 if (builder.prominent_compile_errors) {
226265 std.debug.print("Run step exited with error code {} (expected {})\n", .{
227266 code,
228 expected_exit_code,
267 expected_code,
229268 });
230269 } else {
231270 std.debug.print("The following command exited with error code {} (expected {}):\n", .{
232271 code,
233 expected_exit_code,
272 expected_code,
234273 });
235274 printCmd(cwd, argv);
236275 }
......@@ -245,7 +284,7 @@ fn make(step: *Step) !void {
245284 },
246285 }
247286
248 switch (self.stderr_action) {
287 switch (stderr_action) {
249288 .inherit, .ignore => {},
250289 .expect_exact => |expected_bytes| {
251290 if (!mem.eql(u8, expected_bytes, stderr.?)) {
......@@ -277,7 +316,7 @@ fn make(step: *Step) !void {
277316 },
278317 }
279318
280 switch (self.stdout_action) {
319 switch (stdout_action) {
281320 .inherit, .ignore => {},
282321 .expect_exact => |expected_bytes| {
283322 if (!mem.eql(u8, expected_bytes, stdout.?)) {
......@@ -319,12 +358,18 @@ fn printCmd(cwd: ?[]const u8, argv: []const []const u8) void {
319358}
320359
321360fn addPathForDynLibs(self: *RunStep, artifact: *LibExeObjStep) void {
361 addPathForDynLibsInternal(&self.step, self.builder, artifact);
362}
363
364/// This should only be used for internal usage, this is called automatically
365/// for the user.
366pub fn addPathForDynLibsInternal(step: *Step, builder: *Builder, artifact: *LibExeObjStep) void {
322367 for (artifact.link_objects.items) |link_object| {
323368 switch (link_object) {
324369 .other_step => |other| {
325370 if (other.target.isWindows() and other.isDynamicLibrary()) {
326 self.addPathDir(fs.path.dirname(other.getOutputSource().getPath(self.builder)).?);
327 self.addPathForDynLibs(other);
371 addPathDirInternal(step, builder, fs.path.dirname(other.getOutputSource().getPath(builder)).?);
372 addPathForDynLibsInternal(step, builder, other);
328373 }
329374 },
330375 else => {},
test/link.zig+63-65
......@@ -47,69 +47,67 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
4747 .requires_stage2 = true,
4848 });
4949
50 if (builtin.os.tag == .macos) {
51 cases.addBuildFile("test/link/macho/entry/build.zig", .{
52 .build_modes = true,
53 });
54
55 cases.addBuildFile("test/link/macho/pagezero/build.zig", .{
56 .build_modes = false,
57 });
58
59 cases.addBuildFile("test/link/macho/dylib/build.zig", .{
60 .build_modes = true,
61 });
62
63 cases.addBuildFile("test/link/macho/dead_strip/build.zig", .{
64 .build_modes = false,
65 });
66
67 cases.addBuildFile("test/link/macho/dead_strip_dylibs/build.zig", .{
68 .build_modes = true,
69 .requires_macos_sdk = true,
70 });
71
72 cases.addBuildFile("test/link/macho/needed_library/build.zig", .{
73 .build_modes = true,
74 });
75
76 cases.addBuildFile("test/link/macho/weak_library/build.zig", .{
77 .build_modes = true,
78 });
79
80 cases.addBuildFile("test/link/macho/needed_framework/build.zig", .{
81 .build_modes = true,
82 .requires_macos_sdk = true,
83 });
84
85 cases.addBuildFile("test/link/macho/weak_framework/build.zig", .{
86 .build_modes = true,
87 .requires_macos_sdk = true,
88 });
89
90 // Try to build and run an Objective-C executable.
91 cases.addBuildFile("test/link/macho/objc/build.zig", .{
92 .build_modes = true,
93 .requires_macos_sdk = true,
94 });
95
96 // Try to build and run an Objective-C++ executable.
97 cases.addBuildFile("test/link/macho/objcpp/build.zig", .{
98 .build_modes = true,
99 .requires_macos_sdk = true,
100 });
101
102 cases.addBuildFile("test/link/macho/stack_size/build.zig", .{
103 .build_modes = true,
104 });
105
106 cases.addBuildFile("test/link/macho/search_strategy/build.zig", .{
107 .build_modes = true,
108 });
109
110 cases.addBuildFile("test/link/macho/headerpad/build.zig", .{
111 .build_modes = true,
112 .requires_macos_sdk = true,
113 });
114 }
50 cases.addBuildFile("test/link/macho/entry/build.zig", .{
51 .build_modes = true,
52 });
53
54 cases.addBuildFile("test/link/macho/pagezero/build.zig", .{
55 .build_modes = false,
56 });
57
58 cases.addBuildFile("test/link/macho/dylib/build.zig", .{
59 .build_modes = true,
60 });
61
62 cases.addBuildFile("test/link/macho/dead_strip/build.zig", .{
63 .build_modes = false,
64 });
65
66 cases.addBuildFile("test/link/macho/dead_strip_dylibs/build.zig", .{
67 .build_modes = true,
68 .requires_macos_sdk = true,
69 });
70
71 cases.addBuildFile("test/link/macho/needed_library/build.zig", .{
72 .build_modes = true,
73 });
74
75 cases.addBuildFile("test/link/macho/weak_library/build.zig", .{
76 .build_modes = true,
77 });
78
79 cases.addBuildFile("test/link/macho/needed_framework/build.zig", .{
80 .build_modes = true,
81 .requires_macos_sdk = true,
82 });
83
84 cases.addBuildFile("test/link/macho/weak_framework/build.zig", .{
85 .build_modes = true,
86 .requires_macos_sdk = true,
87 });
88
89 // Try to build and run an Objective-C executable.
90 cases.addBuildFile("test/link/macho/objc/build.zig", .{
91 .build_modes = true,
92 .requires_macos_sdk = true,
93 });
94
95 // Try to build and run an Objective-C++ executable.
96 cases.addBuildFile("test/link/macho/objcpp/build.zig", .{
97 .build_modes = true,
98 .requires_macos_sdk = true,
99 });
100
101 cases.addBuildFile("test/link/macho/stack_size/build.zig", .{
102 .build_modes = true,
103 });
104
105 cases.addBuildFile("test/link/macho/search_strategy/build.zig", .{
106 .build_modes = true,
107 });
108
109 cases.addBuildFile("test/link/macho/headerpad/build.zig", .{
110 .build_modes = true,
111 .requires_macos_sdk = true,
112 });
115113}
test/link/macho/dead_strip/build.zig+2-6
......@@ -16,9 +16,7 @@ pub fn build(b: *Builder) void {
1616 check.checkInSymtab();
1717 check.checkNext("{*} (__TEXT,__text) external _iAmUnused");
1818
19 test_step.dependOn(&check.step);
20
21 const run_cmd = exe.run();
19 const run_cmd = check.runAndCompare();
2220 run_cmd.expectStdOutEqual("Hello!\n");
2321 test_step.dependOn(&run_cmd.step);
2422 }
......@@ -32,9 +30,7 @@ pub fn build(b: *Builder) void {
3230 check.checkInSymtab();
3331 check.checkNotPresent("{*} (__TEXT,__text) external _iAmUnused");
3432
35 test_step.dependOn(&check.step);
36
37 const run_cmd = exe.run();
33 const run_cmd = check.runAndCompare();
3834 run_cmd.expectStdOutEqual("Hello!\n");
3935 test_step.dependOn(&run_cmd.step);
4036 }
test/link/macho/dylib/build.zig+4-3
......@@ -3,12 +3,14 @@ const Builder = std.build.Builder;
33
44pub fn build(b: *Builder) void {
55 const mode = b.standardReleaseOptions();
6 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
67
78 const test_step = b.step("test", "Test");
89 test_step.dependOn(b.getInstallStep());
910
1011 const dylib = b.addSharedLibrary("a", null, b.version(1, 0, 0));
1112 dylib.setBuildMode(mode);
13 dylib.setTarget(target);
1214 dylib.addCSourceFile("a.c", &.{});
1315 dylib.linkLibC();
1416 dylib.install();
......@@ -23,6 +25,7 @@ pub fn build(b: *Builder) void {
2325 test_step.dependOn(&check_dylib.step);
2426
2527 const exe = b.addExecutable("main", null);
28 exe.setTarget(target);
2629 exe.setBuildMode(mode);
2730 exe.addCSourceFile("main.c", &.{});
2831 exe.linkSystemLibrary("a");
......@@ -40,9 +43,7 @@ pub fn build(b: *Builder) void {
4043 check_exe.checkStart("cmd RPATH");
4144 check_exe.checkNext(std.fmt.allocPrint(b.allocator, "path {s}", .{b.pathFromRoot("zig-out/lib")}) catch unreachable);
4245
43 test_step.dependOn(&check_exe.step);
44
45 const run = exe.run();
46 const run = check_exe.runAndCompare();
4647 run.cwd = b.pathFromRoot(".");
4748 run.expectStdOutEqual("Hello world");
4849 test_step.dependOn(&run.step);
test/link/macho/entry/build.zig+2-3
......@@ -8,6 +8,7 @@ pub fn build(b: *Builder) void {
88 test_step.dependOn(b.getInstallStep());
99
1010 const exe = b.addExecutable("main", null);
11 exe.setTarget(.{ .os_tag = .macos });
1112 exe.setBuildMode(mode);
1213 exe.addCSourceFile("main.c", &.{});
1314 exe.linkLibC();
......@@ -26,9 +27,7 @@ pub fn build(b: *Builder) void {
2627
2728 check_exe.checkComputeCompare("vmaddr entryoff +", .{ .op = .eq, .value = .{ .variable = "n_value" } });
2829
29 test_step.dependOn(&check_exe.step);
30
31 const run = exe.run();
30 const run = check_exe.runAndCompare();
3231 run.expectStdOutEqual("42");
3332 test_step.dependOn(&run.step);
3433}
test/link/macho/needed_library/build.zig+4-2
......@@ -4,11 +4,13 @@ const LibExeObjectStep = std.build.LibExeObjStep;
44
55pub fn build(b: *Builder) void {
66 const mode = b.standardReleaseOptions();
7 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
78
89 const test_step = b.step("test", "Test the program");
910 test_step.dependOn(b.getInstallStep());
1011
1112 const dylib = b.addSharedLibrary("a", null, b.version(1, 0, 0));
13 dylib.setTarget(target);
1214 dylib.setBuildMode(mode);
1315 dylib.addCSourceFile("a.c", &.{});
1416 dylib.linkLibC();
......@@ -19,6 +21,7 @@ pub fn build(b: *Builder) void {
1921 const exe = b.addExecutable("test", null);
2022 exe.addCSourceFile("main.c", &[0][]const u8{});
2123 exe.setBuildMode(mode);
24 exe.setTarget(target);
2225 exe.linkLibC();
2326 exe.linkSystemLibraryNeeded("a");
2427 exe.addLibraryPath(b.pathFromRoot("zig-out/lib"));
......@@ -28,8 +31,7 @@ pub fn build(b: *Builder) void {
2831 const check = exe.checkObject(.macho);
2932 check.checkStart("cmd LOAD_DYLIB");
3033 check.checkNext("name @rpath/liba.dylib");
31 test_step.dependOn(&check.step);
3234
33 const run_cmd = exe.run();
35 const run_cmd = check.runAndCompare();
3436 test_step.dependOn(&run_cmd.step);
3537}
test/link/macho/objc/build.zig+1-2
......@@ -7,7 +7,6 @@ pub fn build(b: *Builder) void {
77 const test_step = b.step("test", "Test the program");
88
99 const exe = b.addExecutable("test", null);
10 b.default_step.dependOn(&exe.step);
1110 exe.addIncludePath(".");
1211 exe.addCSourceFile("Foo.m", &[0][]const u8{});
1312 exe.addCSourceFile("test.m", &[0][]const u8{});
......@@ -17,6 +16,6 @@ pub fn build(b: *Builder) void {
1716 // populate paths to the sysroot here.
1817 exe.linkFramework("Foundation");
1918
20 const run_cmd = exe.run();
19 const run_cmd = std.build.EmulatableRunStep.create(b, "run", exe);
2120 test_step.dependOn(&run_cmd.step);
2221}
test/link/macho/pagezero/build.zig+2
......@@ -9,6 +9,7 @@ pub fn build(b: *Builder) void {
99
1010 {
1111 const exe = b.addExecutable("pagezero", null);
12 exe.setTarget(.{ .os_tag = .macos });
1213 exe.setBuildMode(mode);
1314 exe.addCSourceFile("main.c", &.{});
1415 exe.linkLibC();
......@@ -28,6 +29,7 @@ pub fn build(b: *Builder) void {
2829
2930 {
3031 const exe = b.addExecutable("no_pagezero", null);
32 exe.setTarget(.{ .os_tag = .macos });
3133 exe.setBuildMode(mode);
3234 exe.addCSourceFile("main.c", &.{});
3335 exe.linkLibC();
test/link/macho/search_strategy/build.zig+6-4
......@@ -1,6 +1,7 @@
11const std = @import("std");
22const Builder = std.build.Builder;
33const LibExeObjectStep = std.build.LibExeObjStep;
4const target: std.zig.CrossTarget = .{ .os_tag = .macos };
45
56pub fn build(b: *Builder) void {
67 const mode = b.standardReleaseOptions();
......@@ -17,9 +18,7 @@ pub fn build(b: *Builder) void {
1718 check.checkStart("cmd LOAD_DYLIB");
1819 check.checkNext("name @rpath/liba.dylib");
1920
20 test_step.dependOn(&check.step);
21
22 const run = exe.run();
21 const run = check.runAndCompare();
2322 run.cwd = b.pathFromRoot(".");
2423 run.expectStdOutEqual("Hello world");
2524 test_step.dependOn(&run.step);
......@@ -30,7 +29,7 @@ pub fn build(b: *Builder) void {
3029 const exe = createScenario(b, mode);
3130 exe.search_strategy = .paths_first;
3231
33 const run = exe.run();
32 const run = std.build.EmulatableRunStep.create(b, "run", exe);
3433 run.cwd = b.pathFromRoot(".");
3534 run.expectStdOutEqual("Hello world");
3635 test_step.dependOn(&run.step);
......@@ -39,6 +38,7 @@ pub fn build(b: *Builder) void {
3938
4039fn createScenario(b: *Builder, mode: std.builtin.Mode) *LibExeObjectStep {
4140 const static = b.addStaticLibrary("a", null);
41 static.setTarget(target);
4242 static.setBuildMode(mode);
4343 static.addCSourceFile("a.c", &.{});
4444 static.linkLibC();
......@@ -48,6 +48,7 @@ fn createScenario(b: *Builder, mode: std.builtin.Mode) *LibExeObjectStep {
4848 static.install();
4949
5050 const dylib = b.addSharedLibrary("a", null, b.version(1, 0, 0));
51 dylib.setTarget(target);
5152 dylib.setBuildMode(mode);
5253 dylib.addCSourceFile("a.c", &.{});
5354 dylib.linkLibC();
......@@ -57,6 +58,7 @@ fn createScenario(b: *Builder, mode: std.builtin.Mode) *LibExeObjectStep {
5758 dylib.install();
5859
5960 const exe = b.addExecutable("main", null);
61 exe.setTarget(target);
6062 exe.setBuildMode(mode);
6163 exe.addCSourceFile("main.c", &.{});
6264 exe.linkSystemLibraryName("a");
test/link/macho/stack_size/build.zig+2-3
......@@ -8,6 +8,7 @@ pub fn build(b: *Builder) void {
88 test_step.dependOn(b.getInstallStep());
99
1010 const exe = b.addExecutable("main", null);
11 exe.setTarget(.{ .os_tag = .macos });
1112 exe.setBuildMode(mode);
1213 exe.addCSourceFile("main.c", &.{});
1314 exe.linkLibC();
......@@ -17,8 +18,6 @@ pub fn build(b: *Builder) void {
1718 check_exe.checkStart("cmd MAIN");
1819 check_exe.checkNext("stacksize 100000000");
1920
20 test_step.dependOn(&check_exe.step);
21
22 const run = exe.run();
21 const run = check_exe.runAndCompare();
2322 test_step.dependOn(&run.step);
2423}
test/link/macho/weak_library/build.zig+4-3
......@@ -4,11 +4,13 @@ const LibExeObjectStep = std.build.LibExeObjStep;
44
55pub fn build(b: *Builder) void {
66 const mode = b.standardReleaseOptions();
7 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
78
89 const test_step = b.step("test", "Test the program");
910 test_step.dependOn(b.getInstallStep());
1011
1112 const dylib = b.addSharedLibrary("a", null, b.version(1, 0, 0));
13 dylib.setTarget(target);
1214 dylib.setBuildMode(mode);
1315 dylib.addCSourceFile("a.c", &.{});
1416 dylib.linkLibC();
......@@ -16,6 +18,7 @@ pub fn build(b: *Builder) void {
1618
1719 const exe = b.addExecutable("test", null);
1820 exe.addCSourceFile("main.c", &[0][]const u8{});
21 exe.setTarget(target);
1922 exe.setBuildMode(mode);
2023 exe.linkLibC();
2124 exe.linkSystemLibraryWeak("a");
......@@ -30,9 +33,7 @@ pub fn build(b: *Builder) void {
3033 check.checkNext("(undefined) weak external _a (from liba)");
3134 check.checkNext("(undefined) weak external _asStr (from liba)");
3235
33 test_step.dependOn(&check.step);
34
35 const run_cmd = exe.run();
36 const run_cmd = check.runAndCompare();
3637 run_cmd.expectStdOutEqual("42 42");
3738 test_step.dependOn(&run_cmd.step);
3839}