aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar cpeyser <cpeyser@google.com>2017-11-18 16:48:08 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2017-11-18 16:50:06 -0800
commit1ea72aa77af17b413538f66521193c3d1b7657ad (patch)
treed28de3a3b2c1f1461f418cbcf222b9d753452bca
parent2af2bff08c2fab136ea0927242bd4f3fe05456bf (diff)
Change default behavior for cc_toolchain.compiler and cc_toolchain.libc for
platform-based toolchain selection to match legacy behavior. This is a change over previous behavior in the platform case, which was to throw if either of those attributes is not present. The default_toolchain field in CROSSTOOL gives a mapping from cc_toolchain.cpu values to toolchains - this map should be used with compiler and libc are not specified, as is currently the non-platforms, legacy behavior. PiperOrigin-RevId: 176246316
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchain.java17
-rw-r--r--src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainSelectionTest.java15
2 files changed, 12 insertions, 20 deletions
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 f7fc13e4dc..c66dd9378b 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
@@ -601,18 +601,21 @@ public class CcToolchain implements RuleConfiguredTargetFactory {
private CToolchain getToolchainFromAttributes(
RuleContext ruleContext, CppConfiguration cppConfiguration) throws RuleErrorException {
- for (String requiredAttr : ImmutableList.of("cpu", "compiler", "libc")) {
- if (ruleContext.attributes().get(requiredAttr, Type.STRING).isEmpty()) {
- ruleContext.throwWithRuleError(
- String.format(
- "Using cc_toolchain target requires the attribute '%s' to be present.",
- requiredAttr));
- }
+ if (ruleContext.attributes().get("cpu", Type.STRING).isEmpty()) {
+ ruleContext.throwWithRuleError("Using cc_toolchain target requires the attribute 'cpu' "
+ + "to be present");
}
+
String cpu = ruleContext.attributes().get("cpu", Type.STRING);
String compiler = ruleContext.attributes().get("compiler", Type.STRING);
+ if (compiler.isEmpty()) {
+ compiler = null;
+ }
String libc = ruleContext.attributes().get("libc", Type.STRING);
+ if (libc.isEmpty()) {
+ libc = null;
+ }
CrosstoolConfigurationIdentifier config =
new CrosstoolConfigurationIdentifier(cpu, compiler, libc);
diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainSelectionTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainSelectionTest.java
index e0d92a4c1e..e6c453e28a 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainSelectionTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainSelectionTest.java
@@ -26,7 +26,6 @@ import com.google.devtools.build.lib.analysis.util.ScratchAttributeWriter;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.util.MockCcSupport;
import com.google.devtools.build.lib.packages.util.MockPlatformSupport;
-import com.google.devtools.build.lib.testutil.MoreAsserts;
import com.google.devtools.build.lib.testutil.TestConstants;
import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.ToolPath;
import java.util.List;
@@ -128,7 +127,7 @@ public class CcToolchainSelectionTest extends BuildViewTestCase {
}
@Test
- public void testErrorForIncompleteCcToolchain() throws Exception {
+ public void testIncompleteCcToolchain() throws Exception {
mockToolsConfig.create(
"incomplete_toolchain/BUILD",
"toolchain(",
@@ -156,17 +155,7 @@ public class CcToolchainSelectionTest extends BuildViewTestCase {
"--experimental_platforms=//mock_platform:mock-piii-platform",
"--extra_toolchains=//incomplete_toolchain:incomplete_toolchain_cc-compiler-piii");
- AssertionError thrown =
- MoreAsserts.expectThrows(
- AssertionError.class,
- () ->
- ScratchAttributeWriter.fromLabelString(this, "cc_library", "//lib")
- .setList("srcs", "a.cc")
- .write());
-
- assertThat(thrown)
- .hasMessageThat()
- .contains("Using cc_toolchain target requires the attribute 'compiler' to be present");
+ // should not throw.
}
@Test