From c3ab8eab9494518d1c7ca91e8229266e195ab442 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Wed, 6 Nov 2024 12:05:10 +0000 Subject: [PATCH] Bazel aspects: allow sharing source jars and allow empty sourcejars (#757) * Use labelname when declaring output for unzipped sourcejar * Handle empty source jars --- .../src/main/java/srcjar_example/BUILD | 15 +++++++++++++++ .../src/main/java/srcjar_example/Baz.java | 7 +++++++ .../src/main/resources/scip-java/scip_java.bzl | 4 ++-- 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 examples/bazel-example/src/main/java/srcjar_example/Baz.java diff --git a/examples/bazel-example/src/main/java/srcjar_example/BUILD b/examples/bazel-example/src/main/java/srcjar_example/BUILD index ff65eb50..dce48b36 100644 --- a/examples/bazel-example/src/main/java/srcjar_example/BUILD +++ b/examples/bazel-example/src/main/java/srcjar_example/BUILD @@ -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", ], ) diff --git a/examples/bazel-example/src/main/java/srcjar_example/Baz.java b/examples/bazel-example/src/main/java/srcjar_example/Baz.java new file mode 100644 index 00000000..d7b7f387 --- /dev/null +++ b/examples/bazel-example/src/main/java/srcjar_example/Baz.java @@ -0,0 +1,7 @@ +package com.testing; + +public class Baz { + public Bar baz(Bar value) { + return value; + } +} diff --git a/scip-java/src/main/resources/scip-java/scip_java.bzl b/scip-java/src/main/resources/scip-java/scip_java.bzl index 8976d4e6..1525597f 100644 --- a/scip-java/src/main/resources/scip-java/scip_java.bzl +++ b/scip-java/src/main/resources/scip-java/scip_java.bzl @@ -70,7 +70,7 @@ 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( @@ -78,7 +78,7 @@ def _scip_java(target, ctx): 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,