17
17
18
18
import com .google .common .collect .ImmutableMap ;
19
19
import com .google .common .collect .ImmutableSet ;
20
- import com .google .devtools .build .lib .skyframe .serialization .autocodec .AutoCodec ;
21
20
import com .google .devtools .build .lib .util .Fingerprint ;
22
21
import java .util .LinkedHashMap ;
23
22
import java .util .Map ;
44
43
* action cache), such that Bazel knows exactly which actions it needs to rerun, and does not have
45
44
* to reanalyze the entire dependency graph.
46
45
*/
47
- @ AutoCodec
48
46
public final class ActionEnvironment {
49
47
50
- /** A map of environment variables. */
48
+ /**
49
+ * A map of environment variables together with a list of variables to inherit from the shell
50
+ * environment.
51
+ */
51
52
public interface EnvironmentVariables {
52
53
53
54
/**
54
- * Returns the environment variables as a map.
55
+ * Returns the fixed environment variables as a map.
56
+ *
57
+ * <p>WARNING: this allocates additional objects if the underlying implementation is a {@link
58
+ * CompoundEnvironmentVariables}; use sparingly.
59
+ */
60
+ ImmutableMap <String , String > getFixedEnvironment ();
61
+
62
+ /**
63
+ * Returns the inherited environment variables as a set.
55
64
*
56
- * <p>WARNING: this allocations additional objects if the underlying implementation is a {@link
65
+ * <p>WARNING: this allocates additional objects if the underlying implementation is a {@link
57
66
* CompoundEnvironmentVariables}; use sparingly.
58
67
*/
59
- ImmutableMap <String , String > toMap ();
68
+ ImmutableSet <String > getInheritedEnvironment ();
60
69
61
70
default boolean isEmpty () {
62
- return toMap ().isEmpty ();
71
+ return getFixedEnvironment (). isEmpty () && getInheritedEnvironment ().isEmpty ();
63
72
}
64
73
65
74
default int size () {
66
- return toMap ().size ();
75
+ return getFixedEnvironment (). size () + getInheritedEnvironment ().size ();
67
76
}
68
77
}
69
78
@@ -72,11 +81,21 @@ default int size() {
72
81
* allocation a new map.
73
82
*/
74
83
static class CompoundEnvironmentVariables implements EnvironmentVariables {
84
+
85
+ static EnvironmentVariables create (
86
+ Map <String , String > fixedVars , Set <String > inheritedVars , EnvironmentVariables base ) {
87
+ if (fixedVars .isEmpty () && inheritedVars .isEmpty () && base .isEmpty ()) {
88
+ return EMPTY_ENVIRONMENT_VARIABLES ;
89
+ }
90
+ return new CompoundEnvironmentVariables (fixedVars , inheritedVars , base );
91
+ }
92
+
75
93
private final EnvironmentVariables current ;
76
94
private final EnvironmentVariables base ;
77
95
78
- CompoundEnvironmentVariables (Map <String , String > vars , EnvironmentVariables base ) {
79
- this .current = new SimpleEnvironmentVariables (vars );
96
+ private CompoundEnvironmentVariables (
97
+ Map <String , String > fixedVars , Set <String > inheritedVars , EnvironmentVariables base ) {
98
+ this .current = SimpleEnvironmentVariables .create (fixedVars , inheritedVars );
80
99
this .base = base ;
81
100
}
82
101
@@ -86,48 +105,62 @@ public boolean isEmpty() {
86
105
}
87
106
88
107
@ Override
89
- public ImmutableMap <String , String > toMap () {
90
- Map <String , String > result = new LinkedHashMap <>();
91
- result .putAll (base .toMap ());
92
- result .putAll (current .toMap ());
108
+ public ImmutableMap <String , String > getFixedEnvironment () {
109
+ LinkedHashMap <String , String > result = new LinkedHashMap <>();
110
+ result .putAll (base .getFixedEnvironment ());
111
+ result .putAll (current .getFixedEnvironment ());
93
112
return ImmutableMap .copyOf (result );
94
113
}
114
+
115
+ @ Override
116
+ public ImmutableSet <String > getInheritedEnvironment () {
117
+ ImmutableSet .Builder <String > result = new ImmutableSet .Builder <>();
118
+ result .addAll (base .getInheritedEnvironment ());
119
+ result .addAll (current .getInheritedEnvironment ());
120
+ return result .build ();
121
+ }
95
122
}
96
123
97
124
/** A simple {@link EnvironmentVariables}. */
98
125
static class SimpleEnvironmentVariables implements EnvironmentVariables {
99
126
100
- static EnvironmentVariables create (Map <String , String > vars ) {
101
- if (vars .isEmpty ()) {
127
+ static EnvironmentVariables create (Map <String , String > fixedVars , Set < String > inheritedVars ) {
128
+ if (fixedVars . isEmpty () && inheritedVars .isEmpty ()) {
102
129
return EMPTY_ENVIRONMENT_VARIABLES ;
103
130
}
104
- return new SimpleEnvironmentVariables (vars );
131
+ return new SimpleEnvironmentVariables (fixedVars , inheritedVars );
105
132
}
106
133
107
- private final ImmutableMap <String , String > vars ;
134
+ private final ImmutableMap <String , String > fixedVars ;
135
+ private final ImmutableSet <String > inheritedVars ;
108
136
109
- private SimpleEnvironmentVariables (Map <String , String > vars ) {
110
- this .vars = ImmutableMap .copyOf (vars );
137
+ private SimpleEnvironmentVariables (Map <String , String > fixedVars , Set <String > inheritedVars ) {
138
+ this .fixedVars = ImmutableMap .copyOf (fixedVars );
139
+ this .inheritedVars = ImmutableSet .copyOf (inheritedVars );
140
+ }
141
+
142
+ @ Override
143
+ public ImmutableMap <String , String > getFixedEnvironment () {
144
+ return fixedVars ;
111
145
}
112
146
113
147
@ Override
114
- public ImmutableMap <String , String > toMap () {
115
- return vars ;
148
+ public ImmutableSet <String > getInheritedEnvironment () {
149
+ return inheritedVars ;
116
150
}
117
151
}
118
152
119
153
/** An empty {@link EnvironmentVariables}. */
120
154
public static final EnvironmentVariables EMPTY_ENVIRONMENT_VARIABLES =
121
- new SimpleEnvironmentVariables (ImmutableMap .of ());
155
+ new SimpleEnvironmentVariables (ImmutableMap .of (), ImmutableSet . of () );
122
156
123
157
/**
124
158
* An empty environment, mainly for testing. Production code should never use this, but instead
125
159
* get the proper environment from the current configuration.
126
160
*/
127
161
// TODO(ulfjack): Migrate all production code to use the proper action environment, and then make
128
162
// this @VisibleForTesting or rename it to clarify.
129
- public static final ActionEnvironment EMPTY =
130
- new ActionEnvironment (EMPTY_ENVIRONMENT_VARIABLES , ImmutableSet .of ());
163
+ public static final ActionEnvironment EMPTY = new ActionEnvironment (EMPTY_ENVIRONMENT_VARIABLES );
131
164
132
165
/**
133
166
* Splits the given map into a map of variables with a fixed value, and a set of variables that
@@ -147,60 +180,66 @@ public static ActionEnvironment split(Map<String, String> env) {
147
180
inheritedEnv .add (key );
148
181
}
149
182
}
150
- return create (new SimpleEnvironmentVariables (fixedEnv ), ImmutableSet . copyOf ( inheritedEnv ));
183
+ return create (SimpleEnvironmentVariables . create (fixedEnv , inheritedEnv ));
151
184
}
152
185
153
- private final EnvironmentVariables fixedEnv ;
154
- private final ImmutableSet <String > inheritedEnv ;
186
+ private final EnvironmentVariables vars ;
155
187
156
- private ActionEnvironment (EnvironmentVariables fixedEnv , ImmutableSet <String > inheritedEnv ) {
157
- this .fixedEnv = fixedEnv ;
158
- this .inheritedEnv = inheritedEnv ;
188
+ private ActionEnvironment (EnvironmentVariables vars ) {
189
+ this .vars = vars ;
159
190
}
160
191
161
192
/**
162
193
* Creates a new action environment. The order in which the environments are combined is
163
194
* undefined, so callers need to take care that the key set of the {@code fixedEnv} map and the
164
195
* set of {@code inheritedEnv} elements are disjoint.
165
196
*/
166
- @ AutoCodec .Instantiator
167
- public static ActionEnvironment create (
168
- EnvironmentVariables fixedEnv , ImmutableSet <String > inheritedEnv ) {
169
- if (fixedEnv .isEmpty () && inheritedEnv .isEmpty ()) {
197
+ private static ActionEnvironment create (EnvironmentVariables vars ) {
198
+ if (vars .isEmpty ()) {
170
199
return EMPTY ;
171
200
}
172
- return new ActionEnvironment (fixedEnv , inheritedEnv );
201
+ return new ActionEnvironment (vars );
173
202
}
174
203
175
204
public static ActionEnvironment create (
176
205
Map <String , String > fixedEnv , ImmutableSet <String > inheritedEnv ) {
177
- return new ActionEnvironment (SimpleEnvironmentVariables .create (fixedEnv ) , inheritedEnv );
206
+ return ActionEnvironment . create (SimpleEnvironmentVariables .create (fixedEnv , inheritedEnv ) );
178
207
}
179
208
180
209
public static ActionEnvironment create (Map <String , String > fixedEnv ) {
181
- return new ActionEnvironment ( new SimpleEnvironmentVariables (fixedEnv ) , ImmutableSet .of ());
210
+ return ActionEnvironment . create ( SimpleEnvironmentVariables . create (fixedEnv , ImmutableSet .of () ));
182
211
}
183
212
184
213
/**
185
214
* Returns a copy of the environment with the given fixed variables added to it, <em>overwriting
186
215
* any existing occurrences of those variables</em>.
187
216
*/
188
- public ActionEnvironment addFixedVariables (Map <String , String > vars ) {
189
- return new ActionEnvironment (new CompoundEnvironmentVariables (vars , fixedEnv ), inheritedEnv );
217
+ public ActionEnvironment addFixedVariables (Map <String , String > fixedVars ) {
218
+ return ActionEnvironment .create (
219
+ CompoundEnvironmentVariables .create (fixedVars , ImmutableSet .of (), vars ));
220
+ }
221
+
222
+ /**
223
+ * Returns a copy of the environment with the given fixed and inherited variables added to it,
224
+ * <em>overwriting any existing occurrences of those variables</em>.
225
+ */
226
+ public ActionEnvironment addVariables (Map <String , String > fixedVars , Set <String > inheritedVars ) {
227
+ return ActionEnvironment .create (
228
+ CompoundEnvironmentVariables .create (fixedVars , inheritedVars , vars ));
190
229
}
191
230
192
231
/** Returns the combined size of the fixed and inherited environments. */
193
232
public int size () {
194
- return fixedEnv . size () + inheritedEnv .size ();
233
+ return vars .size ();
195
234
}
196
235
197
236
/**
198
237
* Returns the 'fixed' part of the environment, i.e., those environment variables that are set to
199
238
* fixed values and their values. This should only be used for testing and to compute the cache
200
239
* keys of actions. Use {@link #resolve} instead to get the complete environment.
201
240
*/
202
- public EnvironmentVariables getFixedEnv () {
203
- return fixedEnv ;
241
+ public ImmutableMap < String , String > getFixedEnv () {
242
+ return vars . getFixedEnvironment () ;
204
243
}
205
244
206
245
/**
@@ -210,7 +249,7 @@ public EnvironmentVariables getFixedEnv() {
210
249
* get the complete environment.
211
250
*/
212
251
public ImmutableSet <String > getInheritedEnv () {
213
- return inheritedEnv ;
252
+ return vars . getInheritedEnvironment () ;
214
253
}
215
254
216
255
/**
@@ -221,8 +260,8 @@ public ImmutableSet<String> getInheritedEnv() {
221
260
*/
222
261
public void resolve (Map <String , String > result , Map <String , String > clientEnv ) {
223
262
checkNotNull (clientEnv );
224
- result .putAll (fixedEnv . toMap ());
225
- for (String var : inheritedEnv ) {
263
+ result .putAll (vars . getFixedEnvironment ());
264
+ for (String var : vars . getInheritedEnvironment () ) {
226
265
String value = clientEnv .get (var );
227
266
if (value != null ) {
228
267
result .put (var , value );
@@ -231,7 +270,7 @@ public void resolve(Map<String, String> result, Map<String, String> clientEnv) {
231
270
}
232
271
233
272
public void addTo (Fingerprint f ) {
234
- f .addStringMap (fixedEnv . toMap ());
235
- f .addStrings (inheritedEnv );
273
+ f .addStringMap (vars . getFixedEnvironment ());
274
+ f .addStrings (vars . getInheritedEnvironment () );
236
275
}
237
276
}
0 commit comments