Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Step.Run: Add ability to specify LazyPaths as values for env_map #20554

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function must be called only during the configure phase. consider that the make function can be re-executed multiple times, so it should not call this every time.

man.hash.addBytes(path);
}

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