authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-19 22:28:07-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-19 22:28:07-07:00
log2dea37545046e1a7e472e52c8c110ee6f32842d7
tree4fd0545ed62acb08f84554e5da441a23022ca170
parent29cce62a259384e7daffe8fa68a0edf34377c6b3

std.Build.Compile: handle modules sans root source files

Uses the new `-M[name][=src]` CLI syntax to omit the source when the module does not have a zig root source file. Only some kinds of link objects imply that this should happen.

1 files changed, 17 insertions(+), 7 deletions(-)

lib/std/Build/Step/Compile.zig+17-7
......@@ -1220,15 +1220,18 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
12201220 }
12211221 }
12221222
1223 // The CLI assumes if it sees a --mod argument that it is a zig
1224 // compilation unit. If there is no root source file, then this
1225 // is not a zig compilation unit - it is perhaps a set of
1226 // linker objects, or C source files instead.
1227 // In such case, there will be only one module, so we can leave
1228 // off the naming here.
1223 // When the CLI sees a -M argument, it determines whether it
1224 // implies the existence of a Zig compilation unit based on
1225 // whether there is a root source file. If there is no root
1226 // source file, then this is not a zig compilation unit - it is
1227 // perhaps a set of linker objects, or C source files instead.
1228 // Linker objects are added to the CLI globally, while C source
1229 // files must have a module parent.
12291230 if (module.root_source_file) |lp| {
12301231 const src = lp.getPath2(module.owner, step);
1231 try zig_args.appendSlice(&.{ "--mod", module_cli_name, src });
1232 try zig_args.append(b.fmt("-M{s}={s}", .{ module_cli_name, src }));
1233 } else if (moduleNeedsCliArg(module)) {
1234 try zig_args.append(b.fmt("-M{s}", .{module_cli_name}));
12321235 }
12331236 }
12341237 }
......@@ -1850,3 +1853,10 @@ pub fn rootModuleTarget(c: *Compile) std.Target {
18501853 // The root module is always given a target, so we know this to be non-null.
18511854 return c.root_module.resolved_target.?.result;
18521855}
1856
1857fn moduleNeedsCliArg(mod: *const Module) bool {
1858 return for (mod.link_objects.items) |o| switch (o) {
1859 .c_source_file, .c_source_files, .assembly_file, .win32_resource_file => break true,
1860 else => continue,
1861 } else false;
1862}