Skip to content

Commit e5f8326

Browse files
committed
Reimplemented grpc-gateway as a rule (no longer genrule)
1 parent 43cffa2 commit e5f8326

File tree

14 files changed

+164
-235
lines changed

14 files changed

+164
-235
lines changed

bzl/base/class.bzl

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
def get_generated_filename_extensions(lang, self):
22

33
if not hasattr(lang, "protobuf"):
4-
fail("Required struct 'protobuf' not found in lang %s" % lang.name)
5-
6-
if not hasattr(lang.protobuf, "file_extensions"):
7-
fail("Required string_list 'file_extensions' not found in protobuf struct %s" % lang.protobuf)
4+
return
85

9-
exts = getattr(lang.protobuf, "file_extensions")
6+
exts = getattr(lang.protobuf, "file_extensions", [])
107

118
with_grpc = self.get("with_grpc", False)
129
ctx = self.get("ctx", None)
@@ -24,6 +21,9 @@ def build_generated_files(lang, self):
2421
"""Build a list of generated filenames (used by rule)"""
2522
exts = get_generated_filename_extensions(lang, self)
2623

24+
if not exts:
25+
return
26+
2727
ctx = self.get("ctx", None)
2828
if ctx == None:
2929
fail("build_generated_files can only be used in bazel context")
@@ -32,7 +32,7 @@ def build_generated_files(lang, self):
3232
if not protos:
3333
fail("Empty proto file input list.")
3434

35-
for srcfile in self.get("srcs", []):
35+
for srcfile in protos:
3636
base = srcfile.basename[:-len(".proto")]
3737
for ext in exts:
3838
pbfile = ctx.new_file(base + ext)
@@ -57,8 +57,7 @@ def build_generated_filenames(lang, self):
5757
def build_imports(lang, self):
5858
"""Build the list of imports"""
5959
ctx = self["ctx"]
60-
if ctx:
61-
self["imports"] = self.get("imports", []) + ctx.attr.imports
60+
self["imports"] = self.get("imports", []) + ctx.attr.imports
6261

6362
def build_plugin_out(name, key, lang, self):
6463
#print("build_plugin_out(%s, %s)" % (name, key))
@@ -140,7 +139,6 @@ def build_protoc_command(lang, self):
140139

141140
def build_inputs(lang, self):
142141
"""Build a list of inputs to the ctx.action protoc"""
143-
#self["inputs"] += self["srcs"]
144142
self["requires"] += self["srcs"]
145143

146144

bzl/build_file/com_github_grpc_ecosystem_grpc_gateway.BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ go_library(
1919
)
2020

2121
filegroup(
22-
name = "googleapis",
22+
name = "googleapi_protos",
2323
srcs = glob([
2424
"third_party/googleapis/google/api/*.proto",
2525
]),

bzl/classes.bzl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ load("//bzl:javanano/class.bzl", JAVANANO = "CLASS")
88
load("//bzl:grpc_gateway/class.bzl", GATEWAY = "CLASS")
99

1010
CLASSES = {
11-
"base": BASE,
12-
"cpp": CPP,
13-
"gateway": GATEWAY,
14-
"java": JAVA,
15-
"javanano": JAVANANO,
16-
"go": GO,
17-
"py": PYTHON,
18-
"ruby": RUBY,
11+
BASE.name: BASE,
12+
CPP.name: CPP,
13+
GATEWAY.name: GATEWAY,
14+
JAVA.name: JAVA,
15+
JAVANANO.name: JAVANANO,
16+
GO.name: GO,
17+
PYTHON.name: PYTHON,
18+
RUBY.name: RUBY,
1919
}

bzl/cpp/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ cc_library(
7171
srcs = ['MyApp.cpp'],
7272
deps = [
7373
":protolib"
74-
] + CC.grpc.compile_deps,
74+
] + CPP.grpc.compile_deps,
7575
)
7676
```
7777

bzl/cpp/rules.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ load("//bzl:protoc.bzl", "implement", "EXECUTABLE")
22
load("//bzl:util.bzl", "invoke")
33
load("//bzl:cpp/class.bzl", CPP = "CLASS")
44

5-
cc_proto_compile = implement(["cpp"])
5+
cc_proto_compile = implement([CPP.name])
66

77
def cc_proto_library(
88
name,

bzl/go/class.bzl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,20 @@ def implement_compile_attributes(lang, self):
1919
cfg = HOST_CFG,
2020
)
2121

22+
attrs["go_import_map"] = attr.string_dict()
23+
2224

2325
def build_protobuf_out(lang, self):
2426
"""Override behavior to add a plugin option before building the --go_out option"""
2527
if self.get("with_grpc", False):
2628
self["protobuf_plugin_options"] = self.get("protobuf_plugin_options", []) + ["plugins=grpc"]
29+
30+
ctx = self["ctx"]
31+
if ctx.attr.verbose > 1:
32+
print("go_import_map: %s" % ctx.attr.go_import_map)
33+
for k, v in ctx.attr.go_import_map.items():
34+
self["protobuf_plugin_options"] += ["M%s=%s" % (k, v)]
35+
2736
invokesuper("build_protobuf_out", lang, self)
2837

2938

@@ -37,8 +46,6 @@ def build_imports(lang, self):
3746
invokesuper("build_imports", lang, self)
3847

3948
ctx = self["ctx"]
40-
if not ctx:
41-
fail("Bazel context is required for build_imports")
4249

4350
go_prefix = ctx.attr.go_prefix.go_prefix
4451

bzl/go/rules.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ load("//bzl:protoc.bzl", "implement", "EXECUTABLE")
22
load("//bzl:go/class.bzl", GO = "CLASS")
33
load("@io_bazel_rules_go//go:def.bzl", "go_library")
44

5-
go_proto_compile = implement(["go"])
5+
go_proto_compile = implement([GO.name])
66

77
def go_proto_library(
88
name,

bzl/grpc_gateway/class.bzl

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,85 @@ load("//bzl:base/class.bzl", BASE = "CLASS", "build_plugin_out")
22
load("//bzl:go/class.bzl", GO = "CLASS")
33
load("//bzl:util.bzl", "invokesuper")
44

5+
# https://github.com/bazelbuild/bazel/blame/master/src/main/java/com/google/devtools/build/lib/packages/BuildType.java#L227
6+
# NPE at BuildType.java:227 when str() is used to wrap Label i.e. attr.label_list(default = [str(Label("...")])
7+
8+
def implement_compile_attributes(lang, self):
9+
"""Override attributes for the X_proto_compile rule"""
10+
GO.implement_compile_attributes(lang, self)
11+
#invokesuper("implement_compile_attributes", lang, self)
12+
13+
attrs = self["attrs"]
14+
15+
attrs["_googleapi_protos"] = attr.label(
16+
default = Label(
17+
"@com_github_grpc_ecosystem_grpc_gateway//:googleapi_protos",
18+
),
19+
)
20+
21+
attrs["logtostderr"] = attr.bool(default = True)
22+
attrs["alsologtostderr"] = attr.bool()
23+
attrs["stderrthreshold"] = attr.int()
24+
attrs["log_backtrace_at"] = attr.int()
25+
attrs["log_dir"] = attr.string()
26+
attrs["log_level"] = attr.int()
27+
# TODO:-vmodule value: what does this mean?
28+
29+
attrs["import_prefix"] = attr.string()
30+
531

632
def build_imports(lang, self):
733
invokesuper("build_imports", lang, self)
34+
835
self["imports"] += [
9-
"external/com_github_grpc_ecosystem_grpc_gateway/third_party/googleapis",
10-
"external/com_github_google_protobuf/src",
11-
".",
36+
"external/com_github_grpc_ecosystem_grpc_gateway/third_party/googleapis/",
37+
"external/com_github_google_protobuf/src/",
1238
]
1339

1440
def build_inputs(lang, self):
1541
invokesuper("build_inputs", lang, self)
16-
self["requires"] += [
17-
"@com_github_grpc_ecosystem_grpc_gateway/third_party/googleapis",
18-
]
42+
ctx = self["ctx"]
43+
self["requires"] += list(ctx.attr._googleapi_protos.files)
44+
45+
46+
def build_protobuf_out(lang, self):
47+
# Standard protobuf outputs are implemented by the GO.class, so
48+
# there is no additional work to be done here.
49+
pass
50+
1951

2052
def build_grpc_out(lang, self):
53+
ctx = self["ctx"]
54+
outdir = self["outdir"]
2155
opts = self.get("gateway_plugin_options", [])
22-
opts += ["logtostderr=true"]
23-
outdir = self["gendir"]
24-
if opts:
25-
outdir = ",".join(opts) + ":" + outdir
26-
self["args"] += ["--grpc-gateway_out=%s" % (outdir)]
56+
57+
if ctx.attr.logtostderr:
58+
opts += ["logtostderr=true"]
59+
if ctx.attr.alsologtostderr:
60+
opts += ["alsologtostderr=true"]
61+
if ctx.attr.log_backtrace_at:
62+
opts += ["log_backtrace_at=%s" % ctx.attr.log_backtrace_at]
63+
if ctx.attr.stderrthreshold:
64+
opts += ["stderrthreshold=%s" % ctx.attr.stderrthreshold]
65+
if ctx.attr.log_dir:
66+
opts += ["log_dir=%s" % ctx.attr.log_dir]
67+
if ctx.attr.log_level:
68+
opts += ["v=%s" % ctx.attr.log_level]
69+
if ctx.attr.import_prefix:
70+
opts += ["import_prefix=%s" % ctx.attr.import_prefix]
71+
72+
params = ",".join(opts) + ":" + outdir
73+
74+
self["args"] += ["--grpc-gateway_out=%s" % params]
2775

2876

2977
CLASS = struct(
3078
parent = BASE,
31-
name = "go", # Left as "go" to implement gen_go and hence --protoc-gen-go=...
32-
short_name = "gateway",
33-
34-
protobuf = struct(
35-
name = GO.protobuf.name,
36-
file_extensions = GO.protobuf.file_extensions,
37-
executable = GO.protobuf.executable,
38-
tools = [
39-
"@com_github_grpc_ecosystem_grpc_gateway//:googleapis",
40-
],
41-
default_options = [
42-
"Mgoogle/api/annotations.proto=github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api",
43-
],
44-
requires = GO.protobuf.requires,
45-
compile_deps = GO.protobuf.compile_deps,
46-
),
79+
name = "gateway",
80+
81+
default_go_import_map = {
82+
"google/api/annotations.proto": "github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api"
83+
},
4784

4885
grpc = struct(
4986
name = 'protoc-gen-grpc-gateway',
@@ -62,8 +99,11 @@ CLASS = struct(
6299
],
63100
),
64101

65-
build_protobuf_out = GO.build_protobuf_out,
102+
build_protobuf_out = build_protobuf_out,
103+
implement_compile_attributes = implement_compile_attributes,
104+
66105
build_grpc_out = build_grpc_out,
67106
build_imports = build_imports,
68107
build_inputs = build_inputs,
108+
69109
)

0 commit comments

Comments
 (0)