Disable protobuf modulemap generation if it's a linking target. Correctly configure protos intermediate artifacts to use the build configuration, if any provided.

--
MOS_MIGRATED_REVID=137514384
diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/ProtobufSupport.java b/src/main/java/com/google/devtools/build/lib/rules/objc/ProtobufSupport.java
index 47cd02e..9bbe1dd 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/objc/ProtobufSupport.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/objc/ProtobufSupport.java
@@ -64,6 +64,7 @@
   private final RuleContext ruleContext;
   private final BuildConfiguration buildConfiguration;
   private final ProtoAttributes attributes;
+  private final IntermediateArtifacts intermediateArtifacts;
 
   // Each entry of this map represents a generation action and a compilation action. The input set
   // are dependencies of the output set. The output set is always a subset of, or the same set as,
@@ -109,6 +110,13 @@
     this.buildConfiguration = buildConfiguration;
     this.attributes = new ProtoAttributes(ruleContext);
     this.inputsToOutputsMap = getInputsToOutputsMap();
+    if (buildConfiguration != null) {
+      this.intermediateArtifacts =
+          ObjcRuleClasses.intermediateArtifacts(ruleContext, buildConfiguration);
+    } else {
+      this.intermediateArtifacts =
+          ObjcRuleClasses.intermediateArtifacts(ruleContext, ruleContext.getConfiguration());
+    }
   }
 
   /**
@@ -132,11 +140,9 @@
   }
 
   private void registerModuleMapGenerationAction() {
-    IntermediateArtifacts moduleMapIntermediateArtifacts =
-        ObjcRuleClasses.intermediateArtifacts(ruleContext);
     CompilationArtifacts.Builder moduleMapCompilationArtifacts =
         new CompilationArtifacts.Builder()
-            .setIntermediateArtifacts(moduleMapIntermediateArtifacts)
+            .setIntermediateArtifacts(intermediateArtifacts)
             .setPchFile(Optional.<Artifact>absent())
             .addAdditionalHdrs(getProtobufHeaders())
             .addAdditionalHdrs(
@@ -144,7 +150,7 @@
 
     new CompilationSupport(
             ruleContext,
-            ObjcRuleClasses.intermediateArtifacts(ruleContext),
+            intermediateArtifacts,
             new CompilationAttributes.Builder().build())
         .registerGenerateModuleMapAction(Optional.of(moduleMapCompilationArtifacts.build()));
   }
@@ -210,12 +216,11 @@
 
     Iterable<PathFragment> userHeaderSearchPaths =
         ImmutableList.of(getWorkspaceRelativeOutputDir());
-    IntermediateArtifacts moduleMapIntermediateArtifacts =
-        ObjcRuleClasses.intermediateArtifacts(ruleContext);
-    ObjcCommon.Builder commonBuilder =
-        new ObjcCommon.Builder(ruleContext)
-            .setIntermediateArtifacts(moduleMapIntermediateArtifacts)
-            .setHasModuleMap();
+    ObjcCommon.Builder commonBuilder = new ObjcCommon.Builder(ruleContext);
+
+    if (!isLinkingTarget()) {
+      commonBuilder.setIntermediateArtifacts(intermediateArtifacts).setHasModuleMap();
+    }
 
     int actionId = 0;
     for (ImmutableSet<Artifact> inputProtos : inputsToOutputsMap.keySet()) {
@@ -249,8 +254,6 @@
     }
 
     XcodeProvider.Builder xcodeProviderBuilder = new XcodeProvider.Builder();
-    IntermediateArtifacts intermediateArtifacts =
-        ObjcRuleClasses.intermediateArtifacts(ruleContext);
     new XcodeSupport(ruleContext, intermediateArtifacts, getXcodeLabel(getBundledProtosSuffix()))
         .addXcodeSettings(xcodeProviderBuilder, getObjcProvider().get(), LIBRARY_STATIC);
 
@@ -413,6 +416,13 @@
   }
 
   private IntermediateArtifacts getUniqueIntermediateArtifacts(int actionId) {
+    if (buildConfiguration != null) {
+      return new IntermediateArtifacts(
+          ruleContext,
+          getUniqueBundledProtosSuffix(actionId),
+          getUniqueBundledProtosPrefix(actionId),
+          buildConfiguration);
+    }
     return new IntermediateArtifacts(
         ruleContext,
         getUniqueBundledProtosSuffix(actionId),