Reduce number of C++ modules explicitly specified on the command line.

--
PiperOrigin-RevId: 144323833
MOS_MIGRATED_REVID=144323833
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java
index 930be36..9a91c3c 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java
@@ -215,6 +215,9 @@
   /** Set when a two-stage input discovery is used. */
   private Collection<Artifact> usedModules = null;
 
+  /** Used modules that are not transitively used through other topLevelModules. */
+  private Collection<Artifact> topLevelModules = null;
+
   private CcToolchainFeatures.Variables overwrittenVariables = null;
 
   private ImmutableList<Artifact> resolvedInputs = ImmutableList.<Artifact>of();
@@ -502,6 +505,7 @@
     if (shouldPruneModules) {
       Set<Artifact> initialResultSet = Sets.newLinkedHashSet(initialResult);
       usedModules = Sets.newLinkedHashSet();
+      topLevelModules = null;
       for (CppCompilationContext.TransitiveModuleHeaders usedModule :
           context.getUsedModules(usePic, initialResultSet)) {
         usedModules.add(usedModule.getModule());
@@ -557,6 +561,13 @@
         }
       }
     }
+    ImmutableSet.Builder<Artifact> topLevelModules = ImmutableSet.builder();
+    for (Artifact artifact : this.usedModules) {
+      if (!additionalModules.contains(artifact)) {
+        topLevelModules.add(artifact);
+      }
+    }
+    this.topLevelModules = topLevelModules.build();
     this.additionalInputs =
         new ImmutableList.Builder<Artifact>()
             .addAll(this.additionalInputs)
@@ -569,8 +580,11 @@
   @Override
   public Iterable<Artifact> getInputsWhenSkippingInputDiscovery() {
     if (useHeaderModules) {
-      this.additionalInputs = context.getTransitiveModules(usePic).toCollection();
-      return this.additionalInputs;
+      additionalInputs = Sets.newLinkedHashSet(context.getTransitiveModules(usePic).toCollection());
+      // Here, we cannot really know what the top-level modules are, so we just mark all
+      // transitive modules as "top level".
+      topLevelModules = additionalInputs;
+      return additionalInputs;
     }
     return null;
   }
@@ -1012,11 +1026,14 @@
   /** Sets module file flags based on the action's inputs. */
   protected void setModuleFileFlags() {
     if (useHeaderModules) {
-      // If modules pruning is used, modules will be supplied via additionalInputs, otherwise they
+      // If modules pruning is used, modules will be supplied via topLevelModules, otherwise they
       // are regular inputs.
-      Preconditions.checkNotNull(additionalInputs);
-      this.overwrittenVariables =
-          getOverwrittenVariables(shouldPruneModules ? additionalInputs : getInputs());
+      if (shouldPruneModules) {
+        Preconditions.checkNotNull(this.topLevelModules);
+        overwrittenVariables = getOverwrittenVariables(topLevelModules);
+      } else {
+        overwrittenVariables = getOverwrittenVariables(getInputs());
+      }
     }
   }