From 0d95b44a1c9e483beb82f7b70515185159d79bdc Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 30 Apr 2026 11:39:28 -0700 Subject: [PATCH] build system: implement cli positionals --- BRANCH_TODO | 33 +++++++++++++++++++++++---------- lib/compiler/Maker/Step/Run.zig | 23 +++++++++++++---------- lib/compiler/configurer.zig | 4 ++-- lib/init/build.zig | 4 +--- lib/std/Build/Configuration.zig | 2 +- lib/std/Build/Step/Run.zig | 30 +++++++++++++++++++++++++++++- 6 files changed, 69 insertions(+), 27 deletions(-) diff --git a/BRANCH_TODO b/BRANCH_TODO index 5fece6c3c729095a095433f0d6afb1ebb175748b..83accc79d0f31ada93ec3dab277d9a49d3fac410 100644 --- a/BRANCH_TODO +++ b/BRANCH_TODO @@ -1,21 +1,21 @@ -* make more stuff use IndexType -* make addExtra return Index using reflection * remove Cache from configurer * implement the build options -* get zig init template working * finish migrating the rest of the build steps +* inspect b4ffb402c082605c4b324e88120306fc8fb3cf32 diff and apply changes as needed (merge conflict) * make zig-pkg path root configurable in maker (make sure --system still works) * eliminate calls to getPath, getPath2, getPath3 +* [build system compile step data races with getGraph function](https://codeberg.org/ziglang/zig/issues/31397) * solve the TODOs added in this branch * get zig tests passing * test a bunch of third party projects / help people migrate -* refactor with DefaultingEnum -* inspect b4ffb402c082605c4b324e88120306fc8fb3cf32 diff and apply changes as needed (merge conflict) -* https://codeberg.org/ziglang/zig/issues/31397 -* restore the generated_compiler_rt_dyn_lib hack? -* run args -* https://codeberg.org/ziglang/zig/pulls/30762 + * get the target from the parent process instead +* [handle missing cache hits when chaining two run steps](https://codeberg.org/ziglang/zig/pulls/30762) +* [Absolute and cwd-relative paths in build cache](https://codeberg.org/ziglang/zig/issues/32097) + +* make more stuff use IndexType +* make addExtra return Index using reflection +* refactor with DefaultingEnum ## Followup Issues * reduce the size of Maker.Step.Extended (make Run smaller) probably by using an arena per make @@ -28,4 +28,17 @@ ## Release Notes -* run args are all together now, not observable in configure phase whether run args are provided + +run args are all together now, not observable in configure phase whether run args are provided. + +```zig +if (b.args) |args| { + run_cmd.addArgs(args); +} +``` + +⬇️ + +```zig +run_cmd.addBuildPositionals(); +``` diff --git a/lib/compiler/Maker/Step/Run.zig b/lib/compiler/Maker/Step/Run.zig index 45030305a53fe32cc8ced68b94a21d914097b6c3..0c2c6c70db9fd7953780165cb6541f761bba8052 100644 --- a/lib/compiler/Maker/Step/Run.zig +++ b/lib/compiler/Maker/Step/Run.zig @@ -72,6 +72,7 @@ pub fn make( var any_dep_files = false; var any_output_args = false; + var any_cli_positionals = false; for (conf_run.args.slice) |arg_index| { const arg = arg_index.get(conf); @@ -176,10 +177,11 @@ pub fn make( }); argv_list.items.len += 1; }, - .cli_rest_positionals => { + .cli_positionals => { + any_cli_positionals = true; if (maker.run_args) |run_args| { try argv_list.appendSlice(gpa, run_args); - for (run_args) |s| man.hash.addBytes(s); + man.hash.addListOfBytes(run_args); } }, } @@ -236,13 +238,14 @@ pub fn make( } // Whether the Run step has side effects *other than* updating the output arguments. - const has_side_effects = conf_run.flags.has_side_effects or switch (conf_run.flags.stdio) { - .infer_from_args => !any_output_args and - conf_run.captured_stdout.value == null and - conf_run.captured_stderr.value == null, - .inherit => true, - .check, .zig_test => false, - }; + const has_side_effects = conf_run.flags.has_side_effects or any_cli_positionals or + switch (conf_run.flags.stdio) { + .infer_from_args => !any_output_args and + conf_run.captured_stdout.value == null and + conf_run.captured_stderr.value == null, + .inherit => true, + .check, .zig_test => false, + }; if (!has_side_effects and try step.cacheHitAndWatch(maker, &man)) { // Cache hit; skip running command. @@ -1596,7 +1599,7 @@ pub fn rerunInFuzzMode( }, .output_file => unreachable, .output_directory => unreachable, - .cli_rest_positionals => unreachable, + .cli_positionals => unreachable, } } diff --git a/lib/compiler/configurer.zig b/lib/compiler/configurer.zig index e99f6384eff5e0fb08a6769a99e51f32a4ca8b4b..9feb803677b63865f3136fd51b76d3c3b511d045 100644 --- a/lib/compiler/configurer.zig +++ b/lib/compiler/configurer.zig @@ -492,9 +492,9 @@ const Serialize = struct { .producer = .{ .value = null }, .generated = .{ .value = a.generated_file }, }, - .cli_rest_positionals => .{ + .cli_positionals => .{ .flags = .{ - .tag = .cli_rest_positionals, + .tag = .cli_positionals, .prefix = false, .suffix = false, .basename = false, diff --git a/lib/init/build.zig b/lib/init/build.zig index 88c42f760edbfd93979f34b4a40e0e46a79ee40b..d4477c46a9db58da05f18dcca149979f5128a503 100644 --- a/lib/init/build.zig +++ b/lib/init/build.zig @@ -111,9 +111,7 @@ pub fn build(b: *std.Build) void { // This allows the user to pass arguments to the application in the build // command itself, like this: `zig build run -- arg1 arg2 etc` - if (b.args) |args| { - run_cmd.addArgs(args); - } + run_cmd.addCliPositionals(); // Creates an executable that will run `test` blocks from the provided module. // Here `mod` needs to define a target, which is why earlier we made sure to diff --git a/lib/std/Build/Configuration.zig b/lib/std/Build/Configuration.zig index eb1736db1d26d6505d04846abffc2b4b18f5c2c0..47aa0566af6236539e73872e9c0ccac9725d597c 100644 --- a/lib/std/Build/Configuration.zig +++ b/lib/std/Build/Configuration.zig @@ -567,7 +567,7 @@ pub const Step = extern struct { file_content, output_file, output_directory, - cli_rest_positionals, + cli_positionals, }; pub const Index = IndexType(@This()); diff --git a/lib/std/Build/Step/Run.zig b/lib/std/Build/Step/Run.zig index c91d8334fe40c0ddf3ccdc015643e3021b6e40ea..478df688f34a84ae07284718e31dcf29061ee35a 100644 --- a/lib/std/Build/Step/Run.zig +++ b/lib/std/Build/Step/Run.zig @@ -142,7 +142,7 @@ pub const Arg = union(enum) { output_file_dep: *Output, output_directory: *Output, /// The arguments passed after "--" on the "zig build" CLI. - cli_rest_positionals, + cli_positionals, }; pub const PrefixedArtifact = struct { @@ -491,16 +491,44 @@ pub fn addPrefixedDepFileOutputArg(run: *Run, prefix: []const u8, basename: []co return .{ .generated = .{ .index = dep_file.generated_file } }; } +/// Appends the contents of `arg`, verbatim, to the command line that will be +/// passed to the process being run. +/// +/// If `arg` is an input file, `addFileInput` (or related function) must be +/// used instead to ensure correct cache behavior. +/// +/// If `arg` is an output file, `addOutputFileArg` (or related function) must +/// be used instead to ensure correct cache behavior. pub fn addArg(run: *Run, arg: []const u8) void { const graph = run.step.owner.graph; const arena = graph.arena; run.argv.append(arena, .{ .bytes = graph.dupeString(arg) }) catch @panic("OOM"); } +/// Appends each of `args`, verbatim, to the command line that will be passed +/// to the process being run. +/// +/// If any element of `args` is an input file, `addFileInput` must be used +/// instead to ensure correct cache behavior. +/// +/// If any element of `args` is an output file, `addOutputFileArg` (or related +/// function) must be used instead to ensure correct cache behavior. pub fn addArgs(run: *Run, args: []const []const u8) void { for (args) |arg| run.addArg(arg); } +/// Any extra positional args are provided to the `zig build` command, they are +/// appended here. This causes the step to be considered to have side effects, +/// disabling caching. +/// +/// In the example command `zig build run -- arg1 arg2`, "arg1" and "arg2" will +/// be passed to the process being run. +pub fn addCliPositionals(run: *Run) void { + const graph = run.step.owner.graph; + const arena = graph.arena; + run.argv.append(arena, .cli_positionals) catch @panic("OOM"); +} + pub fn setStdIn(run: *Run, stdin: StdIn) void { switch (stdin) { .lazy_path => |lazy_path| lazy_path.addStepDependencies(&run.step), -- 2.54.0