Skip to content

Commit

Permalink
Merge pull request #204 from aherrmann/env
Browse files Browse the repository at this point in the history
feat: add `env` and `env_inherit` attributes
  • Loading branch information
aherrmann authored Jan 30, 2024
2 parents f7330fb + 6e3d29b commit d278000
Show file tree
Hide file tree
Showing 16 changed files with 268 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import %workspace%/.bazelrc.remote
# To update these lines, execute
# `bazel run @rules_bazel_integration_test//tools:update_deleted_packages`
# docs: https://bazel.build/reference/command-line-reference#flag--deleted_packages
build --deleted_packages=e2e/workspace,e2e/workspace/c-sources,e2e/workspace/configure-mode,e2e/workspace/configure-target,e2e/workspace/configure-threaded,e2e/workspace/data-dependencies,e2e/workspace/embed-file,e2e/workspace/include-dependencies,e2e/workspace/include-dependencies/zig-include,e2e/workspace/include-dependencies/zig-include-define,e2e/workspace/include-dependencies/zig-include-isystem,e2e/workspace/include-dependencies/zig-std-include,e2e/workspace/link-dependencies,e2e/workspace/link-dependencies/shared-library,e2e/workspace/link-dependencies/static-library,e2e/workspace/linker-script,e2e/workspace/location-expansion,e2e/workspace/multiple-sources-and-packages-test,e2e/workspace/multiple-sources-binary,e2e/workspace/simple-binary,e2e/workspace/simple-library,e2e/workspace/simple-shared-library,e2e/workspace/simple-test,e2e/workspace/transitive-zig-packages-binary,e2e/workspace/transitive-zig-packages-binary/hello-world,e2e/workspace/transitive-zig-packages-binary/hello-world/data,e2e/workspace/transitive-zig-packages-binary/hello-world/data/hello,e2e/workspace/transitive-zig-packages-binary/hello-world/data/world,e2e/workspace/transitive-zig-packages-binary/hello-world/io,e2e/workspace/zig-package-binary,e2e/workspace/zig-package-binary/data,e2e/workspace/zig-package-binary/io,zig/tests/integration_tests/workspace,zig/tests/integration_tests/workspace/custom_interpreter
query --deleted_packages=e2e/workspace,e2e/workspace/c-sources,e2e/workspace/configure-mode,e2e/workspace/configure-target,e2e/workspace/configure-threaded,e2e/workspace/data-dependencies,e2e/workspace/embed-file,e2e/workspace/include-dependencies,e2e/workspace/include-dependencies/zig-include,e2e/workspace/include-dependencies/zig-include-define,e2e/workspace/include-dependencies/zig-include-isystem,e2e/workspace/include-dependencies/zig-std-include,e2e/workspace/link-dependencies,e2e/workspace/link-dependencies/shared-library,e2e/workspace/link-dependencies/static-library,e2e/workspace/linker-script,e2e/workspace/location-expansion,e2e/workspace/multiple-sources-and-packages-test,e2e/workspace/multiple-sources-binary,e2e/workspace/simple-binary,e2e/workspace/simple-library,e2e/workspace/simple-shared-library,e2e/workspace/simple-test,e2e/workspace/transitive-zig-packages-binary,e2e/workspace/transitive-zig-packages-binary/hello-world,e2e/workspace/transitive-zig-packages-binary/hello-world/data,e2e/workspace/transitive-zig-packages-binary/hello-world/data/hello,e2e/workspace/transitive-zig-packages-binary/hello-world/data/world,e2e/workspace/transitive-zig-packages-binary/hello-world/io,e2e/workspace/zig-package-binary,e2e/workspace/zig-package-binary/data,e2e/workspace/zig-package-binary/io,zig/tests/integration_tests/workspace,zig/tests/integration_tests/workspace/custom_interpreter
build --deleted_packages=e2e/workspace,e2e/workspace/c-sources,e2e/workspace/configure-mode,e2e/workspace/configure-target,e2e/workspace/configure-threaded,e2e/workspace/data-dependencies,e2e/workspace/embed-file,e2e/workspace/env-attr,e2e/workspace/include-dependencies,e2e/workspace/include-dependencies/zig-include,e2e/workspace/include-dependencies/zig-include-define,e2e/workspace/include-dependencies/zig-include-isystem,e2e/workspace/include-dependencies/zig-std-include,e2e/workspace/link-dependencies,e2e/workspace/link-dependencies/shared-library,e2e/workspace/link-dependencies/static-library,e2e/workspace/linker-script,e2e/workspace/location-expansion,e2e/workspace/multiple-sources-and-packages-test,e2e/workspace/multiple-sources-binary,e2e/workspace/simple-binary,e2e/workspace/simple-library,e2e/workspace/simple-shared-library,e2e/workspace/simple-test,e2e/workspace/transitive-zig-packages-binary,e2e/workspace/transitive-zig-packages-binary/hello-world,e2e/workspace/transitive-zig-packages-binary/hello-world/data,e2e/workspace/transitive-zig-packages-binary/hello-world/data/hello,e2e/workspace/transitive-zig-packages-binary/hello-world/data/world,e2e/workspace/transitive-zig-packages-binary/hello-world/io,e2e/workspace/zig-package-binary,e2e/workspace/zig-package-binary/data,e2e/workspace/zig-package-binary/io,zig/tests/integration_tests/workspace,zig/tests/integration_tests/workspace/custom_interpreter,zig/tests/integration_tests/workspace/env-attr
query --deleted_packages=e2e/workspace,e2e/workspace/c-sources,e2e/workspace/configure-mode,e2e/workspace/configure-target,e2e/workspace/configure-threaded,e2e/workspace/data-dependencies,e2e/workspace/embed-file,e2e/workspace/env-attr,e2e/workspace/include-dependencies,e2e/workspace/include-dependencies/zig-include,e2e/workspace/include-dependencies/zig-include-define,e2e/workspace/include-dependencies/zig-include-isystem,e2e/workspace/include-dependencies/zig-std-include,e2e/workspace/link-dependencies,e2e/workspace/link-dependencies/shared-library,e2e/workspace/link-dependencies/static-library,e2e/workspace/linker-script,e2e/workspace/location-expansion,e2e/workspace/multiple-sources-and-packages-test,e2e/workspace/multiple-sources-binary,e2e/workspace/simple-binary,e2e/workspace/simple-library,e2e/workspace/simple-shared-library,e2e/workspace/simple-test,e2e/workspace/transitive-zig-packages-binary,e2e/workspace/transitive-zig-packages-binary/hello-world,e2e/workspace/transitive-zig-packages-binary/hello-world/data,e2e/workspace/transitive-zig-packages-binary/hello-world/data/hello,e2e/workspace/transitive-zig-packages-binary/hello-world/data/world,e2e/workspace/transitive-zig-packages-binary/hello-world/io,e2e/workspace/zig-package-binary,e2e/workspace/zig-package-binary/data,e2e/workspace/zig-package-binary/io,zig/tests/integration_tests/workspace,zig/tests/integration_tests/workspace/custom_interpreter,zig/tests/integration_tests/workspace/env-attr

# Load any settings specific to the current user.
# .bazelrc.user should appear in .gitignore so that settings are not shared with team members
Expand Down
8 changes: 6 additions & 2 deletions docs/rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions e2e/workspace/env-attr/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("@rules_zig//zig:defs.bzl", "zig_binary", "zig_test")

zig_binary(
name = "binary",
env = {"ENV_ATTR": "42"},
main = "main.zig",
)

genrule(
name = "output",
outs = ["output.actual"],
cmd = "ENV_GENRULE=21 $(execpath :binary) > $(OUTS)",
tools = [":binary"],
)

diff_test(
name = "output_test",
size = "small",
file1 = ":output.expected",
file2 = ":output.actual",
)

zig_test(
name = "test",
size = "small",
env = {"ENV_ATTR": "42"},
main = "main.zig",
)
32 changes: 32 additions & 0 deletions e2e/workspace/env-attr/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const std = @import("std");

pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();

const allocator = arena.allocator();

const env_attr: ?[]const u8 = std.process.getEnvVarOwned(allocator, "ENV_ATTR") catch |e| switch (e) {
error.EnvironmentVariableNotFound => null,
else => |e_| return e_,
};
defer if (env_attr) |value| allocator.free(value);

const env_genrule: ?[]const u8 = std.process.getEnvVarOwned(allocator, "ENV_GENRULE") catch |e| switch (e) {
error.EnvironmentVariableNotFound => null,
else => |e_| return e_,
};
defer if (env_genrule) |value| allocator.free(value);

if (env_attr) |value|
try std.io.getStdOut().writer().print("ENV_ATTR: '{s}'\n", .{value});
if (env_genrule) |value|
try std.io.getStdOut().writer().print("ENV_GENRULE: '{s}'\n", .{value});
}

test "bazel controlled env var" {
const value = try std.process.getEnvVarOwned(std.testing.allocator, "ENV_ATTR");
defer std.testing.allocator.free(value);

try std.testing.expectEqualStrings("42", value);
}
1 change: 1 addition & 0 deletions e2e/workspace/env-attr/output.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ENV_GENRULE: '21'
3 changes: 3 additions & 0 deletions e2e/workspace/location-expansion/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ zig_test(
],
csrcs = ["main.c"],
data = ["data.txt"],
env = {
"TARGET": "$(TARGET)",
},
main = "main.zig",
)
15 changes: 12 additions & 3 deletions e2e/workspace/location-expansion/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,34 @@ extern const rlocationpath: [*:0]const u8;
extern const target: [*:0]const u8;
extern const zig_target: [*:0]const u8;

test "RLOCATIONPATH is set" {
test "-DRLOCATIONPATH is set" {
try std.testing.expectStringEndsWith(
std.mem.sliceTo(rlocationpath, 0),
"location-expansion/data.txt",
);
}

test "TARGET is set" {
test "-DTARGET is set" {
try std.testing.expectEqualStrings(
"//location-expansion:test",
std.mem.sliceTo(target, 0),
);
}

test "ZIG_TARGET is set" {
test "-DZIG_TARGET is set" {
const actual_target = try builtin.target.linuxTriple(std.testing.allocator);
defer std.testing.allocator.free(actual_target);
try std.testing.expectEqualStrings(
actual_target,
std.mem.sliceTo(zig_target, 0),
);
}

test "Env-var TARGET is set" {
const value = try std.process.getEnvVarOwned(std.testing.allocator, "TARGET");
defer std.testing.allocator.free(value);
try std.testing.expectEqualStrings(
"//location-expansion:test",
value,
);
}
49 changes: 46 additions & 3 deletions zig/private/common/zig_build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ATTRS = {
providers = [ZigPackageInfo],
),
"cdeps": attr.label_list(
doc = """
doc = """\
C dependencies providing headers to include and libraries to link against, typically `cc_library` targets.
Note, if you need to include C or C++ standard library headers and encounter errors of the following form:
Expand Down Expand Up @@ -91,6 +91,33 @@ Then you may need to list `@rules_zig//zig/lib:libc` or `@rules_zig//zig/lib:lib
),
}

BINARY_ATTRS = {
"env": attr.string_dict(
doc = """\
Additional environment variables to set when executed by `bazel run`.
Subject to location expansion.
NOTE: The environment variables are not set when you run the target outside of Bazel (for example, by manually executing the binary in bazel-bin/).
""",
mandatory = False,
),
}

TEST_ATTRS = {
"env": attr.string_dict(
doc = """\
Additional environment variables to set when executed by `bazel run` or `bazel test`.
Subject to location expansion.
""",
mandatory = False,
),
"env_inherit": attr.string_list(
doc = """\
Environment variables to inherit from external environment when executed by `bazel test`.
""",
mandatory = False,
),
}

TOOLCHAINS = [
"//zig:toolchain_type",
"//zig/target:toolchain_type",
Expand Down Expand Up @@ -261,6 +288,8 @@ def zig_build_impl(ctx, *, kind):
execution_requirements = {tag: "" for tag in ctx.attr.tags},
)

providers = []

default = DefaultInfo(
executable = executable,
files = files,
Expand All @@ -271,5 +300,19 @@ def zig_build_impl(ctx, *, kind):
transitive_runfiles = transitive_runfiles,
),
)

return [default]
providers.append(default)

if kind in ["zig_binary", "zig_test"]:
run_environment = RunEnvironmentInfo(
environment = dict(zip(ctx.attr.env.keys(), location_expansion(
ctx = ctx,
targets = location_targets,
outputs = outputs,
attribute_name = "env",
strings = ctx.attr.env.values(),
))),
inherited_environment = getattr(ctx.attr, "env_inherit", []),
)
providers.append(run_environment)

return providers
3 changes: 2 additions & 1 deletion zig/private/zig_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

load(
"//zig/private/common:zig_build.bzl",
"BINARY_ATTRS",
"zig_build_impl",
COMMON_ATTRS = "ATTRS",
COMMON_TOOLCHAINS = "TOOLCHAINS",
Expand Down Expand Up @@ -31,7 +32,7 @@ zig_binary(
```
"""

ATTRS = COMMON_ATTRS
ATTRS = COMMON_ATTRS | BINARY_ATTRS

TOOLCHAINS = COMMON_TOOLCHAINS

Expand Down
3 changes: 2 additions & 1 deletion zig/private/zig_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

load(
"//zig/private/common:zig_build.bzl",
"TEST_ATTRS",
"zig_build_impl",
COMMON_ATTRS = "ATTRS",
COMMON_TOOLCHAINS = "TOOLCHAINS",
Expand Down Expand Up @@ -30,7 +31,7 @@ zig_test(
```
"""

ATTRS = COMMON_ATTRS
ATTRS = COMMON_ATTRS | TEST_ATTRS

TOOLCHAINS = COMMON_TOOLCHAINS

Expand Down
10 changes: 10 additions & 0 deletions zig/tests/integration_tests/integration_testing.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub const BitContext = struct {
argv: []const []const u8,
print_on_error: bool = true,
omit_bzlmod_flag: bool = false,
extra_env: ?*const std.process.EnvMap = null,
},
) !BazelResult {
var argc = 1 + args.argv.len;
Expand All @@ -68,10 +69,19 @@ pub const BitContext = struct {
if (self.bzlmod_enabled and !args.omit_bzlmod_flag) {
argv[argc - 1] = "--enable_bzlmod";
}
var env_map: ?std.process.EnvMap = null;
defer if (env_map) |*env| env.deinit();
if (args.extra_env) |extra_env| {
env_map = try std.process.getEnvMap(std.testing.allocator);
var iter = extra_env.iterator();
while (iter.next()) |item|
try env_map.?.put(item.key_ptr.*, item.value_ptr.*);
}
const result = try std.ChildProcess.exec(.{
.allocator = std.testing.allocator,
.argv = argv,
.cwd = self.workspace_path,
.env_map = if (env_map) |*env| env else null,
});
const success = switch (result.term) {
.Exited => |code| code == 0,
Expand Down
Loading

0 comments on commit d278000

Please sign in to comment.