Skip to content

Commit

Permalink
Rename ToolchainContext to ResolvedToolchainContext.
Browse files Browse the repository at this point in the history
Part of work on execution transitions, bazelbuild#7935.
  • Loading branch information
katre committed Apr 3, 2019
1 parent b3b3e8b commit 9b21ef9
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public final ConfiguredTarget createConfiguredTarget(
ConfiguredTargetKey configuredTargetKey,
OrderedSetMultimap<DependencyKind, ConfiguredTargetAndData> prerequisiteMap,
ImmutableMap<Label, ConfigMatchingProvider> configConditions,
@Nullable ToolchainContext toolchainContext)
@Nullable ResolvedToolchainContext toolchainContext)
throws InterruptedException, ActionConflictException {
if (target instanceof Rule) {
try {
Expand Down Expand Up @@ -286,7 +286,7 @@ private ConfiguredTarget createRule(
ConfiguredTargetKey configuredTargetKey,
OrderedSetMultimap<DependencyKind, ConfiguredTargetAndData> prerequisiteMap,
ImmutableMap<Label, ConfigMatchingProvider> configConditions,
@Nullable ToolchainContext toolchainContext)
@Nullable ResolvedToolchainContext toolchainContext)
throws InterruptedException, ActionConflictException {

// Visibility computation and checking is done for every rule.
Expand Down Expand Up @@ -473,7 +473,7 @@ public ConfiguredAspect createAspect(
Aspect aspect,
OrderedSetMultimap<DependencyKind, ConfiguredTargetAndData> prerequisiteMap,
ImmutableMap<Label, ConfigMatchingProvider> configConditions,
@Nullable ToolchainContext toolchainContext,
@Nullable ResolvedToolchainContext toolchainContext,
BuildConfiguration aspectConfiguration,
BuildConfiguration hostConfiguration,
ActionLookupValue.ActionLookupKey aspectKey)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// Copyright 2017 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.analysis;

import static java.util.stream.Collectors.joining;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
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.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.skylarkbuildapi.ToolchainContextApi;
import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter;
import com.google.devtools.build.lib.skylarkinterface.StarlarkContext;
import com.google.devtools.build.lib.syntax.EvalException;
import com.google.devtools.build.lib.syntax.EvalUtils;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;

/**
* Represents the data needed for a specific target's use of toolchains and platforms, including
* specific {@link ToolchainInfo} providers for each required toolchain type.
*/
@AutoValue
@Immutable
@ThreadSafe
public abstract class ResolvedToolchainContext implements ToolchainContextApi, ToolchainContext {

static Builder builder() {
return new AutoValue_ResolvedToolchainContext.Builder();
}

/** Builder interface to help create new instances of {@link ResolvedToolchainContext}. */
@AutoValue.Builder
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);

/** Sets the target platform that these toolchains generate output for. */
Builder setTargetPlatform(PlatformInfo targetPlatform);

/** Sets the toolchain types that were requested. */
Builder setRequiredToolchainTypes(Set<ToolchainTypeInfo> requiredToolchainTypes);

/** Sets the map from toolchain type to toolchain provider. */
Builder setToolchains(ImmutableMap<ToolchainTypeInfo, ToolchainInfo> toolchains);

/** Sets the template variables that these toolchains provide. */
Builder setTemplateVariableProviders(ImmutableList<TemplateVariableInfo> providers);

/** Sets the labels of the specific toolchains being used. */
Builder setResolvedToolchainLabels(ImmutableSet<Label> resolvedToolchainLabels);

/** Returns a new {@link ResolvedToolchainContext}. */
ResolvedToolchainContext build();
}

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

abstract ImmutableMap<ToolchainTypeInfo, ToolchainInfo> toolchains();

/** Returns the template variables that these toolchains provide. */
public abstract ImmutableList<TemplateVariableInfo> templateVariableProviders();

/**
* Returns the toolchain for the given type, or {@code null} if the toolchain type was not
* required in this context.
*/
@Nullable
public ToolchainInfo forToolchainType(Label toolchainTypeLabel) {
Optional<ToolchainTypeInfo> toolchainType =
toolchains().keySet().stream()
.filter(info -> info.typeLabel().equals(toolchainTypeLabel))
.findFirst();
if (toolchainType.isPresent()) {
return forToolchainType(toolchainType.get());
} else {
return null;
}
}

@Nullable
public ToolchainInfo forToolchainType(ToolchainTypeInfo toolchainType) {
return toolchains().get(toolchainType);
}

@Override
public boolean isImmutable() {
return true;
}

@Override
public void repr(SkylarkPrinter printer) {
printer.append("<toolchain_context.resolved_labels: ");
printer.append(
toolchains().keySet().stream()
.map(ToolchainTypeInfo::typeLabel)
.map(Label::toString)
.collect(joining(", ")));
printer.append(">");
}

private Label transformKey(Object key, Location loc) throws EvalException {
if (key instanceof Label) {
return (Label) key;
} else if (key instanceof ToolchainTypeInfo) {
return ((ToolchainTypeInfo) key).typeLabel();
} else if (key instanceof String) {
Label toolchainType;
String rawLabel = (String) key;
try {
toolchainType = Label.parseAbsolute(rawLabel, ImmutableMap.of());
} catch (LabelSyntaxException e) {
throw new EvalException(
loc, String.format("Unable to parse toolchain %s: %s", rawLabel, e.getMessage()), e);
}
return toolchainType;
} else {
throw new EvalException(
loc,
String.format(
"Toolchains only supports indexing by toolchain type, got %s instead",
EvalUtils.getDataTypeName(key)));
}
}

@Override
public ToolchainInfo getIndex(Object key, Location loc, StarlarkContext context)
throws EvalException {
Label toolchainTypeLabel = transformKey(key, loc);

if (!containsKey(key, loc, context)) {
throw new EvalException(
loc,
String.format(
"In %s, toolchain type %s was requested but only types [%s] are configured",
targetDescription(),
toolchainTypeLabel,
requiredToolchainTypes().stream()
.map(ToolchainTypeInfo::typeLabel)
.map(Label::toString)
.collect(joining(", "))));
}
return forToolchainType(toolchainTypeLabel);
}

@Override
public boolean containsKey(Object key, Location loc, StarlarkContext context)
throws EvalException {
Label toolchainTypeLabel = transformKey(key, loc);
Optional<Label> matching =
toolchains().keySet().stream()
.map(ToolchainTypeInfo::typeLabel)
.filter(label -> label.equals(toolchainTypeLabel))
.findAny();
return matching.isPresent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public ImmutableList<TransitiveInfoCollection> getFiles() {
private final ConfigurationFragmentPolicy configurationFragmentPolicy;
private final ImmutableList<Class<? extends BuildConfiguration.Fragment>> universalFragments;
private final RuleErrorConsumer reporter;
@Nullable private final ToolchainContext toolchainContext;
@Nullable private final ResolvedToolchainContext toolchainContext;
private final ConstraintSemantics constraintSemantics;

private ActionOwner actionOwner;
Expand All @@ -205,7 +205,7 @@ private RuleContext(
String ruleClassNameForLogging,
ActionLookupValue.ActionLookupKey actionLookupKey,
ImmutableMap<String, Attribute> aspectAttributes,
@Nullable ToolchainContext toolchainContext,
@Nullable ResolvedToolchainContext toolchainContext,
ConstraintSemantics constraintSemantics) {
super(
builder.env,
Expand Down Expand Up @@ -1146,7 +1146,7 @@ public ConfigurationMakeVariableContext getConfigurationMakeVariableContext() {
}

@Nullable
public ToolchainContext getToolchainContext() {
public ResolvedToolchainContext getToolchainContext() {
return toolchainContext;
}

Expand Down Expand Up @@ -1490,7 +1490,7 @@ public static final class Builder implements RuleErrorConsumer {
private NestedSet<PackageGroupContents> visibility;
private ImmutableMap<String, Attribute> aspectAttributes;
private ImmutableList<Aspect> aspects;
private ToolchainContext toolchainContext;
private ResolvedToolchainContext toolchainContext;
private ConstraintSemantics constraintSemantics;

@VisibleForTesting
Expand Down Expand Up @@ -1597,8 +1597,8 @@ public Builder setUniversalFragments(
return this;
}

/** Sets the {@link ToolchainContext} used to access toolchains used by this rule. */
public Builder setToolchainContext(ToolchainContext toolchainContext) {
/** Sets the {@link ResolvedToolchainContext} used to access toolchains used by this rule. */
public Builder setToolchainContext(ResolvedToolchainContext toolchainContext) {
this.toolchainContext = toolchainContext;
return this;
}
Expand Down
Loading

0 comments on commit 9b21ef9

Please sign in to comment.