Skip to content

Commit

Permalink
Hookup env attribute to RunEnvironmentInfo for cc_binary.
Browse files Browse the repository at this point in the history
Fixes bazelbuild#16167

PiperOrigin-RevId: 471811734
Change-Id: I7c904cc9056b59587e95704ef98b7d727f27f568
  • Loading branch information
trybka authored and Copybara-Service committed Sep 2, 2022
1 parent 5e80514 commit c4a1ab8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
Expand Up @@ -957,12 +957,19 @@ def _impl(ctx):

# We construct DefaultInfo here, as other cc_binary-like rules (cc_test) need
# a different DefaultInfo.
default_info = DefaultInfo(
other_providers.append(DefaultInfo(
files = binary_info.files,
runfiles = binary_info.runfiles,
executable = binary_info.executable,
)
other_providers.append(default_info)
))

# We construct RunEnvironmentInfo here as well.
other_providers.append(RunEnvironmentInfo(
environment = cc_helper.get_expanded_env(ctx, {}),
# cc_binary does not have env_inherit attr.
inherited_environment = [],
))

if ctx.fragments.cpp.enable_legacy_cc_provider():
# buildifier: disable=rule-impl-return
return struct(
Expand Down
31 changes: 31 additions & 0 deletions src/test/shell/bazel/cc_integration_test.sh
Expand Up @@ -1426,6 +1426,37 @@ EOF
FOO=1 bazel test //pkg:foo_test &> "$TEST_log" || fail "Should have inherited FOO env."
}

function test_env_attr_cc_binary() {
mkdir pkg
cat > pkg/BUILD <<EOF
cc_binary(
name = 'foo_bin_with_env',
srcs = ['foo_test.cc'],
env = {'FOO': 'bar'},
)
cc_binary(
name = 'foo_bin',
srcs = ['foo_test.cc'],
)
EOF

cat > pkg/foo_test.cc <<EOF
#include <stdlib.h>
int main() {
auto foo = getenv("FOO");
if (foo == nullptr) {
return 1;
}
return 0;
}
EOF

bazel run //pkg:foo_bin &> "$TEST_log" && fail "Did not fail as expected. ENV leak?" || true
bazel run //pkg:foo_bin_with_env &> "$TEST_log" || fail "Should have used env attr."
}

function test_getting_compile_action_env_with_cctoolchain_config_features() {
[ "$PLATFORM" != "darwin" ] || return 0

Expand Down

0 comments on commit c4a1ab8

Please sign in to comment.