authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-12-15 18:09:38+00:00
committergravatar for bratishkaerik@landless-city.netEric Joldasov <bratishkaerik@landless-city.net> 2024-12-18 01:48:55+05:00
log93af1de0502d8041debf96e3579af04c8f7f8e38
treead5bf263efbf76c065d3fd74b25e53968075a9d1
parent6168b8ef86e2ca262585387d12166123842773d1
signaturelock-open Commit is signed but in an unrecognized format.

build.zig: migrate from deprecated std.Build APIs


1 files changed, 86 insertions(+), 74 deletions(-)

build.zig+86-74
...@@ -50,10 +50,12 @@ pub fn build(b: *std.Build) !void {...@@ -50,10 +50,12 @@ pub fn build(b: *std.Build) !void {
5050
51 const autodoc_test = b.addObject(.{51 const autodoc_test = b.addObject(.{
52 .name = "std",52 .name = "std",
53 .root_source_file = b.path("lib/std/std.zig"),
54 .target = target,
55 .zig_lib_dir = b.path("lib"),53 .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 }),
57 });59 });
58 const install_std_docs = b.addInstallDirectory(.{60 const install_std_docs = b.addInstallDirectory(.{
59 .source_dir = autodoc_test.getEmittedDocs(),61 .source_dir = autodoc_test.getEmittedDocs(),
...@@ -234,7 +236,7 @@ pub fn build(b: *std.Build) !void {...@@ -234,7 +236,7 @@ pub fn build(b: *std.Build) !void {
234 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);236 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
236 if (link_libc) {238 if (link_libc) {
237 exe.linkLibC();239 exe.root_module.link_libc = true;
238 }240 }
239241
240 const is_debug = optimize == .Debug;242 const is_debug = optimize == .Debug;
...@@ -330,15 +332,15 @@ pub fn build(b: *std.Build) !void {...@@ -330,15 +332,15 @@ pub fn build(b: *std.Build) !void {
330 try addCmakeCfgOptionsToExe(b, cfg, exe, use_zig_libcxx);332 try addCmakeCfgOptionsToExe(b, cfg, exe, use_zig_libcxx);
331 } else {333 } else {
332 // Here we are -Denable-llvm but no cmake integration.334 // Here we are -Denable-llvm but no cmake integration.
333 try addStaticLlvmOptionsToExe(exe);335 try addStaticLlvmOptionsToModule(exe.root_module);
334 }336 }
335 if (target.result.os.tag == .windows) {337 if (target.result.os.tag == .windows) {
336 // LLVM depends on networking as of version 18.338 // 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");341 exe.root_module.linkSystemLibrary("version", .{});
340 exe.linkSystemLibrary("uuid");342 exe.root_module.linkSystemLibrary("uuid", .{});
341 exe.linkSystemLibrary("ole32");343 exe.root_module.linkSystemLibrary("ole32", .{});
342 }344 }
343 }345 }
344346
...@@ -364,16 +366,16 @@ pub fn build(b: *std.Build) !void {...@@ -364,16 +366,16 @@ pub fn build(b: *std.Build) !void {
364 else366 else
365 &[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined" };367 &[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined" };
366368
367 exe.addIncludePath(.{ .cwd_relative = tracy_path });369 exe.root_module.addIncludePath(.{ .cwd_relative = tracy_path });
368 exe.addCSourceFile(.{ .file = .{ .cwd_relative = client_cpp }, .flags = tracy_c_flags });370 exe.root_module.addCSourceFile(.{ .file = .{ .cwd_relative = client_cpp }, .flags = tracy_c_flags });
369 if (!enable_llvm) {371 if (!enable_llvm) {
370 exe.root_module.linkSystemLibrary("c++", .{ .use_pkg_config = .no });372 exe.root_module.linkSystemLibrary("c++", .{ .use_pkg_config = .no });
371 }373 }
372 exe.linkLibC();374 exe.root_module.link_libc = true;
373375
374 if (target.result.os.tag == .windows) {376 if (target.result.os.tag == .windows) {
375 exe.linkSystemLibrary("dbghelp");377 exe.root_module.linkSystemLibrary("dbghelp", .{});
376 exe.linkSystemLibrary("ws2_32");378 exe.root_module.linkSystemLibrary("ws2_32", .{});
377 }379 }
378 }380 }
379381
...@@ -559,8 +561,10 @@ pub fn build(b: *std.Build) !void {...@@ -559,8 +561,10 @@ pub fn build(b: *std.Build) !void {
559 if (opt_mingw_src_path) |mingw_src_path| {561 if (opt_mingw_src_path) |mingw_src_path| {
560 const update_mingw_exe = b.addExecutable(.{562 const update_mingw_exe = b.addExecutable(.{
561 .name = "update_mingw",563 .name = "update_mingw",
562 .target = b.graph.host,564 .root_module = b.createModule(.{
563 .root_source_file = b.path("tools/update_mingw.zig"),565 .target = b.graph.host,
566 .root_source_file = b.path("tools/update_mingw.zig"),
567 }),
564 });568 });
565 const update_mingw_run = b.addRunArtifact(update_mingw_exe);569 const update_mingw_run = b.addRunArtifact(update_mingw_exe);
566 update_mingw_run.addDirectoryArg(b.path("lib"));570 update_mingw_run.addDirectoryArg(b.path("lib"));
...@@ -636,12 +640,10 @@ const AddCompilerStepOptions = struct {...@@ -636,12 +640,10 @@ const AddCompilerStepOptions = struct {
636};640};
637641
638fn addCompilerStep(b: *std.Build, options: AddCompilerStepOptions) *std.Build.Step.Compile {642fn addCompilerStep(b: *std.Build, options: AddCompilerStepOptions) *std.Build.Step.Compile {
639 const exe = b.addExecutable(.{643 const compiler_mod = b.createModule(.{
640 .name = "zig",
641 .root_source_file = b.path("src/main.zig"),644 .root_source_file = b.path("src/main.zig"),
642 .target = options.target,645 .target = options.target,
643 .optimize = options.optimize,646 .optimize = options.optimize,
644 .max_rss = 7_800_000_000,
645 .strip = options.strip,647 .strip = options.strip,
646 .sanitize_thread = options.sanitize_thread,648 .sanitize_thread = options.sanitize_thread,
647 .single_threaded = options.single_threaded,649 .single_threaded = options.single_threaded,
...@@ -663,26 +665,28 @@ fn addCompilerStep(b: *std.Build, options: AddCompilerStepOptions) *std.Build.St...@@ -663,26 +665,28 @@ fn addCompilerStep(b: *std.Build, options: AddCompilerStepOptions) *std.Build.St
663 .loongarch32, .loongarch64 => .medium,665 .loongarch32, .loongarch64 => .medium,
664 else => .default,666 else => .default,
665 },667 },
668 .valgrind = options.valgrind,
666 });669 });
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(.{
671 .root_source_file = b.path("lib/compiler/aro/aro.zig"),672 .root_source_file = b.path("lib/compiler/aro/aro.zig"),
672 });673 });
673674
674 const aro_translate_c_module = b.createModule(.{675 const aro_translate_c_mod = b.createModule(.{
675 .root_source_file = b.path("lib/compiler/aro_translate_c.zig"),676 .root_source_file = b.path("lib/compiler/aro_translate_c.zig"),
676 .imports = &.{
677 .{
678 .name = "aro",
679 .module = aro_module,
680 },
681 },
682 });677 });
683678
684 exe.root_module.addImport("aro", aro_module);679 aro_translate_c_mod.addImport("aro", aro_mod);
685 exe.root_module.addImport("aro_translate_c", aro_translate_c_module);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
686 return exe;690 return exe;
687}691}
688692
...@@ -707,11 +711,15 @@ fn addCmakeCfgOptionsToExe(...@@ -707,11 +711,15 @@ fn addCmakeCfgOptionsToExe(
707 exe: *std.Build.Step.Compile,711 exe: *std.Build.Step.Compile,
708 use_zig_libcxx: bool,712 use_zig_libcxx: bool,
709) !void {713) !void {
710 if (exe.rootModuleTarget().isDarwin()) {714 const mod = exe.root_module;
715 const target = mod.resolved_target.?.result;
716
717 if (target.isDarwin()) {
711 // useful for package maintainers718 // useful for package maintainers
712 exe.headerpad_max_install_names = true;719 exe.headerpad_max_install_names = true;
713 }720 }
714 exe.addObjectFile(.{ .cwd_relative = b.pathJoin(&[_][]const u8{721
722 mod.addObjectFile(.{ .cwd_relative = b.pathJoin(&.{
715 cfg.cmake_binary_dir,723 cfg.cmake_binary_dir,
716 "zigcpp",724 "zigcpp",
717 b.fmt("{s}{s}{s}", .{725 b.fmt("{s}{s}{s}", .{
...@@ -721,38 +729,38 @@ fn addCmakeCfgOptionsToExe(...@@ -721,38 +729,38 @@ fn addCmakeCfgOptionsToExe(
721 }),729 }),
722 }) });730 }) });
723 assert(cfg.lld_include_dir.len != 0);731 assert(cfg.lld_include_dir.len != 0);
724 exe.addIncludePath(.{ .cwd_relative = cfg.lld_include_dir });732 mod.addIncludePath(.{ .cwd_relative = cfg.lld_include_dir });
725 exe.addIncludePath(.{ .cwd_relative = cfg.llvm_include_dir });733 mod.addIncludePath(.{ .cwd_relative = cfg.llvm_include_dir });
726 exe.addLibraryPath(.{ .cwd_relative = cfg.llvm_lib_dir });734 mod.addLibraryPath(.{ .cwd_relative = cfg.llvm_lib_dir });
727 addCMakeLibraryList(exe, cfg.clang_libraries);735 addCMakeLibraryList(mod, cfg.clang_libraries);
728 addCMakeLibraryList(exe, cfg.lld_libraries);736 addCMakeLibraryList(mod, cfg.lld_libraries);
729 addCMakeLibraryList(exe, cfg.llvm_libraries);737 addCMakeLibraryList(mod, cfg.llvm_libraries);
730738
731 if (use_zig_libcxx) {739 if (use_zig_libcxx) {
732 exe.linkLibCpp();740 mod.link_libcpp = true;
733 } else {741 } else {
734 // System -lc++ must be used because in this code path we are attempting to link742 // System -lc++ must be used because in this code path we are attempting to link
735 // against system-provided LLVM, Clang, LLD.743 // against system-provided LLVM, Clang, LLD.
736 const need_cpp_includes = true;744 const need_cpp_includes = true;
737 const static = cfg.llvm_linkage == .static;745 const static = cfg.llvm_linkage == .static;
738 const lib_suffix = if (static) exe.rootModuleTarget().staticLibSuffix()[1..] else exe.rootModuleTarget().dynamicLibSuffix()[1..];746 const lib_suffix = if (static) target.staticLibSuffix()[1..] else target.dynamicLibSuffix()[1..];
739 switch (exe.rootModuleTarget().os.tag) {747 switch (target.os.tag) {
740 .linux => {748 .linux => {
741 // First we try to link against the detected libcxx name. If that doesn't work, we fall749 // First we try to link against the detected libcxx name. If that doesn't work, we fall
742 // back to -lc++ and cross our fingers.750 // back to -lc++ and cross our fingers.
743 addCxxKnownPath(b, cfg, exe, b.fmt("lib{s}.{s}", .{ cfg.system_libcxx, lib_suffix }), "", need_cpp_includes) catch |err| switch (err) {751 addCxxKnownPath(b, cfg, exe, b.fmt("lib{s}.{s}", .{ cfg.system_libcxx, lib_suffix }), "", need_cpp_includes) catch |err| switch (err) {
744 error.RequiredLibraryNotFound => {752 error.RequiredLibraryNotFound => {
745 exe.linkLibCpp();753 mod.link_libcpp = true;
746 },754 },
747 else => |e| return e,755 else => |e| return e,
748 };756 };
749 exe.linkSystemLibrary("unwind");757 mod.linkSystemLibrary("unwind", .{});
750 },758 },
751 .ios, .macos, .watchos, .tvos, .visionos => {759 .ios, .macos, .watchos, .tvos, .visionos => {
752 exe.linkLibCpp();760 mod.link_libcpp = true;
753 },761 },
754 .windows => {762 .windows => {
755 if (exe.rootModuleTarget().abi != .msvc) exe.linkLibCpp();763 if (target.abi != .msvc) mod.link_libcpp = true;
756 },764 },
757 .freebsd => {765 .freebsd => {
758 if (static) {766 if (static) {
...@@ -786,46 +794,46 @@ fn addCmakeCfgOptionsToExe(...@@ -786,46 +794,46 @@ fn addCmakeCfgOptionsToExe(
786 }794 }
787795
788 if (cfg.dia_guids_lib.len != 0) {796 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 });
790 }798 }
791}799}
792800
793fn addStaticLlvmOptionsToExe(exe: *std.Build.Step.Compile) !void {801fn addStaticLlvmOptionsToModule(mod: *std.Build.Module) !void {
794 // Adds the Zig C++ sources which both stage1 and stage2 need.802 // Adds the Zig C++ sources which both stage1 and stage2 need.
795 //803 //
796 // We need this because otherwise zig_clang_cc1_main.cpp ends up pulling804 // We need this because otherwise zig_clang_cc1_main.cpp ends up pulling
797 // in a dependency on llvm::cfg::Update<llvm::BasicBlock*>::dump() which is805 // in a dependency on llvm::cfg::Update<llvm::BasicBlock*>::dump() which is
798 // unavailable when LLVM is compiled in Release mode.806 // unavailable when LLVM is compiled in Release mode.
799 const zig_cpp_cflags = exe_cflags ++ [_][]const u8{"-DNDEBUG=1"};807 const zig_cpp_cflags = exe_cflags ++ [_][]const u8{"-DNDEBUG=1"};
800 exe.addCSourceFiles(.{808 mod.addCSourceFiles(.{
801 .files = &zig_cpp_sources,809 .files = &zig_cpp_sources,
802 .flags = &zig_cpp_cflags,810 .flags = &zig_cpp_cflags,
803 });811 });
804812
805 for (clang_libs) |lib_name| {813 for (clang_libs) |lib_name| {
806 exe.linkSystemLibrary(lib_name);814 mod.linkSystemLibrary(lib_name, .{});
807 }815 }
808816
809 for (lld_libs) |lib_name| {817 for (lld_libs) |lib_name| {
810 exe.linkSystemLibrary(lib_name);818 mod.linkSystemLibrary(lib_name, .{});
811 }819 }
812820
813 for (llvm_libs) |lib_name| {821 for (llvm_libs) |lib_name| {
814 exe.linkSystemLibrary(lib_name);822 mod.linkSystemLibrary(lib_name, .{});
815 }823 }
816824
817 exe.linkSystemLibrary("z");825 mod.linkSystemLibrary("z", .{});
818 exe.linkSystemLibrary("zstd");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) {
821 // This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries.829 // This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries.
822 exe.linkSystemLibrary("c++");830 mod.linkSystemLibrary("c++", .{});
823 }831 }
824832
825 if (exe.rootModuleTarget().os.tag == .windows) {833 if (mod.resolved_target.?.result.os.tag == .windows) {
826 exe.linkSystemLibrary("version");834 mod.linkSystemLibrary("version", .{});
827 exe.linkSystemLibrary("uuid");835 mod.linkSystemLibrary("uuid", .{});
828 exe.linkSystemLibrary("ole32");836 mod.linkSystemLibrary("ole32", .{});
829 }837 }
830}838}
831839
...@@ -862,29 +870,29 @@ fn addCxxKnownPath(...@@ -862,29 +870,29 @@ fn addCxxKnownPath(
862 // but libc++ may very well be one, so force all inputs to be checked when passing870 // but libc++ may very well be one, so force all inputs to be checked when passing
863 // an explicit path to libc++.871 // an explicit path to libc++.
864 exe.allow_so_scripts = true;872 exe.allow_so_scripts = true;
865 exe.addObjectFile(.{ .cwd_relative = path_unpadded });873 exe.root_module.addObjectFile(.{ .cwd_relative = path_unpadded });
866874
867 // TODO a way to integrate with system c++ include files here875 // TODO a way to integrate with system c++ include files here
868 // c++ -E -Wp,-v -xc++ /dev/null876 // c++ -E -Wp,-v -xc++ /dev/null
869 if (need_cpp_includes) {877 if (need_cpp_includes) {
870 // I used these temporarily for testing something but we obviously need a878 // I used these temporarily for testing something but we obviously need a
871 // more general purpose solution here.879 // more general purpose solution here.
872 //exe.addIncludePath("/nix/store/2lr0fc0ak8rwj0k8n3shcyz1hz63wzma-gcc-11.3.0/include/c++/11.3.0");880 //exe.root_module.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");881 //exe.root_module.addIncludePath("/nix/store/2lr0fc0ak8rwj0k8n3shcyz1hz63wzma-gcc-11.3.0/include/c++/11.3.0/x86_64-unknown-linux-gnu");
874 }882 }
875}883}
876884
877fn addCMakeLibraryList(exe: *std.Build.Step.Compile, list: []const u8) void {885fn addCMakeLibraryList(mod: *std.Build.Module, list: []const u8) void {
878 var it = mem.tokenizeScalar(u8, list, ';');886 var it = mem.tokenizeScalar(u8, list, ';');
879 while (it.next()) |lib| {887 while (it.next()) |lib| {
880 if (mem.startsWith(u8, lib, "-l")) {888 if (mem.startsWith(u8, lib, "-l")) {
881 exe.linkSystemLibrary(lib["-l".len..]);889 mod.linkSystemLibrary(lib["-l".len..], .{});
882 } else if (exe.rootModuleTarget().os.tag == .windows and890 } else if (mod.resolved_target.?.result.os.tag == .windows and
883 mem.endsWith(u8, lib, ".lib") and !fs.path.isAbsolute(lib))891 mem.endsWith(u8, lib, ".lib") and !fs.path.isAbsolute(lib))
884 {892 {
885 exe.linkSystemLibrary(lib[0 .. lib.len - ".lib".len]);893 mod.linkSystemLibrary(lib[0 .. lib.len - ".lib".len], .{});
886 } else {894 } else {
887 exe.addObjectFile(.{ .cwd_relative = lib });895 mod.addObjectFile(.{ .cwd_relative = lib });
888 }896 }
889 }897 }
890}898}
...@@ -1306,9 +1314,11 @@ const llvm_libs = [_][]const u8{...@@ -1306,9 +1314,11 @@ const llvm_libs = [_][]const u8{
1306fn generateLangRef(b: *std.Build) std.Build.LazyPath {1314fn generateLangRef(b: *std.Build) std.Build.LazyPath {
1307 const doctest_exe = b.addExecutable(.{1315 const doctest_exe = b.addExecutable(.{
1308 .name = "doctest",1316 .name = "doctest",
1309 .root_source_file = b.path("tools/doctest.zig"),1317 .root_module = b.createModule(.{
1310 .target = b.graph.host,1318 .root_source_file = b.path("tools/doctest.zig"),
1311 .optimize = .Debug,1319 .target = b.graph.host,
1320 .optimize = .Debug,
1321 }),
1312 });1322 });
13131323
1314 var dir = b.build_root.handle.openDir("doc/langref", .{ .iterate = true }) catch |err| {1324 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 {...@@ -1343,9 +1353,11 @@ fn generateLangRef(b: *std.Build) std.Build.LazyPath {
13431353
1344 const docgen_exe = b.addExecutable(.{1354 const docgen_exe = b.addExecutable(.{
1345 .name = "docgen",1355 .name = "docgen",
1346 .root_source_file = b.path("tools/docgen.zig"),1356 .root_module = b.createModule(.{
1347 .target = b.graph.host,1357 .root_source_file = b.path("tools/docgen.zig"),
1348 .optimize = .Debug,1358 .target = b.graph.host,
1359 .optimize = .Debug,
1360 }),
1349 });1361 });
13501362
1351 const docgen_cmd = b.addRunArtifact(docgen_exe);1363 const docgen_cmd = b.addRunArtifact(docgen_exe);