Skip to content

Commit e82beda

Browse files
mai93copybara-github
authored andcommitted
Expose actions fixed environment variables specified through feature configuration to Starlark
Some fixed environment variables are added to the actions environment [like CppCompile action](https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java#L877) based on features configuration [code](https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/rules/cpp/CompileCommandLine.java#L66). These variables can be available in the analysis phase. This CL adds a flag `experimental_get_fixed_configured_action_env` to return these variables in `action.env`. This is needed to allow the use of aspects to output C++ actions information as a replacement for `print_action` command. Addresses: #10376 PiperOrigin-RevId: 464088598 Change-Id: Id1c4e2badf5078d4d51f733aee8762411fd3b9ba
1 parent 7694cf7 commit e82beda

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed

src/main/java/com/google/devtools/build/lib/actions/AbstractAction.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.google.devtools.build.lib.events.Event;
3737
import com.google.devtools.build.lib.events.EventHandler;
3838
import com.google.devtools.build.lib.packages.AspectDescriptor;
39+
import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions;
3940
import com.google.devtools.build.lib.server.FailureDetails.Execution.Code;
4041
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization;
4142
import com.google.devtools.build.lib.starlarkbuildapi.ActionApi;
@@ -55,6 +56,7 @@
5556
import net.starlark.java.eval.EvalException;
5657
import net.starlark.java.eval.Printer;
5758
import net.starlark.java.eval.Sequence;
59+
import net.starlark.java.eval.StarlarkSemantics;
5860

5961
/**
6062
* Abstract implementation of Action which implements basic functionality: the inputs, outputs, and
@@ -684,8 +686,16 @@ public Dict<String, String> getExecutionInfoDict() {
684686
}
685687

686688
@Override
687-
public Dict<String, String> getEnv() {
688-
return Dict.immutableCopyOf(env.getFixedEnv());
689+
public Dict<String, String> getEnv(StarlarkSemantics semantics) throws EvalException {
690+
if (semantics.getBool(BuildLanguageOptions.EXPERIMENTAL_GET_FIXED_CONFIGURED_ACTION_ENV)) {
691+
try {
692+
return Dict.immutableCopyOf(getEffectiveEnvironment(/*clientEnv=*/ ImmutableMap.of()));
693+
} catch (CommandLineExpansionException ex) {
694+
throw new EvalException(ex);
695+
}
696+
} else {
697+
return Dict.immutableCopyOf(env.getFixedEnv());
698+
}
689699
}
690700

691701
@Override

src/main/java/com/google/devtools/build/lib/actions/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ java_library(
8989
"//src/main/java/com/google/devtools/build/lib/concurrent",
9090
"//src/main/java/com/google/devtools/build/lib/events",
9191
"//src/main/java/com/google/devtools/build/lib/packages",
92+
"//src/main/java/com/google/devtools/build/lib/packages/semantics",
9293
"//src/main/java/com/google/devtools/build/lib/profiler",
9394
"//src/main/java/com/google/devtools/build/lib/profiler:google-auto-profiler-utils",
9495
"//src/main/java/com/google/devtools/build/lib/shell",

src/main/java/com/google/devtools/build/lib/packages/semantics/BuildLanguageOptions.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,17 @@ public final class BuildLanguageOptions extends OptionsBase {
585585
+ "'cfg = \"exec\"' instead.")
586586
public boolean incompatibleDisableStarlarkHostTransitions;
587587

588+
@Option(
589+
name = "experimental_get_fixed_configured_action_env",
590+
defaultValue = "false",
591+
documentationCategory = OptionDocumentationCategory.STARLARK_SEMANTICS,
592+
effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS},
593+
metadataTags = {OptionMetadataTag.EXPERIMENTAL},
594+
help =
595+
"If enabled, action.env will also return fixed environment variables"
596+
+ " specified through features configuration.")
597+
public boolean experimentalGetFixedConfiguredEnvironment;
598+
588599
/**
589600
* An interner to reduce the number of StarlarkSemantics instances. A single Blaze instance should
590601
* never accumulate a large number of these and being able to shortcut on object identity makes a
@@ -658,6 +669,9 @@ public StarlarkSemantics toStarlarkSemantics() {
658669
.setBool(
659670
INCOMPATIBLE_DISABLE_STARLARK_HOST_TRANSITIONS,
660671
incompatibleDisableStarlarkHostTransitions)
672+
.setBool(
673+
EXPERIMENTAL_GET_FIXED_CONFIGURED_ACTION_ENV,
674+
experimentalGetFixedConfiguredEnvironment)
661675
.build();
662676
return INTERNER.intern(semantics);
663677
}
@@ -730,6 +744,8 @@ public StarlarkSemantics toStarlarkSemantics() {
730744
"-incompatible_top_level_aspects_require_providers";
731745
public static final String INCOMPATIBLE_DISABLE_STARLARK_HOST_TRANSITIONS =
732746
"-incompatible_disable_starlark_host_transitions";
747+
public static final String EXPERIMENTAL_GET_FIXED_CONFIGURED_ACTION_ENV =
748+
"-experimental_get_fixed_configured_action_env";
733749

734750
// non-booleans
735751
public static final StarlarkSemantics.Key<String> EXPERIMENTAL_BUILTINS_BZL_PATH =

src/main/java/com/google/devtools/build/lib/starlarkbuildapi/ActionApi.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import net.starlark.java.eval.Dict;
2525
import net.starlark.java.eval.EvalException;
2626
import net.starlark.java.eval.Sequence;
27+
import net.starlark.java.eval.StarlarkSemantics;
2728
import net.starlark.java.eval.StarlarkValue;
2829

2930
/** Interface for actions in Starlark. */
@@ -125,11 +126,12 @@ public interface ActionApi extends StarlarkValue {
125126
@StarlarkMethod(
126127
name = "env",
127128
structField = true,
129+
useStarlarkSemantics = true,
128130
doc =
129131
"The 'fixed' environment variables for this action. This includes only environment"
130132
+ " settings which are explicitly set by the action definition, and thus omits"
131133
+ " settings which are only pre-set in the execution environment.")
132-
Dict<String, String> getEnv();
134+
Dict<String, String> getEnv(StarlarkSemantics semantics) throws EvalException;
133135

134136
@StarlarkMethod(
135137
name = "execution_info",

src/test/shell/bazel/cc_integration_test.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,4 +1457,53 @@ EOF
14571457
FOO=1 bazel test //pkg:foo_test &> "$TEST_log" || fail "Should have inherited FOO env."
14581458
}
14591459

1460+
function test_getting_compile_action_env_with_cctoolchain_config_features() {
1461+
[ "$PLATFORM" != "darwin" ] || return 0
1462+
1463+
mkdir -p package
1464+
1465+
cat > "package/lib.bzl" <<EOF
1466+
def _actions_test_impl(target, ctx):
1467+
compile_action = None
1468+
1469+
for action in target.actions:
1470+
if action.mnemonic in ["CppCompile", "ObjcCompile"]:
1471+
compile_action = action
1472+
1473+
print(compile_action.env)
1474+
return []
1475+
1476+
actions_test_aspect = aspect(implementation = _actions_test_impl)
1477+
EOF
1478+
1479+
cat > "package/x.cc" <<EOF
1480+
#include <stdio.h>
1481+
int main() {
1482+
printf("Hello\n");
1483+
}
1484+
EOF
1485+
1486+
cat > "package/BUILD" <<EOF
1487+
cc_binary(
1488+
name = "x",
1489+
srcs = ["x.cc"],
1490+
)
1491+
EOF
1492+
1493+
# Without the flag, the env should not return extra fixed variables
1494+
bazel build "package:x" \
1495+
--aspects="//package:lib.bzl%actions_test_aspect" &>"$TEST_log" \
1496+
|| fail "Build failed but should have succeeded"
1497+
1498+
expect_not_log "\"PWD\": \"/proc/self/cwd\""
1499+
1500+
# With the flag, the env should return extra fixed variables
1501+
bazel build "package:x" \
1502+
--aspects="//package:lib.bzl%actions_test_aspect" \
1503+
--experimental_get_fixed_configured_action_env &>"$TEST_log" \
1504+
|| fail "Build failed but should have succeeded"
1505+
1506+
expect_log "\"PWD\": \"/proc/self/cwd\""
1507+
}
1508+
14601509
run_suite "cc_integration_test"

0 commit comments

Comments
 (0)