authorgravatar for techatrix@mailbox.orgTechatrix <techatrix@mailbox.org> 2026-08-12 20:36:27+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-08-13 02:03:22+02:00
log3ec6d655dd61ed5fd0a0d33e33e58b12d5b658f5
tree0c2dc079a668ff74c0816db732a7d9716840e211
parent8e48a5c524f970c4630725e2efe69243d8d79fde

fix compilation errors when building compiler for wasm32-wasi


4 files changed, 84 insertions(+), 55 deletions(-)

src/link.zig+1-1
......@@ -1337,7 +1337,7 @@ pub const File = struct {
13371337 // with 0o755 permissions, but it works appropriately if the system is configured
13381338 // more leniently. As another data point, C's fopen seems to open files with the
13391339 // 666 mode.
1340 const executable_mode: Io.File.Permissions = if (builtin.target.os.tag == .windows)
1340 const executable_mode: Io.File.Permissions = if (builtin.target.os.tag == .windows or std.posix.mode_t == u0)
13411341 .default_file
13421342 else
13431343 .fromMode(0o777);
src/main.zig+79-21
......@@ -419,9 +419,32 @@ fn mainArgs(
419419 },
420420 .targets => {
421421 dev.check(.targets_command);
422 const self_exe_path = switch (native_os) {
423 .wasi => {},
424 else => process.executablePathAlloc(io, arena) catch |err| fatal("unable to find zig self exe path: {t}", .{err}),
425 };
426 var dirs: std.zig.Directories = .init(
427 arena,
428 io,
429 EnvVar.ZIG_LIB_DIR.get(environ_map),
430 EnvVar.ZIG_GLOBAL_CACHE_DIR.get(environ_map),
431 .global,
432 preopens,
433 self_exe_path,
434 environ_map,
435 try std.zig.getResolvedCwd(io, arena),
436 );
437 defer dirs.deinit(io);
422438 const host = std.zig.resolveTargetQueryOrFatal(io, .{});
423439 var stdout_writer = Io.File.stdout().writer(io, &stdout_buffer);
424 try @import("print_targets.zig").cmdTargets(arena, io, cmd_args, &stdout_writer.interface, &host);
440 try @import("print_targets.zig").cmdTargets(
441 arena,
442 io,
443 &dirs,
444 cmd_args,
445 &stdout_writer.interface,
446 &host,
447 );
425448 return stdout_writer.interface.flush();
426449 },
427450 .version => {
......@@ -431,16 +454,31 @@ fn mainArgs(
431454 },
432455 .env => {
433456 dev.check(.env_command);
457 const self_exe_path = switch (native_os) {
458 .wasi => args[0],
459 else => process.executablePathAlloc(io, arena) catch |err| fatal("unable to find zig self exe path: {t}", .{err}),
460 };
461 var dirs: std.zig.Directories = .init(
462 arena,
463 io,
464 EnvVar.ZIG_LIB_DIR.get(environ_map),
465 EnvVar.ZIG_GLOBAL_CACHE_DIR.get(environ_map),
466 .global,
467 preopens,
468 if (native_os != .wasi) self_exe_path,
469 environ_map,
470 try std.zig.getResolvedCwd(io, arena),
471 );
472 defer dirs.deinit(io);
434473 const host = std.zig.resolveTargetQueryOrFatal(io, .{});
435474 var stdout_writer = Io.File.stdout().writer(io, &stdout_buffer);
436475 try @import("print_env.zig").cmdEnv(
437476 arena,
438 io,
439477 &stdout_writer.interface,
440 args,
441 preopens,
442478 &host,
443479 environ_map,
480 &dirs,
481 self_exe_path,
444482 );
445483 return stdout_writer.interface.flush();
446484 },
......@@ -3813,9 +3851,14 @@ fn buildOutputType(
38133851 }) {
38143852 dev.checkAny(&.{ .run_command, .test_command });
38153853
3854 const self_exe_path_or_argv0 = switch (native_os) {
3855 .wasi => all_args[0], // Will error because of `!process.can_spawn`
3856 else => self_exe_path,
3857 };
3858
38163859 if (test_exec_args.items.len == 0 and target.ofmt == .c and emit_bin_resolved != .no) {
38173860 // Default to using `zig run` to execute the produced .c code from `zig test`.
3818 try test_exec_args.appendSlice(arena, &.{ self_exe_path, "run" });
3861 try test_exec_args.appendSlice(arena, &.{ self_exe_path_or_argv0, "run" });
38193862 // Skip passing `-ofmt`, we want the default for the target, not `.c` anymore.
38203863
38213864 var prev_has_cflags = false;
......@@ -3896,7 +3939,7 @@ fn buildOutputType(
38963939 arena,
38973940 io,
38983941 test_exec_args.items,
3899 self_exe_path,
3942 self_exe_path_or_argv0,
39003943 arg_mode,
39013944 target,
39023945 &comp_destroyed,
......@@ -4287,7 +4330,10 @@ fn serve(
42874330 in: *Io.Reader,
42884331 out: *Io.Writer,
42894332 test_exec_args: []const ?[]const u8,
4290 self_exe_path: ?[]const u8,
4333 self_exe_path: switch (native_os) {
4334 .wasi => void,
4335 else => []const u8,
4336 },
42914337 arg_mode: ArgMode,
42924338 all_args: []const []const u8,
42934339 runtime_args_start: ?usize,
......@@ -4400,7 +4446,7 @@ fn serve(
44004446 comp,
44014447 gpa,
44024448 test_exec_args,
4403 self_exe_path.?,
4449 self_exe_path,
44044450 arg_mode,
44054451 all_args,
44064452 runtime_args_start,
......@@ -4585,9 +4631,8 @@ fn runOrTest(
45854631 const cmd = try std.mem.join(arena, " ", argv.items);
45864632 fatal("the following command failed to execve with '{t}':\n{s}", .{ err, cmd });
45874633 } else if (!process.can_spawn) {
4588 const cmd = try std.mem.join(arena, " ", argv.items);
4589 fatal("the following command cannot be executed ({t} does not support spawning a child process):\n{s}", .{
4590 native_os, cmd,
4634 fatal("the following command cannot be executed ({t} does not support spawning a child process):\n{f}", .{
4635 native_os, std.zig.SubprocessCommand{ .argv = argv.items },
45914636 });
45924637 }
45934638 const term_result = (term: {
......@@ -4667,7 +4712,10 @@ fn runOrTestHotSwap(
46674712 comp: *Compilation,
46684713 gpa: Allocator,
46694714 test_exec_args: []const ?[]const u8,
4670 self_exe_path: []const u8,
4715 self_exe_path: switch (native_os) {
4716 .wasi => void,
4717 else => []const u8,
4718 },
46714719 arg_mode: ArgMode,
46724720 all_args: []const []const u8,
46734721 runtime_args_start: ?usize,
......@@ -4675,6 +4723,11 @@ fn runOrTestHotSwap(
46754723 const io = comp.io;
46764724 const lf = comp.bin_file.?;
46774725
4726 const self_exe_path_or_argv0 = switch (native_os) {
4727 .wasi => all_args[0], // Will error because of `!process.can_spawn`
4728 else => self_exe_path,
4729 };
4730
46784731 const exe_path = switch (builtin.target.os.tag) {
46794732 // On Windows it seems impossible to perform an atomic rename of a file that is currently
46804733 // running in a process. Therefore, we do the opposite. We create a copy of the file in
......@@ -4700,7 +4753,7 @@ fn runOrTestHotSwap(
47004753 // when testing pass the zig_exe_path to argv
47014754 if (arg_mode == .zig_test)
47024755 try argv.appendSlice(&[_][]const u8{
4703 exe_path, self_exe_path,
4756 exe_path, self_exe_path_or_argv0,
47044757 })
47054758 // when running just pass the current exe
47064759 else
......@@ -4713,7 +4766,7 @@ fn runOrTestHotSwap(
47134766 try argv.append(a);
47144767 } else {
47154768 try argv.appendSlice(&[_][]const u8{
4716 exe_path, self_exe_path,
4769 exe_path, self_exe_path_or_argv0,
47174770 });
47184771 }
47194772 }
......@@ -4722,6 +4775,12 @@ fn runOrTestHotSwap(
47224775 try argv.appendSlice(all_args[i..]);
47234776 }
47244777
4778 if (!process.can_spawn) {
4779 fatal("the following command cannot be executed ({t} does not support spawning a child process):\n{f}", .{
4780 native_os, std.zig.SubprocessCommand{ .argv = argv.items },
4781 });
4782 }
4783
47254784 const child = try std.process.spawn(io, .{
47264785 .argv = argv.items,
47274786 .stdin = .inherit,
......@@ -4902,6 +4961,12 @@ fn jitCmdInner(
49024961 thread_limit: usize,
49034962 options: JitCmdOptions,
49044963) !void {
4964 if (!std.process.can_spawn) {
4965 fatal("The {s} command cannot be executed ({t} does not support spawning a child process)", .{
4966 options.cmd_name, native_os,
4967 });
4968 }
4969
49054970 const target_query: std.Target.Query = .{};
49064971 const resolved_target: Module.ResolvedTarget = .{
49074972 .result = std.zig.resolveTargetQueryOrFatal(io, target_query),
......@@ -5074,13 +5139,6 @@ fn jitCmdInner(
50745139 fatal("the following command failed to execve with {t}:\n{s}", .{ err, cmd });
50755140 }
50765141
5077 if (!process.can_spawn) {
5078 const cmd = try std.mem.join(arena, " ", child_argv.items);
5079 fatal("the following command cannot be executed ({t} does not support spawning a child process):\n{s}", .{
5080 native_os, cmd,
5081 });
5082 }
5083
50845142 const term = t: {
50855143 _ = try io.lockStderr(&.{}, .no_color);
50865144 defer io.unlockStderr();
src/print_env.zig+2-28
......@@ -11,38 +11,12 @@ const Compilation = @import("Compilation.zig");
1111
1212pub fn cmdEnv(
1313 arena: Allocator,
14 io: Io,
1514 out: *std.Io.Writer,
16 args: []const []const u8,
17 preopens: std.process.Preopens,
1815 host: *const std.Target,
1916 environ_map: *std.process.Environ.Map,
17 dirs: *const std.zig.Directories,
18 self_exe_path: []const u8,
2019) !void {
21 const override_lib_dir: ?[]const u8 = EnvVar.ZIG_LIB_DIR.get(environ_map);
22 const override_global_cache_dir: ?[]const u8 = EnvVar.ZIG_GLOBAL_CACHE_DIR.get(environ_map);
23
24 const self_exe_path = switch (builtin.target.os.tag) {
25 .wasi => args[0],
26 else => std.process.executablePathAlloc(io, arena) catch |err| {
27 fatal("unable to find zig self exe path: {t}", .{err});
28 },
29 };
30
31 const cwd_path = try std.zig.getResolvedCwd(io, arena);
32
33 var dirs: std.zig.Directories = .init(
34 arena,
35 io,
36 override_lib_dir,
37 override_global_cache_dir,
38 .global,
39 preopens,
40 if (builtin.target.os.tag != .wasi) self_exe_path,
41 environ_map,
42 cwd_path,
43 );
44 defer dirs.deinit(io);
45
4620 const zig_lib_dir = dirs.zig_lib.path orelse "";
4721 const zig_std_dir = try dirs.zig_lib.join(arena, &.{"std"});
4822 const global_cache_dir = dirs.global_cache.path orelse "";
src/print_targets.zig+2-5
......@@ -14,16 +14,13 @@ const target = @import("target.zig");
1414pub fn cmdTargets(
1515 allocator: Allocator,
1616 io: Io,
17 directories: *const std.zig.Directories,
1718 args: []const []const u8,
1819 out: *std.Io.Writer,
1920 native_target: *const Target,
2021) !void {
2122 _ = args;
22 var zig_lib_directory = std.zig.findZigLibDir(allocator, io) catch |err|
23 fatal("unable to find zig installation directory: {t}", .{err});
24 defer zig_lib_directory.handle.close(io);
25 defer allocator.free(zig_lib_directory.path.?);
26
23 const zig_lib_directory = directories.zig_lib;
2724 const abilists_contents = zig_lib_directory.handle.readFileAlloc(
2825 io,
2926 glibc.abilists_path,