blob: 7e5988455a513836641ef3b25183de5c9b80f963 [file] [log] [blame]
Damien Martin-Guillerez777f3af2017-02-08 17:22:02 +00001// Copyright 2017 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.devtools.build.lib.skyframe;
16
Fabian Meumertzheim49af3d32023-08-15 09:29:05 -070017import static com.google.common.collect.ImmutableSet.toImmutableSet;
18
salma-samy64554a32023-07-18 04:42:43 -070019import com.google.common.collect.ImmutableMap;
emilyguo21b0ba02022-03-16 17:07:40 -070020import com.google.devtools.build.lib.bugreport.BugReport;
Googlerde9d1f52023-11-06 09:28:56 -080021import com.google.devtools.build.lib.skyframe.serialization.VisibleForSerialization;
janakr5fb2a482018-03-02 17:48:57 -080022import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
23import com.google.devtools.build.skyframe.AbstractSkyKey;
Damien Martin-Guillerez777f3af2017-02-08 17:22:02 +000024import com.google.devtools.build.skyframe.SkyFunction;
janakr5fb2a482018-03-02 17:48:57 -080025import com.google.devtools.build.skyframe.SkyFunctionName;
Damien Martin-Guillerez777f3af2017-02-08 17:22:02 +000026import com.google.devtools.build.skyframe.SkyKey;
27import com.google.devtools.build.skyframe.SkyValue;
Googler18319052022-10-14 14:27:35 -070028import com.google.devtools.build.skyframe.SkyframeLookupResult;
Damien Martin-Guillerez777f3af2017-02-08 17:22:02 +000029import java.util.Map;
Fabian Meumertzheimced79982024-05-24 16:00:49 -070030import java.util.Optional;
Fabian Meumertzheim49af3d32023-08-15 09:29:05 -070031import java.util.Set;
Damien Martin-Guillerez777f3af2017-02-08 17:22:02 +000032import javax.annotation.Nullable;
33
34/**
35 * Skyframe function that provides the effective value for a client environment variable. This will
36 * either be the value coming from the default client environment, or the value coming from the
37 * --action_env flag, if the variable's value is explicitly set.
38 */
39public final class ActionEnvironmentFunction implements SkyFunction {
Damien Martin-Guillerez777f3af2017-02-08 17:22:02 +000040 @Nullable
41 @Override
42 public SkyValue compute(SkyKey skyKey, Environment env) throws InterruptedException {
43 Map<String, String> actionEnv = PrecomputedValue.ACTION_ENV.get(env);
44 String key = (String) skyKey.argument();
45 if (actionEnv.containsKey(key) && actionEnv.get(key) != null) {
46 return new ClientEnvironmentValue(actionEnv.get(key));
47 }
janakr5fb2a482018-03-02 17:48:57 -080048 return env.getValue(ClientEnvironmentFunction.key(key));
Damien Martin-Guillerez777f3af2017-02-08 17:22:02 +000049 }
50
salma-samy64554a32023-07-18 04:42:43 -070051 /** Returns the SkyKey to invoke this function for the environment variable {@code variable}. */
janakr5fb2a482018-03-02 17:48:57 -080052 public static Key key(String variable) {
53 return Key.create(variable);
54 }
55
Googlerde9d1f52023-11-06 09:28:56 -080056 @VisibleForSerialization
janakr5fb2a482018-03-02 17:48:57 -080057 @AutoCodec
58 static class Key extends AbstractSkyKey<String> {
Googler1a5c3162023-03-16 10:46:51 -070059 private static final SkyKeyInterner<Key> interner = SkyKey.newInterner();
janakr5fb2a482018-03-02 17:48:57 -080060
61 private Key(String arg) {
62 super(arg);
63 }
64
Googler0e9b1462024-01-29 08:29:26 -080065 private static Key create(String arg) {
janakr5fb2a482018-03-02 17:48:57 -080066 return interner.intern(new Key(arg));
67 }
68
Googler0e9b1462024-01-29 08:29:26 -080069 @VisibleForSerialization
70 @AutoCodec.Interner
71 static Key intern(Key key) {
72 return interner.intern(key);
73 }
74
janakr5fb2a482018-03-02 17:48:57 -080075 @Override
76 public SkyFunctionName functionName() {
77 return SkyFunctions.ACTION_ENVIRONMENT_VARIABLE;
78 }
Googler1a5c3162023-03-16 10:46:51 -070079
80 @Override
81 public SkyKeyInterner<Key> getSkyKeyInterner() {
82 return interner;
83 }
Damien Martin-Guillerez777f3af2017-02-08 17:22:02 +000084 }
85
86 /**
87 * Returns a map of environment variable key => values, getting them from Skyframe. Returns null
88 * if and only if some dependencies from Skyframe still need to be resolved.
89 */
emilyguo21b0ba02022-03-16 17:07:40 -070090 @Nullable
Fabian Meumertzheimced79982024-05-24 16:00:49 -070091 public static ImmutableMap<String, Optional<String>> getEnvironmentView(
92 Environment env, Set<String> keys) throws InterruptedException {
Fabian Meumertzheim49af3d32023-08-15 09:29:05 -070093 var skyframeKeys = keys.stream().map(ActionEnvironmentFunction::key).collect(toImmutableSet());
Googler18319052022-10-14 14:27:35 -070094 SkyframeLookupResult values = env.getValuesAndExceptions(skyframeKeys);
Damien Martin-Guillerez777f3af2017-02-08 17:22:02 +000095 if (env.valuesMissing()) {
96 return null;
97 }
salma-samy64554a32023-07-18 04:42:43 -070098
Fabian Meumertzheimced79982024-05-24 16:00:49 -070099 ImmutableMap.Builder<String, Optional<String>> result =
100 ImmutableMap.builderWithExpectedSize(skyframeKeys.size());
Damien Martin-Guillerez777f3af2017-02-08 17:22:02 +0000101 for (SkyKey key : skyframeKeys) {
Googler18319052022-10-14 14:27:35 -0700102 ClientEnvironmentValue value = (ClientEnvironmentValue) values.get(key);
emilyguo21b0ba02022-03-16 17:07:40 -0700103 if (value == null) {
104 BugReport.sendBugReport(
105 new IllegalStateException(
106 "ClientEnvironmentValue " + key + " was missing, this should never happen"));
107 return null;
108 }
Fabian Meumertzheimced79982024-05-24 16:00:49 -0700109 result.put(key.argument().toString(), Optional.ofNullable(value.getValue()));
Damien Martin-Guillerez777f3af2017-02-08 17:22:02 +0000110 }
salma-samy64554a32023-07-18 04:42:43 -0700111 return result.buildOrThrow();
Damien Martin-Guillerez777f3af2017-02-08 17:22:02 +0000112 }
113}