Skip to content

Commit beda410

Browse files
committed
Improve support for Bazel builds, towards #88
This commit adds a new Bazel build to this codebase so that Bazel users can build the semanticdb-javac compiler plugin from source and use it in their Bazel build. This commit includes examples of how a Bazel user could potentially enable the compiler plugin dynamically based on a command-line flag, but we don't know if this is the best way to integrate lsif-java with a Bazel codebase. This commit is just an incremental step towards Bazel support, it's not a complete solution yet because the `lsif-java index-semanticdb` command still needs additional functionality to read jar files from the `bazel-bin` directory.
1 parent 2c9341c commit beda410

File tree

21 files changed

+347
-40
lines changed

21 files changed

+347
-40
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,7 @@ dump.lsif
5656

5757
./generated
5858
/sources
59+
bazel-bin
60+
bazel-lsif-java
61+
bazel-out
62+
bazel-testlogs

WORKSPACE

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2+
3+
##########
4+
# Protobuf
5+
##########
6+
http_archive(
7+
name = "rules_proto",
8+
sha256 = "66bfdf8782796239d3875d37e7de19b1d94301e8972b3cbd2446b332429b4df1",
9+
strip_prefix = "rules_proto-4.0.0",
10+
urls = [
11+
"https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0.tar.gz",
12+
"https://github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0.tar.gz",
13+
],
14+
)
15+
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
16+
rules_proto_dependencies()
17+
rules_proto_toolchains()
18+
19+
##############
20+
# JVM External
21+
##############
22+
RULES_JVM_EXTERNAL_TAG = "4.2"
23+
RULES_JVM_EXTERNAL_SHA = "cd1a77b7b02e8e008439ca76fd34f5b07aecb8c752961f9640dea15e9e5ba1ca"
24+
http_archive(
25+
name = "rules_jvm_external",
26+
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
27+
sha256 = RULES_JVM_EXTERNAL_SHA,
28+
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
29+
)
30+
load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps")
31+
rules_jvm_external_deps()
32+
load("@rules_jvm_external//:setup.bzl", "rules_jvm_external_setup")
33+
rules_jvm_external_setup()
34+
load("@rules_jvm_external//:defs.bzl", "maven_install")
35+
maven_install(
36+
artifacts = [
37+
"com.google.protobuf:protobuf-java:3.15.6",
38+
"org.projectlombok:lombok:1.18.22",
39+
],
40+
repositories = [
41+
"https://repo1.maven.org/maven2",
42+
],
43+
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
load("@rules_proto//proto:defs.bzl", "proto_library")
2+
load("@rules_java//java:defs.bzl", "java_proto_library")
3+
4+
package(
5+
default_visibility = ["//visibility:public"],
6+
)
7+
8+
java_proto_library(
9+
name = "lsif_java_proto",
10+
deps = [":lsif_proto"],
11+
)
12+
proto_library(
13+
name = "lsif_proto",
14+
srcs = ["lsif.proto"]
15+
)

maven_install.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"dependency_tree": {
3+
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL",
4+
"__INPUT_ARTIFACTS_HASH": -1430904333,
5+
"__RESOLVED_ARTIFACTS_HASH": 2127018902,
6+
"conflict_resolution": {},
7+
"dependencies": [
8+
{
9+
"coord": "com.google.protobuf:protobuf-java:3.15.6",
10+
"dependencies": [],
11+
"directDependencies": [],
12+
"file": "v1/https/repo1.maven.org/maven2/com/google/protobuf/protobuf-java/3.15.6/protobuf-java-3.15.6.jar",
13+
"mirror_urls": [
14+
"https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/3.15.6/protobuf-java-3.15.6.jar"
15+
],
16+
"sha256": "97a2b7dbcd9a81a9760c1531ee0b7253a4633e9f9fc5accfb66c4205d23c30c6",
17+
"url": "https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/3.15.6/protobuf-java-3.15.6.jar"
18+
},
19+
{
20+
"coord": "org.projectlombok:lombok:1.18.22",
21+
"dependencies": [],
22+
"directDependencies": [],
23+
"file": "v1/https/repo1.maven.org/maven2/org/projectlombok/lombok/1.18.22/lombok-1.18.22.jar",
24+
"mirror_urls": [
25+
"https://repo1.maven.org/maven2/org/projectlombok/lombok/1.18.22/lombok-1.18.22.jar"
26+
],
27+
"sha256": "ecef1581411d7a82cc04281667ee0bac5d7c0a5aae74cfc38430396c91c31831",
28+
"url": "https://repo1.maven.org/maven2/org/projectlombok/lombok/1.18.22/lombok-1.18.22.jar"
29+
}
30+
],
31+
"version": "0.1.0"
32+
}
33+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
load("@rules_java//java:defs.bzl", "java_library")
2+
package(
3+
default_visibility = ["//visibility:public"],
4+
)
5+
6+
java_library(
7+
name = "semanticdb_java",
8+
srcs = glob(["*.java"]),
9+
deps = [
10+
"//semanticdb-java/src/main/protobuf:semanticdb_java_proto",
11+
],
12+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load("@rules_java//java:defs.bzl", "java_proto_library")
2+
load("@rules_proto//proto:defs.bzl", "proto_library")
3+
4+
package(
5+
default_visibility = ["//visibility:public"],
6+
)
7+
8+
java_proto_library(
9+
name = "semanticdb_java_proto",
10+
deps = [":semanticdb_proto"],
11+
)
12+
13+
proto_library(
14+
name = "semanticdb_proto",
15+
srcs = ["semanticdb.proto"]
16+
)

semanticdb-javac/BUILD

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
2+
load("@rules_java//java:defs.bzl", "java_import", "java_library", "java_plugin")
3+
4+
package(
5+
default_visibility = ["//visibility:public"],
6+
)
7+
8+
config_setting(
9+
name = "is_enabled",
10+
flag_values = {":enabled": "true"},
11+
)
12+
13+
string_flag(
14+
name = "enabled",
15+
values = ["true", "false"],
16+
build_setting_default = "false",
17+
)
18+
19+
java_import(
20+
name = "javac-import",
21+
jars = ["@bazel_tools//third_party/java/jdk/langtools:javac_jar"],
22+
)
23+
24+
java_plugin(
25+
name = "plugin",
26+
deps = [
27+
":semanticdb-javac",
28+
],
29+
)
30+
31+
32+
java_library(
33+
name = "semanticdb-javac",
34+
srcs = glob(["src/main/java/**/*.java"]),
35+
resources = ["src/main/resources/META-INF/services/com.sun.source.util.Plugin"],
36+
deps = [
37+
"//semanticdb-java/src/main/protobuf:semanticdb_java_proto",
38+
"//semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac:semanticdb_java",
39+
":javac-import",
40+
],
41+
)

semanticdb-javac/defs.bzl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""Java rules that automatically register the SemanticDB compiler plugin based on the presence of a string flag."""
2+
load("@rules_java//java:defs.bzl", native_java_library="java_library", native_java_binary="java_binary")
3+
4+
def java_library(javacopts=[], plugins=[],**kwargs):
5+
native_java_library(
6+
javacopts=_actual_javacopts(javacopts),
7+
plugins=_actual_plugins(plugins),
8+
**kwargs)
9+
10+
11+
def java_binary(javacopts=[], plugins=[],**kwargs):
12+
native_java_binary(
13+
javacopts=_actual_javacopts(javacopts),
14+
plugins=_actual_plugins(plugins),
15+
**kwargs)
16+
17+
def _actual_javacopts(javacopts):
18+
return select({
19+
"//semanticdb-javac:is_enabled": ["'-Xplugin:semanticdb -build-tool:bazel'"] + javacopts,
20+
"//conditions:default": javacopts,
21+
})
22+
23+
def _actual_plugins(plugins):
24+
return select({
25+
"//semanticdb-javac:is_enabled": ["//semanticdb-javac:plugin"] + plugins,
26+
"//conditions:default": plugins,
27+
})

semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbJavacOptions.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class SemanticdbJavacOptions {
2323
public boolean includeText = false;
2424
public boolean verboseEnabled = false;
2525
public final ArrayList<String> errors;
26+
public boolean isErrorReported = false;
2627
public UriScheme uriScheme = UriScheme.DEFAULT;
2728

2829
public static String stubClassName = "META-INF-semanticdb-stub";
@@ -56,6 +57,10 @@ public static SemanticdbJavacOptions parse(String[] args, Context ctx) {
5657
result.sourceroot = Paths.get(arg.substring("-sourceroot:".length())).normalize();
5758
} else if (arg.equals("-build-tool:sbt")) {
5859
result.uriScheme = UriScheme.SBT;
60+
} else if (arg.equals("-build-tool:bazel")) {
61+
result.uriScheme = UriScheme.BAZEL;
62+
useJavacClassesDir = true;
63+
result.targetroot = getJavacClassesDir(result, ctx);
5964
} else if (arg.equals("-text:on")) {
6065
result.includeText = true;
6166
} else if (arg.equals("-text:off")) {
@@ -73,7 +78,11 @@ public static SemanticdbJavacOptions parse(String[] args, Context ctx) {
7378
if (result.targetroot == null && !useJavacClassesDir) {
7479
result.errors.add(missingRequiredDirectoryOption("targetroot"));
7580
}
76-
if (result.sourceroot == null) {
81+
if (result.uriScheme != UriScheme.BAZEL && result.sourceroot == null) {
82+
// When using -build-tool:bazel, the sourceroot is automatically inferred from the first
83+
// compilation unit.
84+
// See `SemanticdbTaskListener.inferBazelSourceroot()` for the method that infers the
85+
// sourceroot.
7786
result.errors.add(missingRequiredDirectoryOption("sourceroot"));
7887
}
7988
return result;
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.sourcegraph.semanticdb_javac;
22

3-
import com.sun.source.util.JavacTask;
4-
import com.sun.source.util.Plugin;
3+
import com.sun.source.util.*;
54
import com.sun.tools.javac.api.BasicJavacTask;
65
import com.sun.tools.javac.model.JavacTypes;
76
import com.sun.tools.javac.util.Context;
7+
import com.sun.tools.javac.util.Options;
88

99
/** Entrypoint of the semanticdb-javac compiler plugin. */
1010
public class SemanticdbPlugin implements Plugin {
@@ -18,17 +18,10 @@ public String getName() {
1818
public void init(JavacTask task, String... args) {
1919
Context ctx = ((BasicJavacTask) task).getContext();
2020

21-
SemanticdbReporter reporter = new SemanticdbReporter();
21+
SemanticdbReporter reporter = new SemanticdbReporter(Trees.instance(task));
2222
SemanticdbJavacOptions options = SemanticdbJavacOptions.parse(args, ctx);
2323
GlobalSymbolsCache globals = new GlobalSymbolsCache(options);
2424
JavacTypes javacTypes = JavacTypes.instance(ctx);
25-
if (!options.errors.isEmpty()) {
26-
for (String error : options.errors) {
27-
reporter.error(error);
28-
}
29-
} else {
30-
task.addTaskListener(
31-
new SemanticdbTaskListener(options, task, globals, reporter, javacTypes));
32-
}
25+
task.addTaskListener(new SemanticdbTaskListener(options, task, globals, reporter, javacTypes));
3326
}
3427
}

0 commit comments

Comments
 (0)