16
16
17
17
import com .google .common .annotations .VisibleForTesting ;
18
18
import com .google .common .base .Preconditions ;
19
+ import com .google .common .collect .ImmutableSet ;
20
+ import com .google .devtools .build .lib .actions .ActionEnvironment ;
19
21
import com .google .devtools .build .lib .actions .Artifact ;
20
22
import com .google .devtools .build .lib .actions .CommandLine ;
21
23
import com .google .devtools .build .lib .analysis .SourceManifestAction .ManifestType ;
34
36
import com .google .devtools .build .lib .vfs .Path ;
35
37
import com .google .devtools .build .lib .vfs .PathFragment ;
36
38
import java .util .Collection ;
39
+ import java .util .LinkedHashSet ;
37
40
import java .util .List ;
38
41
import java .util .Map ;
39
42
import java .util .Set ;
43
+ import java .util .TreeMap ;
40
44
import javax .annotation .Nullable ;
41
45
42
46
/**
@@ -85,6 +89,7 @@ public final class RunfilesSupport {
85
89
private final boolean buildRunfileLinks ;
86
90
private final boolean runfilesEnabled ;
87
91
private final CommandLine args ;
92
+ private final ActionEnvironment actionEnvironment ;
88
93
89
94
/**
90
95
* Creates the RunfilesSupport helper with the given executable and runfiles.
@@ -94,7 +99,11 @@ public final class RunfilesSupport {
94
99
* @param runfiles the runfiles
95
100
*/
96
101
private static RunfilesSupport create (
97
- RuleContext ruleContext , Artifact executable , Runfiles runfiles , CommandLine args ) {
102
+ RuleContext ruleContext ,
103
+ Artifact executable ,
104
+ Runfiles runfiles ,
105
+ CommandLine args ,
106
+ ActionEnvironment actionEnvironment ) {
98
107
Artifact owningExecutable = Preconditions .checkNotNull (executable );
99
108
boolean createManifest = ruleContext .getConfiguration ().buildRunfilesManifests ();
100
109
boolean buildRunfileLinks = ruleContext .getConfiguration ().buildRunfileLinks ();
@@ -139,7 +148,8 @@ private static RunfilesSupport create(
139
148
owningExecutable ,
140
149
buildRunfileLinks ,
141
150
runfilesEnabled ,
142
- args );
151
+ args ,
152
+ actionEnvironment );
143
153
}
144
154
145
155
@ AutoCodec .Instantiator
@@ -152,7 +162,8 @@ private static RunfilesSupport create(
152
162
Artifact owningExecutable ,
153
163
boolean buildRunfileLinks ,
154
164
boolean runfilesEnabled ,
155
- CommandLine args ) {
165
+ CommandLine args ,
166
+ ActionEnvironment actionEnvironment ) {
156
167
this .runfiles = runfiles ;
157
168
this .runfilesInputManifest = runfilesInputManifest ;
158
169
this .runfilesManifest = runfilesManifest ;
@@ -161,6 +172,7 @@ private static RunfilesSupport create(
161
172
this .buildRunfileLinks = buildRunfileLinks ;
162
173
this .runfilesEnabled = runfilesEnabled ;
163
174
this .args = args ;
175
+ this .actionEnvironment = actionEnvironment ;
164
176
}
165
177
166
178
/** Returns the executable owning this RunfilesSupport. Only use from Starlark. */
@@ -394,14 +406,23 @@ public CommandLine getArgs() {
394
406
return args ;
395
407
}
396
408
409
+ /** Returns the immutable environment from the 'env' and 'env_inherit' attribute values. */
410
+ public ActionEnvironment getActionEnvironment () {
411
+ return actionEnvironment ;
412
+ }
413
+
397
414
/**
398
415
* Creates and returns a {@link RunfilesSupport} object for the given rule and executable. Note
399
416
* that this method calls back into the passed in rule to obtain the runfiles.
400
417
*/
401
418
public static RunfilesSupport withExecutable (
402
419
RuleContext ruleContext , Runfiles runfiles , Artifact executable ) {
403
420
return RunfilesSupport .create (
404
- ruleContext , executable , runfiles , computeArgs (ruleContext , CommandLine .EMPTY ));
421
+ ruleContext ,
422
+ executable ,
423
+ runfiles ,
424
+ computeArgs (ruleContext , CommandLine .EMPTY ),
425
+ computeActionEnvironment (ruleContext ));
405
426
}
406
427
407
428
/**
@@ -411,7 +432,11 @@ public static RunfilesSupport withExecutable(
411
432
public static RunfilesSupport withExecutable (
412
433
RuleContext ruleContext , Runfiles runfiles , Artifact executable , List <String > appendingArgs ) {
413
434
return RunfilesSupport .create (
414
- ruleContext , executable , runfiles , computeArgs (ruleContext , CommandLine .of (appendingArgs )));
435
+ ruleContext ,
436
+ executable ,
437
+ runfiles ,
438
+ computeArgs (ruleContext , CommandLine .of (appendingArgs )),
439
+ computeActionEnvironment (ruleContext ));
415
440
}
416
441
417
442
/**
@@ -421,7 +446,11 @@ public static RunfilesSupport withExecutable(
421
446
public static RunfilesSupport withExecutable (
422
447
RuleContext ruleContext , Runfiles runfiles , Artifact executable , CommandLine appendingArgs ) {
423
448
return RunfilesSupport .create (
424
- ruleContext , executable , runfiles , computeArgs (ruleContext , appendingArgs ));
449
+ ruleContext ,
450
+ executable ,
451
+ runfiles ,
452
+ computeArgs (ruleContext , appendingArgs ),
453
+ computeActionEnvironment (ruleContext ));
425
454
}
426
455
427
456
private static CommandLine computeArgs (RuleContext ruleContext , CommandLine additionalArgs ) {
@@ -434,6 +463,30 @@ private static CommandLine computeArgs(RuleContext ruleContext, CommandLine addi
434
463
ruleContext .getExpander ().withDataLocations ().tokenized ("args" ), additionalArgs );
435
464
}
436
465
466
+ private static ActionEnvironment computeActionEnvironment (RuleContext ruleContext ) {
467
+ if (!ruleContext .getRule ().isAttrDefined ("env" , Type .STRING_DICT )
468
+ && !ruleContext .getRule ().isAttrDefined ("env_inherit" , Type .STRING_LIST )) {
469
+ return ActionEnvironment .EMPTY ;
470
+ }
471
+ TreeMap <String , String > fixedEnv = new TreeMap <>();
472
+ Set <String > inheritedEnv = new LinkedHashSet <>();
473
+ if (ruleContext .isAttrDefined ("env" , Type .STRING_DICT )) {
474
+ Expander expander = ruleContext .getExpander ().withDataLocations ();
475
+ for (Map .Entry <String , String > entry :
476
+ ruleContext .attributes ().get ("env" , Type .STRING_DICT ).entrySet ()) {
477
+ fixedEnv .put (entry .getKey (), expander .expand ("env" , entry .getValue ()));
478
+ }
479
+ }
480
+ if (ruleContext .isAttrDefined ("env_inherit" , Type .STRING_LIST )) {
481
+ for (String key : ruleContext .attributes ().get ("env_inherit" , Type .STRING_LIST )) {
482
+ if (!fixedEnv .containsKey (key )) {
483
+ inheritedEnv .add (key );
484
+ }
485
+ }
486
+ }
487
+ return ActionEnvironment .create (fixedEnv , ImmutableSet .copyOf (inheritedEnv ));
488
+ }
489
+
437
490
/** Returns the path of the input manifest of {@code runfilesDir}. */
438
491
public static Path inputManifestPath (Path runfilesDir ) {
439
492
return FileSystemUtils .replaceExtension (runfilesDir , INPUT_MANIFEST_EXT );
0 commit comments