Skip to content

Commit

Permalink
Update toolchain resolution to use ToolchainTypeRequirement.
Browse files Browse the repository at this point in the history
Actually updating the logic to check mandatory/optional is still TODO.

Part of bazelbuild#14726.
  • Loading branch information
katre committed Feb 10, 2022
1 parent 207c31b commit 576c3c9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/google/devtools/build/lib/skyframe/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/analysis:config/fragment_options",
"//src/main/java/com/google/devtools/build/lib/analysis:config/host_transition",
"//src/main/java/com/google/devtools/build/lib/analysis:config/invalid_configuration_exception",
"//src/main/java/com/google/devtools/build/lib/analysis:config/toolchain_type_requirement",
"//src/main/java/com/google/devtools/build/lib/analysis:config/transitions/configuration_transition",
"//src/main/java/com/google/devtools/build/lib/analysis:config/transitions/no_transition",
"//src/main/java/com/google/devtools/build/lib/analysis:config/transitions/null_transition",
Expand Down Expand Up @@ -2529,6 +2530,7 @@ java_library(
deps = [
":build_configuration",
":sky_functions",
"//src/main/java/com/google/devtools/build/lib/analysis:config/toolchain_type_requirement",
"//src/main/java/com/google/devtools/build/lib/cmdline",
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
"//third_party:auto_value",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.analysis.config.ToolchainTypeRequirement;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.ExtendedEventHandler;
Expand Down Expand Up @@ -89,11 +89,13 @@ public boolean maybeReportCycle(
return String.format("toolchain type %s", toolchainType);
}
if (input.argument() instanceof ToolchainContextKey) {
ImmutableSet<Label> toolchainTypes =
((ToolchainContextKey) input.argument()).requiredToolchainTypeLabels();
return String.format(
"toolchain types %s",
toolchainTypes.stream().map(Label::toString).collect(joining(", ")));
ToolchainContextKey toolchainContextKey = (ToolchainContextKey) input.argument();
String toolchainTypes =
toolchainContextKey.toolchainTypes().stream()
.map(ToolchainTypeRequirement::toolchainType)
.map(Label::toString)
.collect(joining(", "));
return String.format("toolchain types %s", toolchainTypes);
}

throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
// limitations under the License.
package com.google.devtools.build.lib.skyframe;

import static com.google.common.collect.ImmutableSet.toImmutableSet;

import com.google.auto.value.AutoValue;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.analysis.config.ToolchainTypeRequirement;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.skyframe.SkyFunctionName;
import com.google.devtools.build.skyframe.SkyKey;
Expand All @@ -31,7 +34,7 @@ public abstract class ToolchainContextKey implements SkyKey {
/** Returns a new {@link Builder}. */
public static Builder key() {
return new AutoValue_ToolchainContextKey.Builder()
.requiredToolchainTypeLabels(ImmutableSet.of())
.toolchainTypes(ImmutableSet.of())
.execConstraintLabels(ImmutableSet.of())
.forceExecutionPlatform(Optional.empty())
.debugTarget(false);
Expand All @@ -44,7 +47,7 @@ public SkyFunctionName functionName() {

abstract BuildConfigurationKey configurationKey();

abstract ImmutableSet<Label> requiredToolchainTypeLabels();
abstract ImmutableSet<ToolchainTypeRequirement> toolchainTypes();

abstract ImmutableSet<Label> execConstraintLabels();

Expand All @@ -57,9 +60,21 @@ public SkyFunctionName functionName() {
public interface Builder {
Builder configurationKey(BuildConfigurationKey key);

Builder requiredToolchainTypeLabels(ImmutableSet<Label> requiredToolchainTypeLabels);
// TODO(katre): Remove this once all callers use toolchainTypes.
default Builder requiredToolchainTypeLabels(Label... requiredToolchainTypeLabels) {
return this.requiredToolchainTypeLabels(ImmutableSet.copyOf(requiredToolchainTypeLabels));
}

// TODO(katre): Remove this once all callers use toolchainTypes.
default Builder requiredToolchainTypeLabels(ImmutableSet<Label> requiredToolchainTypeLabels) {
ImmutableSet<ToolchainTypeRequirement> toolchainTypeRequirements =
requiredToolchainTypeLabels.stream()
.map(label -> ToolchainTypeRequirement.builder().toolchainType(label).build())
.collect(toImmutableSet());
return this.toolchainTypes(toolchainTypeRequirements);
}

Builder requiredToolchainTypeLabels(Label... requiredToolchainTypeLabels);
Builder toolchainTypes(ImmutableSet<ToolchainTypeRequirement> toolchainTypes);

Builder execConstraintLabels(ImmutableSet<Label> execConstraintLabels);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.common.collect.Table;
import com.google.devtools.build.lib.analysis.PlatformConfiguration;
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
import com.google.devtools.build.lib.analysis.config.ToolchainTypeRequirement;
import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo;
import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
import com.google.devtools.build.lib.analysis.platform.ToolchainTypeInfo;
Expand Down Expand Up @@ -83,13 +84,17 @@ public UnloadedToolchainContext compute(SkyKey skyKey, Environment env)
key.debugTarget()
|| configuration
.getFragment(PlatformConfiguration.class)
.debugToolchainResolution(key.requiredToolchainTypeLabels());
.debugToolchainResolution(
key.toolchainTypes().stream()
.map(ToolchainTypeRequirement::toolchainType)
.collect(toImmutableSet()));

// Load the configured target for the toolchain types to ensure that they are valid and
// resolve aliases.
ImmutableMap<Label, ToolchainTypeInfo> resolvedToolchainTypes =
loadToolchainTypes(env, configuration, key.requiredToolchainTypeLabels());
loadToolchainTypes(env, configuration, key.toolchainTypes());
builder.setRequestedLabelToToolchainType(resolvedToolchainTypes);
// TODO(katre): Need to correct for possible aliases, map to mandatory/optional.
ImmutableSet<Label> resolvedToolchainTypeLabels =
resolvedToolchainTypes.values().stream()
.map(ToolchainTypeInfo::typeLabel)
Expand Down Expand Up @@ -149,10 +154,11 @@ public UnloadedToolchainContext compute(SkyKey skyKey, Environment env)
private static ImmutableMap<Label, ToolchainTypeInfo> loadToolchainTypes(
Environment environment,
BuildConfigurationValue configuration,
ImmutableSet<Label> requestedToolchainTypeLabels)
ImmutableSet<ToolchainTypeRequirement> toolchainTypeRequirements)
throws InvalidToolchainTypeException, InterruptedException, ValueMissingException {
ImmutableSet<ConfiguredTargetKey> toolchainTypeKeys =
requestedToolchainTypeLabels.stream()
toolchainTypeRequirements.stream()
.map(ToolchainTypeRequirement::toolchainType)
.map(
label ->
ConfiguredTargetKey.builder()
Expand Down Expand Up @@ -361,7 +367,7 @@ private static void determineToolchainImplementations(
NoMatchingPlatformException, UnresolvedToolchainsException,
InvalidToolchainLabelException {

// Find the toolchains for the required toolchain types.
// Find the toolchains for the requested toolchain types.
List<SingleToolchainResolutionKey> registeredToolchainKeys = new ArrayList<>();
for (Label toolchainTypeLabel : requiredToolchainTypeLabels) {
registeredToolchainKeys.add(
Expand Down Expand Up @@ -420,6 +426,7 @@ private static void determineToolchainImplementations(
ImmutableSet<ToolchainTypeInfo> requiredToolchainTypes = requiredToolchainTypesBuilder.build();

// Find and return the first execution platform which has all required toolchains.
// TODO(katre): Handle mandatory/optional.
Optional<ConfiguredTargetKey> selectedExecutionPlatformKey =
findExecutionPlatformForToolchains(
requiredToolchainTypes,
Expand Down

0 comments on commit 576c3c9

Please sign in to comment.