Skip to content

Commit

Permalink
Re-implement cc_host_toolchain_alias to handle toolchain resolution.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 368833358
  • Loading branch information
katre authored and copybara-github committed Apr 16, 2021
1 parent db17c1e commit 507aeaa
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,72 @@

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

import static com.google.devtools.build.lib.packages.Attribute.attr;
import static com.google.devtools.build.lib.packages.BuildType.LABEL;

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.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.TransitiveInfoCollection;
import com.google.devtools.build.lib.analysis.config.HostTransition;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.Attribute;
import com.google.devtools.build.lib.rules.LateBoundAlias.CommonAliasRule;
import com.google.devtools.build.lib.analysis.platform.ToolchainInfo;
import com.google.devtools.build.lib.packages.RuleClass;

/** Implementation of the {@code cc_toolchain_alias} rule. */
public class CcHostToolchainAliasRule extends CommonAliasRule<CppConfiguration> {
public class CcHostToolchainAliasRule implements RuleDefinition {

public CcHostToolchainAliasRule() {
super(
"cc_host_toolchain_alias",
CppRuleClasses::ccHostToolchainAttribute,
CppConfiguration.class);
@Override
public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env) {
return builder
.requiresConfigurationFragments(CppConfiguration.class)
.removeAttribute("licenses")
.removeAttribute("distribs")
.add(
attr("$cc_toolchain_alias", LABEL)
.cfg(HostTransition.createFactory())
.value(env.getToolsLabel("//tools/cpp:current_cc_toolchain")))
.build();
}

@Override
protected Attribute.Builder<Label> makeAttribute(RuleDefinitionEnvironment environment) {
Attribute.Builder<Label> builder = super.makeAttribute(environment);
return builder.cfg(HostTransition.createFactory());
public Metadata getMetadata() {
return Metadata.builder()
.name("cc_host_toolchain_alias")
.ancestors(BaseRuleClasses.NativeBuildRule.class)
.factoryClass(CcHostToolchainAlias.class)
.build();
}

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

TransitiveInfoCollection ccToolchainAlias =
ruleContext.getPrerequisite("$cc_toolchain_alias");
CcToolchainProvider ccToolchainProvider = ccToolchainAlias.get(CcToolchainProvider.PROVIDER);
TemplateVariableInfo templateVariableInfo =
ccToolchainAlias.get(TemplateVariableInfo.PROVIDER);
ToolchainInfo toolchain = ccToolchainAlias.get(ToolchainInfo.PROVIDER);

return new RuleConfiguredTargetBuilder(ruleContext)
.addProvider(RunfilesProvider.simple(Runfiles.EMPTY))
.addNativeDeclaredProvider(ccToolchainProvider)
.addNativeDeclaredProvider(toolchain)
.addNativeDeclaredProvider(templateVariableInfo)
.setFilesToBuild(ccToolchainProvider.getAllFiles())
.addProvider(new MiddlemanProvider(ccToolchainProvider.getAllFilesMiddleman()))
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.util.MockCcSupport;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -54,7 +52,6 @@ public void testThatHostCrosstoolTopCommandLineArgumentWorks() throws Exception
"})",
"cc_toolchain(",
" name = 'toolchain_b',",
" toolchain_identifier = 'mock-llvm-toolchain-k8',",
" toolchain_config = ':mock_config',",
" all_files = ':banana',",
" ar_files = ':empty',",
Expand All @@ -66,14 +63,31 @@ public void testThatHostCrosstoolTopCommandLineArgumentWorks() throws Exception
" objcopy_files = ':empty')",
"cc_toolchain_config(name='mock_config')");

scratch.file("b/cc_toolchain_config.bzl", MockCcSupport.EMPTY_CC_TOOLCHAIN);
scratch.file(
"b/cc_toolchain_config.bzl",
"def _impl(ctx):",
" return cc_common.create_cc_toolchain_config_info(",
" ctx = ctx,",
" toolchain_identifier = 'custom_toolchain',",
" host_system_name = 'mock-system-name-for-k8',",
" target_system_name = 'mock-target-system-name-for-k8',",
" target_cpu = 'k8',",
" target_libc = 'mock-libc-for-k8',",
" compiler = 'mock-compiler-for-k8',",
" abi_libc_version = 'mock-abi-libc-for-k8',",
" abi_version = 'mock-abi-version-for-k8')",
"cc_toolchain_config = rule(",
" implementation = _impl,",
" attrs = {},",
" provides = [CcToolchainConfigInfo],",
")");

scratch.file("a/BUILD", "cc_host_toolchain_alias(name='current_cc_host_toolchain')");

useConfiguration("--host_crosstool_top=//b:my_custom_toolchain_suite", "--host_cpu=k8");
ConfiguredTarget target = getConfiguredTarget("//a:current_cc_host_toolchain");

assertThat(target.getLabel())
.isEqualTo(Label.parseAbsoluteUnchecked("//b:my_custom_toolchain_suite"));
CcToolchainProvider ccToolchainProvider = target.get(CcToolchainProvider.PROVIDER);
assertThat(ccToolchainProvider.getToolchainIdentifier()).isEqualTo("custom_toolchain");
}
}

0 comments on commit 507aeaa

Please sign in to comment.