Skip to content

Commit

Permalink
Move UnloadedToolchainContext.load to ResolvedToolchainContext.
Browse files Browse the repository at this point in the history
Also pass target description directly to load().

Part of work on execution transitions, bazelbuild#7935.
  • Loading branch information
katre committed Apr 15, 2019
1 parent 19a151f commit 924f447
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 253 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
import com.google.devtools.build.lib.analysis.platform.PlatformProviderUtils;
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;
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.rules.AliasConfiguredTarget;
import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndData;
import com.google.devtools.build.lib.skyframe.ToolchainException;
import com.google.devtools.build.lib.skylarkbuildapi.ToolchainContextApi;
import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter;
import com.google.devtools.build.lib.skylarkinterface.StarlarkContext;
Expand All @@ -46,8 +50,64 @@
@ThreadSafe
public abstract class ResolvedToolchainContext implements ToolchainContextApi, ToolchainContext {

static Builder builder() {
return new AutoValue_ResolvedToolchainContext.Builder();
/**
* Finishes preparing the {@link ResolvedToolchainContext} by finding the specific toolchain
* providers to be used for each toolchain type.
*/
public static ResolvedToolchainContext load(
UnloadedToolchainContext unloadedToolchainContext,
String targetDescription,
Iterable<ConfiguredTargetAndData> toolchainTargets)
throws ToolchainException {

ResolvedToolchainContext.Builder toolchainContext =
new AutoValue_ResolvedToolchainContext.Builder()
.setTargetDescription(targetDescription)
.setExecutionPlatform(unloadedToolchainContext.executionPlatform())
.setTargetPlatform(unloadedToolchainContext.targetPlatform())
.setRequiredToolchainTypes(unloadedToolchainContext.requiredToolchainTypes())
.setResolvedToolchainLabels(unloadedToolchainContext.resolvedToolchainLabels());

ImmutableMap.Builder<ToolchainTypeInfo, ToolchainInfo> toolchains =
new ImmutableMap.Builder<>();
ImmutableList.Builder<TemplateVariableInfo> templateVariableProviders =
new ImmutableList.Builder<>();
for (ConfiguredTargetAndData target : toolchainTargets) {
Label discoveredLabel;
// Aliases are in toolchainTypeToResolved by the original alias label, not via the final
// target's label.
if (target.getConfiguredTarget() instanceof AliasConfiguredTarget) {
discoveredLabel = ((AliasConfiguredTarget) target.getConfiguredTarget()).getOriginalLabel();
} else {
discoveredLabel = target.getConfiguredTarget().getLabel();
}
ToolchainTypeInfo toolchainType =
unloadedToolchainContext.toolchainTypeToResolved().inverse().get(discoveredLabel);
ToolchainInfo toolchainInfo = PlatformProviderUtils.toolchain(target.getConfiguredTarget());

// If the toolchainType hadn't been resolved to an actual target, resolution would have
// failed with an error much earlier. However, the target might still not be an actual
// toolchain.
if (toolchainType != null) {
if (toolchainInfo != null) {
toolchains.put(toolchainType, toolchainInfo);
} else {
throw new TargetNotToolchainException(toolchainType, discoveredLabel);
}
}

// Find any template variables present for this toolchain.
TemplateVariableInfo templateVariableInfo =
target.getConfiguredTarget().get(TemplateVariableInfo.PROVIDER);
if (templateVariableInfo != null) {
templateVariableProviders.add(templateVariableInfo);
}
}

return toolchainContext
.setToolchains(toolchains.build())
.setTemplateVariableProviders(templateVariableProviders.build())
.build();
}

/** Builder interface to help create new instances of {@link ResolvedToolchainContext}. */
Expand Down Expand Up @@ -179,4 +239,18 @@ public boolean containsKey(Object key, Location loc, StarlarkContext context)
.findAny();
return matching.isPresent();
}

/**
* Exception used when a toolchain type is required but the resolved target does not have
* ToolchainInfo.
*/
static final class TargetNotToolchainException extends ToolchainException {
TargetNotToolchainException(ToolchainTypeInfo toolchainType, Label resolvedTargetLabel) {
super(
String.format(
"toolchain type %s resolved to target %s, but that target does not provide"
+ " ToolchainInfo",
toolchainType.typeLabel(), resolvedTargetLabel));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public class ToolchainResolver {
private final BuildConfigurationValue.Key configurationKey;

// Optional data.
private String targetDescription = "";
private ImmutableSet<Label> requiredToolchainTypeLabels = ImmutableSet.of();
private ImmutableSet<Label> execConstraintLabels = ImmutableSet.of();

Expand All @@ -82,15 +81,6 @@ public ToolchainResolver(Environment env, BuildConfigurationValue.Key configurat
this.configurationKey = checkNotNull(configurationKey);
}

/**
* Sets a description of the target that toolchains will be resolved for. This is primarily useful
* for printing informative error messages, so that users can tell which targets had difficulty.
*/
public ToolchainResolver setTargetDescription(String targetDescription) {
this.targetDescription = targetDescription;
return this;
}

/**
* Sets the labels of the required toolchain types that this resolver needs to find toolchains
* for.
Expand All @@ -117,7 +107,7 @@ public ToolchainResolver setExecConstraintLabels(Set<Label> execConstraintLabels
* then an {@link UnloadedToolchainContext} generated. The {@link UnloadedToolchainContext} will
* report the specific toolchain targets to depend on, and those can be found using the typical
* dependency machinery. Once dependencies, including toolchains, have been loaded, the {@link
* UnloadedToolchainContext#load} method can be called to generate the final {@link
* ResolvedToolchainContext#load} method can be called to generate the final {@link
* ResolvedToolchainContext} to be used by the target.
*
* <p>This makes several SkyFrame calls, particularly to {@link
Expand All @@ -133,7 +123,6 @@ public UnloadedToolchainContext resolve() throws InterruptedException, Toolchain
try {
UnloadedToolchainContext.Builder unloadedToolchainContext =
UnloadedToolchainContext.builder();
unloadedToolchainContext.setTargetDescription(targetDescription);

// Determine the configuration being used.
BuildConfigurationValue value =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ static Builder builder() {
/** Builder class to help create the {@link UnloadedToolchainContext}. */
@AutoValue.Builder
public interface Builder {
/** Sets a description of the target being used, for error messaging. */
Builder setTargetDescription(String targetDescription);

/** Sets the selected execution platform that these toolchains use. */
Builder setExecutionPlatform(PlatformInfo executionPlatform);

Expand All @@ -60,84 +57,11 @@ Builder setToolchainTypeToResolved(
UnloadedToolchainContext build();
}

/** Returns a description of the target being used, for error messaging. */
abstract String targetDescription();

/** The map of toolchain type to resolved toolchain to be used. */
abstract ImmutableBiMap<ToolchainTypeInfo, Label> toolchainTypeToResolved();

@Override
public ImmutableSet<Label> resolvedToolchainLabels() {
return toolchainTypeToResolved().values();
}

/**
* Finishes preparing the {@link ResolvedToolchainContext} by finding the specific toolchain
* providers to be used for each toolchain type.
*/
public ResolvedToolchainContext load(Iterable<ConfiguredTargetAndData> toolchainTargets)
throws ToolchainException {

ResolvedToolchainContext.Builder toolchainContext =
ResolvedToolchainContext.builder()
.setTargetDescription(targetDescription())
.setExecutionPlatform(executionPlatform())
.setTargetPlatform(targetPlatform())
.setRequiredToolchainTypes(requiredToolchainTypes())
.setResolvedToolchainLabels(resolvedToolchainLabels());

ImmutableMap.Builder<ToolchainTypeInfo, ToolchainInfo> toolchains =
new ImmutableMap.Builder<>();
ImmutableList.Builder<TemplateVariableInfo> templateVariableProviders =
new ImmutableList.Builder<>();
for (ConfiguredTargetAndData target : toolchainTargets) {
Label discoveredLabel;
// Aliases are in toolchainTypeToResolved by the original alias label, not via the final
// target's label.
if (target.getConfiguredTarget() instanceof AliasConfiguredTarget) {
discoveredLabel = ((AliasConfiguredTarget) target.getConfiguredTarget()).getOriginalLabel();
} else {
discoveredLabel = target.getConfiguredTarget().getLabel();
}
ToolchainTypeInfo toolchainType = toolchainTypeToResolved().inverse().get(discoveredLabel);
ToolchainInfo toolchainInfo = PlatformProviderUtils.toolchain(target.getConfiguredTarget());

// If the toolchainType hadn't been resolved to an actual target, resolution would have
// failed with an error much earlier. However, the target might still not be an actual
// toolchain.
if (toolchainType != null) {
if (toolchainInfo != null) {
toolchains.put(toolchainType, toolchainInfo);
} else {
throw new TargetNotToolchainException(toolchainType, discoveredLabel);
}
}

// Find any template variables present for this toolchain.
TemplateVariableInfo templateVariableInfo =
target.getConfiguredTarget().get(TemplateVariableInfo.PROVIDER);
if (templateVariableInfo != null) {
templateVariableProviders.add(templateVariableInfo);
}
}

return toolchainContext
.setToolchains(toolchains.build())
.setTemplateVariableProviders(templateVariableProviders.build())
.build();
}

/**
* Exception used when a toolchain type is required but the resolved target does not have
* ToolchainInfo.
*/
static final class TargetNotToolchainException extends ToolchainException {
TargetNotToolchainException(ToolchainTypeInfo toolchainType, Label resolvedTargetLabel) {
super(
String.format(
"toolchain type %s resolved to target %s, but that target does not provide"
+ " ToolchainInfo",
toolchainType.typeLabel(), resolvedTargetLabel));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,6 @@ public SkyValue compute(SkyKey skyKey, Environment env)
ImmutableSet<Label> requiredToolchains = aspect.getDefinition().getRequiredToolchains();
unloadedToolchainContext =
new ToolchainResolver(env, BuildConfigurationValue.key(configuration))
.setTargetDescription(
String.format(
"aspect %s applied to %s",
aspect.getDescriptor().getDescription(),
associatedConfiguredTargetAndData.getTarget()))
.setRequiredToolchainTypes(requiredToolchains)
.resolve();
} catch (ToolchainException e) {
Expand Down Expand Up @@ -463,8 +458,16 @@ public SkyValue compute(SkyKey skyKey, Environment env)
// Load the requested toolchains into the ToolchainContext, now that we have dependencies.
ResolvedToolchainContext toolchainContext = null;
if (unloadedToolchainContext != null) {
String targetDescription =
String.format(
"aspect %s applied to %s",
aspect.getDescriptor().getDescription(),
associatedConfiguredTargetAndData.getTarget());
toolchainContext =
unloadedToolchainContext.load(depValueMap.get(DependencyResolver.TOOLCHAIN_DEPENDENCY));
ResolvedToolchainContext.load(
unloadedToolchainContext,
targetDescription,
depValueMap.get(DependencyResolver.TOOLCHAIN_DEPENDENCY));
}

return createAspect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ public SkyValue compute(SkyKey key, Environment env) throws ConfiguredTargetFunc
ImmutableSet<Label> execConstraintLabels = getExecutionPlatformConstraints(rule);
unloadedToolchainContext =
new ToolchainResolver(env, configuredTargetKey.getConfigurationKey())
.setTargetDescription(rule.toString())
.setRequiredToolchainTypes(requiredToolchains)
.setExecConstraintLabels(execConstraintLabels)
.resolve();
Expand Down Expand Up @@ -345,8 +344,12 @@ public SkyValue compute(SkyKey key, Environment env) throws ConfiguredTargetFunc
// Load the requested toolchains into the ToolchainContext, now that we have dependencies.
ResolvedToolchainContext toolchainContext = null;
if (unloadedToolchainContext != null) {
String targetDescription = target.toString();
toolchainContext =
unloadedToolchainContext.load(depValueMap.get(DependencyResolver.TOOLCHAIN_DEPENDENCY));
ResolvedToolchainContext.load(
unloadedToolchainContext,
targetDescription,
depValueMap.get(DependencyResolver.TOOLCHAIN_DEPENDENCY));
}

ConfiguredTargetValue ans =
Expand Down
Loading

0 comments on commit 924f447

Please sign in to comment.