Skip to content

Commit 12d64c4

Browse files
authored
Merge pull request #20388 from BratishkaErik/std.Build/accept-root-module-2
std.Build: add new functions to create artifacts/Step.Compile from existing module
2 parents debba65 + afc77f0 commit 12d64c4

File tree

95 files changed

+1775
-1370
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+1775
-1370
lines changed

build.zig

Lines changed: 86 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ pub fn build(b: *std.Build) !void {
5050

5151
const autodoc_test = b.addObject(.{
5252
.name = "std",
53-
.root_source_file = b.path("lib/std/std.zig"),
54-
.target = target,
5553
.zig_lib_dir = b.path("lib"),
56-
.optimize = .Debug,
54+
.root_module = b.createModule(.{
55+
.root_source_file = b.path("lib/std/std.zig"),
56+
.target = target,
57+
.optimize = .Debug,
58+
}),
5759
});
5860
const install_std_docs = b.addInstallDirectory(.{
5961
.source_dir = autodoc_test.getEmittedDocs(),
@@ -234,7 +236,7 @@ pub fn build(b: *std.Build) !void {
234236
exe_options.addOption(DevEnv, "dev", b.option(DevEnv, "dev", "Build a compiler with a reduced feature set for development of specific features") orelse if (only_c) .bootstrap else .full);
235237

236238
if (link_libc) {
237-
exe.linkLibC();
239+
exe.root_module.link_libc = true;
238240
}
239241

240242
const is_debug = optimize == .Debug;
@@ -330,15 +332,15 @@ pub fn build(b: *std.Build) !void {
330332
try addCmakeCfgOptionsToExe(b, cfg, exe, use_zig_libcxx);
331333
} else {
332334
// Here we are -Denable-llvm but no cmake integration.
333-
try addStaticLlvmOptionsToExe(exe);
335+
try addStaticLlvmOptionsToModule(exe.root_module);
334336
}
335337
if (target.result.os.tag == .windows) {
336338
// LLVM depends on networking as of version 18.
337-
exe.linkSystemLibrary("ws2_32");
339+
exe.root_module.linkSystemLibrary("ws2_32", .{});
338340

339-
exe.linkSystemLibrary("version");
340-
exe.linkSystemLibrary("uuid");
341-
exe.linkSystemLibrary("ole32");
341+
exe.root_module.linkSystemLibrary("version", .{});
342+
exe.root_module.linkSystemLibrary("uuid", .{});
343+
exe.root_module.linkSystemLibrary("ole32", .{});
342344
}
343345
}
344346

@@ -364,16 +366,16 @@ pub fn build(b: *std.Build) !void {
364366
else
365367
&[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined" };
366368

367-
exe.addIncludePath(.{ .cwd_relative = tracy_path });
368-
exe.addCSourceFile(.{ .file = .{ .cwd_relative = client_cpp }, .flags = tracy_c_flags });
369+
exe.root_module.addIncludePath(.{ .cwd_relative = tracy_path });
370+
exe.root_module.addCSourceFile(.{ .file = .{ .cwd_relative = client_cpp }, .flags = tracy_c_flags });
369371
if (!enable_llvm) {
370372
exe.root_module.linkSystemLibrary("c++", .{ .use_pkg_config = .no });
371373
}
372-
exe.linkLibC();
374+
exe.root_module.link_libc = true;
373375

374376
if (target.result.os.tag == .windows) {
375-
exe.linkSystemLibrary("dbghelp");
376-
exe.linkSystemLibrary("ws2_32");
377+
exe.root_module.linkSystemLibrary("dbghelp", .{});
378+
exe.root_module.linkSystemLibrary("ws2_32", .{});
377379
}
378380
}
379381

@@ -559,8 +561,10 @@ pub fn build(b: *std.Build) !void {
559561
if (opt_mingw_src_path) |mingw_src_path| {
560562
const update_mingw_exe = b.addExecutable(.{
561563
.name = "update_mingw",
562-
.target = b.graph.host,
563-
.root_source_file = b.path("tools/update_mingw.zig"),
564+
.root_module = b.createModule(.{
565+
.target = b.graph.host,
566+
.root_source_file = b.path("tools/update_mingw.zig"),
567+
}),
564568
});
565569
const update_mingw_run = b.addRunArtifact(update_mingw_exe);
566570
update_mingw_run.addDirectoryArg(b.path("lib"));
@@ -636,12 +640,10 @@ const AddCompilerStepOptions = struct {
636640
};
637641

638642
fn addCompilerStep(b: *std.Build, options: AddCompilerStepOptions) *std.Build.Step.Compile {
639-
const exe = b.addExecutable(.{
640-
.name = "zig",
643+
const compiler_mod = b.createModule(.{
641644
.root_source_file = b.path("src/main.zig"),
642645
.target = options.target,
643646
.optimize = options.optimize,
644-
.max_rss = 7_800_000_000,
645647
.strip = options.strip,
646648
.sanitize_thread = options.sanitize_thread,
647649
.single_threaded = options.single_threaded,
@@ -663,26 +665,28 @@ fn addCompilerStep(b: *std.Build, options: AddCompilerStepOptions) *std.Build.St
663665
.loongarch32, .loongarch64 => .medium,
664666
else => .default,
665667
},
668+
.valgrind = options.valgrind,
666669
});
667-
exe.root_module.valgrind = options.valgrind;
668-
exe.stack_size = stack_size;
669670

670-
const aro_module = b.createModule(.{
671+
const aro_mod = b.createModule(.{
671672
.root_source_file = b.path("lib/compiler/aro/aro.zig"),
672673
});
673674

674-
const aro_translate_c_module = b.createModule(.{
675+
const aro_translate_c_mod = b.createModule(.{
675676
.root_source_file = b.path("lib/compiler/aro_translate_c.zig"),
676-
.imports = &.{
677-
.{
678-
.name = "aro",
679-
.module = aro_module,
680-
},
681-
},
682677
});
683678

684-
exe.root_module.addImport("aro", aro_module);
685-
exe.root_module.addImport("aro_translate_c", aro_translate_c_module);
679+
aro_translate_c_mod.addImport("aro", aro_mod);
680+
compiler_mod.addImport("aro", aro_mod);
681+
compiler_mod.addImport("aro_translate_c", aro_translate_c_mod);
682+
683+
const exe = b.addExecutable(.{
684+
.name = "zig",
685+
.max_rss = 7_800_000_000,
686+
.root_module = compiler_mod,
687+
});
688+
exe.stack_size = stack_size;
689+
686690
return exe;
687691
}
688692

@@ -707,11 +711,15 @@ fn addCmakeCfgOptionsToExe(
707711
exe: *std.Build.Step.Compile,
708712
use_zig_libcxx: bool,
709713
) !void {
710-
if (exe.rootModuleTarget().isDarwin()) {
714+
const mod = exe.root_module;
715+
const target = mod.resolved_target.?.result;
716+
717+
if (target.isDarwin()) {
711718
// useful for package maintainers
712719
exe.headerpad_max_install_names = true;
713720
}
714-
exe.addObjectFile(.{ .cwd_relative = b.pathJoin(&[_][]const u8{
721+
722+
mod.addObjectFile(.{ .cwd_relative = b.pathJoin(&.{
715723
cfg.cmake_binary_dir,
716724
"zigcpp",
717725
b.fmt("{s}{s}{s}", .{
@@ -721,38 +729,38 @@ fn addCmakeCfgOptionsToExe(
721729
}),
722730
}) });
723731
assert(cfg.lld_include_dir.len != 0);
724-
exe.addIncludePath(.{ .cwd_relative = cfg.lld_include_dir });
725-
exe.addIncludePath(.{ .cwd_relative = cfg.llvm_include_dir });
726-
exe.addLibraryPath(.{ .cwd_relative = cfg.llvm_lib_dir });
727-
addCMakeLibraryList(exe, cfg.clang_libraries);
728-
addCMakeLibraryList(exe, cfg.lld_libraries);
729-
addCMakeLibraryList(exe, cfg.llvm_libraries);
732+
mod.addIncludePath(.{ .cwd_relative = cfg.lld_include_dir });
733+
mod.addIncludePath(.{ .cwd_relative = cfg.llvm_include_dir });
734+
mod.addLibraryPath(.{ .cwd_relative = cfg.llvm_lib_dir });
735+
addCMakeLibraryList(mod, cfg.clang_libraries);
736+
addCMakeLibraryList(mod, cfg.lld_libraries);
737+
addCMakeLibraryList(mod, cfg.llvm_libraries);
730738

731739
if (use_zig_libcxx) {
732-
exe.linkLibCpp();
740+
mod.link_libcpp = true;
733741
} else {
734742
// System -lc++ must be used because in this code path we are attempting to link
735743
// against system-provided LLVM, Clang, LLD.
736744
const need_cpp_includes = true;
737745
const static = cfg.llvm_linkage == .static;
738-
const lib_suffix = if (static) exe.rootModuleTarget().staticLibSuffix()[1..] else exe.rootModuleTarget().dynamicLibSuffix()[1..];
739-
switch (exe.rootModuleTarget().os.tag) {
746+
const lib_suffix = if (static) target.staticLibSuffix()[1..] else target.dynamicLibSuffix()[1..];
747+
switch (target.os.tag) {
740748
.linux => {
741749
// First we try to link against the detected libcxx name. If that doesn't work, we fall
742750
// back to -lc++ and cross our fingers.
743751
addCxxKnownPath(b, cfg, exe, b.fmt("lib{s}.{s}", .{ cfg.system_libcxx, lib_suffix }), "", need_cpp_includes) catch |err| switch (err) {
744752
error.RequiredLibraryNotFound => {
745-
exe.linkLibCpp();
753+
mod.link_libcpp = true;
746754
},
747755
else => |e| return e,
748756
};
749-
exe.linkSystemLibrary("unwind");
757+
mod.linkSystemLibrary("unwind", .{});
750758
},
751759
.ios, .macos, .watchos, .tvos, .visionos => {
752-
exe.linkLibCpp();
760+
mod.link_libcpp = true;
753761
},
754762
.windows => {
755-
if (exe.rootModuleTarget().abi != .msvc) exe.linkLibCpp();
763+
if (target.abi != .msvc) mod.link_libcpp = true;
756764
},
757765
.freebsd => {
758766
if (static) {
@@ -786,46 +794,46 @@ fn addCmakeCfgOptionsToExe(
786794
}
787795

788796
if (cfg.dia_guids_lib.len != 0) {
789-
exe.addObjectFile(.{ .cwd_relative = cfg.dia_guids_lib });
797+
mod.addObjectFile(.{ .cwd_relative = cfg.dia_guids_lib });
790798
}
791799
}
792800

793-
fn addStaticLlvmOptionsToExe(exe: *std.Build.Step.Compile) !void {
801+
fn addStaticLlvmOptionsToModule(mod: *std.Build.Module) !void {
794802
// Adds the Zig C++ sources which both stage1 and stage2 need.
795803
//
796804
// We need this because otherwise zig_clang_cc1_main.cpp ends up pulling
797805
// in a dependency on llvm::cfg::Update<llvm::BasicBlock*>::dump() which is
798806
// unavailable when LLVM is compiled in Release mode.
799807
const zig_cpp_cflags = exe_cflags ++ [_][]const u8{"-DNDEBUG=1"};
800-
exe.addCSourceFiles(.{
808+
mod.addCSourceFiles(.{
801809
.files = &zig_cpp_sources,
802810
.flags = &zig_cpp_cflags,
803811
});
804812

805813
for (clang_libs) |lib_name| {
806-
exe.linkSystemLibrary(lib_name);
814+
mod.linkSystemLibrary(lib_name, .{});
807815
}
808816

809817
for (lld_libs) |lib_name| {
810-
exe.linkSystemLibrary(lib_name);
818+
mod.linkSystemLibrary(lib_name, .{});
811819
}
812820

813821
for (llvm_libs) |lib_name| {
814-
exe.linkSystemLibrary(lib_name);
822+
mod.linkSystemLibrary(lib_name, .{});
815823
}
816824

817-
exe.linkSystemLibrary("z");
818-
exe.linkSystemLibrary("zstd");
825+
mod.linkSystemLibrary("z", .{});
826+
mod.linkSystemLibrary("zstd", .{});
819827

820-
if (exe.rootModuleTarget().os.tag != .windows or exe.rootModuleTarget().abi != .msvc) {
828+
if (mod.resolved_target.?.result.os.tag != .windows or mod.resolved_target.?.result.abi != .msvc) {
821829
// This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries.
822-
exe.linkSystemLibrary("c++");
830+
mod.linkSystemLibrary("c++", .{});
823831
}
824832

825-
if (exe.rootModuleTarget().os.tag == .windows) {
826-
exe.linkSystemLibrary("version");
827-
exe.linkSystemLibrary("uuid");
828-
exe.linkSystemLibrary("ole32");
833+
if (mod.resolved_target.?.result.os.tag == .windows) {
834+
mod.linkSystemLibrary("version", .{});
835+
mod.linkSystemLibrary("uuid", .{});
836+
mod.linkSystemLibrary("ole32", .{});
829837
}
830838
}
831839

@@ -862,29 +870,29 @@ fn addCxxKnownPath(
862870
// but libc++ may very well be one, so force all inputs to be checked when passing
863871
// an explicit path to libc++.
864872
exe.allow_so_scripts = true;
865-
exe.addObjectFile(.{ .cwd_relative = path_unpadded });
873+
exe.root_module.addObjectFile(.{ .cwd_relative = path_unpadded });
866874

867875
// TODO a way to integrate with system c++ include files here
868876
// c++ -E -Wp,-v -xc++ /dev/null
869877
if (need_cpp_includes) {
870878
// I used these temporarily for testing something but we obviously need a
871879
// more general purpose solution here.
872-
//exe.addIncludePath("/nix/store/2lr0fc0ak8rwj0k8n3shcyz1hz63wzma-gcc-11.3.0/include/c++/11.3.0");
873-
//exe.addIncludePath("/nix/store/2lr0fc0ak8rwj0k8n3shcyz1hz63wzma-gcc-11.3.0/include/c++/11.3.0/x86_64-unknown-linux-gnu");
880+
//exe.root_module.addIncludePath("/nix/store/2lr0fc0ak8rwj0k8n3shcyz1hz63wzma-gcc-11.3.0/include/c++/11.3.0");
881+
//exe.root_module.addIncludePath("/nix/store/2lr0fc0ak8rwj0k8n3shcyz1hz63wzma-gcc-11.3.0/include/c++/11.3.0/x86_64-unknown-linux-gnu");
874882
}
875883
}
876884

877-
fn addCMakeLibraryList(exe: *std.Build.Step.Compile, list: []const u8) void {
885+
fn addCMakeLibraryList(mod: *std.Build.Module, list: []const u8) void {
878886
var it = mem.tokenizeScalar(u8, list, ';');
879887
while (it.next()) |lib| {
880888
if (mem.startsWith(u8, lib, "-l")) {
881-
exe.linkSystemLibrary(lib["-l".len..]);
882-
} else if (exe.rootModuleTarget().os.tag == .windows and
889+
mod.linkSystemLibrary(lib["-l".len..], .{});
890+
} else if (mod.resolved_target.?.result.os.tag == .windows and
883891
mem.endsWith(u8, lib, ".lib") and !fs.path.isAbsolute(lib))
884892
{
885-
exe.linkSystemLibrary(lib[0 .. lib.len - ".lib".len]);
893+
mod.linkSystemLibrary(lib[0 .. lib.len - ".lib".len], .{});
886894
} else {
887-
exe.addObjectFile(.{ .cwd_relative = lib });
895+
mod.addObjectFile(.{ .cwd_relative = lib });
888896
}
889897
}
890898
}
@@ -1306,9 +1314,11 @@ const llvm_libs = [_][]const u8{
13061314
fn generateLangRef(b: *std.Build) std.Build.LazyPath {
13071315
const doctest_exe = b.addExecutable(.{
13081316
.name = "doctest",
1309-
.root_source_file = b.path("tools/doctest.zig"),
1310-
.target = b.graph.host,
1311-
.optimize = .Debug,
1317+
.root_module = b.createModule(.{
1318+
.root_source_file = b.path("tools/doctest.zig"),
1319+
.target = b.graph.host,
1320+
.optimize = .Debug,
1321+
}),
13121322
});
13131323

13141324
var dir = b.build_root.handle.openDir("doc/langref", .{ .iterate = true }) catch |err| {
@@ -1343,9 +1353,11 @@ fn generateLangRef(b: *std.Build) std.Build.LazyPath {
13431353

13441354
const docgen_exe = b.addExecutable(.{
13451355
.name = "docgen",
1346-
.root_source_file = b.path("tools/docgen.zig"),
1347-
.target = b.graph.host,
1348-
.optimize = .Debug,
1356+
.root_module = b.createModule(.{
1357+
.root_source_file = b.path("tools/docgen.zig"),
1358+
.target = b.graph.host,
1359+
.optimize = .Debug,
1360+
}),
13491361
});
13501362

13511363
const docgen_cmd = b.addRunArtifact(docgen_exe);

0 commit comments

Comments
 (0)