Skip to content

Commit df7ce76

Browse files
mjduijnMaarten Duijn
authored and
Maarten Duijn
committed
Create Bazel rules for generating reactive bindings
Resolves #164
1 parent 4830296 commit df7ce76

File tree

24 files changed

+602
-1
lines changed

24 files changed

+602
-1
lines changed

.bazelignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
demos/bazel

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ dependency-reduced-pom.xml
99
.settings/
1010
.checkstyle
1111
.classpath
12-
.project
12+
.project
13+
bazel-*
14+
.ijwb/

BUILD.bazel

Whitespace-only changes.

WORKSPACE

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
workspace(name = "com_salesforce_servicelibs_reactive_grpc")
2+
3+
load("//bazel:repositories.bzl", "repositories")
4+
5+
repositories()
6+
7+
load("@io_grpc_grpc_java//:repositories.bzl", "grpc_java_repositories")
8+
9+
grpc_java_repositories()

bazel/BUILD.bazel

Whitespace-only changes.

bazel/java_reactive_grpc_library.bzl

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
load("@bazel_tools//tools/jdk:toolchain_utils.bzl", "find_java_runtime_toolchain", "find_java_toolchain")
2+
3+
# Taken from bazelbuild/rules_go license: Apache 2
4+
# https://github.com/bazelbuild/rules_go/blob/528f6faf83f85c23da367d61f784893d1b3bd72b/proto/compiler.bzl#L94
5+
# replaced `prefix = paths.join(..` with `prefix = "/".join(..`
6+
def _proto_path(src, proto):
7+
"""proto_path returns the string used to import the proto. This is the proto
8+
source path within its repository, adjusted by import_prefix and
9+
strip_import_prefix.
10+
11+
Args:
12+
src: the proto source File.
13+
proto: the ProtoInfo provider.
14+
15+
Returns:
16+
An import path string.
17+
"""
18+
if not hasattr(proto, "proto_source_root"):
19+
# Legacy path. Remove when Bazel minimum version >= 0.21.0.
20+
path = src.path
21+
root = src.root.path
22+
ws = src.owner.workspace_root
23+
if path.startswith(root):
24+
path = path[len(root):]
25+
if path.startswith("/"):
26+
path = path[1:]
27+
if path.startswith(ws):
28+
path = path[len(ws):]
29+
if path.startswith("/"):
30+
path = path[1:]
31+
return path
32+
33+
if proto.proto_source_root == ".":
34+
# true if proto sources were generated
35+
prefix = src.root.path + "/"
36+
elif proto.proto_source_root.startswith(src.root.path):
37+
# sometimes true when import paths are adjusted with import_prefix
38+
prefix = proto.proto_source_root + "/"
39+
else:
40+
# usually true when paths are not adjusted
41+
prefix = "/".join([src.root.path, proto.proto_source_root]) + "/"
42+
if not src.path.startswith(prefix):
43+
# sometimes true when importing multiple adjusted protos
44+
return src.path
45+
return src.path[len(prefix):]
46+
47+
def _reactive_grpc_library_impl(ctx):
48+
proto = ctx.attr.proto[ProtoInfo]
49+
descriptor_set_in = proto.transitive_descriptor_sets
50+
51+
gensrcjar = ctx.actions.declare_file("%s-proto-gensrc.jar" % ctx.label.name)
52+
53+
args = ctx.actions.args()
54+
args.add(ctx.executable.reactive_plugin.path, format = "--plugin=protoc-gen-reactive-grpc-plugin=%s")
55+
args.add("--reactive-grpc-plugin_out=:{0}".format(gensrcjar.path))
56+
args.add_joined("--descriptor_set_in", descriptor_set_in, join_with = ":")
57+
for src in proto.check_deps_sources.to_list():
58+
args.add(_proto_path(src, proto))
59+
60+
ctx.actions.run(
61+
inputs = descriptor_set_in,
62+
tools = [ctx.executable.reactive_plugin],
63+
outputs = [gensrcjar],
64+
executable = ctx.executable._protoc,
65+
arguments = [args],
66+
)
67+
68+
deps = [java_common.make_non_strict(dep[JavaInfo]) for dep in ctx.attr.deps]
69+
deps += [dep[JavaInfo] for dep in ctx.attr.reactive_deps]
70+
71+
java_info = java_common.compile(
72+
ctx,
73+
deps = deps,
74+
host_javabase = find_java_runtime_toolchain(ctx, ctx.attr._host_javabase),
75+
java_toolchain = find_java_toolchain(ctx, ctx.attr._java_toolchain),
76+
output = ctx.outputs.jar,
77+
output_source_jar = ctx.outputs.srcjar,
78+
source_jars = [gensrcjar],
79+
)
80+
81+
return [java_info]
82+
83+
_reactive_grpc_library = rule(
84+
attrs = {
85+
"proto": attr.label(
86+
mandatory = True,
87+
providers = [ProtoInfo],
88+
),
89+
"deps": attr.label_list(
90+
mandatory = True,
91+
allow_empty = False,
92+
providers = [JavaInfo],
93+
),
94+
"_protoc": attr.label(
95+
default = Label("@com_google_protobuf//:protoc"),
96+
executable = True,
97+
cfg = "host",
98+
),
99+
"reactive_deps": attr.label_list(
100+
mandatory = True,
101+
allow_empty = False,
102+
providers = [JavaInfo],
103+
),
104+
"reactive_plugin": attr.label(
105+
mandatory = True,
106+
executable = True,
107+
cfg = "host",
108+
),
109+
"_java_toolchain": attr.label(
110+
default = Label("@bazel_tools//tools/jdk:current_java_toolchain"),
111+
),
112+
"_host_javabase": attr.label(
113+
cfg = "host",
114+
default = Label("@bazel_tools//tools/jdk:current_host_java_runtime"),
115+
),
116+
},
117+
fragments = ["java"],
118+
outputs = {
119+
"jar": "lib%{name}.jar",
120+
"srcjar": "lib%{name}-src.jar",
121+
},
122+
provides = [JavaInfo],
123+
implementation = _reactive_grpc_library_impl,
124+
)
125+
126+
def reactor_grpc_library(**kwargs):
127+
_reactive_grpc_library(
128+
reactive_plugin = "@com_salesforce_servicelibs_reactive_grpc//reactor/reactor-grpc:reactor_grpc_bin",
129+
reactive_deps = [
130+
"@com_salesforce_servicelibs_reactive_grpc//reactor/reactor-grpc-stub",
131+
"@io_projectreactor_reactor_core",
132+
],
133+
**kwargs
134+
)
135+
136+
def rx_grpc_library(**kwargs):
137+
_reactive_grpc_library(
138+
reactive_plugin = "@com_salesforce_servicelibs_reactive_grpc//rx-java/rxgrpc:rxgrpc_bin",
139+
reactive_deps = [
140+
"@com_salesforce_servicelibs_reactive_grpc//rx-java/rxgrpc-stub",
141+
"@com_salesforce_servicelibs_reactive_grpc//common/reactive-grpc-common",
142+
"@io_reactivex_rxjava2_rxjava",
143+
],
144+
**kwargs
145+
)

bazel/repositories.bzl

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2+
load("@bazel_tools//tools/build_defs/repo:java.bzl", "java_import_external")
3+
4+
def repositories(
5+
omit_org_reactivestreams_reactive_streams = False,
6+
omit_io_projectreactor_reactor_core = False,
7+
omit_io_reactivex_rxjava2_rxjava = False,
8+
omit_io_grpc_grpc_java = False,
9+
omit_com_salesforce_servicelibs_jprotoc = False,
10+
omit_com_github_spullara_mustache_java_compiler = False):
11+
if not omit_org_reactivestreams_reactive_streams:
12+
java_import_external(
13+
name = "org_reactivestreams_reactive_streams",
14+
jar_urls = ["http://central.maven.org/maven2/org/reactivestreams/reactive-streams/1.0.2/reactive-streams-1.0.2.jar"],
15+
jar_sha256 = "cc09ab0b140e0d0496c2165d4b32ce24f4d6446c0a26c5dc77b06bdf99ee8fae",
16+
srcjar_urls = ["http://central.maven.org/maven2/org/reactivestreams/reactive-streams/1.0.2/reactive-streams-1.0.2-sources.jar"],
17+
srcjar_sha256 = "963a6480f46a64013d0f144ba41c6c6e63c4d34b655761717a436492886f3667",
18+
licenses = ["notice"], # Apache 2.0
19+
)
20+
21+
if not omit_io_projectreactor_reactor_core:
22+
java_import_external(
23+
name = "io_projectreactor_reactor_core",
24+
jar_urls = ["http://central.maven.org/maven2/io/projectreactor/reactor-core/3.2.6.RELEASE/reactor-core-3.2.6.RELEASE.jar"],
25+
jar_sha256 = "8962081aa9e0fbe1685cc2746471b232f93f58e269cc89b54efebcb99c65af1a",
26+
srcjar_urls = ["http://central.maven.org/maven2/io/projectreactor/reactor-core/3.2.6.RELEASE/reactor-core-3.2.6.RELEASE-sources.jar"],
27+
srcjar_sha256 = "b871669ed12aee2af22f20a611bf871c7f848caede85bc19b0ef08ef5f79bc46",
28+
licenses = ["notice"], # Apache 2.0
29+
)
30+
31+
if not omit_io_reactivex_rxjava2_rxjava:
32+
java_import_external(
33+
name = "io_reactivex_rxjava2_rxjava",
34+
jar_urls = ["http://central.maven.org/maven2/io/reactivex/rxjava2/rxjava/2.2.7/rxjava-2.2.7.jar"],
35+
jar_sha256 = "23798f1b5fecac2aaaa3e224fd0e73f41dc081802c7bd2a6e91030bad36b9013",
36+
srcjar_urls = ["http://central.maven.org/maven2/io/reactivex/rxjava2/rxjava/2.2.7/rxjava-2.2.7-sources.jar"],
37+
srcjar_sha256 = "b7ee7e2b2ce07eda19755e511757427701f4081a051cace1efd69cf0bfcc8ff2",
38+
licenses = ["notice"], # Apache 2.0
39+
)
40+
41+
if not omit_io_grpc_grpc_java:
42+
io_grpc_grpc_java_version = "v1.21.0"
43+
44+
http_archive(
45+
name = "io_grpc_grpc_java",
46+
sha256 = "2137a2b568e8266d6c269c995c7ba68db3d8d7d7936087c540fdbfadae577f81",
47+
strip_prefix = "grpc-java-%s" % io_grpc_grpc_java_version[1:],
48+
urls = ["https://github.com/grpc/grpc-java/archive/%s.zip" % io_grpc_grpc_java_version],
49+
)
50+
51+
if not omit_com_salesforce_servicelibs_jprotoc:
52+
java_import_external(
53+
name = "com_salesforce_servicelibs_jprotoc",
54+
jar_urls = ["http://central.maven.org/maven2/com/salesforce/servicelibs/jprotoc/0.9.1/jprotoc-0.9.1.jar"],
55+
jar_sha256 = "55d78aafa930693856055e7d1d63414670beb59a9b253ece5cf546541b4bbd07",
56+
srcjar_urls = ["http://central.maven.org/maven2/com/salesforce/servicelibs/jprotoc/0.9.1/jprotoc-0.9.1-sources.jar"],
57+
srcjar_sha256 = "ba023a2097874fa7131c277eab69ca748928627bea122a48ef9cb54ca8dafd91",
58+
licenses = ["notice"], # BSD 3-Clause
59+
)
60+
61+
if not omit_com_github_spullara_mustache_java_compiler:
62+
java_import_external(
63+
name = "com_github_spullara_mustache_java_compiler",
64+
jar_urls = ["http://central.maven.org/maven2/com/github/spullara/mustache/java/compiler/0.9.6/compiler-0.9.6.jar"],
65+
jar_sha256 = "c4d697fd3619cb616cc5e22e9530c8a4fd4a8e9a76953c0655ee627cb2d22318",
66+
srcjar_urls = ["http://central.maven.org/maven2/com/github/spullara/mustache/java/compiler/0.9.6/compiler-0.9.6-sources.jar"],
67+
srcjar_sha256 = "fb3cf89e4daa0aaa4e659aca12a8ddb0d7b605271285f3e108201e0a389b4c7a",
68+
licenses = ["notice"], # Apache 2.0
69+
)
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
java_library(
2+
name = "reactive-grpc-common",
3+
srcs = glob(["src/main/**/*.java"]),
4+
visibility = ["//visibility:public"],
5+
deps = [
6+
"@com_google_guava_guava",
7+
"@io_grpc_grpc_java//core",
8+
"@io_grpc_grpc_java//stub",
9+
"@org_reactivestreams_reactive_streams",
10+
],
11+
)
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
java_library(
2+
name = "reactive-grpc-gencommon",
3+
srcs = glob(["src/main/**/*.java"]),
4+
visibility = ["//visibility:public"],
5+
deps = [
6+
"@com_salesforce_servicelibs_jprotoc",
7+
"@com_github_spullara_mustache_java_compiler",
8+
"@io_grpc_grpc_java//protobuf",
9+
"@com_google_guava_guava",
10+
"@com_google_protobuf//:protobuf_java",
11+
],
12+
)

demos/bazel/.bazelrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Required because by default Bazel strips source code info from proto file descriptors
2+
# (see https://github.com/bazelbuild/bazel/issues/3971)
3+
# and com.salesforce.reactivegrpc.gen.ReactiveGrpcGenerator does not generate any
4+
# services without source code info present
5+
build --protocopt=--include_source_info

demos/bazel/BUILD.bazel

Whitespace-only changes.

demos/bazel/README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Bazel Reactive gRPC
2+
3+
This demo shows how to use the Reactive-gRPC Bazel rules.
4+
5+
## Setup
6+
7+
Add `build --protocopt=--include_source_info` to your `.bazelrc` file.
8+
This needs to be done because by default Bazel strips source code info from proto file descriptors.
9+
This information is required by the Reactive-gRPC generator, **it does not generate services without it**.
10+
11+
Include this project as an external dependency in your `WORKSPACE`.
12+
13+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
14+
15+
http_archive(
16+
name = "com_salesforce_servicelibs_reactive_grpc",
17+
sha256 = <sha256>,
18+
strip_prefix = "reactive-grpc-%s" % <version>,
19+
url = "https://github.com/salesforce/reactive-grpc/archive/%s.zip" % <version>,
20+
)
21+
22+
load("@com_salesforce_servicelibs_reactive_grpc//bazel:repositories.bzl", reactive_grpc_repositories="repositories")
23+
reactive_grpc_repositories()
24+
25+
load("@io_grpc_grpc_java//:repositories.bzl", "grpc_java_repositories")
26+
grpc_java_repositories()
27+
28+
29+
In your build files use the following to generate reactive bindings.
30+
31+
32+
load("@com_salesforce_servicelibs_reactive_grpc//bazel:java_reactive_grpc_library.bzl", "reactor_grpc_library", "rx_grpc_library")
33+
34+
reactor_grpc_library(
35+
name = "helloworld_reactor_grpc",
36+
proto = ":helloworld_proto",
37+
visibility = ["//visibility:public"],
38+
deps = [":helloworld_java_grpc"],
39+
)
40+
41+
rx_grpc_library(
42+
name = "helloworld_rx_grpc",
43+
proto = ":helloworld_proto",
44+
visibility = ["//visibility:public"],
45+
deps = [":helloworld_java_grpc"],
46+
)
47+
48+
These targets can be used like any other `java_library` targets.
49+
Via the `deps` attribute they depend on their respective `java_grpc_library` targets.
50+
For more information on creating these see https://github.com/grpc/grpc-java.

demos/bazel/WORKSPACE

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
workspace(name = "com_salesforce_servicelibs_reactive_grpc_demos_bazel")
2+
3+
local_repository(
4+
name = "com_salesforce_servicelibs_reactive_grpc",
5+
path = "../.."
6+
)
7+
8+
load("@com_salesforce_servicelibs_reactive_grpc//bazel:repositories.bzl", "repositories")
9+
10+
repositories()
11+
12+
load("@io_grpc_grpc_java//:repositories.bzl", "grpc_java_repositories")
13+
14+
grpc_java_repositories()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
java_library(
2+
name = "reactivegrpc",
3+
srcs = glob(["*.java"]),
4+
visibility = ["//src/test:__subpackages__"],
5+
deps = [
6+
"//src/main/proto:helloworld_java_grpc",
7+
"//src/main/proto:helloworld_java_proto",
8+
"//src/main/proto:helloworld_rx_grpc",
9+
"//src/main/proto:nested_java_proto",
10+
"//src/main/proto:nested_rx_grpc",
11+
"@io_grpc_grpc_java//core",
12+
"@io_grpc_grpc_java//core:inprocess",
13+
"@io_reactivex_rxjava2_rxjava",
14+
],
15+
)
16+
17+
java_binary(
18+
name = "reactivegrpc_bin",
19+
main_class = "com.salesforce.servicelibs.reactivegrpc.BazelProof",
20+
runtime_deps = [":reactivegrpc"],
21+
)

0 commit comments

Comments
 (0)