diff options
| author | Luuk de Gram <luuk@degram.dev> | 2023-08-30 16:59:48 +0200 |
|---|---|---|
| committer | Luuk de Gram <luuk@degram.dev> | 2023-11-03 12:48:52 +0100 |
| commit | 1cb7a01b25349c42dbe207b68f9e19b92890b3d2 (patch) | |
| tree | 602589918ad620910c8462711492e1ffff6c2826 /lib/std | |
| parent | 94cee4fb27a433824c2540dc37375dc14befdf47 (diff) | |
| download | zig-1cb7a01b25349c42dbe207b68f9e19b92890b3d2.tar.gz zig-1cb7a01b25349c42dbe207b68f9e19b92890b3d2.zip | |
wasm-linker: implement `-fno-entry` flag
This adds support for the `-fno-entry` and `-fentry` flags respectively, for
zig build-{exe/lib} and the build system. For `zig cc` we use the `--no-entry`
flag to be compatible with clang and existing tooling.
In `start.zig` we now make the main function optional when the target is
WebAssembly, as to allow for the build-exe command in combination with
`-fno-entry`.
When the execution model is set, and is set to 'reactor', we now verify
when an entry name is given it matches what is expected. When no entry
point is given, we set it to `_initialize` by default. This means the user
will also be met with an error when they use the reactor model, but did
not provide the correct function.
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/Build/Step/Compile.zig | 3 | ||||
| -rw-r--r-- | lib/std/start.zig | 8 |
2 files changed, 9 insertions, 2 deletions
diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index 9ede31c917..18977ac472 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -64,6 +64,8 @@ initial_memory: ?u64 = null, max_memory: ?u64 = null, shared_memory: bool = false, global_base: ?u64 = null, +/// For WebAssembly only. Tells the linker to not output an entry point. +no_entry: ?bool = null, c_std: std.Build.CStd, /// Set via options; intended to be read-only after that. zig_lib_dir: ?LazyPath, @@ -1851,6 +1853,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { if (self.global_base) |global_base| { try zig_args.append(b.fmt("--global-base={d}", .{global_base})); } + try addFlag(&zig_args, "entry", self.no_entry); if (self.code_model != .default) { try zig_args.append("-mcmodel"); diff --git a/lib/std/start.zig b/lib/std/start.zig index 7ed59a4675..29fdb3b031 100644 --- a/lib/std/start.zig +++ b/lib/std/start.zig @@ -82,11 +82,15 @@ comptime { .reactor => "_initialize", .command => "_start", }; - if (!@hasDecl(root, wasm_start_sym)) { + if (!@hasDecl(root, wasm_start_sym) and @hasDecl(root, "main")) { + // Only call main when defined. For WebAssembly it's allowed to pass `-fno-entry` in which + // case it's not required to provide an entrypoint such as main. @export(wasi_start, .{ .name = wasm_start_sym }); } } else if (native_arch.isWasm() and native_os == .freestanding) { - if (!@hasDecl(root, start_sym_name)) @export(wasm_freestanding_start, .{ .name = start_sym_name }); + // Only call main when defined. For WebAssembly it's allowed to pass `-fno-entry` in which + // case it's not required to provide an entrypoint such as main. + if (!@hasDecl(root, start_sym_name) and @hasDecl(root, "main")) @export(wasm_freestanding_start, .{ .name = start_sym_name }); } else if (native_os != .other and native_os != .freestanding) { if (!@hasDecl(root, start_sym_name)) @export(_start, .{ .name = start_sym_name }); } |
