Skip to content

Commit

Permalink
Add resources to java_common.compile.
Browse files Browse the repository at this point in the history
Progress on the java sandwich bazelbuild#2614 in an effort to make the Java compilation exposed to Skylark as similar as possible to java_library's API.

PiperOrigin-RevId: 155861145
  • Loading branch information
iirina authored and kchodorow committed May 12, 2017
1 parent b006a63 commit a3f2cf8
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public final class JavaLibraryHelper {
private Artifact output;
private final List<Artifact> sourceJars = new ArrayList<>();
private final List<Artifact> sourceFiles = new ArrayList<>();
private final List<Artifact> resources = new ArrayList<>();

/**
* Contains all the dependencies; these are treated as both compile-time and runtime dependencies.
Expand Down Expand Up @@ -85,6 +86,11 @@ public JavaLibraryHelper addSourceJars(Artifact... sourceJars) {
return this.addSourceJars(Arrays.asList(sourceJars));
}

public JavaLibraryHelper addResources(Iterable<Artifact> resources) {
Iterables.addAll(this.resources, resources);
return this;
}

public JavaLibraryHelper addDep(JavaCompilationArgsProvider provider) {
checkNotNull(provider);
this.deps.add(provider);
Expand Down Expand Up @@ -152,6 +158,11 @@ public JavaCompilationArgs build(
attributes.setTargetLabel(ruleContext.getLabel());
attributes.setSourcePath(sourcePathEntries);

for (Artifact resource : resources) {
attributes.addResource(
JavaHelper.getJavaResourcePath(semantics, ruleContext, resource), resource);
}

if (isStrict() && classpathMode != JavaClasspathMode.OFF) {
JavaCompilationHelper.addDependencyArtifactsToAttributes(
attributes, deps);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ public JavaProvider create(SkylarkNestedSet compileTimeJars, SkylarkNestedSet ru
type = SkylarkList.class,
generic1 = Artifact.class,
defaultValue = "[]"
),
@Param(
name = "resources",
positional = false,
named = true,
type = SkylarkList.class,
generic1 = Artifact.class,
defaultValue = "[]"
)
}
)
Expand All @@ -190,13 +198,15 @@ public JavaProvider createJavaCompileAction(
String strictDepsMode,
ConfiguredTarget javaToolchain,
ConfiguredTarget hostJavabase,
SkylarkList<Artifact> sourcepathEntries) throws EvalException {
SkylarkList<Artifact> sourcepathEntries,
SkylarkList<Artifact> resources) throws EvalException {

JavaLibraryHelper helper =
new JavaLibraryHelper(skylarkRuleContext.getRuleContext())
.setOutput(outputJar)
.addSourceJars(sourceJars)
.addSourceFiles(sourceFiles)
.addResources(resources)
.setSourcePathEntries(sourcepathEntries)
.setJavacOpts(javacOpts);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void build(JavaSemantics semantics, RuleContext ruleContext) {
.setJarExecutable(
ruleContext.getHostConfiguration().getFragment(Jvm.class).getJavaExecutable(),
singleJar,
JavaToolchainProvider.fromRuleContext(ruleContext).getJvmOptions())
javaToolchain.getJvmOptions())
.addTransitiveInputs(javabase);
} else {
builder.setExecutable(singleJar);
Expand Down
59 changes: 59 additions & 0 deletions src/test/shell/bazel/bazel_java_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def _impl(ctx):
output = output_jar,
javac_opts = java_common.default_javac_opts(ctx, java_toolchain_attr = "_java_toolchain"),
deps = deps,
resources = ctx.files.resources,
strict_deps = "ERROR",
java_toolchain = ctx.attr._java_toolchain,
host_javabase = ctx.attr._host_javabase
Expand All @@ -91,6 +92,7 @@ java_custom_library = rule(
attrs = {
"srcs": attr.label_list(allow_files=True),
"deps": attr.label_list(),
"resources": attr.label_list(allow_files=True),
"_java_toolchain": attr.label(default = Label("@bazel_tools//tools/jdk:toolchain")),
"_host_javabase": attr.label(default = Label("//tools/defaults:jdk"))
},
Expand Down Expand Up @@ -735,5 +737,62 @@ EOF
expect_log "testTest was run"
}

function test_java_sandwich_resources_file() {
mkdir -p java/com/google/sandwich
workspace_dir="$PWD"
cd java/com/google/sandwich

touch BUILD A.java java_custom_library.bzl my_precious_resource.txt

cat > BUILD << EOF
load(':java_custom_library.bzl', 'java_custom_library')
java_custom_library(
name = "custom",
srcs = ["A.java"],
resources = ["my_precious_resource.txt"]
)
EOF
cat > A.java << EOF
package com.google.sandwich;
class A { }
EOF
write_java_custom_rule
cd "${workspace_dir}"
bazel build java/com/google/sandwich:custom > "$TEST_log" || fail "Java sandwich build failed"
unzip -l bazel-bin/java/com/google/sandwich/libcustom.jar > "$TEST_log"
expect_log "my_precious_resource.txt"
}

function test_java_sandwich_resources_filegroup() {
mkdir -p java/com/google/sandwich
workspace_dir="$PWD"
cd java/com/google/sandwich

touch BUILD A.java java_custom_library.bzl my_precious_resource.txt my_other_precious_resource.txt

cat > BUILD << EOF
load(':java_custom_library.bzl', 'java_custom_library')
filegroup(
name = "resources_group",
srcs = ["my_precious_resource.txt", "my_other_precious_resource.txt"]
)
java_custom_library(
name = "custom",
srcs = ["A.java"],
resources = [":resources_group"]
)
EOF
cat > A.java << EOF
package com.google.sandwich;
class A { }
EOF
write_java_custom_rule
cd "${workspace_dir}"
bazel build java/com/google/sandwich:custom > "$TEST_log" || fail "Java sandwich build failed"
unzip -l bazel-bin/java/com/google/sandwich/libcustom.jar > "$TEST_log"
expect_log "my_precious_resource.txt"
expect_log "my_other_precious_resource.txt"
}

run_suite "Java integration tests"

0 comments on commit a3f2cf8

Please sign in to comment.