Skip to content

Commit

Permalink
[java_common.compile] Name output source jar relative to the output j…
Browse files Browse the repository at this point in the history
…ar name

instead of the rule name.

RELNOTES: None.
PiperOrigin-RevId: 178747070
  • Loading branch information
iirina authored and Copybara-Service committed Dec 12, 2017
1 parent 4f5a92b commit 03964c8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ private static Artifact getIjarArtifact(
* 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ public JavaInfo createJavaCompileAction(
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(
Expand Down Expand Up @@ -481,9 +481,9 @@ public JavaInfo createJavaCompileAction(
.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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,77 @@ public void testJavaCommonCompileTransitiveSourceJars() throws Exception {
.containsExactly("libdep-src.jar", "libcustom-src.jar");
}

@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();
Expand Down

0 comments on commit 03964c8

Please sign in to comment.