aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar elenairina <elenairina@google.com>2017-12-12 05:03:01 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2017-12-12 05:04:55 -0800
commit03964c8ccb20d673add76c7f37245e837c3899b6 (patch)
tree95717ba052b66975414c42f0056383b93588b3a9
parent4f5a92b78ac23adcf23128eef51b99f6147f1027 (diff)
[java_common.compile] Name output source jar relative to the output jar name
instead of the rule name. RELNOTES: None. PiperOrigin-RevId: 178747070
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java2
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkCommon.java8
-rw-r--r--src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java71
3 files changed, 76 insertions, 5 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 75877350ac..0e09320c5f 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
@@ -882,7 +882,7 @@ public final class JavaCompilationHelper {
* prefix and removing the extension and replacing it by the given suffix.
* The new artifact will have the same root as the given one.
*/
- private static Artifact derivedArtifact(
+ static Artifact derivedArtifact(
RuleContext ruleContext, Artifact artifact, String prefix, String suffix) {
PathFragment path = artifact.getRootRelativePath();
String basename = FileSystemUtils.removeExtension(path.getBaseName()) + suffix;
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkCommon.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkCommon.java
index 36a876c78c..d9d14a4eed 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkCommon.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkCommon.java
@@ -434,7 +434,7 @@ public class JavaSkylarkCommon {
boolean generateMergedSourceJar = (sourceJars.size() > 1 || !sourceFiles.isEmpty())
|| (sourceJars.isEmpty() && sourceFiles.isEmpty() && !exports.isEmpty());
Artifact outputSourceJar =
- generateMergedSourceJar ? getSourceJar(skylarkRuleContext) : sourceJars.get(0);
+ generateMergedSourceJar ? getSourceJar(skylarkRuleContext, outputJar) : sourceJars.get(0);
JavaCompilationArtifacts artifacts =
helper.build(
@@ -481,9 +481,9 @@ public class JavaSkylarkCommon {
.build();
}
- private static Artifact getSourceJar(SkylarkRuleContext skylarkRuleContext) throws EvalException {
- return skylarkRuleContext.getRuleContext()
- .getBinArtifact("lib" + skylarkRuleContext.getLabel().getName() + "-src.jar");
+ private static Artifact getSourceJar(SkylarkRuleContext skylarkRuleContext, Artifact outputJar) {
+ return JavaCompilationHelper.derivedArtifact(
+ skylarkRuleContext.getRuleContext(), outputJar, "", "-src.jar");
}
private static Artifact buildIjar(
diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java b/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java
index dcfe291e1a..4624159196 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java
@@ -265,6 +265,77 @@ public class JavaSkylarkApiTest extends BuildViewTestCase {
}
@Test
+ public void testJavaCommonCompileSourceJarName() throws Exception {
+ writeBuildFileForJavaToolchain();
+ scratch.file(
+ "java/test/BUILD",
+ "load(':custom_rule.bzl', 'java_custom_library')",
+ "java_custom_library(",
+ " name = 'custom',",
+ " srcs = ['Main.java'],",
+ " deps = [':dep']",
+ ")",
+ "java_library(",
+ " name = 'dep',",
+ " srcs = [ 'Dep.java'],",
+ ")");
+ scratch.file(
+ "java/test/custom_rule.bzl",
+ "def _impl(ctx):",
+ " output_jar = ctx.actions.declare_file('amazing.jar')",
+ " other_output_jar = ctx.actions.declare_file('wonderful.jar')",
+ " deps = [dep[java_common.provider] for dep in ctx.attr.deps]",
+ " compilation_provider = java_common.compile(",
+ " ctx,",
+ " source_files = ctx.files.srcs,",
+ " output = output_jar,",
+ " javac_opts = java_common.default_javac_opts(",
+ " ctx, java_toolchain_attr = '_java_toolchain'),",
+ " deps = deps,",
+ " java_toolchain = ctx.attr._java_toolchain,",
+ " host_javabase = ctx.attr._host_javabase",
+ " )",
+ " other_compilation_provider = java_common.compile(",
+ " ctx,",
+ " source_files = ctx.files.srcs,",
+ " output = other_output_jar,",
+ " javac_opts = java_common.default_javac_opts(",
+ " ctx, java_toolchain_attr = '_java_toolchain'),",
+ " deps = deps,",
+ " java_toolchain = ctx.attr._java_toolchain,",
+ " host_javabase = ctx.attr._host_javabase",
+ " )",
+ " result_provider = java_common.merge([compilation_provider, other_compilation_provider])",
+ " return struct(",
+ " files = depset([output_jar]),",
+ " providers = [result_provider]",
+ " )",
+ "java_custom_library = rule(",
+ " implementation = _impl,",
+ " outputs = {",
+ " 'my_output': 'amazing.jar',",
+ " 'my_second_output': 'wonderful.jar'",
+ " },",
+ " attrs = {",
+ " 'srcs': attr.label_list(allow_files=['.java']),",
+ " 'deps': attr.label_list(),",
+ " '_java_toolchain': attr.label(default = Label('//java/com/google/test:toolchain')),",
+ " '_host_javabase': attr.label(default = Label('//tools/defaults:jdk'))",
+ " },",
+ " fragments = ['java']",
+ ")");
+
+ ConfiguredTarget configuredTarget = getConfiguredTarget("//java/test:custom");
+ JavaInfo info = configuredTarget.get(JavaInfo.PROVIDER);
+ SkylarkList<Artifact> sourceJars = info.getSourceJars();
+ NestedSet<Artifact> transitiveSourceJars = info.getTransitiveSourceJars();
+ assertThat(artifactFilesNames(sourceJars)).containsExactly(
+ "amazing-src.jar", "wonderful-src.jar");
+ assertThat(artifactFilesNames(transitiveSourceJars))
+ .containsExactly("libdep-src.jar", "amazing-src.jar", "wonderful-src.jar");
+ }
+
+ @Test
public void testJavaCommonCompileWithOnlyOneSourceJar() throws Exception {
writeBuildFileForJavaToolchain();
scratch.file(