Skip to content

Commit

Permalink
Use ToolchainTypeRequirement in ToolchainContext and related classes.
Browse files Browse the repository at this point in the history
Part of Optional Toolchains (bazelbuild#14726).
  • Loading branch information
katre committed Apr 19, 2022
1 parent b059011 commit e9c1372
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 96 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/google/devtools/build/lib/analysis/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,7 @@ java_library(
name = "resolved_toolchain_context",
srcs = ["ResolvedToolchainContext.java"],
deps = [
":config/toolchain_type_requirement",
":template_variable_info",
":toolchain_context",
"//src/main/java/com/google/devtools/build/lib/analysis/platform",
Expand Down Expand Up @@ -1091,6 +1092,7 @@ java_library(
name = "toolchain_context",
srcs = ["ToolchainContext.java"],
deps = [
":config/toolchain_type_requirement",
"//src/main/java/com/google/devtools/build/lib/analysis/platform",
"//src/main/java/com/google/devtools/build/lib/cmdline",
"//src/main/java/com/google/devtools/build/lib/skyframe:toolchain_context_key",
Expand Down Expand Up @@ -2296,6 +2298,7 @@ java_library(
name = "starlark/starlark_toolchain_context",
srcs = ["starlark/StarlarkToolchainContext.java"],
deps = [
":config/toolchain_type_requirement",
":resolved_toolchain_context",
"//src/main/java/com/google/devtools/build/lib/analysis/platform",
"//src/main/java/com/google/devtools/build/lib/cmdline",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static ResolvedToolchainContext load(
unloadedToolchainContext.key(),
unloadedToolchainContext.executionPlatform(),
unloadedToolchainContext.targetPlatform(),
unloadedToolchainContext.requiredToolchainTypes(),
unloadedToolchainContext.toolchainTypes(),
unloadedToolchainContext.resolvedToolchainLabels(),
// this:
targetDescription,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
package com.google.devtools.build.lib.analysis;

import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.analysis.config.ToolchainTypeRequirement;
import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
import com.google.devtools.build.lib.analysis.platform.ToolchainTypeInfo;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.skyframe.ToolchainContextKey;

Expand All @@ -31,7 +31,7 @@ public interface ToolchainContext {
PlatformInfo targetPlatform();

/** Returns the toolchain types that were requested. */
ImmutableSet<ToolchainTypeInfo> requiredToolchainTypes();
ImmutableSet<ToolchainTypeRequirement> toolchainTypes();

/** Returns the labels of the specific toolchains being used. */
ImmutableSet<Label> resolvedToolchainLabels();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public static ToolchainTypeRequirement strictest(
*/
public abstract boolean mandatory();

/** Returns a new Builder to copy this ToolchainTypeRequirement. */
public abstract Builder toBuilder();

/** A builder for a new {@link ToolchainTypeRequirement}. */
@AutoValue.Builder
public interface Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.google.auto.value.AutoValue;
import com.google.devtools.build.lib.analysis.ResolvedToolchainContext;
import com.google.devtools.build.lib.analysis.config.ToolchainTypeRequirement;
import com.google.devtools.build.lib.analysis.platform.ToolchainInfo;
import com.google.devtools.build.lib.analysis.platform.ToolchainTypeInfo;
import com.google.devtools.build.lib.cmdline.Label;
Expand Down Expand Up @@ -110,8 +111,8 @@ public ToolchainInfo getIndex(
"In %s, toolchain type %s was requested but only types [%s] are configured",
toolchainContext().targetDescription(),
toolchainTypeLabel,
toolchainContext().requiredToolchainTypes().stream()
.map(ToolchainTypeInfo::typeLabel)
toolchainContext().toolchainTypes().stream()
.map(ToolchainTypeRequirement::toolchainType)
.map(Label::toString)
.collect(joining(", ")));
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/skyframe/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2726,6 +2726,7 @@ java_library(
deps = [
":toolchain_context_key",
":unloaded_toolchain_context",
"//src/main/java/com/google/devtools/build/lib/analysis:config/toolchain_type_requirement",
"//src/main/java/com/google/devtools/build/lib/analysis/platform",
"//src/main/java/com/google/devtools/build/lib/cmdline",
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public boolean maybeReportCycle(
}
if (input.argument() instanceof SingleToolchainResolutionKey) {
Label toolchainType =
((SingleToolchainResolutionKey) input.argument()).toolchainTypeLabel();
((SingleToolchainResolutionKey) input.argument()).toolchainType().toolchainType();
return String.format("toolchain type %s", toolchainType);
}
if (input.argument() instanceof ToolchainContextKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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.ConfigMatchingProvider;
import com.google.devtools.build.lib.analysis.config.ToolchainTypeRequirement;
import com.google.devtools.build.lib.analysis.platform.ConstraintCollection;
import com.google.devtools.build.lib.analysis.platform.ConstraintSettingInfo;
import com.google.devtools.build.lib.analysis.platform.DeclaredToolchainInfo;
Expand Down Expand Up @@ -84,11 +85,11 @@ public SkyValue compute(SkyKey skyKey, Environment env)
key.debugTarget()
|| configuration
.getFragment(PlatformConfiguration.class)
.debugToolchainResolution(key.toolchainTypeLabel());
.debugToolchainResolution(key.toolchainType().toolchainType());

// Find the right one.
return resolveConstraints(
key.toolchainTypeLabel(),
key.toolchainType(),
key.availableExecutionPlatformKeys(),
key.targetPlatformKey(),
toolchains.registeredToolchains(),
Expand All @@ -103,7 +104,7 @@ public SkyValue compute(SkyKey skyKey, Environment env)
*/
@Nullable
private static SingleToolchainResolutionValue resolveConstraints(
Label toolchainTypeLabel,
ToolchainTypeRequirement toolchainType,
List<ConfiguredTargetKey> availableExecutionPlatformKeys,
ConfiguredTargetKey targetPlatformKey,
ImmutableList<DeclaredToolchainInfo> toolchains,
Expand Down Expand Up @@ -135,13 +136,15 @@ private static SingleToolchainResolutionValue resolveConstraints(
// check whether a platform has already been seen during processing.
Set<ConfiguredTargetKey> platformKeysSeen = new HashSet<>();
ImmutableMap.Builder<ConfiguredTargetKey, Label> builder = ImmutableMap.builder();
ToolchainTypeInfo toolchainType = null;
ToolchainTypeInfo toolchainTypeInfo = null;

// Pre-filter for the correct toolchain type. This simplifies the loop and makes debugging
// toolchain resolution much, much easier.
ImmutableList<DeclaredToolchainInfo> filteredToolchains =
toolchains.stream()
.filter(toolchain -> toolchain.toolchainType().typeLabel().equals(toolchainTypeLabel))
.filter(
toolchain ->
toolchain.toolchainType().typeLabel().equals(toolchainType.toolchainType()))
.collect(toImmutableList());

for (DeclaredToolchainInfo toolchain : filteredToolchains) {
Expand All @@ -155,7 +158,7 @@ private static SingleToolchainResolutionValue resolveConstraints(
debugMessage(
eventHandler,
" Type %s: %s platform %s: Rejected toolchain %s; mismatching config settings: %s",
toolchainTypeLabel,
toolchainType.toolchainType(),
"target",
targetPlatform.label(),
toolchain.toolchainLabel(),
Expand All @@ -169,7 +172,7 @@ private static SingleToolchainResolutionValue resolveConstraints(
toolchain.targetConstraints(),
"target",
targetPlatform,
toolchainTypeLabel,
toolchainType,
toolchain.toolchainLabel())) {
continue;
}
Expand All @@ -187,19 +190,19 @@ private static SingleToolchainResolutionValue resolveConstraints(
toolchain.execConstraints(),
"execution",
executionPlatform,
toolchainTypeLabel,
toolchainType,
toolchain.toolchainLabel())) {
continue;
}

debugMessage(
eventHandler,
" Type %s: target platform %s: execution %s: Selected toolchain %s",
toolchainTypeLabel,
toolchainType.toolchainType(),
targetPlatform.label(),
executionPlatformKey.getLabel(),
toolchain.toolchainLabel());
toolchainType = toolchain.toolchainType();
toolchainTypeInfo = toolchain.toolchainType();
builder.put(executionPlatformKey, toolchain.toolchainLabel());
platformKeysSeen.add(executionPlatformKey);
}
Expand All @@ -210,13 +213,12 @@ private static SingleToolchainResolutionValue resolveConstraints(
debugMessage(
eventHandler,
" Type %s: target platform %s: No toolchains found.",
toolchainTypeLabel,
toolchainType.toolchainType(),
targetPlatform.label());
throw new ToolchainResolutionFunctionException(
new NoToolchainFoundException(toolchainTypeLabel));
throw new ToolchainResolutionFunctionException(new NoToolchainFoundException(toolchainType));
}

return SingleToolchainResolutionValue.create(toolchainType, resolvedToolchainLabels);
return SingleToolchainResolutionValue.create(toolchainTypeInfo, resolvedToolchainLabels);
}

/**
Expand All @@ -242,7 +244,7 @@ private static boolean checkConstraints(
ConstraintCollection toolchainConstraints,
String platformType,
PlatformInfo platform,
Label toolchainTypeLabel,
ToolchainTypeRequirement toolchainType,
Label toolchainLabel) {

// Check every constraint_setting in either the toolchain or the platform.
Expand Down Expand Up @@ -283,7 +285,7 @@ private static boolean checkConstraints(
debugMessage(
eventHandler,
" Type %s: %s platform %s: Rejected toolchain %s%s%s",
toolchainTypeLabel,
toolchainType.toolchainType(),
platformType,
platform.label(),
toolchainLabel,
Expand All @@ -296,15 +298,17 @@ private static boolean checkConstraints(

/** Used to indicate that a toolchain was not found for the current request. */
public static final class NoToolchainFoundException extends NoSuchThingException {
private final Label missingToolchainTypeLabel;
private final ToolchainTypeRequirement missingToolchainType;

public NoToolchainFoundException(Label missingToolchainTypeLabel) {
super(String.format("no matching toolchain found for %s", missingToolchainTypeLabel));
this.missingToolchainTypeLabel = missingToolchainTypeLabel;
public NoToolchainFoundException(ToolchainTypeRequirement missingToolchainType) {
super(
String.format(
"no matching toolchain found for %s", missingToolchainType.toolchainType()));
this.missingToolchainType = missingToolchainType;
}

public Label missingToolchainTypeLabel() {
return missingToolchainTypeLabel;
public ToolchainTypeRequirement missingToolchainType() {
return missingToolchainType;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.analysis.config.ToolchainTypeRequirement;
import com.google.devtools.build.lib.analysis.platform.ToolchainTypeInfo;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.skyframe.SkyFunctionName;
Expand All @@ -35,26 +36,22 @@ public abstract class SingleToolchainResolutionValue implements SkyValue {
// A key representing the input data.
public static SingleToolchainResolutionKey key(
BuildConfigurationKey configurationKey,
Label toolchainTypeLabel,
ToolchainTypeRequirement toolchainType,
ConfiguredTargetKey targetPlatformKey,
List<ConfiguredTargetKey> availableExecutionPlatformKeys) {
return key(
configurationKey,
toolchainTypeLabel,
targetPlatformKey,
availableExecutionPlatformKeys,
false);
configurationKey, toolchainType, targetPlatformKey, availableExecutionPlatformKeys, false);
}

public static SingleToolchainResolutionKey key(
BuildConfigurationKey configurationKey,
Label toolchainTypeLabel,
ToolchainTypeRequirement toolchainType,
ConfiguredTargetKey targetPlatformKey,
List<ConfiguredTargetKey> availableExecutionPlatformKeys,
boolean debugTarget) {
return SingleToolchainResolutionKey.create(
configurationKey,
toolchainTypeLabel,
toolchainType,
targetPlatformKey,
availableExecutionPlatformKeys,
debugTarget);
Expand All @@ -71,7 +68,7 @@ public SkyFunctionName functionName() {

abstract BuildConfigurationKey configurationKey();

public abstract Label toolchainTypeLabel();
public abstract ToolchainTypeRequirement toolchainType();

abstract ConfiguredTargetKey targetPlatformKey();

Expand All @@ -81,13 +78,13 @@ public SkyFunctionName functionName() {

static SingleToolchainResolutionKey create(
BuildConfigurationKey configurationKey,
Label toolchainTypeLabel,
ToolchainTypeRequirement toolchainType,
ConfiguredTargetKey targetPlatformKey,
List<ConfiguredTargetKey> availableExecutionPlatformKeys,
boolean debugTarget) {
return new AutoValue_SingleToolchainResolutionValue_SingleToolchainResolutionKey(
configurationKey,
toolchainTypeLabel,
toolchainType,
targetPlatformKey,
ImmutableList.copyOf(availableExecutionPlatformKeys),
debugTarget);
Expand Down
Loading

0 comments on commit e9c1372

Please sign in to comment.