diff --git a/lib/std/start.zig b/lib/std/start.zig index 067ba1816871..15ebc0df0b5f 100644 --- a/lib/std/start.zig +++ b/lib/std/start.zig @@ -16,7 +16,7 @@ const native_os = builtin.os.tag; var argc_argv_ptr: [*]usize = undefined; -const start_sym_name = if (native_arch.isMIPS()) "__start" else if (builtin.wasi_exec_model == .reactor) "_initialize" else "_start"; +const start_sym_name = if (native_arch.isMIPS()) "__start" else "_start"; comptime { // No matter what, we import the root file, so that any export, test, comptime @@ -65,7 +65,8 @@ comptime { } else if (native_os == .uefi) { if (!@hasDecl(root, "EfiMain")) @export(EfiMain, .{ .name = "EfiMain" }); } else if (native_arch.isWasm()) { - if (!@hasDecl(root, start_sym_name)) @export(wasm_start, .{ .name = start_sym_name }); + const wasm_start_sym = if (builtin.wasi_exec_model == .reactor) "_initialize" else "_start"; + if (!@hasDecl(root, start_sym_name)) @export(wasm_start, .{ .name = wasm_start_sym }); } else if (native_os != .other and native_os != .freestanding) { if (!@hasDecl(root, start_sym_name)) @export(_start, .{ .name = start_sym_name }); } diff --git a/src/Compilation.zig b/src/Compilation.zig index c714c98d1fa2..7f770599c7ee 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -787,6 +787,9 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { const needs_c_symbols = !options.skip_linker_dependencies and is_exe_or_dyn_lib; + // WASI-only. Resolve the optinal exec-model option, defaults to command. + const wasi_exec_model = if (options.target.os.tag != .wasi) undefined else options.wasi_exec_model orelse .command; + const comp: *Compilation = comp: { // For allocations that have the same lifetime as Compilation. This arena is used only during this // initialization and then is freed in deinit(). @@ -1344,7 +1347,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { .disable_lld_caching = options.disable_lld_caching, .subsystem = options.subsystem, .is_test = options.is_test, - .wasi_exec_model = options.wasi_exec_model, + .wasi_exec_model = wasi_exec_model, }); errdefer bin_file.destroy(); comp.* = .{ @@ -1446,7 +1449,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { }); } comp.work_queue.writeAssumeCapacity(&[_]Job{ - .{ .wasi_libc_crt_file = wasi_libc.getExecModelCRTFIle(options.wasi_exec_model) }, + .{ .wasi_libc_crt_file = wasi_libc.getExecModelCrtFIle(wasi_exec_model) }, .{ .wasi_libc_crt_file = .libc_a }, }); } @@ -3638,14 +3641,13 @@ pub fn generateBuiltinZigSource(comp: *Compilation, allocator: *Allocator) Alloc std.zig.fmtId(@tagName(comp.bin_file.options.machine_code_model)), }); - const wasi_exec_model_fmt = if (comp.bin_file.options.wasi_exec_model) |model| - std.zig.fmtId(@tagName(model)) - else - std.zig.fmtId(@tagName(std.builtin.WasiExecModel.command)); - try buffer.writer().print( - \\pub const wasi_exec_model = std.builtin.WasiExecModel.{}; - \\ - , .{wasi_exec_model_fmt}); + if (comp.getTarget().os.tag == .wasi) { + const wasi_exec_model_fmt = std.zig.fmtId(@tagName(comp.bin_file.options.wasi_exec_model)); + try buffer.writer().print( + \\pub const wasi_exec_model = std.builtin.WasiExecModel.{}; + \\ + , .{wasi_exec_model_fmt}); + } if (comp.bin_file.options.is_test) { try buffer.appendSlice( diff --git a/src/link.zig b/src/link.zig index 0072ff2107d4..9ffd28079be2 100644 --- a/src/link.zig +++ b/src/link.zig @@ -119,7 +119,7 @@ pub const Options = struct { libc_installation: ?*const LibCInstallation, /// WASI-only. Type of WASI execution model ("command" or "reactor"). - wasi_exec_model: ?std.builtin.WasiExecModel, + wasi_exec_model: std.builtin.WasiExecModel = undefined, pub fn effectiveOutputMode(options: Options) std.builtin.OutputMode { return if (options.use_lld) .Obj else options.output_mode; diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index 36c554b3252c..b0c95f95b879 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -682,8 +682,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { // before corrupting globals. See https://github.com/ziglang/zig/issues/4496 try argv.append("--stack-first"); - if (self.base.options.wasi_exec_model) |exec_model| blk: { - if (exec_model != .reactor) break :blk; + if (self.base.options.wasi_exec_model == .reactor) { // Reactor execution model does not have _start so lld doesn't look for it. try argv.append("--no-entry"); // Make sure "_initialize" is exported even if this is pure Zig WASI reactor @@ -718,7 +717,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { if (self.base.options.link_libc) { try argv.append(try comp.get_libc_crt_file( arena, - wasi_libc.execModelCRTFileFullName(self.base.options.wasi_exec_model), + wasi_libc.execModelCrtFileFullName(self.base.options.wasi_exec_model), )); try argv.append(try comp.get_libc_crt_file(arena, "libc.a")); } diff --git a/src/main.zig b/src/main.zig index c66a3b1f70ea..c2f34e113283 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1067,12 +1067,9 @@ fn buildOutputType( { try clang_argv.append(arg); } else if (mem.startsWith(u8, arg, "-mexec-model=")) { - const value = arg["-mexec-model=".len..]; - if (std.mem.eql(u8, value, "reactor")) { - wasi_exec_model = .reactor; - } else if (std.mem.eql(u8, value, "command")) { - wasi_exec_model = .command; - } + wasi_exec_model = std.meta.stringToEnum(std.builtin.WasiExecModel, arg["-mexec-model=".len..]) orelse { + fatal("expected [command|reactor] for -mexec-mode=[value], found '{s}'", .{arg["-mexec-model=".len..]}); + }; } else { fatal("unrecognized parameter: '{s}'", .{arg}); } @@ -1279,11 +1276,9 @@ fn buildOutputType( .nostdlibinc => want_native_include_dirs = false, .strip => strip = true, .exec_model => { - if (std.mem.eql(u8, it.only_arg, "reactor")) { - wasi_exec_model = .reactor; - } else if (std.mem.eql(u8, it.only_arg, "command")) { - wasi_exec_model = .command; - } + wasi_exec_model = std.meta.stringToEnum(std.builtin.WasiExecModel, it.only_arg) orelse { + fatal("expected [command|reactor] for -mexec-mode=[value], found '{s}'", .{it.only_arg}); + }; }, } } diff --git a/src/wasi_libc.zig b/src/wasi_libc.zig index 0877446b9d95..a5c7ccb95402 100644 --- a/src/wasi_libc.zig +++ b/src/wasi_libc.zig @@ -45,19 +45,15 @@ pub fn emulatedLibCRFileLibName(crt_file: CRTFile) []const u8 { }; } -pub fn getExecModelCRTFIle(wasi_exec_model: ?std.builtin.WasiExecModel) CRTFile { - return if (wasi_exec_model) |model| - switch (model) { - .reactor => CRTFile.crt1_reactor_o, - .command => CRTFile.crt1_command_o, - } - else - CRTFile.crt1_o; +pub fn getExecModelCrtFIle(wasi_exec_model: std.builtin.WasiExecModel) CRTFile { + return switch (wasi_exec_model) { + .reactor => CRTFile.crt1_reactor_o, + .command => CRTFile.crt1_command_o, + }; } -pub fn execModelCRTFileFullName(wasi_exec_model: ?std.builtin.WasiExecModel) []const u8 { - return switch (getExecModelCRTFIle(wasi_exec_model)) { - .crt1_o => "crt1.o", +pub fn execModelCrtFileFullName(wasi_exec_model: std.builtin.WasiExecModel) []const u8 { + return switch (getExecModelCrtFIle(wasi_exec_model)) { .crt1_reactor_o => "crt1-reactor.o", .crt1_command_o => "crt1-command.o", else => unreachable,