Skip to content

Commit 78cb92f

Browse files
committed
Transitive proto_deps implemented for go importmap support
1 parent bb42937 commit 78cb92f

File tree

9 files changed

+187
-73
lines changed

9 files changed

+187
-73
lines changed

bzl/go/class.bzl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,52 @@ def _build_grpc_out(lang, self):
1414
pass
1515

1616

17+
def _build_imports(lang, self):
18+
"""@Override: Copy any named proto files in the imports to the genfiles area to make visible to protoc. Not sure this is necessary anymore."""
19+
invokesuper("build_imports", lang, self)
20+
21+
ctx = self["ctx"]
22+
if not ctx:
23+
fail("Bazel context is required for build_imports")
24+
25+
go_prefix = ctx.attr.go_prefix.go_prefix
26+
#print("go prefix: %s" % dir(go_prefix))
27+
28+
print("ctx.attr.deps: %s" % ctx.attr.deps)
29+
30+
for dep in ctx.attr.proto_deps:
31+
print("ctx.attr.dep[i]: %s" % dir(dep))
32+
provider = dep.proto
33+
print("proto provider: %s" % dir(provider))
34+
proto_packages = provider.transitive_packages
35+
print("proto_packages: %s" % proto_packages)
36+
for pkg, srcs in proto_packages.items():
37+
target = pkg.rsplit(':') # [0] == ctx.label.package, [1] == ctx.label.name
38+
print("target: %s" % target)
39+
for srcfile in srcs:
40+
src = srcfile.short_path
41+
dst = go_prefix + '/' + target[0]
42+
if target[1] != "go_default_library_pb":
43+
# slice off the '_pb' from 'mylib_pb'
44+
dst += "/" + target[1][:-len("_pb")]
45+
self["protobuf_plugin_options"] = self.get("protobuf_plugin_options", []) + ["M%s=%s" % (src, dst)]
46+
47+
# #print("ctx.attr.imports: %s" % type(ctx.attr.imports))
48+
# for target in ctx.attr.imports:
49+
# label = target.label
50+
# print("target: %s" % dir(target.label))
51+
# for srcfile in target.files:
52+
# print("srcfile: %s" % dir(srcfile))
53+
# src = srcfile.short_path
54+
# # The destination mapping is the go_prefix +
55+
# dst = go_prefix + '/' + srcfile.dirname
56+
# if ctx.label.name != "go_default_library":
57+
# # slice off the '_pb' from 'mylib_pb'
58+
# dst += "/" + ctx.label.name[:-len("_pb")]
59+
# self["protobuf_plugin_options"] = self.get("protobuf_plugin_options", []) + ["M%s=%s" % (src, dst)]
60+
61+
# srcfile.basename[:-len(".proto")]
62+
1763
CLASS = struct(
1864
parent = BASE,
1965
name = "go",
@@ -50,4 +96,5 @@ CLASS = struct(
5096

5197
build_protobuf_out = _build_protobuf_out,
5298
build_grpc_out = _build_grpc_out,
99+
build_imports = _build_imports,
53100
)

bzl/go/rules.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def go_proto_library(
1515
lang = GO,
1616
paths = [],
1717
proto_compile = go_proto_compile,
18+
#proto_deps = [],
1819
protobuf_plugin_options = [],
1920
protobuf_plugin = None,
2021
protoc = EXECUTABLE,
@@ -35,6 +36,7 @@ def go_proto_library(
3536
args["gen_" + lang.name + "_plugin_options"] = protobuf_plugin_options
3637
args["gen_grpc_" + lang.name + "_plugin"] = grpc_plugin
3738
args["paths"] = paths
39+
args["proto_deps"] = [d + "_pb" for d in deps]
3840
args["protoc"] = protoc
3941
args["protos"] = protos
4042
args["verbose"] = verbose
@@ -49,6 +51,9 @@ def go_proto_library(
4951

5052
go_deps = list(set(deps + proto_deps))
5153

54+
#print("go_deps %s" % dir(go_deps[0]))
55+
#go_deps = [d for d in go_deps if hasattr(d, "foo")]
56+
5257
go_library(
5358
name = name,
5459
srcs = srcs + [ name + "_pb"],

bzl/java/class.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def _post_execute(lang, self):
3434
srcjar = ctx.outputs.srcjar
3535
protojar = self["protojar"]
3636

37-
# Rename protojar to srcjar so that rules like java_library can
38-
# consume it.
37+
# Rename proto.jar to proto.srcjar so that bazel
38+
# java_{library,binary} rules can consume it.
3939
ctx.action(
4040
mnemonic = "FixProtoSrcJar",
4141
inputs = [protojar],

bzl/protoc.bzl

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ def protoc_genrule(name,
2929
spec = [],
3030
self = {},
3131
#gendir = "$(BINDIR)", Note: It *has* to be GENDIR for some reason.
32-
gendir = "$(GENDIR)",
32+
outdir = "$(GENDIR)",
3333
protos = [],
34+
paths = [],
3435
protoc=EXECUTABLE,
3536
protobuf_plugin=None,
3637
protobuf_plugin_options=[],
@@ -51,8 +52,10 @@ def protoc_genrule(name,
5152
self += {
5253
"name": name,
5354
"ctx": None,
54-
"gendir": gendir,
55+
"gendir": outdir,
56+
"outdir": outdir,
5557
"protos": protos,
58+
"paths": paths,
5659
"protoc": protoc,
5760
"protobuf_plugin": protobuf_plugin,
5861
"protobuf_plugin_options": protobuf_plugin_options,
@@ -193,14 +196,21 @@ def _protoc_rule_impl(ctx):
193196
"verbose": getattr(ctx.attr, "verbose", True),
194197
"with_grpc": getattr(ctx.attr, "with_grpc", False),
195198
#"descriptor_set_file": descriptor_set_file,
199+
"transitive_imports": [],
200+
"transitive_paths": [],
201+
"transitive_packages": {},
202+
"transitive_requires": [],
203+
"transitive_srcs": [],
196204
}
197205

198-
# Propogate proto deps: TODO: this is completely untested.
199-
for dep in ctx.attr.deps:
200-
if hasattr(dep, "proto"):
201-
self["paths"] += dep.proto.paths
202-
self["requires"] += dep.proto.deps
203-
self["srcs"] += dep.proto.srcs
206+
# Propogate proto deps:
207+
for dep in ctx.attr.proto_deps:
208+
print("ctx.attr dep: %s" % dir(dep))
209+
self["transitive_imports"] += dep.proto.transitive_imports
210+
self["transitive_paths"] += dep.proto.transitive_paths
211+
self["transitive_packages"] += dep.proto.transitive_packages
212+
self["transitive_requires"] += dep.proto.transitive_requires
213+
self["transitive_srcs"] += dep.proto.transitive_srcs
204214

205215
# Copy source files over to outdir
206216
_build_source_files(ctx, self)
@@ -232,21 +242,36 @@ def _protoc_rule_impl(ctx):
232242
for lang in spec:
233243
invoke("post_execute", lang, self)
234244

245+
self["packages"] = {
246+
ctx.label.package + ':' + ctx.label.name: self["srcs"],
247+
}
248+
235249
return struct(
236250
files=set(self["provides"]),
237251
proto=struct(
238-
srcs = set(self["srcs"]),
252+
239253
imports = self["imports"],
240254
paths = self["paths"],
241-
deps = self["requires"],
255+
packages = self["packages"],
256+
srcs = set(self["srcs"]),
257+
requires = self["requires"],
258+
259+
transitive_requires = self["requires"] + self["transitive_requires"],
260+
transitive_imports = self["imports"] + self["transitive_imports"],
261+
transitive_paths = self["paths"] + self["transitive_paths"],
262+
transitive_packages = self["packages"] + self["transitive_packages"],
263+
transitive_srcs = self["srcs"] + self["transitive_srcs"],
242264
),
243265
)
244266

245267

246268
def implement(spec):
247269

248270
attrs = {}
249-
outputs = {}
271+
272+
outputs = {
273+
#"proto": "%{name}.proto",
274+
}
250275

251276
# Language descriptor has an opportunity to override this.
252277
output_to_genfiles = False
@@ -260,11 +285,11 @@ def implement(spec):
260285
allow_files = FileType([".proto"]),
261286
)
262287

263-
# ? How to deps interact here? Don't understand this. Provider
264-
# aspect is "proto".
265288
#attrs["deps"] = attr.label_list(providers = ["proto"])
266289
attrs["deps"] = attr.label_list()
267290

291+
attrs["proto_deps"] = attr.label_list(providers = ["proto"])
292+
268293
# Options to be passed to protoc as --proto_path. Differs from
269294
# imports in that these are raw strings rather than labels.
270295
attrs["paths"] = attr.string_list()
@@ -365,6 +390,17 @@ def implement(spec):
365390
default = output_to_genfiles,
366391
)
367392

393+
# Get the go_prefox
394+
attrs["go_prefix"] = attr.label(
395+
providers = ["go_prefix"],
396+
default = Label(
397+
"//:go_prefix",
398+
relative_to_caller_repository = True,
399+
),
400+
allow_files = False,
401+
cfg = HOST_CFG,
402+
)
403+
368404
return rule(
369405
implementation = _protoc_rule_impl,
370406
attrs = attrs,

examples/helloworld/go/gateway/BUILD

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,35 @@ load(
66
GO = "CLASS",
77
)
88

9-
go_binary(
10-
name = "greeter",
11-
srcs = [
12-
"greeter.go",
13-
],
14-
deps = [
15-
"//examples/helloworld/proto:gw",
16-
"@com_github_golang_glog//:go_default_library",
17-
"@com_github_grpc_ecosystem_grpc_gateway//:runtime",
18-
"@org_golang_google_grpc//:go_default_library",
19-
"@org_golang_x_net//:context",
20-
],
21-
)
9+
# go_binary(
10+
# name = "greeter",
11+
# srcs = [
12+
# "greeter.go",
13+
# ],
14+
# deps = [
15+
# "//examples/helloworld/proto:gw",
16+
# "@com_github_golang_glog//:go_default_library",
17+
# "@com_github_grpc_ecosystem_grpc_gateway//:runtime",
18+
# "@org_golang_google_grpc//:go_default_library",
19+
# "@org_golang_x_net//:context",
20+
# ],
21+
# )
2222

23-
go_test(
24-
name = "greeter_test",
25-
size = "small",
26-
srcs = [
27-
"greeter.go",
28-
"greeter_test.go",
29-
],
30-
deps = [
31-
"//examples/helloworld/go/server:greeter",
32-
"//examples/helloworld/proto:go",
33-
"//examples/helloworld/proto:gw",
34-
"@com_github_golang_glog//:go_default_library",
35-
"@com_github_golang_protobuf//:jsonpb",
36-
"@com_github_grpc_ecosystem_grpc_gateway//:runtime",
37-
"@org_golang_google_grpc//:go_default_library",
38-
"@org_golang_x_net//:context",
39-
],
40-
)
23+
# go_test(
24+
# name = "greeter_test",
25+
# size = "small",
26+
# srcs = [
27+
# "greeter.go",
28+
# "greeter_test.go",
29+
# ],
30+
# deps = [
31+
# "//examples/helloworld/go/server:greeter",
32+
# "//examples/helloworld/proto:go",
33+
# "//examples/helloworld/proto:gw",
34+
# "@com_github_golang_glog//:go_default_library",
35+
# "@com_github_golang_protobuf//:jsonpb",
36+
# "@com_github_grpc_ecosystem_grpc_gateway//:runtime",
37+
# "@org_golang_google_grpc//:go_default_library",
38+
# "@org_golang_x_net//:context",
39+
# ],
40+
# )

examples/helloworld/java/org/pubref/rules_protobuf/examples/helloworld/client/BUILD

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ java_test(
1010
"HelloWorldClientTest.java",
1111
],
1212
test_class = "org.pubref.rules_protobuf.examples.helloworld.client.HelloWorldClientTest",
13-
runtime_deps = JAVA.grpc.netty_runtime_deps,
1413
deps = [
1514
":client",
1615
"//examples/helloworld/java/org/pubref/rules_protobuf/examples/helloworld/server",
1716
"@junit_junit_4//jar",
1817
],
18+
runtime_deps = JAVA.grpc.netty_runtime_deps,
1919
)
2020

2121
java_binary(
@@ -36,11 +36,11 @@ java_library(
3636
] + JAVA.grpc.compile_deps,
3737
)
3838

39-
java_library(
40-
name = "client_from_srcs",
41-
srcs = [
42-
"HelloWorldClient.java",
43-
"//examples/helloworld/java/org/pubref/rules_protobuf/examples/helloworld/proto:srcs",
44-
],
45-
deps = JAVA.grpc.compile_deps,
46-
)
39+
# java_library(
40+
# name = "client_from_srcs",
41+
# srcs = [
42+
# "HelloWorldClient.java",
43+
# "//examples/helloworld/java/org/pubref/rules_protobuf/examples/helloworld/proto:srcs",
44+
# ],
45+
# deps = JAVA.grpc.compile_deps,
46+
# )

examples/helloworld/java/org/pubref/rules_protobuf/examples/helloworld/proto/BUILD

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,22 @@ load("//bzl:java/rules.bzl", "java_proto_library", "java_proto_compile")
44

55
java_proto_library(
66
name = "proto",
7-
protos = ["//examples/helloworld/proto:srcs"],
7+
protos = [
8+
"//examples/helloworld/proto:srcs",
9+
"//examples/proto:srcs",
10+
],
11+
# deps = [
12+
# ":common",
13+
# ],
14+
verbose = 0,
15+
with_grpc = True,
16+
)
17+
18+
java_proto_library(
19+
name = "common",
20+
protos = [
21+
"//examples/proto:srcs",
22+
],
823
verbose = 0,
924
with_grpc = True,
1025
)

examples/helloworld/java/org/pubref/rules_protobuf/examples/helloworld/server/BUILD

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ java_test(
1313
],
1414
tags = ["exclusive"], # Address already in use if another server being tested has same port
1515
test_class = "org.pubref.rules_protobuf.examples.helloworld.server.HelloWorldServerTest",
16-
runtime_deps = JAVA.grpc.netty_runtime_deps,
1716
deps = [
1817
":server",
1918
"@junit_junit_4//jar",
2019
],
20+
runtime_deps = JAVA.grpc.netty_runtime_deps,
2121
)
2222

2323
java_binary(
@@ -32,9 +32,10 @@ java_library(
3232
name = "server",
3333
srcs = [
3434
"HelloWorldServer.java",
35-
"//examples/helloworld/java/org/pubref/rules_protobuf/examples/helloworld/proto:srcs",
3635
],
37-
deps = JAVA.grpc.compile_deps,
36+
deps = [
37+
"//examples/helloworld/java/org/pubref/rules_protobuf/examples/helloworld/proto",
38+
] + JAVA.grpc.compile_deps,
3839
)
3940

4041
java_library(

0 commit comments

Comments
 (0)