Skip to content

Commit 7c0729b

Browse files
committed
- grpc_gateway moved to own folder.
- grpc_gateway functional and passing test. - implementation of grpc_gateway_binary - removal of old genrule related code.
1 parent e5f8326 commit 7c0729b

File tree

13 files changed

+132
-136
lines changed

13 files changed

+132
-136
lines changed

bzl/base/class.bzl

Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
def get_generated_filename_extensions(lang, self):
2-
3-
if not hasattr(lang, "protobuf"):
4-
return
5-
6-
exts = getattr(lang.protobuf, "file_extensions", [])
7-
8-
with_grpc = self.get("with_grpc", False)
92
ctx = self.get("ctx", None)
10-
if ctx != None:
11-
with_grpc = getattr(ctx.attr, "gen_grpc_" + lang.name, False)
3+
with_grpc = self["with_grpc"]
4+
exts = []
125

13-
if with_grpc:
14-
if not hasattr(lang, "grpc"):
15-
fail("Language %s does not support gRPC" % lang.name)
16-
exts += getattr(lang.grpc, "file_extensions", [])
6+
if hasattr(lang, "protobuf"):
7+
exts += getattr(lang.protobuf, "file_extensions", [])
8+
9+
if with_grpc or getattr(ctx.attr, "gen_grpc_" + lang.name, False):
10+
if hasattr(lang, "grpc"):
11+
exts += getattr(lang.grpc, "file_extensions", [])
1712

1813
return exts
1914

15+
2016
def build_generated_files(lang, self):
2117
"""Build a list of generated filenames (used by rule)"""
2218
exts = get_generated_filename_extensions(lang, self)
2319

20+
ctx = self.get("ctx", None)
21+
if ctx.attr.verbose > 1:
22+
print("generated_file extensions for language %s: %s" % (lang.name, exts))
23+
2424
if not exts:
2525
return
2626

27-
ctx = self.get("ctx", None)
2827
if ctx == None:
2928
fail("build_generated_files can only be used in bazel context")
3029

@@ -39,26 +38,12 @@ def build_generated_files(lang, self):
3938
self["provides"] += [pbfile]
4039

4140

42-
def build_generated_filenames(lang, self):
43-
"""Build a list of generated filenames (used by genrule)"""
44-
exts = get_generated_filename_extensions(lang, self)
45-
46-
protos = self.get("protos", [])
47-
if not protos:
48-
fail("Empty proto filename input list.")
49-
50-
for srcfile in protos:
51-
if not srcfile.endswith('.proto'):
52-
fail("Non .proto source file: %s" % srcfile, "protos")
53-
for ext in exts:
54-
self["outs"] += [srcfile.rsplit('.', 1)[0] + ext]
55-
56-
5741
def build_imports(lang, self):
5842
"""Build the list of imports"""
5943
ctx = self["ctx"]
6044
self["imports"] = self.get("imports", []) + ctx.attr.imports
6145

46+
6247
def build_plugin_out(name, key, lang, self):
6348
#print("build_plugin_out(%s, %s)" % (name, key))
6449
if not hasattr(lang, key):
@@ -142,30 +127,6 @@ def build_inputs(lang, self):
142127
self["requires"] += self["srcs"]
143128

144129

145-
def build_tools(lang, self):
146-
"""Build a list of tools required for genrule execution"""
147-
148-
self["tools"] += [self["protoc"]]
149-
150-
protobuf_plugin = self["protobuf_plugin"]
151-
if not protobuf_plugin:
152-
if hasattr(lang, "protobuf") and hasattr(lang.protobuf, "executable"):
153-
protobuf_plugin = lang.protobuf.executable
154-
155-
if protobuf_plugin:
156-
self["tools"] += [protobuf_plugin]
157-
158-
grpc_plugin = self["grpc_plugin"]
159-
if self["with_grpc"] and not grpc_plugin:
160-
if hasattr(lang, "grpc") and hasattr(lang.grpc, "executable"):
161-
grpc_plugin = lang.grpc.executable
162-
163-
if grpc_plugin:
164-
self["tools"] += [grpc_plugin]
165-
166-
#print("tools: %s" % self["tools"])
167-
168-
169130
def post_execute(lang, self):
170131
"""No default post-execute actions"""
171132
pass
@@ -236,10 +197,8 @@ CLASS = struct(
236197
),
237198

238199
build_generated_files = build_generated_files,
239-
build_generated_filenames = build_generated_filenames,
240200
build_imports = build_imports,
241201
build_inputs = build_inputs,
242-
build_tools = build_tools,
243202
build_protobuf_invocation = build_protobuf_invocation,
244203
build_protobuf_out = build_protobuf_out,
245204
build_grpc_out = build_grpc_out,

bzl/cpp/rules.bzl

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

@@ -15,7 +15,7 @@ def cc_proto_library(
1515
protobuf_plugin_options = [],
1616
protobuf_plugin = None,
1717
proto_compile = cc_proto_compile,
18-
protoc = EXECUTABLE,
18+
protoc = PROTOC,
1919
srcs = [],
2020
verbose = 0,
2121
visibility = None,

bzl/go/rules.bzl

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

@@ -15,7 +15,7 @@ def go_proto_library(
1515
protobuf_plugin_options = [],
1616
protobuf_plugin = None,
1717
proto_compile = go_proto_compile,
18-
protoc = EXECUTABLE,
18+
protoc = PROTOC,
1919
srcs = [],
2020
verbose = 0,
2121
visibility = None,

bzl/grpc_gateway/class.bzl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ load("//bzl:util.bzl", "invokesuper")
88
def implement_compile_attributes(lang, self):
99
"""Override attributes for the X_proto_compile rule"""
1010
GO.implement_compile_attributes(lang, self)
11-
#invokesuper("implement_compile_attributes", lang, self)
1211

1312
attrs = self["attrs"]
1413

@@ -27,15 +26,17 @@ def implement_compile_attributes(lang, self):
2726
# TODO:-vmodule value: what does this mean?
2827

2928
attrs["import_prefix"] = attr.string()
29+
attrs["gateway_imports"] = attr.string_list(
30+
default = lang.default_imports
31+
)
3032

3133

3234
def build_imports(lang, self):
3335
invokesuper("build_imports", lang, self)
36+
# TODO(this is hardcoded. Make it configurable.)
37+
ctx = self["ctx"]
38+
self["imports"] += ctx.attr.gateway_imports
3439

35-
self["imports"] += [
36-
"external/com_github_grpc_ecosystem_grpc_gateway/third_party/googleapis/",
37-
"external/com_github_google_protobuf/src/",
38-
]
3940

4041
def build_inputs(lang, self):
4142
invokesuper("build_inputs", lang, self)
@@ -82,12 +83,17 @@ CLASS = struct(
8283
"google/api/annotations.proto": "github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api"
8384
},
8485

86+
default_imports = [
87+
"external/com_github_grpc_ecosystem_grpc_gateway/third_party/googleapis/",
88+
"external/com_github_google_protobuf/src/",
89+
],
90+
8591
grpc = struct(
8692
name = 'protoc-gen-grpc-gateway',
8793
file_extensions = [".pb.gw.go"],
8894
executable = "@com_github_grpc_ecosystem_grpc_gateway//:protoc-gen-grpc-gateway_bin",
8995
default_options = GO.grpc.default_options,
90-
requires = GO.grpc.requires + [
96+
requires = [
9197
"com_github_grpc_ecosystem_grpc_gateway",
9298
],
9399
compile_deps = GO.grpc.compile_deps + [
@@ -99,9 +105,8 @@ CLASS = struct(
99105
],
100106
),
101107

102-
build_protobuf_out = build_protobuf_out,
103108
implement_compile_attributes = implement_compile_attributes,
104-
109+
build_protobuf_out = build_protobuf_out,
105110
build_grpc_out = build_grpc_out,
106111
build_imports = build_imports,
107112
build_inputs = build_inputs,

bzl/grpc_gateway/rules.bzl

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
load("//bzl:protoc.bzl", "EXECUTABLE", "implement")
1+
load("//bzl:protoc.bzl", "PROTOC", "implement")
22
load("//bzl:go/class.bzl", GO = "CLASS")
33
load("//bzl:grpc_gateway/class.bzl", GATEWAY = "CLASS")
4-
load("@io_bazel_rules_go//go:def.bzl", "go_library")
4+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_binary")
55

6-
gateway_proto_compile = implement([GATEWAY.name, GO.name])
6+
grpc_gateway_proto_compile = implement([GO.name, GATEWAY.name])
77

8-
def gateway_proto_library(
8+
9+
def grpc_gateway_proto_library(
910
name,
1011
copy_protos_to_genfiles = False,
1112
deps = [],
@@ -15,12 +16,13 @@ def gateway_proto_library(
1516
lang = GATEWAY,
1617
protobuf_plugin_options = [],
1718
protobuf_plugin = None,
18-
proto_compile = gateway_proto_compile,
19-
protoc = EXECUTABLE,
19+
proto_compile = grpc_gateway_proto_compile,
20+
protoc = PROTOC,
2021
srcs = [],
2122
verbose = 0,
2223
visibility = None,
2324
go_deps = [],
25+
go_rule = go_library,
2426
go_srcs = [],
2527

2628
logtostderr = True,
@@ -65,9 +67,24 @@ def gateway_proto_library(
6567
deps += [str(Label(dep)) for dep in lang.grpc.compile_deps]
6668
deps = list(set(deps + go_deps))
6769

68-
go_library(
70+
go_rule(
6971
name = name,
7072
srcs = go_srcs + [name + ".pb"],
7173
deps = deps,
7274
**kwargs
7375
)
76+
77+
78+
def grpc_gateway_binary(name, protolib_name = "go_default_library", srcs = [], deps = [], protos = [], proto_deps = [], **kwargs):
79+
grpc_gateway_proto_library(
80+
name = protolib_name,
81+
srcs = protos,
82+
deps = proto_deps,
83+
**kwargs
84+
)
85+
86+
go_binary(
87+
name = name,
88+
srcs = srcs,
89+
deps = deps + [protolib_name] + GATEWAY.grpc.compile_deps,
90+
)

bzl/java/rules.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("//bzl:protoc.bzl", "EXECUTABLE", "implement")
1+
load("//bzl:protoc.bzl", "PROTOC", "implement")
22
load("//bzl:java/class.bzl", JAVA = "CLASS")
33

44
java_proto_compile = implement([JAVA.name])
@@ -14,7 +14,7 @@ def java_proto_library(
1414
protobuf_plugin_options = [],
1515
protobuf_plugin = None,
1616
proto_compile = java_proto_compile,
17-
protoc = EXECUTABLE,
17+
protoc = PROTOC,
1818
srcs = [],
1919
verbose = 0,
2020
visibility = None,

bzl/protoc.bzl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
load("//bzl:util.bzl", "invoke")
22
load("//bzl:classes.bzl", "CLASSES")
33

4-
EXECUTABLE = Label("@com_github_google_protobuf//:protoc")
4+
PROTOC = Label("//external:protoc")
55

66
def _get_gendir(ctx):
77
if ctx.attr.output_to_genfiles:
@@ -24,13 +24,13 @@ def _execute_rule(self):
2424
outputs = list(set(self["provides"] + ctx.outputs.outs))
2525

2626
if ctx.attr.verbose:
27-
print("protoc binary: " + ctx.executable.protoc.path)
27+
print("protoc executable: " + ctx.executable.protoc.path)
2828
for i in range(len(arguments)):
2929
print(" > arg%s: %s" % (i, arguments[i]))
30-
for i in inputs:
31-
print(" > input: %s" % i)
32-
for o in outputs:
33-
print(" > output: %s" % o)
30+
for i in range(len(inputs)):
31+
print(" > input%s: %s" % (i, inputs[i]))
32+
for i in range(len(outputs)):
33+
print(" > output%s: %s" % (i, outputs[i]))
3434

3535
ctx.action(
3636
mnemonic="ProtoCompile",
@@ -73,6 +73,7 @@ def _build_source_files(ctx, self):
7373
# workspace root.
7474
self["imports"] += ["."]
7575

76+
7677
def _protoc_rule_impl(ctx):
7778

7879
if ctx.attr.verbose:

examples/helloworld/go/gateway/BUILD

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)