Skip to content

Commit

Permalink
Bazel aspects: allow sharing source jars and allow empty sourcejars (#…
Browse files Browse the repository at this point in the history
…757)

* Use labelname when declaring output for unzipped sourcejar
* Handle empty source jars
  • Loading branch information
antonsviridov-src authored Nov 6, 2024
1 parent 6c183d8 commit c3ab8ea
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
15 changes: 15 additions & 0 deletions examples/bazel-example/src/main/java/srcjar_example/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@ genrule(
cmd = "echo 'package com.testing; public class Bar {};' > Bar.java && jar cf $(@) Bar.java",
)

genrule(
name = "empty-srcjar",
outs = ["empty.srcjar"],
cmd = "touch test.txt && zip $(@) test.txt && zip -d $(@) test.txt",
)

java_library(
name = "testing",
srcs = [
"Foo.java",
":generated-srcjar",
":empty-srcjar"
],
)

java_library(
name = "other_library",
srcs = [
"Baz.java", # create a new file in source at test/Baz.java, alongside test/Foo.java
":generated-srcjar",
],
)
7 changes: 7 additions & 0 deletions examples/bazel-example/src/main/java/srcjar_example/Baz.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.testing;

public class Baz {
public Bar baz(Bar value) {
return value;
}
}
4 changes: 2 additions & 2 deletions scip-java/src/main/resources/scip-java/scip_java.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ def _scip_java(target, ctx):
output_dir = []

for source_jar in source_jars:
dir = ctx.actions.declare_directory("extracted_srcjar/" + source_jar.short_path)
dir = ctx.actions.declare_directory(ctx.label.name + "/extracted_srcjar/" + source_jar.short_path)
output_dir.append(dir)

ctx.actions.run_shell(
inputs = javac_action.inputs,
outputs = [dir],
mnemonic = "ExtractSourceJars",
command = """
unzip {input_file} -d {output_dir}
[ "$(unzip -q -l {input_file} | wc -l)" -eq 0 ] || unzip {input_file} -d {output_dir}
""".format(
output_dir = dir.path,
input_file = source_jar.path,
Expand Down

0 comments on commit c3ab8ea

Please sign in to comment.