aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Googler <noreply@google.com>2018-04-24 17:25:59 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-04-24 17:27:21 -0700
commitb88e3f4bbbbf12001b6aa11453f604d360b3d91b (patch)
tree7f1e3e994e0644588cdca4c1b28bd4dc380c1d27 /src
parentc5c6ef83936cf90fd11038a84637e691beb47288 (diff)
Make the static_link_cpp_runtimes feature settable
as a normal feature. Prior to this cl, it was always set by examining supports_embedded_runtimes. DELTA_BY_EXTENSION=java=100,py=15 RELNOTES: None PiperOrigin-RevId: 194172053
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java42
-rw-r--r--src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainTest.java57
2 files changed, 98 insertions, 1 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java
index 476f872b55..56199a7e75 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java
@@ -688,6 +688,46 @@ public final class CcCommon {
}
/**
+ * Determines whether to statically link the C++ runtimes.
+ *
+ * <p>This is complicated because it depends both on a legacy field in the CROSSTOOL
+ * protobuf--supports_embedded_runtimes--and the newer crosstool
+ * feature--statically_link_cpp_runtimes. Neither, one, or both could be present or set. Or they
+ * could be set in to conflicting values.
+ *
+ * @return true if we should statically link, false otherwise.
+ */
+ private static boolean enableStaticLinkCppRuntimesFeature(
+ ImmutableSet<String> requestedFeatures,
+ ImmutableSet<String> disabledFeatures,
+ CcToolchainProvider toolchain) {
+ // All of these cases are encountered in various unit tests,
+ // integration tests, and obscure CROSSTOOLs.
+
+ // A. If the legacy field "supports_embedded_runtimes" is false (or not present):
+ // dynamically link the cpp runtimes. Done.
+ if (!toolchain.supportsEmbeddedRuntimes()) {
+ return false;
+ }
+ // From here, the toolchain _can_ statically link the cpp runtime.
+ //
+ // B. If the feature static_link_cpp_runtimes is disabled:
+ // dynamically link the cpp runtimes. Done.
+ if (disabledFeatures.contains(CppRuleClasses.STATIC_LINK_CPP_RUNTIMES)) {
+ return false;
+ }
+ // C. If the feature is not requested:
+ // the feature is neither disabled nor requested: statically
+ // link (for compatibility with the legacy field).
+ if (!requestedFeatures.contains(CppRuleClasses.STATIC_LINK_CPP_RUNTIMES)) {
+ return true;
+ }
+ // D. The feature is requested:
+ // statically link the cpp runtimes. Done.
+ return true;
+ }
+
+ /**
* Creates a feature configuration for a given rule. Assumes strictly cc sources.
*
* @param ruleContext the context of the rule we want the feature configuration for.
@@ -740,7 +780,7 @@ public final class CcCommon {
if (toolchain.getCcCompilationContextInfo().getCppModuleMap() == null) {
unsupportedFeaturesBuilder.add(CppRuleClasses.MODULE_MAPS);
}
- if (toolchain.supportsEmbeddedRuntimes()) {
+ if (enableStaticLinkCppRuntimesFeature(requestedFeatures, unsupportedFeatures, toolchain)) {
allRequestedFeaturesBuilder.add(CppRuleClasses.STATIC_LINK_CPP_RUNTIMES);
} else {
unsupportedFeaturesBuilder.add(CppRuleClasses.STATIC_LINK_CPP_RUNTIMES);
diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainTest.java
index ffe4ca9777..f01726a638 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainTest.java
@@ -29,7 +29,9 @@ import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfig
import com.google.devtools.build.lib.rules.cpp.CppConfiguration.DynamicMode;
import com.google.devtools.build.lib.testutil.TestConstants;
import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig;
+import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CToolchain;
import com.google.devtools.common.options.OptionsParsingException;
+import com.google.protobuf.TextFormat;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@@ -863,6 +865,8 @@ public class CcToolchainTest extends BuildViewTestCase {
assertThat(toolchainProvider.supportsDynamicLinker()).isTrue();
}
+ // Tests CcCommon::enableStaticLinkCppRuntimesFeature when supports_embedded_runtimes is not
+ // present at all in the toolchain.
@Test
public void testStaticLinkCppRuntimesSetViaSupportsEmbeddedRuntimesUnset() throws Exception {
writeDummyCcToolchain();
@@ -877,6 +881,8 @@ public class CcToolchainTest extends BuildViewTestCase {
.isEqualTo(featureConfiguration.isEnabled(CppRuleClasses.STATIC_LINK_CPP_RUNTIMES));
}
+ // Tests CcCommon::enableStaticLinkCppRuntimesFeature when supports_embedded_runtimes is false
+ // in the toolchain.
@Test
public void testStaticLinkCppRuntimesSetViaSupportsEmbeddedRuntimesFalse() throws Exception {
writeDummyCcToolchain();
@@ -890,4 +896,55 @@ public class CcToolchainTest extends BuildViewTestCase {
assertThat(toolchainProvider.supportsEmbeddedRuntimes())
.isEqualTo(featureConfiguration.isEnabled(CppRuleClasses.STATIC_LINK_CPP_RUNTIMES));
}
+
+ private FeatureConfiguration configureFeaturesForStaticLinkCppRuntimesTest(
+ String partialToolchain, String configurationToUse) throws Exception {
+ writeDummyCcToolchain();
+ CToolchain.Builder toolchainBuilder = CToolchain.newBuilder();
+ TextFormat.merge(partialToolchain, toolchainBuilder);
+ getAnalysisMock()
+ .ccSupport()
+ .setupCrosstool(
+ mockToolsConfig,
+ /* addEmbeddedRuntimes= */ true,
+ /* addModuleMap= */ false,
+ /* staticRuntimesLabel= */ null,
+ /* dynamicRuntimesLabel= */ null,
+ toolchainBuilder.buildPartial());
+ useConfiguration(configurationToUse);
+ ConfiguredTarget target = getConfiguredTarget("//a:b");
+ CcToolchainProvider toolchainProvider =
+ (CcToolchainProvider) target.get(ToolchainInfo.PROVIDER);
+ return CcCommon.configureFeaturesOrReportRuleError(getRuleContext(target), toolchainProvider);
+ }
+
+ // Tests CcCommon::enableStaticLinkCppRuntimesFeature when supports_embedded_runtimes is true in
+ // the toolchain and the feature is not present at all.
+ @Test
+ public void testSupportsEmbeddedRuntimesNoFeatureAtAll() throws Exception {
+ FeatureConfiguration featureConfiguration =
+ configureFeaturesForStaticLinkCppRuntimesTest("supports_embedded_runtimes: true", "");
+ assertThat(featureConfiguration.isEnabled(CppRuleClasses.STATIC_LINK_CPP_RUNTIMES)).isTrue();
+ }
+
+ // Tests CcCommon::enableStaticLinkCppRuntimesFeature when supports_embedded_runtimes is true in
+ // the toolchain and the feature is enabled.
+ @Test
+ public void testSupportsEmbeddedRuntimesFeatureEnabled() throws Exception {
+ FeatureConfiguration featureConfiguration =
+ configureFeaturesForStaticLinkCppRuntimesTest(
+ "supports_embedded_runtimes: true", "--features=static_link_cpp_runtimes");
+ assertThat(featureConfiguration.isEnabled(CppRuleClasses.STATIC_LINK_CPP_RUNTIMES)).isTrue();
+ }
+
+ // Tests CcCommon::enableStaticLinkCppRuntimesFeature when supports_embedded_runtimes is true in
+ // the toolchain and the feature is disabled.
+ @Test
+ public void testStaticLinkCppRuntimesOverridesSupportsEmbeddedRuntimes() throws Exception {
+ FeatureConfiguration featureConfiguration =
+ configureFeaturesForStaticLinkCppRuntimesTest(
+ "supports_embedded_runtimes: true feature { name: 'static_link_cpp_runtimes' }",
+ "--features=-static_link_cpp_runtimes");
+ assertThat(featureConfiguration.isEnabled(CppRuleClasses.STATIC_LINK_CPP_RUNTIMES)).isFalse();
+ }
}