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 {...@@ -1337,7 +1337,7 @@ pub const File = struct {
1337 // with 0o755 permissions, but it works appropriately if the system is configured1337 // with 0o755 permissions, but it works appropriately if the system is configured
1338 // more leniently. As another data point, C's fopen seems to open files with the1338 // more leniently. As another data point, C's fopen seems to open files with the
1339 // 666 mode.1339 // 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)
1341 .default_file1341 .default_file
1342 else1342 else
1343 .fromMode(0o777);1343 .fromMode(0o777);
src/main.zig+79-21
...@@ -419,9 +419,32 @@ fn mainArgs(...@@ -419,9 +419,32 @@ fn mainArgs(
419 },419 },
420 .targets => {420 .targets => {
421 dev.check(.targets_command);421 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);
422 const host = std.zig.resolveTargetQueryOrFatal(io, .{});438 const host = std.zig.resolveTargetQueryOrFatal(io, .{});
423 var stdout_writer = Io.File.stdout().writer(io, &stdout_buffer);439 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 );
425 return stdout_writer.interface.flush();448 return stdout_writer.interface.flush();
426 },449 },
427 .version => {450 .version => {
...@@ -431,16 +454,31 @@ fn mainArgs(...@@ -431,16 +454,31 @@ fn mainArgs(
431 },454 },
432 .env => {455 .env => {
433 dev.check(.env_command);456 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);
434 const host = std.zig.resolveTargetQueryOrFatal(io, .{});473 const host = std.zig.resolveTargetQueryOrFatal(io, .{});
435 var stdout_writer = Io.File.stdout().writer(io, &stdout_buffer);474 var stdout_writer = Io.File.stdout().writer(io, &stdout_buffer);
436 try @import("print_env.zig").cmdEnv(475 try @import("print_env.zig").cmdEnv(
437 arena,476 arena,
438 io,
439 &stdout_writer.interface,477 &stdout_writer.interface,
440 args,
441 preopens,
442 &host,478 &host,
443 environ_map,479 environ_map,
480 &dirs,
481 self_exe_path,
444 );482 );
445 return stdout_writer.interface.flush();483 return stdout_writer.interface.flush();
446 },484 },
...@@ -3813,9 +3851,14 @@ fn buildOutputType(...@@ -3813,9 +3851,14 @@ fn buildOutputType(
3813 }) {3851 }) {
3814 dev.checkAny(&.{ .run_command, .test_command });3852 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
3816 if (test_exec_args.items.len == 0 and target.ofmt == .c and emit_bin_resolved != .no) {3859 if (test_exec_args.items.len == 0 and target.ofmt == .c and emit_bin_resolved != .no) {
3817 // Default to using `zig run` to execute the produced .c code from `zig test`.3860 // 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" });
3819 // Skip passing `-ofmt`, we want the default for the target, not `.c` anymore.3862 // Skip passing `-ofmt`, we want the default for the target, not `.c` anymore.
38203863
3821 var prev_has_cflags = false;3864 var prev_has_cflags = false;
...@@ -3896,7 +3939,7 @@ fn buildOutputType(...@@ -3896,7 +3939,7 @@ fn buildOutputType(
3896 arena,3939 arena,
3897 io,3940 io,
3898 test_exec_args.items,3941 test_exec_args.items,
3899 self_exe_path,3942 self_exe_path_or_argv0,
3900 arg_mode,3943 arg_mode,
3901 target,3944 target,
3902 &comp_destroyed,3945 &comp_destroyed,
...@@ -4287,7 +4330,10 @@ fn serve(...@@ -4287,7 +4330,10 @@ fn serve(
4287 in: *Io.Reader,4330 in: *Io.Reader,
4288 out: *Io.Writer,4331 out: *Io.Writer,
4289 test_exec_args: []const ?[]const u8,4332 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 },
4291 arg_mode: ArgMode,4337 arg_mode: ArgMode,
4292 all_args: []const []const u8,4338 all_args: []const []const u8,
4293 runtime_args_start: ?usize,4339 runtime_args_start: ?usize,
...@@ -4400,7 +4446,7 @@ fn serve(...@@ -4400,7 +4446,7 @@ fn serve(
4400 comp,4446 comp,
4401 gpa,4447 gpa,
4402 test_exec_args,4448 test_exec_args,
4403 self_exe_path.?,4449 self_exe_path,
4404 arg_mode,4450 arg_mode,
4405 all_args,4451 all_args,
4406 runtime_args_start,4452 runtime_args_start,
...@@ -4585,9 +4631,8 @@ fn runOrTest(...@@ -4585,9 +4631,8 @@ fn runOrTest(
4585 const cmd = try std.mem.join(arena, " ", argv.items);4631 const cmd = try std.mem.join(arena, " ", argv.items);
4586 fatal("the following command failed to execve with '{t}':\n{s}", .{ err, cmd });4632 fatal("the following command failed to execve with '{t}':\n{s}", .{ err, cmd });
4587 } else if (!process.can_spawn) {4633 } else if (!process.can_spawn) {
4588 const cmd = try std.mem.join(arena, " ", argv.items);4634 fatal("the following command cannot be executed ({t} does not support spawning a child process):\n{f}", .{
4589 fatal("the following command cannot be executed ({t} does not support spawning a child process):\n{s}", .{4635 native_os, std.zig.SubprocessCommand{ .argv = argv.items },
4590 native_os, cmd,
4591 });4636 });
4592 }4637 }
4593 const term_result = (term: {4638 const term_result = (term: {
...@@ -4667,7 +4712,10 @@ fn runOrTestHotSwap(...@@ -4667,7 +4712,10 @@ fn runOrTestHotSwap(
4667 comp: *Compilation,4712 comp: *Compilation,
4668 gpa: Allocator,4713 gpa: Allocator,
4669 test_exec_args: []const ?[]const u8,4714 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 },
4671 arg_mode: ArgMode,4719 arg_mode: ArgMode,
4672 all_args: []const []const u8,4720 all_args: []const []const u8,
4673 runtime_args_start: ?usize,4721 runtime_args_start: ?usize,
...@@ -4675,6 +4723,11 @@ fn runOrTestHotSwap(...@@ -4675,6 +4723,11 @@ fn runOrTestHotSwap(
4675 const io = comp.io;4723 const io = comp.io;
4676 const lf = comp.bin_file.?;4724 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
4678 const exe_path = switch (builtin.target.os.tag) {4731 const exe_path = switch (builtin.target.os.tag) {
4679 // On Windows it seems impossible to perform an atomic rename of a file that is currently4732 // On Windows it seems impossible to perform an atomic rename of a file that is currently
4680 // running in a process. Therefore, we do the opposite. We create a copy of the file in4733 // running in a process. Therefore, we do the opposite. We create a copy of the file in
...@@ -4700,7 +4753,7 @@ fn runOrTestHotSwap(...@@ -4700,7 +4753,7 @@ fn runOrTestHotSwap(
4700 // when testing pass the zig_exe_path to argv4753 // when testing pass the zig_exe_path to argv
4701 if (arg_mode == .zig_test)4754 if (arg_mode == .zig_test)
4702 try argv.appendSlice(&[_][]const u8{4755 try argv.appendSlice(&[_][]const u8{
4703 exe_path, self_exe_path,4756 exe_path, self_exe_path_or_argv0,
4704 })4757 })
4705 // when running just pass the current exe4758 // when running just pass the current exe
4706 else4759 else
...@@ -4713,7 +4766,7 @@ fn runOrTestHotSwap(...@@ -4713,7 +4766,7 @@ fn runOrTestHotSwap(
4713 try argv.append(a);4766 try argv.append(a);
4714 } else {4767 } else {
4715 try argv.appendSlice(&[_][]const u8{4768 try argv.appendSlice(&[_][]const u8{
4716 exe_path, self_exe_path,4769 exe_path, self_exe_path_or_argv0,
4717 });4770 });
4718 }4771 }
4719 }4772 }
...@@ -4722,6 +4775,12 @@ fn runOrTestHotSwap(...@@ -4722,6 +4775,12 @@ fn runOrTestHotSwap(
4722 try argv.appendSlice(all_args[i..]);4775 try argv.appendSlice(all_args[i..]);
4723 }4776 }
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
4725 const child = try std.process.spawn(io, .{4784 const child = try std.process.spawn(io, .{
4726 .argv = argv.items,4785 .argv = argv.items,
4727 .stdin = .inherit,4786 .stdin = .inherit,
...@@ -4902,6 +4961,12 @@ fn jitCmdInner(...@@ -4902,6 +4961,12 @@ fn jitCmdInner(
4902 thread_limit: usize,4961 thread_limit: usize,
4903 options: JitCmdOptions,4962 options: JitCmdOptions,
4904) !void {4963) !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
4905 const target_query: std.Target.Query = .{};4970 const target_query: std.Target.Query = .{};
4906 const resolved_target: Module.ResolvedTarget = .{4971 const resolved_target: Module.ResolvedTarget = .{
4907 .result = std.zig.resolveTargetQueryOrFatal(io, target_query),4972 .result = std.zig.resolveTargetQueryOrFatal(io, target_query),
...@@ -5074,13 +5139,6 @@ fn jitCmdInner(...@@ -5074,13 +5139,6 @@ fn jitCmdInner(
5074 fatal("the following command failed to execve with {t}:\n{s}", .{ err, cmd });5139 fatal("the following command failed to execve with {t}:\n{s}", .{ err, cmd });
5075 }5140 }
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
5084 const term = t: {5142 const term = t: {
5085 _ = try io.lockStderr(&.{}, .no_color);5143 _ = try io.lockStderr(&.{}, .no_color);
5086 defer io.unlockStderr();5144 defer io.unlockStderr();
src/print_env.zig+2-28
...@@ -11,38 +11,12 @@ const Compilation = @import("Compilation.zig");...@@ -11,38 +11,12 @@ const Compilation = @import("Compilation.zig");
1111
12pub fn cmdEnv(12pub fn cmdEnv(
13 arena: Allocator,13 arena: Allocator,
14 io: Io,
15 out: *std.Io.Writer,14 out: *std.Io.Writer,
16 args: []const []const u8,
17 preopens: std.process.Preopens,
18 host: *const std.Target,15 host: *const std.Target,
19 environ_map: *std.process.Environ.Map,16 environ_map: *std.process.Environ.Map,
17 dirs: *const std.zig.Directories,
18 self_exe_path: []const u8,
20) !void {19) !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
46 const zig_lib_dir = dirs.zig_lib.path orelse "";20 const zig_lib_dir = dirs.zig_lib.path orelse "";
47 const zig_std_dir = try dirs.zig_lib.join(arena, &.{"std"});21 const zig_std_dir = try dirs.zig_lib.join(arena, &.{"std"});
48 const global_cache_dir = dirs.global_cache.path orelse "";22 const global_cache_dir = dirs.global_cache.path orelse "";
src/print_targets.zig+2-5
...@@ -14,16 +14,13 @@ const target = @import("target.zig");...@@ -14,16 +14,13 @@ const target = @import("target.zig");
14pub fn cmdTargets(14pub fn cmdTargets(
15 allocator: Allocator,15 allocator: Allocator,
16 io: Io,16 io: Io,
17 directories: *const std.zig.Directories,
17 args: []const []const u8,18 args: []const []const u8,
18 out: *std.Io.Writer,19 out: *std.Io.Writer,
19 native_target: *const Target,20 native_target: *const Target,
20) !void {21) !void {
21 _ = args;22 _ = args;
22 var zig_lib_directory = std.zig.findZigLibDir(allocator, io) catch |err|23 const zig_lib_directory = directories.zig_lib;
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
27 const abilists_contents = zig_lib_directory.handle.readFileAlloc(24 const abilists_contents = zig_lib_directory.handle.readFileAlloc(
28 io,25 io,
29 glibc.abilists_path,26 glibc.abilists_path,