diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchain.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchain.java index 40782daaaecb95..5c096847e97b4b 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchain.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchain.java @@ -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 makeVariables = new HashMap<>(toolchainProvider.getAdditionalMakeVariables()); diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainAliasRule.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainAliasRule.java index bdc85a36929643..7a25227f899f9a 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainAliasRule.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainAliasRule.java @@ -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 { - 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.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(); + } } }