authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-07-12 18:14:32+02:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-07-23 10:08:53+02:00
log2429c3d73c0c8fc279a556d65881f7c25c6bb6e9
treed77be2d85d80a2ed2b28c43202d2c96e6d1fe75b
parent34b786fb0f458ff80a030a94d722ffcaef95f7e4
signature Commit is signed but in an unrecognized format.

Share logic between EmulatableRunStep & RunStep


3 files changed, 110 insertions(+), 235 deletions(-)

lib/std/build.zig+15
......@@ -1891,6 +1891,21 @@ pub const LibExeObjStep = struct {
18911891 return run_step;
18921892 }
18931893
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
18941909 pub fn checkObject(self: *LibExeObjStep, obj_format: std.Target.ObjectFormat) *CheckObjectStep {
18951910 return CheckObjectStep.create(self.builder, self.getOutputSource(), obj_format);
18961911 }
lib/std/build/EmulatableRunStep.zig+16-201
......@@ -9,6 +9,7 @@ const build = std.build;
99const Step = std.build.Step;
1010const Builder = std.build.Builder;
1111const LibExeObjStep = std.build.LibExeObjStep;
12const RunStep = std.build.RunStep;
1213
1314const fs = std.fs;
1415const process = std.process;
......@@ -16,7 +17,7 @@ const EnvMap = process.EnvMap;
1617
1718const EmulatableRunStep = @This();
1819
19pub const step_id = .emulatable_run;
20pub const base_id = .emulatable_run;
2021
2122const max_stdout_size = 1 * 1024 * 1024; // 1 MiB
2223
......@@ -35,20 +36,13 @@ env_map: ?*EnvMap,
3536/// Set this to modify the current working directory
3637cwd: ?[]const u8,
3738
38stdout_action: StdIoAction = .inherit,
39stderr_action: StdIoAction = .inherit,
39stdout_action: RunStep.StdIoAction = .inherit,
40stderr_action: RunStep.StdIoAction = .inherit,
4041
4142/// When set to true, hides the warning of skipping a foreign binary which cannot be run on the host
4243/// or through emulation.
4344hide_foreign_binaries_warning: bool,
4445
45pub const StdIoAction = union(enum) {
46 inherit,
47 ignore,
48 expect_exact: []const u8,
49 expect_matches: []const []const u8,
50};
51
5246/// Creates a step that will execute the given artifact. This step will allow running the
5347/// binary through emulation when any of the emulation options such as `enable_rosetta` are set to true.
5448/// When set to false, and the binary is foreign, running the executable is skipped.
......@@ -78,7 +72,6 @@ pub fn create(builder: *Builder, name: []const u8, artifact: *LibExeObjStep) *Em
7872fn make(step: *Step) !void {
7973 const self = @fieldParentPtr(EmulatableRunStep, "step", step);
8074 const host_info = self.builder.host;
81 const cwd = if (self.cwd) |cwd| self.builder.pathFromRoot(cwd) else self.builder.build_root;
8275
8376 var argv_list = std.ArrayList([]const u8).init(self.builder.allocator);
8477 defer argv_list.deinit();
......@@ -131,185 +124,23 @@ fn make(step: *Step) !void {
131124
132125 if (self.exe.target.isWindows()) {
133126 // On Windows we don't have rpaths so we have to add .dll search paths to PATH
134 self.addPathForDynLibs(self.exe);
127 RunStep.addPathForDynLibsInternal(&self.step, self.builder, self.exe);
135128 }
136129
137130 const executable_path = self.exe.installed_path orelse self.exe.getOutputSource().getPath(self.builder);
138131 try argv_list.append(executable_path);
139132
140 if (!std.process.can_spawn) {
141 const cmd = try std.mem.join(self.builder.allocator, " ", argv_list.items);
142 std.debug.print("the following command cannot be executed ({s} does not support spawning a child process):\n{s}", .{ @tagName(@import("builtin").os.tag), cmd });
143 self.builder.allocator.free(cmd);
144 return error.ExecNotSupported;
145 }
146
147 var child = std.ChildProcess.init(argv_list.items, self.builder.allocator);
148 child.cwd = cwd;
149 child.env_map = self.env_map orelse self.builder.env_map;
150
151 child.stdin_behavior = .Inherit;
152 child.stdout_behavior = stdIoActionToBehavior(self.stdout_action);
153 child.stderr_behavior = stdIoActionToBehavior(self.stderr_action);
154
155 child.spawn() catch |err| {
156 std.debug.print("Unable to spawn {s}: {s}\n", .{ argv_list.items[0], @errorName(err) });
157 return err;
158 };
159
160 var stdout: ?[]const u8 = null;
161 defer if (stdout) |s| self.builder.allocator.free(s);
162
163 switch (self.stdout_action) {
164 .expect_exact, .expect_matches => {
165 stdout = child.stdout.?.reader().readAllAlloc(self.builder.allocator, max_stdout_size) catch unreachable;
166 },
167 .inherit, .ignore => {},
168 }
169
170 var stderr: ?[]const u8 = null;
171 defer if (stderr) |s| self.builder.allocator.free(s);
172
173 switch (self.stderr_action) {
174 .expect_exact, .expect_matches => {
175 stderr = child.stderr.?.reader().readAllAlloc(self.builder.allocator, max_stdout_size) catch unreachable;
176 },
177 .inherit, .ignore => {},
178 }
179
180 const term = child.wait() catch |err| {
181 std.debug.print("Unable to spawn {s}: {s}\n", .{ argv_list.items[0], @errorName(err) });
182 return err;
183 };
184
185 switch (term) {
186 .Exited => |code| blk: {
187 const expected_exit_code = self.expected_exit_code orelse break :blk;
188
189 if (code != expected_exit_code) {
190 if (self.builder.prominent_compile_errors) {
191 std.debug.print("Run step exited with error code {} (expected {})\n", .{
192 code,
193 expected_exit_code,
194 });
195 } else {
196 std.debug.print("The following command exited with error code {} (expected {}):\n", .{
197 code,
198 expected_exit_code,
199 });
200 printCmd(cwd, argv_list.items);
201 }
202
203 return error.UnexpectedExitCode;
204 }
205 },
206 else => {
207 std.debug.print("The following command terminated unexpectedly:\n", .{});
208 printCmd(cwd, argv_list.items);
209 return error.UncleanExit;
210 },
211 }
212
213 switch (self.stderr_action) {
214 .inherit, .ignore => {},
215 .expect_exact => |expected_bytes| {
216 if (!std.mem.eql(u8, expected_bytes, stderr.?)) {
217 std.debug.print(
218 \\
219 \\========= Expected this stderr: =========
220 \\{s}
221 \\========= But found: ====================
222 \\{s}
223 \\
224 , .{ expected_bytes, stderr.? });
225 printCmd(cwd, argv_list.items);
226 return error.TestFailed;
227 }
228 },
229 .expect_matches => |matches| for (matches) |match| {
230 if (std.mem.indexOf(u8, stderr.?, match) == null) {
231 std.debug.print(
232 \\
233 \\========= Expected to find in stderr: =========
234 \\{s}
235 \\========= But stderr does not contain it: =====
236 \\{s}
237 \\
238 , .{ match, stderr.? });
239 printCmd(cwd, argv_list.items);
240 return error.TestFailed;
241 }
242 },
243 }
244
245 switch (self.stdout_action) {
246 .inherit, .ignore => {},
247 .expect_exact => |expected_bytes| {
248 if (!std.mem.eql(u8, expected_bytes, stdout.?)) {
249 std.debug.print(
250 \\
251 \\========= Expected this stdout: =========
252 \\{s}
253 \\========= But found: ====================
254 \\{s}
255 \\
256 , .{ expected_bytes, stdout.? });
257 printCmd(cwd, argv_list.items);
258 return error.TestFailed;
259 }
260 },
261 .expect_matches => |matches| for (matches) |match| {
262 if (std.mem.indexOf(u8, stdout.?, match) == null) {
263 std.debug.print(
264 \\
265 \\========= Expected to find in stdout: =========
266 \\{s}
267 \\========= But stdout does not contain it: =====
268 \\{s}
269 \\
270 , .{ match, stdout.? });
271 printCmd(cwd, argv_list.items);
272 return error.TestFailed;
273 }
274 },
275 }
276}
277
278fn addPathForDynLibs(self: *EmulatableRunStep, artifact: *LibExeObjStep) void {
279 for (artifact.link_objects.items) |link_object| {
280 switch (link_object) {
281 .other_step => |other| {
282 if (other.target.isWindows() and other.isDynamicLibrary()) {
283 self.addPathDir(fs.path.dirname(other.getOutputSource().getPath(self.builder)).?);
284 self.addPathForDynLibs(other);
285 }
286 },
287 else => {},
288 }
289 }
290}
291
292pub fn addPathDir(self: *EmulatableRunStep, search_path: []const u8) void {
293 const env_map = self.getEnvMap();
294
295 const key = "PATH";
296 var prev_path = env_map.get(key);
297
298 if (prev_path) |pp| {
299 const new_path = self.builder.fmt("{s}" ++ [1]u8{fs.path.delimiter} ++ "{s}", .{ pp, search_path });
300 env_map.put(key, new_path) catch unreachable;
301 } else {
302 env_map.put(key, self.builder.dupePath(search_path)) catch unreachable;
303 }
304}
305
306pub fn getEnvMap(self: *EmulatableRunStep) *EnvMap {
307 return self.env_map orelse {
308 const env_map = self.builder.allocator.create(EnvMap) catch unreachable;
309 env_map.* = process.getEnvMap(self.builder.allocator) catch unreachable;
310 self.env_map = env_map;
311 return env_map;
312 };
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 );
313144}
314145
315146pub fn expectStdErrEqual(self: *EmulatableRunStep, bytes: []const u8) void {
......@@ -320,22 +151,6 @@ pub fn expectStdOutEqual(self: *EmulatableRunStep, bytes: []const u8) void {
320151 self.stdout_action = .{ .expect_exact = self.builder.dupe(bytes) };
321152}
322153
323fn stdIoActionToBehavior(action: StdIoAction) std.ChildProcess.StdIo {
324 return switch (action) {
325 .ignore => .Ignore,
326 .inherit => .Inherit,
327 .expect_exact, .expect_matches => .Pipe,
328 };
329}
330
331fn printCmd(cwd: ?[]const u8, argv: []const []const u8) void {
332 if (cwd) |yes_cwd| std.debug.print("cd {s} && ", .{yes_cwd});
333 for (argv) |arg| {
334 std.debug.print("{s} ", .{arg});
335 }
336 std.debug.print("\n", .{});
337}
338
339154fn warnAboutForeignBinaries(step: *EmulatableRunStep) void {
340155 if (step.hide_foreign_binaries_warning) return;
341156 const builder = step.builder;
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 => {},