diff --git a/lib/std/Build.zig b/lib/std/Build.zig index 9d8c7455a3..12b6baae73 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -719,7 +719,7 @@ pub const ExecutableOptions = struct { pub fn addExecutable(b: *Build, options: ExecutableOptions) *Step.Compile { return Step.Compile.create(b, .{ .name = options.name, - .root_module = .{ + .root_module = b.createModule(.{ .root_source_file = options.root_source_file, .target = options.target, .optimize = options.optimize, @@ -732,7 +732,7 @@ pub fn addExecutable(b: *Build, options: ExecutableOptions) *Step.Compile { .sanitize_thread = options.sanitize_thread, .error_tracing = options.error_tracing, .code_model = options.code_model, - }, + }), .version = options.version, .kind = .exe, .linkage = options.linkage, @@ -769,7 +769,7 @@ pub const ObjectOptions = struct { pub fn addObject(b: *Build, options: ObjectOptions) *Step.Compile { return Step.Compile.create(b, .{ .name = options.name, - .root_module = .{ + .root_module = b.createModule(.{ .root_source_file = options.root_source_file, .target = options.target, .optimize = options.optimize, @@ -782,7 +782,7 @@ pub fn addObject(b: *Build, options: ObjectOptions) *Step.Compile { .sanitize_thread = options.sanitize_thread, .error_tracing = options.error_tracing, .code_model = options.code_model, - }, + }), .kind = .obj, .max_rss = options.max_rss, .use_llvm = options.use_llvm, @@ -823,7 +823,7 @@ pub const SharedLibraryOptions = struct { pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile { return Step.Compile.create(b, .{ .name = options.name, - .root_module = .{ + .root_module = b.createModule(.{ .target = options.target, .optimize = options.optimize, .root_source_file = options.root_source_file, @@ -836,7 +836,7 @@ pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile .sanitize_thread = options.sanitize_thread, .error_tracing = options.error_tracing, .code_model = options.code_model, - }, + }), .kind = .lib, .linkage = .dynamic, .version = options.version, @@ -874,7 +874,7 @@ pub const StaticLibraryOptions = struct { pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile { return Step.Compile.create(b, .{ .name = options.name, - .root_module = .{ + .root_module = b.createModule(.{ .target = options.target, .optimize = options.optimize, .root_source_file = options.root_source_file, @@ -887,7 +887,7 @@ pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile .sanitize_thread = options.sanitize_thread, .error_tracing = options.error_tracing, .code_model = options.code_model, - }, + }), .kind = .lib, .linkage = .static, .version = options.version, @@ -935,7 +935,7 @@ pub fn addTest(b: *Build, options: TestOptions) *Step.Compile { return Step.Compile.create(b, .{ .name = options.name, .kind = .@"test", - .root_module = .{ + .root_module = b.createModule(.{ .root_source_file = options.root_source_file, .target = options.target orelse b.graph.host, .optimize = options.optimize, @@ -948,7 +948,7 @@ pub fn addTest(b: *Build, options: TestOptions) *Step.Compile { .omit_frame_pointer = options.omit_frame_pointer, .sanitize_thread = options.sanitize_thread, .error_tracing = options.error_tracing, - }, + }), .max_rss = options.max_rss, .filters = if (options.filter != null and options.filters.len > 0) filters: { const filters = b.allocator.alloc([]const u8, 1 + options.filters.len) catch @panic("OOM"); @@ -978,10 +978,10 @@ pub fn addAssembly(b: *Build, options: AssemblyOptions) *Step.Compile { const obj_step = Step.Compile.create(b, .{ .name = options.name, .kind = .obj, - .root_module = .{ + .root_module = b.createModule(.{ .target = options.target, .optimize = options.optimize, - }, + }), .max_rss = options.max_rss, .zig_lib_dir = options.zig_lib_dir, }); diff --git a/lib/std/Build/Module.zig b/lib/std/Build/Module.zig index cc7c5a65c0..629d90dd0f 100644 --- a/lib/std/Build/Module.zig +++ b/lib/std/Build/Module.zig @@ -242,45 +242,57 @@ pub const Import = struct { module: *Module, }; -pub fn init(m: *Module, owner: *std.Build, options: CreateOptions, compile: ?*Step.Compile) void { +pub fn init( + m: *Module, + owner: *std.Build, + value: union(enum) { options: CreateOptions, existing: *const Module }, + compile: ?*Step.Compile, +) void { const allocator = owner.allocator; - m.* = .{ - .owner = owner, - .depending_steps = .{}, - .root_source_file = if (options.root_source_file) |lp| lp.dupe(owner) else null, - .import_table = .{}, - .resolved_target = options.target, - .optimize = options.optimize, - .link_libc = options.link_libc, - .link_libcpp = options.link_libcpp, - .dwarf_format = options.dwarf_format, - .c_macros = .{}, - .include_dirs = .{}, - .lib_paths = .{}, - .rpaths = .{}, - .frameworks = .{}, - .link_objects = .{}, - .strip = options.strip, - .unwind_tables = options.unwind_tables, - .single_threaded = options.single_threaded, - .stack_protector = options.stack_protector, - .stack_check = options.stack_check, - .sanitize_c = options.sanitize_c, - .sanitize_thread = options.sanitize_thread, - .fuzz = options.fuzz, - .code_model = options.code_model, - .valgrind = options.valgrind, - .pic = options.pic, - .red_zone = options.red_zone, - .omit_frame_pointer = options.omit_frame_pointer, - .error_tracing = options.error_tracing, - .export_symbol_names = &.{}, - }; + switch (value) { + .options => |options| { + m.* = .{ + .owner = owner, + .depending_steps = .{}, + .root_source_file = if (options.root_source_file) |lp| lp.dupe(owner) else null, + .import_table = .{}, + .resolved_target = options.target, + .optimize = options.optimize, + .link_libc = options.link_libc, + .link_libcpp = options.link_libcpp, + .dwarf_format = options.dwarf_format, + .c_macros = .{}, + .include_dirs = .{}, + .lib_paths = .{}, + .rpaths = .{}, + .frameworks = .{}, + .link_objects = .{}, + .strip = options.strip, + .unwind_tables = options.unwind_tables, + .single_threaded = options.single_threaded, + .stack_protector = options.stack_protector, + .stack_check = options.stack_check, + .sanitize_c = options.sanitize_c, + .sanitize_thread = options.sanitize_thread, + .fuzz = options.fuzz, + .code_model = options.code_model, + .valgrind = options.valgrind, + .pic = options.pic, + .red_zone = options.red_zone, + .omit_frame_pointer = options.omit_frame_pointer, + .error_tracing = options.error_tracing, + .export_symbol_names = &.{}, + }; - m.import_table.ensureUnusedCapacity(allocator, options.imports.len) catch @panic("OOM"); - for (options.imports) |dep| { - m.import_table.putAssumeCapacity(dep.name, dep.module); + m.import_table.ensureUnusedCapacity(allocator, options.imports.len) catch @panic("OOM"); + for (options.imports) |dep| { + m.import_table.putAssumeCapacity(dep.name, dep.module); + } + }, + .existing => |existing| { + m.* = existing.*; + }, } if (compile) |c| { @@ -294,7 +306,7 @@ pub fn init(m: *Module, owner: *std.Build, options: CreateOptions, compile: ?*St pub fn create(owner: *std.Build, options: CreateOptions) *Module { const m = owner.allocator.create(Module) catch @panic("OOM"); - m.init(owner, options, null); + m.init(owner, .{ .options = options }, null); return m; } diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index cdb0ddeb0c..f34ce7785b 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -262,7 +262,7 @@ pub const Entry = union(enum) { pub const Options = struct { name: []const u8, - root_module: Module.CreateOptions, + root_module: *Module, kind: Kind, linkage: ?std.builtin.LinkMode = null, version: ?std.SemanticVersion = null, @@ -359,7 +359,8 @@ pub fn create(owner: *std.Build, options: Options) *Compile { else owner.fmt("{s} ", .{name}); - const resolved_target = options.root_module.target.?; + const resolved_target = options.root_module.resolved_target orelse + @panic("the root Module of a Compile step must be created with a known 'target' field"); const target = resolved_target.result; const step_name = owner.fmt("{s} {s}{s} {s}", .{ @@ -431,10 +432,8 @@ pub fn create(owner: *std.Build, options: Options) *Compile { .zig_process = null, }; - - const root_module = owner.allocator.create(Module) catch @panic("OOM"); - root_module.init(owner, options.root_module, compile); - compile.root_module = root_module; + options.root_module.init(owner, .{ .existing = options.root_module }, compile); + compile.root_module = options.root_module; if (options.zig_lib_dir) |lp| { compile.zig_lib_dir = lp.dupe(compile.step.owner); diff --git a/test/link/link.zig b/test/link/link.zig index 4537846353..6885aa9321 100644 --- a/test/link/link.zig +++ b/test/link/link.zig @@ -70,7 +70,7 @@ fn addCompileStep( ) *Compile { const compile_step = Compile.create(b, .{ .name = overlay.name, - .root_module = .{ + .root_module = b.createModule(.{ .target = base.target, .optimize = base.optimize, .root_source_file = rsf: { @@ -80,7 +80,7 @@ fn addCompileStep( }, .pic = overlay.pic, .strip = if (base.strip) |s| s else overlay.strip, - }, + }), .use_llvm = base.use_llvm, .use_lld = base.use_lld, .kind = switch (kind) {