forked from stackb/rules_proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rust_proto_lib.bzl
62 lines (53 loc) · 1.62 KB
/
rust_proto_lib.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
load("//:compile.bzl", "ProtoCompileInfo")
RustProtoLibInfo = provider(fields = {
"name": "rule name",
"lib": "lib.rs file",
})
def _basename(f):
return f.basename[:-len(f.extension) - 1]
def _rust_proto_lib_impl(ctx):
"""Generate a lib.rs file for the crates."""
compilation = ctx.attr.compilation[ProtoCompileInfo]
deps = ctx.attr.deps
srcs = compilation.files
lib_rs = ctx.actions.declare_file("%s/lib.rs" % compilation.label.name)
# Search in the plugin list for 'protoc_gen_rust_grpc' or similar.
grpc = False
for plugin in compilation.plugins:
if plugin.executable.path.endswith("grpc"):
grpc = True
break
content = ["extern crate protobuf;"]
if grpc:
content.append("extern crate grpc;")
content.append("extern crate tls_api;")
# for dep in deps:
# content.append("extern crate %s;" % dep.label.name)
# content.append("pub use %s::*;" % dep.label.name)
for f in srcs:
content.append("pub mod %s;" % _basename(f))
content.append("pub use %s::*;" % _basename(f))
ctx.actions.write(
lib_rs,
"\n".join(content),
False,
)
return [RustProtoLibInfo(
name = ctx.label.name,
lib = lib_rs,
), DefaultInfo(
files = depset([lib_rs]),
)]
rust_proto_lib = rule(
implementation = _rust_proto_lib_impl,
attrs = {
"compilation": attr.label(
providers = [ProtoCompileInfo],
mandatory = True,
),
"deps": attr.label_list(
# providers = [""],
),
},
output_to_genfiles = True,
)