Skip to content

Commit

Permalink
Re-implement cc_toolchain_alias to handle toolchain resolution.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 368833080
  • Loading branch information
katre authored and copybara-github committed Apr 16, 2021
1 parent 8ace6db commit db17c1e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ public ConfiguredTarget create(RuleContext ruleContext)
return ruleConfiguredTargetBuilder.build();
}

static TemplateVariableInfo createMakeVariableProvider(
CcToolchainProvider toolchainProvider,
Location location) {
public static TemplateVariableInfo createMakeVariableProvider(
CcToolchainProvider toolchainProvider, Location location) {

HashMap<String, String> makeVariables =
new HashMap<>(toolchainProvider.getAdditionalMakeVariables());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,88 @@

package com.google.devtools.build.lib.rules.cpp;

import com.google.devtools.build.lib.rules.LateBoundAlias.CommonAliasRule;
import static com.google.devtools.build.lib.packages.Attribute.attr;
import static com.google.devtools.build.lib.packages.BuildType.LABEL;
import static com.google.devtools.build.lib.packages.BuildType.NODEP_LABEL;

import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException;
import com.google.devtools.build.lib.analysis.BaseRuleClasses;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.MiddlemanProvider;
import com.google.devtools.build.lib.analysis.PlatformConfiguration;
import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder;
import com.google.devtools.build.lib.analysis.RuleConfiguredTargetFactory;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.analysis.Runfiles;
import com.google.devtools.build.lib.analysis.RunfilesProvider;
import com.google.devtools.build.lib.analysis.TemplateVariableInfo;
import com.google.devtools.build.lib.analysis.platform.ToolchainInfo;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.RuleClass.ToolchainTransitionMode;

/** Implementation of the {@code cc_toolchain_alias} rule. */
public class CcToolchainAliasRule extends CommonAliasRule<CppConfiguration> {
public CcToolchainAliasRule() {
super("cc_toolchain_alias", CppRuleClasses::ccToolchainAttribute, CppConfiguration.class);
public class CcToolchainAliasRule implements RuleDefinition {
@Override
public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env) {
return builder
.requiresConfigurationFragments(CppConfiguration.class)
.removeAttribute("licenses")
.removeAttribute("distribs")
.add(
attr(CcToolchain.CC_TOOLCHAIN_DEFAULT_ATTRIBUTE_NAME, LABEL)
.mandatoryProviders(CcToolchainProvider.PROVIDER.id())
.value(CppRuleClasses.ccToolchainAttribute(env)))
.add(
attr(CcToolchain.CC_TOOLCHAIN_TYPE_ATTRIBUTE_NAME, NODEP_LABEL)
.value(CppRuleClasses.ccToolchainTypeAttribute(env)))
.requiresConfigurationFragments(PlatformConfiguration.class)
.addRequiredToolchains(CppRuleClasses.ccToolchainTypeAttribute(env))
.useToolchainTransition(ToolchainTransitionMode.ENABLED)
.build();
}

@Override
public Metadata getMetadata() {
return Metadata.builder()
.name("cc_toolchain_alias")
.ancestors(BaseRuleClasses.NativeBuildRule.class)
.factoryClass(CcToolchainAlias.class)
.build();
}

/** Implementation of cc_toolchain_alias. */
public static class CcToolchainAlias implements RuleConfiguredTargetFactory {
@Override
public ConfiguredTarget create(RuleContext ruleContext)
throws InterruptedException, RuleErrorException, ActionConflictException {

CcToolchainProvider ccToolchainProvider =
CppHelper.getToolchainUsingDefaultCcToolchainAttribute(ruleContext);

TemplateVariableInfo templateVariableInfo =
CcToolchain.createMakeVariableProvider(
ccToolchainProvider, ruleContext.getRule().getLocation());

ToolchainInfo toolchain =
new ToolchainInfo(
ImmutableMap.<String, Object>builder()
.put("cc", ccToolchainProvider)
// Add a clear signal that this is a CcToolchainProvider, since just "cc" is
// generic enough to possibly be re-used.
.put("cc_provider_in_toolchain", true)
.build());

return new RuleConfiguredTargetBuilder(ruleContext)
.addProvider(RunfilesProvider.simple(Runfiles.EMPTY))
.addNativeDeclaredProvider(ccToolchainProvider)
.addNativeDeclaredProvider(toolchain)
.addNativeDeclaredProvider(templateVariableInfo)
.setFilesToBuild(ccToolchainProvider.getAllFiles())
.addProvider(new MiddlemanProvider(ccToolchainProvider.getAllFilesMiddleman()))
.build();
}
}
}

0 comments on commit db17c1e

Please sign in to comment.