Skip to content

Commit 073fa95

Browse files
committed
Implemented go, cpp as rule instead of genrule
1 parent 4951c88 commit 073fa95

File tree

25 files changed

+307
-266
lines changed

25 files changed

+307
-266
lines changed

WORKSPACE

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ workspace(name = "org_pubref_rules_protobuf")
44
# Go support requires rules_go
55
# ================================================================
66

7-
git_repository(
8-
name = "io_bazel_buildifier",
9-
commit = "88525b110d7c5a3cdeebc92926a35864874bd878",
10-
remote = "https://github.com/bazelbuild/buildifier.git",
11-
)
12-
137
git_repository(
148
name = "io_bazel_rules_go",
159
#tag = "0.0.4",
@@ -28,13 +22,13 @@ go_repositories()
2822
load("//bzl:rules.bzl", "protobuf_repositories")
2923

3024
protobuf_repositories(
31-
# For demonstration purposes
25+
# For demonstration purposes of how to override dependencies.
3226
overrides = {
3327
"com_github_golang_protobuf": {
3428
"commit": "2c1988e8c18d14b142c0b472624f71647cf39adb", # Aug 8, 2016
3529
},
3630
},
37-
verbose = 1,
31+
verbose = 0,
3832
with_cpp = True,
3933
with_go = True,
4034
with_grpc_gateway = True,

bzl/base/class.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ def build_generated_filenames(lang, self):
5656

5757
def build_imports(lang, self):
5858
"""Build the list of imports"""
59-
pass
60-
59+
ctx = self["ctx"]
60+
if ctx:
61+
self["imports"] = self.get("imports", []) + ctx.attr.imports
6162

6263
def build_plugin_out(name, key, lang, self):
6364
#print("build_plugin_out(%s, %s)" % (name, key))
@@ -131,7 +132,6 @@ def build_protobuf_invocation(lang, self):
131132

132133
def build_protoc_command(lang, self):
133134
"""Build a command list required for genrule execution"""
134-
135135
self["cmd"] += ["$(location %s)" % self["protoc"]]
136136
self["cmd"] += ["-I" + i for i in self["imports"]]
137137
self["cmd"] += self["args"]

bzl/classes.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ load("//bzl:python/class.bzl", PYTHON = "CLASS")
55
load("//bzl:ruby/class.bzl", RUBY = "CLASS")
66
load("//bzl:java/class.bzl", JAVA = "CLASS")
77
load("//bzl:javanano/class.bzl", JAVANANO = "CLASS")
8-
load("//bzl:grpc_gateway/class.bzl", GRPC_GATEWAY = "CLASS")
8+
load("//bzl:grpc_gateway/class.bzl", GATEWAY = "CLASS")
99

1010
CLASSES = {
1111
"base": BASE,
1212
"cpp": CPP,
13-
"grpc_gateway": GRPC_GATEWAY,
13+
"gateway": GATEWAY,
1414
"java": JAVA,
1515
"javanano": JAVANANO,
1616
"go": GO,

bzl/cpp/class.bzl

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,45 @@
11
load("//bzl:base/class.bzl", BASE = "CLASS")
22

33
CLASS = struct(
4-
parent = BASE,
5-
name = "cpp",
6-
short_name = "cpp",
7-
8-
protobuf = struct(
9-
file_extensions = [".pb.h", ".pb.cc"],
10-
compile_deps = [
11-
"//external:protobuf_clib",
12-
],
13-
requires = [
14-
"protobuf",
15-
"external_protobuf_clib",
16-
"gtest", # not really a protobuf dependency, but needed
17-
# for our internal tests. Clean this up.
18-
],
19-
),
20-
grpc = struct(
21-
executable = "//external:protoc_gen_grpc_cpp",
22-
name = "protoc-gen-grpc",
23-
file_extensions = [".grpc.pb.h", ".grpc.pb.cc"],
24-
requires = [
25-
"grpc",
26-
"zlib",
27-
"external_zlib",
28-
"nanopb",
29-
"external_nanopb",
30-
"boringssl",
31-
"libssl",
32-
"external_protobuf_compiler",
33-
"third_party_protoc",
34-
"external_protoc_gen_grpc_cpp",
35-
],
36-
compile_deps = [
37-
"//external:protobuf_clib",
38-
'@com_github_grpc_grpc//:grpc++',
39-
],
40-
),
4+
parent = BASE,
5+
name = "cpp",
6+
short_name = "cpp",
7+
# cc_*.pb.* outputs need to output to genfiles (not binfiles)
8+
# for some reason.
9+
output_to_genfiles = True,
4110

11+
protobuf = struct(
12+
file_extensions = [".pb.h", ".pb.cc"],
13+
compile_deps = [
14+
"//external:protobuf_clib",
15+
],
16+
# gtest not a protobuf dependency but needed for our
17+
# internal tests. Clean this up.
18+
requires = [
19+
"protobuf",
20+
"external_protobuf_clib",
21+
"gtest",
22+
],
23+
),
24+
grpc = struct(
25+
executable = "//external:protoc_gen_grpc_cpp",
26+
name = "protoc-gen-grpc",
27+
file_extensions = [".grpc.pb.h", ".grpc.pb.cc"],
28+
requires = [
29+
"grpc",
30+
"zlib",
31+
"external_zlib",
32+
"nanopb",
33+
"external_nanopb",
34+
"boringssl",
35+
"libssl",
36+
"external_protobuf_compiler",
37+
"third_party_protoc",
38+
"external_protoc_gen_grpc_cpp",
39+
],
40+
compile_deps = [
41+
"//external:protobuf_clib",
42+
'@com_github_grpc_grpc//:grpc++',
43+
],
44+
),
4245
)

bzl/cpp/rules.bzl

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

55
cc_proto_compile = implement(["cpp"])
66

77
def cc_proto_library(
88
name,
9-
protos = [],
10-
lang = CPP,
11-
srcs = [],
9+
protos,
10+
copy_protos_to_genfiles = True,
11+
deps = [],
12+
grpc_plugin = None,
13+
grpc_plugin_options = [],
1214
imports = [],
13-
visibility = None,
14-
testonly = 0,
15-
protoc = EXECUTABLE,
15+
lang = CPP,
16+
proto_compile = cc_proto_compile,
1617
protobuf_plugin_options = [],
1718
protobuf_plugin = None,
18-
grpc_plugin = None,
19-
grpc_plugin_options = [],
20-
descriptor_set = None,
21-
verbose = False,
19+
protoc = EXECUTABLE,
20+
srcs = [],
21+
verbose = 0,
22+
visibility = None,
2223
with_grpc = False,
23-
deps = [],
24-
hdrs = [],
25-
linkopts = ['-ldl'],
2624
**kwargs):
2725

28-
result = protoc_genrule(
29-
spec = [lang],
30-
name = name + "_pb",
31-
protos = protos,
32-
protoc = protoc,
33-
protobuf_plugin = protobuf_plugin,
34-
visibility = visibility,
35-
testonly = testonly,
36-
imports = imports,
37-
with_grpc = with_grpc,
38-
verbose = verbose,
39-
)
26+
args = {}
27+
args["name"] = name + "_pb"
28+
args["copy_protos_to_genfiles"] = copy_protos_to_genfiles
29+
args["deps"] = deps
30+
args["imports"] = imports
31+
args["gen_" + lang.name] = True
32+
args["gen_grpc_" + lang.name] = with_grpc
33+
args["gen_protobuf_" + lang.name + "_plugin"] = protobuf_plugin
34+
args["gen_" + lang.name + "_plugin_options"] = protobuf_plugin_options
35+
args["gen_grpc_" + lang.name + "_plugin"] = grpc_plugin
36+
args["protoc"] = protoc
37+
args["protos"] = protos
38+
args["verbose"] = verbose
39+
args["with_grpc"] = with_grpc
40+
41+
proto_compile(**args)
42+
43+
if with_grpc and hasattr(lang, "grpc"):
44+
proto_deps = [str(Label(dep)) for dep in getattr(lang.grpc, "compile_deps", [])]
45+
elif hasattr(lang, "protobuf"):
46+
proto_deps = [str(Label(dep)) for dep in getattr(lang.protobuf, "compile_deps", [])]
4047

41-
if with_grpc:
42-
cc_deps = [str(Label(dep)) for dep in getattr(lang.grpc, "compile_deps", [])]
43-
else:
44-
cc_deps = [str(Label(dep)) for dep in getattr(lang.protobuf, "compile_deps", [])]
48+
cc_deps = list(set(deps + proto_deps))
4549

4650
native.cc_library(
4751
name = name,
48-
srcs = srcs + result.outs,
49-
deps = deps + cc_deps,
50-
linkopts = linkopts,
52+
srcs = srcs + [name + "_pb"],
53+
deps = cc_deps,
5154
**kwargs
5255
)

bzl/go/class.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ load("//bzl:util.bzl", "invokesuper")
55
def _build_protobuf_out(lang, self):
66
"""Override behavior to add a plugin option before building the --go_out option"""
77
if self.get("with_grpc", False):
8-
self["protobuf_plugin_options"] += ["plugins=grpc"]
8+
self["protobuf_plugin_options"] = self.get("protobuf_plugin_options", []) + ["plugins=grpc"]
99
invokesuper("build_protobuf_out", lang, self)
1010

1111

bzl/go/rules.bzl

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("//bzl:protoc.bzl", "protoc_genrule", "implement")
1+
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

@@ -7,38 +7,49 @@ go_proto_compile = implement(["go"])
77
def go_proto_library(
88
name,
99
protos,
10+
copy_protos_to_genfiles = True,
11+
deps = [],
12+
grpc_plugin = None,
13+
grpc_plugin_options = [],
14+
imports = [],
1015
lang = GO,
16+
proto_compile = go_proto_compile,
17+
protobuf_plugin_options = [],
18+
protobuf_plugin = None,
19+
protoc = EXECUTABLE,
1120
srcs = [],
12-
imports = [],
21+
verbose = 0,
1322
visibility = None,
14-
testonly = 0,
15-
protoc = None,
16-
protobuf_plugin = None,
1723
with_grpc = False,
18-
verbose = 0,
1924
**kwargs):
2025

21-
result = protoc_genrule(
22-
spec = [lang],
23-
name = name + "_pb",
24-
protos = protos,
25-
protoc = protoc,
26-
protobuf_plugin = protobuf_plugin,
27-
visibility = visibility,
28-
testonly = testonly,
29-
imports = imports,
30-
with_grpc = with_grpc,
31-
verbose = verbose,
32-
)
26+
args = {}
27+
args["name"] = name + "_pb"
28+
args["copy_protos_to_genfiles"] = copy_protos_to_genfiles
29+
args["deps"] = deps
30+
args["imports"] = imports
31+
args["gen_" + lang.name] = True
32+
args["gen_grpc_" + lang.name] = with_grpc
33+
args["gen_protobuf_" + lang.name + "_plugin"] = protobuf_plugin
34+
args["gen_" + lang.name + "_plugin_options"] = protobuf_plugin_options
35+
args["gen_grpc_" + lang.name + "_plugin"] = grpc_plugin
36+
args["protoc"] = protoc
37+
args["protos"] = protos
38+
args["verbose"] = verbose
39+
args["with_grpc"] = with_grpc
40+
41+
proto_compile(**args)
42+
43+
if with_grpc and hasattr(lang, "grpc"):
44+
proto_deps = [str(Label(dep)) for dep in getattr(lang.grpc, "compile_deps", [])]
45+
elif hasattr(lang, "protobuf"):
46+
proto_deps = [str(Label(dep)) for dep in getattr(lang.protobuf, "compile_deps", [])]
3347

34-
if with_grpc:
35-
deps = [str(Label(dep)) for dep in getattr(lang.grpc, "compile_deps", [])]
36-
else:
37-
deps = [str(Label(dep)) for dep in getattr(lang.protobuf, "compile_deps", [])]
48+
go_deps = list(set(deps + proto_deps))
3849

3950
go_library(
40-
name = name,
41-
srcs = result.outs,
42-
deps = deps,
43-
**kwargs
51+
name = name,
52+
srcs = srcs + [ name + "_pb"],
53+
deps = go_deps,
54+
**kwargs
4455
)

bzl/grpc_gateway/class.bzl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ 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+
6+
def build_protobuf_out(lang, self):
7+
"""Build the --{lang}_out option"""
8+
build_plugin_out(lang.name, "protobuf", lang, self)
9+
10+
511
def build_imports(lang, self):
612
invokesuper("build_imports", lang, self)
713
self["imports"] += [
@@ -10,7 +16,6 @@ def build_imports(lang, self):
1016
".",
1117
]
1218

13-
1419
def build_grpc_out(lang, self):
1520
opts = self.get("gateway_plugin_options", [])
1621
opts += ["logtostderr=true"]
@@ -22,7 +27,7 @@ def build_grpc_out(lang, self):
2227

2328
CLASS = struct(
2429
parent = BASE,
25-
name = "go",
30+
name = "go", # Left as "go" to implement gen_go and hence --protoc-gen-go=...
2631
short_name = "gateway",
2732

2833
protobuf = struct(

0 commit comments

Comments
 (0)