Skip to content

Commit a2585d8

Browse files
committed
cli: consolidate entry point flags
1 parent f98425e commit a2585d8

File tree

5 files changed

+36
-37
lines changed

5 files changed

+36
-37
lines changed

lib/std/Build/Step/Compile.zig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ initial_memory: ?u64 = null,
6464
max_memory: ?u64 = null,
6565
shared_memory: bool = false,
6666
global_base: ?u64 = null,
67-
/// For WebAssembly only. Tells the linker to not output an entry point.
67+
/// Tells the linker to not output an entry point.
6868
no_entry: ?bool = null,
6969
c_std: std.Build.CStd,
7070
/// Set via options; intended to be read-only after that.
@@ -1421,8 +1421,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
14211421
}
14221422

14231423
if (self.entry_symbol_name) |entry| {
1424-
try zig_args.append("--entry");
1425-
try zig_args.append(entry);
1424+
try zig_args.append(try std.fmt.allocPrint(b.allocator, "-fentry={s}", .{entry}));
14261425
}
14271426

14281427
{

src/Compilation.zig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,6 @@ pub const InitOptions = struct {
643643
linker_import_symbols: bool = false,
644644
linker_import_table: bool = false,
645645
linker_export_table: bool = false,
646-
linker_no_entry: bool = false,
647646
linker_initial_memory: ?u64 = null,
648647
linker_max_memory: ?u64 = null,
649648
linker_shared_memory: bool = false,
@@ -1615,7 +1614,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
16151614
.import_symbols = options.linker_import_symbols,
16161615
.import_table = options.linker_import_table,
16171616
.export_table = options.linker_export_table,
1618-
.no_entry = options.linker_no_entry,
16191617
.initial_memory = options.linker_initial_memory,
16201618
.max_memory = options.linker_max_memory,
16211619
.shared_memory = options.linker_shared_memory,
@@ -2579,7 +2577,6 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
25792577
man.hash.addOptional(comp.bin_file.options.max_memory);
25802578
man.hash.add(comp.bin_file.options.shared_memory);
25812579
man.hash.addOptional(comp.bin_file.options.global_base);
2582-
man.hash.add(comp.bin_file.options.no_entry);
25832580

25842581
// Mach-O specific stuff
25852582
man.hash.addListOfBytes(comp.bin_file.options.framework_dirs);

src/link.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ pub const Options = struct {
166166
export_table: bool,
167167
initial_memory: ?u64,
168168
max_memory: ?u64,
169-
no_entry: bool,
170169
shared_memory: bool,
171170
export_symbol_names: []const []const u8,
172171
global_base: ?u64,

src/link/Wasm.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2817,11 +2817,11 @@ fn setupExports(wasm: *Wasm) !void {
28172817
}
28182818

28192819
fn setupStart(wasm: *Wasm) !void {
2820-
if (wasm.base.options.no_entry) return;
2821-
const entry_name = wasm.base.options.entry orelse "_start";
2820+
// do not export entry point if user set none or no default was set.
2821+
const entry_name = wasm.base.options.entry orelse return;
28222822

28232823
const symbol_loc = wasm.findGlobalSymbol(entry_name) orelse {
2824-
log.err("Entry symbol '{s}' missing", .{entry_name});
2824+
log.err("Entry symbol '{s}' missing, use '-fno-entry' to suppress", .{entry_name});
28252825
return error.MissingSymbol;
28262826
};
28272827

@@ -4544,7 +4544,7 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
45444544
try argv.append("--allow-undefined");
45454545
}
45464546

4547-
if (wasm.base.options.no_entry) {
4547+
if (wasm.base.options.entry == null) {
45484548
try argv.append("--no-entry");
45494549
}
45504550

src/main.zig

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,9 @@ const usage_build_generic =
509509
\\ --dynamic-linker [path] Set the dynamic interpreter path (usually ld.so)
510510
\\ --sysroot [path] Set the system root directory (usually /)
511511
\\ --version [ver] Dynamic library semver
512-
\\ --entry [name] Set the entrypoint symbol name
512+
\\ -fentry Enable entry point with default symbol name
513+
\\ -fentry=[name] Override the entry point symbol name
514+
\\ -fno-entry Do not output any entry point
513515
\\ --force_undefined [name] Specify the symbol must be defined for the link to succeed
514516
\\ -fsoname[=name] Override the default SONAME value
515517
\\ -fno-soname Disable emitting a SONAME
@@ -577,8 +579,6 @@ const usage_build_generic =
577579
\\ --shared-memory (WebAssembly) use shared linear memory
578580
\\ --global-base=[addr] (WebAssembly) where to start to place global data
579581
\\ --export=[value] (WebAssembly) Force a symbol to be exported
580-
\\ -fentry (WebAssembly) Force output an entry point
581-
\\ -fno-entry (WebAssembly) Do not output any entry point
582582
\\
583583
\\Test Options:
584584
\\ --test-filter [text] Skip tests that do not match filter
@@ -837,7 +837,7 @@ fn buildOutputType(
837837
var linker_import_symbols: bool = false;
838838
var linker_import_table: bool = false;
839839
var linker_export_table: bool = false;
840-
var linker_no_entry: ?bool = null;
840+
var linker_force_entry: ?bool = null;
841841
var linker_initial_memory: ?u64 = null;
842842
var linker_max_memory: ?u64 = null;
843843
var linker_shared_memory: bool = false;
@@ -1074,8 +1074,8 @@ fn buildOutputType(
10741074
subsystem = try parseSubSystem(args_iter.nextOrFatal());
10751075
} else if (mem.eql(u8, arg, "-O")) {
10761076
optimize_mode_string = args_iter.nextOrFatal();
1077-
} else if (mem.eql(u8, arg, "--entry")) {
1078-
entry = args_iter.nextOrFatal();
1077+
} else if (mem.startsWith(u8, arg, "-fentry=")) {
1078+
entry = arg["-fentry=".len..];
10791079
} else if (mem.eql(u8, arg, "--force_undefined")) {
10801080
try force_undefined_symbols.put(gpa, args_iter.nextOrFatal(), {});
10811081
} else if (mem.eql(u8, arg, "--stack")) {
@@ -1507,9 +1507,9 @@ fn buildOutputType(
15071507
} else if (mem.eql(u8, arg, "--import-memory")) {
15081508
linker_import_memory = true;
15091509
} else if (mem.eql(u8, arg, "-fentry")) {
1510-
linker_no_entry = false;
1510+
linker_force_entry = true;
15111511
} else if (mem.eql(u8, arg, "-fno-entry")) {
1512-
linker_no_entry = true;
1512+
linker_force_entry = false;
15131513
} else if (mem.eql(u8, arg, "--export-memory")) {
15141514
linker_export_memory = true;
15151515
} else if (mem.eql(u8, arg, "--import-symbols")) {
@@ -2142,7 +2142,7 @@ fn buildOutputType(
21422142
} else if (mem.eql(u8, arg, "--export-table")) {
21432143
linker_export_table = true;
21442144
} else if (mem.eql(u8, arg, "--no-entry")) {
2145-
linker_no_entry = true;
2145+
linker_force_entry = false;
21462146
} else if (mem.eql(u8, arg, "--initial-memory")) {
21472147
const next_arg = linker_args_it.nextOrFatal();
21482148
linker_initial_memory = std.fmt.parseUnsigned(u32, eatIntPrefix(next_arg, 16), 16) catch |err| {
@@ -2605,6 +2605,23 @@ fn buildOutputType(
26052605
link_libcpp = true;
26062606
}
26072607

2608+
const linker_default_entry = linker_force_entry orelse true;
2609+
if (!linker_default_entry and entry != null) {
2610+
fatal("combination of '-fentry=[name]' and `-fno-entry` are incompatible", .{});
2611+
}
2612+
if (linker_default_entry and entry == null and output_mode == .Exe) {
2613+
entry = switch (target_info.target.ofmt) {
2614+
.coff => "wWinMainCRTStartup",
2615+
.macho => "_main",
2616+
.elf, .plan9 => "_start",
2617+
.wasm => if (wasi_exec_model != null and wasi_exec_model.? == .reactor)
2618+
"_initialize"
2619+
else
2620+
"_start",
2621+
else => |tag| fatal("No default entry point available for output format {s}", .{@tagName(tag)}),
2622+
};
2623+
}
2624+
26082625
if (target_info.target.ofmt == .coff) {
26092626
// Now that we know the target supports resources,
26102627
// we can add the res files as link objects.
@@ -2637,23 +2654,11 @@ fn buildOutputType(
26372654
linker_export_memory = false;
26382655
}
26392656
}
2640-
if (wasi_exec_model) |model| {
2641-
if (model == .reactor) {
2642-
if (linker_no_entry != null and !linker_no_entry.?) {
2643-
fatal("WASI exucution model 'reactor' incompatible with flag '-fentry'. Reactor execution model has no entry point", .{});
2657+
if (wasi_exec_model != null and wasi_exec_model.? == .reactor) {
2658+
if (entry) |entry_name| {
2659+
if (!mem.eql(u8, "_initialize", entry_name)) {
2660+
fatal("the entry symbol of the reactor model must be '_initialize', but found '{s}'", .{entry_name});
26442661
}
2645-
if (entry) |entry_name| {
2646-
if (!mem.eql(u8, "_initialize", entry_name)) {
2647-
fatal("the entry symbol of the reactor model must be '_initialize', but found '{s}'", .{entry_name});
2648-
}
2649-
} else {
2650-
entry = "_initialize";
2651-
}
2652-
}
2653-
}
2654-
if (linker_no_entry) |no_entry| {
2655-
if (no_entry and entry != null) {
2656-
fatal("combination of '--entry' and `-fno-entry` are incompatible", .{});
26572662
}
26582663
}
26592664
if (linker_shared_memory) {
@@ -3503,7 +3508,6 @@ fn buildOutputType(
35033508
.linker_import_symbols = linker_import_symbols,
35043509
.linker_import_table = linker_import_table,
35053510
.linker_export_table = linker_export_table,
3506-
.linker_no_entry = linker_no_entry orelse false,
35073511
.linker_initial_memory = linker_initial_memory,
35083512
.linker_max_memory = linker_max_memory,
35093513
.linker_shared_memory = linker_shared_memory,

0 commit comments

Comments
 (0)