Skip to content

Commit

Permalink
review: exec-model non-optional in link option, style improvements.
Browse files Browse the repository at this point in the history
also drop the support for the old crt1.o in favor of crt1-command.o

Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
  • Loading branch information
mathetake committed Jun 30, 2021
1 parent 6769659 commit 85b5503
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 38 deletions.
5 changes: 3 additions & 2 deletions lib/std/start.zig
Expand Up @@ -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
Expand Down Expand Up @@ -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 });
}
Expand Down
22 changes: 12 additions & 10 deletions src/Compilation.zig
Expand Up @@ -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().
Expand Down Expand Up @@ -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.* = .{
Expand Down Expand Up @@ -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 },
});
}
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/link.zig
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions src/link/Wasm.zig
Expand Up @@ -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
Expand Down Expand Up @@ -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"));
}
Expand Down
17 changes: 6 additions & 11 deletions src/main.zig
Expand Up @@ -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});
}
Expand Down Expand Up @@ -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});
};
},
}
}
Expand Down
18 changes: 7 additions & 11 deletions src/wasi_libc.zig
Expand Up @@ -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,
Expand Down

0 comments on commit 85b5503

Please sign in to comment.