Skip to content

Commit

Permalink
Fix parameters of java_common.compile and java_common.pack_sources in…
Browse files Browse the repository at this point in the history
… Bazel/Blaze tests.

Parameter host_javabase is removed and output_jar replaced with output_source_jar.

This is preparation for flipping incompatible_java_common_parameters.
See #12373

PiperOrigin-RevId: 341043592
  • Loading branch information
comius authored and copybara-github committed Nov 6, 2020
1 parent 9a2893d commit 95ed8c6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.google.devtools.build.lib.packages.StarlarkProvider;
import com.google.devtools.build.lib.packages.StructImpl;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar;
import com.google.devtools.build.lib.testutil.TestConstants;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand All @@ -34,9 +33,6 @@
@RunWith(JUnit4.class)
public class JavaInfoStarlarkApiTest extends BuildViewTestCase {

private static final String HOST_JAVA_RUNTIME_LABEL =
TestConstants.TOOLS_REPOSITORY + "//tools/jdk:current_host_java_runtime";

@Test
public void buildHelperCreateJavaInfoWithOutputJarOnly() throws Exception {
ruleBuilder().build();
Expand Down Expand Up @@ -247,37 +243,6 @@ public void buildHelperPackSources_repackSingleJar() throws Exception {
.containsExactly("foo/my_starlark_rule_lib-src.jar");
}

@Test
public void buildHelperCreateJavaInfoWithSourceFiles_namingSourceJarFromOutputJar()
throws Exception {
ruleBuilder().withSourceFilesFromJar().build();

scratch.file(
"foo/BUILD",
"load(':extension.bzl', 'my_rule')",
"my_rule(",
" name = 'my_starlark_rule',",
" output_jar = 'my_starlark_rule_lib.jar',",
" sources = ['ClassA.java', 'ClassB.java', 'ClassC.java', 'ClassD.java'],",
")");
assertNoEvents();

JavaRuleOutputJarsProvider javaRuleOutputJarsProvider =
fetchJavaInfo().getProvider(JavaRuleOutputJarsProvider.class);

assertThat(prettyArtifactNames(javaRuleOutputJarsProvider.getAllSrcOutputJars()))
.containsExactly("foo/my_starlark_rule_lib-src.jar");

JavaSourceJarsProvider sourceJarsProvider =
fetchJavaInfo().getProvider(JavaSourceJarsProvider.class);

assertThat(prettyArtifactNames(sourceJarsProvider.getSourceJars()))
.containsExactly("foo/my_starlark_rule_lib-src.jar");

assertThat(prettyArtifactNames(sourceJarsProvider.getTransitiveSourceJars()))
.containsExactly("foo/my_starlark_rule_lib-src.jar");
}

@Test
public void buildHelperCreateJavaInfoWithSourcesFiles() throws Exception {
ruleBuilder().withSourceFiles().build();
Expand Down Expand Up @@ -733,7 +698,6 @@ private class RuleBuilder {
private boolean useIJar = false;
private boolean stampJar;
private boolean neverLink = false;
private boolean sourceFilesFromJar = false;
private boolean sourceFiles = false;

private RuleBuilder withIJar() {
Expand All @@ -751,11 +715,6 @@ private RuleBuilder withNeverLink() {
return this;
}

private RuleBuilder withSourceFilesFromJar() {
sourceFilesFromJar = true;
return this;
}

private RuleBuilder withSourceFiles() {
sourceFiles = true;
return this;
Expand All @@ -782,22 +741,15 @@ private String[] newJavaInfo() {
} else {
lines.add(" compile_jar = ctx.outputs.output_jar");
}
if (sourceFiles || sourceFilesFromJar) {
String outputJar = sourceFilesFromJar ? " output_jar = ctx.outputs.output_jar," : "";
String outputSrcJar =
sourceFiles
? " output_source_jar = ctx.actions.declare_file("
+ " ctx.outputs.output_jar.basename[:-4] + '-src.jar'),"
: "";
if (sourceFiles) {
lines.add(
" source_jar = java_common.pack_sources(",
" ctx.actions,",
outputJar,
outputSrcJar,
" output_source_jar = ",
" ctx.actions.declare_file(ctx.outputs.output_jar.basename[:-4] + '-src.jar'),",
" sources = ctx.files.sources,",
" source_jars = ctx.files.source_jars,",
" java_toolchain = ctx.attr._toolchain[java_common.JavaToolchainInfo],",
" host_javabase = ctx.attr._host_javabase[java_common.JavaRuntimeInfo],",
")");
} else {
lines.add(
Expand All @@ -822,7 +774,7 @@ private String[] newJavaInfo() {
}

private void build() throws Exception {
if (useIJar || stampJar || sourceFiles || sourceFilesFromJar) {
if (useIJar || stampJar || sourceFiles) {
writeBuildFileForJavaToolchain();
}

Expand All @@ -846,14 +798,9 @@ private void build() throws Exception {
" 'source_jars' : attr.label_list(allow_files=['.jar']),",
" 'sources' : attr.label_list(allow_files=['.java']),",
" 'jdeps' : attr.label(allow_single_file=True),",
useIJar || stampJar || sourceFiles || sourceFilesFromJar
useIJar || stampJar || sourceFiles
? " '_toolchain': attr.label(default = Label('//java/com/google/test:toolchain')),"
: "",
sourceFiles || sourceFilesFromJar
? " '_host_javabase': attr.label(default = Label('"
+ HOST_JAVA_RUNTIME_LABEL
+ "')),"
: "",
" }",
")");

Expand Down
6 changes: 3 additions & 3 deletions src/test/shell/bazel/bazel_java_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,7 @@ load(":my_rule.bzl", "my_rule")
my_rule(
name = 'my_starlark_rule',
output_jar = 'my_starlark_rule_lib.jar',
output_source_jar = 'my_starlark_rule_lib-src.jar',
source_jars = ['my_starlark_rule_src.jar'],
)
EOF
Expand All @@ -1462,10 +1463,9 @@ def _impl(ctx):
)
source_jar = java_common.pack_sources(
ctx.actions,
output_jar = ctx.file.output_jar,
output_source_jar = ctx.actions.declare_file(ctx.attr.output_source_jar),
source_jars = ctx.files.source_jars,
java_toolchain = ctx.attr._java_toolchain[java_common.JavaToolchainInfo],
host_javabase = ctx.attr._host_javabase[java_common.JavaRuntimeInfo],
)
javaInfo = JavaInfo(
output_jar = ctx.file.output_jar,
Expand All @@ -1478,9 +1478,9 @@ my_rule = rule(
implementation = _impl,
attrs = {
'output_jar' : attr.label(allow_single_file=True),
'output_source_jar' : attr.string(),
'source_jars' : attr.label_list(allow_files=['.jar']),
"_java_toolchain": attr.label(default = Label("@bazel_tools//tools/jdk:remote_toolchain")),
"_host_javabase": attr.label(default = Label("@bazel_tools//tools/jdk:current_host_java_runtime"))
}
)
EOF
Expand Down

0 comments on commit 95ed8c6

Please sign in to comment.