Don't attempt LTO indexing of cc_binary if there are no bitcode inputs to link.

The problem is that the linker will not produce the expected .params file output
if there are no inputs, and there is no need for this step in that case.
This required adding a method to check if there are any bitcode inputs to
the link either directly or indirectly via a library. This is the same walk done
by createLtoArtifacts once we decide to do an LTO indexing step.

RELNOTES: NONE
PiperOrigin-RevId: 168281516
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java
index c613ff0..8c91d6b 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java
@@ -288,7 +288,8 @@
     Iterable<LtoBackendArtifacts> ltoBackendArtifacts = ImmutableList.of();
     boolean usePic = CppHelper.usePic(ruleContext, !isLinkShared(ruleContext));
 
-    if (featureConfiguration.isEnabled(CppRuleClasses.THIN_LTO)) {
+    if (linkActionBuilder.hasLtoBitcodeInputs()
+        && featureConfiguration.isEnabled(CppRuleClasses.THIN_LTO)) {
       linkActionBuilder.setLtoIndexing(true);
       linkActionBuilder.setUsePicForLtoBackendActions(usePic);
       CppLinkAction indexAction = linkActionBuilder.build();
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionBuilder.java
index d2a0317..d4f3e4e 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionBuilder.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionBuilder.java
@@ -498,6 +498,22 @@
     return uniqueLibrariesBuilder.build();
   }
 
+  /**
+   * Returns true if there are any LTO bitcode inputs to this link, either directly transitively via
+   * library inputs.
+   */
+  boolean hasLtoBitcodeInputs() {
+    if (!ltoBitcodeFiles.isEmpty()) {
+      return true;
+    }
+    for (LibraryToLink lib : libraries.build()) {
+      if (!lib.getLtoBitcodeFiles().isEmpty()) {
+        return true;
+      }
+    }
+    return false;
+  }
+
   private Iterable<LtoBackendArtifacts> createLtoArtifacts(
       PathFragment ltoOutputRootPrefix, NestedSet<LibraryToLink> uniqueLibraries) {
     Set<Artifact> compiled = new LinkedHashSet<>();