Skip to content

Commit 1f30836

Browse files
committed
Complete Bazel support.
- Add LSIF generation support. - Add e2e example on how to import lsif-java as an external repo. - Add cross-repo support for bazelbuild/rules_jvm_external and twitter/bazel-multiversion.
1 parent beda410 commit 1f30836

File tree

26 files changed

+1257
-43
lines changed

26 files changed

+1257
-43
lines changed

.bazelignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
examples/
2+

.bazelversion

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5.1.1

.github/workflows/ci.yml

+14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ jobs:
2121
with:
2222
go-version: "^1.13.1"
2323
- run: sbt test
24+
bazel:
25+
runs-on: ubuntu-latest
26+
steps:
27+
- uses: actions/checkout@v2
28+
- run: yarn global add @bazel/bazelisk
29+
- run: bazel build //... --//semanticdb-javac:enabled=true
30+
- run: bazel run lsif-semanticdb:bazel -- --sourceroot $PWD
31+
- run: du -h dump.lsif-typed
32+
- run: bazel build //... --@lsif_java//semanticdb-javac:enabled=true
33+
working-directory: examples/bazel-example
34+
- run: bazel run @lsif_java//lsif-semanticdb:bazel -- --sourceroot $PWD
35+
working-directory: examples/bazel-example
36+
- run: du -h dump.lsif-typed
37+
working-directory: examples/bazel-example
2438
check:
2539
runs-on: ubuntu-latest
2640
steps:

.tool-versions

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
golang 1.17.5

WORKSPACE

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
workspace(name = "lsif_java_tests")
12
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
23

4+
##############
5+
# Bazel stdlib
6+
##############
7+
http_archive(
8+
name = "bazel_skylib",
9+
sha256 = "1c531376ac7e5a180e0237938a2536de0c54d93f5c278634818e0efc952dd56c",
10+
urls = [
11+
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz",
12+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz",
13+
],
14+
)
15+
316
##########
417
# Protobuf
518
##########
@@ -35,6 +48,7 @@ load("@rules_jvm_external//:defs.bzl", "maven_install")
3548
maven_install(
3649
artifacts = [
3750
"com.google.protobuf:protobuf-java:3.15.6",
51+
"com.google.protobuf:protobuf-java-util:3.15.6",
3852
"org.projectlombok:lombok:1.18.22",
3953
],
4054
repositories = [

examples/bazel-example/WORKSPACE

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
# This is an end-to-end example of how to consume lsif-java from an external repository.
3+
workspace(name = "lsif_java_example")
4+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
5+
6+
##############
7+
# Bazel stdlib
8+
##############
9+
http_archive(
10+
name = "bazel_skylib",
11+
sha256 = "1c531376ac7e5a180e0237938a2536de0c54d93f5c278634818e0efc952dd56c",
12+
urls = [
13+
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz",
14+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz",
15+
],
16+
)
17+
18+
##############
19+
# lsif-java
20+
##############
21+
local_repository(
22+
name = "lsif_java",
23+
path = "../.."
24+
)
25+
# TODO: add commented out `http_archive` example once this workspace file is available on GitHub.
26+
27+
##########
28+
# Protobuf
29+
##########
30+
http_archive(
31+
name = "rules_proto",
32+
sha256 = "66bfdf8782796239d3875d37e7de19b1d94301e8972b3cbd2446b332429b4df1",
33+
strip_prefix = "rules_proto-4.0.0",
34+
urls = [
35+
"https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0.tar.gz",
36+
"https://github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0.tar.gz",
37+
],
38+
)
39+
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
40+
rules_proto_dependencies()
41+
rules_proto_toolchains()
42+
43+
##############
44+
# JVM External
45+
##############
46+
RULES_JVM_EXTERNAL_TAG = "4.2"
47+
RULES_JVM_EXTERNAL_SHA = "cd1a77b7b02e8e008439ca76fd34f5b07aecb8c752961f9640dea15e9e5ba1ca"
48+
http_archive(
49+
name = "rules_jvm_external",
50+
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
51+
sha256 = RULES_JVM_EXTERNAL_SHA,
52+
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
53+
)
54+
load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps")
55+
rules_jvm_external_deps()
56+
load("@rules_jvm_external//:setup.bzl", "rules_jvm_external_setup")
57+
rules_jvm_external_setup()
58+
load("@rules_jvm_external//:defs.bzl", "maven_install")
59+
maven_install(
60+
artifacts = [
61+
"com.google.protobuf:protobuf-java:3.15.6", # Required dependency by lsif-java.
62+
"com.google.protobuf:protobuf-java-util:3.15.6", # Required dependency by lsif-java.
63+
"com.google.guava:guava:31.0-jre", # Not required dependency, only used by tests.
64+
],
65+
repositories = [
66+
"https://repo1.maven.org/maven2",
67+
],
68+
)
69+
load("@maven//:defs.bzl", "pinned_maven_install")
70+
pinned_maven_install()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/private/var/tmp/_bazel_olafurpg/a640400552e2e069e7e8c1f34cb00c9d/execroot/lsif_java_example
+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
{
2+
"dependency_tree": {
3+
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL",
4+
"__INPUT_ARTIFACTS_HASH": -2050933211,
5+
"__RESOLVED_ARTIFACTS_HASH": -730128961,
6+
"conflict_resolution": {},
7+
"dependencies": [
8+
{
9+
"coord": "com.google.code.findbugs:jsr305:3.0.2",
10+
"dependencies": [],
11+
"directDependencies": [],
12+
"file": "v1/https/repo1.maven.org/maven2/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.jar",
13+
"mirror_urls": [
14+
"https://repo1.maven.org/maven2/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.jar"
15+
],
16+
"sha256": "766ad2a0783f2687962c8ad74ceecc38a28b9f72a2d085ee438b7813e928d0c7",
17+
"url": "https://repo1.maven.org/maven2/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.jar"
18+
},
19+
{
20+
"coord": "com.google.code.gson:gson:2.8.6",
21+
"dependencies": [],
22+
"directDependencies": [],
23+
"file": "v1/https/repo1.maven.org/maven2/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar",
24+
"mirror_urls": [
25+
"https://repo1.maven.org/maven2/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar"
26+
],
27+
"sha256": "c8fb4839054d280b3033f800d1f5a97de2f028eb8ba2eb458ad287e536f3f25f",
28+
"url": "https://repo1.maven.org/maven2/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar"
29+
},
30+
{
31+
"coord": "com.google.errorprone:error_prone_annotations:2.7.1",
32+
"dependencies": [],
33+
"directDependencies": [],
34+
"file": "v1/https/repo1.maven.org/maven2/com/google/errorprone/error_prone_annotations/2.7.1/error_prone_annotations-2.7.1.jar",
35+
"mirror_urls": [
36+
"https://repo1.maven.org/maven2/com/google/errorprone/error_prone_annotations/2.7.1/error_prone_annotations-2.7.1.jar"
37+
],
38+
"sha256": "cd5257c08a246cf8628817ae71cb822be192ef91f6881ca4a3fcff4f1de1cff3",
39+
"url": "https://repo1.maven.org/maven2/com/google/errorprone/error_prone_annotations/2.7.1/error_prone_annotations-2.7.1.jar"
40+
},
41+
{
42+
"coord": "com.google.guava:failureaccess:1.0.1",
43+
"dependencies": [],
44+
"directDependencies": [],
45+
"file": "v1/https/repo1.maven.org/maven2/com/google/guava/failureaccess/1.0.1/failureaccess-1.0.1.jar",
46+
"mirror_urls": [
47+
"https://repo1.maven.org/maven2/com/google/guava/failureaccess/1.0.1/failureaccess-1.0.1.jar"
48+
],
49+
"sha256": "a171ee4c734dd2da837e4b16be9df4661afab72a41adaf31eb84dfdaf936ca26",
50+
"url": "https://repo1.maven.org/maven2/com/google/guava/failureaccess/1.0.1/failureaccess-1.0.1.jar"
51+
},
52+
{
53+
"coord": "com.google.guava:guava:31.0-jre",
54+
"dependencies": [
55+
"com.google.code.findbugs:jsr305:3.0.2",
56+
"com.google.errorprone:error_prone_annotations:2.7.1",
57+
"com.google.guava:failureaccess:1.0.1",
58+
"com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava",
59+
"com.google.j2objc:j2objc-annotations:1.3",
60+
"org.checkerframework:checker-qual:3.12.0"
61+
],
62+
"directDependencies": [
63+
"com.google.code.findbugs:jsr305:3.0.2",
64+
"com.google.errorprone:error_prone_annotations:2.7.1",
65+
"com.google.guava:failureaccess:1.0.1",
66+
"com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava",
67+
"com.google.j2objc:j2objc-annotations:1.3",
68+
"org.checkerframework:checker-qual:3.12.0"
69+
],
70+
"file": "v1/https/repo1.maven.org/maven2/com/google/guava/guava/31.0-jre/guava-31.0-jre.jar",
71+
"mirror_urls": [
72+
"https://repo1.maven.org/maven2/com/google/guava/guava/31.0-jre/guava-31.0-jre.jar"
73+
],
74+
"sha256": "040d17b7a434c3e7908d2b51b3e18b30535029cc3edf1f9e3945faed78ad3eab",
75+
"url": "https://repo1.maven.org/maven2/com/google/guava/guava/31.0-jre/guava-31.0-jre.jar"
76+
},
77+
{
78+
"coord": "com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava",
79+
"dependencies": [],
80+
"directDependencies": [],
81+
"file": "v1/https/repo1.maven.org/maven2/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar",
82+
"mirror_urls": [
83+
"https://repo1.maven.org/maven2/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar"
84+
],
85+
"sha256": "b372a037d4230aa57fbeffdef30fd6123f9c0c2db85d0aced00c91b974f33f99",
86+
"url": "https://repo1.maven.org/maven2/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar"
87+
},
88+
{
89+
"coord": "com.google.j2objc:j2objc-annotations:1.3",
90+
"dependencies": [],
91+
"directDependencies": [],
92+
"file": "v1/https/repo1.maven.org/maven2/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3.jar",
93+
"mirror_urls": [
94+
"https://repo1.maven.org/maven2/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3.jar"
95+
],
96+
"sha256": "21af30c92267bd6122c0e0b4d20cccb6641a37eaf956c6540ec471d584e64a7b",
97+
"url": "https://repo1.maven.org/maven2/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3.jar"
98+
},
99+
{
100+
"coord": "com.google.protobuf:protobuf-java-util:3.15.6",
101+
"dependencies": [
102+
"com.google.code.gson:gson:2.8.6",
103+
"com.google.errorprone:error_prone_annotations:2.7.1",
104+
"com.google.guava:guava:31.0-jre",
105+
"com.google.protobuf:protobuf-java:3.15.6"
106+
],
107+
"directDependencies": [
108+
"com.google.code.gson:gson:2.8.6",
109+
"com.google.errorprone:error_prone_annotations:2.7.1",
110+
"com.google.guava:guava:31.0-jre",
111+
"com.google.protobuf:protobuf-java:3.15.6"
112+
],
113+
"file": "v1/https/repo1.maven.org/maven2/com/google/protobuf/protobuf-java-util/3.15.6/protobuf-java-util-3.15.6.jar",
114+
"mirror_urls": [
115+
"https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java-util/3.15.6/protobuf-java-util-3.15.6.jar"
116+
],
117+
"sha256": "ebeeddb7e9fba9a52207237070354a0a2d2ba7febd9885b94b599879ba4b5ab0",
118+
"url": "https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java-util/3.15.6/protobuf-java-util-3.15.6.jar"
119+
},
120+
{
121+
"coord": "com.google.protobuf:protobuf-java:3.15.6",
122+
"dependencies": [],
123+
"directDependencies": [],
124+
"file": "v1/https/repo1.maven.org/maven2/com/google/protobuf/protobuf-java/3.15.6/protobuf-java-3.15.6.jar",
125+
"mirror_urls": [
126+
"https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/3.15.6/protobuf-java-3.15.6.jar"
127+
],
128+
"sha256": "97a2b7dbcd9a81a9760c1531ee0b7253a4633e9f9fc5accfb66c4205d23c30c6",
129+
"url": "https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/3.15.6/protobuf-java-3.15.6.jar"
130+
},
131+
{
132+
"coord": "org.checkerframework:checker-qual:3.12.0",
133+
"dependencies": [],
134+
"directDependencies": [],
135+
"file": "v1/https/repo1.maven.org/maven2/org/checkerframework/checker-qual/3.12.0/checker-qual-3.12.0.jar",
136+
"mirror_urls": [
137+
"https://repo1.maven.org/maven2/org/checkerframework/checker-qual/3.12.0/checker-qual-3.12.0.jar"
138+
],
139+
"sha256": "ff10785ac2a357ec5de9c293cb982a2cbb605c0309ea4cc1cb9b9bc6dbe7f3cb",
140+
"url": "https://repo1.maven.org/maven2/org/checkerframework/checker-qual/3.12.0/checker-qual-3.12.0.jar"
141+
}
142+
],
143+
"version": "0.1.0"
144+
}
145+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# We import the custom `java_library` implementation that automatically adds the
2+
# SemanticDB compiler plugin based on the presence of the flag
3+
# `--@lsif_java//semanticdb-javac:enabled=true`. By default, this java_library
4+
# rule works just like the official java_library rule. Feel free to copy-paste
5+
# the `semanticdb:defs.bzl` file and adapt to your needs. This example is only
6+
# for demonstration purposes.
7+
load("@lsif_java//semanticdb-javac:defs.bzl", "java_library")
8+
9+
package(
10+
default_visibility = ["//visibility:public"],
11+
)
12+
13+
java_library(
14+
name = "example",
15+
srcs = ["Example.java"],
16+
deps = [
17+
"@maven//:com_google_guava_guava",
18+
],
19+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package example;
2+
import com.google.common.util.concurrent.Futures;
3+
4+
public class Example {
5+
public static void hello() {
6+
System.out.println(Futures.immediateCancelledFuture());
7+
}
8+
9+
}

lsif-java/src/main/scala/com/sourcegraph/lsif_java/commands/IndexSemanticdbCommand.scala

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ final case class IndexSemanticdbCommand(
3333
@Description(
3434
"Whether to process the SemanticDB files in parallel"
3535
) parallel: Boolean = true,
36+
@Description(
37+
"Whether to infer the location of SemanticDB files based as produced by Bazel"
38+
) bazel: Boolean = true,
3639
@Description("URL to a PackageHub instance")
3740
@Hidden
3841
packagehub: Option[String] = None,

lsif-semanticdb/BUILD

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
load("@rules_java//java:defs.bzl", "java_binary", "java_library", "java_proto_library")
2+
load("@rules_proto//proto:defs.bzl", "proto_library")
3+
4+
package(
5+
default_visibility = ["//visibility:public"],
6+
)
7+
8+
java_binary(
9+
name = "bazel",
10+
main_class = "com.sourcegraph.lsif_semanticdb.BazelBuildTool",
11+
runtime_deps = [
12+
":lsif-semanticdb",
13+
],
14+
)
15+
16+
java_library(
17+
name = "lsif-semanticdb",
18+
srcs = glob(["src/main/java/**/*.java"]),
19+
deps = [
20+
":all_java_proto",
21+
"//semanticdb-java",
22+
"//semanticdb-java/src/main/protobuf:semanticdb_java_proto",
23+
"@maven//:com_google_protobuf_protobuf_java",
24+
"@maven//:com_google_protobuf_protobuf_java_util",
25+
],
26+
)
27+
28+
java_proto_library(
29+
name = "all_java_proto",
30+
deps = [":all_proto"],
31+
)
32+
33+
proto_library(
34+
name = "all_proto",
35+
srcs = glob(["src/main/protobuf/*.proto"]),
36+
)

0 commit comments

Comments
 (0)