aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar tomlu <tomlu@google.com>2018-02-14 16:25:36 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2018-02-14 16:27:31 -0800
commitf4c5314803d2f2fdd1c6a37324f7f2ebd2d908ca (patch)
treefe87ed3e4d0e04ff32c1b6c185ac29db6b72949e
parentb97150dab5b3284eb8c1a573289077c03fc0bb2e (diff)
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
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java36
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaCompileAction.java10
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaHeaderCompileAction.java10
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java17
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java7
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java17
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java3
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java3
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspectCommon.java10
9 files changed, 92 insertions, 21 deletions
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 849b9d0595..a4eb03e9a7 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;
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 @@ public final class JavaCompilationHelper {
builder.setTargetLabel(
attributes.getTargetLabel() == null
? ruleContext.getLabel() : attributes.getTargetLabel());
+ builder.setInjectingRuleKind(attributes.getInjectingRuleKind());
getAnalysisEnvironment().registerAction(builder.build());
}
@@ -406,6 +408,7 @@ public final class JavaCompilationHelper {
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 @@ public final class JavaCompilationHelper {
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 @@ public final class JavaCompilationHelper {
* 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 @@ public final class JavaCompilationHelper {
.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 42e8543517..2496d62bf9 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.List;
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 @@ public final class JavaCompileAction extends SpawnAction {
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 @@ public final class JavaCompileAction extends SpawnAction {
result.addPrefixedLabel("@", targetLabel);
}
}
+ if (injectingRuleKind != null) {
+ result.add("--injecting_rule_kind", injectingRuleKind);
+ }
if (!classpathEntries.isEmpty()) {
result.addExecPaths("--classpath", classpathEntries);
@@ -1000,5 +1005,10 @@ public final class JavaCompileAction extends SpawnAction {
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 8b8c72a925..b8d30cb54e 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 @@ public class JavaHeaderCompileAction extends SpawnAction {
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 @@ public class JavaHeaderCompileAction extends SpawnAction {
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 @@ public class JavaHeaderCompileAction extends SpawnAction {
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 cdd553f414..cd2866d4d4 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 @@ public class JavaImport implements RuleConfiguredTargetFactory {
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 0ba0cda601..8baf2c41e5 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 @@ public final class JavaLibraryHelper {
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 @@ public final class JavaLibraryHelper {
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 @@ public final class JavaLibraryHelper {
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 89a10faad2..5e080aec8d 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.LinkedHashSet;
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 @@ public class JavaTargetAttributes {
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 @@ public class JavaTargetAttributes {
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 @@ public class JavaTargetAttributes {
directJars.build(),
compileTimeDependencyArtifacts.build(),
targetLabel,
+ injectingRuleKind,
excludedArtifacts,
strictJavaDeps);
}
@@ -437,6 +446,7 @@ public class JavaTargetAttributes {
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 @@ public class JavaTargetAttributes {
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 @@ public class JavaTargetAttributes {
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 @@ public class JavaTargetAttributes {
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 d486b0e756..82accda1b0 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 @@ public class JavaLiteProtoAspect extends NativeAspectClass implements Configured
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 93f8482048..e910ed86eb 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 @@ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspe
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 101101bb25..e1d15777f4 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 @@ public class JavaProtoAspectCommon {
* 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));