Skip to content

Commit af02b71

Browse files
committed
Support setting properties under exec groups on platforms.
Previously, exec groups were rejected on platforms. This change allows setting arbitrary exec groups on platforms, and applying only the relevant ones to actions after platform resolution. Also fixes a bug where the default execution platform for the "test" execution group did not take into account execution constraints set on targets. Builds upon a change by @ulfjack: ulfjack@342543f
1 parent 342543f commit af02b71

File tree

2 files changed

+57
-24
lines changed

2 files changed

+57
-24
lines changed

src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -475,14 +475,21 @@ public ActionOwner getActionOwner() {
475475
* rather than the host platform. Note that the value is not cached (on the assumption that this
476476
* method is only called once).
477477
*/
478-
public ActionOwner getTestActionOwner() {
478+
public ActionOwner getTestActionOwner(boolean useTargetPlatform) {
479+
PlatformInfo executionPlatform = null;
480+
if (toolchainContexts != null) {
481+
executionPlatform = useTargetPlatform
482+
? toolchainContexts.getTargetPlatform()
483+
: getExecutionPlatform();
484+
}
479485
ActionOwner actionOwner =
480486
createActionOwner(
481487
rule,
482488
aspectDescriptors,
483489
getConfiguration(),
484490
getExecProperties(TEST_RUNNER_EXEC_GROUP, execProperties),
485-
toolchainContexts == null ? null : toolchainContexts.getTargetPlatform());
491+
executionPlatform,
492+
ImmutableSet.of(TEST_RUNNER_EXEC_GROUP));
486493
return actionOwner;
487494
}
488495

@@ -501,7 +508,8 @@ public ActionOwner getActionOwner(String execGroup) {
501508
aspectDescriptors,
502509
getConfiguration(),
503510
getExecProperties(execGroup, execProperties),
504-
getExecutionPlatform(execGroup));
511+
getExecutionPlatform(execGroup),
512+
ImmutableSet.of(execGroup));
505513
actionOwners.put(execGroup, actionOwner);
506514
return actionOwner;
507515
}
@@ -608,12 +616,24 @@ public ImmutableList<Artifact> getBuildInfo(BuildInfoKey key) throws Interrupted
608616
AnalysisUtils.isStampingEnabled(this, getConfiguration()), key, getConfiguration());
609617
}
610618

619+
/**
620+
* Computes a map of exec properties given the execution platform, taking only properties in exec
621+
* groups that are applicable to this action.
622+
*/
611623
private static ImmutableMap<String, String> computeExecProperties(
612-
Map<String, String> targetExecProperties, @Nullable PlatformInfo executionPlatform) {
624+
Map<String, String> targetExecProperties,
625+
@Nullable PlatformInfo executionPlatform,
626+
Set<String> execGroups) {
613627
Map<String, String> execProperties = new HashMap<>();
614628

615629
if (executionPlatform != null) {
616-
execProperties.putAll(executionPlatform.execProperties());
630+
for (Map.Entry<String, Map<String, String>> execGroup :
631+
parseExecGroups(executionPlatform.execProperties()).entrySet()) {
632+
if (execGroup.getKey().equals(DEFAULT_EXEC_GROUP_NAME)
633+
|| execGroups.contains(execGroup.getKey())) {
634+
execProperties.putAll(execGroup.getValue());
635+
}
636+
}
617637
}
618638

619639
// If the same key occurs both in the platform and in target-specific properties, the
@@ -629,7 +649,8 @@ public static ActionOwner createActionOwner(
629649
ImmutableList<AspectDescriptor> aspectDescriptors,
630650
BuildConfiguration configuration,
631651
Map<String, String> targetExecProperties,
632-
@Nullable PlatformInfo executionPlatform) {
652+
@Nullable PlatformInfo executionPlatform,
653+
Set<String> execGroups) {
633654
return ActionOwner.create(
634655
rule.getLabel(),
635656
aspectDescriptors,
@@ -639,7 +660,7 @@ public static ActionOwner createActionOwner(
639660
configuration.checksum(),
640661
configuration.toBuildEvent(),
641662
configuration.isHostConfiguration() ? HOST_CONFIGURATION_PROGRESS_TAG : null,
642-
computeExecProperties(targetExecProperties, executionPlatform),
663+
computeExecProperties(targetExecProperties, executionPlatform, execGroups),
643664
executionPlatform);
644665
}
645666

@@ -1277,19 +1298,18 @@ private ImmutableMap<String, ImmutableMap<String, String>> parseExecProperties(
12771298
} else {
12781299
return parseExecProperties(
12791300
execProperties,
1280-
toolchainContexts == null ? ImmutableSet.of() : toolchainContexts.getExecGroups());
1301+
toolchainContexts == null ? null : toolchainContexts.getExecGroups());
12811302
}
12821303
}
12831304

12841305
/**
12851306
* Parse raw exec properties attribute value into a map of exec group names to their properties.
12861307
* The raw map can have keys of two forms: (1) 'property' and (2) 'exec_group_name.property'. The
1287-
* former get parsed into the target's default exec group, the latter get parsed into their
1288-
* relevant exec groups.
1308+
* former get parsed into the default exec group, the latter get parsed into their relevant exec
1309+
* groups.
12891310
*/
1290-
private static ImmutableMap<String, ImmutableMap<String, String>> parseExecProperties(
1291-
Map<String, String> rawExecProperties, Set<String> execGroups)
1292-
throws InvalidExecGroupException {
1311+
private static Map<String, Map<String, String>> parseExecGroups(
1312+
Map<String, String> rawExecProperties) {
12931313
Map<String, Map<String, String>> consolidatedProperties = new HashMap<>();
12941314
consolidatedProperties.put(DEFAULT_EXEC_GROUP_NAME, new HashMap<>());
12951315
for (Map.Entry<String, String> execProperty : rawExecProperties.entrySet()) {
@@ -1302,14 +1322,30 @@ private static ImmutableMap<String, ImmutableMap<String, String>> parseExecPrope
13021322
} else {
13031323
String execGroup = rawProperty.substring(0, delimiterIndex);
13041324
String property = rawProperty.substring(delimiterIndex + 1);
1305-
if (!execGroups.contains(execGroup)) {
1325+
consolidatedProperties.putIfAbsent(execGroup, new HashMap<>());
1326+
consolidatedProperties.get(execGroup).put(property, execProperty.getValue());
1327+
}
1328+
}
1329+
return consolidatedProperties;
1330+
}
1331+
1332+
/**
1333+
* Parse raw exec properties attribute value into a map of exec group names to their properties.
1334+
* If given a set of exec groups, validates all the exec groups in the map are applicable to the
1335+
* action.
1336+
*/
1337+
private static ImmutableMap<String, ImmutableMap<String, String>> parseExecProperties(
1338+
Map<String, String> rawExecProperties, @Nullable Set<String> execGroups)
1339+
throws InvalidExecGroupException {
1340+
Map<String, Map<String, String>> consolidatedProperties = parseExecGroups(rawExecProperties);
1341+
if (execGroups != null) {
1342+
for (Map.Entry<String, Map<String, String>> execGroup : consolidatedProperties.entrySet()) {
1343+
String execGroupName = execGroup.getKey();
1344+
if (!execGroupName.equals(DEFAULT_EXEC_GROUP_NAME) && !execGroups.contains(execGroupName)) {
13061345
throw new InvalidExecGroupException(
13071346
String.format(
1308-
"Tried to set exec property '%s' for non-existent exec group '%s'.",
1309-
property, execGroup));
1347+
"Tried to set properties for non-existent exec group '%s'.", execGroupName));
13101348
}
1311-
consolidatedProperties.putIfAbsent(execGroup, new HashMap<>());
1312-
consolidatedProperties.get(execGroup).put(property, execProperty.getValue());
13131349
}
13141350
}
13151351

src/main/java/com/google/devtools/build/lib/analysis/test/TestActionBuilder.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,9 @@ private TestParams createTestAction(int shards) throws InterruptedException {
331331
ImmutableList.Builder<Artifact> coverageArtifacts = ImmutableList.builder();
332332
ImmutableList.Builder<ActionInput> testOutputs = ImmutableList.builder();
333333

334-
ActionOwner actionOwner = testConfiguration.useTargetPlatformForTests()
335-
? ruleContext.getTestActionOwner()
336-
: ruleContext.getActionOwner(TEST_RUNNER_EXEC_GROUP);
337-
if (actionOwner == null) {
338-
actionOwner = ruleContext.getActionOwner();
339-
}
334+
ActionOwner actionOwner = ruleContext.getTestActionOwner(
335+
testConfiguration.useTargetPlatformForTests());
336+
340337
// Use 1-based indices for user friendliness.
341338
for (int shard = 0; shard < shardRuns; shard++) {
342339
String shardDir = shardRuns > 1 ? String.format("shard_%d_of_%d", shard + 1, shards) : null;

0 commit comments

Comments
 (0)