From 3d393dba6fc06cea3508aaa3db9d49042663367e Mon Sep 17 00:00:00 2001 From: Eric Joldasov Date: Fri, 21 Jun 2024 09:36:45 +0500 Subject: [PATCH 01/16] std.Build: remove deprecated APIs These APIs were all deprecated prior to the release of 0.13.0, so can be safety removed in the current release cycle. `std.Build`: * `host` -> `graph.host` `std.Build.Step.Compile`: * `setLinkerScriptPath` -> `setLinkerScript` * `defineCMacro` -> `root_module.addCMacro` * `linkFrameworkNeeded`-> `root_module.linkFramework` * `linkFrameworkWeak`-> `root_module.linkFramework` `std.Build.Step.ObjCopy`: * `getOutputSource` -> `getOutput` `std.Build.Step.Options`: * `addOptionArtifact` -> `addOptionPath` * `getSource` -> `getOutput` `std.Build.Step.Run`: * `extra_file_dependencies` -> `addFileInput` * `addDirectorySourceArg` -> `addDirectoryArg` * `addPrefixedDirectorySourceArg` -> `addPrefixedDirectoryArg` --- lib/std/Build.zig | 5 ----- lib/std/Build/Step/Compile.zig | 18 ------------------ lib/std/Build/Step/ObjCopy.zig | 3 --- lib/std/Build/Step/Options.zig | 8 -------- lib/std/Build/Step/Run.zig | 13 ------------- test/standalone/extern/build.zig | 2 +- test/standalone/issue_11595/build.zig | 12 ++++++------ test/standalone/stack_iterator/build.zig | 2 +- test/tests.zig | 2 +- 9 files changed, 9 insertions(+), 56 deletions(-) diff --git a/lib/std/Build.zig b/lib/std/Build.zig index 65e2db3d1d2b8672fd801fe55cdfea34a45e7ec8..9d8c7455a3f95fef3ca550f588abba47f29d7c29 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -81,9 +81,6 @@ enable_wine: bool = false, /// that contains the path `aarch64-linux-gnu/lib/ld-linux-aarch64.so.1`. glibc_runtimes_dir: ?[]const u8 = null, -/// Deprecated. Use `b.graph.host`. -host: ResolvedTarget, - dep_prefix: []const u8 = "", modules: std.StringArrayHashMap(*Module), @@ -301,7 +298,6 @@ pub fn create( }, .install_path = undefined, .args = null, - .host = graph.host, .modules = .init(arena), .named_writefiles = .init(arena), .named_lazy_paths = .init(arena), @@ -393,7 +389,6 @@ fn createChildOnly( .enable_wasmtime = parent.enable_wasmtime, .enable_wine = parent.enable_wine, .glibc_runtimes_dir = parent.glibc_runtimes_dir, - .host = parent.host, .dep_prefix = parent.fmt("{s}{s}.", .{ parent.dep_prefix, dep_name }), .modules = .init(allocator), .named_writefiles = .init(allocator), diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index 1dd444abf2e42261f2197a380f6b59ea59f4324b..a85a67bd9b3e256cac0ebc919c9d970ffe06ea6d 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -583,9 +583,6 @@ pub fn checkObject(compile: *Compile) *Step.CheckObject { return Step.CheckObject.create(compile.step.owner, compile.getEmittedBin(), compile.rootModuleTarget().ofmt); } -/// deprecated: use `setLinkerScript` -pub const setLinkerScriptPath = setLinkerScript; - pub fn setLinkerScript(compile: *Compile, source: LazyPath) void { const b = compile.step.owner; compile.linker_script = source.dupe(b); @@ -675,11 +672,6 @@ pub fn linkLibCpp(compile: *Compile) void { compile.root_module.link_libcpp = true; } -/// Deprecated. Use `c.root_module.addCMacro`. -pub fn defineCMacro(c: *Compile, name: []const u8, value: ?[]const u8) void { - c.root_module.addCMacro(name, value orelse "1"); -} - const PkgConfigResult = struct { cflags: []const []const u8, libs: []const []const u8, @@ -805,16 +797,6 @@ pub fn linkFramework(c: *Compile, name: []const u8) void { c.root_module.linkFramework(name, .{}); } -/// Deprecated. Use `c.root_module.linkFramework`. -pub fn linkFrameworkNeeded(c: *Compile, name: []const u8) void { - c.root_module.linkFramework(name, .{ .needed = true }); -} - -/// Deprecated. Use `c.root_module.linkFramework`. -pub fn linkFrameworkWeak(c: *Compile, name: []const u8) void { - c.root_module.linkFramework(name, .{ .weak = true }); -} - /// Handy when you have many C/C++ source files and want them all to have the same flags. pub fn addCSourceFiles(compile: *Compile, options: Module.AddCSourceFilesOptions) void { compile.root_module.addCSourceFiles(options); diff --git a/lib/std/Build/Step/ObjCopy.zig b/lib/std/Build/Step/ObjCopy.zig index 8f0f62e222b14565fb9b8513a1e149ea3551b669..9b2c2d596ff5abc99767e631d174a270d1985183 100644 --- a/lib/std/Build/Step/ObjCopy.zig +++ b/lib/std/Build/Step/ObjCopy.zig @@ -135,9 +135,6 @@ pub fn create( return objcopy; } -/// deprecated: use getOutput -pub const getOutputSource = getOutput; - pub fn getOutput(objcopy: *const ObjCopy) std.Build.LazyPath { return .{ .generated = .{ .file = &objcopy.output_file } }; } diff --git a/lib/std/Build/Step/Options.zig b/lib/std/Build/Step/Options.zig index 41739dba471e49183e74f3f049fc417fae08b8cd..44228bef2084a43041547aeaaf8f081e1309147c 100644 --- a/lib/std/Build/Step/Options.zig +++ b/lib/std/Build/Step/Options.zig @@ -390,20 +390,12 @@ pub fn addOptionPath( path.addStepDependencies(&options.step); } -/// Deprecated: use `addOptionPath(options, name, artifact.getEmittedBin())` instead. -pub fn addOptionArtifact(options: *Options, name: []const u8, artifact: *Step.Compile) void { - return addOptionPath(options, name, artifact.getEmittedBin()); -} - pub fn createModule(options: *Options) *std.Build.Module { return options.step.owner.createModule(.{ .root_source_file = options.getOutput(), }); } -/// deprecated: use `getOutput` -pub const getSource = getOutput; - /// Returns the main artifact of this Build Step which is a Zig source file /// generated from the key-value pairs of the Options. pub fn getOutput(options: *Options) LazyPath { diff --git a/lib/std/Build/Step/Run.zig b/lib/std/Build/Step/Run.zig index 6a2ebc6e9f22daafa0bc5e5feeebc70692f8e3d9..270090d07c3f595adbd0b2cdea6e43b35352a030 100644 --- a/lib/std/Build/Step/Run.zig +++ b/lib/std/Build/Step/Run.zig @@ -43,9 +43,6 @@ stdio: StdIo, /// It should be only set using `setStdIn`. stdin: StdIn, -/// Deprecated: use `addFileInput` -extra_file_dependencies: []const []const u8, - /// Additional input files that, when modified, indicate that the Run step /// should be re-executed. /// 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 { .disable_zig_progress = false, .stdio = .infer_from_args, .stdin = .none, - .extra_file_dependencies = &.{}, .file_inputs = .{}, .rename_step_with_output_arg = true, .skip_foreign_checks = false, @@ -364,16 +360,10 @@ pub fn addPrefixedOutputDirectoryArg( return .{ .generated = .{ .file = &output.generated_file } }; } -/// deprecated: use `addDirectoryArg` -pub const addDirectorySourceArg = addDirectoryArg; - pub fn addDirectoryArg(run: *Run, directory_source: std.Build.LazyPath) void { run.addPrefixedDirectoryArg("", directory_source); } -// deprecated: use `addPrefixedDirectoryArg` -pub const addPrefixedDirectorySourceArg = addPrefixedDirectoryArg; - pub fn addPrefixedDirectoryArg(run: *Run, prefix: []const u8, directory_source: std.Build.LazyPath) void { const b = run.step.owner; @@ -698,9 +688,6 @@ fn make(step: *Step, options: Step.MakeOptions) !void { hashStdIo(&man.hash, run.stdio); - for (run.extra_file_dependencies) |file_path| { - _ = try man.addFile(b.pathFromRoot(file_path), null); - } for (run.file_inputs.items) |lazy_path| { _ = try man.addFile(lazy_path.getPath2(b, step), null); } diff --git a/test/standalone/extern/build.zig b/test/standalone/extern/build.zig index ff8701bf40c0c255cd2e0585ed0adfcd24606d6f..1886408933e347e9f512492328d70121c736f83c 100644 --- a/test/standalone/extern/build.zig +++ b/test/standalone/extern/build.zig @@ -18,7 +18,7 @@ pub fn build(b: *std.Build) void { .optimize = optimize, .link_libc = true, }); - if (b.graph.host.result.abi == .msvc) shared.defineCMacro("API", "__declspec(dllexport)"); + if (b.graph.host.result.abi == .msvc) shared.root_module.addCMacro("API", "__declspec(dllexport)"); shared.addCSourceFile(.{ .file = b.path("shared.c"), .flags = &.{} }); const test_exe = b.addTest(.{ .root_source_file = b.path("main.zig"), diff --git a/test/standalone/issue_11595/build.zig b/test/standalone/issue_11595/build.zig index d0592b546bd8e70dc52dd555cf6a2fa8547382cf..c37dfb12b57d8b87ba09443a312d1ad675fdc2e1 100644 --- a/test/standalone/issue_11595/build.zig +++ b/test/standalone/issue_11595/build.zig @@ -30,16 +30,16 @@ pub fn build(b: *std.Build) void { var i: i32 = 0; while (i < 1000) : (i += 1) { - exe.defineCMacro("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); + exe.root_module.addCMacro("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); } - exe.defineCMacro("FOO", "42"); - exe.defineCMacro("BAR", "\"BAR\""); - exe.defineCMacro("BAZ", + exe.root_module.addCMacro("FOO", "42"); + exe.root_module.addCMacro("BAR", "\"BAR\""); + exe.root_module.addCMacro("BAZ", \\"\"BAZ\"" ); - exe.defineCMacro("QUX", "\"Q\" \"UX\""); - exe.defineCMacro("QUUX", "\"QU\\\"UX\""); + exe.root_module.addCMacro("QUX", "\"Q\" \"UX\""); + exe.root_module.addCMacro("QUUX", "\"QU\\\"UX\""); b.default_step.dependOn(&exe.step); diff --git a/test/standalone/stack_iterator/build.zig b/test/standalone/stack_iterator/build.zig index 38f66edd9f606c3a80a367b0d41e959b1808dcf4..20027428b0ff41126c235d1d936b78828e9b3923 100644 --- a/test/standalone/stack_iterator/build.zig +++ b/test/standalone/stack_iterator/build.zig @@ -72,7 +72,7 @@ pub fn build(b: *std.Build) void { }); if (target.result.os.tag == .windows) - c_shared_lib.defineCMacro("LIB_API", "__declspec(dllexport)"); + c_shared_lib.root_module.addCMacro("LIB_API", "__declspec(dllexport)"); c_shared_lib.addCSourceFile(.{ .file = b.path("shared_lib.c"), diff --git a/test/tests.zig b/test/tests.zig index 572838a2ea4b7538aa27b1a1ba36e61032876c0f..d1102ff0669810c5c4fc680d5197021181b1942d 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -1704,7 +1704,7 @@ pub fn addCAbiTests(b: *std.Build, options: CAbiTestOptions) *Step { .file = b.path("test/c_abi/cfuncs.c"), .flags = &.{"-std=c99"}, }); - for (c_abi_target.c_defines) |define| test_step.defineCMacro(define, null); + for (c_abi_target.c_defines) |define| test_step.root_module.addCMacro(define, "1"); // This test is intentionally trying to check if the external ABI is // done properly. LTO would be a hindrance to this. -- 2.54.0 From faafeb51afb9edf5a1f11cb3ab1f9091f07344c7 Mon Sep 17 00:00:00 2001 From: mlugg Date: Tue, 18 Jun 2024 19:53:55 +0500 Subject: [PATCH 02/16] std.Build.Step.Compile: change `root_module` field type to `*Module` This commit changes the `root_module` field of `std.Build.Step.Compile` to be a `*Module` rather than a `Module`. This is a breaking change, but an incredibly minor one (the full potential extent of the breakage can be seen in the modified standalone test). This change will be necessary for an upcoming improvement, so it was convenient to make it here. --- lib/std/Build/Module.zig | 2 +- lib/std/Build/Step/Compile.zig | 8 +++++--- lib/std/Build/Step/Run.zig | 2 +- test/standalone/depend_on_main_mod/build.zig | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/std/Build/Module.zig b/lib/std/Build/Module.zig index 0e421280f0958ca4bf7fa2fcc7d712f37869923a..cc7c5a65c00014deb4bade0f9b88c87239c0d37e 100644 --- a/lib/std/Build/Module.zig +++ b/lib/std/Build/Module.zig @@ -430,7 +430,7 @@ pub const DependencyIterator = struct { if (!it.chase_dyn_libs and compile.isDynamicLibrary()) continue; it.set.put(it.allocator, .{ - .module = &compile.root_module, + .module = compile.root_module, .compile = compile, }, "root") catch @panic("OOM"); }, diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index a85a67bd9b3e256cac0ebc919c9d970ffe06ea6d..cdb0ddeb0ce23f4f368ac2de98b4dc06046690aa 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -22,7 +22,7 @@ const Path = std.Build.Cache.Path; pub const base_id: Step.Id = .compile; step: Step, -root_module: Module, +root_module: *Module, name: []const u8, linker_script: ?LazyPath = null, @@ -432,7 +432,9 @@ pub fn create(owner: *std.Build, options: Options) *Compile { .zig_process = null, }; - compile.root_module.init(owner, options.root_module, compile); + const root_module = owner.allocator.create(Module) catch @panic("OOM"); + root_module.init(owner, options.root_module, compile); + compile.root_module = root_module; if (options.zig_lib_dir) |lp| { compile.zig_lib_dir = lp.dupe(compile.step.owner); @@ -1089,7 +1091,7 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 { } } - var cli_named_modules = try CliNamedModules.init(arena, &compile.root_module); + var cli_named_modules = try CliNamedModules.init(arena, compile.root_module); // For this loop, don't chase dynamic libraries because their link // objects are already linked. diff --git a/lib/std/Build/Step/Run.zig b/lib/std/Build/Step/Run.zig index 270090d07c3f595adbd0b2cdea6e43b35352a030..5408bb287b9aaf57aaa00f460b8a42aaca04eec1 100644 --- a/lib/std/Build/Step/Run.zig +++ b/lib/std/Build/Step/Run.zig @@ -1722,7 +1722,7 @@ fn addPathForDynLibs(run: *Run, artifact: *Step.Compile) void { var it = artifact.root_module.iterateDependencies(artifact, true); while (it.next()) |item| { const other = item.compile.?; - if (item.module == &other.root_module) { + if (item.module == other.root_module) { if (item.module.resolved_target.?.result.os.tag == .windows and other.isDynamicLibrary()) { diff --git a/test/standalone/depend_on_main_mod/build.zig b/test/standalone/depend_on_main_mod/build.zig index 42e96e0aa0676e718f09e75c7f0125fefdb8797a..ae1dc8fb757c4a57114522cd9f5eff8cd46e5dc8 100644 --- a/test/standalone/depend_on_main_mod/build.zig +++ b/test/standalone/depend_on_main_mod/build.zig @@ -18,7 +18,7 @@ pub fn build(b: *std.Build) void { .root_source_file = b.path("src/foo.zig"), }); - foo_module.addImport("root2", &exe.root_module); + foo_module.addImport("root2", exe.root_module); exe.root_module.addImport("foo", foo_module); const run_cmd = b.addRunArtifact(exe); -- 2.54.0 From 3aa802090436a1882fc2093cf0ff8e906340bfcb Mon Sep 17 00:00:00 2001 From: mlugg Date: Wed, 19 Jun 2024 00:21:22 +0500 Subject: [PATCH 03/16] std.Build.Step.Compile.Options: change `root_module` field type to `*Module` --- lib/std/Build.zig | 24 +++++----- lib/std/Build/Module.zig | 86 +++++++++++++++++++--------------- lib/std/Build/Step/Compile.zig | 11 ++--- test/link/link.zig | 4 +- 4 files changed, 68 insertions(+), 57 deletions(-) diff --git a/lib/std/Build.zig b/lib/std/Build.zig index 9d8c7455a3f95fef3ca550f588abba47f29d7c29..12b6baae73b56e4ee3a1e7f1d80796316c7275f6 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -719,7 +719,7 @@ pub const ExecutableOptions = struct { pub fn addExecutable(b: *Build, options: ExecutableOptions) *Step.Compile { return Step.Compile.create(b, .{ .name = options.name, - .root_module = .{ + .root_module = b.createModule(.{ .root_source_file = options.root_source_file, .target = options.target, .optimize = options.optimize, @@ -732,7 +732,7 @@ pub fn addExecutable(b: *Build, options: ExecutableOptions) *Step.Compile { .sanitize_thread = options.sanitize_thread, .error_tracing = options.error_tracing, .code_model = options.code_model, - }, + }), .version = options.version, .kind = .exe, .linkage = options.linkage, @@ -769,7 +769,7 @@ pub const ObjectOptions = struct { pub fn addObject(b: *Build, options: ObjectOptions) *Step.Compile { return Step.Compile.create(b, .{ .name = options.name, - .root_module = .{ + .root_module = b.createModule(.{ .root_source_file = options.root_source_file, .target = options.target, .optimize = options.optimize, @@ -782,7 +782,7 @@ pub fn addObject(b: *Build, options: ObjectOptions) *Step.Compile { .sanitize_thread = options.sanitize_thread, .error_tracing = options.error_tracing, .code_model = options.code_model, - }, + }), .kind = .obj, .max_rss = options.max_rss, .use_llvm = options.use_llvm, @@ -823,7 +823,7 @@ pub const SharedLibraryOptions = struct { pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile { return Step.Compile.create(b, .{ .name = options.name, - .root_module = .{ + .root_module = b.createModule(.{ .target = options.target, .optimize = options.optimize, .root_source_file = options.root_source_file, @@ -836,7 +836,7 @@ pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile .sanitize_thread = options.sanitize_thread, .error_tracing = options.error_tracing, .code_model = options.code_model, - }, + }), .kind = .lib, .linkage = .dynamic, .version = options.version, @@ -874,7 +874,7 @@ pub const StaticLibraryOptions = struct { pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile { return Step.Compile.create(b, .{ .name = options.name, - .root_module = .{ + .root_module = b.createModule(.{ .target = options.target, .optimize = options.optimize, .root_source_file = options.root_source_file, @@ -887,7 +887,7 @@ pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile .sanitize_thread = options.sanitize_thread, .error_tracing = options.error_tracing, .code_model = options.code_model, - }, + }), .kind = .lib, .linkage = .static, .version = options.version, @@ -935,7 +935,7 @@ pub fn addTest(b: *Build, options: TestOptions) *Step.Compile { return Step.Compile.create(b, .{ .name = options.name, .kind = .@"test", - .root_module = .{ + .root_module = b.createModule(.{ .root_source_file = options.root_source_file, .target = options.target orelse b.graph.host, .optimize = options.optimize, @@ -948,7 +948,7 @@ pub fn addTest(b: *Build, options: TestOptions) *Step.Compile { .omit_frame_pointer = options.omit_frame_pointer, .sanitize_thread = options.sanitize_thread, .error_tracing = options.error_tracing, - }, + }), .max_rss = options.max_rss, .filters = if (options.filter != null and options.filters.len > 0) filters: { const filters = b.allocator.alloc([]const u8, 1 + options.filters.len) catch @panic("OOM"); @@ -978,10 +978,10 @@ pub fn addAssembly(b: *Build, options: AssemblyOptions) *Step.Compile { const obj_step = Step.Compile.create(b, .{ .name = options.name, .kind = .obj, - .root_module = .{ + .root_module = b.createModule(.{ .target = options.target, .optimize = options.optimize, - }, + }), .max_rss = options.max_rss, .zig_lib_dir = options.zig_lib_dir, }); diff --git a/lib/std/Build/Module.zig b/lib/std/Build/Module.zig index cc7c5a65c00014deb4bade0f9b88c87239c0d37e..629d90dd0fab6e3d446008e746d38387d6bd2ab2 100644 --- a/lib/std/Build/Module.zig +++ b/lib/std/Build/Module.zig @@ -242,45 +242,57 @@ pub const Import = struct { module: *Module, }; -pub fn init(m: *Module, owner: *std.Build, options: CreateOptions, compile: ?*Step.Compile) void { +pub fn init( + m: *Module, + owner: *std.Build, + value: union(enum) { options: CreateOptions, existing: *const Module }, + compile: ?*Step.Compile, +) void { const allocator = owner.allocator; - m.* = .{ - .owner = owner, - .depending_steps = .{}, - .root_source_file = if (options.root_source_file) |lp| lp.dupe(owner) else null, - .import_table = .{}, - .resolved_target = options.target, - .optimize = options.optimize, - .link_libc = options.link_libc, - .link_libcpp = options.link_libcpp, - .dwarf_format = options.dwarf_format, - .c_macros = .{}, - .include_dirs = .{}, - .lib_paths = .{}, - .rpaths = .{}, - .frameworks = .{}, - .link_objects = .{}, - .strip = options.strip, - .unwind_tables = options.unwind_tables, - .single_threaded = options.single_threaded, - .stack_protector = options.stack_protector, - .stack_check = options.stack_check, - .sanitize_c = options.sanitize_c, - .sanitize_thread = options.sanitize_thread, - .fuzz = options.fuzz, - .code_model = options.code_model, - .valgrind = options.valgrind, - .pic = options.pic, - .red_zone = options.red_zone, - .omit_frame_pointer = options.omit_frame_pointer, - .error_tracing = options.error_tracing, - .export_symbol_names = &.{}, - }; + switch (value) { + .options => |options| { + m.* = .{ + .owner = owner, + .depending_steps = .{}, + .root_source_file = if (options.root_source_file) |lp| lp.dupe(owner) else null, + .import_table = .{}, + .resolved_target = options.target, + .optimize = options.optimize, + .link_libc = options.link_libc, + .link_libcpp = options.link_libcpp, + .dwarf_format = options.dwarf_format, + .c_macros = .{}, + .include_dirs = .{}, + .lib_paths = .{}, + .rpaths = .{}, + .frameworks = .{}, + .link_objects = .{}, + .strip = options.strip, + .unwind_tables = options.unwind_tables, + .single_threaded = options.single_threaded, + .stack_protector = options.stack_protector, + .stack_check = options.stack_check, + .sanitize_c = options.sanitize_c, + .sanitize_thread = options.sanitize_thread, + .fuzz = options.fuzz, + .code_model = options.code_model, + .valgrind = options.valgrind, + .pic = options.pic, + .red_zone = options.red_zone, + .omit_frame_pointer = options.omit_frame_pointer, + .error_tracing = options.error_tracing, + .export_symbol_names = &.{}, + }; - m.import_table.ensureUnusedCapacity(allocator, options.imports.len) catch @panic("OOM"); - for (options.imports) |dep| { - m.import_table.putAssumeCapacity(dep.name, dep.module); + m.import_table.ensureUnusedCapacity(allocator, options.imports.len) catch @panic("OOM"); + for (options.imports) |dep| { + m.import_table.putAssumeCapacity(dep.name, dep.module); + } + }, + .existing => |existing| { + m.* = existing.*; + }, } if (compile) |c| { @@ -294,7 +306,7 @@ pub fn init(m: *Module, owner: *std.Build, options: CreateOptions, compile: ?*St pub fn create(owner: *std.Build, options: CreateOptions) *Module { const m = owner.allocator.create(Module) catch @panic("OOM"); - m.init(owner, options, null); + m.init(owner, .{ .options = options }, null); return m; } diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index cdb0ddeb0ce23f4f368ac2de98b4dc06046690aa..f34ce7785b226d30092400a3ae50d206ff808946 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -262,7 +262,7 @@ pub const Entry = union(enum) { pub const Options = struct { name: []const u8, - root_module: Module.CreateOptions, + root_module: *Module, kind: Kind, linkage: ?std.builtin.LinkMode = null, version: ?std.SemanticVersion = null, @@ -359,7 +359,8 @@ pub fn create(owner: *std.Build, options: Options) *Compile { else owner.fmt("{s} ", .{name}); - const resolved_target = options.root_module.target.?; + const resolved_target = options.root_module.resolved_target orelse + @panic("the root Module of a Compile step must be created with a known 'target' field"); const target = resolved_target.result; const step_name = owner.fmt("{s} {s}{s} {s}", .{ @@ -431,10 +432,8 @@ pub fn create(owner: *std.Build, options: Options) *Compile { .zig_process = null, }; - - const root_module = owner.allocator.create(Module) catch @panic("OOM"); - root_module.init(owner, options.root_module, compile); - compile.root_module = root_module; + options.root_module.init(owner, .{ .existing = options.root_module }, compile); + compile.root_module = options.root_module; if (options.zig_lib_dir) |lp| { compile.zig_lib_dir = lp.dupe(compile.step.owner); diff --git a/test/link/link.zig b/test/link/link.zig index 4537846353e35ddec9737f826fe0b0f82eedef0c..6885aa93214172b4cdce88176e0a5ad842c0cebf 100644 --- a/test/link/link.zig +++ b/test/link/link.zig @@ -70,7 +70,7 @@ fn addCompileStep( ) *Compile { const compile_step = Compile.create(b, .{ .name = overlay.name, - .root_module = .{ + .root_module = b.createModule(.{ .target = base.target, .optimize = base.optimize, .root_source_file = rsf: { @@ -80,7 +80,7 @@ fn addCompileStep( }, .pic = overlay.pic, .strip = if (base.strip) |s| s else overlay.strip, - }, + }), .use_llvm = base.use_llvm, .use_lld = base.use_lld, .kind = switch (kind) { -- 2.54.0 From b83b161f4b2102e8f10ab84f1c99def352554dd2 Mon Sep 17 00:00:00 2001 From: Eric Joldasov Date: Mon, 15 Jul 2024 19:14:59 +0500 Subject: [PATCH 04/16] std.Build.Step.TranslateC: propagate target, optimize, link_libc to added module Will be needed for the future commit with the new API where artifacts are created from the pre-existing module. --- lib/std/Build/Step/TranslateC.zig | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/std/Build/Step/TranslateC.zig b/lib/std/Build/Step/TranslateC.zig index 13f437599946ce3e25f3443691fbe5a8c8c9887d..a2585cafd0ba6054ec9d922e2bd9f1aea01f9211 100644 --- a/lib/std/Build/Step/TranslateC.zig +++ b/lib/std/Build/Step/TranslateC.zig @@ -81,6 +81,9 @@ pub fn addExecutable(translate_c: *TranslateC, options: AddExecutableOptions) *S pub fn addModule(translate_c: *TranslateC, name: []const u8) *std.Build.Module { return translate_c.step.owner.addModule(name, .{ .root_source_file = translate_c.getOutput(), + .target = translate_c.target, + .optimize = translate_c.optimize, + .link_libc = translate_c.link_libc, }); } -- 2.54.0 From 0bb93ca053f9520a396a652e929d2cc6358ec5be Mon Sep 17 00:00:00 2001 From: mlugg Date: Wed, 11 Dec 2024 19:03:32 +0000 Subject: [PATCH 05/16] std.Build: simplify module dependency handling At the expense of a slight special case in the build runner, we can make the handling of dependencies between modules a little shorter and much easier to follow. When module and step graphs are being constructed during the "configure" phase, we do not set up step dependencies triggered by modules. Instead, after the configure phase, the build runner traverses the whole step/module graph, starting from the root top-level steps, and configures all step dependencies implied by modules. The "make" phase then proceeds as normal. Also, the old `Module.dependencyIterator` logic is replaced by two separate iterables. `Module.getGraph` takes the root module of a compilation, and returns all modules in its graph; while `Step.Compile.getCompileDependencies` takes a `*Step.Compile` and returns all `*Step.Compile` it depends on, recursively, possibly excluding dynamic libraries. The old `Module.dependencyIterator` combined these two functions into one unintuitive iterator; they are now separated, which in particular helps readability at the usage sites which only need one or the other. --- lib/compiler/build_runner.zig | 78 +++++ lib/std/Build/Module.zig | 227 +++----------- lib/std/Build/Step/Compile.zig | 541 +++++++++++++++++---------------- lib/std/Build/Step/Run.zig | 15 +- 4 files changed, 414 insertions(+), 447 deletions(-) diff --git a/lib/compiler/build_runner.zig b/lib/compiler/build_runner.zig index 632982e812e35beede8e255064f8f668edc58197..91443a093a750d08e9b34689e850d718b0687d68 100644 --- a/lib/compiler/build_runner.zig +++ b/lib/compiler/build_runner.zig @@ -337,6 +337,7 @@ pub fn main() !void { var prog_node = main_progress_node.start("Configure", 0); defer prog_node.end(); try builder.runBuild(root); + createModuleDependencies(builder) catch @panic("OOM"); } if (graph.needed_lazy_dependencies.entries.len != 0) { @@ -1440,3 +1441,80 @@ fn validateSystemLibraryOptions(b: *std.Build) void { process.exit(1); } } + +/// Starting from all top-level steps in `b`, traverses the entire step graph +/// and adds all step dependencies implied by module graphs. +fn createModuleDependencies(b: *std.Build) Allocator.Error!void { + const arena = b.graph.arena; + + var all_steps: std.AutoArrayHashMapUnmanaged(*Step, void) = .empty; + var next_step_idx: usize = 0; + + try all_steps.ensureUnusedCapacity(arena, b.top_level_steps.count()); + for (b.top_level_steps.values()) |tls| { + all_steps.putAssumeCapacityNoClobber(&tls.step, {}); + } + + while (next_step_idx < all_steps.count()) { + const step = all_steps.keys()[next_step_idx]; + next_step_idx += 1; + + // Set up any implied dependencies for this step. It's important that we do this first, so + // that the loop below discovers steps implied by the module graph. + try createModuleDependenciesForStep(step); + + try all_steps.ensureUnusedCapacity(arena, step.dependencies.items.len); + for (step.dependencies.items) |other_step| { + all_steps.putAssumeCapacity(other_step, {}); + } + } +} + +/// If the given `Step` is a `Step.Compile`, adds any dependencies for that step which +/// are implied by the module graph rooted at `step.cast(Step.Compile).?.root_module`. +fn createModuleDependenciesForStep(step: *Step) Allocator.Error!void { + const root_module = if (step.cast(Step.Compile)) |cs| root: { + break :root cs.root_module; + } else return; // not a compile step so no module dependencies + + // Starting from `root_module`, discover all modules in this graph. + const modules = root_module.getGraph().modules; + + // For each of those modules, set up the implied step dependencies. + for (modules) |mod| { + if (mod.root_source_file) |lp| lp.addStepDependencies(step); + for (mod.include_dirs.items) |include_dir| switch (include_dir) { + .path, + .path_system, + .path_after, + .framework_path, + .framework_path_system, + => |lp| lp.addStepDependencies(step), + + .other_step => |other| { + other.getEmittedIncludeTree().addStepDependencies(step); + step.dependOn(&other.step); + }, + + .config_header_step => |other| step.dependOn(&other.step), + }; + for (mod.lib_paths.items) |lp| lp.addStepDependencies(step); + for (mod.rpaths.items) |rpath| switch (rpath) { + .lazy_path => |lp| lp.addStepDependencies(step), + .special => {}, + }; + for (mod.link_objects.items) |link_object| switch (link_object) { + .static_path, + .assembly_file, + => |lp| lp.addStepDependencies(step), + .other_step => |other| step.dependOn(&other.step), + .system_lib => {}, + .c_source_file => |source| source.file.addStepDependencies(step), + .c_source_files => |source_files| source_files.root.addStepDependencies(step), + .win32_resource_file => |rc_source| { + rc_source.file.addStepDependencies(step); + for (rc_source.include_paths) |lp| lp.addStepDependencies(step); + }, + }; + } +} diff --git a/lib/std/Build/Module.zig b/lib/std/Build/Module.zig index 629d90dd0fab6e3d446008e746d38387d6bd2ab2..1f2b4f3fcb7f0a819cc5fac96d27eedb9c42a51a 100644 --- a/lib/std/Build/Module.zig +++ b/lib/std/Build/Module.zig @@ -1,9 +1,5 @@ /// The one responsible for creating this module. owner: *std.Build, -/// Tracks the set of steps that depend on this `Module`. This ensures that -/// when making this `Module` depend on other `Module` objects and `Step` -/// objects, respective `Step` dependencies can be added. -depending_steps: std.AutoArrayHashMapUnmanaged(*Step.Compile, void), root_source_file: ?LazyPath, /// The modules that are mapped into this module's import table. /// Use `addImport` rather than modifying this field directly in order to @@ -41,6 +37,10 @@ link_libcpp: ?bool, /// Symbols to be exported when compiling to WebAssembly. export_symbol_names: []const []const u8 = &.{}, +/// Caches the result of `getGraph` when called multiple times. +/// Use `getGraph` instead of accessing this field directly. +cached_graph: Graph = .{ .modules = &.{}, .names = &.{} }, + pub const RPath = union(enum) { lazy_path: LazyPath, special: []const u8, @@ -246,7 +246,6 @@ pub fn init( m: *Module, owner: *std.Build, value: union(enum) { options: CreateOptions, existing: *const Module }, - compile: ?*Step.Compile, ) void { const allocator = owner.allocator; @@ -254,7 +253,6 @@ pub fn init( .options => |options| { m.* = .{ .owner = owner, - .depending_steps = .{}, .root_source_file = if (options.root_source_file) |lp| lp.dupe(owner) else null, .import_table = .{}, .resolved_target = options.target, @@ -294,19 +292,11 @@ pub fn init( m.* = existing.*; }, } - - if (compile) |c| { - m.depending_steps.put(allocator, c, {}) catch @panic("OOM"); - } - - // This logic accesses `depending_steps` which was just modified above. - var it = m.iterateDependencies(null, false); - while (it.next()) |item| addShallowDependencies(m, item.module); } pub fn create(owner: *std.Build, options: CreateOptions) *Module { const m = owner.allocator.create(Module) catch @panic("OOM"); - m.init(owner, .{ .options = options }, null); + m.init(owner, .{ .options = options }); return m; } @@ -314,69 +304,6 @@ pub fn create(owner: *std.Build, options: CreateOptions) *Module { pub fn addImport(m: *Module, name: []const u8, module: *Module) void { const b = m.owner; m.import_table.put(b.allocator, b.dupe(name), module) catch @panic("OOM"); - - var it = module.iterateDependencies(null, false); - while (it.next()) |item| addShallowDependencies(m, item.module); -} - -/// Creates step dependencies and updates `depending_steps` of `dependee` so that -/// subsequent calls to `addImport` on `dependee` will additionally create step -/// dependencies on `m`'s `depending_steps`. -fn addShallowDependencies(m: *Module, dependee: *Module) void { - if (dependee.root_source_file) |lazy_path| addLazyPathDependencies(m, dependee, lazy_path); - for (dependee.lib_paths.items) |lib_path| addLazyPathDependencies(m, dependee, lib_path); - for (dependee.rpaths.items) |rpath| switch (rpath) { - .lazy_path => |lp| addLazyPathDependencies(m, dependee, lp), - .special => {}, - }; - - for (dependee.link_objects.items) |link_object| switch (link_object) { - .other_step => |compile| { - addStepDependencies(m, dependee, &compile.step); - addLazyPathDependenciesOnly(m, compile.getEmittedIncludeTree()); - }, - - .static_path, - .assembly_file, - => |lp| addLazyPathDependencies(m, dependee, lp), - - .c_source_file => |x| addLazyPathDependencies(m, dependee, x.file), - .win32_resource_file => |x| addLazyPathDependencies(m, dependee, x.file), - - .c_source_files, - .system_lib, - => {}, - }; -} - -fn addLazyPathDependencies(m: *Module, module: *Module, lazy_path: LazyPath) void { - addLazyPathDependenciesOnly(m, lazy_path); - if (m != module) { - for (m.depending_steps.keys()) |compile| { - module.depending_steps.put(m.owner.allocator, compile, {}) catch @panic("OOM"); - } - } -} - -fn addLazyPathDependenciesOnly(m: *Module, lazy_path: LazyPath) void { - for (m.depending_steps.keys()) |compile| { - lazy_path.addStepDependencies(&compile.step); - } -} - -fn addStepDependencies(m: *Module, module: *Module, dependee: *Step) void { - addStepDependenciesOnly(m, dependee); - if (m != module) { - for (m.depending_steps.keys()) |compile| { - module.depending_steps.put(m.owner.allocator, compile, {}) catch @panic("OOM"); - } - } -} - -fn addStepDependenciesOnly(m: *Module, dependee: *Step) void { - for (m.depending_steps.keys()) |compile| { - compile.step.dependOn(dependee); - } } /// Creates a new module and adds it to be used with `@import`. @@ -392,91 +319,6 @@ pub fn addOptions(m: *Module, module_name: []const u8, options: *Step.Options) v addImport(m, module_name, options.createModule()); } -pub const DependencyIterator = struct { - allocator: std.mem.Allocator, - index: usize, - set: std.AutoArrayHashMapUnmanaged(Key, []const u8), - chase_dyn_libs: bool, - - pub const Key = struct { - /// The compilation that contains the `Module`. Note that a `Module` might be - /// used by more than one compilation. - compile: ?*Step.Compile, - module: *Module, - }; - - pub const Item = struct { - /// The compilation that contains the `Module`. Note that a `Module` might be - /// used by more than one compilation. - compile: ?*Step.Compile, - module: *Module, - name: []const u8, - }; - - pub fn deinit(it: *DependencyIterator) void { - it.set.deinit(it.allocator); - it.* = undefined; - } - - pub fn next(it: *DependencyIterator) ?Item { - if (it.index >= it.set.count()) { - it.set.clearAndFree(it.allocator); - return null; - } - const key = it.set.keys()[it.index]; - const name = it.set.values()[it.index]; - it.index += 1; - const module = key.module; - it.set.ensureUnusedCapacity(it.allocator, module.import_table.count()) catch - @panic("OOM"); - for (module.import_table.keys(), module.import_table.values()) |dep_name, dep| { - it.set.putAssumeCapacity(.{ - .module = dep, - .compile = key.compile, - }, dep_name); - } - - if (key.compile != null) { - for (module.link_objects.items) |link_object| switch (link_object) { - .other_step => |compile| { - if (!it.chase_dyn_libs and compile.isDynamicLibrary()) continue; - - it.set.put(it.allocator, .{ - .module = compile.root_module, - .compile = compile, - }, "root") catch @panic("OOM"); - }, - else => {}, - }; - } - - return .{ - .compile = key.compile, - .module = key.module, - .name = name, - }; - } -}; - -pub fn iterateDependencies( - m: *Module, - chase_steps: ?*Step.Compile, - chase_dyn_libs: bool, -) DependencyIterator { - var it: DependencyIterator = .{ - .allocator = m.owner.allocator, - .index = 0, - .set = .{}, - .chase_dyn_libs = chase_dyn_libs, - }; - it.set.ensureUnusedCapacity(m.owner.allocator, m.import_table.count() + 1) catch @panic("OOM"); - it.set.putAssumeCapacity(.{ - .module = m, - .compile = chase_steps, - }, "root"); - return it; -} - pub const LinkSystemLibraryOptions = struct { /// Causes dynamic libraries to be linked regardless of whether they are /// actually depended on. When false, dynamic libraries with no referenced @@ -559,7 +401,6 @@ pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void { .flags = b.dupeStrings(options.flags), }; m.link_objects.append(allocator, .{ .c_source_files = c_source_files }) catch @panic("OOM"); - addLazyPathDependenciesOnly(m, c_source_files.root); } pub fn addCSourceFile(m: *Module, source: CSourceFile) void { @@ -568,7 +409,6 @@ pub fn addCSourceFile(m: *Module, source: CSourceFile) void { const c_source_file = allocator.create(CSourceFile) catch @panic("OOM"); c_source_file.* = source.dupe(b); m.link_objects.append(allocator, .{ .c_source_file = c_source_file }) catch @panic("OOM"); - addLazyPathDependenciesOnly(m, source.file); } /// Resource files must have the extension `.rc`. @@ -585,22 +425,16 @@ pub fn addWin32ResourceFile(m: *Module, source: RcSourceFile) void { const rc_source_file = allocator.create(RcSourceFile) catch @panic("OOM"); rc_source_file.* = source.dupe(b); m.link_objects.append(allocator, .{ .win32_resource_file = rc_source_file }) catch @panic("OOM"); - addLazyPathDependenciesOnly(m, source.file); - for (source.include_paths) |include_path| { - addLazyPathDependenciesOnly(m, include_path); - } } pub fn addAssemblyFile(m: *Module, source: LazyPath) void { const b = m.owner; m.link_objects.append(b.allocator, .{ .assembly_file = source.dupe(b) }) catch @panic("OOM"); - addLazyPathDependenciesOnly(m, source); } pub fn addObjectFile(m: *Module, object: LazyPath) void { const b = m.owner; m.link_objects.append(b.allocator, .{ .static_path = object.dupe(b) }) catch @panic("OOM"); - addLazyPathDependenciesOnly(m, object); } pub fn addObject(m: *Module, object: *Step.Compile) void { @@ -616,51 +450,43 @@ pub fn linkLibrary(m: *Module, library: *Step.Compile) void { pub fn addAfterIncludePath(m: *Module, lazy_path: LazyPath) void { const b = m.owner; m.include_dirs.append(b.allocator, .{ .path_after = lazy_path.dupe(b) }) catch @panic("OOM"); - addLazyPathDependenciesOnly(m, lazy_path); } pub fn addSystemIncludePath(m: *Module, lazy_path: LazyPath) void { const b = m.owner; m.include_dirs.append(b.allocator, .{ .path_system = lazy_path.dupe(b) }) catch @panic("OOM"); - addLazyPathDependenciesOnly(m, lazy_path); } pub fn addIncludePath(m: *Module, lazy_path: LazyPath) void { const b = m.owner; m.include_dirs.append(b.allocator, .{ .path = lazy_path.dupe(b) }) catch @panic("OOM"); - addLazyPathDependenciesOnly(m, lazy_path); } pub fn addConfigHeader(m: *Module, config_header: *Step.ConfigHeader) void { const allocator = m.owner.allocator; m.include_dirs.append(allocator, .{ .config_header_step = config_header }) catch @panic("OOM"); - addStepDependenciesOnly(m, &config_header.step); } pub fn addSystemFrameworkPath(m: *Module, directory_path: LazyPath) void { const b = m.owner; m.include_dirs.append(b.allocator, .{ .framework_path_system = directory_path.dupe(b) }) catch @panic("OOM"); - addLazyPathDependenciesOnly(m, directory_path); } pub fn addFrameworkPath(m: *Module, directory_path: LazyPath) void { const b = m.owner; m.include_dirs.append(b.allocator, .{ .framework_path = directory_path.dupe(b) }) catch @panic("OOM"); - addLazyPathDependenciesOnly(m, directory_path); } pub fn addLibraryPath(m: *Module, directory_path: LazyPath) void { const b = m.owner; m.lib_paths.append(b.allocator, directory_path.dupe(b)) catch @panic("OOM"); - addLazyPathDependenciesOnly(m, directory_path); } pub fn addRPath(m: *Module, directory_path: LazyPath) void { const b = m.owner; m.rpaths.append(b.allocator, .{ .lazy_path = directory_path.dupe(b) }) catch @panic("OOM"); - addLazyPathDependenciesOnly(m, directory_path); } pub fn addRPathSpecial(m: *Module, bytes: []const u8) void { @@ -784,7 +610,6 @@ fn addFlag( fn linkLibraryOrObject(m: *Module, other: *Step.Compile) void { const allocator = m.owner.allocator; _ = other.getEmittedBin(); // Indicate there is a dependency on the outputted binary. - addStepDependenciesOnly(m, &other.step); if (other.rootModuleTarget().os.tag == .windows and other.isDynamicLibrary()) { _ = other.getEmittedImplib(); // Indicate dependency on the outputted implib. @@ -792,8 +617,6 @@ fn linkLibraryOrObject(m: *Module, other: *Step.Compile) void { m.link_objects.append(allocator, .{ .other_step = other }) catch @panic("OOM"); m.include_dirs.append(allocator, .{ .other_step = other }) catch @panic("OOM"); - - addLazyPathDependenciesOnly(m, other.getEmittedIncludeTree()); } fn requireKnownTarget(m: *Module) std.Target { @@ -802,6 +625,46 @@ fn requireKnownTarget(m: *Module) std.Target { return resolved_target.result; } +/// Elements of `modules` and `names` are matched one-to-one. +pub const Graph = struct { + modules: []const *Module, + names: []const []const u8, +}; + +/// Intended to be used during the make phase only. +/// +/// Given that `root` is the root `Module` of a compilation, return all `Module`s +/// in the module graph, including `root` itself. `root` is guaranteed to be the +/// first module in the returned slice. +pub fn getGraph(root: *Module) Graph { + if (root.cached_graph.modules.len != 0) { + return root.cached_graph; + } + + const arena = root.owner.graph.arena; + + var modules: std.AutoArrayHashMapUnmanaged(*std.Build.Module, []const u8) = .empty; + var next_idx: usize = 0; + + modules.putNoClobber(arena, root, "root") catch @panic("OOM"); + + while (next_idx < modules.count()) { + const mod = modules.keys()[next_idx]; + next_idx += 1; + modules.ensureUnusedCapacity(arena, mod.import_table.count()) catch @panic("OOM"); + for (mod.import_table.keys(), mod.import_table.values()) |import_name, other_mod| { + modules.putAssumeCapacity(other_mod, import_name); + } + } + + const result: Graph = .{ + .modules = modules.keys(), + .names = modules.values(), + }; + root.cached_graph = result; + return result; +} + const Module = @This(); const std = @import("std"); const assert = std.debug.assert; diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index f34ce7785b226d30092400a3ae50d206ff808946..3c6e8231caa1a694292e86734d9edac0e5966b81 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -389,7 +389,7 @@ pub fn create(owner: *std.Build, options: Options) *Compile { const compile = owner.allocator.create(Compile) catch @panic("OOM"); compile.* = .{ - .root_module = undefined, + .root_module = options.root_module, .verbose_link = false, .verbose_cc = false, .linkage = options.linkage, @@ -432,8 +432,6 @@ pub fn create(owner: *std.Build, options: Options) *Compile { .zig_process = null, }; - options.root_module.init(owner, .{ .existing = options.root_module }, compile); - compile.root_module = options.root_module; if (options.zig_lib_dir) |lp| { compile.zig_lib_dir = lp.dupe(compile.step.owner); @@ -607,16 +605,17 @@ pub fn dependsOnSystemLibrary(compile: *const Compile, name: []const u8) bool { var is_linking_libc = false; var is_linking_libcpp = false; - var dep_it = compile.root_module.iterateDependencies(compile, true); - while (dep_it.next()) |module| { - for (module.link_objects.items) |link_object| { - switch (link_object) { - .system_lib => |lib| if (mem.eql(u8, lib.name, name)) return true, - else => continue, + for (compile.getCompileDependencies(true)) |some_compile| { + for (some_compile.root_module.getGraph().modules) |mod| { + for (mod.link_objects.items) |lo| { + switch (lo) { + .system_lib => |lib| if (mem.eql(u8, lib.name, name)) return true, + else => {}, + } } + if (mod.link_libc) is_linking_libc = true; + if (mod.link_libcpp) is_linking_libcpp = true; } - is_linking_libc = is_linking_libc or module.link_libc == true; - is_linking_libcpp = is_linking_libcpp or module.link_libcpp == true; } const target = compile.rootModuleTarget(); @@ -961,23 +960,22 @@ const CliNamedModules = struct { .modules = .{}, .names = .{}, }; - var dep_it = root_module.iterateDependencies(null, false); + const graph = root_module.getGraph(); { - const item = dep_it.next().?; - assert(root_module == item.module); + assert(graph.modules[0] == root_module); try compile.modules.put(arena, root_module, {}); try compile.names.put(arena, "root", {}); } - while (dep_it.next()) |item| { - var name = item.name; + for (graph.modules[1..], graph.names[1..]) |mod, orig_name| { + var name = orig_name; var n: usize = 0; while (true) { const gop = try compile.names.getOrPut(arena, name); if (!gop.found_existing) { - try compile.modules.putNoClobber(arena, item.module, {}); + try compile.modules.putNoClobber(arena, mod, {}); break; } - name = try std.fmt.allocPrint(arena, "{s}{d}", .{ item.name, n }); + name = try std.fmt.allocPrint(arena, "{s}{d}", .{ orig_name, n }); n += 1; } } @@ -1080,13 +1078,12 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 { // emitted if there is nothing to link. var total_linker_objects: usize = @intFromBool(compile.root_module.root_source_file != null); - { - // Fully recursive iteration including dynamic libraries to detect - // libc and libc++ linkage. - var dep_it = compile.root_module.iterateDependencies(compile, true); - while (dep_it.next()) |key| { - if (key.module.link_libc == true) compile.is_linking_libc = true; - if (key.module.link_libcpp == true) compile.is_linking_libcpp = true; + // Fully recursive iteration including dynamic libraries to detect + // libc and libc++ linkage. + for (compile.getCompileDependencies(true)) |some_compile| { + for (some_compile.root_module.getGraph().modules) |mod| { + if (mod.link_libc == true) compile.is_linking_libc = true; + if (mod.link_libcpp == true) compile.is_linking_libcpp = true; } } @@ -1094,265 +1091,265 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 { // For this loop, don't chase dynamic libraries because their link // objects are already linked. - var dep_it = compile.root_module.iterateDependencies(compile, false); + for (compile.getCompileDependencies(false)) |dep_compile| { + for (dep_compile.root_module.getGraph().modules) |mod| { + // While walking transitive dependencies, if a given link object is + // already included in a library, it should not redundantly be + // placed on the linker line of the dependee. + const my_responsibility = dep_compile == compile; + const already_linked = !my_responsibility and dep_compile.isDynamicLibrary(); - while (dep_it.next()) |dep| { - // While walking transitive dependencies, if a given link object is - // already included in a library, it should not redundantly be - // placed on the linker line of the dependee. - const my_responsibility = dep.compile.? == compile; - const already_linked = !my_responsibility and dep.compile.?.isDynamicLibrary(); - - // Inherit dependencies on darwin frameworks. - if (!already_linked) { - for (dep.module.frameworks.keys(), dep.module.frameworks.values()) |name, info| { - try frameworks.put(arena, name, info); + // Inherit dependencies on darwin frameworks. + if (!already_linked) { + for (mod.frameworks.keys(), mod.frameworks.values()) |name, info| { + try frameworks.put(arena, name, info); + } } - } - // Inherit dependencies on system libraries and static libraries. - for (dep.module.link_objects.items) |link_object| { - switch (link_object) { - .static_path => |static_path| { - if (my_responsibility) { - try zig_args.append(static_path.getPath2(dep.module.owner, step)); - total_linker_objects += 1; - } - }, - .system_lib => |system_lib| { - const system_lib_gop = try seen_system_libs.getOrPut(arena, system_lib.name); - if (system_lib_gop.found_existing) { - try zig_args.appendSlice(system_lib_gop.value_ptr.*); - continue; - } else { - system_lib_gop.value_ptr.* = &.{}; - } - - if (already_linked) - continue; - - if ((system_lib.search_strategy != prev_search_strategy or - system_lib.preferred_link_mode != prev_preferred_link_mode) and - compile.linkage != .static) - { - switch (system_lib.search_strategy) { - .no_fallback => switch (system_lib.preferred_link_mode) { - .dynamic => try zig_args.append("-search_dylibs_only"), - .static => try zig_args.append("-search_static_only"), - }, - .paths_first => switch (system_lib.preferred_link_mode) { - .dynamic => try zig_args.append("-search_paths_first"), - .static => try zig_args.append("-search_paths_first_static"), - }, - .mode_first => switch (system_lib.preferred_link_mode) { - .dynamic => try zig_args.append("-search_dylibs_first"), - .static => try zig_args.append("-search_static_first"), - }, - } - prev_search_strategy = system_lib.search_strategy; - prev_preferred_link_mode = system_lib.preferred_link_mode; - } - - const prefix: []const u8 = prefix: { - if (system_lib.needed) break :prefix "-needed-l"; - if (system_lib.weak) break :prefix "-weak-l"; - break :prefix "-l"; - }; - switch (system_lib.use_pkg_config) { - .no => try zig_args.append(b.fmt("{s}{s}", .{ prefix, system_lib.name })), - .yes, .force => { - if (compile.runPkgConfig(system_lib.name)) |result| { - try zig_args.appendSlice(result.cflags); - try zig_args.appendSlice(result.libs); - try seen_system_libs.put(arena, system_lib.name, result.cflags); - } else |err| switch (err) { - error.PkgConfigInvalidOutput, - error.PkgConfigCrashed, - error.PkgConfigFailed, - error.PkgConfigNotInstalled, - error.PackageNotFound, - => switch (system_lib.use_pkg_config) { - .yes => { - // pkg-config failed, so fall back to linking the library - // by name directly. - try zig_args.append(b.fmt("{s}{s}", .{ - prefix, - system_lib.name, - })); - }, - .force => { - panic("pkg-config failed for library {s}", .{system_lib.name}); - }, - .no => unreachable, - }, - - else => |e| return e, - } - }, - } - }, - .other_step => |other| { - switch (other.kind) { - .exe => return step.fail("cannot link with an executable build artifact", .{}), - .@"test" => return step.fail("cannot link with a test", .{}), - .obj => { - const included_in_lib_or_obj = !my_responsibility and - (dep.compile.?.kind == .lib or dep.compile.?.kind == .obj); - if (!already_linked and !included_in_lib_or_obj) { - try zig_args.append(other.getEmittedBin().getPath2(b, step)); - total_linker_objects += 1; - } - }, - .lib => l: { - const other_produces_implib = other.producesImplib(); - const other_is_static = other_produces_implib or other.isStaticLibrary(); - - if (compile.isStaticLibrary() and other_is_static) { - // Avoid putting a static library inside a static library. - break :l; - } - - // For DLLs, we must link against the implib. - // For everything else, we directly link - // against the library file. - const full_path_lib = if (other_produces_implib) - other.getGeneratedFilePath("generated_implib", &compile.step) - else - other.getGeneratedFilePath("generated_bin", &compile.step); - - try zig_args.append(full_path_lib); + // Inherit dependencies on system libraries and static libraries. + for (mod.link_objects.items) |link_object| { + switch (link_object) { + .static_path => |static_path| { + if (my_responsibility) { + try zig_args.append(static_path.getPath2(mod.owner, step)); total_linker_objects += 1; + } + }, + .system_lib => |system_lib| { + const system_lib_gop = try seen_system_libs.getOrPut(arena, system_lib.name); + if (system_lib_gop.found_existing) { + try zig_args.appendSlice(system_lib_gop.value_ptr.*); + continue; + } else { + system_lib_gop.value_ptr.* = &.{}; + } - if (other.linkage == .dynamic and - compile.rootModuleTarget().os.tag != .windows) - { - if (fs.path.dirname(full_path_lib)) |dirname| { - try zig_args.append("-rpath"); - try zig_args.append(dirname); - } + if (already_linked) + continue; + + if ((system_lib.search_strategy != prev_search_strategy or + system_lib.preferred_link_mode != prev_preferred_link_mode) and + compile.linkage != .static) + { + switch (system_lib.search_strategy) { + .no_fallback => switch (system_lib.preferred_link_mode) { + .dynamic => try zig_args.append("-search_dylibs_only"), + .static => try zig_args.append("-search_static_only"), + }, + .paths_first => switch (system_lib.preferred_link_mode) { + .dynamic => try zig_args.append("-search_paths_first"), + .static => try zig_args.append("-search_paths_first_static"), + }, + .mode_first => switch (system_lib.preferred_link_mode) { + .dynamic => try zig_args.append("-search_dylibs_first"), + .static => try zig_args.append("-search_static_first"), + }, } - }, - } - }, - .assembly_file => |asm_file| l: { - if (!my_responsibility) break :l; - - if (prev_has_cflags) { - try zig_args.append("-cflags"); - try zig_args.append("--"); - prev_has_cflags = false; - } - try zig_args.append(asm_file.getPath2(dep.module.owner, step)); - total_linker_objects += 1; - }, - - .c_source_file => |c_source_file| l: { - if (!my_responsibility) break :l; - - if (c_source_file.flags.len == 0) { + prev_search_strategy = system_lib.search_strategy; + prev_preferred_link_mode = system_lib.preferred_link_mode; + } + + const prefix: []const u8 = prefix: { + if (system_lib.needed) break :prefix "-needed-l"; + if (system_lib.weak) break :prefix "-weak-l"; + break :prefix "-l"; + }; + switch (system_lib.use_pkg_config) { + .no => try zig_args.append(b.fmt("{s}{s}", .{ prefix, system_lib.name })), + .yes, .force => { + if (compile.runPkgConfig(system_lib.name)) |result| { + try zig_args.appendSlice(result.cflags); + try zig_args.appendSlice(result.libs); + try seen_system_libs.put(arena, system_lib.name, result.cflags); + } else |err| switch (err) { + error.PkgConfigInvalidOutput, + error.PkgConfigCrashed, + error.PkgConfigFailed, + error.PkgConfigNotInstalled, + error.PackageNotFound, + => switch (system_lib.use_pkg_config) { + .yes => { + // pkg-config failed, so fall back to linking the library + // by name directly. + try zig_args.append(b.fmt("{s}{s}", .{ + prefix, + system_lib.name, + })); + }, + .force => { + panic("pkg-config failed for library {s}", .{system_lib.name}); + }, + .no => unreachable, + }, + + else => |e| return e, + } + }, + } + }, + .other_step => |other| { + switch (other.kind) { + .exe => return step.fail("cannot link with an executable build artifact", .{}), + .@"test" => return step.fail("cannot link with a test", .{}), + .obj => { + const included_in_lib_or_obj = !my_responsibility and + (dep_compile.kind == .lib or dep_compile.kind == .obj); + if (!already_linked and !included_in_lib_or_obj) { + try zig_args.append(other.getEmittedBin().getPath2(b, step)); + total_linker_objects += 1; + } + }, + .lib => l: { + const other_produces_implib = other.producesImplib(); + const other_is_static = other_produces_implib or other.isStaticLibrary(); + + if (compile.isStaticLibrary() and other_is_static) { + // Avoid putting a static library inside a static library. + break :l; + } + + // For DLLs, we must link against the implib. + // For everything else, we directly link + // against the library file. + const full_path_lib = if (other_produces_implib) + other.getGeneratedFilePath("generated_implib", &compile.step) + else + other.getGeneratedFilePath("generated_bin", &compile.step); + + try zig_args.append(full_path_lib); + total_linker_objects += 1; + + if (other.linkage == .dynamic and + compile.rootModuleTarget().os.tag != .windows) + { + if (fs.path.dirname(full_path_lib)) |dirname| { + try zig_args.append("-rpath"); + try zig_args.append(dirname); + } + } + }, + } + }, + .assembly_file => |asm_file| l: { + if (!my_responsibility) break :l; + if (prev_has_cflags) { try zig_args.append("-cflags"); try zig_args.append("--"); prev_has_cflags = false; } - } else { - try zig_args.append("-cflags"); - for (c_source_file.flags) |arg| { - try zig_args.append(arg); - } - try zig_args.append("--"); - prev_has_cflags = true; - } - try zig_args.append(c_source_file.file.getPath2(dep.module.owner, step)); - total_linker_objects += 1; - }, + try zig_args.append(asm_file.getPath2(mod.owner, step)); + total_linker_objects += 1; + }, - .c_source_files => |c_source_files| l: { - if (!my_responsibility) break :l; + .c_source_file => |c_source_file| l: { + if (!my_responsibility) break :l; - if (c_source_files.flags.len == 0) { - if (prev_has_cflags) { + if (c_source_file.flags.len == 0) { + if (prev_has_cflags) { + try zig_args.append("-cflags"); + try zig_args.append("--"); + prev_has_cflags = false; + } + } else { try zig_args.append("-cflags"); + for (c_source_file.flags) |arg| { + try zig_args.append(arg); + } try zig_args.append("--"); - prev_has_cflags = false; + prev_has_cflags = true; } - } else { - try zig_args.append("-cflags"); - for (c_source_files.flags) |flag| { - try zig_args.append(flag); + try zig_args.append(c_source_file.file.getPath2(mod.owner, step)); + total_linker_objects += 1; + }, + + .c_source_files => |c_source_files| l: { + if (!my_responsibility) break :l; + + if (c_source_files.flags.len == 0) { + if (prev_has_cflags) { + try zig_args.append("-cflags"); + try zig_args.append("--"); + prev_has_cflags = false; + } + } else { + try zig_args.append("-cflags"); + for (c_source_files.flags) |flag| { + try zig_args.append(flag); + } + try zig_args.append("--"); + prev_has_cflags = true; } - try zig_args.append("--"); - prev_has_cflags = true; - } - const root_path = c_source_files.root.getPath2(dep.module.owner, step); - for (c_source_files.files) |file| { - try zig_args.append(b.pathJoin(&.{ root_path, file })); - } + const root_path = c_source_files.root.getPath2(mod.owner, step); + for (c_source_files.files) |file| { + try zig_args.append(b.pathJoin(&.{ root_path, file })); + } - total_linker_objects += c_source_files.files.len; - }, + total_linker_objects += c_source_files.files.len; + }, - .win32_resource_file => |rc_source_file| l: { - if (!my_responsibility) break :l; + .win32_resource_file => |rc_source_file| l: { + if (!my_responsibility) break :l; - if (rc_source_file.flags.len == 0 and rc_source_file.include_paths.len == 0) { - if (prev_has_rcflags) { + if (rc_source_file.flags.len == 0 and rc_source_file.include_paths.len == 0) { + if (prev_has_rcflags) { + try zig_args.append("-rcflags"); + try zig_args.append("--"); + prev_has_rcflags = false; + } + } else { try zig_args.append("-rcflags"); + for (rc_source_file.flags) |arg| { + try zig_args.append(arg); + } + for (rc_source_file.include_paths) |include_path| { + try zig_args.append("/I"); + try zig_args.append(include_path.getPath2(mod.owner, step)); + } try zig_args.append("--"); - prev_has_rcflags = false; + prev_has_rcflags = true; } + try zig_args.append(rc_source_file.file.getPath2(mod.owner, step)); + total_linker_objects += 1; + }, + } + } + + // We need to emit the --mod argument here so that the above link objects + // have the correct parent module, but only if the module is part of + // this compilation. + if (!my_responsibility) continue; + if (cli_named_modules.modules.getIndex(mod)) |module_cli_index| { + const module_cli_name = cli_named_modules.names.keys()[module_cli_index]; + try mod.appendZigProcessFlags(&zig_args, step); + + // --dep arguments + try zig_args.ensureUnusedCapacity(mod.import_table.count() * 2); + for (mod.import_table.keys(), mod.import_table.values()) |name, import| { + const import_index = cli_named_modules.modules.getIndex(import).?; + const import_cli_name = cli_named_modules.names.keys()[import_index]; + zig_args.appendAssumeCapacity("--dep"); + if (std.mem.eql(u8, import_cli_name, name)) { + zig_args.appendAssumeCapacity(import_cli_name); } else { - try zig_args.append("-rcflags"); - for (rc_source_file.flags) |arg| { - try zig_args.append(arg); - } - for (rc_source_file.include_paths) |include_path| { - try zig_args.append("/I"); - try zig_args.append(include_path.getPath2(dep.module.owner, step)); - } - try zig_args.append("--"); - prev_has_rcflags = true; + zig_args.appendAssumeCapacity(b.fmt("{s}={s}", .{ name, import_cli_name })); } - try zig_args.append(rc_source_file.file.getPath2(dep.module.owner, step)); - total_linker_objects += 1; - }, - } - } - - // We need to emit the --mod argument here so that the above link objects - // have the correct parent module, but only if the module is part of - // this compilation. - if (!my_responsibility) continue; - if (cli_named_modules.modules.getIndex(dep.module)) |module_cli_index| { - const module_cli_name = cli_named_modules.names.keys()[module_cli_index]; - try dep.module.appendZigProcessFlags(&zig_args, step); - - // --dep arguments - try zig_args.ensureUnusedCapacity(dep.module.import_table.count() * 2); - for (dep.module.import_table.keys(), dep.module.import_table.values()) |name, import| { - const import_index = cli_named_modules.modules.getIndex(import).?; - const import_cli_name = cli_named_modules.names.keys()[import_index]; - zig_args.appendAssumeCapacity("--dep"); - if (std.mem.eql(u8, import_cli_name, name)) { - zig_args.appendAssumeCapacity(import_cli_name); - } else { - zig_args.appendAssumeCapacity(b.fmt("{s}={s}", .{ name, import_cli_name })); } - } - // When the CLI sees a -M argument, it determines whether it - // implies the existence of a Zig compilation unit based on - // whether there is a root source file. If there is no root - // source file, then this is not a zig compilation unit - it is - // perhaps a set of linker objects, or C source files instead. - // Linker objects are added to the CLI globally, while C source - // files must have a module parent. - if (dep.module.root_source_file) |lp| { - const src = lp.getPath2(dep.module.owner, step); - try zig_args.append(b.fmt("-M{s}={s}", .{ module_cli_name, src })); - } else if (moduleNeedsCliArg(dep.module)) { - try zig_args.append(b.fmt("-M{s}", .{module_cli_name})); + // When the CLI sees a -M argument, it determines whether it + // implies the existence of a Zig compilation unit based on + // whether there is a root source file. If there is no root + // source file, then this is not a zig compilation unit - it is + // perhaps a set of linker objects, or C source files instead. + // Linker objects are added to the CLI globally, while C source + // files must have a module parent. + if (mod.root_source_file) |lp| { + const src = lp.getPath2(mod.owner, step); + try zig_args.append(b.fmt("-M{s}={s}", .{ module_cli_name, src })); + } else if (moduleNeedsCliArg(mod)) { + try zig_args.append(b.fmt("-M{s}", .{module_cli_name})); + } } } } @@ -2064,3 +2061,35 @@ fn moduleNeedsCliArg(mod: *const Module) bool { else => continue, } else false; } + +/// Return the full set of `Step.Compile` which `start` depends on, recursively. `start` itself is +/// always returned as the first element. If `chase_dynamic` is `false`, then dynamic libraries are +/// not included, and their dependencies are not considered; if `chase_dynamic` is `true`, dynamic +/// libraries are treated the same as other linked `Compile`s. +pub fn getCompileDependencies(start: *Compile, chase_dynamic: bool) []const *Compile { + const arena = start.step.owner.graph.arena; + + var compiles: std.AutoArrayHashMapUnmanaged(*Compile, void) = .empty; + var next_idx: usize = 0; + + compiles.putNoClobber(arena, start, {}) catch @panic("OOM"); + + while (next_idx < compiles.count()) { + const compile = compiles.keys()[next_idx]; + next_idx += 1; + + for (compile.root_module.getGraph().modules) |mod| { + for (mod.link_objects.items) |lo| { + switch (lo) { + .other_step => |other_compile| { + if (!chase_dynamic and other_compile.isDynamicLibrary()) continue; + compiles.put(arena, other_compile, {}) catch @panic("OOM"); + }, + else => {}, + } + } + } + } + + return compiles.keys(); +} diff --git a/lib/std/Build/Step/Run.zig b/lib/std/Build/Step/Run.zig index 5408bb287b9aaf57aaa00f460b8a42aaca04eec1..37db10916faafe291c4016a0a256c22dc89a1a3f 100644 --- a/lib/std/Build/Step/Run.zig +++ b/lib/std/Build/Step/Run.zig @@ -1719,15 +1719,12 @@ fn evalGeneric(run: *Run, child: *std.process.Child) !StdIoResult { fn addPathForDynLibs(run: *Run, artifact: *Step.Compile) void { const b = run.step.owner; - var it = artifact.root_module.iterateDependencies(artifact, true); - while (it.next()) |item| { - const other = item.compile.?; - if (item.module == other.root_module) { - if (item.module.resolved_target.?.result.os.tag == .windows and - other.isDynamicLibrary()) - { - addPathDir(run, fs.path.dirname(other.getEmittedBin().getPath2(b, &run.step)).?); - } + const compiles = artifact.getCompileDependencies(true); + for (compiles) |compile| { + if (compile.root_module.resolved_target.?.result.os.tag == .windows and + compile.isDynamicLibrary()) + { + addPathDir(run, fs.path.dirname(compile.getEmittedBin().getPath2(b, &run.step)).?); } } } -- 2.54.0 From 6168b8ef86e2ca262585387d12166123842773d1 Mon Sep 17 00:00:00 2001 From: mlugg Date: Tue, 18 Jun 2024 23:49:21 +0500 Subject: [PATCH 06/16] std.Build: add API to create Compile steps from existing module This commit amends `std.Build.ExecutableOptions` etc to have a new field, `root_module`, which allows artifacts to be created whose root module is an existing `*Module` rather than a freshly constructed one. This API can be far more versatile, allowing construction of complex module graphs before creating any compile steps, and therefore also allowing easy reuse of modules. The fields which correspond to module options, such as `root_source_file`, are all considered deprecated. They may not be populated at the same time as the `root_module` field. In the next release cycle, these deprecated fields will be removed, and the `root_module` field made non-optional. --- lib/std/Build.zig | 252 +++++++++++++++++++++--------- lib/std/Build/Step/TranslateC.zig | 1 + 2 files changed, 176 insertions(+), 77 deletions(-) diff --git a/lib/std/Build.zig b/lib/std/Build.zig index 12b6baae73b56e4ee3a1e7f1d80796316c7275f6..636b5bbe4f6c3bfb034bc693554819f16f9cb789 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -687,24 +687,9 @@ pub fn addOptions(b: *Build) *Step.Options { pub const ExecutableOptions = struct { name: []const u8, - /// If you want the executable to run on the same computer as the one - /// building the package, pass the `host` field of the package's `Build` - /// instance. - target: ResolvedTarget, - root_source_file: ?LazyPath = null, version: ?std.SemanticVersion = null, - optimize: std.builtin.OptimizeMode = .Debug, - code_model: std.builtin.CodeModel = .default, linkage: ?std.builtin.LinkMode = null, max_rss: usize = 0, - link_libc: ?bool = null, - single_threaded: ?bool = null, - pic: ?bool = null, - strip: ?bool = null, - unwind_tables: ?std.builtin.UnwindTables = null, - omit_frame_pointer: ?bool = null, - sanitize_thread: ?bool = null, - error_tracing: ?bool = null, use_llvm: ?bool = null, use_lld: ?bool = null, zig_lib_dir: ?LazyPath = null, @@ -714,14 +699,47 @@ pub const ExecutableOptions = struct { /// Can be set regardless of target. The `.manifest` file will be ignored /// if the target object format does not support embedded manifests. win32_manifest: ?LazyPath = null, + + /// Prefer populating this field (using e.g. `createModule`) instead of populating + /// the following fields (`root_source_file` etc). In a future release, those fields + /// will be removed, and this field will become non-optional. + root_module: ?*Module = null, + + /// Deprecated; prefer populating `root_module`. + root_source_file: ?LazyPath = null, + /// Deprecated; prefer populating `root_module`. + target: ?ResolvedTarget = null, + /// Deprecated; prefer populating `root_module`. + optimize: std.builtin.OptimizeMode = .Debug, + /// Deprecated; prefer populating `root_module`. + code_model: std.builtin.CodeModel = .default, + /// Deprecated; prefer populating `root_module`. + link_libc: ?bool = null, + /// Deprecated; prefer populating `root_module`. + single_threaded: ?bool = null, + /// Deprecated; prefer populating `root_module`. + pic: ?bool = null, + /// Deprecated; prefer populating `root_module`. + strip: ?bool = null, + /// Deprecated; prefer populating `root_module`. + unwind_tables: ?std.builtin.UnwindTables = null, + /// Deprecated; prefer populating `root_module`. + omit_frame_pointer: ?bool = null, + /// Deprecated; prefer populating `root_module`. + sanitize_thread: ?bool = null, + /// Deprecated; prefer populating `root_module`. + error_tracing: ?bool = null, }; pub fn addExecutable(b: *Build, options: ExecutableOptions) *Step.Compile { - return Step.Compile.create(b, .{ + if (options.root_module != null and options.target != null) { + @panic("`root_module` and `target` cannot both be populated"); + } + return .create(b, .{ .name = options.name, - .root_module = b.createModule(.{ + .root_module = options.root_module orelse b.createModule(.{ .root_source_file = options.root_source_file, - .target = options.target, + .target = options.target orelse @panic("`root_module` and `target` cannot both be null"), .optimize = options.optimize, .link_libc = options.link_libc, .single_threaded = options.single_threaded, @@ -746,32 +764,51 @@ pub fn addExecutable(b: *Build, options: ExecutableOptions) *Step.Compile { pub const ObjectOptions = struct { name: []const u8, + max_rss: usize = 0, + use_llvm: ?bool = null, + use_lld: ?bool = null, + zig_lib_dir: ?LazyPath = null, + + /// Prefer populating this field (using e.g. `createModule`) instead of populating + /// the following fields (`root_source_file` etc). In a future release, those fields + /// will be removed, and this field will become non-optional. + root_module: ?*Module = null, + + /// Deprecated; prefer populating `root_module`. root_source_file: ?LazyPath = null, - /// To choose the same computer as the one building the package, pass the - /// `host` field of the package's `Build` instance. - target: ResolvedTarget, + /// Deprecated; prefer populating `root_module`. + target: ?ResolvedTarget = null, + /// Deprecated; prefer populating `root_module`. + optimize: std.builtin.OptimizeMode = .Debug, + /// Deprecated; prefer populating `root_module`. code_model: std.builtin.CodeModel = .default, - optimize: std.builtin.OptimizeMode, - max_rss: usize = 0, + /// Deprecated; prefer populating `root_module`. link_libc: ?bool = null, + /// Deprecated; prefer populating `root_module`. single_threaded: ?bool = null, + /// Deprecated; prefer populating `root_module`. pic: ?bool = null, + /// Deprecated; prefer populating `root_module`. strip: ?bool = null, + /// Deprecated; prefer populating `root_module`. unwind_tables: ?std.builtin.UnwindTables = null, + /// Deprecated; prefer populating `root_module`. omit_frame_pointer: ?bool = null, + /// Deprecated; prefer populating `root_module`. sanitize_thread: ?bool = null, + /// Deprecated; prefer populating `root_module`. error_tracing: ?bool = null, - use_llvm: ?bool = null, - use_lld: ?bool = null, - zig_lib_dir: ?LazyPath = null, }; pub fn addObject(b: *Build, options: ObjectOptions) *Step.Compile { - return Step.Compile.create(b, .{ + if (options.root_module != null and options.target != null) { + @panic("`root_module` and `target` cannot both be populated"); + } + return .create(b, .{ .name = options.name, - .root_module = b.createModule(.{ + .root_module = options.root_module orelse b.createModule(.{ .root_source_file = options.root_source_file, - .target = options.target, + .target = options.target orelse @panic("`root_module` and `target` cannot both be null"), .optimize = options.optimize, .link_libc = options.link_libc, .single_threaded = options.single_threaded, @@ -793,22 +830,8 @@ pub fn addObject(b: *Build, options: ObjectOptions) *Step.Compile { pub const SharedLibraryOptions = struct { name: []const u8, - /// To choose the same computer as the one building the package, pass the - /// `host` field of the package's `Build` instance. - target: ResolvedTarget, - optimize: std.builtin.OptimizeMode, - code_model: std.builtin.CodeModel = .default, - root_source_file: ?LazyPath = null, version: ?std.SemanticVersion = null, max_rss: usize = 0, - link_libc: ?bool = null, - single_threaded: ?bool = null, - pic: ?bool = null, - strip: ?bool = null, - unwind_tables: ?std.builtin.UnwindTables = null, - omit_frame_pointer: ?bool = null, - sanitize_thread: ?bool = null, - error_tracing: ?bool = null, use_llvm: ?bool = null, use_lld: ?bool = null, zig_lib_dir: ?LazyPath = null, @@ -818,13 +841,46 @@ pub const SharedLibraryOptions = struct { /// Can be set regardless of target. The `.manifest` file will be ignored /// if the target object format does not support embedded manifests. win32_manifest: ?LazyPath = null, + + /// Prefer populating this field (using e.g. `createModule`) instead of populating + /// the following fields (`root_source_file` etc). In a future release, those fields + /// will be removed, and this field will become non-optional. + root_module: ?*Module = null, + + /// Deprecated; prefer populating `root_module`. + root_source_file: ?LazyPath = null, + /// Deprecated; prefer populating `root_module`. + target: ?ResolvedTarget = null, + /// Deprecated; prefer populating `root_module`. + optimize: std.builtin.OptimizeMode = .Debug, + /// Deprecated; prefer populating `root_module`. + code_model: std.builtin.CodeModel = .default, + /// Deprecated; prefer populating `root_module`. + link_libc: ?bool = null, + /// Deprecated; prefer populating `root_module`. + single_threaded: ?bool = null, + /// Deprecated; prefer populating `root_module`. + pic: ?bool = null, + /// Deprecated; prefer populating `root_module`. + strip: ?bool = null, + /// Deprecated; prefer populating `root_module`. + unwind_tables: ?std.builtin.UnwindTables = null, + /// Deprecated; prefer populating `root_module`. + omit_frame_pointer: ?bool = null, + /// Deprecated; prefer populating `root_module`. + sanitize_thread: ?bool = null, + /// Deprecated; prefer populating `root_module`. + error_tracing: ?bool = null, }; pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile { - return Step.Compile.create(b, .{ + if (options.root_module != null and options.target != null) { + @panic("`root_module` and `target` cannot both be populated"); + } + return .create(b, .{ .name = options.name, - .root_module = b.createModule(.{ - .target = options.target, + .root_module = options.root_module orelse b.createModule(.{ + .target = options.target orelse @panic("`root_module` and `target` cannot both be null"), .optimize = options.optimize, .root_source_file = options.root_source_file, .link_libc = options.link_libc, @@ -850,32 +906,51 @@ pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile pub const StaticLibraryOptions = struct { name: []const u8, - root_source_file: ?LazyPath = null, - /// To choose the same computer as the one building the package, pass the - /// `host` field of the package's `Build` instance. - target: ResolvedTarget, - optimize: std.builtin.OptimizeMode, - code_model: std.builtin.CodeModel = .default, version: ?std.SemanticVersion = null, max_rss: usize = 0, + use_llvm: ?bool = null, + use_lld: ?bool = null, + zig_lib_dir: ?LazyPath = null, + + /// Prefer populating this field (using e.g. `createModule`) instead of populating + /// the following fields (`root_source_file` etc). In a future release, those fields + /// will be removed, and this field will become non-optional. + root_module: ?*Module = null, + + /// Deprecated; prefer populating `root_module`. + root_source_file: ?LazyPath = null, + /// Deprecated; prefer populating `root_module`. + target: ?ResolvedTarget = null, + /// Deprecated; prefer populating `root_module`. + optimize: std.builtin.OptimizeMode = .Debug, + /// Deprecated; prefer populating `root_module`. + code_model: std.builtin.CodeModel = .default, + /// Deprecated; prefer populating `root_module`. link_libc: ?bool = null, + /// Deprecated; prefer populating `root_module`. single_threaded: ?bool = null, + /// Deprecated; prefer populating `root_module`. pic: ?bool = null, + /// Deprecated; prefer populating `root_module`. strip: ?bool = null, + /// Deprecated; prefer populating `root_module`. unwind_tables: ?std.builtin.UnwindTables = null, + /// Deprecated; prefer populating `root_module`. omit_frame_pointer: ?bool = null, + /// Deprecated; prefer populating `root_module`. sanitize_thread: ?bool = null, + /// Deprecated; prefer populating `root_module`. error_tracing: ?bool = null, - use_llvm: ?bool = null, - use_lld: ?bool = null, - zig_lib_dir: ?LazyPath = null, }; pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile { - return Step.Compile.create(b, .{ + if (options.root_module != null and options.target != null) { + @panic("`root_module` and `target` cannot both be populated"); + } + return .create(b, .{ .name = options.name, - .root_module = b.createModule(.{ - .target = options.target, + .root_module = options.root_module orelse b.createModule(.{ + .target = options.target orelse @panic("`root_module` and `target` cannot both be null"), .optimize = options.optimize, .root_source_file = options.root_source_file, .link_libc = options.link_libc, @@ -900,27 +975,46 @@ pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile pub const TestOptions = struct { name: []const u8 = "test", - root_source_file: LazyPath, - target: ?ResolvedTarget = null, - optimize: std.builtin.OptimizeMode = .Debug, - version: ?std.SemanticVersion = null, max_rss: usize = 0, - /// deprecated: use `.filters = &.{filter}` instead of `.filter = filter`. + /// Deprecated; use `.filters = &.{filter}` instead of `.filter = filter`. filter: ?[]const u8 = null, filters: []const []const u8 = &.{}, test_runner: ?LazyPath = null, + use_llvm: ?bool = null, + use_lld: ?bool = null, + zig_lib_dir: ?LazyPath = null, + + /// Prefer populating this field (using e.g. `createModule`) instead of populating + /// the following fields (`root_source_file` etc). In a future release, those fields + /// will be removed, and this field will become non-optional. + root_module: ?*Module = null, + + /// Deprecated; prefer populating `root_module`. + root_source_file: ?LazyPath = null, + /// Deprecated; prefer populating `root_module`. + target: ?ResolvedTarget = null, + /// Deprecated; prefer populating `root_module`. + optimize: std.builtin.OptimizeMode = .Debug, + /// Deprecated; prefer populating `root_module`. + version: ?std.SemanticVersion = null, + /// Deprecated; prefer populating `root_module`. link_libc: ?bool = null, + /// Deprecated; prefer populating `root_module`. link_libcpp: ?bool = null, + /// Deprecated; prefer populating `root_module`. single_threaded: ?bool = null, + /// Deprecated; prefer populating `root_module`. pic: ?bool = null, + /// Deprecated; prefer populating `root_module`. strip: ?bool = null, + /// Deprecated; prefer populating `root_module`. unwind_tables: ?std.builtin.UnwindTables = null, + /// Deprecated; prefer populating `root_module`. omit_frame_pointer: ?bool = null, + /// Deprecated; prefer populating `root_module`. sanitize_thread: ?bool = null, + /// Deprecated; prefer populating `root_module`. error_tracing: ?bool = null, - use_llvm: ?bool = null, - use_lld: ?bool = null, - zig_lib_dir: ?LazyPath = null, }; /// Creates an executable containing unit tests. @@ -932,11 +1026,14 @@ pub const TestOptions = struct { /// two steps are separated because they are independently configured and /// cached. pub fn addTest(b: *Build, options: TestOptions) *Step.Compile { - return Step.Compile.create(b, .{ + if (options.root_module != null and options.root_source_file != null) { + @panic("`root_module` and `root_source_file` cannot both be populated"); + } + return .create(b, .{ .name = options.name, .kind = .@"test", - .root_module = b.createModule(.{ - .root_source_file = options.root_source_file, + .root_module = options.root_module orelse b.createModule(.{ + .root_source_file = options.root_source_file orelse @panic("`root_module` and `root_source_file` cannot both be null"), .target = options.target orelse b.graph.host, .optimize = options.optimize, .link_libc = options.link_libc, @@ -974,19 +1071,20 @@ pub const AssemblyOptions = struct { zig_lib_dir: ?LazyPath = null, }; +/// Deprecated; prefer using `addObject` where the `root_module` has an empty +/// `root_source_file` and contains an assembly file via `Module.addAssemblyFile`. pub fn addAssembly(b: *Build, options: AssemblyOptions) *Step.Compile { - const obj_step = Step.Compile.create(b, .{ + const root_module = b.createModule(.{ + .target = options.target, + .optimize = options.optimize, + }); + root_module.addAssemblyFile(options.source_file); + return b.addObject(.{ .name = options.name, - .kind = .obj, - .root_module = b.createModule(.{ - .target = options.target, - .optimize = options.optimize, - }), .max_rss = options.max_rss, .zig_lib_dir = options.zig_lib_dir, + .root_module = root_module, }); - obj_step.addAssemblyFile(options.source_file); - return obj_step; } /// This function creates a module and adds it to the package's module set, making diff --git a/lib/std/Build/Step/TranslateC.zig b/lib/std/Build/Step/TranslateC.zig index a2585cafd0ba6054ec9d922e2bd9f1aea01f9211..2592719f80153bb1d2134eb1a006c3efb07a51c8 100644 --- a/lib/std/Build/Step/TranslateC.zig +++ b/lib/std/Build/Step/TranslateC.zig @@ -63,6 +63,7 @@ pub fn getOutput(translate_c: *TranslateC) std.Build.LazyPath { return .{ .generated = .{ .file = &translate_c.output_file } }; } +/// Deprecated: use `createModule` or `addModule` with `std.Build.addExecutable` instead. /// Creates a step to build an executable from the translated source. pub fn addExecutable(translate_c: *TranslateC, options: AddExecutableOptions) *Step.Compile { return translate_c.step.owner.addExecutable(.{ -- 2.54.0 From 93af1de0502d8041debf96e3579af04c8f7f8e38 Mon Sep 17 00:00:00 2001 From: mlugg Date: Sun, 15 Dec 2024 18:09:38 +0000 Subject: [PATCH 07/16] build.zig: migrate from deprecated std.Build APIs --- build.zig | 160 +++++++++++++++++++++++++++++------------------------- 1 file changed, 86 insertions(+), 74 deletions(-) diff --git a/build.zig b/build.zig index 47f4bad49c28b328cc27f77334baa5f22f94b3a2..d79a785c76a2c8fcac2fc04cec729c5710e4f52f 100644 --- a/build.zig +++ b/build.zig @@ -50,10 +50,12 @@ pub fn build(b: *std.Build) !void { const autodoc_test = b.addObject(.{ .name = "std", - .root_source_file = b.path("lib/std/std.zig"), - .target = target, .zig_lib_dir = b.path("lib"), - .optimize = .Debug, + .root_module = b.createModule(.{ + .root_source_file = b.path("lib/std/std.zig"), + .target = target, + .optimize = .Debug, + }), }); const install_std_docs = b.addInstallDirectory(.{ .source_dir = autodoc_test.getEmittedDocs(), @@ -234,7 +236,7 @@ pub fn build(b: *std.Build) !void { 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); if (link_libc) { - exe.linkLibC(); + exe.root_module.link_libc = true; } const is_debug = optimize == .Debug; @@ -330,15 +332,15 @@ pub fn build(b: *std.Build) !void { try addCmakeCfgOptionsToExe(b, cfg, exe, use_zig_libcxx); } else { // Here we are -Denable-llvm but no cmake integration. - try addStaticLlvmOptionsToExe(exe); + try addStaticLlvmOptionsToModule(exe.root_module); } if (target.result.os.tag == .windows) { // LLVM depends on networking as of version 18. - exe.linkSystemLibrary("ws2_32"); + exe.root_module.linkSystemLibrary("ws2_32", .{}); - exe.linkSystemLibrary("version"); - exe.linkSystemLibrary("uuid"); - exe.linkSystemLibrary("ole32"); + exe.root_module.linkSystemLibrary("version", .{}); + exe.root_module.linkSystemLibrary("uuid", .{}); + exe.root_module.linkSystemLibrary("ole32", .{}); } } @@ -364,16 +366,16 @@ pub fn build(b: *std.Build) !void { else &[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined" }; - exe.addIncludePath(.{ .cwd_relative = tracy_path }); - exe.addCSourceFile(.{ .file = .{ .cwd_relative = client_cpp }, .flags = tracy_c_flags }); + exe.root_module.addIncludePath(.{ .cwd_relative = tracy_path }); + exe.root_module.addCSourceFile(.{ .file = .{ .cwd_relative = client_cpp }, .flags = tracy_c_flags }); if (!enable_llvm) { exe.root_module.linkSystemLibrary("c++", .{ .use_pkg_config = .no }); } - exe.linkLibC(); + exe.root_module.link_libc = true; if (target.result.os.tag == .windows) { - exe.linkSystemLibrary("dbghelp"); - exe.linkSystemLibrary("ws2_32"); + exe.root_module.linkSystemLibrary("dbghelp", .{}); + exe.root_module.linkSystemLibrary("ws2_32", .{}); } } @@ -559,8 +561,10 @@ pub fn build(b: *std.Build) !void { if (opt_mingw_src_path) |mingw_src_path| { const update_mingw_exe = b.addExecutable(.{ .name = "update_mingw", - .target = b.graph.host, - .root_source_file = b.path("tools/update_mingw.zig"), + .root_module = b.createModule(.{ + .target = b.graph.host, + .root_source_file = b.path("tools/update_mingw.zig"), + }), }); const update_mingw_run = b.addRunArtifact(update_mingw_exe); update_mingw_run.addDirectoryArg(b.path("lib")); @@ -636,12 +640,10 @@ const AddCompilerStepOptions = struct { }; fn addCompilerStep(b: *std.Build, options: AddCompilerStepOptions) *std.Build.Step.Compile { - const exe = b.addExecutable(.{ - .name = "zig", + const compiler_mod = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = options.target, .optimize = options.optimize, - .max_rss = 7_800_000_000, .strip = options.strip, .sanitize_thread = options.sanitize_thread, .single_threaded = options.single_threaded, @@ -663,26 +665,28 @@ fn addCompilerStep(b: *std.Build, options: AddCompilerStepOptions) *std.Build.St .loongarch32, .loongarch64 => .medium, else => .default, }, + .valgrind = options.valgrind, }); - exe.root_module.valgrind = options.valgrind; - exe.stack_size = stack_size; - const aro_module = b.createModule(.{ + const aro_mod = b.createModule(.{ .root_source_file = b.path("lib/compiler/aro/aro.zig"), }); - const aro_translate_c_module = b.createModule(.{ + const aro_translate_c_mod = b.createModule(.{ .root_source_file = b.path("lib/compiler/aro_translate_c.zig"), - .imports = &.{ - .{ - .name = "aro", - .module = aro_module, - }, - }, }); - exe.root_module.addImport("aro", aro_module); - exe.root_module.addImport("aro_translate_c", aro_translate_c_module); + aro_translate_c_mod.addImport("aro", aro_mod); + compiler_mod.addImport("aro", aro_mod); + compiler_mod.addImport("aro_translate_c", aro_translate_c_mod); + + const exe = b.addExecutable(.{ + .name = "zig", + .max_rss = 7_800_000_000, + .root_module = compiler_mod, + }); + exe.stack_size = stack_size; + return exe; } @@ -707,11 +711,15 @@ fn addCmakeCfgOptionsToExe( exe: *std.Build.Step.Compile, use_zig_libcxx: bool, ) !void { - if (exe.rootModuleTarget().isDarwin()) { + const mod = exe.root_module; + const target = mod.resolved_target.?.result; + + if (target.isDarwin()) { // useful for package maintainers exe.headerpad_max_install_names = true; } - exe.addObjectFile(.{ .cwd_relative = b.pathJoin(&[_][]const u8{ + + mod.addObjectFile(.{ .cwd_relative = b.pathJoin(&.{ cfg.cmake_binary_dir, "zigcpp", b.fmt("{s}{s}{s}", .{ @@ -721,38 +729,38 @@ fn addCmakeCfgOptionsToExe( }), }) }); assert(cfg.lld_include_dir.len != 0); - exe.addIncludePath(.{ .cwd_relative = cfg.lld_include_dir }); - exe.addIncludePath(.{ .cwd_relative = cfg.llvm_include_dir }); - exe.addLibraryPath(.{ .cwd_relative = cfg.llvm_lib_dir }); - addCMakeLibraryList(exe, cfg.clang_libraries); - addCMakeLibraryList(exe, cfg.lld_libraries); - addCMakeLibraryList(exe, cfg.llvm_libraries); + mod.addIncludePath(.{ .cwd_relative = cfg.lld_include_dir }); + mod.addIncludePath(.{ .cwd_relative = cfg.llvm_include_dir }); + mod.addLibraryPath(.{ .cwd_relative = cfg.llvm_lib_dir }); + addCMakeLibraryList(mod, cfg.clang_libraries); + addCMakeLibraryList(mod, cfg.lld_libraries); + addCMakeLibraryList(mod, cfg.llvm_libraries); if (use_zig_libcxx) { - exe.linkLibCpp(); + mod.link_libcpp = true; } else { // System -lc++ must be used because in this code path we are attempting to link // against system-provided LLVM, Clang, LLD. const need_cpp_includes = true; const static = cfg.llvm_linkage == .static; - const lib_suffix = if (static) exe.rootModuleTarget().staticLibSuffix()[1..] else exe.rootModuleTarget().dynamicLibSuffix()[1..]; - switch (exe.rootModuleTarget().os.tag) { + const lib_suffix = if (static) target.staticLibSuffix()[1..] else target.dynamicLibSuffix()[1..]; + switch (target.os.tag) { .linux => { // First we try to link against the detected libcxx name. If that doesn't work, we fall // back to -lc++ and cross our fingers. addCxxKnownPath(b, cfg, exe, b.fmt("lib{s}.{s}", .{ cfg.system_libcxx, lib_suffix }), "", need_cpp_includes) catch |err| switch (err) { error.RequiredLibraryNotFound => { - exe.linkLibCpp(); + mod.link_libcpp = true; }, else => |e| return e, }; - exe.linkSystemLibrary("unwind"); + mod.linkSystemLibrary("unwind", .{}); }, .ios, .macos, .watchos, .tvos, .visionos => { - exe.linkLibCpp(); + mod.link_libcpp = true; }, .windows => { - if (exe.rootModuleTarget().abi != .msvc) exe.linkLibCpp(); + if (target.abi != .msvc) mod.link_libcpp = true; }, .freebsd => { if (static) { @@ -786,46 +794,46 @@ fn addCmakeCfgOptionsToExe( } if (cfg.dia_guids_lib.len != 0) { - exe.addObjectFile(.{ .cwd_relative = cfg.dia_guids_lib }); + mod.addObjectFile(.{ .cwd_relative = cfg.dia_guids_lib }); } } -fn addStaticLlvmOptionsToExe(exe: *std.Build.Step.Compile) !void { +fn addStaticLlvmOptionsToModule(mod: *std.Build.Module) !void { // Adds the Zig C++ sources which both stage1 and stage2 need. // // We need this because otherwise zig_clang_cc1_main.cpp ends up pulling // in a dependency on llvm::cfg::Update::dump() which is // unavailable when LLVM is compiled in Release mode. const zig_cpp_cflags = exe_cflags ++ [_][]const u8{"-DNDEBUG=1"}; - exe.addCSourceFiles(.{ + mod.addCSourceFiles(.{ .files = &zig_cpp_sources, .flags = &zig_cpp_cflags, }); for (clang_libs) |lib_name| { - exe.linkSystemLibrary(lib_name); + mod.linkSystemLibrary(lib_name, .{}); } for (lld_libs) |lib_name| { - exe.linkSystemLibrary(lib_name); + mod.linkSystemLibrary(lib_name, .{}); } for (llvm_libs) |lib_name| { - exe.linkSystemLibrary(lib_name); + mod.linkSystemLibrary(lib_name, .{}); } - exe.linkSystemLibrary("z"); - exe.linkSystemLibrary("zstd"); + mod.linkSystemLibrary("z", .{}); + mod.linkSystemLibrary("zstd", .{}); - if (exe.rootModuleTarget().os.tag != .windows or exe.rootModuleTarget().abi != .msvc) { + if (mod.resolved_target.?.result.os.tag != .windows or mod.resolved_target.?.result.abi != .msvc) { // This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries. - exe.linkSystemLibrary("c++"); + mod.linkSystemLibrary("c++", .{}); } - if (exe.rootModuleTarget().os.tag == .windows) { - exe.linkSystemLibrary("version"); - exe.linkSystemLibrary("uuid"); - exe.linkSystemLibrary("ole32"); + if (mod.resolved_target.?.result.os.tag == .windows) { + mod.linkSystemLibrary("version", .{}); + mod.linkSystemLibrary("uuid", .{}); + mod.linkSystemLibrary("ole32", .{}); } } @@ -862,29 +870,29 @@ fn addCxxKnownPath( // but libc++ may very well be one, so force all inputs to be checked when passing // an explicit path to libc++. exe.allow_so_scripts = true; - exe.addObjectFile(.{ .cwd_relative = path_unpadded }); + exe.root_module.addObjectFile(.{ .cwd_relative = path_unpadded }); // TODO a way to integrate with system c++ include files here // c++ -E -Wp,-v -xc++ /dev/null if (need_cpp_includes) { // I used these temporarily for testing something but we obviously need a // more general purpose solution here. - //exe.addIncludePath("/nix/store/2lr0fc0ak8rwj0k8n3shcyz1hz63wzma-gcc-11.3.0/include/c++/11.3.0"); - //exe.addIncludePath("/nix/store/2lr0fc0ak8rwj0k8n3shcyz1hz63wzma-gcc-11.3.0/include/c++/11.3.0/x86_64-unknown-linux-gnu"); + //exe.root_module.addIncludePath("/nix/store/2lr0fc0ak8rwj0k8n3shcyz1hz63wzma-gcc-11.3.0/include/c++/11.3.0"); + //exe.root_module.addIncludePath("/nix/store/2lr0fc0ak8rwj0k8n3shcyz1hz63wzma-gcc-11.3.0/include/c++/11.3.0/x86_64-unknown-linux-gnu"); } } -fn addCMakeLibraryList(exe: *std.Build.Step.Compile, list: []const u8) void { +fn addCMakeLibraryList(mod: *std.Build.Module, list: []const u8) void { var it = mem.tokenizeScalar(u8, list, ';'); while (it.next()) |lib| { if (mem.startsWith(u8, lib, "-l")) { - exe.linkSystemLibrary(lib["-l".len..]); - } else if (exe.rootModuleTarget().os.tag == .windows and + mod.linkSystemLibrary(lib["-l".len..], .{}); + } else if (mod.resolved_target.?.result.os.tag == .windows and mem.endsWith(u8, lib, ".lib") and !fs.path.isAbsolute(lib)) { - exe.linkSystemLibrary(lib[0 .. lib.len - ".lib".len]); + mod.linkSystemLibrary(lib[0 .. lib.len - ".lib".len], .{}); } else { - exe.addObjectFile(.{ .cwd_relative = lib }); + mod.addObjectFile(.{ .cwd_relative = lib }); } } } @@ -1306,9 +1314,11 @@ const llvm_libs = [_][]const u8{ fn generateLangRef(b: *std.Build) std.Build.LazyPath { const doctest_exe = b.addExecutable(.{ .name = "doctest", - .root_source_file = b.path("tools/doctest.zig"), - .target = b.graph.host, - .optimize = .Debug, + .root_module = b.createModule(.{ + .root_source_file = b.path("tools/doctest.zig"), + .target = b.graph.host, + .optimize = .Debug, + }), }); 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 { const docgen_exe = b.addExecutable(.{ .name = "docgen", - .root_source_file = b.path("tools/docgen.zig"), - .target = b.graph.host, - .optimize = .Debug, + .root_module = b.createModule(.{ + .root_source_file = b.path("tools/docgen.zig"), + .target = b.graph.host, + .optimize = .Debug, + }), }); const docgen_cmd = b.addRunArtifact(docgen_exe); -- 2.54.0 From b8472db215a892754b7e834ce44b2d7f254ff60d Mon Sep 17 00:00:00 2001 From: mlugg Date: Mon, 16 Dec 2024 15:09:34 +0000 Subject: [PATCH 08/16] tests.zig: migrate from deprecated std.Build APIs --- test/tests.zig | 87 +++++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 36 deletions(-) diff --git a/test/tests.zig b/test/tests.zig index d1102ff0669810c5c4fc680d5197021181b1942d..5e037b1c594feeee8787d786d3394cb082ec3bdf 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -1106,9 +1106,11 @@ pub fn addStackTraceTests( ) *Step { const check_exe = b.addExecutable(.{ .name = "check-stack-trace", - .root_source_file = b.path("test/src/check-stack-trace.zig"), - .target = b.graph.host, - .optimize = .Debug, + .root_module = b.createModule(.{ + .root_source_file = b.path("test/src/check-stack-trace.zig"), + .target = b.graph.host, + .optimize = .Debug, + }), }); const cases = b.allocator.create(StackTracesContext) catch @panic("OOM"); @@ -1330,7 +1332,7 @@ pub fn addCliTests(b: *std.Build) *Step { run5.step.dependOn(&run4.step); const unformatted_code_utf16 = "\xff\xfe \x00 \x00 \x00 \x00/\x00/\x00 \x00n\x00o\x00 \x00r\x00e\x00a\x00s\x00o\x00n\x00"; - const fmt6_path = std.fs.path.join(b.allocator, &.{ tmp_path, "fmt6.zig" }) catch @panic("OOM"); + const fmt6_path = b.pathJoin(&.{ tmp_path, "fmt6.zig" }); const write6 = b.addUpdateSourceFiles(); write6.addBytesToSource(unformatted_code_utf16, fmt6_path); write6.step.dependOn(&run5.step); @@ -1514,18 +1516,20 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step { options.max_rss; const these_tests = b.addTest(.{ - .root_source_file = b.path(options.root_src), - .optimize = test_target.optimize_mode, - .target = resolved_target, + .root_module = b.createModule(.{ + .root_source_file = b.path(options.root_src), + .optimize = test_target.optimize_mode, + .target = resolved_target, + .link_libc = test_target.link_libc, + .pic = test_target.pic, + .strip = test_target.strip, + .single_threaded = test_target.single_threaded, + }), .max_rss = max_rss, .filters = options.test_filters, - .link_libc = test_target.link_libc, - .single_threaded = test_target.single_threaded, .use_llvm = test_target.use_llvm, .use_lld = test_target.use_lld, .zig_lib_dir = b.path("lib"), - .pic = test_target.pic, - .strip = test_target.strip, }); if (options.no_builtin) these_tests.no_builtin = true; if (options.build_options) |build_options| { @@ -1561,12 +1565,17 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step { var altered_query = test_target.target; altered_query.ofmt = null; - const compile_c = b.addExecutable(.{ - .name = qualified_name, + const compile_c = b.createModule(.{ + .root_source_file = null, .link_libc = test_target.link_libc, .target = b.resolveTargetQuery(altered_query), + }); + const compile_c_exe = b.addExecutable(.{ + .name = qualified_name, + .root_module = compile_c, .zig_lib_dir = b.path("lib"), }); + compile_c.addCSourceFile(.{ .file = these_tests.getEmittedBin(), .flags = &.{ @@ -1611,22 +1620,22 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step { continue; } if (test_target.link_libc == false) { - compile_c.subsystem = .Console; - compile_c.linkSystemLibrary("kernel32"); - compile_c.linkSystemLibrary("ntdll"); + compile_c_exe.subsystem = .Console; + compile_c.linkSystemLibrary("kernel32", .{}); + compile_c.linkSystemLibrary("ntdll", .{}); } if (mem.eql(u8, options.name, "std")) { if (test_target.link_libc == false) { - compile_c.linkSystemLibrary("shell32"); - compile_c.linkSystemLibrary("advapi32"); + compile_c.linkSystemLibrary("shell32", .{}); + compile_c.linkSystemLibrary("advapi32", .{}); } - compile_c.linkSystemLibrary("crypt32"); - compile_c.linkSystemLibrary("ws2_32"); - compile_c.linkSystemLibrary("ole32"); + compile_c.linkSystemLibrary("crypt32", .{}); + compile_c.linkSystemLibrary("ws2_32", .{}); + compile_c.linkSystemLibrary("ole32", .{}); } } - const run = b.addRunArtifact(compile_c); + const run = b.addRunArtifact(compile_c_exe); run.skip_foreign_checks = true; run.enableTestRunnerMode(); run.setName(b.fmt("run test {s}", .{qualified_name})); @@ -1675,6 +1684,20 @@ pub fn addCAbiTests(b: *std.Build, options: CAbiTestOptions) *Step { continue; } + const test_mod = b.createModule(.{ + .root_source_file = b.path("test/c_abi/main.zig"), + .target = resolved_target, + .optimize = optimize_mode, + .link_libc = true, + .pic = c_abi_target.pic, + .strip = c_abi_target.strip, + }); + test_mod.addCSourceFile(.{ + .file = b.path("test/c_abi/cfuncs.c"), + .flags = &.{"-std=c99"}, + }); + for (c_abi_target.c_defines) |define| test_mod.addCMacro(define, "1"); + const test_step = b.addTest(.{ .name = b.fmt("test-c-abi-{s}-{s}-{s}{s}{s}{s}", .{ triple_txt, @@ -1691,20 +1714,10 @@ pub fn addCAbiTests(b: *std.Build, options: CAbiTestOptions) *Step { if (c_abi_target.use_lld == false) "-no-lld" else "", if (c_abi_target.pic == true) "-pic" else "", }), - .root_source_file = b.path("test/c_abi/main.zig"), - .target = resolved_target, - .optimize = optimize_mode, - .link_libc = true, + .root_module = test_mod, .use_llvm = c_abi_target.use_llvm, .use_lld = c_abi_target.use_lld, - .pic = c_abi_target.pic, - .strip = c_abi_target.strip, }); - test_step.addCSourceFile(.{ - .file = b.path("test/c_abi/cfuncs.c"), - .flags = &.{"-std=c99"}, - }); - for (c_abi_target.c_defines) |define| test_step.root_module.addCMacro(define, "1"); // This test is intentionally trying to check if the external ABI is // done properly. LTO would be a hindrance to this. @@ -1784,9 +1797,11 @@ pub fn addDebuggerTests(b: *std.Build, options: DebuggerContext.Options) ?*Step pub fn addIncrementalTests(b: *std.Build, test_step: *Step) !void { const incr_check = b.addExecutable(.{ .name = "incr-check", - .root_source_file = b.path("tools/incr-check.zig"), - .target = b.graph.host, - .optimize = .Debug, + .root_module = b.createModule(.{ + .root_source_file = b.path("tools/incr-check.zig"), + .target = b.graph.host, + .optimize = .Debug, + }), }); var dir = try b.build_root.handle.openDir("test/incremental", .{ .iterate = true }); -- 2.54.0 From 6d4e2a9171a2616f6a744f8f842fb4042b868fc8 Mon Sep 17 00:00:00 2001 From: mlugg Date: Mon, 16 Dec 2024 15:18:07 +0000 Subject: [PATCH 09/16] test-cases: migrate from deprecated std.Build APIs --- test/src/Cases.zig | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/test/src/Cases.zig b/test/src/Cases.zig index 208e8a6ce17d55a52b5eee8244c83e0d35f04431..b821aad447dd99a4337b873ffb88a69ebdfb3b21 100644 --- a/test/src/Cases.zig +++ b/test/src/Cases.zig @@ -660,34 +660,37 @@ pub fn lowerToBuildSteps( file_sources.put(file.path, writefiles.add(file.path, file.src)) catch @panic("OOM"); } - const artifact = if (case.is_test) b.addTest(.{ + const mod = b.createModule(.{ .root_source_file = root_source_file, - .name = case.name, .target = case.target, .optimize = case.optimize_mode, + }); + if (case.link_libc) mod.link_libc = true; + if (case.pic) |pic| mod.pic = pic; + for (case.deps.items) |dep| { + mod.addAnonymousImport(dep.name, .{ + .root_source_file = file_sources.get(dep.path).?, + }); + } + + const artifact = if (case.is_test) b.addTest(.{ + .name = case.name, + .root_module = mod, }) else switch (case.output_mode) { .Obj => b.addObject(.{ - .root_source_file = root_source_file, .name = case.name, - .target = case.target, - .optimize = case.optimize_mode, + .root_module = mod, }), .Lib => b.addStaticLibrary(.{ - .root_source_file = root_source_file, .name = case.name, - .target = case.target, - .optimize = case.optimize_mode, + .root_module = mod, }), .Exe => b.addExecutable(.{ - .root_source_file = root_source_file, .name = case.name, - .target = case.target, - .optimize = case.optimize_mode, + .root_module = mod, }), }; - if (case.link_libc) artifact.linkLibC(); - if (case.pic) |pic| artifact.root_module.pic = pic; if (case.pie) |pie| artifact.pie = pie; switch (case.backend) { @@ -701,12 +704,6 @@ pub fn lowerToBuildSteps( }, } - for (case.deps.items) |dep| { - artifact.root_module.addAnonymousImport(dep.name, .{ - .root_source_file = file_sources.get(dep.path).?, - }); - } - switch (update.case) { .Compile => { // Force the binary to be emitted if requested. -- 2.54.0 From 11b99339702fc91bfab912fb4ddc45995bdbfdb0 Mon Sep 17 00:00:00 2001 From: mlugg Date: Mon, 16 Dec 2024 15:20:39 +0000 Subject: [PATCH 10/16] test-stack-traces: migrate from deprecated std.Build APIs --- test/src/StackTrace.zig | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/test/src/StackTrace.zig b/test/src/StackTrace.zig index 09af0005de4bd234c486dca43c8136344611acc4..37e390c78df68d85194d2335a251500cde9e6804 100644 --- a/test/src/StackTrace.zig +++ b/test/src/StackTrace.zig @@ -44,9 +44,9 @@ fn addExpect( for (mode_config.exclude_os) |tag| if (tag == builtin.os.tag) return; const b = self.b; - const annotated_case_name = fmt.allocPrint(b.allocator, "check {s} ({s})", .{ + const annotated_case_name = b.fmt("check {s} ({s})", .{ name, @tagName(optimize_mode), - }) catch @panic("OOM"); + }); for (self.test_filters) |test_filter| { if (mem.indexOf(u8, annotated_case_name, test_filter)) |_| break; } else if (self.test_filters.len > 0) return; @@ -55,10 +55,12 @@ fn addExpect( const source_zig = write_files.add("source.zig", source); const exe = b.addExecutable(.{ .name = "test", - .root_source_file = source_zig, - .optimize = optimize_mode, - .target = b.graph.host, - .error_tracing = mode_config.error_tracing, + .root_module = b.createModule(.{ + .root_source_file = source_zig, + .optimize = optimize_mode, + .target = b.graph.host, + .error_tracing = mode_config.error_tracing, + }), }); const run = b.addRunArtifact(exe); @@ -83,5 +85,4 @@ const std = @import("std"); const builtin = @import("builtin"); const Step = std.Build.Step; const OptimizeMode = std.builtin.OptimizeMode; -const fmt = std.fmt; const mem = std.mem; -- 2.54.0 From 8c5c1de60b71b4310ae629c30e3c7b0df3575341 Mon Sep 17 00:00:00 2001 From: mlugg Date: Mon, 16 Dec 2024 15:37:10 +0000 Subject: [PATCH 11/16] test-compare-output: migrate from deprecated std.Build APIs --- test/src/CompareOutput.zig | 44 ++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/test/src/CompareOutput.zig b/test/src/CompareOutput.zig index 58c123870526a72f9d0e21bf7407cf715abbdb02..c491028333d2c4f2232efdab969f8e759c90589f 100644 --- a/test/src/CompareOutput.zig +++ b/test/src/CompareOutput.zig @@ -89,19 +89,22 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void { switch (case.special) { Special.Asm => { - const annotated_case_name = fmt.allocPrint(self.b.allocator, "run assemble-and-link {s}", .{ + const annotated_case_name = b.fmt("run assemble-and-link {s}", .{ case.name, - }) catch @panic("OOM"); + }); for (self.test_filters) |test_filter| { if (mem.indexOf(u8, annotated_case_name, test_filter)) |_| break; } else if (self.test_filters.len > 0) return; const exe = b.addExecutable(.{ .name = "test", - .target = b.graph.host, - .optimize = .Debug, + .root_module = b.createModule(.{ + .root_source_file = null, + .target = b.graph.host, + .optimize = .Debug, + }), }); - exe.addAssemblyFile(first_file); + exe.root_module.addAssemblyFile(first_file); const run = b.addRunArtifact(exe); run.setName(annotated_case_name); @@ -112,22 +115,22 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void { }, Special.None => { for (self.optimize_modes) |optimize| { - const annotated_case_name = fmt.allocPrint(self.b.allocator, "run compare-output {s} ({s})", .{ + const annotated_case_name = b.fmt("run compare-output {s} ({s})", .{ case.name, @tagName(optimize), - }) catch @panic("OOM"); + }); for (self.test_filters) |test_filter| { if (mem.indexOf(u8, annotated_case_name, test_filter)) |_| break; } else if (self.test_filters.len > 0) return; const exe = b.addExecutable(.{ .name = "test", - .root_source_file = first_file, - .optimize = optimize, - .target = b.graph.host, + .root_module = b.createModule(.{ + .root_source_file = first_file, + .optimize = optimize, + .target = b.graph.host, + }), }); - if (case.link_libc) { - exe.linkSystemLibrary("c"); - } + if (case.link_libc) exe.root_module.link_libc = true; const run = b.addRunArtifact(exe); run.setName(annotated_case_name); @@ -140,20 +143,20 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void { Special.RuntimeSafety => { // TODO iterate over self.optimize_modes and test this in both // debug and release safe mode - const annotated_case_name = fmt.allocPrint(self.b.allocator, "run safety {s}", .{case.name}) catch @panic("OOM"); + const annotated_case_name = b.fmt("run safety {s}", .{case.name}); for (self.test_filters) |test_filter| { if (mem.indexOf(u8, annotated_case_name, test_filter)) |_| break; } else if (self.test_filters.len > 0) return; const exe = b.addExecutable(.{ .name = "test", - .root_source_file = first_file, - .target = b.graph.host, - .optimize = .Debug, + .root_module = b.createModule(.{ + .root_source_file = first_file, + .target = b.graph.host, + .optimize = .Debug, + }), }); - if (case.link_libc) { - exe.linkSystemLibrary("c"); - } + if (case.link_libc) exe.root_module.link_libc = true; const run = b.addRunArtifact(exe); run.setName(annotated_case_name); @@ -168,7 +171,6 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void { const CompareOutput = @This(); const std = @import("std"); const ArrayList = std.ArrayList; -const fmt = std.fmt; const mem = std.mem; const fs = std.fs; const OptimizeMode = std.builtin.OptimizeMode; -- 2.54.0 From 1cfbfc1d3429d33e3c02f6f68b4cc94370d95db5 Mon Sep 17 00:00:00 2001 From: mlugg Date: Mon, 16 Dec 2024 15:45:29 +0000 Subject: [PATCH 12/16] test-run-translated-c: migrate from deprecated std.Build APIs --- test/src/Cases.zig | 5 ++++- test/src/RunTranslatedC.zig | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/test/src/Cases.zig b/test/src/Cases.zig index b821aad447dd99a4337b873ffb88a69ebdfb3b21..360a0fda9241ec7e3d3b4622a5facdd0b3f04161 100644 --- a/test/src/Cases.zig +++ b/test/src/Cases.zig @@ -572,7 +572,10 @@ pub fn lowerToTranslateCSteps( }); translate_c.step.name = b.fmt("{s} translate-c", .{annotated_case_name}); - const run_exe = translate_c.addExecutable(.{}); + const run_exe = b.addExecutable(.{ + .name = "translated_c", + .root_module = translate_c.createModule(), + }); run_exe.step.name = b.fmt("{s} build-exe", .{annotated_case_name}); run_exe.linkLibC(); const run = b.addRunArtifact(run_exe); diff --git a/test/src/RunTranslatedC.zig b/test/src/RunTranslatedC.zig index 59f87c6a8298d536e63a71b0ebef5afcb7fb944d..528df69c4b72d9a92d2af7a37764ed0267bd49dc 100644 --- a/test/src/RunTranslatedC.zig +++ b/test/src/RunTranslatedC.zig @@ -84,7 +84,10 @@ pub fn addCase(self: *RunTranslatedCContext, case: *const TestCase) void { }); translate_c.step.name = b.fmt("{s} translate-c", .{annotated_case_name}); - const exe = translate_c.addExecutable(.{}); + const exe = b.addExecutable(.{ + .name = "translated_c", + .root_module = translate_c.createModule(), + }); exe.step.name = b.fmt("{s} build-exe", .{annotated_case_name}); exe.linkLibC(); const run = b.addRunArtifact(exe); -- 2.54.0 From 6179d9ef55b071677100af27eeb18582c15c149f Mon Sep 17 00:00:00 2001 From: mlugg Date: Mon, 16 Dec 2024 15:47:08 +0000 Subject: [PATCH 13/16] test-debugger: migrate from deprecated std.Build APIs --- test/src/Debugger.zig | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/test/src/Debugger.zig b/test/src/Debugger.zig index d6fae4f2c9395e6b4e9ef55bf9b36a111e734040..e5fd8b20b3b0b3789db541b44a0a963965aba973 100644 --- a/test/src/Debugger.zig +++ b/test/src/Debugger.zig @@ -2414,8 +2414,8 @@ fn addTest( } else return; } const files_wf = db.b.addWriteFiles(); - const exe = db.b.addExecutable(.{ - .name = name, + + const mod = db.b.createModule(.{ .target = target.resolved, .root_source_file = files_wf.add(files[0].path, files[0].source), .optimize = target.optimize_mode, @@ -2423,15 +2423,21 @@ fn addTest( .single_threaded = target.single_threaded, .pic = target.pic, .strip = false, - .use_llvm = false, - .use_lld = false, }); for (files[1..]) |file| { const path = files_wf.add(file.path, file.source); - if (file.import) |import| exe.root_module.addImport(import, db.b.createModule(.{ + if (file.import) |import| mod.addImport(import, db.b.createModule(.{ .root_source_file = path, })); } + + const exe = db.b.addExecutable(.{ + .name = name, + .root_module = mod, + .use_llvm = false, + .use_lld = false, + }); + const commands_wf = db.b.addWriteFiles(); const run = std.Build.Step.Run.create(db.b, db.b.fmt("run {s} {s}", .{ name, target.test_name_suffix })); run.addArgs(db_argv1); -- 2.54.0 From 82659c4594d12e5b80d2684320e15ce93eedb966 Mon Sep 17 00:00:00 2001 From: mlugg Date: Mon, 16 Dec 2024 16:47:17 +0000 Subject: [PATCH 14/16] test-link: migrate from deprecated std.Build APIs --- test/link/bss/build.zig | 8 +- test/link/common_symbols/build.zig | 17 ++-- test/link/common_symbols_alignment/build.zig | 18 ++-- test/link/glibc_compat/build.zig | 30 ++++--- .../interdependent_static_c_libs/build.zig | 35 +++++--- test/link/link.zig | 90 ++++++++++--------- .../static_libs_from_object_files/build.zig | 85 ++++++++++-------- test/link/wasm/archive/build.zig | 10 ++- test/link/wasm/basic-features/build.zig | 16 ++-- test/link/wasm/bss/build.zig | 20 +++-- test/link/wasm/export-data/build.zig | 8 +- test/link/wasm/export/build.zig | 24 +++-- test/link/wasm/extern-mangle/build.zig | 8 +- test/link/wasm/extern/build.zig | 8 +- test/link/wasm/function-table/build.zig | 24 +++-- test/link/wasm/infer-features/build.zig | 31 ++++--- test/link/wasm/producers/build.zig | 10 ++- test/link/wasm/segments/build.zig | 10 ++- test/link/wasm/shared-memory/build.zig | 20 +++-- test/link/wasm/stack_pointer/build.zig | 10 ++- test/link/wasm/type/build.zig | 10 ++- 21 files changed, 288 insertions(+), 204 deletions(-) diff --git a/test/link/bss/build.zig b/test/link/bss/build.zig index 27fd016ea17f3ee1abc4f7321ca2562d4770fb5f..128ec04caa0bcce08e1fb312653bc6254b20ab39 100644 --- a/test/link/bss/build.zig +++ b/test/link/bss/build.zig @@ -6,9 +6,11 @@ pub fn build(b: *std.Build) void { const exe = b.addExecutable(.{ .name = "bss", - .root_source_file = b.path("main.zig"), - .target = b.graph.host, - .optimize = .Debug, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .target = b.graph.host, + .optimize = .Debug, + }), }); const run = b.addRunArtifact(exe); diff --git a/test/link/common_symbols/build.zig b/test/link/common_symbols/build.zig index 3f0e4a19f0f27227a0d50989d1b2e83b159fa3fb..d4826956a58d63ce4a12ca1304d3880634406081 100644 --- a/test/link/common_symbols/build.zig +++ b/test/link/common_symbols/build.zig @@ -13,19 +13,24 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const lib_a = b.addStaticLibrary(.{ .name = "a", - .optimize = optimize, - .target = b.graph.host, + .root_module = b.createModule(.{ + .root_source_file = null, + .optimize = optimize, + .target = b.graph.host, + }), }); - lib_a.addCSourceFiles(.{ + lib_a.root_module.addCSourceFiles(.{ .files = &.{ "c.c", "a.c", "b.c" }, .flags = &.{"-fcommon"}, }); const test_exe = b.addTest(.{ - .root_source_file = b.path("main.zig"), - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = optimize, + }), }); - test_exe.linkLibrary(lib_a); + test_exe.root_module.linkLibrary(lib_a); test_step.dependOn(&b.addRunArtifact(test_exe).step); } diff --git a/test/link/common_symbols_alignment/build.zig b/test/link/common_symbols_alignment/build.zig index 384d60b90dcec264b4fe0723c306af0c194e74aa..6f0a0efd8024b15b45ea914420d309ffadf99cc5 100644 --- a/test/link/common_symbols_alignment/build.zig +++ b/test/link/common_symbols_alignment/build.zig @@ -13,19 +13,25 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const lib_a = b.addStaticLibrary(.{ .name = "a", - .optimize = optimize, - .target = b.graph.host, + .root_module = b.createModule(.{ + .root_source_file = null, + .optimize = optimize, + .target = b.graph.host, + }), }); - lib_a.addCSourceFiles(.{ + lib_a.root_module.addCSourceFiles(.{ .files = &.{"a.c"}, .flags = &.{"-fcommon"}, }); const test_exe = b.addTest(.{ - .root_source_file = b.path("main.zig"), - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .target = b.graph.host, + .optimize = optimize, + }), }); - test_exe.linkLibrary(lib_a); + test_exe.root_module.linkLibrary(lib_a); test_step.dependOn(&b.addRunArtifact(test_exe).step); } diff --git a/test/link/glibc_compat/build.zig b/test/link/glibc_compat/build.zig index e9d7ee412e8e2d3c81176f508d27004545ae793e..972a7a6ae9f04c8d12798f0f00a7321b6d75ade4 100644 --- a/test/link/glibc_compat/build.zig +++ b/test/link/glibc_compat/build.zig @@ -14,12 +14,15 @@ pub fn build(b: *std.Build) void { for ([_][]const u8{ "aarch64-linux-gnu.2.27", "aarch64-linux-gnu.2.34" }) |t| { const exe = b.addExecutable(.{ .name = t, - .target = b.resolveTargetQuery(std.Target.Query.parse( - .{ .arch_os_abi = t }, - ) catch unreachable), + .root_module = b.createModule(.{ + .root_source_file = null, + .target = b.resolveTargetQuery(std.Target.Query.parse( + .{ .arch_os_abi = t }, + ) catch unreachable), + .link_libc = true, + }), }); - exe.addCSourceFile(.{ .file = b.path("main.c") }); - exe.linkLibC(); + exe.root_module.addCSourceFile(.{ .file = b.path("main.c") }); // TODO: actually test the output _ = exe.getEmittedBin(); test_step.dependOn(&exe.step); @@ -53,10 +56,13 @@ pub fn build(b: *std.Build) void { const exe = b.addExecutable(.{ .name = t, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = null, + .target = target, + .link_libc = true, + }), }); - exe.addCSourceFile(.{ .file = b.path("glibc_runtime_check.c") }); - exe.linkLibC(); + exe.root_module.addCSourceFile(.{ .file = b.path("glibc_runtime_check.c") }); // Only try running the test if the host glibc is known to be good enough. Ideally, the Zig // 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 { const exe = b.addExecutable(.{ .name = t, - .root_source_file = b.path("glibc_runtime_check.zig"), - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("glibc_runtime_check.zig"), + .target = target, + .link_libc = true, + }), }); - exe.linkLibC(); // Only try running the test if the host glibc is known to be good enough. Ideally, the Zig // test runner would be able to check this, but see https://github.com/ziglang/zig/pull/17702#issuecomment-1831310453 diff --git a/test/link/interdependent_static_c_libs/build.zig b/test/link/interdependent_static_c_libs/build.zig index 8a700b74ffca9a6aa810fe2f26b7066b15c5b785..b18371fdcc05cc56399293ae2761889fdebe5261 100644 --- a/test/link/interdependent_static_c_libs/build.zig +++ b/test/link/interdependent_static_c_libs/build.zig @@ -13,27 +13,36 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const lib_a = b.addStaticLibrary(.{ .name = "a", - .optimize = optimize, - .target = b.graph.host, + .root_module = b.createModule(.{ + .root_source_file = null, + .optimize = optimize, + .target = b.graph.host, + }), }); - lib_a.addCSourceFile(.{ .file = b.path("a.c"), .flags = &[_][]const u8{} }); - lib_a.addIncludePath(b.path(".")); + lib_a.root_module.addCSourceFile(.{ .file = b.path("a.c"), .flags = &[_][]const u8{} }); + lib_a.root_module.addIncludePath(b.path(".")); const lib_b = b.addStaticLibrary(.{ .name = "b", - .optimize = optimize, - .target = b.graph.host, + .root_module = b.createModule(.{ + .root_source_file = null, + .optimize = optimize, + .target = b.graph.host, + }), }); - lib_b.addCSourceFile(.{ .file = b.path("b.c"), .flags = &[_][]const u8{} }); - lib_b.addIncludePath(b.path(".")); + lib_b.root_module.addCSourceFile(.{ .file = b.path("b.c"), .flags = &[_][]const u8{} }); + lib_b.root_module.addIncludePath(b.path(".")); const test_exe = b.addTest(.{ - .root_source_file = b.path("main.zig"), - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .target = b.graph.host, + .optimize = optimize, + }), }); - test_exe.linkLibrary(lib_a); - test_exe.linkLibrary(lib_b); - test_exe.addIncludePath(b.path(".")); + test_exe.root_module.linkLibrary(lib_a); + test_exe.root_module.linkLibrary(lib_b); + test_exe.root_module.addIncludePath(b.path(".")); test_step.dependOn(&b.addRunArtifact(test_exe).step); } diff --git a/test/link/link.zig b/test/link/link.zig index 6885aa93214172b4cdce88176e0a5ad842c0cebf..960d8fd7430f37f7a1285ccd8957cb3fc00a7375 100644 --- a/test/link/link.zig +++ b/test/link/link.zig @@ -47,81 +47,85 @@ const OverlayOptions = struct { }; pub fn addExecutable(b: *std.Build, base: Options, overlay: OverlayOptions) *Compile { - return addCompileStep(b, base, overlay, .exe); + return b.addExecutable(.{ + .name = overlay.name, + .root_module = createModule(b, base, overlay), + .use_llvm = base.use_llvm, + .use_lld = base.use_lld, + }); } pub fn addObject(b: *Build, base: Options, overlay: OverlayOptions) *Compile { - return addCompileStep(b, base, overlay, .obj); + return b.addObject(.{ + .name = overlay.name, + .root_module = createModule(b, base, overlay), + .use_llvm = base.use_llvm, + .use_lld = base.use_lld, + }); } pub fn addStaticLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Compile { - return addCompileStep(b, base, overlay, .static_lib); + return b.addStaticLibrary(.{ + .name = overlay.name, + .root_module = createModule(b, base, overlay), + .use_llvm = base.use_llvm, + .use_lld = base.use_lld, + }); } pub fn addSharedLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Compile { - return addCompileStep(b, base, overlay, .shared_lib); -} - -fn addCompileStep( - b: *Build, - base: Options, - overlay: OverlayOptions, - kind: enum { exe, obj, shared_lib, static_lib }, -) *Compile { - const compile_step = Compile.create(b, .{ + return b.addSharedLibrary(.{ .name = overlay.name, - .root_module = b.createModule(.{ - .target = base.target, - .optimize = base.optimize, - .root_source_file = rsf: { - const bytes = overlay.zig_source_bytes orelse break :rsf null; - const name = b.fmt("{s}.zig", .{overlay.name}); - break :rsf b.addWriteFiles().add(name, bytes); - }, - .pic = overlay.pic, - .strip = if (base.strip) |s| s else overlay.strip, - }), + .root_module = createModule(b, base, overlay), .use_llvm = base.use_llvm, .use_lld = base.use_lld, - .kind = switch (kind) { - .exe => .exe, - .obj => .obj, - .shared_lib, .static_lib => .lib, - }, - .linkage = switch (kind) { - .exe, .obj => null, - .shared_lib => .dynamic, - .static_lib => .static, + }); +} + +fn createModule(b: *Build, base: Options, overlay: OverlayOptions) *Build.Module { + const write_files = b.addWriteFiles(); + + const mod = b.createModule(.{ + .target = base.target, + .optimize = base.optimize, + .root_source_file = rsf: { + const bytes = overlay.zig_source_bytes orelse break :rsf null; + const name = b.fmt("{s}.zig", .{overlay.name}); + break :rsf write_files.add(name, bytes); }, + .pic = overlay.pic, + .strip = if (base.strip) |s| s else overlay.strip, }); + if (overlay.objcpp_source_bytes) |bytes| { - compile_step.addCSourceFile(.{ - .file = b.addWriteFiles().add("a.mm", bytes), + mod.addCSourceFile(.{ + .file = write_files.add("a.mm", bytes), .flags = overlay.objcpp_source_flags, }); } if (overlay.objc_source_bytes) |bytes| { - compile_step.addCSourceFile(.{ - .file = b.addWriteFiles().add("a.m", bytes), + mod.addCSourceFile(.{ + .file = write_files.add("a.m", bytes), .flags = overlay.objc_source_flags, }); } if (overlay.cpp_source_bytes) |bytes| { - compile_step.addCSourceFile(.{ - .file = b.addWriteFiles().add("a.cpp", bytes), + mod.addCSourceFile(.{ + .file = write_files.add("a.cpp", bytes), .flags = overlay.cpp_source_flags, }); } if (overlay.c_source_bytes) |bytes| { - compile_step.addCSourceFile(.{ - .file = b.addWriteFiles().add("a.c", bytes), + mod.addCSourceFile(.{ + .file = write_files.add("a.c", bytes), .flags = overlay.c_source_flags, }); } if (overlay.asm_source_bytes) |bytes| { - compile_step.addAssemblyFile(b.addWriteFiles().add("a.s", bytes)); + mod.addAssemblyFile(write_files.add("a.s", bytes)); } - return compile_step; + + return mod; } pub fn addRunArtifact(comp: *Compile) *Run { diff --git a/test/link/static_libs_from_object_files/build.zig b/test/link/static_libs_from_object_files/build.zig index 8e4a6151beb4b628763d92367e2f05a3342669ff..461f26a37471473d6b61cf197e8c058138756398 100644 --- a/test/link/static_libs_from_object_files/build.zig +++ b/test/link/static_libs_from_object_files/build.zig @@ -57,13 +57,15 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built { const exe = b.addExecutable(.{ .name = "test1", - .root_source_file = b.path("main.zig"), - .optimize = optimize, - .target = b.graph.host, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = optimize, + .target = b.graph.host, + }), }); for (files) |file| { - exe.addCSourceFile(.{ .file = file, .flags = &flags }); + exe.root_module.addCSourceFile(.{ .file = file, .flags = &flags }); } const run_cmd = b.addRunArtifact(exe); @@ -75,30 +77,33 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built // using static librairies { + const mod_a = b.createModule(.{ .target = b.graph.host, .optimize = optimize }); + const mod_b = b.createModule(.{ .target = b.graph.host, .optimize = optimize }); + + for (files, 1..) |file, i| { + const mod = if (i & 1 == 0) mod_a else mod_b; + mod.addCSourceFile(.{ .file = file, .flags = &flags }); + } + const lib_a = b.addStaticLibrary(.{ .name = "test2_a", - .target = b.graph.host, - .optimize = optimize, + .root_module = mod_a, }); const lib_b = b.addStaticLibrary(.{ .name = "test2_b", - .target = b.graph.host, - .optimize = optimize, + .root_module = mod_b, }); - for (files, 1..) |file, i| { - const lib = if (i & 1 == 0) lib_a else lib_b; - lib.addCSourceFile(.{ .file = file, .flags = &flags }); - } - const exe = b.addExecutable(.{ .name = "test2", - .root_source_file = b.path("main.zig"), - .target = b.graph.host, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .target = b.graph.host, + .optimize = optimize, + }), }); - exe.linkLibrary(lib_a); - exe.linkLibrary(lib_b); + exe.root_module.linkLibrary(lib_a); + exe.root_module.linkLibrary(lib_b); const run_cmd = b.addRunArtifact(exe); run_cmd.skip_foreign_checks = true; @@ -109,37 +114,41 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built // using static librairies and object files { - const lib_a = b.addStaticLibrary(.{ - .name = "test3_a", - .target = b.graph.host, - .optimize = optimize, - }); - const lib_b = b.addStaticLibrary(.{ - .name = "test3_b", - .target = b.graph.host, - .optimize = optimize, - }); + const mod_a = b.createModule(.{ .target = b.graph.host, .optimize = optimize }); + const mod_b = b.createModule(.{ .target = b.graph.host, .optimize = optimize }); for (files, 1..) |file, i| { + const obj_mod = b.createModule(.{ .target = b.graph.host, .optimize = optimize }); + obj_mod.addCSourceFile(.{ .file = file, .flags = &flags }); + const obj = b.addObject(.{ .name = b.fmt("obj_{}", .{i}), - .target = b.graph.host, - .optimize = optimize, + .root_module = obj_mod, }); - obj.addCSourceFile(.{ .file = file, .flags = &flags }); - const lib = if (i & 1 == 0) lib_a else lib_b; - lib.addObject(obj); + const lib_mod = if (i & 1 == 0) mod_a else mod_b; + lib_mod.addObject(obj); } + const lib_a = b.addStaticLibrary(.{ + .name = "test3_a", + .root_module = mod_a, + }); + const lib_b = b.addStaticLibrary(.{ + .name = "test3_b", + .root_module = mod_b, + }); + const exe = b.addExecutable(.{ .name = "test3", - .root_source_file = b.path("main.zig"), - .target = b.graph.host, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .target = b.graph.host, + .optimize = optimize, + }), }); - exe.linkLibrary(lib_a); - exe.linkLibrary(lib_b); + exe.root_module.linkLibrary(lib_a); + exe.root_module.linkLibrary(lib_b); const run_cmd = b.addRunArtifact(exe); run_cmd.skip_foreign_checks = true; diff --git a/test/link/wasm/archive/build.zig b/test/link/wasm/archive/build.zig index 34c5818ad88b244d4ca9d851bbab2c958a112f9b..e57eb25fc6636abc9ea2e380199d1e653204e584 100644 --- a/test/link/wasm/archive/build.zig +++ b/test/link/wasm/archive/build.zig @@ -17,10 +17,12 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize // and therefore link with its archive file. const lib = b.addExecutable(.{ .name = "main", - .root_source_file = b.path("main.zig"), - .optimize = optimize, - .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), - .strip = false, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = optimize, + .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), + .strip = false, + }), }); lib.entry = .disabled; lib.use_llvm = false; diff --git a/test/link/wasm/basic-features/build.zig b/test/link/wasm/basic-features/build.zig index 87355a5c12880fd7c9f92aa0f6a9d81c215bf877..1eaeeb5196aef5bcc8592c9cc5a16e3bda8a50ba 100644 --- a/test/link/wasm/basic-features/build.zig +++ b/test/link/wasm/basic-features/build.zig @@ -6,13 +6,15 @@ pub fn build(b: *std.Build) void { // Library with explicitly set cpu features const lib = b.addExecutable(.{ .name = "lib", - .root_source_file = b.path("main.zig"), - .optimize = .Debug, - .target = b.resolveTargetQuery(.{ - .cpu_arch = .wasm32, - .cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp }, - .cpu_features_add = std.Target.wasm.featureSet(&.{.atomics}), - .os_tag = .freestanding, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = .Debug, + .target = b.resolveTargetQuery(.{ + .cpu_arch = .wasm32, + .cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp }, + .cpu_features_add = std.Target.wasm.featureSet(&.{.atomics}), + .os_tag = .freestanding, + }), }), }); lib.entry = .disabled; diff --git a/test/link/wasm/bss/build.zig b/test/link/wasm/bss/build.zig index dc7d1bae4b9a78a975b4402e249f49a5f6d0d5c9..d73c7ed757848b84e792904e4272b2b46927214b 100644 --- a/test/link/wasm/bss/build.zig +++ b/test/link/wasm/bss/build.zig @@ -16,10 +16,12 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt { const lib = b.addExecutable(.{ .name = "lib", - .root_source_file = b.path("lib.zig"), - .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), - .optimize = optimize_mode, - .strip = false, + .root_module = b.createModule(.{ + .root_source_file = b.path("lib.zig"), + .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), + .optimize = optimize_mode, + .strip = false, + }), }); lib.entry = .disabled; lib.use_llvm = false; @@ -64,10 +66,12 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt { const lib = b.addExecutable(.{ .name = "lib", - .root_source_file = b.path("lib2.zig"), - .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), - .optimize = optimize_mode, - .strip = false, + .root_module = b.createModule(.{ + .root_source_file = b.path("lib2.zig"), + .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), + .optimize = optimize_mode, + .strip = false, + }), }); lib.entry = .disabled; lib.use_llvm = false; diff --git a/test/link/wasm/export-data/build.zig b/test/link/wasm/export-data/build.zig index 7e3c5e5841396f20d51440fa25fa50733bae169c..726d6caba5531976c12caecd475713ca628cc0ad 100644 --- a/test/link/wasm/export-data/build.zig +++ b/test/link/wasm/export-data/build.zig @@ -11,9 +11,11 @@ pub fn build(b: *std.Build) void { const lib = b.addExecutable(.{ .name = "lib", - .root_source_file = b.path("lib.zig"), - .optimize = .ReleaseSafe, // to make the output deterministic in address positions - .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), + .root_module = b.createModule(.{ + .root_source_file = b.path("lib.zig"), + .optimize = .ReleaseSafe, // to make the output deterministic in address positions + .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), + }), }); lib.entry = .disabled; lib.use_lld = false; diff --git a/test/link/wasm/export/build.zig b/test/link/wasm/export/build.zig index 368826843a6c3caf6a3d5fcd86f07e3c3ff6a9f4..5c40612ca29bd795dbf3bf61deb25e450da25266 100644 --- a/test/link/wasm/export/build.zig +++ b/test/link/wasm/export/build.zig @@ -15,9 +15,11 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const no_export = b.addExecutable(.{ .name = "no-export", - .root_source_file = b.path("main.zig"), - .optimize = optimize, - .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = optimize, + .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), + }), }); no_export.entry = .disabled; no_export.use_llvm = false; @@ -25,9 +27,11 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize const dynamic_export = b.addExecutable(.{ .name = "dynamic", - .root_source_file = b.path("main.zig"), - .optimize = optimize, - .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = optimize, + .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), + }), }); dynamic_export.entry = .disabled; dynamic_export.rdynamic = true; @@ -36,9 +40,11 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize const force_export = b.addExecutable(.{ .name = "force", - .root_source_file = b.path("main.zig"), - .optimize = optimize, - .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = optimize, + .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), + }), }); force_export.entry = .disabled; force_export.root_module.export_symbol_names = &.{"foo"}; diff --git a/test/link/wasm/extern-mangle/build.zig b/test/link/wasm/extern-mangle/build.zig index 5a4cbc1cdae3eaa03c225b65145458fdefba477e..c856611e970441ca8c9c736abf8188fe3595f884 100644 --- a/test/link/wasm/extern-mangle/build.zig +++ b/test/link/wasm/extern-mangle/build.zig @@ -13,9 +13,11 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const lib = b.addExecutable(.{ .name = "lib", - .root_source_file = b.path("lib.zig"), - .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("lib.zig"), + .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), + .optimize = optimize, + }), }); lib.entry = .disabled; lib.import_symbols = true; // import `a` and `b` diff --git a/test/link/wasm/extern/build.zig b/test/link/wasm/extern/build.zig index 77dcbc47c51cc2a73fdf8b8593ddbc8e36ee1b87..236f01fd886e2471f8f2c3dbde5dbb9ab66e6791 100644 --- a/test/link/wasm/extern/build.zig +++ b/test/link/wasm/extern/build.zig @@ -15,9 +15,11 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const exe = b.addExecutable(.{ .name = "extern", - .root_source_file = b.path("main.zig"), - .optimize = optimize, - .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .wasi }), + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = optimize, + .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .wasi }), + }), }); exe.addCSourceFile(.{ .file = b.path("foo.c"), .flags = &.{} }); exe.use_llvm = false; diff --git a/test/link/wasm/function-table/build.zig b/test/link/wasm/function-table/build.zig index 3042ddf5d55e32d12af59a46675e6c1dfae70c3d..7fd306285cadf7b38626cac235a9bedef027e76e 100644 --- a/test/link/wasm/function-table/build.zig +++ b/test/link/wasm/function-table/build.zig @@ -15,9 +15,11 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const import_table = b.addExecutable(.{ .name = "import_table", - .root_source_file = b.path("lib.zig"), - .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("lib.zig"), + .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), + .optimize = optimize, + }), }); import_table.entry = .disabled; import_table.use_llvm = false; @@ -27,9 +29,11 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize const export_table = b.addExecutable(.{ .name = "export_table", - .root_source_file = b.path("lib.zig"), - .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("lib.zig"), + .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), + .optimize = optimize, + }), }); export_table.entry = .disabled; export_table.use_llvm = false; @@ -39,9 +43,11 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize const regular_table = b.addExecutable(.{ .name = "regular_table", - .root_source_file = b.path("lib.zig"), - .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("lib.zig"), + .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), + .optimize = optimize, + }), }); regular_table.entry = .disabled; regular_table.use_llvm = false; diff --git a/test/link/wasm/infer-features/build.zig b/test/link/wasm/infer-features/build.zig index af40175da05609cd4ad0791f935a01af4b86100d..83aab77841ff169b7d4cebda61ac2063124efa95 100644 --- a/test/link/wasm/infer-features/build.zig +++ b/test/link/wasm/infer-features/build.zig @@ -6,31 +6,36 @@ pub fn build(b: *std.Build) void { // Wasm Object file which we will use to infer the features from const c_obj = b.addObject(.{ .name = "c_obj", - .optimize = .Debug, - .target = b.resolveTargetQuery(.{ - .cpu_arch = .wasm32, - .cpu_model = .{ .explicit = &std.Target.wasm.cpu.bleeding_edge }, - .os_tag = .freestanding, + .root_module = b.createModule(.{ + .root_source_file = null, + .optimize = .Debug, + .target = b.resolveTargetQuery(.{ + .cpu_arch = .wasm32, + .cpu_model = .{ .explicit = &std.Target.wasm.cpu.bleeding_edge }, + .os_tag = .freestanding, + }), }), }); - c_obj.addCSourceFile(.{ .file = b.path("foo.c"), .flags = &.{} }); + c_obj.root_module.addCSourceFile(.{ .file = b.path("foo.c"), .flags = &.{} }); // Wasm library that doesn't have any features specified. This will // infer its featureset from other linked object files. const lib = b.addExecutable(.{ .name = "lib", - .root_source_file = b.path("main.zig"), - .optimize = .Debug, - .target = b.resolveTargetQuery(.{ - .cpu_arch = .wasm32, - .cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp }, - .os_tag = .freestanding, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = .Debug, + .target = b.resolveTargetQuery(.{ + .cpu_arch = .wasm32, + .cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp }, + .os_tag = .freestanding, + }), }), }); lib.entry = .disabled; lib.use_llvm = false; lib.use_lld = false; - lib.addObject(c_obj); + lib.root_module.addObject(c_obj); // Verify the result contains the features from the C Object file. const check = lib.checkObject(); diff --git a/test/link/wasm/producers/build.zig b/test/link/wasm/producers/build.zig index 2dae6e3f9bd17772c4f5aebed0060511f96eb76b..e31b07ce8b3b83563f7375d7bc2a259b64af59d0 100644 --- a/test/link/wasm/producers/build.zig +++ b/test/link/wasm/producers/build.zig @@ -16,10 +16,12 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const lib = b.addExecutable(.{ .name = "lib", - .root_source_file = b.path("lib.zig"), - .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), - .optimize = optimize, - .strip = false, + .root_module = b.createModule(.{ + .root_source_file = b.path("lib.zig"), + .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), + .optimize = optimize, + .strip = false, + }), }); lib.entry = .disabled; lib.use_llvm = false; diff --git a/test/link/wasm/segments/build.zig b/test/link/wasm/segments/build.zig index 3c8bac3f07309a7f86a9697377b3de8fb79c9a01..c2c62a3b88677553de58347e3110b26639dc4110 100644 --- a/test/link/wasm/segments/build.zig +++ b/test/link/wasm/segments/build.zig @@ -15,10 +15,12 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const lib = b.addExecutable(.{ .name = "lib", - .root_source_file = b.path("lib.zig"), - .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), - .optimize = optimize, - .strip = false, + .root_module = b.createModule(.{ + .root_source_file = b.path("lib.zig"), + .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), + .optimize = optimize, + .strip = false, + }), }); lib.entry = .disabled; lib.use_llvm = false; diff --git a/test/link/wasm/shared-memory/build.zig b/test/link/wasm/shared-memory/build.zig index 7807a95a4fc4b87a1a6ffdd9a586981a43e7cc8e..7725454f3f56e5f4c73a57bd0451864d4a07b656 100644 --- a/test/link/wasm/shared-memory/build.zig +++ b/test/link/wasm/shared-memory/build.zig @@ -13,16 +13,18 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.OptimizeMode) void { const exe = b.addExecutable(.{ .name = "lib", - .root_source_file = b.path("lib.zig"), - .target = b.resolveTargetQuery(.{ - .cpu_arch = .wasm32, - .cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp }, - .cpu_features_add = std.Target.wasm.featureSet(&.{ .atomics, .bulk_memory }), - .os_tag = .freestanding, + .root_module = b.createModule(.{ + .root_source_file = b.path("lib.zig"), + .target = b.resolveTargetQuery(.{ + .cpu_arch = .wasm32, + .cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp }, + .cpu_features_add = std.Target.wasm.featureSet(&.{ .atomics, .bulk_memory }), + .os_tag = .freestanding, + }), + .optimize = optimize_mode, + .strip = false, + .single_threaded = false, }), - .optimize = optimize_mode, - .strip = false, - .single_threaded = false, }); exe.entry = .disabled; exe.use_lld = false; diff --git a/test/link/wasm/stack_pointer/build.zig b/test/link/wasm/stack_pointer/build.zig index e42e3628800181b81ec0ba26cdf8cc1476008684..57ce5ddefdeb88ed29e9bd6a6dae80b4fa6aef61 100644 --- a/test/link/wasm/stack_pointer/build.zig +++ b/test/link/wasm/stack_pointer/build.zig @@ -15,10 +15,12 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const lib = b.addExecutable(.{ .name = "lib", - .root_source_file = b.path("lib.zig"), - .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), - .optimize = optimize, - .strip = false, + .root_module = b.createModule(.{ + .root_source_file = b.path("lib.zig"), + .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), + .optimize = optimize, + .strip = false, + }), }); lib.entry = .disabled; lib.use_llvm = false; diff --git a/test/link/wasm/type/build.zig b/test/link/wasm/type/build.zig index 46b80dbfe5dca58e8e2bd64f0f36fceaaf3ec79f..aa19d4d403933ca49c83714d8a7b06b7337cf157 100644 --- a/test/link/wasm/type/build.zig +++ b/test/link/wasm/type/build.zig @@ -15,10 +15,12 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const exe = b.addExecutable(.{ .name = "lib", - .root_source_file = b.path("lib.zig"), - .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), - .optimize = optimize, - .strip = false, + .root_module = b.createModule(.{ + .root_source_file = b.path("lib.zig"), + .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }), + .optimize = optimize, + .strip = false, + }), }); exe.entry = .disabled; exe.use_llvm = false; -- 2.54.0 From 6bd590ad37dcb7e46c8eb78c54e69ad07e5b565e Mon Sep 17 00:00:00 2001 From: mlugg Date: Mon, 16 Dec 2024 22:26:34 +0000 Subject: [PATCH 15/16] test-standalone: migrate from deprecated std.Build APIs --- test/standalone/build.zig | 8 ++- test/standalone/c_compiler/build.zig | 19 +++--- test/standalone/child_process/build.zig | 16 +++-- test/standalone/coff_dwarf/build.zig | 21 +++--- test/standalone/compiler_rt_panic/build.zig | 11 ++-- test/standalone/dep_diamond/build.zig | 26 ++++---- .../standalone/dep_duplicate_module/build.zig | 29 +++++---- test/standalone/dep_lazypath/build.zig | 19 ++++-- .../dep_mutually_recursive/build.zig | 20 +++--- test/standalone/dep_recursive/build.zig | 20 +++--- test/standalone/dep_shared_builtin/build.zig | 12 +++- test/standalone/dep_triangle/build.zig | 23 ++++--- test/standalone/depend_on_main_mod/build.zig | 15 +++-- test/standalone/dirname/build.zig | 24 ++++--- .../standalone/embed_generated_file/build.zig | 17 +++-- test/standalone/emit_asm_and_bin/build.zig | 5 +- test/standalone/emit_asm_no_bin/build.zig | 8 ++- test/standalone/emit_llvm_no_bin/build.zig | 8 ++- test/standalone/empty_env/build.zig | 8 ++- test/standalone/extern/build.zig | 24 ++++--- test/standalone/global_linkage/build.zig | 24 ++++--- test/standalone/install_headers/build.zig | 42 +++++++----- test/standalone/install_raw_hex/build.zig | 8 ++- test/standalone/ios/build.zig | 21 +++--- test/standalone/issue_11595/build.zig | 12 ++-- test/standalone/issue_12706/build.zig | 12 ++-- test/standalone/issue_13970/build.zig | 15 ++++- test/standalone/issue_339/build.zig | 8 ++- test/standalone/issue_5825/build.zig | 21 +++--- test/standalone/issue_794/build.zig | 5 +- test/standalone/issue_8550/build.zig | 10 +-- test/standalone/libcxx/build.zig | 26 ++++---- .../standalone/load_dynamic_library/build.zig | 16 +++-- test/standalone/mix_c_files/build.zig | 12 ++-- test/standalone/mix_o_files/build.zig | 21 +++--- test/standalone/options/build.zig | 4 +- test/standalone/pie/build.zig | 8 ++- test/standalone/pkg_import/build.zig | 8 ++- test/standalone/run_output_caching/build.zig | 8 ++- test/standalone/run_output_paths/build.zig | 8 ++- test/standalone/self_exe_symlink/build.zig | 16 +++-- test/standalone/shared_library/build.zig | 21 +++--- test/standalone/simple/build.zig | 20 +++--- test/standalone/stack_iterator/build.zig | 65 +++++++++++-------- test/standalone/static_c_lib/build.zig | 20 +++--- test/standalone/strip_empty_loop/build.zig | 10 +-- test/standalone/strip_struct_init/build.zig | 9 ++- .../test_runner_module_imports/build.zig | 18 ++--- test/standalone/test_runner_path/build.zig | 5 +- test/standalone/use_alias/build.zig | 7 +- test/standalone/windows_argv/build.zig | 58 ++++++++++------- test/standalone/windows_bat_args/build.zig | 24 ++++--- .../standalone/windows_entry_points/build.zig | 44 ++++++++----- test/standalone/windows_resources/build.zig | 10 +-- test/standalone/windows_spawn/build.zig | 16 +++-- test/standalone/zerolength_check/build.zig | 4 +- 56 files changed, 592 insertions(+), 377 deletions(-) diff --git a/test/standalone/build.zig b/test/standalone/build.zig index fee8c031619fdb26305b8e53395e1a3104ebe316..f89ccd2a18284efd534873224b5e6b3bd434bb1a 100644 --- a/test/standalone/build.zig +++ b/test/standalone/build.zig @@ -47,9 +47,11 @@ pub fn build(b: *std.Build) void { }) |tool_src_path| { const tool = b.addTest(.{ .name = std.fs.path.stem(tool_src_path), - .root_source_file = b.path(tool_src_path), - .optimize = .Debug, - .target = tools_target, + .root_module = b.createModule(.{ + .root_source_file = b.path(tool_src_path), + .optimize = .Debug, + .target = tools_target, + }), }); const run = b.addRunArtifact(tool); tools_tests_step.dependOn(&run.step); diff --git a/test/standalone/c_compiler/build.zig b/test/standalone/c_compiler/build.zig index 5328d6e01d0e49e98bda6dc4c1b74cec1ad3500e..af9d8b492a3b081e9444524b2ff44a40ab402ab1 100644 --- a/test/standalone/c_compiler/build.zig +++ b/test/standalone/c_compiler/build.zig @@ -20,22 +20,25 @@ fn add( ) void { const target = b.graph.host; - const exe_c = b.addExecutable(.{ - .name = c_name, + const c_mod = b.createModule(.{ .optimize = optimize, .target = target, }); - exe_c.addCSourceFile(.{ .file = b.path("test.c"), .flags = &[0][]const u8{} }); - exe_c.linkLibC(); + c_mod.addCSourceFile(.{ .file = b.path("test.c"), .flags = &[0][]const u8{} }); + c_mod.link_libc = true; - const exe_cpp = b.addExecutable(.{ - .name = cpp_name, + const exe_c = b.addExecutable(.{ .name = c_name, .root_module = c_mod }); + + const cpp_mod = b.createModule(.{ .optimize = optimize, .target = target, }); + cpp_mod.addCSourceFile(.{ .file = b.path("test.cpp"), .flags = &[0][]const u8{} }); + cpp_mod.link_libcpp = true; + + const exe_cpp = b.addExecutable(.{ .name = cpp_name, .root_module = cpp_mod }); + b.default_step.dependOn(&exe_cpp.step); - exe_cpp.addCSourceFile(.{ .file = b.path("test.cpp"), .flags = &[0][]const u8{} }); - exe_cpp.linkLibCpp(); switch (target.result.os.tag) { .windows => { diff --git a/test/standalone/child_process/build.zig b/test/standalone/child_process/build.zig index 0180fb8a83d62cda8a0fdfd039bc494f176e2b18..81b40835f3d7950356a41ec3a723e9913e553745 100644 --- a/test/standalone/child_process/build.zig +++ b/test/standalone/child_process/build.zig @@ -12,16 +12,20 @@ pub fn build(b: *std.Build) void { const child = b.addExecutable(.{ .name = "child", - .root_source_file = b.path("child.zig"), - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("child.zig"), + .optimize = optimize, + .target = target, + }), }); const main = b.addExecutable(.{ .name = "main", - .root_source_file = b.path("main.zig"), - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = optimize, + .target = target, + }), }); const run = b.addRunArtifact(main); run.addArtifactArg(child); diff --git a/test/standalone/coff_dwarf/build.zig b/test/standalone/coff_dwarf/build.zig index bb9879d089ddb8f188036025efc68015ce6b63ca..b95ebed80dff67d64ec083106d2537a24cf1fb97 100644 --- a/test/standalone/coff_dwarf/build.zig +++ b/test/standalone/coff_dwarf/build.zig @@ -14,19 +14,24 @@ pub fn build(b: *std.Build) void { const exe = b.addExecutable(.{ .name = "main", - .root_source_file = b.path("main.zig"), - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = optimize, + .target = target, + }), }); const lib = b.addSharedLibrary(.{ .name = "shared_lib", - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = null, + .optimize = optimize, + .target = target, + .link_libc = true, + }), }); - lib.addCSourceFile(.{ .file = b.path("shared_lib.c"), .flags = &.{"-gdwarf"} }); - lib.linkLibC(); - exe.linkLibrary(lib); + lib.root_module.addCSourceFile(.{ .file = b.path("shared_lib.c"), .flags = &.{"-gdwarf"} }); + exe.root_module.linkLibrary(lib); const run = b.addRunArtifact(exe); run.expectExitCode(0); diff --git a/test/standalone/compiler_rt_panic/build.zig b/test/standalone/compiler_rt_panic/build.zig index 93331a028245d686d729141eeeff3d44a58715ef..e25fe0eff78347aec5ab3fed9428bbf1e8608aa5 100644 --- a/test/standalone/compiler_rt_panic/build.zig +++ b/test/standalone/compiler_rt_panic/build.zig @@ -12,11 +12,14 @@ pub fn build(b: *std.Build) void { const exe = b.addExecutable(.{ .name = "main", - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = null, + .optimize = optimize, + .target = target, + .link_libc = true, + }), }); - exe.linkLibC(); - exe.addCSourceFile(.{ + exe.root_module.addCSourceFile(.{ .file = b.path("main.c"), .flags = &.{}, }); diff --git a/test/standalone/dep_diamond/build.zig b/test/standalone/dep_diamond/build.zig index 44338b36d44568b001374b6523afc7651f428af5..ba437c310abd4411db868265be93bc2438929d83 100644 --- a/test/standalone/dep_diamond/build.zig +++ b/test/standalone/dep_diamond/build.zig @@ -6,23 +6,23 @@ pub fn build(b: *std.Build) void { const optimize: std.builtin.OptimizeMode = .Debug; - const shared = b.createModule(.{ - .root_source_file = b.path("shared.zig"), - }); - - const exe = b.addExecutable(.{ - .name = "test", + const main_mod = b.createModule(.{ .root_source_file = b.path("test.zig"), .target = b.graph.host, .optimize = optimize, }); - exe.root_module.addAnonymousImport("foo", .{ - .root_source_file = b.path("foo.zig"), - .imports = &.{.{ .name = "shared", .module = shared }}, - }); - exe.root_module.addAnonymousImport("bar", .{ - .root_source_file = b.path("bar.zig"), - .imports = &.{.{ .name = "shared", .module = shared }}, + const shared_mod = b.createModule(.{ .root_source_file = b.path("shared.zig") }); + const foo_mod = b.createModule(.{ .root_source_file = b.path("foo.zig") }); + const bar_mod = b.createModule(.{ .root_source_file = b.path("bar.zig") }); + + main_mod.addImport("foo", foo_mod); + main_mod.addImport("bar", bar_mod); + foo_mod.addImport("shared", shared_mod); + bar_mod.addImport("shared", shared_mod); + + const exe = b.addExecutable(.{ + .name = "test", + .root_module = main_mod, }); const run = b.addRunArtifact(exe); diff --git a/test/standalone/dep_duplicate_module/build.zig b/test/standalone/dep_duplicate_module/build.zig index 733d49b91c939c113d38a42bdb7cccdba84ade1a..5974ba9968ffb0406df03030c0cd9086efa67519 100644 --- a/test/standalone/dep_duplicate_module/build.zig +++ b/test/standalone/dep_duplicate_module/build.zig @@ -4,29 +4,36 @@ pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const mod = b.addModule("mod", .{ + const shared_mod = b.createModule(.{ .root_source_file = b.path("mod.zig"), .target = target, .optimize = optimize, }); + const lib_mod = b.createModule(.{ + .root_source_file = b.path("lib.zig"), + .target = target, + .optimize = optimize, + }); + const exe_mod = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .target = target, + .optimize = optimize, + }); + + lib_mod.addImport("mod", shared_mod); + exe_mod.addImport("mod", shared_mod); const lib = b.addStaticLibrary(.{ .name = "lib", - .root_source_file = b.path("lib.zig"), - .target = target, - .optimize = optimize, + .root_module = lib_mod, }); - lib.root_module.addImport("mod", mod); + + exe_mod.linkLibrary(lib); const exe = b.addExecutable(.{ .name = "app", - .root_source_file = b.path("main.zig"), - .target = target, - .optimize = optimize, + .root_module = exe_mod, }); - exe.root_module.addImport("mod", mod); - exe.root_module.linkLibrary(lib); - b.installArtifact(exe); } diff --git a/test/standalone/dep_lazypath/build.zig b/test/standalone/dep_lazypath/build.zig index f309487981c796f86ac223a2348aaa13c57806a3..01b4be8fd476a3eb8f0b7a4c91e2e6236c9a923e 100644 --- a/test/standalone/dep_lazypath/build.zig +++ b/test/standalone/dep_lazypath/build.zig @@ -11,10 +11,13 @@ pub fn build(b: *std.Build) void { const generated_main_c = write_files.add("main.c", ""); const exe = b.addExecutable(.{ .name = "test", - .target = b.graph.host, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = null, + .target = b.graph.host, + .optimize = optimize, + }), }); - exe.addCSourceFiles(.{ + exe.root_module.addCSourceFiles(.{ .root = generated_main_c.dirname(), .files = &.{"main.c"}, }); @@ -26,11 +29,13 @@ pub fn build(b: *std.Build) void { const dir = write_files.addCopyDirectory(b.path("inc"), "", .{}); const exe = b.addExecutable(.{ .name = "test", - .root_source_file = b.path("inctest.zig"), - .target = b.graph.host, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("inctest.zig"), + .target = b.graph.host, + .optimize = optimize, + }), }); - exe.addIncludePath(dir); + exe.root_module.addIncludePath(dir); b.step("copydir", "").dependOn(&exe.step); test_step.dependOn(&exe.step); } diff --git a/test/standalone/dep_mutually_recursive/build.zig b/test/standalone/dep_mutually_recursive/build.zig index 0dc739b7aff081bc71b9f8e734143e1ce8215d53..f333c1917a4798b3b09aa56732b3486e7c49008a 100644 --- a/test/standalone/dep_mutually_recursive/build.zig +++ b/test/standalone/dep_mutually_recursive/build.zig @@ -6,22 +6,26 @@ pub fn build(b: *std.Build) void { const optimize: std.builtin.OptimizeMode = .Debug; - const foo = b.createModule(.{ + const main_mod = b.createModule(.{ + .root_source_file = b.path("test.zig"), + .target = b.graph.host, + .optimize = optimize, + }); + const foo_mod = b.createModule(.{ .root_source_file = b.path("foo.zig"), }); - const bar = b.createModule(.{ + const bar_mod = b.createModule(.{ .root_source_file = b.path("bar.zig"), }); - foo.addImport("bar", bar); - bar.addImport("foo", foo); + + main_mod.addImport("foo", foo_mod); + foo_mod.addImport("bar", bar_mod); + bar_mod.addImport("foo", foo_mod); const exe = b.addExecutable(.{ .name = "test", - .root_source_file = b.path("test.zig"), - .target = b.graph.host, - .optimize = optimize, + .root_module = main_mod, }); - exe.root_module.addImport("foo", foo); const run = b.addRunArtifact(exe); test_step.dependOn(&run.step); diff --git a/test/standalone/dep_recursive/build.zig b/test/standalone/dep_recursive/build.zig index bc1b7a55bb3d4733d2fe8026cf64448d3e9a0f18..133b7706b008913897217e4038b3058b7f55ce9f 100644 --- a/test/standalone/dep_recursive/build.zig +++ b/test/standalone/dep_recursive/build.zig @@ -6,18 +6,22 @@ pub fn build(b: *std.Build) void { const optimize: std.builtin.OptimizeMode = .Debug; - const foo = b.createModule(.{ - .root_source_file = b.path("foo.zig"), - }); - foo.addImport("foo", foo); - - const exe = b.addExecutable(.{ - .name = "test", + const main_mod = b.createModule(.{ .root_source_file = b.path("test.zig"), .target = b.graph.host, .optimize = optimize, }); - exe.root_module.addImport("foo", foo); + const foo_mod = b.createModule(.{ + .root_source_file = b.path("foo.zig"), + }); + + main_mod.addImport("foo", foo_mod); + foo_mod.addImport("foo", foo_mod); + + const exe = b.addExecutable(.{ + .name = "test", + .root_module = main_mod, + }); const run = b.addRunArtifact(exe); test_step.dependOn(&run.step); diff --git a/test/standalone/dep_shared_builtin/build.zig b/test/standalone/dep_shared_builtin/build.zig index 85a58da95a4822dddb1cb11f3caec7c9da1f26a5..eb8457e4942ac29ebf22df73f810d1d987cc0192 100644 --- a/test/standalone/dep_shared_builtin/build.zig +++ b/test/standalone/dep_shared_builtin/build.zig @@ -6,16 +6,22 @@ pub fn build(b: *std.Build) void { const optimize: std.builtin.OptimizeMode = .Debug; - const exe = b.addExecutable(.{ - .name = "test", + const main_mod = b.createModule(.{ .root_source_file = b.path("test.zig"), .target = b.graph.host, .optimize = optimize, }); - exe.root_module.addAnonymousImport("foo", .{ + const foo_mod = b.createModule(.{ .root_source_file = b.path("foo.zig"), }); + main_mod.addImport("foo", foo_mod); + + const exe = b.addExecutable(.{ + .name = "test", + .root_module = main_mod, + }); + const run = b.addRunArtifact(exe); test_step.dependOn(&run.step); } diff --git a/test/standalone/dep_triangle/build.zig b/test/standalone/dep_triangle/build.zig index c8562fd7168cd47d8421ddc9491249ff5cadd875..70f64dc3347985b3964c09a1816efdac468073aa 100644 --- a/test/standalone/dep_triangle/build.zig +++ b/test/standalone/dep_triangle/build.zig @@ -6,21 +6,26 @@ pub fn build(b: *std.Build) void { const optimize: std.builtin.OptimizeMode = .Debug; - const shared = b.createModule(.{ - .root_source_file = b.path("shared.zig"), - }); - - const exe = b.addExecutable(.{ - .name = "test", + const main_mod = b.createModule(.{ .root_source_file = b.path("test.zig"), .target = b.graph.host, .optimize = optimize, }); - exe.root_module.addAnonymousImport("foo", .{ + const foo_mod = b.createModule(.{ .root_source_file = b.path("foo.zig"), - .imports = &.{.{ .name = "shared", .module = shared }}, }); - exe.root_module.addImport("shared", shared); + const shared_mod = b.createModule(.{ + .root_source_file = b.path("shared.zig"), + }); + + main_mod.addImport("foo", foo_mod); + main_mod.addImport("shared", shared_mod); + foo_mod.addImport("shared", shared_mod); + + const exe = b.addExecutable(.{ + .name = "test", + .root_module = main_mod, + }); const run = b.addRunArtifact(exe); test_step.dependOn(&run.step); diff --git a/test/standalone/depend_on_main_mod/build.zig b/test/standalone/depend_on_main_mod/build.zig index ae1dc8fb757c4a57114522cd9f5eff8cd46e5dc8..4dc08ba1bb9a04784c8016a5bb3254cda34f73aa 100644 --- a/test/standalone/depend_on_main_mod/build.zig +++ b/test/standalone/depend_on_main_mod/build.zig @@ -7,19 +7,22 @@ pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const exe = b.addExecutable(.{ - .name = "depend_on_main_mod", + const main_mod = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, }); - - const foo_module = b.addModule("foo", .{ + const foo_mod = b.createModule(.{ .root_source_file = b.path("src/foo.zig"), }); - foo_module.addImport("root2", exe.root_module); - exe.root_module.addImport("foo", foo_module); + foo_mod.addImport("root2", main_mod); + main_mod.addImport("foo", foo_mod); + + const exe = b.addExecutable(.{ + .name = "depend_on_main_mod", + .root_module = main_mod, + }); const run_cmd = b.addRunArtifact(exe); run_cmd.expectExitCode(0); diff --git a/test/standalone/dirname/build.zig b/test/standalone/dirname/build.zig index 2562c677c3dc784d477675c85e9d796c621a42ad..0da85e29236dc7dc80a496b72b699c900e3805fd 100644 --- a/test/standalone/dirname/build.zig +++ b/test/standalone/dirname/build.zig @@ -10,24 +10,30 @@ pub fn build(b: *std.Build) void { const touch = b.addExecutable(.{ .name = "touch", - .root_source_file = touch_src, - .optimize = .Debug, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = touch_src, + .optimize = .Debug, + .target = target, + }), }); const generated = b.addRunArtifact(touch).addOutputFileArg("subdir" ++ std.fs.path.sep_str ++ "generated.txt"); const exists_in = b.addExecutable(.{ .name = "exists_in", - .root_source_file = b.path("exists_in.zig"), - .optimize = .Debug, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("exists_in.zig"), + .optimize = .Debug, + .target = target, + }), }); const has_basename = b.addExecutable(.{ .name = "has_basename", - .root_source_file = b.path("has_basename.zig"), - .optimize = .Debug, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("has_basename.zig"), + .optimize = .Debug, + .target = target, + }), }); // Known path: diff --git a/test/standalone/embed_generated_file/build.zig b/test/standalone/embed_generated_file/build.zig index f7430b7716ba4a57a632fcb1c82efe6bb0cf0b7f..75670f6ba0bff84d431309a5d27b7a5249828e7b 100644 --- a/test/standalone/embed_generated_file/build.zig +++ b/test/standalone/embed_generated_file/build.zig @@ -6,18 +6,21 @@ pub fn build(b: *std.Build) void { const bootloader = b.addExecutable(.{ .name = "bootloader", - .root_source_file = b.path("bootloader.zig"), - .target = b.resolveTargetQuery(.{ - .cpu_arch = .x86, - .os_tag = .freestanding, + .root_module = b.createModule(.{ + .root_source_file = b.path("bootloader.zig"), + .target = b.resolveTargetQuery(.{ + .cpu_arch = .x86, + .os_tag = .freestanding, + }), + .optimize = .ReleaseSmall, }), - .optimize = .ReleaseSmall, }); - const exe = b.addTest(.{ + const exe = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("main.zig"), + .target = b.graph.host, .optimize = .Debug, - }); + }) }); exe.root_module.addAnonymousImport("bootloader.elf", .{ .root_source_file = bootloader.getEmittedBin(), }); diff --git a/test/standalone/emit_asm_and_bin/build.zig b/test/standalone/emit_asm_and_bin/build.zig index 51b4066e6ab6bc4a4c51b1ba8b2b6b7854ea1231..cc38a87ddc19d42a1b245e225f0834273d332ab6 100644 --- a/test/standalone/emit_asm_and_bin/build.zig +++ b/test/standalone/emit_asm_and_bin/build.zig @@ -4,10 +4,11 @@ pub fn build(b: *std.Build) void { const test_step = b.step("test", "Test it"); b.default_step = test_step; - const main = b.addTest(.{ + const main = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("main.zig"), + .target = b.graph.host, .optimize = b.standardOptimizeOption(.{}), - }); + }) }); // TODO: actually check these two artifacts for correctness _ = main.getEmittedBin(); _ = main.getEmittedAsm(); diff --git a/test/standalone/emit_asm_no_bin/build.zig b/test/standalone/emit_asm_no_bin/build.zig index f2f2391bc1794606f95a548d724a11a8d45ed61f..cd5b670146c372f3d95c747ec14338502b7d8a1e 100644 --- a/test/standalone/emit_asm_no_bin/build.zig +++ b/test/standalone/emit_asm_no_bin/build.zig @@ -8,9 +8,11 @@ pub fn build(b: *std.Build) void { const obj = b.addObject(.{ .name = "main", - .root_source_file = b.path("main.zig"), - .optimize = optimize, - .target = b.graph.host, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = optimize, + .target = b.graph.host, + }), }); _ = obj.getEmittedAsm(); b.default_step.dependOn(&obj.step); diff --git a/test/standalone/emit_llvm_no_bin/build.zig b/test/standalone/emit_llvm_no_bin/build.zig index f82dd5193043ab7048950106e2a96c95d841ae0f..04946e47b6be011553450f86d6c1e68047d8c922 100644 --- a/test/standalone/emit_llvm_no_bin/build.zig +++ b/test/standalone/emit_llvm_no_bin/build.zig @@ -8,9 +8,11 @@ pub fn build(b: *std.Build) void { const obj = b.addObject(.{ .name = "main", - .root_source_file = b.path("main.zig"), - .optimize = optimize, - .target = b.graph.host, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = optimize, + .target = b.graph.host, + }), }); _ = obj.getEmittedLlvmIr(); _ = obj.getEmittedLlvmBc(); diff --git a/test/standalone/empty_env/build.zig b/test/standalone/empty_env/build.zig index dcbb0ad052f6255fa4ea8492119033b7b5f7c5d9..592566d62ccf5541fdc8148e00cf0d96c027bc6e 100644 --- a/test/standalone/empty_env/build.zig +++ b/test/standalone/empty_env/build.zig @@ -21,9 +21,11 @@ pub fn build(b: *std.Build) void { const main = b.addExecutable(.{ .name = "main", - .root_source_file = b.path("main.zig"), - .target = b.graph.host, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .target = b.graph.host, + .optimize = optimize, + }), }); const run = b.addRunArtifact(main); diff --git a/test/standalone/extern/build.zig b/test/standalone/extern/build.zig index 1886408933e347e9f512492328d70121c736f83c..a3907e20dba8fa38bdba59bc415e1c6c3d2c0387 100644 --- a/test/standalone/extern/build.zig +++ b/test/standalone/extern/build.zig @@ -8,22 +8,28 @@ pub fn build(b: *std.Build) void { const obj = b.addObject(.{ .name = "exports", - .root_source_file = b.path("exports.zig"), - .target = b.graph.host, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("exports.zig"), + .target = b.graph.host, + .optimize = optimize, + }), }); const shared = b.addSharedLibrary(.{ .name = "shared", - .target = b.graph.host, - .optimize = optimize, - .link_libc = true, + .root_module = b.createModule(.{ + .root_source_file = null, + .target = b.graph.host, + .optimize = optimize, + .link_libc = true, + }), }); if (b.graph.host.result.abi == .msvc) shared.root_module.addCMacro("API", "__declspec(dllexport)"); - shared.addCSourceFile(.{ .file = b.path("shared.c"), .flags = &.{} }); - const test_exe = b.addTest(.{ + shared.root_module.addCSourceFile(.{ .file = b.path("shared.c"), .flags = &.{} }); + const test_exe = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("main.zig"), + .target = b.graph.host, .optimize = optimize, - }); + }) }); test_exe.addObject(obj); test_exe.linkLibrary(shared); diff --git a/test/standalone/global_linkage/build.zig b/test/standalone/global_linkage/build.zig index 50e270d1c359a4564902bf75178d4a28d84507cf..795bf008b31398364cad3b4ebe43f335e3d6c81d 100644 --- a/test/standalone/global_linkage/build.zig +++ b/test/standalone/global_linkage/build.zig @@ -9,24 +9,30 @@ pub fn build(b: *std.Build) void { const obj1 = b.addStaticLibrary(.{ .name = "obj1", - .root_source_file = b.path("obj1.zig"), - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("obj1.zig"), + .optimize = optimize, + .target = target, + }), }); const obj2 = b.addStaticLibrary(.{ .name = "obj2", - .root_source_file = b.path("obj2.zig"), - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("obj2.zig"), + .optimize = optimize, + .target = target, + }), }); - const main = b.addTest(.{ + const main_mod = b.createModule(.{ .root_source_file = b.path("main.zig"), + .target = b.graph.host, .optimize = optimize, }); - main.linkLibrary(obj1); - main.linkLibrary(obj2); + main_mod.linkLibrary(obj1); + main_mod.linkLibrary(obj2); + const main = b.addTest(.{ .root_module = main_mod }); test_step.dependOn(&b.addRunArtifact(main).step); } diff --git a/test/standalone/install_headers/build.zig b/test/standalone/install_headers/build.zig index 889ee717d685b0c13fefbd4021cb2457f3ee0b17..9f725c0c8149b03e69e6fa77d1c76ce46dfc6d27 100644 --- a/test/standalone/install_headers/build.zig +++ b/test/standalone/install_headers/build.zig @@ -8,18 +8,24 @@ pub fn build(b: *std.Build) void { const libfoo = b.addStaticLibrary(.{ .name = "foo", - .target = b.resolveTargetQuery(.{}), - .optimize = .Debug, + .root_module = b.createModule(.{ + .root_source_file = null, + .target = b.resolveTargetQuery(.{}), + .optimize = .Debug, + }), }); - libfoo.addCSourceFile(.{ .file = empty_c }); + libfoo.root_module.addCSourceFile(.{ .file = empty_c }); const exe = b.addExecutable(.{ .name = "exe", - .target = b.resolveTargetQuery(.{}), - .optimize = .Debug, - .link_libc = true, + .root_module = b.createModule(.{ + .root_source_file = null, + .target = b.resolveTargetQuery(.{}), + .optimize = .Debug, + .link_libc = true, + }), }); - exe.addCSourceFile(.{ .file = b.addWriteFiles().add("main.c", + exe.root_module.addCSourceFile(.{ .file = b.addWriteFiles().add("main.c", \\#include \\#include \\#include @@ -40,9 +46,10 @@ pub fn build(b: *std.Build) void { if (libfoo.installed_headers_include_tree != null) std.debug.panic("include tree step was created before linking", .{}); - // Link before we have registered all headers for installation, + // Link (and get the include tree) before we have registered all headers for installation, // to verify that the lazily created write files step is properly taken into account. - exe.linkLibrary(libfoo); + exe.root_module.linkLibrary(libfoo); + _ = libfoo.getEmittedIncludeTree(); if (libfoo.installed_headers_include_tree == null) std.debug.panic("include tree step was not created after linking", .{}); @@ -56,10 +63,13 @@ pub fn build(b: *std.Build) void { const libbar = b.addStaticLibrary(.{ .name = "bar", - .target = b.resolveTargetQuery(.{}), - .optimize = .Debug, + .root_module = b.createModule(.{ + .root_source_file = null, + .target = b.resolveTargetQuery(.{}), + .optimize = .Debug, + }), }); - libbar.addCSourceFile(.{ .file = empty_c }); + libbar.root_module.addCSourceFile(.{ .file = empty_c }); libbar.installHeader(b.addWriteFiles().add("bar.h", \\#define BAR_X "X" \\ @@ -78,9 +88,11 @@ pub fn build(b: *std.Build) void { }); const check_exists = b.addExecutable(.{ .name = "check_exists", - .root_source_file = b.path("check_exists.zig"), - .target = b.resolveTargetQuery(.{}), - .optimize = .Debug, + .root_module = b.createModule(.{ + .root_source_file = b.path("check_exists.zig"), + .target = b.resolveTargetQuery(.{}), + .optimize = .Debug, + }), }); const run_check_exists = b.addRunArtifact(check_exists); run_check_exists.addArgs(&.{ diff --git a/test/standalone/install_raw_hex/build.zig b/test/standalone/install_raw_hex/build.zig index 515528534ee7238a0bed9aacd41a3f093353ab90..c12949a9cec08b55180d367f07197d5e451a1b66 100644 --- a/test/standalone/install_raw_hex/build.zig +++ b/test/standalone/install_raw_hex/build.zig @@ -16,9 +16,11 @@ pub fn build(b: *std.Build) void { const elf = b.addExecutable(.{ .name = "zig-nrf52-blink.elf", - .root_source_file = b.path("main.zig"), - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .target = target, + .optimize = optimize, + }), }); const hex_step = elf.addObjCopy(.{ diff --git a/test/standalone/ios/build.zig b/test/standalone/ios/build.zig index c4ae240bd6f5ffed531c76f80c89d681db57a7c0..6148920b54ba6494359c58f7e6393e36ac3094f1 100644 --- a/test/standalone/ios/build.zig +++ b/test/standalone/ios/build.zig @@ -18,16 +18,19 @@ pub fn build(b: *std.Build) void { const exe = b.addExecutable(.{ .name = "main", - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = null, + .optimize = optimize, + .target = target, + .link_libc = true, + }), }); - exe.addCSourceFile(.{ .file = b.path("main.m"), .flags = &.{} }); - exe.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/usr/include" }) }); - exe.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/System/Library/Frameworks" }) }); - exe.addLibraryPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/usr/lib" }) }); - exe.linkFramework("Foundation"); - exe.linkFramework("UIKit"); - exe.linkLibC(); + exe.root_module.addCSourceFile(.{ .file = b.path("main.m"), .flags = &.{} }); + exe.root_module.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/usr/include" }) }); + exe.root_module.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/System/Library/Frameworks" }) }); + exe.root_module.addLibraryPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/usr/lib" }) }); + exe.root_module.linkFramework("Foundation", .{}); + exe.root_module.linkFramework("UIKit", .{}); const check = exe.checkObject(); check.checkInHeaders(); diff --git a/test/standalone/issue_11595/build.zig b/test/standalone/issue_11595/build.zig index c37dfb12b57d8b87ba09443a312d1ad675fdc2e1..88b7af06a55ee440b3766169cb69f3b3343a687a 100644 --- a/test/standalone/issue_11595/build.zig +++ b/test/standalone/issue_11595/build.zig @@ -15,9 +15,11 @@ pub fn build(b: *std.Build) void { const exe = b.addExecutable(.{ .name = "zigtest", - .root_source_file = b.path("main.zig"), - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .target = target, + .optimize = optimize, + }), }); b.installArtifact(exe); @@ -25,8 +27,8 @@ pub fn build(b: *std.Build) void { "test.c", }; - exe.addCSourceFiles(.{ .files = &c_sources }); - exe.linkLibC(); + exe.root_module.addCSourceFiles(.{ .files = &c_sources }); + exe.root_module.link_libc = true; var i: i32 = 0; while (i < 1000) : (i += 1) { diff --git a/test/standalone/issue_12706/build.zig b/test/standalone/issue_12706/build.zig index 9cb858d0194e0a175214d2ab40f89bb0536f7a89..cace3c7d2d3b66376e529376ffabb316a2fd4e8f 100644 --- a/test/standalone/issue_12706/build.zig +++ b/test/standalone/issue_12706/build.zig @@ -10,16 +10,18 @@ pub fn build(b: *std.Build) void { const exe = b.addExecutable(.{ .name = "main", - .root_source_file = b.path("main.zig"), - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = optimize, + .target = target, + }), }); const c_sources = [_][]const u8{ "test.c", }; - exe.addCSourceFiles(.{ .files = &c_sources }); - exe.linkLibC(); + exe.root_module.addCSourceFiles(.{ .files = &c_sources }); + exe.root_module.link_libc = true; const run_cmd = b.addRunArtifact(exe); run_cmd.expectExitCode(0); diff --git a/test/standalone/issue_13970/build.zig b/test/standalone/issue_13970/build.zig index e7d3a8243162e43ad274a4d8c344bdb60848624c..bad94161af0dd5f413591d1da2568c75e9aac594 100644 --- a/test/standalone/issue_13970/build.zig +++ b/test/standalone/issue_13970/build.zig @@ -5,15 +5,24 @@ pub fn build(b: *std.Build) void { b.default_step = test_step; const test1 = b.addTest(.{ - .root_source_file = b.path("test_root/empty.zig"), + .root_module = b.createModule(.{ + .root_source_file = b.path("test_root/empty.zig"), + .target = b.graph.host, + }), .test_runner = "src/main.zig", }); const test2 = b.addTest(.{ - .root_source_file = b.path("src/empty.zig"), + .root_module = b.createModule(.{ + .root_source_file = b.path("src/empty.zig"), + .target = b.graph.host, + }), .test_runner = "src/main.zig", }); const test3 = b.addTest(.{ - .root_source_file = b.path("empty.zig"), + .root_module = b.createModule(.{ + .root_source_file = b.path("empty.zig"), + .target = b.graph.host, + }), .test_runner = "src/main.zig", }); diff --git a/test/standalone/issue_339/build.zig b/test/standalone/issue_339/build.zig index 321a6352549467b6931f62cc4d1d590f76799bf1..364dc0e81bef9816b6a186bfb51a6348a4c95e10 100644 --- a/test/standalone/issue_339/build.zig +++ b/test/standalone/issue_339/build.zig @@ -9,9 +9,11 @@ pub fn build(b: *std.Build) void { const obj = b.addObject(.{ .name = "test", - .root_source_file = b.path("test.zig"), - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("test.zig"), + .target = target, + .optimize = optimize, + }), }); // TODO: actually check the output diff --git a/test/standalone/issue_5825/build.zig b/test/standalone/issue_5825/build.zig index d4462bb5a1080e4748e8315d17296101123a661c..3b4bb33defa7bbbd9214b4d8d3e0a7ed14985654 100644 --- a/test/standalone/issue_5825/build.zig +++ b/test/standalone/issue_5825/build.zig @@ -16,20 +16,25 @@ pub fn build(b: *std.Build) void { const optimize: std.builtin.OptimizeMode = .Debug; const obj = b.addObject(.{ .name = "issue_5825", - .root_source_file = b.path("main.zig"), - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = optimize, + .target = target, + }), }); const exe = b.addExecutable(.{ .name = "issue_5825", - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = null, + .optimize = optimize, + .target = target, + }), }); exe.subsystem = .Console; - exe.linkSystemLibrary("kernel32"); - exe.linkSystemLibrary("ntdll"); - exe.addObject(obj); + exe.root_module.linkSystemLibrary("kernel32", .{}); + exe.root_module.linkSystemLibrary("ntdll", .{}); + exe.root_module.addObject(obj); // TODO: actually check the output _ = exe.getEmittedBin(); diff --git a/test/standalone/issue_794/build.zig b/test/standalone/issue_794/build.zig index d42ff1f3049f6ebb68facc22115806841b89ebda..4b0c089f97ee382630e274c9f46f42c0f19e2911 100644 --- a/test/standalone/issue_794/build.zig +++ b/test/standalone/issue_794/build.zig @@ -4,9 +4,10 @@ pub fn build(b: *std.Build) void { const test_step = b.step("test", "Test it"); b.default_step = test_step; - const test_artifact = b.addTest(.{ + const test_artifact = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("main.zig"), - }); + .target = b.graph.host, + }) }); test_artifact.addIncludePath(b.path("a_directory")); // TODO: actually check the output diff --git a/test/standalone/issue_8550/build.zig b/test/standalone/issue_8550/build.zig index a0dea518d683aeecc9126c0d4e29b326908494a8..ee82332973d072bf7464dca1f2c4606e09c4e4eb 100644 --- a/test/standalone/issue_8550/build.zig +++ b/test/standalone/issue_8550/build.zig @@ -15,11 +15,13 @@ pub fn build(b: *std.Build) !void { const kernel = b.addExecutable(.{ .name = "kernel", - .root_source_file = b.path("./main.zig"), - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("./main.zig"), + .optimize = optimize, + .target = target, + }), }); - kernel.addObjectFile(b.path("./boot.S")); + kernel.root_module.addObjectFile(b.path("./boot.S")); kernel.setLinkerScript(b.path("./linker.ld")); b.installArtifact(kernel); diff --git a/test/standalone/libcxx/build.zig b/test/standalone/libcxx/build.zig index 3989bda9bc8bafc9c0d3b0bfad66234f38f8c0a6..aaa62abdb4db67c6b108b4a4281e3a7ea0319b52 100644 --- a/test/standalone/libcxx/build.zig +++ b/test/standalone/libcxx/build.zig @@ -11,12 +11,14 @@ pub fn build(b: *std.Build) void { { const exe = b.addExecutable(.{ .name = "mt", - .root_source_file = b.path("mt.zig"), - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("mt.zig"), + .target = target, + .optimize = optimize, + .link_libcpp = true, + }), }); - exe.linkLibCpp(); - exe.addCSourceFile(.{ .file = b.path("mt_doit.cpp") }); + exe.root_module.addCSourceFile(.{ .file = b.path("mt_doit.cpp") }); link_step.dependOn(&exe.step); b.installArtifact(exe); run_step.dependOn(&b.addRunArtifact(exe).step); @@ -24,13 +26,15 @@ pub fn build(b: *std.Build) void { { const exe = b.addExecutable(.{ .name = "st", - .root_source_file = b.path("st.zig"), - .target = target, - .optimize = optimize, - .single_threaded = true, + .root_module = b.createModule(.{ + .root_source_file = b.path("st.zig"), + .target = target, + .optimize = optimize, + .link_libcpp = true, + .single_threaded = true, + }), }); - exe.linkLibCpp(); - exe.addCSourceFile(.{ .file = b.path("st_doit.cpp") }); + exe.root_module.addCSourceFile(.{ .file = b.path("st_doit.cpp") }); link_step.dependOn(&exe.step); b.installArtifact(exe); run_step.dependOn(&b.addRunArtifact(exe).step); diff --git a/test/standalone/load_dynamic_library/build.zig b/test/standalone/load_dynamic_library/build.zig index f484d2a6e6fff88e06f31a7ac7c33fbd05336f5a..ec05c45c21e2ab585916a8f9cd9b20777aaf9b0e 100644 --- a/test/standalone/load_dynamic_library/build.zig +++ b/test/standalone/load_dynamic_library/build.zig @@ -12,17 +12,21 @@ pub fn build(b: *std.Build) void { const lib = b.addSharedLibrary(.{ .name = "add", - .root_source_file = b.path("add.zig"), .version = .{ .major = 1, .minor = 0, .patch = 0 }, - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("add.zig"), + .optimize = optimize, + .target = target, + }), }); const main = b.addExecutable(.{ .name = "main", - .root_source_file = b.path("main.zig"), - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = optimize, + .target = target, + }), }); const run = b.addRunArtifact(main); diff --git a/test/standalone/mix_c_files/build.zig b/test/standalone/mix_c_files/build.zig index 0b626ba0faa4edc5e55b1b4abae6bcf338b57b4a..32b1d57b5cb87f39463ffc01cd45670a769345e8 100644 --- a/test/standalone/mix_c_files/build.zig +++ b/test/standalone/mix_c_files/build.zig @@ -18,12 +18,14 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const exe = b.addExecutable(.{ .name = "test", - .root_source_file = b.path("main.zig"), - .target = b.graph.host, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .target = b.graph.host, + .optimize = optimize, + .link_libc = true, + }), }); - exe.addCSourceFile(.{ .file = b.path("test.c"), .flags = &[_][]const u8{"-std=c11"} }); - exe.linkLibC(); + exe.root_module.addCSourceFile(.{ .file = b.path("test.c"), .flags = &[_][]const u8{"-std=c11"} }); const run_cmd = b.addRunArtifact(exe); run_cmd.skip_foreign_checks = true; diff --git a/test/standalone/mix_o_files/build.zig b/test/standalone/mix_o_files/build.zig index 7be4ff5090ac9730a524a100f973bcd747ad2dbb..12b90f36247b379422b4ff616d671b85d5b5a66c 100644 --- a/test/standalone/mix_o_files/build.zig +++ b/test/standalone/mix_o_files/build.zig @@ -9,22 +9,27 @@ pub fn build(b: *std.Build) void { const obj = b.addObject(.{ .name = "base64", - .root_source_file = b.path("base64.zig"), - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("base64.zig"), + .optimize = optimize, + .target = target, + }), }); const exe = b.addExecutable(.{ .name = "test", - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = null, + .optimize = optimize, + .target = target, + .link_libc = true, + }), }); - exe.addCSourceFile(.{ + exe.root_module.addCSourceFile(.{ .file = b.path("test.c"), .flags = &[_][]const u8{"-std=c99"}, }); - exe.addObject(obj); - exe.linkSystemLibrary("c"); + exe.root_module.addObject(obj); b.default_step.dependOn(&exe.step); diff --git a/test/standalone/options/build.zig b/test/standalone/options/build.zig index 1c17e30d6a03a909e7080412e644934dc0063158..07efea2a7b8e094182e437b3529b7ccc02fef434 100644 --- a/test/standalone/options/build.zig +++ b/test/standalone/options/build.zig @@ -1,11 +1,11 @@ const std = @import("std"); pub fn build(b: *std.Build) void { - const main = b.addTest(.{ + const main = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = b.graph.host, .optimize = .Debug, - }); + }) }); const options = b.addOptions(); main.addOptions("build_options", options); diff --git a/test/standalone/pie/build.zig b/test/standalone/pie/build.zig index 25ed330abce22e388dbe1159b778fc97d79fe348..79d078a9eba56d191f7cdd1f55452aa91e632001 100644 --- a/test/standalone/pie/build.zig +++ b/test/standalone/pie/build.zig @@ -11,9 +11,11 @@ pub fn build(b: *std.Build) void { }); const main = b.addTest(.{ - .root_source_file = b.path("main.zig"), - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = optimize, + .target = target, + }), }); main.pie = true; diff --git a/test/standalone/pkg_import/build.zig b/test/standalone/pkg_import/build.zig index b0d0c035ddb80dd5183b46fb9c471154e0a9d3d9..3d94930ef3fdbcfb61b15623dd51036f23855045 100644 --- a/test/standalone/pkg_import/build.zig +++ b/test/standalone/pkg_import/build.zig @@ -8,9 +8,11 @@ pub fn build(b: *std.Build) void { const exe = b.addExecutable(.{ .name = "test", - .root_source_file = b.path("test.zig"), - .optimize = optimize, - .target = b.graph.host, + .root_module = b.createModule(.{ + .root_source_file = b.path("test.zig"), + .optimize = optimize, + .target = b.graph.host, + }), }); exe.root_module.addAnonymousImport("my_pkg", .{ .root_source_file = b.path("pkg.zig") }); diff --git a/test/standalone/run_output_caching/build.zig b/test/standalone/run_output_caching/build.zig index 3842f097d1e6308de9716d108f83abc7ffcbd24b..98f5a63dd540878d8f706268071ff650ac95027c 100644 --- a/test/standalone/run_output_caching/build.zig +++ b/test/standalone/run_output_caching/build.zig @@ -9,9 +9,11 @@ pub fn build(b: *std.Build) void { const exe = b.addExecutable(.{ .name = "create-file", - .root_source_file = b.path("main.zig"), - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .target = target, + .optimize = optimize, + }), }); { diff --git a/test/standalone/run_output_paths/build.zig b/test/standalone/run_output_paths/build.zig index f4c7254d8fc7286766c495a03b06a91dd5dd7bdd..78b75e147d33eded9dffb672c043074e58bd5306 100644 --- a/test/standalone/run_output_paths/build.zig +++ b/test/standalone/run_output_paths/build.zig @@ -9,9 +9,11 @@ pub fn build(b: *std.Build) void { const create_file_exe = b.addExecutable(.{ .name = "create_file", - .root_source_file = b.path("create_file.zig"), - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("create_file.zig"), + .target = target, + .optimize = optimize, + }), }); const create_first = b.addRunArtifact(create_file_exe); diff --git a/test/standalone/self_exe_symlink/build.zig b/test/standalone/self_exe_symlink/build.zig index 2b7b3d423c5d3e1b656d2001c7b39fa4ddae1bc9..651740c04be04442d1f85007f6a23bc2938a6a30 100644 --- a/test/standalone/self_exe_symlink/build.zig +++ b/test/standalone/self_exe_symlink/build.zig @@ -15,16 +15,20 @@ pub fn build(b: *std.Build) void { const main = b.addExecutable(.{ .name = "main", - .root_source_file = b.path("main.zig"), - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = optimize, + .target = target, + }), }); const create_symlink_exe = b.addExecutable(.{ .name = "create-symlink", - .root_source_file = b.path("create-symlink.zig"), - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("create-symlink.zig"), + .optimize = optimize, + .target = target, + }), }); var run_create_symlink = b.addRunArtifact(create_symlink_exe); diff --git a/test/standalone/shared_library/build.zig b/test/standalone/shared_library/build.zig index 28c1bdb13e1d81f328246325cd1e7508b0fd21de..ab90c129b9965cd79d8a124715f6d149ee59eeda 100644 --- a/test/standalone/shared_library/build.zig +++ b/test/standalone/shared_library/build.zig @@ -8,23 +8,28 @@ pub fn build(b: *std.Build) void { const target = b.graph.host; const lib = b.addSharedLibrary(.{ .name = "mathtest", - .root_source_file = b.path("mathtest.zig"), .version = .{ .major = 1, .minor = 0, .patch = 0 }, - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("mathtest.zig"), + .target = target, + .optimize = optimize, + }), }); const exe = b.addExecutable(.{ .name = "test", - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = null, + .target = target, + .optimize = optimize, + .link_libc = true, + }), }); - exe.addCSourceFile(.{ + exe.root_module.addCSourceFile(.{ .file = b.path("test.c"), .flags = &[_][]const u8{"-std=c99"}, }); - exe.linkLibrary(lib); - exe.linkSystemLibrary("c"); + exe.root_module.linkLibrary(lib); const run_cmd = b.addRunArtifact(exe); test_step.dependOn(&run_cmd.step); diff --git a/test/standalone/simple/build.zig b/test/standalone/simple/build.zig index c623780fa5df43627de0d71f026f19b2120759f5..ba4464cefcea85c3a7582b1a1f026d4033e16f88 100644 --- a/test/standalone/simple/build.zig +++ b/test/standalone/simple/build.zig @@ -42,11 +42,13 @@ pub fn build(b: *std.Build) void { if (case.is_exe) { const exe = b.addExecutable(.{ .name = std.fs.path.stem(case.src_path), - .root_source_file = b.path(case.src_path), - .optimize = optimize, - .target = resolved_target, + .root_module = b.createModule(.{ + .root_source_file = b.path(case.src_path), + .optimize = optimize, + .target = resolved_target, + }), }); - if (case.link_libc) exe.linkLibC(); + if (case.link_libc) exe.root_module.link_libc = true; _ = exe.getEmittedBin(); @@ -56,11 +58,13 @@ pub fn build(b: *std.Build) void { if (case.is_test) { const exe = b.addTest(.{ .name = std.fs.path.stem(case.src_path), - .root_source_file = b.path(case.src_path), - .optimize = optimize, - .target = resolved_target, + .root_module = b.createModule(.{ + .root_source_file = b.path(case.src_path), + .optimize = optimize, + .target = resolved_target, + }), }); - if (case.link_libc) exe.linkLibC(); + if (case.link_libc) exe.root_module.link_libc = true; const run = b.addRunArtifact(exe); step.dependOn(&run.step); diff --git a/test/standalone/stack_iterator/build.zig b/test/standalone/stack_iterator/build.zig index 20027428b0ff41126c235d1d936b78828e9b3923..9d660b5c57bac985af4c37483645b103aac49298 100644 --- a/test/standalone/stack_iterator/build.zig +++ b/test/standalone/stack_iterator/build.zig @@ -20,11 +20,13 @@ pub fn build(b: *std.Build) void { { const exe = b.addExecutable(.{ .name = "unwind_fp", - .root_source_file = b.path("unwind.zig"), - .target = target, - .optimize = optimize, - .unwind_tables = if (target.result.isDarwin()) .@"async" else null, - .omit_frame_pointer = false, + .root_module = b.createModule(.{ + .root_source_file = b.path("unwind.zig"), + .target = target, + .optimize = optimize, + .unwind_tables = if (target.result.isDarwin()) .@"async" else null, + .omit_frame_pointer = false, + }), }); const run_cmd = b.addRunArtifact(exe); @@ -43,11 +45,13 @@ pub fn build(b: *std.Build) void { { const exe = b.addExecutable(.{ .name = "unwind_nofp", - .root_source_file = b.path("unwind.zig"), - .target = target, - .optimize = optimize, - .unwind_tables = .@"async", - .omit_frame_pointer = true, + .root_module = b.createModule(.{ + .root_source_file = b.path("unwind.zig"), + .target = target, + .optimize = optimize, + .unwind_tables = .@"async", + .omit_frame_pointer = true, + }), }); const run_cmd = b.addRunArtifact(exe); @@ -66,27 +70,32 @@ pub fn build(b: *std.Build) void { { const c_shared_lib = b.addSharedLibrary(.{ .name = "c_shared_lib", - .target = target, - .optimize = optimize, - .strip = false, + .root_module = b.createModule(.{ + .root_source_file = null, + .target = target, + .optimize = optimize, + .link_libc = true, + .strip = false, + }), }); if (target.result.os.tag == .windows) c_shared_lib.root_module.addCMacro("LIB_API", "__declspec(dllexport)"); - c_shared_lib.addCSourceFile(.{ + c_shared_lib.root_module.addCSourceFile(.{ .file = b.path("shared_lib.c"), .flags = &.{"-fomit-frame-pointer"}, }); - c_shared_lib.linkLibC(); const exe = b.addExecutable(.{ .name = "shared_lib_unwind", - .root_source_file = b.path("shared_lib_unwind.zig"), - .target = target, - .optimize = optimize, - .unwind_tables = if (target.result.isDarwin()) .@"async" else null, - .omit_frame_pointer = true, + .root_module = b.createModule(.{ + .root_source_file = b.path("shared_lib_unwind.zig"), + .target = target, + .optimize = optimize, + .unwind_tables = if (target.result.isDarwin()) .@"async" else null, + .omit_frame_pointer = true, + }), }); exe.linkLibrary(c_shared_lib); @@ -117,14 +126,16 @@ pub fn build(b: *std.Build) void { inline for (no_os_targets) |os_tag| { const exe = b.addExecutable(.{ .name = "unwind_freestanding", - .root_source_file = b.path("unwind_freestanding.zig"), - .target = b.resolveTargetQuery(.{ - .cpu_arch = .x86_64, - .os_tag = os_tag, + .root_module = b.createModule(.{ + .root_source_file = b.path("unwind_freestanding.zig"), + .target = b.resolveTargetQuery(.{ + .cpu_arch = .x86_64, + .os_tag = os_tag, + }), + .optimize = optimize, + .unwind_tables = null, + .omit_frame_pointer = false, }), - .optimize = optimize, - .unwind_tables = null, - .omit_frame_pointer = false, }); // This "freestanding" binary is runnable because it invokes the diff --git a/test/standalone/static_c_lib/build.zig b/test/standalone/static_c_lib/build.zig index c952e9b25013e1395c6a235532b7da33827687f8..98b417aaf7cb69cc982fde714499ea04567318ea 100644 --- a/test/standalone/static_c_lib/build.zig +++ b/test/standalone/static_c_lib/build.zig @@ -8,18 +8,22 @@ pub fn build(b: *std.Build) void { const foo = b.addStaticLibrary(.{ .name = "foo", - .optimize = optimize, - .target = b.graph.host, + .root_module = b.createModule(.{ + .root_source_file = null, + .optimize = optimize, + .target = b.graph.host, + }), }); - foo.addCSourceFile(.{ .file = b.path("foo.c"), .flags = &[_][]const u8{} }); - foo.addIncludePath(b.path(".")); + foo.root_module.addCSourceFile(.{ .file = b.path("foo.c"), .flags = &[_][]const u8{} }); + foo.root_module.addIncludePath(b.path(".")); - const test_exe = b.addTest(.{ + const test_exe = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("foo.zig"), + .target = b.graph.host, .optimize = optimize, - }); - test_exe.linkLibrary(foo); - test_exe.addIncludePath(b.path(".")); + }) }); + test_exe.root_module.linkLibrary(foo); + test_exe.root_module.addIncludePath(b.path(".")); test_step.dependOn(&b.addRunArtifact(test_exe).step); } diff --git a/test/standalone/strip_empty_loop/build.zig b/test/standalone/strip_empty_loop/build.zig index d34120ed06b8230902e9dedc1d5339bfbf012f9b..9f268d4cb864cf7ea6a405ddf238f2b040af7c1b 100644 --- a/test/standalone/strip_empty_loop/build.zig +++ b/test/standalone/strip_empty_loop/build.zig @@ -9,10 +9,12 @@ pub fn build(b: *std.Build) void { const main = b.addExecutable(.{ .name = "main", - .root_source_file = b.path("main.zig"), - .optimize = optimize, - .target = target, - .strip = true, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = optimize, + .target = target, + .strip = true, + }), }); // TODO: actually check the output diff --git a/test/standalone/strip_struct_init/build.zig b/test/standalone/strip_struct_init/build.zig index ef7960d13051ccf348b65f4df4676b6d994eb9cc..175724df6348f524f7680a73177ec81ab2e97cf0 100644 --- a/test/standalone/strip_struct_init/build.zig +++ b/test/standalone/strip_struct_init/build.zig @@ -7,9 +7,12 @@ pub fn build(b: *std.Build) void { const optimize: std.builtin.OptimizeMode = .Debug; const main = b.addTest(.{ - .root_source_file = b.path("main.zig"), - .optimize = optimize, - .strip = true, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .target = b.graph.host, + .optimize = optimize, + .strip = true, + }), }); test_step.dependOn(&b.addRunArtifact(main).step); diff --git a/test/standalone/test_runner_module_imports/build.zig b/test/standalone/test_runner_module_imports/build.zig index cad3b05e08eaf9a9149118c0717b66c655232f72..6b24ef7124f7b4539ee15ed81d1d4b81386f95d2 100644 --- a/test/standalone/test_runner_module_imports/build.zig +++ b/test/standalone/test_runner_module_imports/build.zig @@ -1,19 +1,21 @@ const std = @import("std"); pub fn build(b: *std.Build) void { - const t = b.addTest(.{ + const test_mod = b.createModule(.{ .root_source_file = b.path("src/main.zig"), - .test_runner = b.path("test_runner/main.zig"), + .target = b.graph.host, }); - const module1 = b.createModule(.{ .root_source_file = b.path("module1/main.zig") }); - const module2 = b.createModule(.{ - .root_source_file = b.path("module2/main.zig"), - .imports = &.{.{ .name = "module1", .module = module1 }}, + const module2 = b.createModule(.{ .root_source_file = b.path("module2/main.zig") }); + + module2.addImport("module1", module1); + test_mod.addImport("module2", module2); + + const t = b.addTest(.{ + .root_module = test_mod, + .test_runner = b.path("test_runner/main.zig"), }); - t.root_module.addImport("module2", module2); - const test_step = b.step("test", "Run unit tests"); test_step.dependOn(&b.addRunArtifact(t).step); b.default_step = test_step; diff --git a/test/standalone/test_runner_path/build.zig b/test/standalone/test_runner_path/build.zig index 48ca3c19db8849d935c24bf1301b71c46224533f..a7b8eaf321361316afa5a6323b2beed987364ee5 100644 --- a/test/standalone/test_runner_path/build.zig +++ b/test/standalone/test_runner_path/build.zig @@ -6,9 +6,10 @@ pub fn build(b: *std.Build) void { const test_step = b.step("test", "Test the program"); b.default_step = test_step; - const test_exe = b.addTest(.{ + const test_exe = b.addTest(.{ .root_module = b.createModule(.{ + .target = b.graph.host, .root_source_file = b.path("test.zig"), - }); + }) }); test_exe.test_runner = b.path("test_runner.zig"); const test_run = b.addRunArtifact(test_exe); diff --git a/test/standalone/use_alias/build.zig b/test/standalone/use_alias/build.zig index 7ee501713c32e29f0dc0410e4bdcf9632ca4f8a1..399a23d924ca43eb56e2e4533d29bde77848d138 100644 --- a/test/standalone/use_alias/build.zig +++ b/test/standalone/use_alias/build.zig @@ -6,11 +6,12 @@ pub fn build(b: *std.Build) void { const optimize: std.builtin.OptimizeMode = .Debug; - const main = b.addTest(.{ + const main = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("main.zig"), + .target = b.graph.host, .optimize = optimize, - }); - main.addIncludePath(b.path(".")); + }) }); + main.root_module.addIncludePath(b.path(".")); test_step.dependOn(&b.addRunArtifact(main).step); } diff --git a/test/standalone/windows_argv/build.zig b/test/standalone/windows_argv/build.zig index 4e41a8716e96619c73cd95ed3ab25fbcc79b4053..ea4b26cc2281bd95bc33ff4dce38f65d943f0777 100644 --- a/test/standalone/windows_argv/build.zig +++ b/test/standalone/windows_argv/build.zig @@ -11,32 +11,39 @@ pub fn build(b: *std.Build) !void { const lib_gnu = b.addStaticLibrary(.{ .name = "toargv-gnu", - .root_source_file = b.path("lib.zig"), - .target = b.resolveTargetQuery(.{ - .abi = .gnu, + .root_module = b.createModule(.{ + .root_source_file = b.path("lib.zig"), + .target = b.resolveTargetQuery(.{ + .abi = .gnu, + }), + .optimize = optimize, }), - .optimize = optimize, }); const verify_gnu = b.addExecutable(.{ .name = "verify-gnu", - .target = b.resolveTargetQuery(.{ - .abi = .gnu, + .root_module = b.createModule(.{ + .root_source_file = null, + .target = b.resolveTargetQuery(.{ + .abi = .gnu, + }), + .optimize = optimize, }), - .optimize = optimize, }); - verify_gnu.addCSourceFile(.{ + verify_gnu.root_module.addCSourceFile(.{ .file = b.path("verify.c"), .flags = &.{ "-DUNICODE", "-D_UNICODE" }, }); verify_gnu.mingw_unicode_entry_point = true; - verify_gnu.linkLibrary(lib_gnu); - verify_gnu.linkLibC(); + verify_gnu.root_module.linkLibrary(lib_gnu); + verify_gnu.root_module.link_libc = true; const fuzz = b.addExecutable(.{ .name = "fuzz", - .root_source_file = b.path("fuzz.zig"), - .target = b.graph.host, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("fuzz.zig"), + .target = b.graph.host, + .optimize = optimize, + }), }); 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 { if (has_msvc) { const lib_msvc = b.addStaticLibrary(.{ .name = "toargv-msvc", - .root_source_file = b.path("lib.zig"), - .target = b.resolveTargetQuery(.{ - .abi = .msvc, + .root_module = b.createModule(.{ + .root_source_file = b.path("lib.zig"), + .target = b.resolveTargetQuery(.{ + .abi = .msvc, + }), + .optimize = optimize, }), - .optimize = optimize, }); const verify_msvc = b.addExecutable(.{ .name = "verify-msvc", - .target = b.resolveTargetQuery(.{ - .abi = .msvc, + .root_module = b.createModule(.{ + .root_source_file = null, + .target = b.resolveTargetQuery(.{ + .abi = .msvc, + }), + .optimize = optimize, }), - .optimize = optimize, }); - verify_msvc.addCSourceFile(.{ + verify_msvc.root_module.addCSourceFile(.{ .file = b.path("verify.c"), .flags = &.{ "-DUNICODE", "-D_UNICODE" }, }); - verify_msvc.linkLibrary(lib_msvc); - verify_msvc.linkLibC(); + verify_msvc.root_module.linkLibrary(lib_msvc); + verify_msvc.root_module.link_libc = true; const run_msvc = b.addRunArtifact(fuzz); run_msvc.setName("fuzz-msvc"); diff --git a/test/standalone/windows_bat_args/build.zig b/test/standalone/windows_bat_args/build.zig index 7c35cf2b768ef27b0293fca74d36d187ceeae58e..b91b4fb3bbfd510bf3f89eb9eb0dce16067410f7 100644 --- a/test/standalone/windows_bat_args/build.zig +++ b/test/standalone/windows_bat_args/build.zig @@ -12,16 +12,20 @@ pub fn build(b: *std.Build) !void { const echo_args = b.addExecutable(.{ .name = "echo-args", - .root_source_file = b.path("echo-args.zig"), - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("echo-args.zig"), + .optimize = optimize, + .target = target, + }), }); const test_exe = b.addExecutable(.{ .name = "test", - .root_source_file = b.path("test.zig"), - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("test.zig"), + .optimize = optimize, + .target = target, + }), }); const run = b.addRunArtifact(test_exe); @@ -33,9 +37,11 @@ pub fn build(b: *std.Build) !void { const fuzz = b.addExecutable(.{ .name = "fuzz", - .root_source_file = b.path("fuzz.zig"), - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("fuzz.zig"), + .optimize = optimize, + .target = target, + }), }); const fuzz_max_iterations = b.option(u64, "iterations", "The max fuzz iterations (default: 100)") orelse 100; diff --git a/test/standalone/windows_entry_points/build.zig b/test/standalone/windows_entry_points/build.zig index c3d9c49940830291bc53d25f1d2a8f66eb81df2b..1889b0c8435ff665fcad98717c160bb939c2e113 100644 --- a/test/standalone/windows_entry_points/build.zig +++ b/test/standalone/windows_entry_points/build.zig @@ -13,11 +13,14 @@ pub fn build(b: *std.Build) void { { const exe = b.addExecutable(.{ .name = "main", - .target = target, - .optimize = .Debug, - .link_libc = true, + .root_module = b.createModule(.{ + .root_source_file = null, + .target = target, + .optimize = .Debug, + .link_libc = true, + }), }); - exe.addCSourceFile(.{ .file = b.path("main.c") }); + exe.root_module.addCSourceFile(.{ .file = b.path("main.c") }); _ = exe.getEmittedBin(); test_step.dependOn(&exe.step); @@ -26,12 +29,15 @@ pub fn build(b: *std.Build) void { { const exe = b.addExecutable(.{ .name = "wmain", - .target = target, - .optimize = .Debug, - .link_libc = true, + .root_module = b.createModule(.{ + .root_source_file = null, + .target = target, + .optimize = .Debug, + .link_libc = true, + }), }); exe.mingw_unicode_entry_point = true; - exe.addCSourceFile(.{ .file = b.path("wmain.c") }); + exe.root_module.addCSourceFile(.{ .file = b.path("wmain.c") }); _ = exe.getEmittedBin(); test_step.dependOn(&exe.step); @@ -40,12 +46,15 @@ pub fn build(b: *std.Build) void { { const exe = b.addExecutable(.{ .name = "winmain", - .target = target, - .optimize = .Debug, - .link_libc = true, + .root_module = b.createModule(.{ + .root_source_file = null, + .target = target, + .optimize = .Debug, + .link_libc = true, + }), }); // Note: `exe.subsystem = .Windows;` is not necessary - exe.addCSourceFile(.{ .file = b.path("winmain.c") }); + exe.root_module.addCSourceFile(.{ .file = b.path("winmain.c") }); _ = exe.getEmittedBin(); test_step.dependOn(&exe.step); @@ -54,13 +63,16 @@ pub fn build(b: *std.Build) void { { const exe = b.addExecutable(.{ .name = "wwinmain", - .target = target, - .optimize = .Debug, - .link_libc = true, + .root_module = b.createModule(.{ + .root_source_file = null, + .target = target, + .optimize = .Debug, + .link_libc = true, + }), }); exe.mingw_unicode_entry_point = true; // Note: `exe.subsystem = .Windows;` is not necessary - exe.addCSourceFile(.{ .file = b.path("wwinmain.c") }); + exe.root_module.addCSourceFile(.{ .file = b.path("wwinmain.c") }); _ = exe.getEmittedBin(); test_step.dependOn(&exe.step); diff --git a/test/standalone/windows_resources/build.zig b/test/standalone/windows_resources/build.zig index 27f4fa919ee2b5470b9f1686ae10644c919a21a4..d3afadb60d5d4a6e16cc3f98f04a45c75c350307 100644 --- a/test/standalone/windows_resources/build.zig +++ b/test/standalone/windows_resources/build.zig @@ -28,11 +28,13 @@ fn add( ) void { const exe = b.addExecutable(.{ .name = "zig_resource_test", - .root_source_file = b.path("main.zig"), - .target = target, - .optimize = .Debug, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .target = target, + .optimize = .Debug, + }), }); - exe.addWin32ResourceFile(.{ + exe.root_module.addWin32ResourceFile(.{ .file = b.path("res/zig.rc"), .flags = &.{"/c65001"}, // UTF-8 code page .include_paths = &.{ diff --git a/test/standalone/windows_spawn/build.zig b/test/standalone/windows_spawn/build.zig index 7f59ff1da4a81c7e3778ab5aa96981347a369905..2b967b16d5b3572db345d34ddc538053f2ce0d62 100644 --- a/test/standalone/windows_spawn/build.zig +++ b/test/standalone/windows_spawn/build.zig @@ -12,16 +12,20 @@ pub fn build(b: *std.Build) void { const hello = b.addExecutable(.{ .name = "hello", - .root_source_file = b.path("hello.zig"), - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("hello.zig"), + .optimize = optimize, + .target = target, + }), }); const main = b.addExecutable(.{ .name = "main", - .root_source_file = b.path("main.zig"), - .optimize = optimize, - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .optimize = optimize, + .target = target, + }), }); const run = b.addRunArtifact(main); diff --git a/test/standalone/zerolength_check/build.zig b/test/standalone/zerolength_check/build.zig index 8118c6e17231ed40e0516c3ea07aac76715c06d6..7edc55932cbdedcd594988abe605cbeb282e31b6 100644 --- a/test/standalone/zerolength_check/build.zig +++ b/test/standalone/zerolength_check/build.zig @@ -11,7 +11,7 @@ pub fn build(b: *std.Build) void { } fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { - const unit_tests = b.addTest(.{ + const unit_tests = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = b.resolveTargetQuery(.{ .os_tag = .wasi, @@ -19,7 +19,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize .cpu_features_add = std.Target.wasm.featureSet(&.{.bulk_memory}), }), .optimize = optimize, - }); + }) }); const run_unit_tests = b.addRunArtifact(unit_tests); run_unit_tests.skip_foreign_checks = true; -- 2.54.0 From afc77f0603395d9c1829a49081e198b0fa255abc Mon Sep 17 00:00:00 2001 From: mlugg Date: Mon, 16 Dec 2024 16:19:35 +0000 Subject: [PATCH 16/16] init template: expand slightly, migrate from deprecated std.Build APIs --- lib/init/build.zig | 52 +++++++++++++++++++++++++++++++------------ lib/init/src/main.zig | 10 ++++++++- lib/init/src/root.zig | 2 +- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/lib/init/build.zig b/lib/init/build.zig index d53cbbb982c3cd5cff1074e85d5a3502a0a96f24..c75a4e6be3bbca6d4d79885d602280b72800d485 100644 --- a/lib/init/build.zig +++ b/lib/init/build.zig @@ -15,13 +15,41 @@ pub fn build(b: *std.Build) void { // set a preferred release mode, allowing the user to decide how to optimize. const optimize = b.standardOptimizeOption(.{}); + // This creates a "module", which represents a collection of source files alongside + // some compilation options, such as optimization mode and linked system libraries. + // Every executable or library we compile will be based on one or more modules. + const lib_mod = b.createModule(.{ + // `root_source_file` is the Zig "entry point" of the module. If a module + // only contains e.g. external object files, you can make this `null`. + // In this case the main source file is merely a path, however, in more + // complicated build scripts, this could be a generated file. + .root_source_file = b.path("src/root.zig"), + .target = target, + .optimize = optimize, + }); + + // We will also create a module for our other entry point, 'main.zig'. + const exe_mod = b.createModule(.{ + // `root_source_file` is the Zig "entry point" of the module. If a module + // only contains e.g. external object files, you can make this `null`. + // In this case the main source file is merely a path, however, in more + // complicated build scripts, this could be a generated file. + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }); + + // Modules can depend on one another using the `std.Build.Module.addImport` function. + // This is what allows Zig source code to use `@import("foo")` where 'foo' is not a + // file path. In this case, we set up `exe_mod` to import `lib_mod`. + exe_mod.addImport("$_lib", lib_mod); + + // Now, we will create a static library based on the module we created above. + // This creates a `std.Build.Step.Compile`, which is the build step responsible + // for actually invoking the compiler. const lib = b.addStaticLibrary(.{ .name = "$", - // In this case the main source file is merely a path, however, in more - // complicated build scripts, this could be a generated file. - .root_source_file = b.path("src/root.zig"), - .target = target, - .optimize = optimize, + .root_module = lib_mod, }); // This declares intent for the library to be installed into the standard @@ -29,11 +57,11 @@ pub fn build(b: *std.Build) void { // running `zig build`). b.installArtifact(lib); + // This creates another `std.Build.Step.Compile`, but this one builds an executable + // rather than a static library. const exe = b.addExecutable(.{ .name = "$", - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, + .root_module = exe_mod, }); // This declares intent for the executable to be installed into the @@ -67,17 +95,13 @@ pub fn build(b: *std.Build) void { // Creates a step for unit testing. This only builds the test executable // but does not run it. const lib_unit_tests = b.addTest(.{ - .root_source_file = b.path("src/root.zig"), - .target = target, - .optimize = optimize, + .root_module = lib_mod, }); const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests); const exe_unit_tests = b.addTest(.{ - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, + .root_module = exe_mod, }); const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); diff --git a/lib/init/src/main.zig b/lib/init/src/main.zig index ba5a2ccef2339fc967fbdbe1b925ae9d4133a7cb..caf8f4f37a968fa41c12fb1895d0138bd326421f 100644 --- a/lib/init/src/main.zig +++ b/lib/init/src/main.zig @@ -1,7 +1,6 @@ //! By convention, main.zig is where your main function lives in the case that //! you are building an executable. If you are making a library, the convention //! is to delete this file and start with root.zig instead. -const std = @import("std"); pub fn main() !void { // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) @@ -26,6 +25,10 @@ test "simple test" { try std.testing.expectEqual(@as(i32, 42), list.pop()); } +test "use other module" { + try std.testing.expectEqual(@as(i32, 150), lib.add(100, 50)); +} + test "fuzz example" { const global = struct { fn testOne(input: []const u8) anyerror!void { @@ -35,3 +38,8 @@ test "fuzz example" { }; try std.testing.fuzz(global.testOne, .{}); } + +const std = @import("std"); + +/// This imports the separate module containing `root.zig`. Take a look in `build.zig` for details. +const lib = @import("$_lib"); diff --git a/lib/init/src/root.zig b/lib/init/src/root.zig index f034ca3589ffeeb5d08e438d65dfece2c5c2bc56..27d2be80b5ddfa4c5384af3907fd1ab0d181b371 100644 --- a/lib/init/src/root.zig +++ b/lib/init/src/root.zig @@ -4,7 +4,7 @@ const std = @import("std"); const testing = std.testing; -export fn add(a: i32, b: i32) i32 { +pub export fn add(a: i32, b: i32) i32 { return a + b; } -- 2.54.0