Pass --target_label, --injecting_rule_kind to all java compile actions.

JavaBuilder and friends will write this into the manifest of the produced jars to assist with add_dep commands, when strict_deps is violated.

This will obviate the need for blaze to pass jar owners on the command line.

PiperOrigin-RevId: 185763422
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java
index 849b9d0..a4eb03e 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java
@@ -34,6 +34,7 @@
 import com.google.devtools.build.lib.analysis.config.BuildConfiguration.StrictDepsMode;
 import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode;
 import com.google.devtools.build.lib.analysis.test.InstrumentedFilesCollector;
+import com.google.devtools.build.lib.cmdline.Label;
 import com.google.devtools.build.lib.collect.nestedset.NestedSet;
 import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
 import com.google.devtools.build.lib.collect.nestedset.Order;
@@ -230,6 +231,7 @@
     builder.setTargetLabel(
         attributes.getTargetLabel() == null
             ? ruleContext.getLabel() : attributes.getTargetLabel());
+    builder.setInjectingRuleKind(attributes.getInjectingRuleKind());
     getAnalysisEnvironment().registerAction(builder.build());
   }
 
@@ -406,6 +408,7 @@
     builder.setCompileTimeDependencyArtifacts(attributes.getCompileTimeDependencyArtifacts());
     builder.setDirectJars(attributes.getDirectJars());
     builder.setTargetLabel(attributes.getTargetLabel());
+    builder.setInjectingRuleKind(attributes.getInjectingRuleKind());
     builder.setAdditionalInputs(NestedSetBuilder.wrap(Order.LINK_ORDER, additionalJavaBaseInputs));
     builder.setJavacJar(javaToolchain.getJavac());
     builder.setToolsJars(javaToolchain.getTools());
@@ -653,7 +656,15 @@
     if (shouldUseHeaderCompilation()) {
       jar = createHeaderCompilationAction(runtimeJar, builder);
     } else if (getJavaConfiguration().getUseIjars()) {
-      jar = createIjarAction(ruleContext, javaToolchain, runtimeJar, false);
+      JavaTargetAttributes attributes = getAttributes();
+      jar =
+          createIjarAction(
+              ruleContext,
+              javaToolchain,
+              runtimeJar,
+              attributes.getTargetLabel(),
+              attributes.getInjectingRuleKind(),
+              false);
     } else {
       jar = runtimeJar;
       isFullJar = true;
@@ -829,17 +840,28 @@
    * Creates the Action that creates ijars from Jar files.
    *
    * @param inputJar the Jar to create the ijar for
-   * @param addPrefix whether to prefix the path of the generated ijar with the package and
-   *     name of the current rule
+   * @param addPrefix whether to prefix the path of the generated ijar with the package and name of
+   *     the current rule
    * @return the Artifact to create with the Action
    */
   protected static Artifact createIjarAction(
       RuleContext ruleContext,
       JavaToolchainProvider javaToolchain,
-      Artifact inputJar, boolean addPrefix) {
+      Artifact inputJar,
+      @Nullable Label targetLabel,
+      @Nullable String injectingRuleKind,
+      boolean addPrefix) {
     Artifact interfaceJar = getIjarArtifact(ruleContext, inputJar, addPrefix);
     FilesToRunProvider ijarTarget = javaToolchain.getIjar();
     if (!ruleContext.hasErrors()) {
+      CustomCommandLine.Builder commandLine =
+          CustomCommandLine.builder().addExecPath(inputJar).addExecPath(interfaceJar);
+      if (targetLabel != null) {
+        commandLine.addLabel("--target_label", targetLabel);
+      }
+      if (injectingRuleKind != null) {
+        commandLine.add("--injecting_rule_kind", injectingRuleKind);
+      }
       ruleContext.registerAction(
           new SpawnAction.Builder()
               .addInput(inputJar)
@@ -851,11 +873,7 @@
               .useDefaultShellEnvironment()
               .setProgressMessage("Extracting interface %s", ruleContext.getLabel())
               .setMnemonic("JavaIjar")
-              .addCommandLine(
-                  CustomCommandLine.builder()
-                      .addExecPath(inputJar)
-                      .addExecPath(interfaceJar)
-                      .build())
+              .addCommandLine(commandLine.build())
               .build(ruleContext));
     }
     return interfaceJar;
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompileAction.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompileAction.java
index 42e8543..2496d62 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompileAction.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompileAction.java
@@ -68,6 +68,7 @@
 import java.util.Map;
 import java.util.Set;
 import java.util.stream.Stream;
+import javax.annotation.Nullable;
 
 /** Action that represents a Java compilation. */
 @ThreadCompatible
@@ -493,6 +494,7 @@
     private NestedSet<Artifact> processorPath = NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER);
     private final List<String> processorNames = new ArrayList<>();
     private Label targetLabel;
+    @Nullable private String injectingRuleKind;
 
     /**
      * Creates a Builder from an owner and a build configuration.
@@ -724,6 +726,9 @@
           result.addPrefixedLabel("@", targetLabel);
         }
       }
+      if (injectingRuleKind != null) {
+        result.add("--injecting_rule_kind", injectingRuleKind);
+      }
 
       if (!classpathEntries.isEmpty()) {
         result.addExecPaths("--classpath", classpathEntries);
@@ -1000,5 +1005,10 @@
       this.targetLabel = targetLabel;
       return this;
     }
+
+    public Builder setInjectingRuleKind(@Nullable String injectingRuleKind) {
+      this.injectingRuleKind = injectingRuleKind;
+      return this;
+    }
   }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaHeaderCompileAction.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaHeaderCompileAction.java
index 8b8c72a..b8d30cb 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaHeaderCompileAction.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaHeaderCompileAction.java
@@ -190,6 +190,7 @@
         NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER);
     private ImmutableList<Artifact> bootclasspathEntries = ImmutableList.<Artifact>of();
     @Nullable private Label targetLabel;
+    @Nullable private String injectingRuleKind;
     private PathFragment tempDirectory;
     private BuildConfiguration.StrictDepsMode strictJavaDeps
         = BuildConfiguration.StrictDepsMode.OFF;
@@ -290,6 +291,12 @@
       return this;
     }
 
+    /** Sets the injecting rule kind of the target being compiled. */
+    public Builder setInjectingRuleKind(@Nullable String injectingRuleKind) {
+      this.injectingRuleKind = injectingRuleKind;
+      return this;
+    }
+
     /**
      * Sets the path to a temporary directory, e.g. for extracting sourcejar entries to before
      * compilation.
@@ -559,6 +566,9 @@
           result.addPrefixedLabel("@", targetLabel);
         }
       }
+      if (injectingRuleKind != null) {
+        result.add("--injecting_rule_kind", injectingRuleKind);
+      }
       result.addExecPaths("--classpath", classpathEntries);
       return result;
     }
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java
index cdd553f..cd2866d 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java
@@ -242,13 +242,16 @@
     ImmutableList.Builder<Artifact> interfaceJarsBuilder = ImmutableList.builder();
     boolean useIjar = ruleContext.getFragment(JavaConfiguration.class).getUseIjars();
     for (Artifact jar : jars) {
-      Artifact interfaceJar = useIjar
-          ? JavaCompilationHelper.createIjarAction(
-              ruleContext,
-              JavaCompilationHelper.getJavaToolchainProvider(ruleContext),
-              jar,
-              true)
-          : jar;
+      Artifact interfaceJar =
+          useIjar
+              ? JavaCompilationHelper.createIjarAction(
+                  ruleContext,
+                  JavaCompilationHelper.getJavaToolchainProvider(ruleContext),
+                  jar,
+                  ruleContext.getLabel(),
+                  /* injectingRuleKind */ null,
+                  true)
+              : jar;
       interfaceJarsBuilder.add(interfaceJar);
       compilationToRuntimeJarMap.put(interfaceJar, jar);
     }
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java
index 0ba0cda..8baf2c4 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java
@@ -60,6 +60,7 @@
   private ImmutableList<Artifact> sourcePathEntries = ImmutableList.of();
   private StrictDepsMode strictDepsMode = StrictDepsMode.OFF;
   private JavaClasspathMode classpathMode = JavaClasspathMode.OFF;
+  private String injectingRuleKind;
 
   public JavaLibraryHelper(RuleContext ruleContext) {
     this.ruleContext = ruleContext;
@@ -140,6 +141,11 @@
     return this;
   }
 
+  public JavaLibraryHelper setInjectingRuleKind(String injectingRuleKind) {
+    this.injectingRuleKind = injectingRuleKind;
+    return this;
+  }
+
   /**
    * When in strict mode, compiling the source-jars passed to this JavaLibraryHelper will break if
    * they depend on classes not in any of the {@link
@@ -187,6 +193,7 @@
     addDepsToAttributes(attributes);
     attributes.setStrictJavaDeps(strictDepsMode);
     attributes.setTargetLabel(ruleContext.getLabel());
+    attributes.setInjectingRuleKind(injectingRuleKind);
     attributes.setSourcePath(sourcePathEntries);
     JavaCommon.addPlugins(attributes, plugins);
 
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java
index 89a10fa..5e080ae 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java
@@ -33,6 +33,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import javax.annotation.Nullable;
 
 /**
  * An object that captures the temporary state we need to pass around while
@@ -94,6 +95,7 @@
     private final NestedSetBuilder<Artifact> compileTimeDependencyArtifacts =
         NestedSetBuilder.stableOrder();
     private Label targetLabel;
+    @Nullable private String injectingRuleKind;
 
     private final NestedSetBuilder<Artifact> excludedArtifacts =
         NestedSetBuilder.naiveLinkOrder();
@@ -181,6 +183,12 @@
       return this;
     }
 
+    public Builder setInjectingRuleKind(@Nullable String injectingRuleKind) {
+      Preconditions.checkArgument(!built);
+      this.injectingRuleKind = injectingRuleKind;
+      return this;
+    }
+
     /**
      * Sets the bootclasspath to be passed to the Java compiler.
      *
@@ -367,6 +375,7 @@
           directJars.build(),
           compileTimeDependencyArtifacts.build(),
           targetLabel,
+          injectingRuleKind,
           excludedArtifacts,
           strictJavaDeps);
     }
@@ -437,6 +446,7 @@
   private final NestedSet<Artifact> directJars;
   private final NestedSet<Artifact> compileTimeDependencyArtifacts;
   private final Label targetLabel;
+  @Nullable private String injectingRuleKind;
 
   private final NestedSet<Artifact> excludedArtifacts;
   private final BuildConfiguration.StrictDepsMode strictJavaDeps;
@@ -462,6 +472,7 @@
       NestedSet<Artifact> directJars,
       NestedSet<Artifact> compileTimeDependencyArtifacts,
       Label targetLabel,
+      @Nullable String injectingRuleKind,
       NestedSetBuilder<Artifact> excludedArtifacts,
       BuildConfiguration.StrictDepsMode strictJavaDeps) {
     this.sourceFiles = ImmutableSet.copyOf(sourceFiles);
@@ -487,6 +498,7 @@
     this.additionalOutputs = ImmutableSet.copyOf(additionalOutputs);
     this.compileTimeDependencyArtifacts = compileTimeDependencyArtifacts;
     this.targetLabel = targetLabel;
+    this.injectingRuleKind = injectingRuleKind;
     this.excludedArtifacts = excludedArtifacts.build();
     this.strictJavaDeps = strictJavaDeps;
   }
@@ -624,6 +636,11 @@
     return targetLabel;
   }
 
+  @Nullable
+  public String getInjectingRuleKind() {
+    return injectingRuleKind;
+  }
+
   public BuildConfiguration.StrictDepsMode getStrictJavaDeps() {
     return strictJavaDeps;
   }
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java
index d486b0e..82accda 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java
@@ -190,7 +190,8 @@
         Artifact outputJar = aspectCommon.getOutputJarArtifact();
 
         generatedCompilationArgsProvider =
-            aspectCommon.createJavaCompileAction(sourceJar, outputJar, dependencyCompilationArgs);
+            aspectCommon.createJavaCompileAction(
+                "java_lite_proto_library", sourceJar, outputJar, dependencyCompilationArgs);
 
         NestedSet<Artifact> javaSourceJars =
             NestedSetBuilder.<Artifact>stableOrder().add(sourceJar).build();
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java
index 93f8482..e910ed8 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java
@@ -203,7 +203,8 @@
         Artifact outputJar = aspectCommon.getOutputJarArtifact();
 
         generatedCompilationArgsProvider =
-            aspectCommon.createJavaCompileAction(sourceJar, outputJar, dependencyCompilationArgs);
+            aspectCommon.createJavaCompileAction(
+                "java_proto_library", sourceJar, outputJar, dependencyCompilationArgs);
 
         NestedSet<Artifact> javaSourceJars =
             NestedSetBuilder.<Artifact>stableOrder().add(sourceJar).build();
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspectCommon.java b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspectCommon.java
index 101101b..e1d1577 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspectCommon.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspectCommon.java
@@ -115,13 +115,17 @@
    * Registers an action that compiles the given {@code sourceJar} and archives the compiled classes
    * into {@code outputJar}, using {@code dep} as information about the dependencies compilation.
    *
-   * @return a {@JavaCompilationArgsProvider} wrapping information about the compilation action
-   * that was registered.
+   * @return a {@JavaCompilationArgsProvider} wrapping information about the compilation action that
+   *     was registered.
    */
   public JavaCompilationArgsProvider createJavaCompileAction(
-      Artifact sourceJar, Artifact outputJar, JavaCompilationArgsProvider dep) {
+      String injectingRuleKind,
+      Artifact sourceJar,
+      Artifact outputJar,
+      JavaCompilationArgsProvider dep) {
     JavaLibraryHelper helper =
         new JavaLibraryHelper(ruleContext)
+            .setInjectingRuleKind(injectingRuleKind)
             .setOutput(outputJar)
             .addSourceJars(sourceJar)
             .setJavacOpts(ProtoJavacOpts.constructJavacOpts(ruleContext));