diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderHelper.java index 85a6b66b3b9dab..7ede204c121108 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderHelper.java @@ -477,6 +477,11 @@ static CcToolchainProvider getCcToolchainProvider( final NestedSet<Artifact> staticRuntimeLinkInputs; final Artifact staticRuntimeLinkMiddleman; if (toolchainInfo.supportsEmbeddedRuntimes()) { + if (staticRuntimeLibDep == null) { + throw ruleContext.throwWithRuleError( + "Toolchain supports embedded runtimes, but didn't " + + "provide static_runtime_libs attribute."); + } staticRuntimeLinkInputs = staticRuntimeLibDep.getProvider(FileProvider.class).getFilesToBuild(); } else { @@ -505,6 +510,11 @@ static CcToolchainProvider getCcToolchainProvider( List<Artifact> dynamicRuntimeLinkInputs = new ArrayList<>(); Artifact dynamicRuntimeLinkMiddleman; if (toolchainInfo.supportsEmbeddedRuntimes()) { + if (dynamicRuntimeLibDep == null) { + throw ruleContext.throwWithRuleError( + "Toolchain supports embedded runtimes, but didn't " + + "provide dynamic_runtime_libs attribute."); + } NestedSetBuilder<Artifact> dynamicRuntimeLinkSymlinksBuilder = NestedSetBuilder.stableOrder(); for (Artifact artifact : dynamicRuntimeLibDep.getProvider(FileProvider.class).getFilesToBuild()) { @@ -835,6 +845,9 @@ private static CppModuleMap createCrosstoolModuleMap(CcToolchainAttributesProvid static TransitiveInfoCollection selectDep( ImmutableList<? extends TransitiveInfoCollection> deps, Label label) { + if (deps.isEmpty()) { + return null; + } for (TransitiveInfoCollection dep : deps) { if (dep.getLabel().equals(label)) { return dep; diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainRule.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainRule.java index 6f0329b2c03d7e..63dcbfab00aaa3 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainRule.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainRule.java @@ -221,7 +221,7 @@ Currently unused (<a href="https://github.com/bazelbuild/bazel/issues/6928">#692 <p>cc_toolchain will select one of these libraries based on the label from crosstool_proto.static_runtimes_filegroup field.</p> <!-- #END_BLAZE_RULE.ATTRIBUTE -->*/ - .add(attr("static_runtime_libs", LABEL_LIST).legacyAllowAnyFileType().mandatory()) + .add(attr("static_runtime_libs", LABEL_LIST).legacyAllowAnyFileType()) /* <!-- #BLAZE_RULE(cc_toolchain).ATTRIBUTE(dynamic_runtime_libs) --> A collection of artifacts for dynamic libraries for the C++ runtime library (e.g. libstdc++.so). @@ -229,7 +229,7 @@ Currently unused (<a href="https://github.com/bazelbuild/bazel/issues/6928">#692 <p>cc_toolchain will select one of these libraries based on the label from crosstool_proto.dynamic_runtimes_filegroup field.</p> <!-- #END_BLAZE_RULE.ATTRIBUTE -->*/ - .add(attr("dynamic_runtime_libs", LABEL_LIST).legacyAllowAnyFileType().mandatory()) + .add(attr("dynamic_runtime_libs", LABEL_LIST).legacyAllowAnyFileType()) /* <!-- #BLAZE_RULE(cc_toolchain).ATTRIBUTE(module_map) --> Module map artifact to be used for modular builds. <!-- #END_BLAZE_RULE.ATTRIBUTE -->*/ diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderTest.java index ed2281386cfc53..af9e0d903cb09e 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderTest.java @@ -409,4 +409,107 @@ public void testFissionConfigWithMissingDwp() throws Exception { getConfiguredTarget("//a:a"); assertContainsEvent("Tool path for 'dwp' is missing"); } + + @Test + public void testRuntimeLibsAttributesAreNotObligatory() throws Exception { + scratch.file( + "a/BUILD", + "filegroup(name='empty') ", + "cc_toolchain_suite(", + " name = 'a',", + " toolchains = { 'k8': ':b' },", + ")", + "cc_toolchain(", + " name = 'b',", + " cpu = 'banana',", + " all_files = ':empty',", + " ar_files = ':empty',", + " as_files = ':empty',", + " compiler_files = ':empty',", + " dwp_files = ':empty',", + " linker_files = ':empty',", + " strip_files = ':empty',", + " objcopy_files = ':empty',", + " proto = \"\"\"", + " toolchain_identifier: \"a\"", + " host_system_name: \"a\"", + " target_system_name: \"a\"", + " target_cpu: \"a\"", + " target_libc: \"a\"", + " compiler: \"a\"", + " abi_version: \"a\"", + " abi_libc_version: \"a\"", + "\"\"\")"); + reporter.removeHandler(failFastHandler); + useConfiguration("--cpu=k8", "--host_cpu=k8"); + getConfiguredTarget("//a:a"); + assertNoEvents(); + } + + @Test + public void testWhenRuntimeLibsAtttributesMandatoryWhenSupportsEmbeddedRuntimes() + throws Exception { + scratch.file( + "a/BUILD", + "filegroup(name='empty') ", + "cc_toolchain_suite(", + " name = 'a',", + " toolchains = { 'k8': ':b', 'k9': ':c' },", + ")", + "cc_toolchain(", + " name = 'b',", + " cpu = 'banana',", + " all_files = ':empty',", + " ar_files = ':empty',", + " as_files = ':empty',", + " compiler_files = ':empty',", + " dwp_files = ':empty',", + " linker_files = ':empty',", + " strip_files = ':empty',", + " objcopy_files = ':empty',", + " proto = \"\"\"", + " toolchain_identifier: \"a\"", + " host_system_name: \"a\"", + " target_system_name: \"a\"", + " target_cpu: \"a\"", + " target_libc: \"a\"", + " compiler: \"a\"", + " abi_version: \"a\"", + " abi_libc_version: \"a\"", + " supports_embedded_runtimes: true,", + "\"\"\")", + "cc_toolchain(", + " name = 'c',", + " cpu = 'banana',", + " all_files = ':empty',", + " ar_files = ':empty',", + " as_files = ':empty',", + " compiler_files = ':empty',", + " dwp_files = ':empty',", + " linker_files = ':empty',", + " strip_files = ':empty',", + " objcopy_files = ':empty',", + " static_runtime_libs = [ ':yolo' ],", + " proto = \"\"\"", + " toolchain_identifier: \"a\"", + " host_system_name: \"a\"", + " target_system_name: \"a\"", + " target_cpu: \"a\"", + " target_libc: \"a\"", + " compiler: \"a\"", + " abi_version: \"a\"", + " abi_libc_version: \"a\"", + " supports_embedded_runtimes: true,", + "\"\"\")"); + reporter.removeHandler(failFastHandler); + useConfiguration("--cpu=k8", "--host_cpu=k8"); + getConfiguredTarget("//a:a"); + assertContainsEvent( + "Toolchain supports embedded runtimes, but didn't provide static_runtime_libs attribute."); + + useConfiguration("--cpu=k9", "--host_cpu=k9"); + getConfiguredTarget("//a:a"); + assertContainsEvent( + "Toolchain supports embedded runtimes, but didn't provide dynamic_runtime_libs attribute."); + } }