authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-12-18 03:34:43+00:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-12-18 03:34:43+00:00
log12d64c456b7590b1486717232f77c6d3700813a6
treeca604dbab15963d63216650d77d76f14c6156e91
parentdebba652a3f5af76840517321d389a2ec98a88f4
parentafc77f0603395d9c1829a49081e198b0fa255abc
signaturebadge-check Signed by PGP key B5690EEEBB952194

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

95 files changed, 1775 insertions(+), 1370 deletions(-)

build.zig+86-74
......@@ -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
638642fn 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
793fn addStaticLlvmOptionsToExe(exe: *std.Build.Step.Compile) !void {
801fn 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
877fn addCMakeLibraryList(exe: *std.Build.Step.Compile, list: []const u8) void {
885fn 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{
13061314fn 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);
lib/compiler/build_runner.zig+78
......@@ -337,6 +337,7 @@ pub fn main() !void {
337337 var prog_node = main_progress_node.start("Configure", 0);
338338 defer prog_node.end();
339339 try builder.runBuild(root);
340 createModuleDependencies(builder) catch @panic("OOM");
340341 }
341342
342343 if (graph.needed_lazy_dependencies.entries.len != 0) {
......@@ -1440,3 +1441,80 @@ fn validateSystemLibraryOptions(b: *std.Build) void {
14401441 process.exit(1);
14411442 }
14421443}
1444
1445/// Starting from all top-level steps in `b`, traverses the entire step graph
1446/// and adds all step dependencies implied by module graphs.
1447fn createModuleDependencies(b: *std.Build) Allocator.Error!void {
1448 const arena = b.graph.arena;
1449
1450 var all_steps: std.AutoArrayHashMapUnmanaged(*Step, void) = .empty;
1451 var next_step_idx: usize = 0;
1452
1453 try all_steps.ensureUnusedCapacity(arena, b.top_level_steps.count());
1454 for (b.top_level_steps.values()) |tls| {
1455 all_steps.putAssumeCapacityNoClobber(&tls.step, {});
1456 }
1457
1458 while (next_step_idx < all_steps.count()) {
1459 const step = all_steps.keys()[next_step_idx];
1460 next_step_idx += 1;
1461
1462 // Set up any implied dependencies for this step. It's important that we do this first, so
1463 // that the loop below discovers steps implied by the module graph.
1464 try createModuleDependenciesForStep(step);
1465
1466 try all_steps.ensureUnusedCapacity(arena, step.dependencies.items.len);
1467 for (step.dependencies.items) |other_step| {
1468 all_steps.putAssumeCapacity(other_step, {});
1469 }
1470 }
1471}
1472
1473/// If the given `Step` is a `Step.Compile`, adds any dependencies for that step which
1474/// are implied by the module graph rooted at `step.cast(Step.Compile).?.root_module`.
1475fn createModuleDependenciesForStep(step: *Step) Allocator.Error!void {
1476 const root_module = if (step.cast(Step.Compile)) |cs| root: {
1477 break :root cs.root_module;
1478 } else return; // not a compile step so no module dependencies
1479
1480 // Starting from `root_module`, discover all modules in this graph.
1481 const modules = root_module.getGraph().modules;
1482
1483 // For each of those modules, set up the implied step dependencies.
1484 for (modules) |mod| {
1485 if (mod.root_source_file) |lp| lp.addStepDependencies(step);
1486 for (mod.include_dirs.items) |include_dir| switch (include_dir) {
1487 .path,
1488 .path_system,
1489 .path_after,
1490 .framework_path,
1491 .framework_path_system,
1492 => |lp| lp.addStepDependencies(step),
1493
1494 .other_step => |other| {
1495 other.getEmittedIncludeTree().addStepDependencies(step);
1496 step.dependOn(&other.step);
1497 },
1498
1499 .config_header_step => |other| step.dependOn(&other.step),
1500 };
1501 for (mod.lib_paths.items) |lp| lp.addStepDependencies(step);
1502 for (mod.rpaths.items) |rpath| switch (rpath) {
1503 .lazy_path => |lp| lp.addStepDependencies(step),
1504 .special => {},
1505 };
1506 for (mod.link_objects.items) |link_object| switch (link_object) {
1507 .static_path,
1508 .assembly_file,
1509 => |lp| lp.addStepDependencies(step),
1510 .other_step => |other| step.dependOn(&other.step),
1511 .system_lib => {},
1512 .c_source_file => |source| source.file.addStepDependencies(step),
1513 .c_source_files => |source_files| source_files.root.addStepDependencies(step),
1514 .win32_resource_file => |rc_source| {
1515 rc_source.file.addStepDependencies(step);
1516 for (rc_source.include_paths) |lp| lp.addStepDependencies(step);
1517 },
1518 };
1519 }
1520}
lib/init/build.zig+35-11
......@@ -15,8 +15,12 @@ pub fn build(b: *std.Build) void {
1515 // set a preferred release mode, allowing the user to decide how to optimize.
1616 const optimize = b.standardOptimizeOption(.{});
1717
18 const lib = b.addStaticLibrary(.{
19 .name = "$",
18 // This creates a "module", which represents a collection of source files alongside
19 // some compilation options, such as optimization mode and linked system libraries.
20 // Every executable or library we compile will be based on one or more modules.
21 const lib_mod = b.createModule(.{
22 // `root_source_file` is the Zig "entry point" of the module. If a module
23 // only contains e.g. external object files, you can make this `null`.
2024 // In this case the main source file is merely a path, however, in more
2125 // complicated build scripts, this could be a generated file.
2226 .root_source_file = b.path("src/root.zig"),
......@@ -24,16 +28,40 @@ pub fn build(b: *std.Build) void {
2428 .optimize = optimize,
2529 });
2630
31 // We will also create a module for our other entry point, 'main.zig'.
32 const exe_mod = b.createModule(.{
33 // `root_source_file` is the Zig "entry point" of the module. If a module
34 // only contains e.g. external object files, you can make this `null`.
35 // In this case the main source file is merely a path, however, in more
36 // complicated build scripts, this could be a generated file.
37 .root_source_file = b.path("src/main.zig"),
38 .target = target,
39 .optimize = optimize,
40 });
41
42 // Modules can depend on one another using the `std.Build.Module.addImport` function.
43 // This is what allows Zig source code to use `@import("foo")` where 'foo' is not a
44 // file path. In this case, we set up `exe_mod` to import `lib_mod`.
45 exe_mod.addImport("$_lib", lib_mod);
46
47 // Now, we will create a static library based on the module we created above.
48 // This creates a `std.Build.Step.Compile`, which is the build step responsible
49 // for actually invoking the compiler.
50 const lib = b.addStaticLibrary(.{
51 .name = "$",
52 .root_module = lib_mod,
53 });
54
2755 // This declares intent for the library to be installed into the standard
2856 // location when the user invokes the "install" step (the default step when
2957 // running `zig build`).
3058 b.installArtifact(lib);
3159
60 // This creates another `std.Build.Step.Compile`, but this one builds an executable
61 // rather than a static library.
3262 const exe = b.addExecutable(.{
3363 .name = "$",
34 .root_source_file = b.path("src/main.zig"),
35 .target = target,
36 .optimize = optimize,
64 .root_module = exe_mod,
3765 });
3866
3967 // This declares intent for the executable to be installed into the
......@@ -67,17 +95,13 @@ pub fn build(b: *std.Build) void {
6795 // Creates a step for unit testing. This only builds the test executable
6896 // but does not run it.
6997 const lib_unit_tests = b.addTest(.{
70 .root_source_file = b.path("src/root.zig"),
71 .target = target,
72 .optimize = optimize,
98 .root_module = lib_mod,
7399 });
74100
75101 const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
76102
77103 const exe_unit_tests = b.addTest(.{
78 .root_source_file = b.path("src/main.zig"),
79 .target = target,
80 .optimize = optimize,
104 .root_module = exe_mod,
81105 });
82106
83107 const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
lib/init/src/main.zig+9-1
......@@ -1,7 +1,6 @@
11//! By convention, main.zig is where your main function lives in the case that
22//! you are building an executable. If you are making a library, the convention
33//! is to delete this file and start with root.zig instead.
4const std = @import("std");
54
65pub fn main() !void {
76 // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
......@@ -26,6 +25,10 @@ test "simple test" {
2625 try std.testing.expectEqual(@as(i32, 42), list.pop());
2726}
2827
28test "use other module" {
29 try std.testing.expectEqual(@as(i32, 150), lib.add(100, 50));
30}
31
2932test "fuzz example" {
3033 const global = struct {
3134 fn testOne(input: []const u8) anyerror!void {
......@@ -35,3 +38,8 @@ test "fuzz example" {
3538 };
3639 try std.testing.fuzz(global.testOne, .{});
3740}
41
42const std = @import("std");
43
44/// This imports the separate module containing `root.zig`. Take a look in `build.zig` for details.
45const lib = @import("$_lib");
lib/init/src/root.zig+1-1
......@@ -4,7 +4,7 @@
44const std = @import("std");
55const testing = std.testing;
66
7export fn add(a: i32, b: i32) i32 {
7pub export fn add(a: i32, b: i32) i32 {
88 return a + b;
99}
1010
lib/std/Build.zig+180-87
......@@ -81,9 +81,6 @@ enable_wine: bool = false,
8181/// that contains the path `aarch64-linux-gnu/lib/ld-linux-aarch64.so.1`.
8282glibc_runtimes_dir: ?[]const u8 = null,
8383
84/// Deprecated. Use `b.graph.host`.
85host: ResolvedTarget,
86
8784dep_prefix: []const u8 = "",
8885
8986modules: std.StringArrayHashMap(*Module),
......@@ -301,7 +298,6 @@ pub fn create(
301298 },
302299 .install_path = undefined,
303300 .args = null,
304 .host = graph.host,
305301 .modules = .init(arena),
306302 .named_writefiles = .init(arena),
307303 .named_lazy_paths = .init(arena),
......@@ -393,7 +389,6 @@ fn createChildOnly(
393389 .enable_wasmtime = parent.enable_wasmtime,
394390 .enable_wine = parent.enable_wine,
395391 .glibc_runtimes_dir = parent.glibc_runtimes_dir,
396 .host = parent.host,
397392 .dep_prefix = parent.fmt("{s}{s}.", .{ parent.dep_prefix, dep_name }),
398393 .modules = .init(allocator),
399394 .named_writefiles = .init(allocator),
......@@ -692,24 +687,9 @@ pub fn addOptions(b: *Build) *Step.Options {
692687
693688pub const ExecutableOptions = struct {
694689 name: []const u8,
695 /// If you want the executable to run on the same computer as the one
696 /// building the package, pass the `host` field of the package's `Build`
697 /// instance.
698 target: ResolvedTarget,
699 root_source_file: ?LazyPath = null,
700690 version: ?std.SemanticVersion = null,
701 optimize: std.builtin.OptimizeMode = .Debug,
702 code_model: std.builtin.CodeModel = .default,
703691 linkage: ?std.builtin.LinkMode = null,
704692 max_rss: usize = 0,
705 link_libc: ?bool = null,
706 single_threaded: ?bool = null,
707 pic: ?bool = null,
708 strip: ?bool = null,
709 unwind_tables: ?std.builtin.UnwindTables = null,
710 omit_frame_pointer: ?bool = null,
711 sanitize_thread: ?bool = null,
712 error_tracing: ?bool = null,
713693 use_llvm: ?bool = null,
714694 use_lld: ?bool = null,
715695 zig_lib_dir: ?LazyPath = null,
......@@ -719,14 +699,47 @@ pub const ExecutableOptions = struct {
719699 /// Can be set regardless of target. The `.manifest` file will be ignored
720700 /// if the target object format does not support embedded manifests.
721701 win32_manifest: ?LazyPath = null,
702
703 /// Prefer populating this field (using e.g. `createModule`) instead of populating
704 /// the following fields (`root_source_file` etc). In a future release, those fields
705 /// will be removed, and this field will become non-optional.
706 root_module: ?*Module = null,
707
708 /// Deprecated; prefer populating `root_module`.
709 root_source_file: ?LazyPath = null,
710 /// Deprecated; prefer populating `root_module`.
711 target: ?ResolvedTarget = null,
712 /// Deprecated; prefer populating `root_module`.
713 optimize: std.builtin.OptimizeMode = .Debug,
714 /// Deprecated; prefer populating `root_module`.
715 code_model: std.builtin.CodeModel = .default,
716 /// Deprecated; prefer populating `root_module`.
717 link_libc: ?bool = null,
718 /// Deprecated; prefer populating `root_module`.
719 single_threaded: ?bool = null,
720 /// Deprecated; prefer populating `root_module`.
721 pic: ?bool = null,
722 /// Deprecated; prefer populating `root_module`.
723 strip: ?bool = null,
724 /// Deprecated; prefer populating `root_module`.
725 unwind_tables: ?std.builtin.UnwindTables = null,
726 /// Deprecated; prefer populating `root_module`.
727 omit_frame_pointer: ?bool = null,
728 /// Deprecated; prefer populating `root_module`.
729 sanitize_thread: ?bool = null,
730 /// Deprecated; prefer populating `root_module`.
731 error_tracing: ?bool = null,
722732};
723733
724734pub fn addExecutable(b: *Build, options: ExecutableOptions) *Step.Compile {
725 return Step.Compile.create(b, .{
735 if (options.root_module != null and options.target != null) {
736 @panic("`root_module` and `target` cannot both be populated");
737 }
738 return .create(b, .{
726739 .name = options.name,
727 .root_module = .{
740 .root_module = options.root_module orelse b.createModule(.{
728741 .root_source_file = options.root_source_file,
729 .target = options.target,
742 .target = options.target orelse @panic("`root_module` and `target` cannot both be null"),
730743 .optimize = options.optimize,
731744 .link_libc = options.link_libc,
732745 .single_threaded = options.single_threaded,
......@@ -737,7 +750,7 @@ pub fn addExecutable(b: *Build, options: ExecutableOptions) *Step.Compile {
737750 .sanitize_thread = options.sanitize_thread,
738751 .error_tracing = options.error_tracing,
739752 .code_model = options.code_model,
740 },
753 }),
741754 .version = options.version,
742755 .kind = .exe,
743756 .linkage = options.linkage,
......@@ -751,32 +764,51 @@ pub fn addExecutable(b: *Build, options: ExecutableOptions) *Step.Compile {
751764
752765pub const ObjectOptions = struct {
753766 name: []const u8,
767 max_rss: usize = 0,
768 use_llvm: ?bool = null,
769 use_lld: ?bool = null,
770 zig_lib_dir: ?LazyPath = null,
771
772 /// Prefer populating this field (using e.g. `createModule`) instead of populating
773 /// the following fields (`root_source_file` etc). In a future release, those fields
774 /// will be removed, and this field will become non-optional.
775 root_module: ?*Module = null,
776
777 /// Deprecated; prefer populating `root_module`.
754778 root_source_file: ?LazyPath = null,
755 /// To choose the same computer as the one building the package, pass the
756 /// `host` field of the package's `Build` instance.
757 target: ResolvedTarget,
779 /// Deprecated; prefer populating `root_module`.
780 target: ?ResolvedTarget = null,
781 /// Deprecated; prefer populating `root_module`.
782 optimize: std.builtin.OptimizeMode = .Debug,
783 /// Deprecated; prefer populating `root_module`.
758784 code_model: std.builtin.CodeModel = .default,
759 optimize: std.builtin.OptimizeMode,
760 max_rss: usize = 0,
785 /// Deprecated; prefer populating `root_module`.
761786 link_libc: ?bool = null,
787 /// Deprecated; prefer populating `root_module`.
762788 single_threaded: ?bool = null,
789 /// Deprecated; prefer populating `root_module`.
763790 pic: ?bool = null,
791 /// Deprecated; prefer populating `root_module`.
764792 strip: ?bool = null,
793 /// Deprecated; prefer populating `root_module`.
765794 unwind_tables: ?std.builtin.UnwindTables = null,
795 /// Deprecated; prefer populating `root_module`.
766796 omit_frame_pointer: ?bool = null,
797 /// Deprecated; prefer populating `root_module`.
767798 sanitize_thread: ?bool = null,
799 /// Deprecated; prefer populating `root_module`.
768800 error_tracing: ?bool = null,
769 use_llvm: ?bool = null,
770 use_lld: ?bool = null,
771 zig_lib_dir: ?LazyPath = null,
772801};
773802
774803pub fn addObject(b: *Build, options: ObjectOptions) *Step.Compile {
775 return Step.Compile.create(b, .{
804 if (options.root_module != null and options.target != null) {
805 @panic("`root_module` and `target` cannot both be populated");
806 }
807 return .create(b, .{
776808 .name = options.name,
777 .root_module = .{
809 .root_module = options.root_module orelse b.createModule(.{
778810 .root_source_file = options.root_source_file,
779 .target = options.target,
811 .target = options.target orelse @panic("`root_module` and `target` cannot both be null"),
780812 .optimize = options.optimize,
781813 .link_libc = options.link_libc,
782814 .single_threaded = options.single_threaded,
......@@ -787,7 +819,7 @@ pub fn addObject(b: *Build, options: ObjectOptions) *Step.Compile {
787819 .sanitize_thread = options.sanitize_thread,
788820 .error_tracing = options.error_tracing,
789821 .code_model = options.code_model,
790 },
822 }),
791823 .kind = .obj,
792824 .max_rss = options.max_rss,
793825 .use_llvm = options.use_llvm,
......@@ -798,22 +830,8 @@ pub fn addObject(b: *Build, options: ObjectOptions) *Step.Compile {
798830
799831pub const SharedLibraryOptions = struct {
800832 name: []const u8,
801 /// To choose the same computer as the one building the package, pass the
802 /// `host` field of the package's `Build` instance.
803 target: ResolvedTarget,
804 optimize: std.builtin.OptimizeMode,
805 code_model: std.builtin.CodeModel = .default,
806 root_source_file: ?LazyPath = null,
807833 version: ?std.SemanticVersion = null,
808834 max_rss: usize = 0,
809 link_libc: ?bool = null,
810 single_threaded: ?bool = null,
811 pic: ?bool = null,
812 strip: ?bool = null,
813 unwind_tables: ?std.builtin.UnwindTables = null,
814 omit_frame_pointer: ?bool = null,
815 sanitize_thread: ?bool = null,
816 error_tracing: ?bool = null,
817835 use_llvm: ?bool = null,
818836 use_lld: ?bool = null,
819837 zig_lib_dir: ?LazyPath = null,
......@@ -823,13 +841,46 @@ pub const SharedLibraryOptions = struct {
823841 /// Can be set regardless of target. The `.manifest` file will be ignored
824842 /// if the target object format does not support embedded manifests.
825843 win32_manifest: ?LazyPath = null,
844
845 /// Prefer populating this field (using e.g. `createModule`) instead of populating
846 /// the following fields (`root_source_file` etc). In a future release, those fields
847 /// will be removed, and this field will become non-optional.
848 root_module: ?*Module = null,
849
850 /// Deprecated; prefer populating `root_module`.
851 root_source_file: ?LazyPath = null,
852 /// Deprecated; prefer populating `root_module`.
853 target: ?ResolvedTarget = null,
854 /// Deprecated; prefer populating `root_module`.
855 optimize: std.builtin.OptimizeMode = .Debug,
856 /// Deprecated; prefer populating `root_module`.
857 code_model: std.builtin.CodeModel = .default,
858 /// Deprecated; prefer populating `root_module`.
859 link_libc: ?bool = null,
860 /// Deprecated; prefer populating `root_module`.
861 single_threaded: ?bool = null,
862 /// Deprecated; prefer populating `root_module`.
863 pic: ?bool = null,
864 /// Deprecated; prefer populating `root_module`.
865 strip: ?bool = null,
866 /// Deprecated; prefer populating `root_module`.
867 unwind_tables: ?std.builtin.UnwindTables = null,
868 /// Deprecated; prefer populating `root_module`.
869 omit_frame_pointer: ?bool = null,
870 /// Deprecated; prefer populating `root_module`.
871 sanitize_thread: ?bool = null,
872 /// Deprecated; prefer populating `root_module`.
873 error_tracing: ?bool = null,
826874};
827875
828876pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile {
829 return Step.Compile.create(b, .{
877 if (options.root_module != null and options.target != null) {
878 @panic("`root_module` and `target` cannot both be populated");
879 }
880 return .create(b, .{
830881 .name = options.name,
831 .root_module = .{
832 .target = options.target,
882 .root_module = options.root_module orelse b.createModule(.{
883 .target = options.target orelse @panic("`root_module` and `target` cannot both be null"),
833884 .optimize = options.optimize,
834885 .root_source_file = options.root_source_file,
835886 .link_libc = options.link_libc,
......@@ -841,7 +892,7 @@ pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile
841892 .sanitize_thread = options.sanitize_thread,
842893 .error_tracing = options.error_tracing,
843894 .code_model = options.code_model,
844 },
895 }),
845896 .kind = .lib,
846897 .linkage = .dynamic,
847898 .version = options.version,
......@@ -855,32 +906,51 @@ pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile
855906
856907pub const StaticLibraryOptions = struct {
857908 name: []const u8,
858 root_source_file: ?LazyPath = null,
859 /// To choose the same computer as the one building the package, pass the
860 /// `host` field of the package's `Build` instance.
861 target: ResolvedTarget,
862 optimize: std.builtin.OptimizeMode,
863 code_model: std.builtin.CodeModel = .default,
864909 version: ?std.SemanticVersion = null,
865910 max_rss: usize = 0,
911 use_llvm: ?bool = null,
912 use_lld: ?bool = null,
913 zig_lib_dir: ?LazyPath = null,
914
915 /// Prefer populating this field (using e.g. `createModule`) instead of populating
916 /// the following fields (`root_source_file` etc). In a future release, those fields
917 /// will be removed, and this field will become non-optional.
918 root_module: ?*Module = null,
919
920 /// Deprecated; prefer populating `root_module`.
921 root_source_file: ?LazyPath = null,
922 /// Deprecated; prefer populating `root_module`.
923 target: ?ResolvedTarget = null,
924 /// Deprecated; prefer populating `root_module`.
925 optimize: std.builtin.OptimizeMode = .Debug,
926 /// Deprecated; prefer populating `root_module`.
927 code_model: std.builtin.CodeModel = .default,
928 /// Deprecated; prefer populating `root_module`.
866929 link_libc: ?bool = null,
930 /// Deprecated; prefer populating `root_module`.
867931 single_threaded: ?bool = null,
932 /// Deprecated; prefer populating `root_module`.
868933 pic: ?bool = null,
934 /// Deprecated; prefer populating `root_module`.
869935 strip: ?bool = null,
936 /// Deprecated; prefer populating `root_module`.
870937 unwind_tables: ?std.builtin.UnwindTables = null,
938 /// Deprecated; prefer populating `root_module`.
871939 omit_frame_pointer: ?bool = null,
940 /// Deprecated; prefer populating `root_module`.
872941 sanitize_thread: ?bool = null,
942 /// Deprecated; prefer populating `root_module`.
873943 error_tracing: ?bool = null,
874 use_llvm: ?bool = null,
875 use_lld: ?bool = null,
876 zig_lib_dir: ?LazyPath = null,
877944};
878945
879946pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile {
880 return Step.Compile.create(b, .{
947 if (options.root_module != null and options.target != null) {
948 @panic("`root_module` and `target` cannot both be populated");
949 }
950 return .create(b, .{
881951 .name = options.name,
882 .root_module = .{
883 .target = options.target,
952 .root_module = options.root_module orelse b.createModule(.{
953 .target = options.target orelse @panic("`root_module` and `target` cannot both be null"),
884954 .optimize = options.optimize,
885955 .root_source_file = options.root_source_file,
886956 .link_libc = options.link_libc,
......@@ -892,7 +962,7 @@ pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile
892962 .sanitize_thread = options.sanitize_thread,
893963 .error_tracing = options.error_tracing,
894964 .code_model = options.code_model,
895 },
965 }),
896966 .kind = .lib,
897967 .linkage = .static,
898968 .version = options.version,
......@@ -905,27 +975,46 @@ pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile
905975
906976pub const TestOptions = struct {
907977 name: []const u8 = "test",
908 root_source_file: LazyPath,
909 target: ?ResolvedTarget = null,
910 optimize: std.builtin.OptimizeMode = .Debug,
911 version: ?std.SemanticVersion = null,
912978 max_rss: usize = 0,
913 /// deprecated: use `.filters = &.{filter}` instead of `.filter = filter`.
979 /// Deprecated; use `.filters = &.{filter}` instead of `.filter = filter`.
914980 filter: ?[]const u8 = null,
915981 filters: []const []const u8 = &.{},
916982 test_runner: ?LazyPath = null,
983 use_llvm: ?bool = null,
984 use_lld: ?bool = null,
985 zig_lib_dir: ?LazyPath = null,
986
987 /// Prefer populating this field (using e.g. `createModule`) instead of populating
988 /// the following fields (`root_source_file` etc). In a future release, those fields
989 /// will be removed, and this field will become non-optional.
990 root_module: ?*Module = null,
991
992 /// Deprecated; prefer populating `root_module`.
993 root_source_file: ?LazyPath = null,
994 /// Deprecated; prefer populating `root_module`.
995 target: ?ResolvedTarget = null,
996 /// Deprecated; prefer populating `root_module`.
997 optimize: std.builtin.OptimizeMode = .Debug,
998 /// Deprecated; prefer populating `root_module`.
999 version: ?std.SemanticVersion = null,
1000 /// Deprecated; prefer populating `root_module`.
9171001 link_libc: ?bool = null,
1002 /// Deprecated; prefer populating `root_module`.
9181003 link_libcpp: ?bool = null,
1004 /// Deprecated; prefer populating `root_module`.
9191005 single_threaded: ?bool = null,
1006 /// Deprecated; prefer populating `root_module`.
9201007 pic: ?bool = null,
1008 /// Deprecated; prefer populating `root_module`.
9211009 strip: ?bool = null,
1010 /// Deprecated; prefer populating `root_module`.
9221011 unwind_tables: ?std.builtin.UnwindTables = null,
1012 /// Deprecated; prefer populating `root_module`.
9231013 omit_frame_pointer: ?bool = null,
1014 /// Deprecated; prefer populating `root_module`.
9241015 sanitize_thread: ?bool = null,
1016 /// Deprecated; prefer populating `root_module`.
9251017 error_tracing: ?bool = null,
926 use_llvm: ?bool = null,
927 use_lld: ?bool = null,
928 zig_lib_dir: ?LazyPath = null,
9291018};
9301019
9311020/// Creates an executable containing unit tests.
......@@ -937,11 +1026,14 @@ pub const TestOptions = struct {
9371026/// two steps are separated because they are independently configured and
9381027/// cached.
9391028pub fn addTest(b: *Build, options: TestOptions) *Step.Compile {
940 return Step.Compile.create(b, .{
1029 if (options.root_module != null and options.root_source_file != null) {
1030 @panic("`root_module` and `root_source_file` cannot both be populated");
1031 }
1032 return .create(b, .{
9411033 .name = options.name,
9421034 .kind = .@"test",
943 .root_module = .{
944 .root_source_file = options.root_source_file,
1035 .root_module = options.root_module orelse b.createModule(.{
1036 .root_source_file = options.root_source_file orelse @panic("`root_module` and `root_source_file` cannot both be null"),
9451037 .target = options.target orelse b.graph.host,
9461038 .optimize = options.optimize,
9471039 .link_libc = options.link_libc,
......@@ -953,7 +1045,7 @@ pub fn addTest(b: *Build, options: TestOptions) *Step.Compile {
9531045 .omit_frame_pointer = options.omit_frame_pointer,
9541046 .sanitize_thread = options.sanitize_thread,
9551047 .error_tracing = options.error_tracing,
956 },
1048 }),
9571049 .max_rss = options.max_rss,
9581050 .filters = if (options.filter != null and options.filters.len > 0) filters: {
9591051 const filters = b.allocator.alloc([]const u8, 1 + options.filters.len) catch @panic("OOM");
......@@ -979,19 +1071,20 @@ pub const AssemblyOptions = struct {
9791071 zig_lib_dir: ?LazyPath = null,
9801072};
9811073
1074/// Deprecated; prefer using `addObject` where the `root_module` has an empty
1075/// `root_source_file` and contains an assembly file via `Module.addAssemblyFile`.
9821076pub fn addAssembly(b: *Build, options: AssemblyOptions) *Step.Compile {
983 const obj_step = Step.Compile.create(b, .{
1077 const root_module = b.createModule(.{
1078 .target = options.target,
1079 .optimize = options.optimize,
1080 });
1081 root_module.addAssemblyFile(options.source_file);
1082 return b.addObject(.{
9841083 .name = options.name,
985 .kind = .obj,
986 .root_module = .{
987 .target = options.target,
988 .optimize = options.optimize,
989 },
9901084 .max_rss = options.max_rss,
9911085 .zig_lib_dir = options.zig_lib_dir,
1086 .root_module = root_module,
9921087 });
993 obj_step.addAssemblyFile(options.source_file);
994 return obj_step;
9951088}
9961089
9971090/// This function creates a module and adds it to the package's module set, making
lib/std/Build/Module.zig+91-216
......@@ -1,9 +1,5 @@
11/// The one responsible for creating this module.
22owner: *std.Build,
3/// Tracks the set of steps that depend on this `Module`. This ensures that
4/// when making this `Module` depend on other `Module` objects and `Step`
5/// objects, respective `Step` dependencies can be added.
6depending_steps: std.AutoArrayHashMapUnmanaged(*Step.Compile, void),
73root_source_file: ?LazyPath,
84/// The modules that are mapped into this module's import table.
95/// Use `addImport` rather than modifying this field directly in order to
......@@ -41,6 +37,10 @@ link_libcpp: ?bool,
4137/// Symbols to be exported when compiling to WebAssembly.
4238export_symbol_names: []const []const u8 = &.{},
4339
40/// Caches the result of `getGraph` when called multiple times.
41/// Use `getGraph` instead of accessing this field directly.
42cached_graph: Graph = .{ .modules = &.{}, .names = &.{} },
43
4444pub const RPath = union(enum) {
4545 lazy_path: LazyPath,
4646 special: []const u8,
......@@ -242,59 +242,61 @@ pub const Import = struct {
242242 module: *Module,
243243};
244244
245pub fn init(m: *Module, owner: *std.Build, options: CreateOptions, compile: ?*Step.Compile) void {
245pub fn init(
246 m: *Module,
247 owner: *std.Build,
248 value: union(enum) { options: CreateOptions, existing: *const Module },
249) void {
246250 const allocator = owner.allocator;
247251
248 m.* = .{
249 .owner = owner,
250 .depending_steps = .{},
251 .root_source_file = if (options.root_source_file) |lp| lp.dupe(owner) else null,
252 .import_table = .{},
253 .resolved_target = options.target,
254 .optimize = options.optimize,
255 .link_libc = options.link_libc,
256 .link_libcpp = options.link_libcpp,
257 .dwarf_format = options.dwarf_format,
258 .c_macros = .{},
259 .include_dirs = .{},
260 .lib_paths = .{},
261 .rpaths = .{},
262 .frameworks = .{},
263 .link_objects = .{},
264 .strip = options.strip,
265 .unwind_tables = options.unwind_tables,
266 .single_threaded = options.single_threaded,
267 .stack_protector = options.stack_protector,
268 .stack_check = options.stack_check,
269 .sanitize_c = options.sanitize_c,
270 .sanitize_thread = options.sanitize_thread,
271 .fuzz = options.fuzz,
272 .code_model = options.code_model,
273 .valgrind = options.valgrind,
274 .pic = options.pic,
275 .red_zone = options.red_zone,
276 .omit_frame_pointer = options.omit_frame_pointer,
277 .error_tracing = options.error_tracing,
278 .export_symbol_names = &.{},
279 };
280
281 m.import_table.ensureUnusedCapacity(allocator, options.imports.len) catch @panic("OOM");
282 for (options.imports) |dep| {
283 m.import_table.putAssumeCapacity(dep.name, dep.module);
284 }
252 switch (value) {
253 .options => |options| {
254 m.* = .{
255 .owner = owner,
256 .root_source_file = if (options.root_source_file) |lp| lp.dupe(owner) else null,
257 .import_table = .{},
258 .resolved_target = options.target,
259 .optimize = options.optimize,
260 .link_libc = options.link_libc,
261 .link_libcpp = options.link_libcpp,
262 .dwarf_format = options.dwarf_format,
263 .c_macros = .{},
264 .include_dirs = .{},
265 .lib_paths = .{},
266 .rpaths = .{},
267 .frameworks = .{},
268 .link_objects = .{},
269 .strip = options.strip,
270 .unwind_tables = options.unwind_tables,
271 .single_threaded = options.single_threaded,
272 .stack_protector = options.stack_protector,
273 .stack_check = options.stack_check,
274 .sanitize_c = options.sanitize_c,
275 .sanitize_thread = options.sanitize_thread,
276 .fuzz = options.fuzz,
277 .code_model = options.code_model,
278 .valgrind = options.valgrind,
279 .pic = options.pic,
280 .red_zone = options.red_zone,
281 .omit_frame_pointer = options.omit_frame_pointer,
282 .error_tracing = options.error_tracing,
283 .export_symbol_names = &.{},
284 };
285285
286 if (compile) |c| {
287 m.depending_steps.put(allocator, c, {}) catch @panic("OOM");
286 m.import_table.ensureUnusedCapacity(allocator, options.imports.len) catch @panic("OOM");
287 for (options.imports) |dep| {
288 m.import_table.putAssumeCapacity(dep.name, dep.module);
289 }
290 },
291 .existing => |existing| {
292 m.* = existing.*;
293 },
288294 }
289
290 // This logic accesses `depending_steps` which was just modified above.
291 var it = m.iterateDependencies(null, false);
292 while (it.next()) |item| addShallowDependencies(m, item.module);
293295}
294296
295297pub fn create(owner: *std.Build, options: CreateOptions) *Module {
296298 const m = owner.allocator.create(Module) catch @panic("OOM");
297 m.init(owner, options, null);
299 m.init(owner, .{ .options = options });
298300 return m;
299301}
300302
......@@ -302,69 +304,6 @@ pub fn create(owner: *std.Build, options: CreateOptions) *Module {
302304pub fn addImport(m: *Module, name: []const u8, module: *Module) void {
303305 const b = m.owner;
304306 m.import_table.put(b.allocator, b.dupe(name), module) catch @panic("OOM");
305
306 var it = module.iterateDependencies(null, false);
307 while (it.next()) |item| addShallowDependencies(m, item.module);
308}
309
310/// Creates step dependencies and updates `depending_steps` of `dependee` so that
311/// subsequent calls to `addImport` on `dependee` will additionally create step
312/// dependencies on `m`'s `depending_steps`.
313fn addShallowDependencies(m: *Module, dependee: *Module) void {
314 if (dependee.root_source_file) |lazy_path| addLazyPathDependencies(m, dependee, lazy_path);
315 for (dependee.lib_paths.items) |lib_path| addLazyPathDependencies(m, dependee, lib_path);
316 for (dependee.rpaths.items) |rpath| switch (rpath) {
317 .lazy_path => |lp| addLazyPathDependencies(m, dependee, lp),
318 .special => {},
319 };
320
321 for (dependee.link_objects.items) |link_object| switch (link_object) {
322 .other_step => |compile| {
323 addStepDependencies(m, dependee, &compile.step);
324 addLazyPathDependenciesOnly(m, compile.getEmittedIncludeTree());
325 },
326
327 .static_path,
328 .assembly_file,
329 => |lp| addLazyPathDependencies(m, dependee, lp),
330
331 .c_source_file => |x| addLazyPathDependencies(m, dependee, x.file),
332 .win32_resource_file => |x| addLazyPathDependencies(m, dependee, x.file),
333
334 .c_source_files,
335 .system_lib,
336 => {},
337 };
338}
339
340fn addLazyPathDependencies(m: *Module, module: *Module, lazy_path: LazyPath) void {
341 addLazyPathDependenciesOnly(m, lazy_path);
342 if (m != module) {
343 for (m.depending_steps.keys()) |compile| {
344 module.depending_steps.put(m.owner.allocator, compile, {}) catch @panic("OOM");
345 }
346 }
347}
348
349fn addLazyPathDependenciesOnly(m: *Module, lazy_path: LazyPath) void {
350 for (m.depending_steps.keys()) |compile| {
351 lazy_path.addStepDependencies(&compile.step);
352 }
353}
354
355fn addStepDependencies(m: *Module, module: *Module, dependee: *Step) void {
356 addStepDependenciesOnly(m, dependee);
357 if (m != module) {
358 for (m.depending_steps.keys()) |compile| {
359 module.depending_steps.put(m.owner.allocator, compile, {}) catch @panic("OOM");
360 }
361 }
362}
363
364fn addStepDependenciesOnly(m: *Module, dependee: *Step) void {
365 for (m.depending_steps.keys()) |compile| {
366 compile.step.dependOn(dependee);
367 }
368307}
369308
370309/// Creates a new module and adds it to be used with `@import`.
......@@ -380,91 +319,6 @@ pub fn addOptions(m: *Module, module_name: []const u8, options: *Step.Options) v
380319 addImport(m, module_name, options.createModule());
381320}
382321
383pub const DependencyIterator = struct {
384 allocator: std.mem.Allocator,
385 index: usize,
386 set: std.AutoArrayHashMapUnmanaged(Key, []const u8),
387 chase_dyn_libs: bool,
388
389 pub const Key = struct {
390 /// The compilation that contains the `Module`. Note that a `Module` might be
391 /// used by more than one compilation.
392 compile: ?*Step.Compile,
393 module: *Module,
394 };
395
396 pub const Item = struct {
397 /// The compilation that contains the `Module`. Note that a `Module` might be
398 /// used by more than one compilation.
399 compile: ?*Step.Compile,
400 module: *Module,
401 name: []const u8,
402 };
403
404 pub fn deinit(it: *DependencyIterator) void {
405 it.set.deinit(it.allocator);
406 it.* = undefined;
407 }
408
409 pub fn next(it: *DependencyIterator) ?Item {
410 if (it.index >= it.set.count()) {
411 it.set.clearAndFree(it.allocator);
412 return null;
413 }
414 const key = it.set.keys()[it.index];
415 const name = it.set.values()[it.index];
416 it.index += 1;
417 const module = key.module;
418 it.set.ensureUnusedCapacity(it.allocator, module.import_table.count()) catch
419 @panic("OOM");
420 for (module.import_table.keys(), module.import_table.values()) |dep_name, dep| {
421 it.set.putAssumeCapacity(.{
422 .module = dep,
423 .compile = key.compile,
424 }, dep_name);
425 }
426
427 if (key.compile != null) {
428 for (module.link_objects.items) |link_object| switch (link_object) {
429 .other_step => |compile| {
430 if (!it.chase_dyn_libs and compile.isDynamicLibrary()) continue;
431
432 it.set.put(it.allocator, .{
433 .module = &compile.root_module,
434 .compile = compile,
435 }, "root") catch @panic("OOM");
436 },
437 else => {},
438 };
439 }
440
441 return .{
442 .compile = key.compile,
443 .module = key.module,
444 .name = name,
445 };
446 }
447};
448
449pub fn iterateDependencies(
450 m: *Module,
451 chase_steps: ?*Step.Compile,
452 chase_dyn_libs: bool,
453) DependencyIterator {
454 var it: DependencyIterator = .{
455 .allocator = m.owner.allocator,
456 .index = 0,
457 .set = .{},
458 .chase_dyn_libs = chase_dyn_libs,
459 };
460 it.set.ensureUnusedCapacity(m.owner.allocator, m.import_table.count() + 1) catch @panic("OOM");
461 it.set.putAssumeCapacity(.{
462 .module = m,
463 .compile = chase_steps,
464 }, "root");
465 return it;
466}
467
468322pub const LinkSystemLibraryOptions = struct {
469323 /// Causes dynamic libraries to be linked regardless of whether they are
470324 /// actually depended on. When false, dynamic libraries with no referenced
......@@ -547,7 +401,6 @@ pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void {
547401 .flags = b.dupeStrings(options.flags),
548402 };
549403 m.link_objects.append(allocator, .{ .c_source_files = c_source_files }) catch @panic("OOM");
550 addLazyPathDependenciesOnly(m, c_source_files.root);
551404}
552405
553406pub fn addCSourceFile(m: *Module, source: CSourceFile) void {
......@@ -556,7 +409,6 @@ pub fn addCSourceFile(m: *Module, source: CSourceFile) void {
556409 const c_source_file = allocator.create(CSourceFile) catch @panic("OOM");
557410 c_source_file.* = source.dupe(b);
558411 m.link_objects.append(allocator, .{ .c_source_file = c_source_file }) catch @panic("OOM");
559 addLazyPathDependenciesOnly(m, source.file);
560412}
561413
562414/// Resource files must have the extension `.rc`.
......@@ -573,22 +425,16 @@ pub fn addWin32ResourceFile(m: *Module, source: RcSourceFile) void {
573425 const rc_source_file = allocator.create(RcSourceFile) catch @panic("OOM");
574426 rc_source_file.* = source.dupe(b);
575427 m.link_objects.append(allocator, .{ .win32_resource_file = rc_source_file }) catch @panic("OOM");
576 addLazyPathDependenciesOnly(m, source.file);
577 for (source.include_paths) |include_path| {
578 addLazyPathDependenciesOnly(m, include_path);
579 }
580428}
581429
582430pub fn addAssemblyFile(m: *Module, source: LazyPath) void {
583431 const b = m.owner;
584432 m.link_objects.append(b.allocator, .{ .assembly_file = source.dupe(b) }) catch @panic("OOM");
585 addLazyPathDependenciesOnly(m, source);
586433}
587434
588435pub fn addObjectFile(m: *Module, object: LazyPath) void {
589436 const b = m.owner;
590437 m.link_objects.append(b.allocator, .{ .static_path = object.dupe(b) }) catch @panic("OOM");
591 addLazyPathDependenciesOnly(m, object);
592438}
593439
594440pub fn addObject(m: *Module, object: *Step.Compile) void {
......@@ -604,51 +450,43 @@ pub fn linkLibrary(m: *Module, library: *Step.Compile) void {
604450pub fn addAfterIncludePath(m: *Module, lazy_path: LazyPath) void {
605451 const b = m.owner;
606452 m.include_dirs.append(b.allocator, .{ .path_after = lazy_path.dupe(b) }) catch @panic("OOM");
607 addLazyPathDependenciesOnly(m, lazy_path);
608453}
609454
610455pub fn addSystemIncludePath(m: *Module, lazy_path: LazyPath) void {
611456 const b = m.owner;
612457 m.include_dirs.append(b.allocator, .{ .path_system = lazy_path.dupe(b) }) catch @panic("OOM");
613 addLazyPathDependenciesOnly(m, lazy_path);
614458}
615459
616460pub fn addIncludePath(m: *Module, lazy_path: LazyPath) void {
617461 const b = m.owner;
618462 m.include_dirs.append(b.allocator, .{ .path = lazy_path.dupe(b) }) catch @panic("OOM");
619 addLazyPathDependenciesOnly(m, lazy_path);
620463}
621464
622465pub fn addConfigHeader(m: *Module, config_header: *Step.ConfigHeader) void {
623466 const allocator = m.owner.allocator;
624467 m.include_dirs.append(allocator, .{ .config_header_step = config_header }) catch @panic("OOM");
625 addStepDependenciesOnly(m, &config_header.step);
626468}
627469
628470pub fn addSystemFrameworkPath(m: *Module, directory_path: LazyPath) void {
629471 const b = m.owner;
630472 m.include_dirs.append(b.allocator, .{ .framework_path_system = directory_path.dupe(b) }) catch
631473 @panic("OOM");
632 addLazyPathDependenciesOnly(m, directory_path);
633474}
634475
635476pub fn addFrameworkPath(m: *Module, directory_path: LazyPath) void {
636477 const b = m.owner;
637478 m.include_dirs.append(b.allocator, .{ .framework_path = directory_path.dupe(b) }) catch
638479 @panic("OOM");
639 addLazyPathDependenciesOnly(m, directory_path);
640480}
641481
642482pub fn addLibraryPath(m: *Module, directory_path: LazyPath) void {
643483 const b = m.owner;
644484 m.lib_paths.append(b.allocator, directory_path.dupe(b)) catch @panic("OOM");
645 addLazyPathDependenciesOnly(m, directory_path);
646485}
647486
648487pub fn addRPath(m: *Module, directory_path: LazyPath) void {
649488 const b = m.owner;
650489 m.rpaths.append(b.allocator, .{ .lazy_path = directory_path.dupe(b) }) catch @panic("OOM");
651 addLazyPathDependenciesOnly(m, directory_path);
652490}
653491
654492pub fn addRPathSpecial(m: *Module, bytes: []const u8) void {
......@@ -772,7 +610,6 @@ fn addFlag(
772610fn linkLibraryOrObject(m: *Module, other: *Step.Compile) void {
773611 const allocator = m.owner.allocator;
774612 _ = other.getEmittedBin(); // Indicate there is a dependency on the outputted binary.
775 addStepDependenciesOnly(m, &other.step);
776613
777614 if (other.rootModuleTarget().os.tag == .windows and other.isDynamicLibrary()) {
778615 _ = other.getEmittedImplib(); // Indicate dependency on the outputted implib.
......@@ -780,8 +617,6 @@ fn linkLibraryOrObject(m: *Module, other: *Step.Compile) void {
780617
781618 m.link_objects.append(allocator, .{ .other_step = other }) catch @panic("OOM");
782619 m.include_dirs.append(allocator, .{ .other_step = other }) catch @panic("OOM");
783
784 addLazyPathDependenciesOnly(m, other.getEmittedIncludeTree());
785620}
786621
787622fn requireKnownTarget(m: *Module) std.Target {
......@@ -790,6 +625,46 @@ fn requireKnownTarget(m: *Module) std.Target {
790625 return resolved_target.result;
791626}
792627
628/// Elements of `modules` and `names` are matched one-to-one.
629pub const Graph = struct {
630 modules: []const *Module,
631 names: []const []const u8,
632};
633
634/// Intended to be used during the make phase only.
635///
636/// Given that `root` is the root `Module` of a compilation, return all `Module`s
637/// in the module graph, including `root` itself. `root` is guaranteed to be the
638/// first module in the returned slice.
639pub fn getGraph(root: *Module) Graph {
640 if (root.cached_graph.modules.len != 0) {
641 return root.cached_graph;
642 }
643
644 const arena = root.owner.graph.arena;
645
646 var modules: std.AutoArrayHashMapUnmanaged(*std.Build.Module, []const u8) = .empty;
647 var next_idx: usize = 0;
648
649 modules.putNoClobber(arena, root, "root") catch @panic("OOM");
650
651 while (next_idx < modules.count()) {
652 const mod = modules.keys()[next_idx];
653 next_idx += 1;
654 modules.ensureUnusedCapacity(arena, mod.import_table.count()) catch @panic("OOM");
655 for (mod.import_table.keys(), mod.import_table.values()) |import_name, other_mod| {
656 modules.putAssumeCapacity(other_mod, import_name);
657 }
658 }
659
660 const result: Graph = .{
661 .modules = modules.keys(),
662 .names = modules.values(),
663 };
664 root.cached_graph = result;
665 return result;
666}
667
793668const Module = @This();
794669const std = @import("std");
795670const assert = std.debug.assert;
lib/std/Build/Step/Compile.zig+283-271
......@@ -22,7 +22,7 @@ const Path = std.Build.Cache.Path;
2222pub const base_id: Step.Id = .compile;
2323
2424step: Step,
25root_module: Module,
25root_module: *Module,
2626
2727name: []const u8,
2828linker_script: ?LazyPath = null,
......@@ -262,7 +262,7 @@ pub const Entry = union(enum) {
262262
263263pub const Options = struct {
264264 name: []const u8,
265 root_module: Module.CreateOptions,
265 root_module: *Module,
266266 kind: Kind,
267267 linkage: ?std.builtin.LinkMode = null,
268268 version: ?std.SemanticVersion = null,
......@@ -359,7 +359,8 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
359359 else
360360 owner.fmt("{s} ", .{name});
361361
362 const resolved_target = options.root_module.target.?;
362 const resolved_target = options.root_module.resolved_target orelse
363 @panic("the root Module of a Compile step must be created with a known 'target' field");
363364 const target = resolved_target.result;
364365
365366 const step_name = owner.fmt("{s} {s}{s} {s}", .{
......@@ -388,7 +389,7 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
388389
389390 const compile = owner.allocator.create(Compile) catch @panic("OOM");
390391 compile.* = .{
391 .root_module = undefined,
392 .root_module = options.root_module,
392393 .verbose_link = false,
393394 .verbose_cc = false,
394395 .linkage = options.linkage,
......@@ -432,8 +433,6 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
432433 .zig_process = null,
433434 };
434435
435 compile.root_module.init(owner, options.root_module, compile);
436
437436 if (options.zig_lib_dir) |lp| {
438437 compile.zig_lib_dir = lp.dupe(compile.step.owner);
439438 lp.addStepDependencies(&compile.step);
......@@ -583,9 +582,6 @@ pub fn checkObject(compile: *Compile) *Step.CheckObject {
583582 return Step.CheckObject.create(compile.step.owner, compile.getEmittedBin(), compile.rootModuleTarget().ofmt);
584583}
585584
586/// deprecated: use `setLinkerScript`
587pub const setLinkerScriptPath = setLinkerScript;
588
589585pub fn setLinkerScript(compile: *Compile, source: LazyPath) void {
590586 const b = compile.step.owner;
591587 compile.linker_script = source.dupe(b);
......@@ -609,16 +605,17 @@ pub fn dependsOnSystemLibrary(compile: *const Compile, name: []const u8) bool {
609605 var is_linking_libc = false;
610606 var is_linking_libcpp = false;
611607
612 var dep_it = compile.root_module.iterateDependencies(compile, true);
613 while (dep_it.next()) |module| {
614 for (module.link_objects.items) |link_object| {
615 switch (link_object) {
616 .system_lib => |lib| if (mem.eql(u8, lib.name, name)) return true,
617 else => continue,
608 for (compile.getCompileDependencies(true)) |some_compile| {
609 for (some_compile.root_module.getGraph().modules) |mod| {
610 for (mod.link_objects.items) |lo| {
611 switch (lo) {
612 .system_lib => |lib| if (mem.eql(u8, lib.name, name)) return true,
613 else => {},
614 }
618615 }
616 if (mod.link_libc) is_linking_libc = true;
617 if (mod.link_libcpp) is_linking_libcpp = true;
619618 }
620 is_linking_libc = is_linking_libc or module.link_libc == true;
621 is_linking_libcpp = is_linking_libcpp or module.link_libcpp == true;
622619 }
623620
624621 const target = compile.rootModuleTarget();
......@@ -675,11 +672,6 @@ pub fn linkLibCpp(compile: *Compile) void {
675672 compile.root_module.link_libcpp = true;
676673}
677674
678/// Deprecated. Use `c.root_module.addCMacro`.
679pub fn defineCMacro(c: *Compile, name: []const u8, value: ?[]const u8) void {
680 c.root_module.addCMacro(name, value orelse "1");
681}
682
683675const PkgConfigResult = struct {
684676 cflags: []const []const u8,
685677 libs: []const []const u8,
......@@ -805,16 +797,6 @@ pub fn linkFramework(c: *Compile, name: []const u8) void {
805797 c.root_module.linkFramework(name, .{});
806798}
807799
808/// Deprecated. Use `c.root_module.linkFramework`.
809pub fn linkFrameworkNeeded(c: *Compile, name: []const u8) void {
810 c.root_module.linkFramework(name, .{ .needed = true });
811}
812
813/// Deprecated. Use `c.root_module.linkFramework`.
814pub fn linkFrameworkWeak(c: *Compile, name: []const u8) void {
815 c.root_module.linkFramework(name, .{ .weak = true });
816}
817
818800/// Handy when you have many C/C++ source files and want them all to have the same flags.
819801pub fn addCSourceFiles(compile: *Compile, options: Module.AddCSourceFilesOptions) void {
820802 compile.root_module.addCSourceFiles(options);
......@@ -978,23 +960,22 @@ const CliNamedModules = struct {
978960 .modules = .{},
979961 .names = .{},
980962 };
981 var dep_it = root_module.iterateDependencies(null, false);
963 const graph = root_module.getGraph();
982964 {
983 const item = dep_it.next().?;
984 assert(root_module == item.module);
965 assert(graph.modules[0] == root_module);
985966 try compile.modules.put(arena, root_module, {});
986967 try compile.names.put(arena, "root", {});
987968 }
988 while (dep_it.next()) |item| {
989 var name = item.name;
969 for (graph.modules[1..], graph.names[1..]) |mod, orig_name| {
970 var name = orig_name;
990971 var n: usize = 0;
991972 while (true) {
992973 const gop = try compile.names.getOrPut(arena, name);
993974 if (!gop.found_existing) {
994 try compile.modules.putNoClobber(arena, item.module, {});
975 try compile.modules.putNoClobber(arena, mod, {});
995976 break;
996977 }
997 name = try std.fmt.allocPrint(arena, "{s}{d}", .{ item.name, n });
978 name = try std.fmt.allocPrint(arena, "{s}{d}", .{ orig_name, n });
998979 n += 1;
999980 }
1000981 }
......@@ -1097,279 +1078,278 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
10971078 // emitted if there is nothing to link.
10981079 var total_linker_objects: usize = @intFromBool(compile.root_module.root_source_file != null);
10991080
1100 {
1101 // Fully recursive iteration including dynamic libraries to detect
1102 // libc and libc++ linkage.
1103 var dep_it = compile.root_module.iterateDependencies(compile, true);
1104 while (dep_it.next()) |key| {
1105 if (key.module.link_libc == true) compile.is_linking_libc = true;
1106 if (key.module.link_libcpp == true) compile.is_linking_libcpp = true;
1081 // Fully recursive iteration including dynamic libraries to detect
1082 // libc and libc++ linkage.
1083 for (compile.getCompileDependencies(true)) |some_compile| {
1084 for (some_compile.root_module.getGraph().modules) |mod| {
1085 if (mod.link_libc == true) compile.is_linking_libc = true;
1086 if (mod.link_libcpp == true) compile.is_linking_libcpp = true;
11071087 }
11081088 }
11091089
1110 var cli_named_modules = try CliNamedModules.init(arena, &compile.root_module);
1090 var cli_named_modules = try CliNamedModules.init(arena, compile.root_module);
11111091
11121092 // For this loop, don't chase dynamic libraries because their link
11131093 // objects are already linked.
1114 var dep_it = compile.root_module.iterateDependencies(compile, false);
1115
1116 while (dep_it.next()) |dep| {
1117 // While walking transitive dependencies, if a given link object is
1118 // already included in a library, it should not redundantly be
1119 // placed on the linker line of the dependee.
1120 const my_responsibility = dep.compile.? == compile;
1121 const already_linked = !my_responsibility and dep.compile.?.isDynamicLibrary();
1122
1123 // Inherit dependencies on darwin frameworks.
1124 if (!already_linked) {
1125 for (dep.module.frameworks.keys(), dep.module.frameworks.values()) |name, info| {
1126 try frameworks.put(arena, name, info);
1094 for (compile.getCompileDependencies(false)) |dep_compile| {
1095 for (dep_compile.root_module.getGraph().modules) |mod| {
1096 // While walking transitive dependencies, if a given link object is
1097 // already included in a library, it should not redundantly be
1098 // placed on the linker line of the dependee.
1099 const my_responsibility = dep_compile == compile;
1100 const already_linked = !my_responsibility and dep_compile.isDynamicLibrary();
1101
1102 // Inherit dependencies on darwin frameworks.
1103 if (!already_linked) {
1104 for (mod.frameworks.keys(), mod.frameworks.values()) |name, info| {
1105 try frameworks.put(arena, name, info);
1106 }
11271107 }
1128 }
11291108
1130 // Inherit dependencies on system libraries and static libraries.
1131 for (dep.module.link_objects.items) |link_object| {
1132 switch (link_object) {
1133 .static_path => |static_path| {
1134 if (my_responsibility) {
1135 try zig_args.append(static_path.getPath2(dep.module.owner, step));
1136 total_linker_objects += 1;
1137 }
1138 },
1139 .system_lib => |system_lib| {
1140 const system_lib_gop = try seen_system_libs.getOrPut(arena, system_lib.name);
1141 if (system_lib_gop.found_existing) {
1142 try zig_args.appendSlice(system_lib_gop.value_ptr.*);
1143 continue;
1144 } else {
1145 system_lib_gop.value_ptr.* = &.{};
1146 }
1147
1148 if (already_linked)
1149 continue;
1150
1151 if ((system_lib.search_strategy != prev_search_strategy or
1152 system_lib.preferred_link_mode != prev_preferred_link_mode) and
1153 compile.linkage != .static)
1154 {
1155 switch (system_lib.search_strategy) {
1156 .no_fallback => switch (system_lib.preferred_link_mode) {
1157 .dynamic => try zig_args.append("-search_dylibs_only"),
1158 .static => try zig_args.append("-search_static_only"),
1159 },
1160 .paths_first => switch (system_lib.preferred_link_mode) {
1161 .dynamic => try zig_args.append("-search_paths_first"),
1162 .static => try zig_args.append("-search_paths_first_static"),
1163 },
1164 .mode_first => switch (system_lib.preferred_link_mode) {
1165 .dynamic => try zig_args.append("-search_dylibs_first"),
1166 .static => try zig_args.append("-search_static_first"),
1167 },
1109 // Inherit dependencies on system libraries and static libraries.
1110 for (mod.link_objects.items) |link_object| {
1111 switch (link_object) {
1112 .static_path => |static_path| {
1113 if (my_responsibility) {
1114 try zig_args.append(static_path.getPath2(mod.owner, step));
1115 total_linker_objects += 1;
1116 }
1117 },
1118 .system_lib => |system_lib| {
1119 const system_lib_gop = try seen_system_libs.getOrPut(arena, system_lib.name);
1120 if (system_lib_gop.found_existing) {
1121 try zig_args.appendSlice(system_lib_gop.value_ptr.*);
1122 continue;
1123 } else {
1124 system_lib_gop.value_ptr.* = &.{};
11681125 }
1169 prev_search_strategy = system_lib.search_strategy;
1170 prev_preferred_link_mode = system_lib.preferred_link_mode;
1171 }
11721126
1173 const prefix: []const u8 = prefix: {
1174 if (system_lib.needed) break :prefix "-needed-l";
1175 if (system_lib.weak) break :prefix "-weak-l";
1176 break :prefix "-l";
1177 };
1178 switch (system_lib.use_pkg_config) {
1179 .no => try zig_args.append(b.fmt("{s}{s}", .{ prefix, system_lib.name })),
1180 .yes, .force => {
1181 if (compile.runPkgConfig(system_lib.name)) |result| {
1182 try zig_args.appendSlice(result.cflags);
1183 try zig_args.appendSlice(result.libs);
1184 try seen_system_libs.put(arena, system_lib.name, result.cflags);
1185 } else |err| switch (err) {
1186 error.PkgConfigInvalidOutput,
1187 error.PkgConfigCrashed,
1188 error.PkgConfigFailed,
1189 error.PkgConfigNotInstalled,
1190 error.PackageNotFound,
1191 => switch (system_lib.use_pkg_config) {
1192 .yes => {
1193 // pkg-config failed, so fall back to linking the library
1194 // by name directly.
1195 try zig_args.append(b.fmt("{s}{s}", .{
1196 prefix,
1197 system_lib.name,
1198 }));
1199 },
1200 .force => {
1201 panic("pkg-config failed for library {s}", .{system_lib.name});
1202 },
1203 .no => unreachable,
1127 if (already_linked)
1128 continue;
1129
1130 if ((system_lib.search_strategy != prev_search_strategy or
1131 system_lib.preferred_link_mode != prev_preferred_link_mode) and
1132 compile.linkage != .static)
1133 {
1134 switch (system_lib.search_strategy) {
1135 .no_fallback => switch (system_lib.preferred_link_mode) {
1136 .dynamic => try zig_args.append("-search_dylibs_only"),
1137 .static => try zig_args.append("-search_static_only"),
1138 },
1139 .paths_first => switch (system_lib.preferred_link_mode) {
1140 .dynamic => try zig_args.append("-search_paths_first"),
1141 .static => try zig_args.append("-search_paths_first_static"),
1142 },
1143 .mode_first => switch (system_lib.preferred_link_mode) {
1144 .dynamic => try zig_args.append("-search_dylibs_first"),
1145 .static => try zig_args.append("-search_static_first"),
12041146 },
1205
1206 else => |e| return e,
1207 }
1208 },
1209 }
1210 },
1211 .other_step => |other| {
1212 switch (other.kind) {
1213 .exe => return step.fail("cannot link with an executable build artifact", .{}),
1214 .@"test" => return step.fail("cannot link with a test", .{}),
1215 .obj => {
1216 const included_in_lib_or_obj = !my_responsibility and
1217 (dep.compile.?.kind == .lib or dep.compile.?.kind == .obj);
1218 if (!already_linked and !included_in_lib_or_obj) {
1219 try zig_args.append(other.getEmittedBin().getPath2(b, step));
1220 total_linker_objects += 1;
1221 }
1222 },
1223 .lib => l: {
1224 const other_produces_implib = other.producesImplib();
1225 const other_is_static = other_produces_implib or other.isStaticLibrary();
1226
1227 if (compile.isStaticLibrary() and other_is_static) {
1228 // Avoid putting a static library inside a static library.
1229 break :l;
12301147 }
1148 prev_search_strategy = system_lib.search_strategy;
1149 prev_preferred_link_mode = system_lib.preferred_link_mode;
1150 }
12311151
1232 // For DLLs, we must link against the implib.
1233 // For everything else, we directly link
1234 // against the library file.
1235 const full_path_lib = if (other_produces_implib)
1236 other.getGeneratedFilePath("generated_implib", &compile.step)
1237 else
1238 other.getGeneratedFilePath("generated_bin", &compile.step);
1152 const prefix: []const u8 = prefix: {
1153 if (system_lib.needed) break :prefix "-needed-l";
1154 if (system_lib.weak) break :prefix "-weak-l";
1155 break :prefix "-l";
1156 };
1157 switch (system_lib.use_pkg_config) {
1158 .no => try zig_args.append(b.fmt("{s}{s}", .{ prefix, system_lib.name })),
1159 .yes, .force => {
1160 if (compile.runPkgConfig(system_lib.name)) |result| {
1161 try zig_args.appendSlice(result.cflags);
1162 try zig_args.appendSlice(result.libs);
1163 try seen_system_libs.put(arena, system_lib.name, result.cflags);
1164 } else |err| switch (err) {
1165 error.PkgConfigInvalidOutput,
1166 error.PkgConfigCrashed,
1167 error.PkgConfigFailed,
1168 error.PkgConfigNotInstalled,
1169 error.PackageNotFound,
1170 => switch (system_lib.use_pkg_config) {
1171 .yes => {
1172 // pkg-config failed, so fall back to linking the library
1173 // by name directly.
1174 try zig_args.append(b.fmt("{s}{s}", .{
1175 prefix,
1176 system_lib.name,
1177 }));
1178 },
1179 .force => {
1180 panic("pkg-config failed for library {s}", .{system_lib.name});
1181 },
1182 .no => unreachable,
1183 },
12391184
1240 try zig_args.append(full_path_lib);
1241 total_linker_objects += 1;
1185 else => |e| return e,
1186 }
1187 },
1188 }
1189 },
1190 .other_step => |other| {
1191 switch (other.kind) {
1192 .exe => return step.fail("cannot link with an executable build artifact", .{}),
1193 .@"test" => return step.fail("cannot link with a test", .{}),
1194 .obj => {
1195 const included_in_lib_or_obj = !my_responsibility and
1196 (dep_compile.kind == .lib or dep_compile.kind == .obj);
1197 if (!already_linked and !included_in_lib_or_obj) {
1198 try zig_args.append(other.getEmittedBin().getPath2(b, step));
1199 total_linker_objects += 1;
1200 }
1201 },
1202 .lib => l: {
1203 const other_produces_implib = other.producesImplib();
1204 const other_is_static = other_produces_implib or other.isStaticLibrary();
12421205
1243 if (other.linkage == .dynamic and
1244 compile.rootModuleTarget().os.tag != .windows)
1245 {
1246 if (fs.path.dirname(full_path_lib)) |dirname| {
1247 try zig_args.append("-rpath");
1248 try zig_args.append(dirname);
1206 if (compile.isStaticLibrary() and other_is_static) {
1207 // Avoid putting a static library inside a static library.
1208 break :l;
12491209 }
1250 }
1251 },
1252 }
1253 },
1254 .assembly_file => |asm_file| l: {
1255 if (!my_responsibility) break :l;
12561210
1257 if (prev_has_cflags) {
1258 try zig_args.append("-cflags");
1259 try zig_args.append("--");
1260 prev_has_cflags = false;
1261 }
1262 try zig_args.append(asm_file.getPath2(dep.module.owner, step));
1263 total_linker_objects += 1;
1264 },
1211 // For DLLs, we must link against the implib.
1212 // For everything else, we directly link
1213 // against the library file.
1214 const full_path_lib = if (other_produces_implib)
1215 other.getGeneratedFilePath("generated_implib", &compile.step)
1216 else
1217 other.getGeneratedFilePath("generated_bin", &compile.step);
12651218
1266 .c_source_file => |c_source_file| l: {
1267 if (!my_responsibility) break :l;
1219 try zig_args.append(full_path_lib);
1220 total_linker_objects += 1;
1221
1222 if (other.linkage == .dynamic and
1223 compile.rootModuleTarget().os.tag != .windows)
1224 {
1225 if (fs.path.dirname(full_path_lib)) |dirname| {
1226 try zig_args.append("-rpath");
1227 try zig_args.append(dirname);
1228 }
1229 }
1230 },
1231 }
1232 },
1233 .assembly_file => |asm_file| l: {
1234 if (!my_responsibility) break :l;
12681235
1269 if (c_source_file.flags.len == 0) {
12701236 if (prev_has_cflags) {
12711237 try zig_args.append("-cflags");
12721238 try zig_args.append("--");
12731239 prev_has_cflags = false;
12741240 }
1275 } else {
1276 try zig_args.append("-cflags");
1277 for (c_source_file.flags) |arg| {
1278 try zig_args.append(arg);
1279 }
1280 try zig_args.append("--");
1281 prev_has_cflags = true;
1282 }
1283 try zig_args.append(c_source_file.file.getPath2(dep.module.owner, step));
1284 total_linker_objects += 1;
1285 },
1241 try zig_args.append(asm_file.getPath2(mod.owner, step));
1242 total_linker_objects += 1;
1243 },
12861244
1287 .c_source_files => |c_source_files| l: {
1288 if (!my_responsibility) break :l;
1245 .c_source_file => |c_source_file| l: {
1246 if (!my_responsibility) break :l;
12891247
1290 if (c_source_files.flags.len == 0) {
1291 if (prev_has_cflags) {
1248 if (c_source_file.flags.len == 0) {
1249 if (prev_has_cflags) {
1250 try zig_args.append("-cflags");
1251 try zig_args.append("--");
1252 prev_has_cflags = false;
1253 }
1254 } else {
12921255 try zig_args.append("-cflags");
1256 for (c_source_file.flags) |arg| {
1257 try zig_args.append(arg);
1258 }
12931259 try zig_args.append("--");
1294 prev_has_cflags = false;
1260 prev_has_cflags = true;
12951261 }
1296 } else {
1297 try zig_args.append("-cflags");
1298 for (c_source_files.flags) |flag| {
1299 try zig_args.append(flag);
1262 try zig_args.append(c_source_file.file.getPath2(mod.owner, step));
1263 total_linker_objects += 1;
1264 },
1265
1266 .c_source_files => |c_source_files| l: {
1267 if (!my_responsibility) break :l;
1268
1269 if (c_source_files.flags.len == 0) {
1270 if (prev_has_cflags) {
1271 try zig_args.append("-cflags");
1272 try zig_args.append("--");
1273 prev_has_cflags = false;
1274 }
1275 } else {
1276 try zig_args.append("-cflags");
1277 for (c_source_files.flags) |flag| {
1278 try zig_args.append(flag);
1279 }
1280 try zig_args.append("--");
1281 prev_has_cflags = true;
13001282 }
1301 try zig_args.append("--");
1302 prev_has_cflags = true;
1303 }
13041283
1305 const root_path = c_source_files.root.getPath2(dep.module.owner, step);
1306 for (c_source_files.files) |file| {
1307 try zig_args.append(b.pathJoin(&.{ root_path, file }));
1308 }
1284 const root_path = c_source_files.root.getPath2(mod.owner, step);
1285 for (c_source_files.files) |file| {
1286 try zig_args.append(b.pathJoin(&.{ root_path, file }));
1287 }
13091288
1310 total_linker_objects += c_source_files.files.len;
1311 },
1289 total_linker_objects += c_source_files.files.len;
1290 },
13121291
1313 .win32_resource_file => |rc_source_file| l: {
1314 if (!my_responsibility) break :l;
1292 .win32_resource_file => |rc_source_file| l: {
1293 if (!my_responsibility) break :l;
13151294
1316 if (rc_source_file.flags.len == 0 and rc_source_file.include_paths.len == 0) {
1317 if (prev_has_rcflags) {
1295 if (rc_source_file.flags.len == 0 and rc_source_file.include_paths.len == 0) {
1296 if (prev_has_rcflags) {
1297 try zig_args.append("-rcflags");
1298 try zig_args.append("--");
1299 prev_has_rcflags = false;
1300 }
1301 } else {
13181302 try zig_args.append("-rcflags");
1303 for (rc_source_file.flags) |arg| {
1304 try zig_args.append(arg);
1305 }
1306 for (rc_source_file.include_paths) |include_path| {
1307 try zig_args.append("/I");
1308 try zig_args.append(include_path.getPath2(mod.owner, step));
1309 }
13191310 try zig_args.append("--");
1320 prev_has_rcflags = false;
1321 }
1322 } else {
1323 try zig_args.append("-rcflags");
1324 for (rc_source_file.flags) |arg| {
1325 try zig_args.append(arg);
1326 }
1327 for (rc_source_file.include_paths) |include_path| {
1328 try zig_args.append("/I");
1329 try zig_args.append(include_path.getPath2(dep.module.owner, step));
1311 prev_has_rcflags = true;
13301312 }
1331 try zig_args.append("--");
1332 prev_has_rcflags = true;
1333 }
1334 try zig_args.append(rc_source_file.file.getPath2(dep.module.owner, step));
1335 total_linker_objects += 1;
1336 },
1313 try zig_args.append(rc_source_file.file.getPath2(mod.owner, step));
1314 total_linker_objects += 1;
1315 },
1316 }
13371317 }
1338 }
13391318
1340 // We need to emit the --mod argument here so that the above link objects
1341 // have the correct parent module, but only if the module is part of
1342 // this compilation.
1343 if (!my_responsibility) continue;
1344 if (cli_named_modules.modules.getIndex(dep.module)) |module_cli_index| {
1345 const module_cli_name = cli_named_modules.names.keys()[module_cli_index];
1346 try dep.module.appendZigProcessFlags(&zig_args, step);
1347
1348 // --dep arguments
1349 try zig_args.ensureUnusedCapacity(dep.module.import_table.count() * 2);
1350 for (dep.module.import_table.keys(), dep.module.import_table.values()) |name, import| {
1351 const import_index = cli_named_modules.modules.getIndex(import).?;
1352 const import_cli_name = cli_named_modules.names.keys()[import_index];
1353 zig_args.appendAssumeCapacity("--dep");
1354 if (std.mem.eql(u8, import_cli_name, name)) {
1355 zig_args.appendAssumeCapacity(import_cli_name);
1356 } else {
1357 zig_args.appendAssumeCapacity(b.fmt("{s}={s}", .{ name, import_cli_name }));
1319 // We need to emit the --mod argument here so that the above link objects
1320 // have the correct parent module, but only if the module is part of
1321 // this compilation.
1322 if (!my_responsibility) continue;
1323 if (cli_named_modules.modules.getIndex(mod)) |module_cli_index| {
1324 const module_cli_name = cli_named_modules.names.keys()[module_cli_index];
1325 try mod.appendZigProcessFlags(&zig_args, step);
1326
1327 // --dep arguments
1328 try zig_args.ensureUnusedCapacity(mod.import_table.count() * 2);
1329 for (mod.import_table.keys(), mod.import_table.values()) |name, import| {
1330 const import_index = cli_named_modules.modules.getIndex(import).?;
1331 const import_cli_name = cli_named_modules.names.keys()[import_index];
1332 zig_args.appendAssumeCapacity("--dep");
1333 if (std.mem.eql(u8, import_cli_name, name)) {
1334 zig_args.appendAssumeCapacity(import_cli_name);
1335 } else {
1336 zig_args.appendAssumeCapacity(b.fmt("{s}={s}", .{ name, import_cli_name }));
1337 }
13581338 }
1359 }
13601339
1361 // When the CLI sees a -M argument, it determines whether it
1362 // implies the existence of a Zig compilation unit based on
1363 // whether there is a root source file. If there is no root
1364 // source file, then this is not a zig compilation unit - it is
1365 // perhaps a set of linker objects, or C source files instead.
1366 // Linker objects are added to the CLI globally, while C source
1367 // files must have a module parent.
1368 if (dep.module.root_source_file) |lp| {
1369 const src = lp.getPath2(dep.module.owner, step);
1370 try zig_args.append(b.fmt("-M{s}={s}", .{ module_cli_name, src }));
1371 } else if (moduleNeedsCliArg(dep.module)) {
1372 try zig_args.append(b.fmt("-M{s}", .{module_cli_name}));
1340 // When the CLI sees a -M argument, it determines whether it
1341 // implies the existence of a Zig compilation unit based on
1342 // whether there is a root source file. If there is no root
1343 // source file, then this is not a zig compilation unit - it is
1344 // perhaps a set of linker objects, or C source files instead.
1345 // Linker objects are added to the CLI globally, while C source
1346 // files must have a module parent.
1347 if (mod.root_source_file) |lp| {
1348 const src = lp.getPath2(mod.owner, step);
1349 try zig_args.append(b.fmt("-M{s}={s}", .{ module_cli_name, src }));
1350 } else if (moduleNeedsCliArg(mod)) {
1351 try zig_args.append(b.fmt("-M{s}", .{module_cli_name}));
1352 }
13731353 }
13741354 }
13751355 }
......@@ -2081,3 +2061,35 @@ fn moduleNeedsCliArg(mod: *const Module) bool {
20812061 else => continue,
20822062 } else false;
20832063}
2064
2065/// Return the full set of `Step.Compile` which `start` depends on, recursively. `start` itself is
2066/// always returned as the first element. If `chase_dynamic` is `false`, then dynamic libraries are
2067/// not included, and their dependencies are not considered; if `chase_dynamic` is `true`, dynamic
2068/// libraries are treated the same as other linked `Compile`s.
2069pub fn getCompileDependencies(start: *Compile, chase_dynamic: bool) []const *Compile {
2070 const arena = start.step.owner.graph.arena;
2071
2072 var compiles: std.AutoArrayHashMapUnmanaged(*Compile, void) = .empty;
2073 var next_idx: usize = 0;
2074
2075 compiles.putNoClobber(arena, start, {}) catch @panic("OOM");
2076
2077 while (next_idx < compiles.count()) {
2078 const compile = compiles.keys()[next_idx];
2079 next_idx += 1;
2080
2081 for (compile.root_module.getGraph().modules) |mod| {
2082 for (mod.link_objects.items) |lo| {
2083 switch (lo) {
2084 .other_step => |other_compile| {
2085 if (!chase_dynamic and other_compile.isDynamicLibrary()) continue;
2086 compiles.put(arena, other_compile, {}) catch @panic("OOM");
2087 },
2088 else => {},
2089 }
2090 }
2091 }
2092 }
2093
2094 return compiles.keys();
2095}
lib/std/Build/Step/ObjCopy.zig-3
......@@ -135,9 +135,6 @@ pub fn create(
135135 return objcopy;
136136}
137137
138/// deprecated: use getOutput
139pub const getOutputSource = getOutput;
140
141138pub fn getOutput(objcopy: *const ObjCopy) std.Build.LazyPath {
142139 return .{ .generated = .{ .file = &objcopy.output_file } };
143140}
lib/std/Build/Step/Options.zig-8
......@@ -390,20 +390,12 @@ pub fn addOptionPath(
390390 path.addStepDependencies(&options.step);
391391}
392392
393/// Deprecated: use `addOptionPath(options, name, artifact.getEmittedBin())` instead.
394pub fn addOptionArtifact(options: *Options, name: []const u8, artifact: *Step.Compile) void {
395 return addOptionPath(options, name, artifact.getEmittedBin());
396}
397
398393pub fn createModule(options: *Options) *std.Build.Module {
399394 return options.step.owner.createModule(.{
400395 .root_source_file = options.getOutput(),
401396 });
402397}
403398
404/// deprecated: use `getOutput`
405pub const getSource = getOutput;
406
407399/// Returns the main artifact of this Build Step which is a Zig source file
408400/// generated from the key-value pairs of the Options.
409401pub fn getOutput(options: *Options) LazyPath {
lib/std/Build/Step/Run.zig+6-22
......@@ -43,9 +43,6 @@ stdio: StdIo,
4343/// It should be only set using `setStdIn`.
4444stdin: StdIn,
4545
46/// Deprecated: use `addFileInput`
47extra_file_dependencies: []const []const u8,
48
4946/// Additional input files that, when modified, indicate that the Run step
5047/// should be re-executed.
5148/// If the Run step is determined to have side-effects, the Run step is always
......@@ -178,7 +175,6 @@ pub fn create(owner: *std.Build, name: []const u8) *Run {
178175 .disable_zig_progress = false,
179176 .stdio = .infer_from_args,
180177 .stdin = .none,
181 .extra_file_dependencies = &.{},
182178 .file_inputs = .{},
183179 .rename_step_with_output_arg = true,
184180 .skip_foreign_checks = false,
......@@ -364,16 +360,10 @@ pub fn addPrefixedOutputDirectoryArg(
364360 return .{ .generated = .{ .file = &output.generated_file } };
365361}
366362
367/// deprecated: use `addDirectoryArg`
368pub const addDirectorySourceArg = addDirectoryArg;
369
370363pub fn addDirectoryArg(run: *Run, directory_source: std.Build.LazyPath) void {
371364 run.addPrefixedDirectoryArg("", directory_source);
372365}
373366
374// deprecated: use `addPrefixedDirectoryArg`
375pub const addPrefixedDirectorySourceArg = addPrefixedDirectoryArg;
376
377367pub fn addPrefixedDirectoryArg(run: *Run, prefix: []const u8, directory_source: std.Build.LazyPath) void {
378368 const b = run.step.owner;
379369
......@@ -698,9 +688,6 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
698688
699689 hashStdIo(&man.hash, run.stdio);
700690
701 for (run.extra_file_dependencies) |file_path| {
702 _ = try man.addFile(b.pathFromRoot(file_path), null);
703 }
704691 for (run.file_inputs.items) |lazy_path| {
705692 _ = try man.addFile(lazy_path.getPath2(b, step), null);
706693 }
......@@ -1732,15 +1719,12 @@ fn evalGeneric(run: *Run, child: *std.process.Child) !StdIoResult {
17321719
17331720fn addPathForDynLibs(run: *Run, artifact: *Step.Compile) void {
17341721 const b = run.step.owner;
1735 var it = artifact.root_module.iterateDependencies(artifact, true);
1736 while (it.next()) |item| {
1737 const other = item.compile.?;
1738 if (item.module == &other.root_module) {
1739 if (item.module.resolved_target.?.result.os.tag == .windows and
1740 other.isDynamicLibrary())
1741 {
1742 addPathDir(run, fs.path.dirname(other.getEmittedBin().getPath2(b, &run.step)).?);
1743 }
1722 const compiles = artifact.getCompileDependencies(true);
1723 for (compiles) |compile| {
1724 if (compile.root_module.resolved_target.?.result.os.tag == .windows and
1725 compile.isDynamicLibrary())
1726 {
1727 addPathDir(run, fs.path.dirname(compile.getEmittedBin().getPath2(b, &run.step)).?);
17441728 }
17451729 }
17461730}
lib/std/Build/Step/TranslateC.zig+4
......@@ -63,6 +63,7 @@ pub fn getOutput(translate_c: *TranslateC) std.Build.LazyPath {
6363 return .{ .generated = .{ .file = &translate_c.output_file } };
6464}
6565
66/// Deprecated: use `createModule` or `addModule` with `std.Build.addExecutable` instead.
6667/// Creates a step to build an executable from the translated source.
6768pub fn addExecutable(translate_c: *TranslateC, options: AddExecutableOptions) *Step.Compile {
6869 return translate_c.step.owner.addExecutable(.{
......@@ -81,6 +82,9 @@ pub fn addExecutable(translate_c: *TranslateC, options: AddExecutableOptions) *S
8182pub fn addModule(translate_c: *TranslateC, name: []const u8) *std.Build.Module {
8283 return translate_c.step.owner.addModule(name, .{
8384 .root_source_file = translate_c.getOutput(),
85 .target = translate_c.target,
86 .optimize = translate_c.optimize,
87 .link_libc = translate_c.link_libc,
8488 });
8589}
8690
test/link/bss/build.zig+5-3
......@@ -6,9 +6,11 @@ pub fn build(b: *std.Build) void {
66
77 const exe = b.addExecutable(.{
88 .name = "bss",
9 .root_source_file = b.path("main.zig"),
10 .target = b.graph.host,
11 .optimize = .Debug,
9 .root_module = b.createModule(.{
10 .root_source_file = b.path("main.zig"),
11 .target = b.graph.host,
12 .optimize = .Debug,
13 }),
1214 });
1315
1416 const run = b.addRunArtifact(exe);
test/link/common_symbols/build.zig+11-6
......@@ -13,19 +13,24 @@ pub fn build(b: *std.Build) void {
1313fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
1414 const lib_a = b.addStaticLibrary(.{
1515 .name = "a",
16 .optimize = optimize,
17 .target = b.graph.host,
16 .root_module = b.createModule(.{
17 .root_source_file = null,
18 .optimize = optimize,
19 .target = b.graph.host,
20 }),
1821 });
19 lib_a.addCSourceFiles(.{
22 lib_a.root_module.addCSourceFiles(.{
2023 .files = &.{ "c.c", "a.c", "b.c" },
2124 .flags = &.{"-fcommon"},
2225 });
2326
2427 const test_exe = b.addTest(.{
25 .root_source_file = b.path("main.zig"),
26 .optimize = optimize,
28 .root_module = b.createModule(.{
29 .root_source_file = b.path("main.zig"),
30 .optimize = optimize,
31 }),
2732 });
28 test_exe.linkLibrary(lib_a);
33 test_exe.root_module.linkLibrary(lib_a);
2934
3035 test_step.dependOn(&b.addRunArtifact(test_exe).step);
3136}
test/link/common_symbols_alignment/build.zig+12-6
......@@ -13,19 +13,25 @@ pub fn build(b: *std.Build) void {
1313fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
1414 const lib_a = b.addStaticLibrary(.{
1515 .name = "a",
16 .optimize = optimize,
17 .target = b.graph.host,
16 .root_module = b.createModule(.{
17 .root_source_file = null,
18 .optimize = optimize,
19 .target = b.graph.host,
20 }),
1821 });
19 lib_a.addCSourceFiles(.{
22 lib_a.root_module.addCSourceFiles(.{
2023 .files = &.{"a.c"},
2124 .flags = &.{"-fcommon"},
2225 });
2326
2427 const test_exe = b.addTest(.{
25 .root_source_file = b.path("main.zig"),
26 .optimize = optimize,
28 .root_module = b.createModule(.{
29 .root_source_file = b.path("main.zig"),
30 .target = b.graph.host,
31 .optimize = optimize,
32 }),
2733 });
28 test_exe.linkLibrary(lib_a);
34 test_exe.root_module.linkLibrary(lib_a);
2935
3036 test_step.dependOn(&b.addRunArtifact(test_exe).step);
3137}
test/link/glibc_compat/build.zig+19-11
......@@ -14,12 +14,15 @@ pub fn build(b: *std.Build) void {
1414 for ([_][]const u8{ "aarch64-linux-gnu.2.27", "aarch64-linux-gnu.2.34" }) |t| {
1515 const exe = b.addExecutable(.{
1616 .name = t,
17 .target = b.resolveTargetQuery(std.Target.Query.parse(
18 .{ .arch_os_abi = t },
19 ) catch unreachable),
17 .root_module = b.createModule(.{
18 .root_source_file = null,
19 .target = b.resolveTargetQuery(std.Target.Query.parse(
20 .{ .arch_os_abi = t },
21 ) catch unreachable),
22 .link_libc = true,
23 }),
2024 });
21 exe.addCSourceFile(.{ .file = b.path("main.c") });
22 exe.linkLibC();
25 exe.root_module.addCSourceFile(.{ .file = b.path("main.c") });
2326 // TODO: actually test the output
2427 _ = exe.getEmittedBin();
2528 test_step.dependOn(&exe.step);
......@@ -53,10 +56,13 @@ pub fn build(b: *std.Build) void {
5356
5457 const exe = b.addExecutable(.{
5558 .name = t,
56 .target = target,
59 .root_module = b.createModule(.{
60 .root_source_file = null,
61 .target = target,
62 .link_libc = true,
63 }),
5764 });
58 exe.addCSourceFile(.{ .file = b.path("glibc_runtime_check.c") });
59 exe.linkLibC();
65 exe.root_module.addCSourceFile(.{ .file = b.path("glibc_runtime_check.c") });
6066
6167 // Only try running the test if the host glibc is known to be good enough. Ideally, the Zig
6268 // test runner would be able to check this, but see https://github.com/ziglang/zig/pull/17702#issuecomment-1831310453
......@@ -149,10 +155,12 @@ pub fn build(b: *std.Build) void {
149155
150156 const exe = b.addExecutable(.{
151157 .name = t,
152 .root_source_file = b.path("glibc_runtime_check.zig"),
153 .target = target,
158 .root_module = b.createModule(.{
159 .root_source_file = b.path("glibc_runtime_check.zig"),
160 .target = target,
161 .link_libc = true,
162 }),
154163 });
155 exe.linkLibC();
156164
157165 // Only try running the test if the host glibc is known to be good enough. Ideally, the Zig
158166 // test runner would be able to check this, but see https://github.com/ziglang/zig/pull/17702#issuecomment-1831310453
test/link/interdependent_static_c_libs/build.zig+22-13
......@@ -13,27 +13,36 @@ pub fn build(b: *std.Build) void {
1313fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
1414 const lib_a = b.addStaticLibrary(.{
1515 .name = "a",
16 .optimize = optimize,
17 .target = b.graph.host,
16 .root_module = b.createModule(.{
17 .root_source_file = null,
18 .optimize = optimize,
19 .target = b.graph.host,
20 }),
1821 });
19 lib_a.addCSourceFile(.{ .file = b.path("a.c"), .flags = &[_][]const u8{} });
20 lib_a.addIncludePath(b.path("."));
22 lib_a.root_module.addCSourceFile(.{ .file = b.path("a.c"), .flags = &[_][]const u8{} });
23 lib_a.root_module.addIncludePath(b.path("."));
2124
2225 const lib_b = b.addStaticLibrary(.{
2326 .name = "b",
24 .optimize = optimize,
25 .target = b.graph.host,
27 .root_module = b.createModule(.{
28 .root_source_file = null,
29 .optimize = optimize,
30 .target = b.graph.host,
31 }),
2632 });
27 lib_b.addCSourceFile(.{ .file = b.path("b.c"), .flags = &[_][]const u8{} });
28 lib_b.addIncludePath(b.path("."));
33 lib_b.root_module.addCSourceFile(.{ .file = b.path("b.c"), .flags = &[_][]const u8{} });
34 lib_b.root_module.addIncludePath(b.path("."));
2935
3036 const test_exe = b.addTest(.{
31 .root_source_file = b.path("main.zig"),
32 .optimize = optimize,
37 .root_module = b.createModule(.{
38 .root_source_file = b.path("main.zig"),
39 .target = b.graph.host,
40 .optimize = optimize,
41 }),
3342 });
34 test_exe.linkLibrary(lib_a);
35 test_exe.linkLibrary(lib_b);
36 test_exe.addIncludePath(b.path("."));
43 test_exe.root_module.linkLibrary(lib_a);
44 test_exe.root_module.linkLibrary(lib_b);
45 test_exe.root_module.addIncludePath(b.path("."));
3746
3847 test_step.dependOn(&b.addRunArtifact(test_exe).step);
3948}
test/link/link.zig+47-43
......@@ -47,81 +47,85 @@ const OverlayOptions = struct {
4747};
4848
4949pub fn addExecutable(b: *std.Build, base: Options, overlay: OverlayOptions) *Compile {
50 return addCompileStep(b, base, overlay, .exe);
50 return b.addExecutable(.{
51 .name = overlay.name,
52 .root_module = createModule(b, base, overlay),
53 .use_llvm = base.use_llvm,
54 .use_lld = base.use_lld,
55 });
5156}
5257
5358pub fn addObject(b: *Build, base: Options, overlay: OverlayOptions) *Compile {
54 return addCompileStep(b, base, overlay, .obj);
59 return b.addObject(.{
60 .name = overlay.name,
61 .root_module = createModule(b, base, overlay),
62 .use_llvm = base.use_llvm,
63 .use_lld = base.use_lld,
64 });
5565}
5666
5767pub fn addStaticLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Compile {
58 return addCompileStep(b, base, overlay, .static_lib);
68 return b.addStaticLibrary(.{
69 .name = overlay.name,
70 .root_module = createModule(b, base, overlay),
71 .use_llvm = base.use_llvm,
72 .use_lld = base.use_lld,
73 });
5974}
6075
6176pub fn addSharedLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Compile {
62 return addCompileStep(b, base, overlay, .shared_lib);
63}
64
65fn addCompileStep(
66 b: *Build,
67 base: Options,
68 overlay: OverlayOptions,
69 kind: enum { exe, obj, shared_lib, static_lib },
70) *Compile {
71 const compile_step = Compile.create(b, .{
77 return b.addSharedLibrary(.{
7278 .name = overlay.name,
73 .root_module = .{
74 .target = base.target,
75 .optimize = base.optimize,
76 .root_source_file = rsf: {
77 const bytes = overlay.zig_source_bytes orelse break :rsf null;
78 const name = b.fmt("{s}.zig", .{overlay.name});
79 break :rsf b.addWriteFiles().add(name, bytes);
80 },
81 .pic = overlay.pic,
82 .strip = if (base.strip) |s| s else overlay.strip,
83 },
79 .root_module = createModule(b, base, overlay),
8480 .use_llvm = base.use_llvm,
8581 .use_lld = base.use_lld,
86 .kind = switch (kind) {
87 .exe => .exe,
88 .obj => .obj,
89 .shared_lib, .static_lib => .lib,
90 },
91 .linkage = switch (kind) {
92 .exe, .obj => null,
93 .shared_lib => .dynamic,
94 .static_lib => .static,
82 });
83}
84
85fn createModule(b: *Build, base: Options, overlay: OverlayOptions) *Build.Module {
86 const write_files = b.addWriteFiles();
87
88 const mod = b.createModule(.{
89 .target = base.target,
90 .optimize = base.optimize,
91 .root_source_file = rsf: {
92 const bytes = overlay.zig_source_bytes orelse break :rsf null;
93 const name = b.fmt("{s}.zig", .{overlay.name});
94 break :rsf write_files.add(name, bytes);
9595 },
96 .pic = overlay.pic,
97 .strip = if (base.strip) |s| s else overlay.strip,
9698 });
99
97100 if (overlay.objcpp_source_bytes) |bytes| {
98 compile_step.addCSourceFile(.{
99 .file = b.addWriteFiles().add("a.mm", bytes),
101 mod.addCSourceFile(.{
102 .file = write_files.add("a.mm", bytes),
100103 .flags = overlay.objcpp_source_flags,
101104 });
102105 }
103106 if (overlay.objc_source_bytes) |bytes| {
104 compile_step.addCSourceFile(.{
105 .file = b.addWriteFiles().add("a.m", bytes),
107 mod.addCSourceFile(.{
108 .file = write_files.add("a.m", bytes),
106109 .flags = overlay.objc_source_flags,
107110 });
108111 }
109112 if (overlay.cpp_source_bytes) |bytes| {
110 compile_step.addCSourceFile(.{
111 .file = b.addWriteFiles().add("a.cpp", bytes),
113 mod.addCSourceFile(.{
114 .file = write_files.add("a.cpp", bytes),
112115 .flags = overlay.cpp_source_flags,
113116 });
114117 }
115118 if (overlay.c_source_bytes) |bytes| {
116 compile_step.addCSourceFile(.{
117 .file = b.addWriteFiles().add("a.c", bytes),
119 mod.addCSourceFile(.{
120 .file = write_files.add("a.c", bytes),
118121 .flags = overlay.c_source_flags,
119122 });
120123 }
121124 if (overlay.asm_source_bytes) |bytes| {
122 compile_step.addAssemblyFile(b.addWriteFiles().add("a.s", bytes));
125 mod.addAssemblyFile(write_files.add("a.s", bytes));
123126 }
124 return compile_step;
127
128 return mod;
125129}
126130
127131pub fn addRunArtifact(comp: *Compile) *Run {
test/link/static_libs_from_object_files/build.zig+47-38
......@@ -57,13 +57,15 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built
5757 {
5858 const exe = b.addExecutable(.{
5959 .name = "test1",
60 .root_source_file = b.path("main.zig"),
61 .optimize = optimize,
62 .target = b.graph.host,
60 .root_module = b.createModule(.{
61 .root_source_file = b.path("main.zig"),
62 .optimize = optimize,
63 .target = b.graph.host,
64 }),
6365 });
6466
6567 for (files) |file| {
66 exe.addCSourceFile(.{ .file = file, .flags = &flags });
68 exe.root_module.addCSourceFile(.{ .file = file, .flags = &flags });
6769 }
6870
6971 const run_cmd = b.addRunArtifact(exe);
......@@ -75,30 +77,33 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built
7577
7678 // using static librairies
7779 {
80 const mod_a = b.createModule(.{ .target = b.graph.host, .optimize = optimize });
81 const mod_b = b.createModule(.{ .target = b.graph.host, .optimize = optimize });
82
83 for (files, 1..) |file, i| {
84 const mod = if (i & 1 == 0) mod_a else mod_b;
85 mod.addCSourceFile(.{ .file = file, .flags = &flags });
86 }
87
7888 const lib_a = b.addStaticLibrary(.{
7989 .name = "test2_a",
80 .target = b.graph.host,
81 .optimize = optimize,
90 .root_module = mod_a,
8291 });
8392 const lib_b = b.addStaticLibrary(.{
8493 .name = "test2_b",
85 .target = b.graph.host,
86 .optimize = optimize,
94 .root_module = mod_b,
8795 });
8896
89 for (files, 1..) |file, i| {
90 const lib = if (i & 1 == 0) lib_a else lib_b;
91 lib.addCSourceFile(.{ .file = file, .flags = &flags });
92 }
93
9497 const exe = b.addExecutable(.{
9598 .name = "test2",
96 .root_source_file = b.path("main.zig"),
97 .target = b.graph.host,
98 .optimize = optimize,
99 .root_module = b.createModule(.{
100 .root_source_file = b.path("main.zig"),
101 .target = b.graph.host,
102 .optimize = optimize,
103 }),
99104 });
100 exe.linkLibrary(lib_a);
101 exe.linkLibrary(lib_b);
105 exe.root_module.linkLibrary(lib_a);
106 exe.root_module.linkLibrary(lib_b);
102107
103108 const run_cmd = b.addRunArtifact(exe);
104109 run_cmd.skip_foreign_checks = true;
......@@ -109,37 +114,41 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built
109114
110115 // using static librairies and object files
111116 {
112 const lib_a = b.addStaticLibrary(.{
113 .name = "test3_a",
114 .target = b.graph.host,
115 .optimize = optimize,
116 });
117 const lib_b = b.addStaticLibrary(.{
118 .name = "test3_b",
119 .target = b.graph.host,
120 .optimize = optimize,
121 });
117 const mod_a = b.createModule(.{ .target = b.graph.host, .optimize = optimize });
118 const mod_b = b.createModule(.{ .target = b.graph.host, .optimize = optimize });
122119
123120 for (files, 1..) |file, i| {
121 const obj_mod = b.createModule(.{ .target = b.graph.host, .optimize = optimize });
122 obj_mod.addCSourceFile(.{ .file = file, .flags = &flags });
123
124124 const obj = b.addObject(.{
125125 .name = b.fmt("obj_{}", .{i}),
126 .target = b.graph.host,
127 .optimize = optimize,
126 .root_module = obj_mod,
128127 });
129 obj.addCSourceFile(.{ .file = file, .flags = &flags });
130128
131 const lib = if (i & 1 == 0) lib_a else lib_b;
132 lib.addObject(obj);
129 const lib_mod = if (i & 1 == 0) mod_a else mod_b;
130 lib_mod.addObject(obj);
133131 }
134132
133 const lib_a = b.addStaticLibrary(.{
134 .name = "test3_a",
135 .root_module = mod_a,
136 });
137 const lib_b = b.addStaticLibrary(.{
138 .name = "test3_b",
139 .root_module = mod_b,
140 });
141
135142 const exe = b.addExecutable(.{
136143 .name = "test3",
137 .root_source_file = b.path("main.zig"),
138 .target = b.graph.host,
139 .optimize = optimize,
144 .root_module = b.createModule(.{
145 .root_source_file = b.path("main.zig"),
146 .target = b.graph.host,
147 .optimize = optimize,
148 }),
140149 });
141 exe.linkLibrary(lib_a);
142 exe.linkLibrary(lib_b);
150 exe.root_module.linkLibrary(lib_a);
151 exe.root_module.linkLibrary(lib_b);
143152
144153 const run_cmd = b.addRunArtifact(exe);
145154 run_cmd.skip_foreign_checks = true;
test/link/wasm/archive/build.zig+6-4
......@@ -17,10 +17,12 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1717 // and therefore link with its archive file.
1818 const lib = b.addExecutable(.{
1919 .name = "main",
20 .root_source_file = b.path("main.zig"),
21 .optimize = optimize,
22 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
23 .strip = false,
20 .root_module = b.createModule(.{
21 .root_source_file = b.path("main.zig"),
22 .optimize = optimize,
23 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
24 .strip = false,
25 }),
2426 });
2527 lib.entry = .disabled;
2628 lib.use_llvm = false;
test/link/wasm/basic-features/build.zig+9-7
......@@ -6,13 +6,15 @@ pub fn build(b: *std.Build) void {
66 // Library with explicitly set cpu features
77 const lib = b.addExecutable(.{
88 .name = "lib",
9 .root_source_file = b.path("main.zig"),
10 .optimize = .Debug,
11 .target = b.resolveTargetQuery(.{
12 .cpu_arch = .wasm32,
13 .cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp },
14 .cpu_features_add = std.Target.wasm.featureSet(&.{.atomics}),
15 .os_tag = .freestanding,
9 .root_module = b.createModule(.{
10 .root_source_file = b.path("main.zig"),
11 .optimize = .Debug,
12 .target = b.resolveTargetQuery(.{
13 .cpu_arch = .wasm32,
14 .cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp },
15 .cpu_features_add = std.Target.wasm.featureSet(&.{.atomics}),
16 .os_tag = .freestanding,
17 }),
1618 }),
1719 });
1820 lib.entry = .disabled;
test/link/wasm/bss/build.zig+12-8
......@@ -16,10 +16,12 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt
1616 {
1717 const lib = b.addExecutable(.{
1818 .name = "lib",
19 .root_source_file = b.path("lib.zig"),
20 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
21 .optimize = optimize_mode,
22 .strip = false,
19 .root_module = b.createModule(.{
20 .root_source_file = b.path("lib.zig"),
21 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
22 .optimize = optimize_mode,
23 .strip = false,
24 }),
2325 });
2426 lib.entry = .disabled;
2527 lib.use_llvm = false;
......@@ -64,10 +66,12 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt
6466 {
6567 const lib = b.addExecutable(.{
6668 .name = "lib",
67 .root_source_file = b.path("lib2.zig"),
68 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
69 .optimize = optimize_mode,
70 .strip = false,
69 .root_module = b.createModule(.{
70 .root_source_file = b.path("lib2.zig"),
71 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
72 .optimize = optimize_mode,
73 .strip = false,
74 }),
7175 });
7276 lib.entry = .disabled;
7377 lib.use_llvm = false;
test/link/wasm/export-data/build.zig+5-3
......@@ -11,9 +11,11 @@ pub fn build(b: *std.Build) void {
1111
1212 const lib = b.addExecutable(.{
1313 .name = "lib",
14 .root_source_file = b.path("lib.zig"),
15 .optimize = .ReleaseSafe, // to make the output deterministic in address positions
16 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
14 .root_module = b.createModule(.{
15 .root_source_file = b.path("lib.zig"),
16 .optimize = .ReleaseSafe, // to make the output deterministic in address positions
17 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
18 }),
1719 });
1820 lib.entry = .disabled;
1921 lib.use_lld = false;
test/link/wasm/export/build.zig+15-9
......@@ -15,9 +15,11 @@ pub fn build(b: *std.Build) void {
1515fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
1616 const no_export = b.addExecutable(.{
1717 .name = "no-export",
18 .root_source_file = b.path("main.zig"),
19 .optimize = optimize,
20 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
18 .root_module = b.createModule(.{
19 .root_source_file = b.path("main.zig"),
20 .optimize = optimize,
21 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
22 }),
2123 });
2224 no_export.entry = .disabled;
2325 no_export.use_llvm = false;
......@@ -25,9 +27,11 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2527
2628 const dynamic_export = b.addExecutable(.{
2729 .name = "dynamic",
28 .root_source_file = b.path("main.zig"),
29 .optimize = optimize,
30 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
30 .root_module = b.createModule(.{
31 .root_source_file = b.path("main.zig"),
32 .optimize = optimize,
33 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
34 }),
3135 });
3236 dynamic_export.entry = .disabled;
3337 dynamic_export.rdynamic = true;
......@@ -36,9 +40,11 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
3640
3741 const force_export = b.addExecutable(.{
3842 .name = "force",
39 .root_source_file = b.path("main.zig"),
40 .optimize = optimize,
41 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
43 .root_module = b.createModule(.{
44 .root_source_file = b.path("main.zig"),
45 .optimize = optimize,
46 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
47 }),
4248 });
4349 force_export.entry = .disabled;
4450 force_export.root_module.export_symbol_names = &.{"foo"};
test/link/wasm/extern-mangle/build.zig+5-3
......@@ -13,9 +13,11 @@ pub fn build(b: *std.Build) void {
1313fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
1414 const lib = b.addExecutable(.{
1515 .name = "lib",
16 .root_source_file = b.path("lib.zig"),
17 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
18 .optimize = optimize,
16 .root_module = b.createModule(.{
17 .root_source_file = b.path("lib.zig"),
18 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
19 .optimize = optimize,
20 }),
1921 });
2022 lib.entry = .disabled;
2123 lib.import_symbols = true; // import `a` and `b`
test/link/wasm/extern/build.zig+5-3
......@@ -15,9 +15,11 @@ pub fn build(b: *std.Build) void {
1515fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
1616 const exe = b.addExecutable(.{
1717 .name = "extern",
18 .root_source_file = b.path("main.zig"),
19 .optimize = optimize,
20 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .wasi }),
18 .root_module = b.createModule(.{
19 .root_source_file = b.path("main.zig"),
20 .optimize = optimize,
21 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .wasi }),
22 }),
2123 });
2224 exe.addCSourceFile(.{ .file = b.path("foo.c"), .flags = &.{} });
2325 exe.use_llvm = false;
test/link/wasm/function-table/build.zig+15-9
......@@ -15,9 +15,11 @@ pub fn build(b: *std.Build) void {
1515fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
1616 const import_table = b.addExecutable(.{
1717 .name = "import_table",
18 .root_source_file = b.path("lib.zig"),
19 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
20 .optimize = optimize,
18 .root_module = b.createModule(.{
19 .root_source_file = b.path("lib.zig"),
20 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
21 .optimize = optimize,
22 }),
2123 });
2224 import_table.entry = .disabled;
2325 import_table.use_llvm = false;
......@@ -27,9 +29,11 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2729
2830 const export_table = b.addExecutable(.{
2931 .name = "export_table",
30 .root_source_file = b.path("lib.zig"),
31 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
32 .optimize = optimize,
32 .root_module = b.createModule(.{
33 .root_source_file = b.path("lib.zig"),
34 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
35 .optimize = optimize,
36 }),
3337 });
3438 export_table.entry = .disabled;
3539 export_table.use_llvm = false;
......@@ -39,9 +43,11 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
3943
4044 const regular_table = b.addExecutable(.{
4145 .name = "regular_table",
42 .root_source_file = b.path("lib.zig"),
43 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
44 .optimize = optimize,
46 .root_module = b.createModule(.{
47 .root_source_file = b.path("lib.zig"),
48 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
49 .optimize = optimize,
50 }),
4551 });
4652 regular_table.entry = .disabled;
4753 regular_table.use_llvm = false;
test/link/wasm/infer-features/build.zig+18-13
......@@ -6,31 +6,36 @@ pub fn build(b: *std.Build) void {
66 // Wasm Object file which we will use to infer the features from
77 const c_obj = b.addObject(.{
88 .name = "c_obj",
9 .optimize = .Debug,
10 .target = b.resolveTargetQuery(.{
11 .cpu_arch = .wasm32,
12 .cpu_model = .{ .explicit = &std.Target.wasm.cpu.bleeding_edge },
13 .os_tag = .freestanding,
9 .root_module = b.createModule(.{
10 .root_source_file = null,
11 .optimize = .Debug,
12 .target = b.resolveTargetQuery(.{
13 .cpu_arch = .wasm32,
14 .cpu_model = .{ .explicit = &std.Target.wasm.cpu.bleeding_edge },
15 .os_tag = .freestanding,
16 }),
1417 }),
1518 });
16 c_obj.addCSourceFile(.{ .file = b.path("foo.c"), .flags = &.{} });
19 c_obj.root_module.addCSourceFile(.{ .file = b.path("foo.c"), .flags = &.{} });
1720
1821 // Wasm library that doesn't have any features specified. This will
1922 // infer its featureset from other linked object files.
2023 const lib = b.addExecutable(.{
2124 .name = "lib",
22 .root_source_file = b.path("main.zig"),
23 .optimize = .Debug,
24 .target = b.resolveTargetQuery(.{
25 .cpu_arch = .wasm32,
26 .cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp },
27 .os_tag = .freestanding,
25 .root_module = b.createModule(.{
26 .root_source_file = b.path("main.zig"),
27 .optimize = .Debug,
28 .target = b.resolveTargetQuery(.{
29 .cpu_arch = .wasm32,
30 .cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp },
31 .os_tag = .freestanding,
32 }),
2833 }),
2934 });
3035 lib.entry = .disabled;
3136 lib.use_llvm = false;
3237 lib.use_lld = false;
33 lib.addObject(c_obj);
38 lib.root_module.addObject(c_obj);
3439
3540 // Verify the result contains the features from the C Object file.
3641 const check = lib.checkObject();
test/link/wasm/producers/build.zig+6-4
......@@ -16,10 +16,12 @@ pub fn build(b: *std.Build) void {
1616fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
1717 const lib = b.addExecutable(.{
1818 .name = "lib",
19 .root_source_file = b.path("lib.zig"),
20 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
21 .optimize = optimize,
22 .strip = false,
19 .root_module = b.createModule(.{
20 .root_source_file = b.path("lib.zig"),
21 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
22 .optimize = optimize,
23 .strip = false,
24 }),
2325 });
2426 lib.entry = .disabled;
2527 lib.use_llvm = false;
test/link/wasm/segments/build.zig+6-4
......@@ -15,10 +15,12 @@ pub fn build(b: *std.Build) void {
1515fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
1616 const lib = b.addExecutable(.{
1717 .name = "lib",
18 .root_source_file = b.path("lib.zig"),
19 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
20 .optimize = optimize,
21 .strip = false,
18 .root_module = b.createModule(.{
19 .root_source_file = b.path("lib.zig"),
20 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
21 .optimize = optimize,
22 .strip = false,
23 }),
2224 });
2325 lib.entry = .disabled;
2426 lib.use_llvm = false;
test/link/wasm/shared-memory/build.zig+11-9
......@@ -13,16 +13,18 @@ pub fn build(b: *std.Build) void {
1313fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.OptimizeMode) void {
1414 const exe = b.addExecutable(.{
1515 .name = "lib",
16 .root_source_file = b.path("lib.zig"),
17 .target = b.resolveTargetQuery(.{
18 .cpu_arch = .wasm32,
19 .cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp },
20 .cpu_features_add = std.Target.wasm.featureSet(&.{ .atomics, .bulk_memory }),
21 .os_tag = .freestanding,
16 .root_module = b.createModule(.{
17 .root_source_file = b.path("lib.zig"),
18 .target = b.resolveTargetQuery(.{
19 .cpu_arch = .wasm32,
20 .cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp },
21 .cpu_features_add = std.Target.wasm.featureSet(&.{ .atomics, .bulk_memory }),
22 .os_tag = .freestanding,
23 }),
24 .optimize = optimize_mode,
25 .strip = false,
26 .single_threaded = false,
2227 }),
23 .optimize = optimize_mode,
24 .strip = false,
25 .single_threaded = false,
2628 });
2729 exe.entry = .disabled;
2830 exe.use_lld = false;
test/link/wasm/stack_pointer/build.zig+6-4
......@@ -15,10 +15,12 @@ pub fn build(b: *std.Build) void {
1515fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
1616 const lib = b.addExecutable(.{
1717 .name = "lib",
18 .root_source_file = b.path("lib.zig"),
19 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
20 .optimize = optimize,
21 .strip = false,
18 .root_module = b.createModule(.{
19 .root_source_file = b.path("lib.zig"),
20 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
21 .optimize = optimize,
22 .strip = false,
23 }),
2224 });
2325 lib.entry = .disabled;
2426 lib.use_llvm = false;
test/link/wasm/type/build.zig+6-4
......@@ -15,10 +15,12 @@ pub fn build(b: *std.Build) void {
1515fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
1616 const exe = b.addExecutable(.{
1717 .name = "lib",
18 .root_source_file = b.path("lib.zig"),
19 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
20 .optimize = optimize,
21 .strip = false,
18 .root_module = b.createModule(.{
19 .root_source_file = b.path("lib.zig"),
20 .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
21 .optimize = optimize,
22 .strip = false,
23 }),
2224 });
2325 exe.entry = .disabled;
2426 exe.use_llvm = false;
test/src/Cases.zig+20-20
......@@ -572,7 +572,10 @@ pub fn lowerToTranslateCSteps(
572572 });
573573 translate_c.step.name = b.fmt("{s} translate-c", .{annotated_case_name});
574574
575 const run_exe = translate_c.addExecutable(.{});
575 const run_exe = b.addExecutable(.{
576 .name = "translated_c",
577 .root_module = translate_c.createModule(),
578 });
576579 run_exe.step.name = b.fmt("{s} build-exe", .{annotated_case_name});
577580 run_exe.linkLibC();
578581 const run = b.addRunArtifact(run_exe);
......@@ -660,34 +663,37 @@ pub fn lowerToBuildSteps(
660663 file_sources.put(file.path, writefiles.add(file.path, file.src)) catch @panic("OOM");
661664 }
662665
663 const artifact = if (case.is_test) b.addTest(.{
666 const mod = b.createModule(.{
664667 .root_source_file = root_source_file,
665 .name = case.name,
666668 .target = case.target,
667669 .optimize = case.optimize_mode,
670 });
671 if (case.link_libc) mod.link_libc = true;
672 if (case.pic) |pic| mod.pic = pic;
673 for (case.deps.items) |dep| {
674 mod.addAnonymousImport(dep.name, .{
675 .root_source_file = file_sources.get(dep.path).?,
676 });
677 }
678
679 const artifact = if (case.is_test) b.addTest(.{
680 .name = case.name,
681 .root_module = mod,
668682 }) else switch (case.output_mode) {
669683 .Obj => b.addObject(.{
670 .root_source_file = root_source_file,
671684 .name = case.name,
672 .target = case.target,
673 .optimize = case.optimize_mode,
685 .root_module = mod,
674686 }),
675687 .Lib => b.addStaticLibrary(.{
676 .root_source_file = root_source_file,
677688 .name = case.name,
678 .target = case.target,
679 .optimize = case.optimize_mode,
689 .root_module = mod,
680690 }),
681691 .Exe => b.addExecutable(.{
682 .root_source_file = root_source_file,
683692 .name = case.name,
684 .target = case.target,
685 .optimize = case.optimize_mode,
693 .root_module = mod,
686694 }),
687695 };
688696
689 if (case.link_libc) artifact.linkLibC();
690 if (case.pic) |pic| artifact.root_module.pic = pic;
691697 if (case.pie) |pie| artifact.pie = pie;
692698
693699 switch (case.backend) {
......@@ -701,12 +707,6 @@ pub fn lowerToBuildSteps(
701707 },
702708 }
703709
704 for (case.deps.items) |dep| {
705 artifact.root_module.addAnonymousImport(dep.name, .{
706 .root_source_file = file_sources.get(dep.path).?,
707 });
708 }
709
710710 switch (update.case) {
711711 .Compile => {
712712 // Force the binary to be emitted if requested.
test/src/CompareOutput.zig+23-21
......@@ -89,19 +89,22 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
8989
9090 switch (case.special) {
9191 Special.Asm => {
92 const annotated_case_name = fmt.allocPrint(self.b.allocator, "run assemble-and-link {s}", .{
92 const annotated_case_name = b.fmt("run assemble-and-link {s}", .{
9393 case.name,
94 }) catch @panic("OOM");
94 });
9595 for (self.test_filters) |test_filter| {
9696 if (mem.indexOf(u8, annotated_case_name, test_filter)) |_| break;
9797 } else if (self.test_filters.len > 0) return;
9898
9999 const exe = b.addExecutable(.{
100100 .name = "test",
101 .target = b.graph.host,
102 .optimize = .Debug,
101 .root_module = b.createModule(.{
102 .root_source_file = null,
103 .target = b.graph.host,
104 .optimize = .Debug,
105 }),
103106 });
104 exe.addAssemblyFile(first_file);
107 exe.root_module.addAssemblyFile(first_file);
105108
106109 const run = b.addRunArtifact(exe);
107110 run.setName(annotated_case_name);
......@@ -112,22 +115,22 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
112115 },
113116 Special.None => {
114117 for (self.optimize_modes) |optimize| {
115 const annotated_case_name = fmt.allocPrint(self.b.allocator, "run compare-output {s} ({s})", .{
118 const annotated_case_name = b.fmt("run compare-output {s} ({s})", .{
116119 case.name, @tagName(optimize),
117 }) catch @panic("OOM");
120 });
118121 for (self.test_filters) |test_filter| {
119122 if (mem.indexOf(u8, annotated_case_name, test_filter)) |_| break;
120123 } else if (self.test_filters.len > 0) return;
121124
122125 const exe = b.addExecutable(.{
123126 .name = "test",
124 .root_source_file = first_file,
125 .optimize = optimize,
126 .target = b.graph.host,
127 .root_module = b.createModule(.{
128 .root_source_file = first_file,
129 .optimize = optimize,
130 .target = b.graph.host,
131 }),
127132 });
128 if (case.link_libc) {
129 exe.linkSystemLibrary("c");
130 }
133 if (case.link_libc) exe.root_module.link_libc = true;
131134
132135 const run = b.addRunArtifact(exe);
133136 run.setName(annotated_case_name);
......@@ -140,20 +143,20 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
140143 Special.RuntimeSafety => {
141144 // TODO iterate over self.optimize_modes and test this in both
142145 // debug and release safe mode
143 const annotated_case_name = fmt.allocPrint(self.b.allocator, "run safety {s}", .{case.name}) catch @panic("OOM");
146 const annotated_case_name = b.fmt("run safety {s}", .{case.name});
144147 for (self.test_filters) |test_filter| {
145148 if (mem.indexOf(u8, annotated_case_name, test_filter)) |_| break;
146149 } else if (self.test_filters.len > 0) return;
147150
148151 const exe = b.addExecutable(.{
149152 .name = "test",
150 .root_source_file = first_file,
151 .target = b.graph.host,
152 .optimize = .Debug,
153 .root_module = b.createModule(.{
154 .root_source_file = first_file,
155 .target = b.graph.host,
156 .optimize = .Debug,
157 }),
153158 });
154 if (case.link_libc) {
155 exe.linkSystemLibrary("c");
156 }
159 if (case.link_libc) exe.root_module.link_libc = true;
157160
158161 const run = b.addRunArtifact(exe);
159162 run.setName(annotated_case_name);
......@@ -168,7 +171,6 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
168171const CompareOutput = @This();
169172const std = @import("std");
170173const ArrayList = std.ArrayList;
171const fmt = std.fmt;
172174const mem = std.mem;
173175const fs = std.fs;
174176const OptimizeMode = std.builtin.OptimizeMode;
test/src/Debugger.zig+11-5
......@@ -2414,8 +2414,8 @@ fn addTest(
24142414 } else return;
24152415 }
24162416 const files_wf = db.b.addWriteFiles();
2417 const exe = db.b.addExecutable(.{
2418 .name = name,
2417
2418 const mod = db.b.createModule(.{
24192419 .target = target.resolved,
24202420 .root_source_file = files_wf.add(files[0].path, files[0].source),
24212421 .optimize = target.optimize_mode,
......@@ -2423,15 +2423,21 @@ fn addTest(
24232423 .single_threaded = target.single_threaded,
24242424 .pic = target.pic,
24252425 .strip = false,
2426 .use_llvm = false,
2427 .use_lld = false,
24282426 });
24292427 for (files[1..]) |file| {
24302428 const path = files_wf.add(file.path, file.source);
2431 if (file.import) |import| exe.root_module.addImport(import, db.b.createModule(.{
2429 if (file.import) |import| mod.addImport(import, db.b.createModule(.{
24322430 .root_source_file = path,
24332431 }));
24342432 }
2433
2434 const exe = db.b.addExecutable(.{
2435 .name = name,
2436 .root_module = mod,
2437 .use_llvm = false,
2438 .use_lld = false,
2439 });
2440
24352441 const commands_wf = db.b.addWriteFiles();
24362442 const run = std.Build.Step.Run.create(db.b, db.b.fmt("run {s} {s}", .{ name, target.test_name_suffix }));
24372443 run.addArgs(db_argv1);
test/src/RunTranslatedC.zig+4-1
......@@ -84,7 +84,10 @@ pub fn addCase(self: *RunTranslatedCContext, case: *const TestCase) void {
8484 });
8585
8686 translate_c.step.name = b.fmt("{s} translate-c", .{annotated_case_name});
87 const exe = translate_c.addExecutable(.{});
87 const exe = b.addExecutable(.{
88 .name = "translated_c",
89 .root_module = translate_c.createModule(),
90 });
8891 exe.step.name = b.fmt("{s} build-exe", .{annotated_case_name});
8992 exe.linkLibC();
9093 const run = b.addRunArtifact(exe);
test/src/StackTrace.zig+8-7
......@@ -44,9 +44,9 @@ fn addExpect(
4444 for (mode_config.exclude_os) |tag| if (tag == builtin.os.tag) return;
4545
4646 const b = self.b;
47 const annotated_case_name = fmt.allocPrint(b.allocator, "check {s} ({s})", .{
47 const annotated_case_name = b.fmt("check {s} ({s})", .{
4848 name, @tagName(optimize_mode),
49 }) catch @panic("OOM");
49 });
5050 for (self.test_filters) |test_filter| {
5151 if (mem.indexOf(u8, annotated_case_name, test_filter)) |_| break;
5252 } else if (self.test_filters.len > 0) return;
......@@ -55,10 +55,12 @@ fn addExpect(
5555 const source_zig = write_files.add("source.zig", source);
5656 const exe = b.addExecutable(.{
5757 .name = "test",
58 .root_source_file = source_zig,
59 .optimize = optimize_mode,
60 .target = b.graph.host,
61 .error_tracing = mode_config.error_tracing,
58 .root_module = b.createModule(.{
59 .root_source_file = source_zig,
60 .optimize = optimize_mode,
61 .target = b.graph.host,
62 .error_tracing = mode_config.error_tracing,
63 }),
6264 });
6365
6466 const run = b.addRunArtifact(exe);
......@@ -83,5 +85,4 @@ const std = @import("std");
8385const builtin = @import("builtin");
8486const Step = std.Build.Step;
8587const OptimizeMode = std.builtin.OptimizeMode;
86const fmt = std.fmt;
8788const mem = std.mem;
test/standalone/build.zig+5-3
......@@ -47,9 +47,11 @@ pub fn build(b: *std.Build) void {
4747 }) |tool_src_path| {
4848 const tool = b.addTest(.{
4949 .name = std.fs.path.stem(tool_src_path),
50 .root_source_file = b.path(tool_src_path),
51 .optimize = .Debug,
52 .target = tools_target,
50 .root_module = b.createModule(.{
51 .root_source_file = b.path(tool_src_path),
52 .optimize = .Debug,
53 .target = tools_target,
54 }),
5355 });
5456 const run = b.addRunArtifact(tool);
5557 tools_tests_step.dependOn(&run.step);
test/standalone/c_compiler/build.zig+11-8
......@@ -20,22 +20,25 @@ fn add(
2020) void {
2121 const target = b.graph.host;
2222
23 const exe_c = b.addExecutable(.{
24 .name = c_name,
23 const c_mod = b.createModule(.{
2524 .optimize = optimize,
2625 .target = target,
2726 });
28 exe_c.addCSourceFile(.{ .file = b.path("test.c"), .flags = &[0][]const u8{} });
29 exe_c.linkLibC();
27 c_mod.addCSourceFile(.{ .file = b.path("test.c"), .flags = &[0][]const u8{} });
28 c_mod.link_libc = true;
3029
31 const exe_cpp = b.addExecutable(.{
32 .name = cpp_name,
30 const exe_c = b.addExecutable(.{ .name = c_name, .root_module = c_mod });
31
32 const cpp_mod = b.createModule(.{
3333 .optimize = optimize,
3434 .target = target,
3535 });
36 cpp_mod.addCSourceFile(.{ .file = b.path("test.cpp"), .flags = &[0][]const u8{} });
37 cpp_mod.link_libcpp = true;
38
39 const exe_cpp = b.addExecutable(.{ .name = cpp_name, .root_module = cpp_mod });
40
3641 b.default_step.dependOn(&exe_cpp.step);
37 exe_cpp.addCSourceFile(.{ .file = b.path("test.cpp"), .flags = &[0][]const u8{} });
38 exe_cpp.linkLibCpp();
3942
4043 switch (target.result.os.tag) {
4144 .windows => {
test/standalone/child_process/build.zig+10-6
......@@ -12,16 +12,20 @@ pub fn build(b: *std.Build) void {
1212
1313 const child = b.addExecutable(.{
1414 .name = "child",
15 .root_source_file = b.path("child.zig"),
16 .optimize = optimize,
17 .target = target,
15 .root_module = b.createModule(.{
16 .root_source_file = b.path("child.zig"),
17 .optimize = optimize,
18 .target = target,
19 }),
1820 });
1921
2022 const main = b.addExecutable(.{
2123 .name = "main",
22 .root_source_file = b.path("main.zig"),
23 .optimize = optimize,
24 .target = target,
24 .root_module = b.createModule(.{
25 .root_source_file = b.path("main.zig"),
26 .optimize = optimize,
27 .target = target,
28 }),
2529 });
2630 const run = b.addRunArtifact(main);
2731 run.addArtifactArg(child);
test/standalone/coff_dwarf/build.zig+13-8
......@@ -14,19 +14,24 @@ pub fn build(b: *std.Build) void {
1414
1515 const exe = b.addExecutable(.{
1616 .name = "main",
17 .root_source_file = b.path("main.zig"),
18 .optimize = optimize,
19 .target = target,
17 .root_module = b.createModule(.{
18 .root_source_file = b.path("main.zig"),
19 .optimize = optimize,
20 .target = target,
21 }),
2022 });
2123
2224 const lib = b.addSharedLibrary(.{
2325 .name = "shared_lib",
24 .optimize = optimize,
25 .target = target,
26 .root_module = b.createModule(.{
27 .root_source_file = null,
28 .optimize = optimize,
29 .target = target,
30 .link_libc = true,
31 }),
2632 });
27 lib.addCSourceFile(.{ .file = b.path("shared_lib.c"), .flags = &.{"-gdwarf"} });
28 lib.linkLibC();
29 exe.linkLibrary(lib);
33 lib.root_module.addCSourceFile(.{ .file = b.path("shared_lib.c"), .flags = &.{"-gdwarf"} });
34 exe.root_module.linkLibrary(lib);
3035
3136 const run = b.addRunArtifact(exe);
3237 run.expectExitCode(0);
test/standalone/compiler_rt_panic/build.zig+7-4
......@@ -12,11 +12,14 @@ pub fn build(b: *std.Build) void {
1212
1313 const exe = b.addExecutable(.{
1414 .name = "main",
15 .optimize = optimize,
16 .target = target,
15 .root_module = b.createModule(.{
16 .root_source_file = null,
17 .optimize = optimize,
18 .target = target,
19 .link_libc = true,
20 }),
1721 });
18 exe.linkLibC();
19 exe.addCSourceFile(.{
22 exe.root_module.addCSourceFile(.{
2023 .file = b.path("main.c"),
2124 .flags = &.{},
2225 });
test/standalone/dep_diamond/build.zig+13-13
......@@ -6,23 +6,23 @@ pub fn build(b: *std.Build) void {
66
77 const optimize: std.builtin.OptimizeMode = .Debug;
88
9 const shared = b.createModule(.{
10 .root_source_file = b.path("shared.zig"),
11 });
12
13 const exe = b.addExecutable(.{
14 .name = "test",
9 const main_mod = b.createModule(.{
1510 .root_source_file = b.path("test.zig"),
1611 .target = b.graph.host,
1712 .optimize = optimize,
1813 });
19 exe.root_module.addAnonymousImport("foo", .{
20 .root_source_file = b.path("foo.zig"),
21 .imports = &.{.{ .name = "shared", .module = shared }},
22 });
23 exe.root_module.addAnonymousImport("bar", .{
24 .root_source_file = b.path("bar.zig"),
25 .imports = &.{.{ .name = "shared", .module = shared }},
14 const shared_mod = b.createModule(.{ .root_source_file = b.path("shared.zig") });
15 const foo_mod = b.createModule(.{ .root_source_file = b.path("foo.zig") });
16 const bar_mod = b.createModule(.{ .root_source_file = b.path("bar.zig") });
17
18 main_mod.addImport("foo", foo_mod);
19 main_mod.addImport("bar", bar_mod);
20 foo_mod.addImport("shared", shared_mod);
21 bar_mod.addImport("shared", shared_mod);
22
23 const exe = b.addExecutable(.{
24 .name = "test",
25 .root_module = main_mod,
2626 });
2727
2828 const run = b.addRunArtifact(exe);
test/standalone/dep_duplicate_module/build.zig+17-10
......@@ -4,29 +4,36 @@ pub fn build(b: *std.Build) void {
44 const target = b.standardTargetOptions(.{});
55 const optimize = b.standardOptimizeOption(.{});
66
7 const mod = b.addModule("mod", .{
7 const shared_mod = b.createModule(.{
88 .root_source_file = b.path("mod.zig"),
99 .target = target,
1010 .optimize = optimize,
1111 });
12
13 const lib = b.addStaticLibrary(.{
14 .name = "lib",
12 const lib_mod = b.createModule(.{
1513 .root_source_file = b.path("lib.zig"),
1614 .target = target,
1715 .optimize = optimize,
1816 });
19 lib.root_module.addImport("mod", mod);
20
21 const exe = b.addExecutable(.{
22 .name = "app",
17 const exe_mod = b.createModule(.{
2318 .root_source_file = b.path("main.zig"),
2419 .target = target,
2520 .optimize = optimize,
2621 });
2722
28 exe.root_module.addImport("mod", mod);
29 exe.root_module.linkLibrary(lib);
23 lib_mod.addImport("mod", shared_mod);
24 exe_mod.addImport("mod", shared_mod);
25
26 const lib = b.addStaticLibrary(.{
27 .name = "lib",
28 .root_module = lib_mod,
29 });
30
31 exe_mod.linkLibrary(lib);
32
33 const exe = b.addExecutable(.{
34 .name = "app",
35 .root_module = exe_mod,
36 });
3037
3138 b.installArtifact(exe);
3239}
test/standalone/dep_lazypath/build.zig+12-7
......@@ -11,10 +11,13 @@ pub fn build(b: *std.Build) void {
1111 const generated_main_c = write_files.add("main.c", "");
1212 const exe = b.addExecutable(.{
1313 .name = "test",
14 .target = b.graph.host,
15 .optimize = optimize,
14 .root_module = b.createModule(.{
15 .root_source_file = null,
16 .target = b.graph.host,
17 .optimize = optimize,
18 }),
1619 });
17 exe.addCSourceFiles(.{
20 exe.root_module.addCSourceFiles(.{
1821 .root = generated_main_c.dirname(),
1922 .files = &.{"main.c"},
2023 });
......@@ -26,11 +29,13 @@ pub fn build(b: *std.Build) void {
2629 const dir = write_files.addCopyDirectory(b.path("inc"), "", .{});
2730 const exe = b.addExecutable(.{
2831 .name = "test",
29 .root_source_file = b.path("inctest.zig"),
30 .target = b.graph.host,
31 .optimize = optimize,
32 .root_module = b.createModule(.{
33 .root_source_file = b.path("inctest.zig"),
34 .target = b.graph.host,
35 .optimize = optimize,
36 }),
3237 });
33 exe.addIncludePath(dir);
38 exe.root_module.addIncludePath(dir);
3439 b.step("copydir", "").dependOn(&exe.step);
3540 test_step.dependOn(&exe.step);
3641 }
test/standalone/dep_mutually_recursive/build.zig+12-8
......@@ -6,22 +6,26 @@ pub fn build(b: *std.Build) void {
66
77 const optimize: std.builtin.OptimizeMode = .Debug;
88
9 const foo = b.createModule(.{
9 const main_mod = b.createModule(.{
10 .root_source_file = b.path("test.zig"),
11 .target = b.graph.host,
12 .optimize = optimize,
13 });
14 const foo_mod = b.createModule(.{
1015 .root_source_file = b.path("foo.zig"),
1116 });
12 const bar = b.createModule(.{
17 const bar_mod = b.createModule(.{
1318 .root_source_file = b.path("bar.zig"),
1419 });
15 foo.addImport("bar", bar);
16 bar.addImport("foo", foo);
20
21 main_mod.addImport("foo", foo_mod);
22 foo_mod.addImport("bar", bar_mod);
23 bar_mod.addImport("foo", foo_mod);
1724
1825 const exe = b.addExecutable(.{
1926 .name = "test",
20 .root_source_file = b.path("test.zig"),
21 .target = b.graph.host,
22 .optimize = optimize,
27 .root_module = main_mod,
2328 });
24 exe.root_module.addImport("foo", foo);
2529
2630 const run = b.addRunArtifact(exe);
2731 test_step.dependOn(&run.step);
test/standalone/dep_recursive/build.zig+10-6
......@@ -6,18 +6,22 @@ pub fn build(b: *std.Build) void {
66
77 const optimize: std.builtin.OptimizeMode = .Debug;
88
9 const foo = b.createModule(.{
9 const main_mod = b.createModule(.{
10 .root_source_file = b.path("test.zig"),
11 .target = b.graph.host,
12 .optimize = optimize,
13 });
14 const foo_mod = b.createModule(.{
1015 .root_source_file = b.path("foo.zig"),
1116 });
12 foo.addImport("foo", foo);
17
18 main_mod.addImport("foo", foo_mod);
19 foo_mod.addImport("foo", foo_mod);
1320
1421 const exe = b.addExecutable(.{
1522 .name = "test",
16 .root_source_file = b.path("test.zig"),
17 .target = b.graph.host,
18 .optimize = optimize,
23 .root_module = main_mod,
1924 });
20 exe.root_module.addImport("foo", foo);
2125
2226 const run = b.addRunArtifact(exe);
2327 test_step.dependOn(&run.step);
test/standalone/dep_shared_builtin/build.zig+9-3
......@@ -6,16 +6,22 @@ pub fn build(b: *std.Build) void {
66
77 const optimize: std.builtin.OptimizeMode = .Debug;
88
9 const exe = b.addExecutable(.{
10 .name = "test",
9 const main_mod = b.createModule(.{
1110 .root_source_file = b.path("test.zig"),
1211 .target = b.graph.host,
1312 .optimize = optimize,
1413 });
15 exe.root_module.addAnonymousImport("foo", .{
14 const foo_mod = b.createModule(.{
1615 .root_source_file = b.path("foo.zig"),
1716 });
1817
18 main_mod.addImport("foo", foo_mod);
19
20 const exe = b.addExecutable(.{
21 .name = "test",
22 .root_module = main_mod,
23 });
24
1925 const run = b.addRunArtifact(exe);
2026 test_step.dependOn(&run.step);
2127}
test/standalone/dep_triangle/build.zig+14-9
......@@ -6,21 +6,26 @@ pub fn build(b: *std.Build) void {
66
77 const optimize: std.builtin.OptimizeMode = .Debug;
88
9 const shared = b.createModule(.{
10 .root_source_file = b.path("shared.zig"),
11 });
12
13 const exe = b.addExecutable(.{
14 .name = "test",
9 const main_mod = b.createModule(.{
1510 .root_source_file = b.path("test.zig"),
1611 .target = b.graph.host,
1712 .optimize = optimize,
1813 });
19 exe.root_module.addAnonymousImport("foo", .{
14 const foo_mod = b.createModule(.{
2015 .root_source_file = b.path("foo.zig"),
21 .imports = &.{.{ .name = "shared", .module = shared }},
2216 });
23 exe.root_module.addImport("shared", shared);
17 const shared_mod = b.createModule(.{
18 .root_source_file = b.path("shared.zig"),
19 });
20
21 main_mod.addImport("foo", foo_mod);
22 main_mod.addImport("shared", shared_mod);
23 foo_mod.addImport("shared", shared_mod);
24
25 const exe = b.addExecutable(.{
26 .name = "test",
27 .root_module = main_mod,
28 });
2429
2530 const run = b.addRunArtifact(exe);
2631 test_step.dependOn(&run.step);
test/standalone/depend_on_main_mod/build.zig+9-6
......@@ -7,19 +7,22 @@ pub fn build(b: *std.Build) void {
77 const target = b.standardTargetOptions(.{});
88 const optimize = b.standardOptimizeOption(.{});
99
10 const exe = b.addExecutable(.{
11 .name = "depend_on_main_mod",
10 const main_mod = b.createModule(.{
1211 .root_source_file = b.path("src/main.zig"),
1312 .target = target,
1413 .optimize = optimize,
1514 });
16
17 const foo_module = b.addModule("foo", .{
15 const foo_mod = b.createModule(.{
1816 .root_source_file = b.path("src/foo.zig"),
1917 });
2018
21 foo_module.addImport("root2", &exe.root_module);
22 exe.root_module.addImport("foo", foo_module);
19 foo_mod.addImport("root2", main_mod);
20 main_mod.addImport("foo", foo_mod);
21
22 const exe = b.addExecutable(.{
23 .name = "depend_on_main_mod",
24 .root_module = main_mod,
25 });
2326
2427 const run_cmd = b.addRunArtifact(exe);
2528 run_cmd.expectExitCode(0);
test/standalone/dirname/build.zig+15-9
......@@ -10,24 +10,30 @@ pub fn build(b: *std.Build) void {
1010
1111 const touch = b.addExecutable(.{
1212 .name = "touch",
13 .root_source_file = touch_src,
14 .optimize = .Debug,
15 .target = target,
13 .root_module = b.createModule(.{
14 .root_source_file = touch_src,
15 .optimize = .Debug,
16 .target = target,
17 }),
1618 });
1719 const generated = b.addRunArtifact(touch).addOutputFileArg("subdir" ++ std.fs.path.sep_str ++ "generated.txt");
1820
1921 const exists_in = b.addExecutable(.{
2022 .name = "exists_in",
21 .root_source_file = b.path("exists_in.zig"),
22 .optimize = .Debug,
23 .target = target,
23 .root_module = b.createModule(.{
24 .root_source_file = b.path("exists_in.zig"),
25 .optimize = .Debug,
26 .target = target,
27 }),
2428 });
2529
2630 const has_basename = b.addExecutable(.{
2731 .name = "has_basename",
28 .root_source_file = b.path("has_basename.zig"),
29 .optimize = .Debug,
30 .target = target,
32 .root_module = b.createModule(.{
33 .root_source_file = b.path("has_basename.zig"),
34 .optimize = .Debug,
35 .target = target,
36 }),
3137 });
3238
3339 // Known path:
test/standalone/embed_generated_file/build.zig+10-7
......@@ -6,18 +6,21 @@ pub fn build(b: *std.Build) void {
66
77 const bootloader = b.addExecutable(.{
88 .name = "bootloader",
9 .root_source_file = b.path("bootloader.zig"),
10 .target = b.resolveTargetQuery(.{
11 .cpu_arch = .x86,
12 .os_tag = .freestanding,
9 .root_module = b.createModule(.{
10 .root_source_file = b.path("bootloader.zig"),
11 .target = b.resolveTargetQuery(.{
12 .cpu_arch = .x86,
13 .os_tag = .freestanding,
14 }),
15 .optimize = .ReleaseSmall,
1316 }),
14 .optimize = .ReleaseSmall,
1517 });
1618
17 const exe = b.addTest(.{
19 const exe = b.addTest(.{ .root_module = b.createModule(.{
1820 .root_source_file = b.path("main.zig"),
21 .target = b.graph.host,
1922 .optimize = .Debug,
20 });
23 }) });
2124 exe.root_module.addAnonymousImport("bootloader.elf", .{
2225 .root_source_file = bootloader.getEmittedBin(),
2326 });
test/standalone/emit_asm_and_bin/build.zig+3-2
......@@ -4,10 +4,11 @@ pub fn build(b: *std.Build) void {
44 const test_step = b.step("test", "Test it");
55 b.default_step = test_step;
66
7 const main = b.addTest(.{
7 const main = b.addTest(.{ .root_module = b.createModule(.{
88 .root_source_file = b.path("main.zig"),
9 .target = b.graph.host,
910 .optimize = b.standardOptimizeOption(.{}),
10 });
11 }) });
1112 // TODO: actually check these two artifacts for correctness
1213 _ = main.getEmittedBin();
1314 _ = main.getEmittedAsm();
test/standalone/emit_asm_no_bin/build.zig+5-3
......@@ -8,9 +8,11 @@ pub fn build(b: *std.Build) void {
88
99 const obj = b.addObject(.{
1010 .name = "main",
11 .root_source_file = b.path("main.zig"),
12 .optimize = optimize,
13 .target = b.graph.host,
11 .root_module = b.createModule(.{
12 .root_source_file = b.path("main.zig"),
13 .optimize = optimize,
14 .target = b.graph.host,
15 }),
1416 });
1517 _ = obj.getEmittedAsm();
1618 b.default_step.dependOn(&obj.step);
test/standalone/emit_llvm_no_bin/build.zig+5-3
......@@ -8,9 +8,11 @@ pub fn build(b: *std.Build) void {
88
99 const obj = b.addObject(.{
1010 .name = "main",
11 .root_source_file = b.path("main.zig"),
12 .optimize = optimize,
13 .target = b.graph.host,
11 .root_module = b.createModule(.{
12 .root_source_file = b.path("main.zig"),
13 .optimize = optimize,
14 .target = b.graph.host,
15 }),
1416 });
1517 _ = obj.getEmittedLlvmIr();
1618 _ = obj.getEmittedLlvmBc();
test/standalone/empty_env/build.zig+5-3
......@@ -21,9 +21,11 @@ pub fn build(b: *std.Build) void {
2121
2222 const main = b.addExecutable(.{
2323 .name = "main",
24 .root_source_file = b.path("main.zig"),
25 .target = b.graph.host,
26 .optimize = optimize,
24 .root_module = b.createModule(.{
25 .root_source_file = b.path("main.zig"),
26 .target = b.graph.host,
27 .optimize = optimize,
28 }),
2729 });
2830
2931 const run = b.addRunArtifact(main);
test/standalone/extern/build.zig+16-10
......@@ -8,22 +8,28 @@ pub fn build(b: *std.Build) void {
88
99 const obj = b.addObject(.{
1010 .name = "exports",
11 .root_source_file = b.path("exports.zig"),
12 .target = b.graph.host,
13 .optimize = optimize,
11 .root_module = b.createModule(.{
12 .root_source_file = b.path("exports.zig"),
13 .target = b.graph.host,
14 .optimize = optimize,
15 }),
1416 });
1517 const shared = b.addSharedLibrary(.{
1618 .name = "shared",
17 .target = b.graph.host,
18 .optimize = optimize,
19 .link_libc = true,
19 .root_module = b.createModule(.{
20 .root_source_file = null,
21 .target = b.graph.host,
22 .optimize = optimize,
23 .link_libc = true,
24 }),
2025 });
21 if (b.graph.host.result.abi == .msvc) shared.defineCMacro("API", "__declspec(dllexport)");
22 shared.addCSourceFile(.{ .file = b.path("shared.c"), .flags = &.{} });
23 const test_exe = b.addTest(.{
26 if (b.graph.host.result.abi == .msvc) shared.root_module.addCMacro("API", "__declspec(dllexport)");
27 shared.root_module.addCSourceFile(.{ .file = b.path("shared.c"), .flags = &.{} });
28 const test_exe = b.addTest(.{ .root_module = b.createModule(.{
2429 .root_source_file = b.path("main.zig"),
30 .target = b.graph.host,
2531 .optimize = optimize,
26 });
32 }) });
2733 test_exe.addObject(obj);
2834 test_exe.linkLibrary(shared);
2935
test/standalone/global_linkage/build.zig+15-9
......@@ -9,24 +9,30 @@ pub fn build(b: *std.Build) void {
99
1010 const obj1 = b.addStaticLibrary(.{
1111 .name = "obj1",
12 .root_source_file = b.path("obj1.zig"),
13 .optimize = optimize,
14 .target = target,
12 .root_module = b.createModule(.{
13 .root_source_file = b.path("obj1.zig"),
14 .optimize = optimize,
15 .target = target,
16 }),
1517 });
1618
1719 const obj2 = b.addStaticLibrary(.{
1820 .name = "obj2",
19 .root_source_file = b.path("obj2.zig"),
20 .optimize = optimize,
21 .target = target,
21 .root_module = b.createModule(.{
22 .root_source_file = b.path("obj2.zig"),
23 .optimize = optimize,
24 .target = target,
25 }),
2226 });
2327
24 const main = b.addTest(.{
28 const main_mod = b.createModule(.{
2529 .root_source_file = b.path("main.zig"),
30 .target = b.graph.host,
2631 .optimize = optimize,
2732 });
28 main.linkLibrary(obj1);
29 main.linkLibrary(obj2);
33 main_mod.linkLibrary(obj1);
34 main_mod.linkLibrary(obj2);
3035
36 const main = b.addTest(.{ .root_module = main_mod });
3137 test_step.dependOn(&b.addRunArtifact(main).step);
3238}
test/standalone/install_headers/build.zig+27-15
......@@ -8,18 +8,24 @@ pub fn build(b: *std.Build) void {
88
99 const libfoo = b.addStaticLibrary(.{
1010 .name = "foo",
11 .target = b.resolveTargetQuery(.{}),
12 .optimize = .Debug,
11 .root_module = b.createModule(.{
12 .root_source_file = null,
13 .target = b.resolveTargetQuery(.{}),
14 .optimize = .Debug,
15 }),
1316 });
14 libfoo.addCSourceFile(.{ .file = empty_c });
17 libfoo.root_module.addCSourceFile(.{ .file = empty_c });
1518
1619 const exe = b.addExecutable(.{
1720 .name = "exe",
18 .target = b.resolveTargetQuery(.{}),
19 .optimize = .Debug,
20 .link_libc = true,
21 .root_module = b.createModule(.{
22 .root_source_file = null,
23 .target = b.resolveTargetQuery(.{}),
24 .optimize = .Debug,
25 .link_libc = true,
26 }),
2127 });
22 exe.addCSourceFile(.{ .file = b.addWriteFiles().add("main.c",
28 exe.root_module.addCSourceFile(.{ .file = b.addWriteFiles().add("main.c",
2329 \\#include <stdio.h>
2430 \\#include <foo/a.h>
2531 \\#include <foo/sub_dir/b.h>
......@@ -40,9 +46,10 @@ pub fn build(b: *std.Build) void {
4046
4147 if (libfoo.installed_headers_include_tree != null) std.debug.panic("include tree step was created before linking", .{});
4248
43 // Link before we have registered all headers for installation,
49 // Link (and get the include tree) before we have registered all headers for installation,
4450 // to verify that the lazily created write files step is properly taken into account.
45 exe.linkLibrary(libfoo);
51 exe.root_module.linkLibrary(libfoo);
52 _ = libfoo.getEmittedIncludeTree();
4653
4754 if (libfoo.installed_headers_include_tree == null) std.debug.panic("include tree step was not created after linking", .{});
4855
......@@ -56,10 +63,13 @@ pub fn build(b: *std.Build) void {
5663
5764 const libbar = b.addStaticLibrary(.{
5865 .name = "bar",
59 .target = b.resolveTargetQuery(.{}),
60 .optimize = .Debug,
66 .root_module = b.createModule(.{
67 .root_source_file = null,
68 .target = b.resolveTargetQuery(.{}),
69 .optimize = .Debug,
70 }),
6171 });
62 libbar.addCSourceFile(.{ .file = empty_c });
72 libbar.root_module.addCSourceFile(.{ .file = empty_c });
6373 libbar.installHeader(b.addWriteFiles().add("bar.h",
6474 \\#define BAR_X "X"
6575 \\
......@@ -78,9 +88,11 @@ pub fn build(b: *std.Build) void {
7888 });
7989 const check_exists = b.addExecutable(.{
8090 .name = "check_exists",
81 .root_source_file = b.path("check_exists.zig"),
82 .target = b.resolveTargetQuery(.{}),
83 .optimize = .Debug,
91 .root_module = b.createModule(.{
92 .root_source_file = b.path("check_exists.zig"),
93 .target = b.resolveTargetQuery(.{}),
94 .optimize = .Debug,
95 }),
8496 });
8597 const run_check_exists = b.addRunArtifact(check_exists);
8698 run_check_exists.addArgs(&.{
test/standalone/install_raw_hex/build.zig+5-3
......@@ -16,9 +16,11 @@ pub fn build(b: *std.Build) void {
1616
1717 const elf = b.addExecutable(.{
1818 .name = "zig-nrf52-blink.elf",
19 .root_source_file = b.path("main.zig"),
20 .target = target,
21 .optimize = optimize,
19 .root_module = b.createModule(.{
20 .root_source_file = b.path("main.zig"),
21 .target = target,
22 .optimize = optimize,
23 }),
2224 });
2325
2426 const hex_step = elf.addObjCopy(.{
test/standalone/ios/build.zig+12-9
......@@ -18,16 +18,19 @@ pub fn build(b: *std.Build) void {
1818
1919 const exe = b.addExecutable(.{
2020 .name = "main",
21 .optimize = optimize,
22 .target = target,
21 .root_module = b.createModule(.{
22 .root_source_file = null,
23 .optimize = optimize,
24 .target = target,
25 .link_libc = true,
26 }),
2327 });
24 exe.addCSourceFile(.{ .file = b.path("main.m"), .flags = &.{} });
25 exe.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/usr/include" }) });
26 exe.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/System/Library/Frameworks" }) });
27 exe.addLibraryPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/usr/lib" }) });
28 exe.linkFramework("Foundation");
29 exe.linkFramework("UIKit");
30 exe.linkLibC();
28 exe.root_module.addCSourceFile(.{ .file = b.path("main.m"), .flags = &.{} });
29 exe.root_module.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/usr/include" }) });
30 exe.root_module.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/System/Library/Frameworks" }) });
31 exe.root_module.addLibraryPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/usr/lib" }) });
32 exe.root_module.linkFramework("Foundation", .{});
33 exe.root_module.linkFramework("UIKit", .{});
3134
3235 const check = exe.checkObject();
3336 check.checkInHeaders();
test/standalone/issue_11595/build.zig+13-11
......@@ -15,9 +15,11 @@ pub fn build(b: *std.Build) void {
1515
1616 const exe = b.addExecutable(.{
1717 .name = "zigtest",
18 .root_source_file = b.path("main.zig"),
19 .target = target,
20 .optimize = optimize,
18 .root_module = b.createModule(.{
19 .root_source_file = b.path("main.zig"),
20 .target = target,
21 .optimize = optimize,
22 }),
2123 });
2224 b.installArtifact(exe);
2325
......@@ -25,21 +27,21 @@ pub fn build(b: *std.Build) void {
2527 "test.c",
2628 };
2729
28 exe.addCSourceFiles(.{ .files = &c_sources });
29 exe.linkLibC();
30 exe.root_module.addCSourceFiles(.{ .files = &c_sources });
31 exe.root_module.link_libc = true;
3032
3133 var i: i32 = 0;
3234 while (i < 1000) : (i += 1) {
33 exe.defineCMacro("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
35 exe.root_module.addCMacro("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
3436 }
3537
36 exe.defineCMacro("FOO", "42");
37 exe.defineCMacro("BAR", "\"BAR\"");
38 exe.defineCMacro("BAZ",
38 exe.root_module.addCMacro("FOO", "42");
39 exe.root_module.addCMacro("BAR", "\"BAR\"");
40 exe.root_module.addCMacro("BAZ",
3941 \\"\"BAZ\""
4042 );
41 exe.defineCMacro("QUX", "\"Q\" \"UX\"");
42 exe.defineCMacro("QUUX", "\"QU\\\"UX\"");
43 exe.root_module.addCMacro("QUX", "\"Q\" \"UX\"");
44 exe.root_module.addCMacro("QUUX", "\"QU\\\"UX\"");
4345
4446 b.default_step.dependOn(&exe.step);
4547
test/standalone/issue_12706/build.zig+7-5
......@@ -10,16 +10,18 @@ pub fn build(b: *std.Build) void {
1010
1111 const exe = b.addExecutable(.{
1212 .name = "main",
13 .root_source_file = b.path("main.zig"),
14 .optimize = optimize,
15 .target = target,
13 .root_module = b.createModule(.{
14 .root_source_file = b.path("main.zig"),
15 .optimize = optimize,
16 .target = target,
17 }),
1618 });
1719
1820 const c_sources = [_][]const u8{
1921 "test.c",
2022 };
21 exe.addCSourceFiles(.{ .files = &c_sources });
22 exe.linkLibC();
23 exe.root_module.addCSourceFiles(.{ .files = &c_sources });
24 exe.root_module.link_libc = true;
2325
2426 const run_cmd = b.addRunArtifact(exe);
2527 run_cmd.expectExitCode(0);
test/standalone/issue_13970/build.zig+12-3
......@@ -5,15 +5,24 @@ pub fn build(b: *std.Build) void {
55 b.default_step = test_step;
66
77 const test1 = b.addTest(.{
8 .root_source_file = b.path("test_root/empty.zig"),
8 .root_module = b.createModule(.{
9 .root_source_file = b.path("test_root/empty.zig"),
10 .target = b.graph.host,
11 }),
912 .test_runner = "src/main.zig",
1013 });
1114 const test2 = b.addTest(.{
12 .root_source_file = b.path("src/empty.zig"),
15 .root_module = b.createModule(.{
16 .root_source_file = b.path("src/empty.zig"),
17 .target = b.graph.host,
18 }),
1319 .test_runner = "src/main.zig",
1420 });
1521 const test3 = b.addTest(.{
16 .root_source_file = b.path("empty.zig"),
22 .root_module = b.createModule(.{
23 .root_source_file = b.path("empty.zig"),
24 .target = b.graph.host,
25 }),
1726 .test_runner = "src/main.zig",
1827 });
1928
test/standalone/issue_339/build.zig+5-3
......@@ -9,9 +9,11 @@ pub fn build(b: *std.Build) void {
99
1010 const obj = b.addObject(.{
1111 .name = "test",
12 .root_source_file = b.path("test.zig"),
13 .target = target,
14 .optimize = optimize,
12 .root_module = b.createModule(.{
13 .root_source_file = b.path("test.zig"),
14 .target = target,
15 .optimize = optimize,
16 }),
1517 });
1618
1719 // TODO: actually check the output
test/standalone/issue_5825/build.zig+13-8
......@@ -16,20 +16,25 @@ pub fn build(b: *std.Build) void {
1616 const optimize: std.builtin.OptimizeMode = .Debug;
1717 const obj = b.addObject(.{
1818 .name = "issue_5825",
19 .root_source_file = b.path("main.zig"),
20 .optimize = optimize,
21 .target = target,
19 .root_module = b.createModule(.{
20 .root_source_file = b.path("main.zig"),
21 .optimize = optimize,
22 .target = target,
23 }),
2224 });
2325
2426 const exe = b.addExecutable(.{
2527 .name = "issue_5825",
26 .optimize = optimize,
27 .target = target,
28 .root_module = b.createModule(.{
29 .root_source_file = null,
30 .optimize = optimize,
31 .target = target,
32 }),
2833 });
2934 exe.subsystem = .Console;
30 exe.linkSystemLibrary("kernel32");
31 exe.linkSystemLibrary("ntdll");
32 exe.addObject(obj);
35 exe.root_module.linkSystemLibrary("kernel32", .{});
36 exe.root_module.linkSystemLibrary("ntdll", .{});
37 exe.root_module.addObject(obj);
3338
3439 // TODO: actually check the output
3540 _ = exe.getEmittedBin();
test/standalone/issue_794/build.zig+3-2
......@@ -4,9 +4,10 @@ pub fn build(b: *std.Build) void {
44 const test_step = b.step("test", "Test it");
55 b.default_step = test_step;
66
7 const test_artifact = b.addTest(.{
7 const test_artifact = b.addTest(.{ .root_module = b.createModule(.{
88 .root_source_file = b.path("main.zig"),
9 });
9 .target = b.graph.host,
10 }) });
1011 test_artifact.addIncludePath(b.path("a_directory"));
1112
1213 // TODO: actually check the output
test/standalone/issue_8550/build.zig+6-4
......@@ -15,11 +15,13 @@ pub fn build(b: *std.Build) !void {
1515
1616 const kernel = b.addExecutable(.{
1717 .name = "kernel",
18 .root_source_file = b.path("./main.zig"),
19 .optimize = optimize,
20 .target = target,
18 .root_module = b.createModule(.{
19 .root_source_file = b.path("./main.zig"),
20 .optimize = optimize,
21 .target = target,
22 }),
2123 });
22 kernel.addObjectFile(b.path("./boot.S"));
24 kernel.root_module.addObjectFile(b.path("./boot.S"));
2325 kernel.setLinkerScript(b.path("./linker.ld"));
2426 b.installArtifact(kernel);
2527
test/standalone/libcxx/build.zig+15-11
......@@ -11,12 +11,14 @@ pub fn build(b: *std.Build) void {
1111 {
1212 const exe = b.addExecutable(.{
1313 .name = "mt",
14 .root_source_file = b.path("mt.zig"),
15 .target = target,
16 .optimize = optimize,
14 .root_module = b.createModule(.{
15 .root_source_file = b.path("mt.zig"),
16 .target = target,
17 .optimize = optimize,
18 .link_libcpp = true,
19 }),
1720 });
18 exe.linkLibCpp();
19 exe.addCSourceFile(.{ .file = b.path("mt_doit.cpp") });
21 exe.root_module.addCSourceFile(.{ .file = b.path("mt_doit.cpp") });
2022 link_step.dependOn(&exe.step);
2123 b.installArtifact(exe);
2224 run_step.dependOn(&b.addRunArtifact(exe).step);
......@@ -24,13 +26,15 @@ pub fn build(b: *std.Build) void {
2426 {
2527 const exe = b.addExecutable(.{
2628 .name = "st",
27 .root_source_file = b.path("st.zig"),
28 .target = target,
29 .optimize = optimize,
30 .single_threaded = true,
29 .root_module = b.createModule(.{
30 .root_source_file = b.path("st.zig"),
31 .target = target,
32 .optimize = optimize,
33 .link_libcpp = true,
34 .single_threaded = true,
35 }),
3136 });
32 exe.linkLibCpp();
33 exe.addCSourceFile(.{ .file = b.path("st_doit.cpp") });
37 exe.root_module.addCSourceFile(.{ .file = b.path("st_doit.cpp") });
3438 link_step.dependOn(&exe.step);
3539 b.installArtifact(exe);
3640 run_step.dependOn(&b.addRunArtifact(exe).step);
test/standalone/load_dynamic_library/build.zig+10-6
......@@ -12,17 +12,21 @@ pub fn build(b: *std.Build) void {
1212
1313 const lib = b.addSharedLibrary(.{
1414 .name = "add",
15 .root_source_file = b.path("add.zig"),
1615 .version = .{ .major = 1, .minor = 0, .patch = 0 },
17 .optimize = optimize,
18 .target = target,
16 .root_module = b.createModule(.{
17 .root_source_file = b.path("add.zig"),
18 .optimize = optimize,
19 .target = target,
20 }),
1921 });
2022
2123 const main = b.addExecutable(.{
2224 .name = "main",
23 .root_source_file = b.path("main.zig"),
24 .optimize = optimize,
25 .target = target,
25 .root_module = b.createModule(.{
26 .root_source_file = b.path("main.zig"),
27 .optimize = optimize,
28 .target = target,
29 }),
2630 });
2731
2832 const run = b.addRunArtifact(main);
test/standalone/mix_c_files/build.zig+7-5
......@@ -18,12 +18,14 @@ pub fn build(b: *std.Build) void {
1818fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
1919 const exe = b.addExecutable(.{
2020 .name = "test",
21 .root_source_file = b.path("main.zig"),
22 .target = b.graph.host,
23 .optimize = optimize,
21 .root_module = b.createModule(.{
22 .root_source_file = b.path("main.zig"),
23 .target = b.graph.host,
24 .optimize = optimize,
25 .link_libc = true,
26 }),
2427 });
25 exe.addCSourceFile(.{ .file = b.path("test.c"), .flags = &[_][]const u8{"-std=c11"} });
26 exe.linkLibC();
28 exe.root_module.addCSourceFile(.{ .file = b.path("test.c"), .flags = &[_][]const u8{"-std=c11"} });
2729
2830 const run_cmd = b.addRunArtifact(exe);
2931 run_cmd.skip_foreign_checks = true;
test/standalone/mix_o_files/build.zig+13-8
......@@ -9,22 +9,27 @@ pub fn build(b: *std.Build) void {
99
1010 const obj = b.addObject(.{
1111 .name = "base64",
12 .root_source_file = b.path("base64.zig"),
13 .optimize = optimize,
14 .target = target,
12 .root_module = b.createModule(.{
13 .root_source_file = b.path("base64.zig"),
14 .optimize = optimize,
15 .target = target,
16 }),
1517 });
1618
1719 const exe = b.addExecutable(.{
1820 .name = "test",
19 .optimize = optimize,
20 .target = target,
21 .root_module = b.createModule(.{
22 .root_source_file = null,
23 .optimize = optimize,
24 .target = target,
25 .link_libc = true,
26 }),
2127 });
22 exe.addCSourceFile(.{
28 exe.root_module.addCSourceFile(.{
2329 .file = b.path("test.c"),
2430 .flags = &[_][]const u8{"-std=c99"},
2531 });
26 exe.addObject(obj);
27 exe.linkSystemLibrary("c");
32 exe.root_module.addObject(obj);
2833
2934 b.default_step.dependOn(&exe.step);
3035
test/standalone/options/build.zig+2-2
......@@ -1,11 +1,11 @@
11const std = @import("std");
22
33pub fn build(b: *std.Build) void {
4 const main = b.addTest(.{
4 const main = b.addTest(.{ .root_module = b.createModule(.{
55 .root_source_file = b.path("src/main.zig"),
66 .target = b.graph.host,
77 .optimize = .Debug,
8 });
8 }) });
99
1010 const options = b.addOptions();
1111 main.addOptions("build_options", options);
test/standalone/pie/build.zig+5-3
......@@ -11,9 +11,11 @@ pub fn build(b: *std.Build) void {
1111 });
1212
1313 const main = b.addTest(.{
14 .root_source_file = b.path("main.zig"),
15 .optimize = optimize,
16 .target = target,
14 .root_module = b.createModule(.{
15 .root_source_file = b.path("main.zig"),
16 .optimize = optimize,
17 .target = target,
18 }),
1719 });
1820 main.pie = true;
1921
test/standalone/pkg_import/build.zig+5-3
......@@ -8,9 +8,11 @@ pub fn build(b: *std.Build) void {
88
99 const exe = b.addExecutable(.{
1010 .name = "test",
11 .root_source_file = b.path("test.zig"),
12 .optimize = optimize,
13 .target = b.graph.host,
11 .root_module = b.createModule(.{
12 .root_source_file = b.path("test.zig"),
13 .optimize = optimize,
14 .target = b.graph.host,
15 }),
1416 });
1517 exe.root_module.addAnonymousImport("my_pkg", .{ .root_source_file = b.path("pkg.zig") });
1618
test/standalone/run_output_caching/build.zig+5-3
......@@ -9,9 +9,11 @@ pub fn build(b: *std.Build) void {
99
1010 const exe = b.addExecutable(.{
1111 .name = "create-file",
12 .root_source_file = b.path("main.zig"),
13 .target = target,
14 .optimize = optimize,
12 .root_module = b.createModule(.{
13 .root_source_file = b.path("main.zig"),
14 .target = target,
15 .optimize = optimize,
16 }),
1517 });
1618
1719 {
test/standalone/run_output_paths/build.zig+5-3
......@@ -9,9 +9,11 @@ pub fn build(b: *std.Build) void {
99
1010 const create_file_exe = b.addExecutable(.{
1111 .name = "create_file",
12 .root_source_file = b.path("create_file.zig"),
13 .target = target,
14 .optimize = optimize,
12 .root_module = b.createModule(.{
13 .root_source_file = b.path("create_file.zig"),
14 .target = target,
15 .optimize = optimize,
16 }),
1517 });
1618
1719 const create_first = b.addRunArtifact(create_file_exe);
test/standalone/self_exe_symlink/build.zig+10-6
......@@ -15,16 +15,20 @@ pub fn build(b: *std.Build) void {
1515
1616 const main = b.addExecutable(.{
1717 .name = "main",
18 .root_source_file = b.path("main.zig"),
19 .optimize = optimize,
20 .target = target,
18 .root_module = b.createModule(.{
19 .root_source_file = b.path("main.zig"),
20 .optimize = optimize,
21 .target = target,
22 }),
2123 });
2224
2325 const create_symlink_exe = b.addExecutable(.{
2426 .name = "create-symlink",
25 .root_source_file = b.path("create-symlink.zig"),
26 .optimize = optimize,
27 .target = target,
27 .root_module = b.createModule(.{
28 .root_source_file = b.path("create-symlink.zig"),
29 .optimize = optimize,
30 .target = target,
31 }),
2832 });
2933
3034 var run_create_symlink = b.addRunArtifact(create_symlink_exe);
test/standalone/shared_library/build.zig+13-8
......@@ -8,23 +8,28 @@ pub fn build(b: *std.Build) void {
88 const target = b.graph.host;
99 const lib = b.addSharedLibrary(.{
1010 .name = "mathtest",
11 .root_source_file = b.path("mathtest.zig"),
1211 .version = .{ .major = 1, .minor = 0, .patch = 0 },
13 .target = target,
14 .optimize = optimize,
12 .root_module = b.createModule(.{
13 .root_source_file = b.path("mathtest.zig"),
14 .target = target,
15 .optimize = optimize,
16 }),
1517 });
1618
1719 const exe = b.addExecutable(.{
1820 .name = "test",
19 .target = target,
20 .optimize = optimize,
21 .root_module = b.createModule(.{
22 .root_source_file = null,
23 .target = target,
24 .optimize = optimize,
25 .link_libc = true,
26 }),
2127 });
22 exe.addCSourceFile(.{
28 exe.root_module.addCSourceFile(.{
2329 .file = b.path("test.c"),
2430 .flags = &[_][]const u8{"-std=c99"},
2531 });
26 exe.linkLibrary(lib);
27 exe.linkSystemLibrary("c");
32 exe.root_module.linkLibrary(lib);
2833
2934 const run_cmd = b.addRunArtifact(exe);
3035 test_step.dependOn(&run_cmd.step);
test/standalone/simple/build.zig+12-8
......@@ -42,11 +42,13 @@ pub fn build(b: *std.Build) void {
4242 if (case.is_exe) {
4343 const exe = b.addExecutable(.{
4444 .name = std.fs.path.stem(case.src_path),
45 .root_source_file = b.path(case.src_path),
46 .optimize = optimize,
47 .target = resolved_target,
45 .root_module = b.createModule(.{
46 .root_source_file = b.path(case.src_path),
47 .optimize = optimize,
48 .target = resolved_target,
49 }),
4850 });
49 if (case.link_libc) exe.linkLibC();
51 if (case.link_libc) exe.root_module.link_libc = true;
5052
5153 _ = exe.getEmittedBin();
5254
......@@ -56,11 +58,13 @@ pub fn build(b: *std.Build) void {
5658 if (case.is_test) {
5759 const exe = b.addTest(.{
5860 .name = std.fs.path.stem(case.src_path),
59 .root_source_file = b.path(case.src_path),
60 .optimize = optimize,
61 .target = resolved_target,
61 .root_module = b.createModule(.{
62 .root_source_file = b.path(case.src_path),
63 .optimize = optimize,
64 .target = resolved_target,
65 }),
6266 });
63 if (case.link_libc) exe.linkLibC();
67 if (case.link_libc) exe.root_module.link_libc = true;
6468
6569 const run = b.addRunArtifact(exe);
6670 step.dependOn(&run.step);
test/standalone/stack_iterator/build.zig+39-28
......@@ -20,11 +20,13 @@ pub fn build(b: *std.Build) void {
2020 {
2121 const exe = b.addExecutable(.{
2222 .name = "unwind_fp",
23 .root_source_file = b.path("unwind.zig"),
24 .target = target,
25 .optimize = optimize,
26 .unwind_tables = if (target.result.isDarwin()) .@"async" else null,
27 .omit_frame_pointer = false,
23 .root_module = b.createModule(.{
24 .root_source_file = b.path("unwind.zig"),
25 .target = target,
26 .optimize = optimize,
27 .unwind_tables = if (target.result.isDarwin()) .@"async" else null,
28 .omit_frame_pointer = false,
29 }),
2830 });
2931
3032 const run_cmd = b.addRunArtifact(exe);
......@@ -43,11 +45,13 @@ pub fn build(b: *std.Build) void {
4345 {
4446 const exe = b.addExecutable(.{
4547 .name = "unwind_nofp",
46 .root_source_file = b.path("unwind.zig"),
47 .target = target,
48 .optimize = optimize,
49 .unwind_tables = .@"async",
50 .omit_frame_pointer = true,
48 .root_module = b.createModule(.{
49 .root_source_file = b.path("unwind.zig"),
50 .target = target,
51 .optimize = optimize,
52 .unwind_tables = .@"async",
53 .omit_frame_pointer = true,
54 }),
5155 });
5256
5357 const run_cmd = b.addRunArtifact(exe);
......@@ -66,27 +70,32 @@ pub fn build(b: *std.Build) void {
6670 {
6771 const c_shared_lib = b.addSharedLibrary(.{
6872 .name = "c_shared_lib",
69 .target = target,
70 .optimize = optimize,
71 .strip = false,
73 .root_module = b.createModule(.{
74 .root_source_file = null,
75 .target = target,
76 .optimize = optimize,
77 .link_libc = true,
78 .strip = false,
79 }),
7280 });
7381
7482 if (target.result.os.tag == .windows)
75 c_shared_lib.defineCMacro("LIB_API", "__declspec(dllexport)");
83 c_shared_lib.root_module.addCMacro("LIB_API", "__declspec(dllexport)");
7684
77 c_shared_lib.addCSourceFile(.{
85 c_shared_lib.root_module.addCSourceFile(.{
7886 .file = b.path("shared_lib.c"),
7987 .flags = &.{"-fomit-frame-pointer"},
8088 });
81 c_shared_lib.linkLibC();
8289
8390 const exe = b.addExecutable(.{
8491 .name = "shared_lib_unwind",
85 .root_source_file = b.path("shared_lib_unwind.zig"),
86 .target = target,
87 .optimize = optimize,
88 .unwind_tables = if (target.result.isDarwin()) .@"async" else null,
89 .omit_frame_pointer = true,
92 .root_module = b.createModule(.{
93 .root_source_file = b.path("shared_lib_unwind.zig"),
94 .target = target,
95 .optimize = optimize,
96 .unwind_tables = if (target.result.isDarwin()) .@"async" else null,
97 .omit_frame_pointer = true,
98 }),
9099 });
91100
92101 exe.linkLibrary(c_shared_lib);
......@@ -117,14 +126,16 @@ pub fn build(b: *std.Build) void {
117126 inline for (no_os_targets) |os_tag| {
118127 const exe = b.addExecutable(.{
119128 .name = "unwind_freestanding",
120 .root_source_file = b.path("unwind_freestanding.zig"),
121 .target = b.resolveTargetQuery(.{
122 .cpu_arch = .x86_64,
123 .os_tag = os_tag,
129 .root_module = b.createModule(.{
130 .root_source_file = b.path("unwind_freestanding.zig"),
131 .target = b.resolveTargetQuery(.{
132 .cpu_arch = .x86_64,
133 .os_tag = os_tag,
134 }),
135 .optimize = optimize,
136 .unwind_tables = null,
137 .omit_frame_pointer = false,
124138 }),
125 .optimize = optimize,
126 .unwind_tables = null,
127 .omit_frame_pointer = false,
128139 });
129140
130141 // This "freestanding" binary is runnable because it invokes the
test/standalone/static_c_lib/build.zig+12-8
......@@ -8,18 +8,22 @@ pub fn build(b: *std.Build) void {
88
99 const foo = b.addStaticLibrary(.{
1010 .name = "foo",
11 .optimize = optimize,
12 .target = b.graph.host,
11 .root_module = b.createModule(.{
12 .root_source_file = null,
13 .optimize = optimize,
14 .target = b.graph.host,
15 }),
1316 });
14 foo.addCSourceFile(.{ .file = b.path("foo.c"), .flags = &[_][]const u8{} });
15 foo.addIncludePath(b.path("."));
17 foo.root_module.addCSourceFile(.{ .file = b.path("foo.c"), .flags = &[_][]const u8{} });
18 foo.root_module.addIncludePath(b.path("."));
1619
17 const test_exe = b.addTest(.{
20 const test_exe = b.addTest(.{ .root_module = b.createModule(.{
1821 .root_source_file = b.path("foo.zig"),
22 .target = b.graph.host,
1923 .optimize = optimize,
20 });
21 test_exe.linkLibrary(foo);
22 test_exe.addIncludePath(b.path("."));
24 }) });
25 test_exe.root_module.linkLibrary(foo);
26 test_exe.root_module.addIncludePath(b.path("."));
2327
2428 test_step.dependOn(&b.addRunArtifact(test_exe).step);
2529}
test/standalone/strip_empty_loop/build.zig+6-4
......@@ -9,10 +9,12 @@ pub fn build(b: *std.Build) void {
99
1010 const main = b.addExecutable(.{
1111 .name = "main",
12 .root_source_file = b.path("main.zig"),
13 .optimize = optimize,
14 .target = target,
15 .strip = true,
12 .root_module = b.createModule(.{
13 .root_source_file = b.path("main.zig"),
14 .optimize = optimize,
15 .target = target,
16 .strip = true,
17 }),
1618 });
1719
1820 // TODO: actually check the output
test/standalone/strip_struct_init/build.zig+6-3
......@@ -7,9 +7,12 @@ pub fn build(b: *std.Build) void {
77 const optimize: std.builtin.OptimizeMode = .Debug;
88
99 const main = b.addTest(.{
10 .root_source_file = b.path("main.zig"),
11 .optimize = optimize,
12 .strip = true,
10 .root_module = b.createModule(.{
11 .root_source_file = b.path("main.zig"),
12 .target = b.graph.host,
13 .optimize = optimize,
14 .strip = true,
15 }),
1316 });
1417
1518 test_step.dependOn(&b.addRunArtifact(main).step);
test/standalone/test_runner_module_imports/build.zig+10-8
......@@ -1,18 +1,20 @@
11const std = @import("std");
22
33pub fn build(b: *std.Build) void {
4 const t = b.addTest(.{
4 const test_mod = b.createModule(.{
55 .root_source_file = b.path("src/main.zig"),
6 .test_runner = b.path("test_runner/main.zig"),
6 .target = b.graph.host,
77 });
8
98 const module1 = b.createModule(.{ .root_source_file = b.path("module1/main.zig") });
10 const module2 = b.createModule(.{
11 .root_source_file = b.path("module2/main.zig"),
12 .imports = &.{.{ .name = "module1", .module = module1 }},
13 });
9 const module2 = b.createModule(.{ .root_source_file = b.path("module2/main.zig") });
1410
15 t.root_module.addImport("module2", module2);
11 module2.addImport("module1", module1);
12 test_mod.addImport("module2", module2);
13
14 const t = b.addTest(.{
15 .root_module = test_mod,
16 .test_runner = b.path("test_runner/main.zig"),
17 });
1618
1719 const test_step = b.step("test", "Run unit tests");
1820 test_step.dependOn(&b.addRunArtifact(t).step);
test/standalone/test_runner_path/build.zig+3-2
......@@ -6,9 +6,10 @@ pub fn build(b: *std.Build) void {
66 const test_step = b.step("test", "Test the program");
77 b.default_step = test_step;
88
9 const test_exe = b.addTest(.{
9 const test_exe = b.addTest(.{ .root_module = b.createModule(.{
10 .target = b.graph.host,
1011 .root_source_file = b.path("test.zig"),
11 });
12 }) });
1213 test_exe.test_runner = b.path("test_runner.zig");
1314
1415 const test_run = b.addRunArtifact(test_exe);
test/standalone/use_alias/build.zig+4-3
......@@ -6,11 +6,12 @@ pub fn build(b: *std.Build) void {
66
77 const optimize: std.builtin.OptimizeMode = .Debug;
88
9 const main = b.addTest(.{
9 const main = b.addTest(.{ .root_module = b.createModule(.{
1010 .root_source_file = b.path("main.zig"),
11 .target = b.graph.host,
1112 .optimize = optimize,
12 });
13 main.addIncludePath(b.path("."));
13 }) });
14 main.root_module.addIncludePath(b.path("."));
1415
1516 test_step.dependOn(&b.addRunArtifact(main).step);
1617}
test/standalone/windows_argv/build.zig+35-23
......@@ -11,32 +11,39 @@ pub fn build(b: *std.Build) !void {
1111
1212 const lib_gnu = b.addStaticLibrary(.{
1313 .name = "toargv-gnu",
14 .root_source_file = b.path("lib.zig"),
15 .target = b.resolveTargetQuery(.{
16 .abi = .gnu,
14 .root_module = b.createModule(.{
15 .root_source_file = b.path("lib.zig"),
16 .target = b.resolveTargetQuery(.{
17 .abi = .gnu,
18 }),
19 .optimize = optimize,
1720 }),
18 .optimize = optimize,
1921 });
2022 const verify_gnu = b.addExecutable(.{
2123 .name = "verify-gnu",
22 .target = b.resolveTargetQuery(.{
23 .abi = .gnu,
24 .root_module = b.createModule(.{
25 .root_source_file = null,
26 .target = b.resolveTargetQuery(.{
27 .abi = .gnu,
28 }),
29 .optimize = optimize,
2430 }),
25 .optimize = optimize,
2631 });
27 verify_gnu.addCSourceFile(.{
32 verify_gnu.root_module.addCSourceFile(.{
2833 .file = b.path("verify.c"),
2934 .flags = &.{ "-DUNICODE", "-D_UNICODE" },
3035 });
3136 verify_gnu.mingw_unicode_entry_point = true;
32 verify_gnu.linkLibrary(lib_gnu);
33 verify_gnu.linkLibC();
37 verify_gnu.root_module.linkLibrary(lib_gnu);
38 verify_gnu.root_module.link_libc = true;
3439
3540 const fuzz = b.addExecutable(.{
3641 .name = "fuzz",
37 .root_source_file = b.path("fuzz.zig"),
38 .target = b.graph.host,
39 .optimize = optimize,
42 .root_module = b.createModule(.{
43 .root_source_file = b.path("fuzz.zig"),
44 .target = b.graph.host,
45 .optimize = optimize,
46 }),
4047 });
4148
4249 const fuzz_max_iterations = b.option(u64, "iterations", "The max fuzz iterations (default: 100)") orelse 100;
......@@ -69,25 +76,30 @@ pub fn build(b: *std.Build) !void {
6976 if (has_msvc) {
7077 const lib_msvc = b.addStaticLibrary(.{
7178 .name = "toargv-msvc",
72 .root_source_file = b.path("lib.zig"),
73 .target = b.resolveTargetQuery(.{
74 .abi = .msvc,
79 .root_module = b.createModule(.{
80 .root_source_file = b.path("lib.zig"),
81 .target = b.resolveTargetQuery(.{
82 .abi = .msvc,
83 }),
84 .optimize = optimize,
7585 }),
76 .optimize = optimize,
7786 });
7887 const verify_msvc = b.addExecutable(.{
7988 .name = "verify-msvc",
80 .target = b.resolveTargetQuery(.{
81 .abi = .msvc,
89 .root_module = b.createModule(.{
90 .root_source_file = null,
91 .target = b.resolveTargetQuery(.{
92 .abi = .msvc,
93 }),
94 .optimize = optimize,
8295 }),
83 .optimize = optimize,
8496 });
85 verify_msvc.addCSourceFile(.{
97 verify_msvc.root_module.addCSourceFile(.{
8698 .file = b.path("verify.c"),
8799 .flags = &.{ "-DUNICODE", "-D_UNICODE" },
88100 });
89 verify_msvc.linkLibrary(lib_msvc);
90 verify_msvc.linkLibC();
101 verify_msvc.root_module.linkLibrary(lib_msvc);
102 verify_msvc.root_module.link_libc = true;
91103
92104 const run_msvc = b.addRunArtifact(fuzz);
93105 run_msvc.setName("fuzz-msvc");
test/standalone/windows_bat_args/build.zig+15-9
......@@ -12,16 +12,20 @@ pub fn build(b: *std.Build) !void {
1212
1313 const echo_args = b.addExecutable(.{
1414 .name = "echo-args",
15 .root_source_file = b.path("echo-args.zig"),
16 .optimize = optimize,
17 .target = target,
15 .root_module = b.createModule(.{
16 .root_source_file = b.path("echo-args.zig"),
17 .optimize = optimize,
18 .target = target,
19 }),
1820 });
1921
2022 const test_exe = b.addExecutable(.{
2123 .name = "test",
22 .root_source_file = b.path("test.zig"),
23 .optimize = optimize,
24 .target = target,
24 .root_module = b.createModule(.{
25 .root_source_file = b.path("test.zig"),
26 .optimize = optimize,
27 .target = target,
28 }),
2529 });
2630
2731 const run = b.addRunArtifact(test_exe);
......@@ -33,9 +37,11 @@ pub fn build(b: *std.Build) !void {
3337
3438 const fuzz = b.addExecutable(.{
3539 .name = "fuzz",
36 .root_source_file = b.path("fuzz.zig"),
37 .optimize = optimize,
38 .target = target,
40 .root_module = b.createModule(.{
41 .root_source_file = b.path("fuzz.zig"),
42 .optimize = optimize,
43 .target = target,
44 }),
3945 });
4046
4147 const fuzz_max_iterations = b.option(u64, "iterations", "The max fuzz iterations (default: 100)") orelse 100;
test/standalone/windows_entry_points/build.zig+28-16
......@@ -13,11 +13,14 @@ pub fn build(b: *std.Build) void {
1313 {
1414 const exe = b.addExecutable(.{
1515 .name = "main",
16 .target = target,
17 .optimize = .Debug,
18 .link_libc = true,
16 .root_module = b.createModule(.{
17 .root_source_file = null,
18 .target = target,
19 .optimize = .Debug,
20 .link_libc = true,
21 }),
1922 });
20 exe.addCSourceFile(.{ .file = b.path("main.c") });
23 exe.root_module.addCSourceFile(.{ .file = b.path("main.c") });
2124
2225 _ = exe.getEmittedBin();
2326 test_step.dependOn(&exe.step);
......@@ -26,12 +29,15 @@ pub fn build(b: *std.Build) void {
2629 {
2730 const exe = b.addExecutable(.{
2831 .name = "wmain",
29 .target = target,
30 .optimize = .Debug,
31 .link_libc = true,
32 .root_module = b.createModule(.{
33 .root_source_file = null,
34 .target = target,
35 .optimize = .Debug,
36 .link_libc = true,
37 }),
3238 });
3339 exe.mingw_unicode_entry_point = true;
34 exe.addCSourceFile(.{ .file = b.path("wmain.c") });
40 exe.root_module.addCSourceFile(.{ .file = b.path("wmain.c") });
3541
3642 _ = exe.getEmittedBin();
3743 test_step.dependOn(&exe.step);
......@@ -40,12 +46,15 @@ pub fn build(b: *std.Build) void {
4046 {
4147 const exe = b.addExecutable(.{
4248 .name = "winmain",
43 .target = target,
44 .optimize = .Debug,
45 .link_libc = true,
49 .root_module = b.createModule(.{
50 .root_source_file = null,
51 .target = target,
52 .optimize = .Debug,
53 .link_libc = true,
54 }),
4655 });
4756 // Note: `exe.subsystem = .Windows;` is not necessary
48 exe.addCSourceFile(.{ .file = b.path("winmain.c") });
57 exe.root_module.addCSourceFile(.{ .file = b.path("winmain.c") });
4958
5059 _ = exe.getEmittedBin();
5160 test_step.dependOn(&exe.step);
......@@ -54,13 +63,16 @@ pub fn build(b: *std.Build) void {
5463 {
5564 const exe = b.addExecutable(.{
5665 .name = "wwinmain",
57 .target = target,
58 .optimize = .Debug,
59 .link_libc = true,
66 .root_module = b.createModule(.{
67 .root_source_file = null,
68 .target = target,
69 .optimize = .Debug,
70 .link_libc = true,
71 }),
6072 });
6173 exe.mingw_unicode_entry_point = true;
6274 // Note: `exe.subsystem = .Windows;` is not necessary
63 exe.addCSourceFile(.{ .file = b.path("wwinmain.c") });
75 exe.root_module.addCSourceFile(.{ .file = b.path("wwinmain.c") });
6476
6577 _ = exe.getEmittedBin();
6678 test_step.dependOn(&exe.step);
test/standalone/windows_resources/build.zig+6-4
......@@ -28,11 +28,13 @@ fn add(
2828) void {
2929 const exe = b.addExecutable(.{
3030 .name = "zig_resource_test",
31 .root_source_file = b.path("main.zig"),
32 .target = target,
33 .optimize = .Debug,
31 .root_module = b.createModule(.{
32 .root_source_file = b.path("main.zig"),
33 .target = target,
34 .optimize = .Debug,
35 }),
3436 });
35 exe.addWin32ResourceFile(.{
37 exe.root_module.addWin32ResourceFile(.{
3638 .file = b.path("res/zig.rc"),
3739 .flags = &.{"/c65001"}, // UTF-8 code page
3840 .include_paths = &.{
test/standalone/windows_spawn/build.zig+10-6
......@@ -12,16 +12,20 @@ pub fn build(b: *std.Build) void {
1212
1313 const hello = b.addExecutable(.{
1414 .name = "hello",
15 .root_source_file = b.path("hello.zig"),
16 .optimize = optimize,
17 .target = target,
15 .root_module = b.createModule(.{
16 .root_source_file = b.path("hello.zig"),
17 .optimize = optimize,
18 .target = target,
19 }),
1820 });
1921
2022 const main = b.addExecutable(.{
2123 .name = "main",
22 .root_source_file = b.path("main.zig"),
23 .optimize = optimize,
24 .target = target,
24 .root_module = b.createModule(.{
25 .root_source_file = b.path("main.zig"),
26 .optimize = optimize,
27 .target = target,
28 }),
2529 });
2630
2731 const run = b.addRunArtifact(main);
test/standalone/zerolength_check/build.zig+2-2
......@@ -11,7 +11,7 @@ pub fn build(b: *std.Build) void {
1111}
1212
1313fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
14 const unit_tests = b.addTest(.{
14 const unit_tests = b.addTest(.{ .root_module = b.createModule(.{
1515 .root_source_file = b.path("src/main.zig"),
1616 .target = b.resolveTargetQuery(.{
1717 .os_tag = .wasi,
......@@ -19,7 +19,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1919 .cpu_features_add = std.Target.wasm.featureSet(&.{.bulk_memory}),
2020 }),
2121 .optimize = optimize,
22 });
22 }) });
2323
2424 const run_unit_tests = b.addRunArtifact(unit_tests);
2525 run_unit_tests.skip_foreign_checks = true;
test/tests.zig+51-36
......@@ -1106,9 +1106,11 @@ pub fn addStackTraceTests(
11061106) *Step {
11071107 const check_exe = b.addExecutable(.{
11081108 .name = "check-stack-trace",
1109 .root_source_file = b.path("test/src/check-stack-trace.zig"),
1110 .target = b.graph.host,
1111 .optimize = .Debug,
1109 .root_module = b.createModule(.{
1110 .root_source_file = b.path("test/src/check-stack-trace.zig"),
1111 .target = b.graph.host,
1112 .optimize = .Debug,
1113 }),
11121114 });
11131115
11141116 const cases = b.allocator.create(StackTracesContext) catch @panic("OOM");
......@@ -1330,7 +1332,7 @@ pub fn addCliTests(b: *std.Build) *Step {
13301332 run5.step.dependOn(&run4.step);
13311333
13321334 const unformatted_code_utf16 = "\xff\xfe \x00 \x00 \x00 \x00/\x00/\x00 \x00n\x00o\x00 \x00r\x00e\x00a\x00s\x00o\x00n\x00";
1333 const fmt6_path = std.fs.path.join(b.allocator, &.{ tmp_path, "fmt6.zig" }) catch @panic("OOM");
1335 const fmt6_path = b.pathJoin(&.{ tmp_path, "fmt6.zig" });
13341336 const write6 = b.addUpdateSourceFiles();
13351337 write6.addBytesToSource(unformatted_code_utf16, fmt6_path);
13361338 write6.step.dependOn(&run5.step);
......@@ -1514,18 +1516,20 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
15141516 options.max_rss;
15151517
15161518 const these_tests = b.addTest(.{
1517 .root_source_file = b.path(options.root_src),
1518 .optimize = test_target.optimize_mode,
1519 .target = resolved_target,
1519 .root_module = b.createModule(.{
1520 .root_source_file = b.path(options.root_src),
1521 .optimize = test_target.optimize_mode,
1522 .target = resolved_target,
1523 .link_libc = test_target.link_libc,
1524 .pic = test_target.pic,
1525 .strip = test_target.strip,
1526 .single_threaded = test_target.single_threaded,
1527 }),
15201528 .max_rss = max_rss,
15211529 .filters = options.test_filters,
1522 .link_libc = test_target.link_libc,
1523 .single_threaded = test_target.single_threaded,
15241530 .use_llvm = test_target.use_llvm,
15251531 .use_lld = test_target.use_lld,
15261532 .zig_lib_dir = b.path("lib"),
1527 .pic = test_target.pic,
1528 .strip = test_target.strip,
15291533 });
15301534 if (options.no_builtin) these_tests.no_builtin = true;
15311535 if (options.build_options) |build_options| {
......@@ -1561,12 +1565,17 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
15611565 var altered_query = test_target.target;
15621566 altered_query.ofmt = null;
15631567
1564 const compile_c = b.addExecutable(.{
1565 .name = qualified_name,
1568 const compile_c = b.createModule(.{
1569 .root_source_file = null,
15661570 .link_libc = test_target.link_libc,
15671571 .target = b.resolveTargetQuery(altered_query),
1572 });
1573 const compile_c_exe = b.addExecutable(.{
1574 .name = qualified_name,
1575 .root_module = compile_c,
15681576 .zig_lib_dir = b.path("lib"),
15691577 });
1578
15701579 compile_c.addCSourceFile(.{
15711580 .file = these_tests.getEmittedBin(),
15721581 .flags = &.{
......@@ -1611,22 +1620,22 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
16111620 continue;
16121621 }
16131622 if (test_target.link_libc == false) {
1614 compile_c.subsystem = .Console;
1615 compile_c.linkSystemLibrary("kernel32");
1616 compile_c.linkSystemLibrary("ntdll");
1623 compile_c_exe.subsystem = .Console;
1624 compile_c.linkSystemLibrary("kernel32", .{});
1625 compile_c.linkSystemLibrary("ntdll", .{});
16171626 }
16181627 if (mem.eql(u8, options.name, "std")) {
16191628 if (test_target.link_libc == false) {
1620 compile_c.linkSystemLibrary("shell32");
1621 compile_c.linkSystemLibrary("advapi32");
1629 compile_c.linkSystemLibrary("shell32", .{});
1630 compile_c.linkSystemLibrary("advapi32", .{});
16221631 }
1623 compile_c.linkSystemLibrary("crypt32");
1624 compile_c.linkSystemLibrary("ws2_32");
1625 compile_c.linkSystemLibrary("ole32");
1632 compile_c.linkSystemLibrary("crypt32", .{});
1633 compile_c.linkSystemLibrary("ws2_32", .{});
1634 compile_c.linkSystemLibrary("ole32", .{});
16261635 }
16271636 }
16281637
1629 const run = b.addRunArtifact(compile_c);
1638 const run = b.addRunArtifact(compile_c_exe);
16301639 run.skip_foreign_checks = true;
16311640 run.enableTestRunnerMode();
16321641 run.setName(b.fmt("run test {s}", .{qualified_name}));
......@@ -1675,6 +1684,20 @@ pub fn addCAbiTests(b: *std.Build, options: CAbiTestOptions) *Step {
16751684 continue;
16761685 }
16771686
1687 const test_mod = b.createModule(.{
1688 .root_source_file = b.path("test/c_abi/main.zig"),
1689 .target = resolved_target,
1690 .optimize = optimize_mode,
1691 .link_libc = true,
1692 .pic = c_abi_target.pic,
1693 .strip = c_abi_target.strip,
1694 });
1695 test_mod.addCSourceFile(.{
1696 .file = b.path("test/c_abi/cfuncs.c"),
1697 .flags = &.{"-std=c99"},
1698 });
1699 for (c_abi_target.c_defines) |define| test_mod.addCMacro(define, "1");
1700
16781701 const test_step = b.addTest(.{
16791702 .name = b.fmt("test-c-abi-{s}-{s}-{s}{s}{s}{s}", .{
16801703 triple_txt,
......@@ -1691,20 +1714,10 @@ pub fn addCAbiTests(b: *std.Build, options: CAbiTestOptions) *Step {
16911714 if (c_abi_target.use_lld == false) "-no-lld" else "",
16921715 if (c_abi_target.pic == true) "-pic" else "",
16931716 }),
1694 .root_source_file = b.path("test/c_abi/main.zig"),
1695 .target = resolved_target,
1696 .optimize = optimize_mode,
1697 .link_libc = true,
1717 .root_module = test_mod,
16981718 .use_llvm = c_abi_target.use_llvm,
16991719 .use_lld = c_abi_target.use_lld,
1700 .pic = c_abi_target.pic,
1701 .strip = c_abi_target.strip,
17021720 });
1703 test_step.addCSourceFile(.{
1704 .file = b.path("test/c_abi/cfuncs.c"),
1705 .flags = &.{"-std=c99"},
1706 });
1707 for (c_abi_target.c_defines) |define| test_step.defineCMacro(define, null);
17081721
17091722 // This test is intentionally trying to check if the external ABI is
17101723 // done properly. LTO would be a hindrance to this.
......@@ -1784,9 +1797,11 @@ pub fn addDebuggerTests(b: *std.Build, options: DebuggerContext.Options) ?*Step
17841797pub fn addIncrementalTests(b: *std.Build, test_step: *Step) !void {
17851798 const incr_check = b.addExecutable(.{
17861799 .name = "incr-check",
1787 .root_source_file = b.path("tools/incr-check.zig"),
1788 .target = b.graph.host,
1789 .optimize = .Debug,
1800 .root_module = b.createModule(.{
1801 .root_source_file = b.path("tools/incr-check.zig"),
1802 .target = b.graph.host,
1803 .optimize = .Debug,
1804 }),
17901805 });
17911806
17921807 var dir = try b.build_root.handle.openDir("test/incremental", .{ .iterate = true });