blob: 37f111ff437e88249c7eab6549248cc15ed96b02 [file] [log] [blame]
Klaus Aehligcd708c32016-09-15 13:48:47 +00001// Copyright 2016 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
janakr5fb2a482018-03-02 17:48:57 -080017import com.google.common.collect.Interner;
18import com.google.devtools.build.lib.concurrent.BlazeInterners;
19import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
20import com.google.devtools.build.skyframe.AbstractSkyKey;
Klaus Aehligcd708c32016-09-15 13:48:47 +000021import com.google.devtools.build.skyframe.SkyFunction;
janakr5fb2a482018-03-02 17:48:57 -080022import com.google.devtools.build.skyframe.SkyFunctionName;
Klaus Aehligcd708c32016-09-15 13:48:47 +000023import com.google.devtools.build.skyframe.SkyKey;
24import com.google.devtools.build.skyframe.SkyValue;
25import java.util.Map;
Damien Martin-Guillerezea459a02017-02-01 23:32:33 +000026import java.util.concurrent.atomic.AtomicReference;
Klaus Aehligcd708c32016-09-15 13:48:47 +000027import javax.annotation.Nullable;
28
29/** The Skyframe function that generates values for variables of the client environment. */
30public final class ClientEnvironmentFunction implements SkyFunction {
janakr5fb2a482018-03-02 17:48:57 -080031 public static Key key(String keyString) {
32 return ClientEnvironmentFunction.Key.create(keyString);
33 }
34
35 @AutoCodec.VisibleForSerialization
36 @AutoCodec
37 static class Key extends AbstractSkyKey<String> {
38 private static final Interner<Key> interner = BlazeInterners.newWeakInterner();
39
40 private Key(String arg) {
41 super(arg);
42 }
43
44 @AutoCodec.VisibleForSerialization
45 @AutoCodec.Instantiator
46 static Key create(String arg) {
47 return interner.intern(new Key(arg));
48 }
49
50 @Override
51 public SkyFunctionName functionName() {
52 return SkyFunctions.CLIENT_ENVIRONMENT_VARIABLE;
53 }
54 }
Klaus Aehligcd708c32016-09-15 13:48:47 +000055
Damien Martin-Guillerezea459a02017-02-01 23:32:33 +000056 private final AtomicReference<Map<String, String>> clientEnv;
57
carmid6a98282018-03-13 19:19:16 -070058 public ClientEnvironmentFunction(AtomicReference<Map<String, String>> clientEnv) {
Damien Martin-Guillerezea459a02017-02-01 23:32:33 +000059 this.clientEnv = clientEnv;
60 }
61
Klaus Aehligcd708c32016-09-15 13:48:47 +000062 @Nullable
63 @Override
64 public String extractTag(SkyKey skyKey) {
65 return null;
66 }
67
68 @Nullable
69 @Override
Damien Martin-Guillerezea459a02017-02-01 23:32:33 +000070 public SkyValue compute(SkyKey key, Environment env) throws InterruptedException {
71 return new ClientEnvironmentValue(clientEnv.get().get((String) key.argument()));
Klaus Aehligcd708c32016-09-15 13:48:47 +000072 }
73}