Skip to content

Commit

Permalink
Step.Run: Add ability to specify LazyPaths as values for env_map
Browse files Browse the repository at this point in the history
  • Loading branch information
der-teufel-programming committed Jul 9, 2024
1 parent 854e86c commit fb928cd
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/std/Build/Step/Run.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ argv: std.ArrayListUnmanaged(Arg),
/// Use `setCwd` to set the initial current working directory
cwd: ?Build.LazyPath,

/// Override this field to modify the environment, or use setEnvironmentVariable
/// Override this field to modify the environment, or use `setEnvironmentVariable`
env_map: ?*EnvMap,

/// Use `addLazyEnvVar` instead of modyfing this directly
lazy_env_map: std.ArrayListUnmanaged(LazyEnvVar),

/// When `true` prevents `ZIG_PROGRESS` environment variable from being passed
/// to the child process, which otherwise would be used for the child to send
/// progress updates to the parent.
Expand Down Expand Up @@ -145,6 +148,11 @@ pub const Output = struct {
basename: []const u8,
};

pub const LazyEnvVar = struct {
key: []const u8,
value: std.Build.LazyPath,
};

pub fn create(owner: *std.Build, name: []const u8) *Run {
const run = owner.allocator.create(Run) catch @panic("OOM");
run.* = .{
Expand All @@ -157,6 +165,7 @@ pub fn create(owner: *std.Build, name: []const u8) *Run {
.argv = .{},
.cwd = null,
.env_map = null,
.lazy_env_map = .{},
.disable_zig_progress = false,
.stdio = .infer_from_args,
.stdin = .none,
Expand Down Expand Up @@ -434,6 +443,13 @@ fn getEnvMapInternal(run: *Run) *EnvMap {
};
}

pub fn addLazyEnvVar(run: *Run, key: []const u8, lp: std.Build.LazyPath) void {
const b = run.step.owner;

run.lazy_env_map.append(b.allocator, .{ .key = key, .value = lp }) catch @panic("OOM");
lp.addStepDependencies(&run.step);
}

pub fn setEnvironmentVariable(run: *Run, key: []const u8, value: []const u8) void {
const b = run.step.owner;
const env_map = run.getEnvMap();
Expand Down Expand Up @@ -592,6 +608,12 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
var man = b.graph.cache.obtain();
defer man.deinit();

for (run.lazy_env_map.items) |lazy_env_var| {
const path = lazy_env_var.value.getPath2(b, step);
run.setEnvironmentVariable(lazy_env_var.key, path);
man.hash.addBytes(path);
}

for (run.argv.items) |arg| {
switch (arg) {
.bytes => |bytes| {
Expand Down

0 comments on commit fb928cd

Please sign in to comment.