Skip to content

Commit a4eb361

Browse files
Handle .srcjar inputs in Bazel aspect (#754)
* Unpack .srcjars in Bazel aspect and add them to scip config * Use --verbose_failures when invoking scip-java aspect * Scip buildtool can now handle folders of source files * Improve compatibility with different Bazel versions * remove redundant conversion * Add a srcjar example/test
1 parent e2d6e7e commit a4eb361

File tree

5 files changed

+91
-18
lines changed

5 files changed

+91
-18
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
genrule(
2+
name = "generated-srcjar",
3+
outs = ["sources.srcjar"],
4+
cmd = "echo 'package com.testing; public class Bar {};' > Bar.java && jar cf $(@) Bar.java",
5+
)
6+
7+
java_library(
8+
name = "testing",
9+
srcs = [
10+
"Foo.java",
11+
":generated-srcjar",
12+
],
13+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// testing/Foo.java
2+
package com.testing;
3+
4+
public class Foo {
5+
public Bar foo(Bar value) {
6+
return value;
7+
}
8+
}

scip-java/src/main/resources/scip-java/scip_java.bzl

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,36 @@ def _scip_java(target, ctx):
5757
annotations = info.annotation_processing
5858

5959
source_files = []
60+
source_jars = []
6061
for src in ctx.rule.files.srcs:
61-
source_files.append(src.path)
62+
if src.path.endswith(".java"):
63+
source_files.append(src.path)
64+
elif src.path.endswith(".srcjar"):
65+
source_jars.append(src)
66+
6267
if len(source_files) == 0:
6368
return None
69+
70+
output_dir = []
71+
72+
for source_jar in source_jars:
73+
dir = ctx.actions.declare_directory("extracted_srcjar/" + source_jar.short_path)
74+
output_dir.append(dir)
75+
76+
ctx.actions.run_shell(
77+
inputs = javac_action.inputs,
78+
outputs = [dir],
79+
mnemonic = "ExtractSourceJars",
80+
command = """
81+
unzip {input_file} -d {output_dir}
82+
""".format(
83+
output_dir = dir.path,
84+
input_file = source_jar.path,
85+
),
86+
progress_message = "Extracting source jar {jar}".format(jar = source_jar.path),
87+
)
88+
89+
source_files.append(dir.path)
6490

6591
classpath = [j.path for j in compilation.compilation_classpath.to_list()]
6692
bootclasspath = [j.path for j in compilation.boot_classpath]
@@ -73,11 +99,23 @@ def _scip_java(target, ctx):
7399

74100
launcher_javac_flags = []
75101
compiler_javac_flags = []
76-
for value in compilation.javac_options:
77-
if value.startswith("-J"):
78-
launcher_javac_flags.append(value)
79-
else:
80-
compiler_javac_flags.append(value)
102+
103+
# In different versions of bazel javac options are either a nested set or a depset or a list...
104+
javac_options = []
105+
if hasattr(compilation, "javac_options_list"):
106+
javac_options = compilation.javac_options_list
107+
else:
108+
javac_options = compilation.javac_options
109+
110+
for value in javac_options:
111+
# NOTE(Anton): for some bizarre reason I see empty string starting the list of
112+
# javac options - which then gets propagated into the JSON config, and ends up
113+
# crashing the actual javac invokation.
114+
if value != "":
115+
if value.startswith("-J"):
116+
launcher_javac_flags.append(value)
117+
else:
118+
compiler_javac_flags.append(value)
81119

82120
build_config = struct(**{
83121
"javaHome": ctx.var["java_home"],
@@ -100,6 +138,7 @@ def _scip_java(target, ctx):
100138
)
101139

102140
deps = [javac_action.inputs, annotations.processor_classpath]
141+
103142
ctx.actions.run_shell(
104143
command = "\"{}\" index --no-cleanup --index-semanticdb.allow-empty-index --cwd \"{}\" --targetroot {} --scip-config \"{}\" --output \"{}\"".format(
105144
ctx.var["scip_java_binary"],
@@ -113,7 +152,7 @@ def _scip_java(target, ctx):
113152
"NO_PROGRESS_BAR": "true",
114153
},
115154
mnemonic = "ScipJavaIndex",
116-
inputs = depset([build_config_path], transitive = deps),
155+
inputs = depset([build_config_path] + output_dir, transitive = deps),
117156
outputs = [scip_output, targetroot],
118157
)
119158

scip-java/src/main/scala/com/sourcegraph/scip_java/buildtools/BazelBuildTool.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ class BazelBuildTool(index: IndexCommand) extends BuildTool("Bazel", index) {
7171
"--output_groups=scip",
7272
s"--define=sourceroot=${index.workingDirectory}",
7373
s"--define=java_home=$javaHome",
74-
s"--define=scip_java_binary=$scipJavaBinary"
74+
s"--define=scip_java_binary=$scipJavaBinary",
75+
"--verbose_failures"
7576
) ++ targetSpecs
7677

7778
val buildExitCode = runBazelBuild(buildCommand)

scip-java/src/main/scala/com/sourcegraph/scip_java/buildtools/ScipBuildTool.scala

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -683,15 +683,8 @@ class ScipBuildTool(index: IndexCommand) extends BuildTool("SCIP", index) {
683683
Files.walkFileTree(targetroot, new DeleteVisitor)
684684
}
685685

686-
/** Recursively collects all Java files in the working directory */
687-
private def collectAllSourceFiles(config: Config, dir: Path): List[Path] = {
688-
if (config.sourceFiles.nonEmpty) {
689-
return config
690-
.sourceFiles
691-
.map(path => AbsolutePath.of(Paths.get(path), dir))
692-
.filter(path => Files.isRegularFile(path))
693-
}
694-
val buf = ListBuffer.empty[Path]
686+
private def collectAllSourceFiles(dir: Path) = {
687+
val buf = List.newBuilder[Path]
695688
Files.walkFileTree(
696689
dir,
697690
new SimpleFileVisitor[Path] {
@@ -719,7 +712,26 @@ class ScipBuildTool(index: IndexCommand) extends BuildTool("SCIP", index) {
719712
): FileVisitResult = FileVisitResult.CONTINUE
720713
}
721714
)
722-
buf.toList
715+
buf.result()
716+
}
717+
718+
/** Recursively collects all Java files in the working directory */
719+
private def collectAllSourceFiles(config: Config, dir: Path): List[Path] = {
720+
if (config.sourceFiles.nonEmpty) {
721+
config
722+
.sourceFiles
723+
.flatMap { relativePath =>
724+
val path = AbsolutePath.of(Paths.get(relativePath), dir)
725+
726+
if (Files.isRegularFile(path) && allPatterns.matches(path))
727+
List(path)
728+
else if (Files.isDirectory(path))
729+
collectAllSourceFiles(path)
730+
else
731+
Nil
732+
}
733+
} else
734+
collectAllSourceFiles(dir)
723735
}
724736

725737
// HACK(olafurpg): I haven't figured out a reliable way to get annotation processor jars on the processorpath.

0 commit comments

Comments
 (0)