Skip to content

Commit

Permalink
Change the definition of genrule to require a c++ toolchain.
Browse files Browse the repository at this point in the history
This will allow a genrule implementation to make checks against the c++ target platform.

PiperOrigin-RevId: 179692246
  • Loading branch information
calpeyser authored and Copybara-Service committed Dec 20, 2017
1 parent 3d0a04d commit 1619d40
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.rules.cpp.CcToolchain;
import com.google.devtools.build.lib.rules.cpp.CppConfiguration;
import com.google.devtools.build.lib.rules.cpp.CppRuleClasses;
import com.google.devtools.build.lib.rules.genrule.GenRuleBaseRule;
import com.google.devtools.build.lib.rules.java.JavaConfiguration;
import com.google.devtools.build.lib.rules.java.JavaSemantics;
Expand Down Expand Up @@ -55,6 +56,7 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
attr(CcToolchain.CC_TOOLCHAIN_TYPE_ATTRIBUTE_NAME, LABEL)
.value(GenRuleBaseRule.ccToolchainTypeAttribute(env)))
.add(attr(":host_jdk", LABEL).cfg(HOST).value(JavaSemantics.hostJdkAttribute(env)))
.addRequiredToolchains(CppRuleClasses.ccToolchainTypeAttribute(env))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ public void testResolvedCcToolchain() throws Exception {
.endsWith("piii");
}

@Test
public void testResolvedCcToolchainGenrule() throws Exception {
useConfiguration(
"--experimental_platforms=//mock_platform:mock-piii-platform",
"--extra_toolchains=//mock_platform:toolchain_cc-compiler-piii");
ConfiguredTarget target =
ScratchAttributeWriter.fromLabelString(this, "genrule", "//gen")
.set("cmd", "\"foobar\"")
.setList("outs", "out.txt")
.write();
ResolvedToolchainProviders providers =
(ResolvedToolchainProviders)
getRuleContext(target).getToolchainContext().getResolvedToolchainProviders();
CcToolchainProvider toolchain =
(CcToolchainProvider)
providers.getForToolchainType(Label.parseAbsolute(CPP_TOOLCHAIN_TYPE));
assertThat(Iterables.getOnlyElement(toolchain.getCompile()).getExecPathString())
.endsWith("piii");
}

@Test
public void testToolchainSelectionWithPlatforms() throws Exception {
useConfiguration(
Expand Down
10 changes: 6 additions & 4 deletions src/test/shell/bazel/toolchain_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -573,12 +573,14 @@ register_toolchains('//demo:invalid')
EOF

mkdir -p demo
cat >> demo/out.log<<EOF
INVALID
EOF
cat >> demo/BUILD <<EOF
genrule(
filegroup(
name = "invalid",
srcs = [],
outs = ["out.log"],
cmd = "echo invalid > $@")
srcs = ["out.log"],
)
load('//toolchain:rule_use_toolchain.bzl', 'use_toolchain')
# Use the toolchain.
Expand Down

0 comments on commit 1619d40

Please sign in to comment.