Skip to content

Commit faa7a83

Browse files
committed
- Renamed 'protos' attribute to 'srcs' to better match convention.
- Renamed implicit target '_pb' to '.pb' for better aesthetics. - Cleanup print statements. - Implement cc_srcs, go_srcs, java_srcs macro attributes to clarify intent with library rules. - Implement cc_deps, go_deps, java_deps macro attributes to clarify intent with library rules. - *_proto_compile attribute 'deps' now requires 'proto' provider for transitive data. - Build file cleanup. - README cleanup.
1 parent 83f982d commit faa7a83

File tree

19 files changed

+114
-217
lines changed

19 files changed

+114
-217
lines changed

bzl/base/class.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def build_imports(lang, self):
6868
#print("ctx.attr.imports: %s" % type(ctx.attr.imports))
6969
for target in ctx.attr.imports:
7070
for srcfile in target.files:
71-
print("srcfile: %s" % srcfile.path)
71+
#print("srcfile: %s" % srcfile.path)
7272
dstfile = ctx.new_file(srcfile.path)
7373
#dstfile = ctx.new_file(srcfile.path)
7474
# By declaring that the proto action (happening later)
@@ -89,7 +89,7 @@ def build_imports(lang, self):
8989
#self["srcs"] += [dstfile]
9090

9191
#self["go_plugin_options"] = self.get("go_plugin_options", []) + ["M" + srcfile.path + "="]
92-
self["paths"] += [self["outdir"]]
92+
#self["paths"] += [self["outdir"]]
9393

9494
def build_plugin_out(name, key, lang, self):
9595
#print("build_plugin_out(%s, %s)" % (name, key))

bzl/cpp/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ load("@org_pubref_rules_protobuf//bzl:cpp/rules.bzl", "cc_proto_library")
2626
```
2727

2828
Invoke the rule. Pass the set of protobuf source files to the
29-
`protos` attribute.
29+
`srcs` attribute.
3030

3131
```python
3232
cc_proto_library(
3333
name = "protolib",
34-
protos = ["my.proto"],
34+
srcs = ["my.proto"],
3535
with_grpc = True,
3636
)
3737
```

bzl/cpp/rules.bzl

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,29 @@ cc_proto_compile = implement(["cpp"])
66

77
def cc_proto_library(
88
name,
9-
protos,
109
copy_protos_to_genfiles = False,
1110
deps = [],
1211
grpc_plugin = None,
1312
grpc_plugin_options = [],
1413
imports = [],
1514
lang = CPP,
1615
paths = [],
17-
proto_compile = cc_proto_compile,
1816
protobuf_plugin_options = [],
1917
protobuf_plugin = None,
18+
proto_compile = cc_proto_compile,
2019
protoc = EXECUTABLE,
2120
srcs = [],
2221
verbose = 0,
2322
visibility = None,
2423
with_grpc = False,
24+
cc_deps = [],
25+
cc_srcs = [],
2526
**kwargs):
2627

2728
args = {}
28-
args["name"] = name + "_pb"
29+
args["name"] = name + ".pb"
2930
args["copy_protos_to_genfiles"] = copy_protos_to_genfiles
30-
args["deps"] = deps
31+
args["deps"] = [d + ".pb" for d in deps]
3132
args["imports"] = imports
3233
args["gen_" + lang.name] = True
3334
args["gen_grpc_" + lang.name] = with_grpc
@@ -36,22 +37,22 @@ def cc_proto_library(
3637
args["gen_grpc_" + lang.name + "_plugin"] = grpc_plugin
3738
args["paths"] = paths
3839
args["protoc"] = protoc
39-
args["protos"] = protos
40+
args["protos"] = srcs
4041
args["verbose"] = verbose
4142
args["with_grpc"] = with_grpc
4243

4344
proto_compile(**args)
4445

4546
if with_grpc and hasattr(lang, "grpc"):
46-
proto_deps = [str(Label(dep)) for dep in getattr(lang.grpc, "compile_deps", [])]
47+
deps += [str(Label(dep)) for dep in getattr(lang.grpc, "compile_deps", [])]
4748
elif hasattr(lang, "protobuf"):
48-
proto_deps = [str(Label(dep)) for dep in getattr(lang.protobuf, "compile_deps", [])]
49+
deps += [str(Label(dep)) for dep in getattr(lang.protobuf, "compile_deps", [])]
4950

50-
cc_deps = list(set(deps + proto_deps))
51+
deps = list(set(deps + cc_deps))
5152

5253
native.cc_library(
5354
name = name,
54-
srcs = srcs + [name + "_pb"],
55-
deps = cc_deps,
55+
srcs = cc_srcs + [name + ".pb"],
56+
deps = deps,
5657
**kwargs
5758
)

bzl/go/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ load("@org_pubref_rules_protobuf//bzl:go/rules.bzl", "go_proto_library")
2424
```
2525

2626
Invoke the rule. Pass the set of protobuf source files to the
27-
`protos` attribute.
27+
`srcs` attribute.
2828

2929
```python
3030
go_proto_library(
3131
name = "protolib",
32-
protos = ["my.proto"],
32+
srcs = ["my.proto"],
3333
with_grpc = True,
3434
)
3535
```
@@ -86,7 +86,7 @@ go_prefix("github.com/my_organization_name")
8686
# //go/app_1/BUILD
8787
go_proto_library(
8888
name = "protolib",
89-
protos = ["my.proto"],
89+
srcs = ["my.proto"],
9090
with_grpc = True,
9191
)
9292
```

bzl/go/class.bzl

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,23 @@ def _build_imports(lang, self):
2323
fail("Bazel context is required for build_imports")
2424

2525
go_prefix = ctx.attr.go_prefix.go_prefix
26-
#print("go prefix: %s" % dir(go_prefix))
2726

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))
27+
for dep in ctx.attr.deps:
3228
provider = dep.proto
33-
print("proto provider: %s" % dir(provider))
29+
#print("proto provider: %s" % dir(provider))
3430
proto_packages = provider.transitive_packages
35-
print("proto_packages: %s" % proto_packages)
31+
#print("proto_packages: %s" % proto_packages)
3632
for pkg, srcs in proto_packages.items():
3733
target = pkg.rsplit(':') # [0] == ctx.label.package, [1] == ctx.label.name
38-
print("target: %s" % target)
34+
#print("target: %s" % target)
3935
for srcfile in srcs:
4036
src = srcfile.short_path
4137
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")]
38+
if target[1] != "go_default_library.pb":
39+
# slice off the '.pb' from 'mylib.protos'
40+
dst += "/" + target[1][:-len(".pb")]
4541
self["protobuf_plugin_options"] = self.get("protobuf_plugin_options", []) + ["M%s=%s" % (src, dst)]
4642

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-
6343
CLASS = struct(
6444
parent = BASE,
6545
name = "go",

bzl/go/rules.bzl

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,53 @@ go_proto_compile = implement(["go"])
66

77
def go_proto_library(
88
name,
9-
protos,
109
copy_protos_to_genfiles = True,
1110
deps = [],
1211
grpc_plugin = None,
1312
grpc_plugin_options = [],
1413
imports = [],
1514
lang = GO,
1615
paths = [],
17-
proto_compile = go_proto_compile,
18-
#proto_deps = [],
1916
protobuf_plugin_options = [],
2017
protobuf_plugin = None,
18+
proto_compile = go_proto_compile,
2119
protoc = EXECUTABLE,
2220
srcs = [],
2321
verbose = 0,
2422
visibility = None,
2523
with_grpc = False,
24+
go_deps = [],
25+
go_srcs = [],
2626
**kwargs):
2727

2828
args = {}
29-
args["name"] = name + "_pb"
29+
args["name"] = name + ".pb"
3030
args["copy_protos_to_genfiles"] = copy_protos_to_genfiles
31-
args["deps"] = deps
31+
args["deps"] = [d + ".pb" for d in deps]
3232
args["imports"] = imports
3333
args["gen_" + lang.name] = True
3434
args["gen_grpc_" + lang.name] = with_grpc
3535
args["gen_protobuf_" + lang.name + "_plugin"] = protobuf_plugin
3636
args["gen_" + lang.name + "_plugin_options"] = protobuf_plugin_options
3737
args["gen_grpc_" + lang.name + "_plugin"] = grpc_plugin
3838
args["paths"] = paths
39-
args["proto_deps"] = [d + "_pb" for d in deps]
4039
args["protoc"] = protoc
41-
args["protos"] = protos
40+
args["protos"] = srcs
4241
args["verbose"] = verbose
4342
args["with_grpc"] = with_grpc
4443

4544
proto_compile(**args)
4645

4746
if with_grpc and hasattr(lang, "grpc"):
48-
proto_deps = [str(Label(dep)) for dep in getattr(lang.grpc, "compile_deps", [])]
47+
deps += [str(Label(dep)) for dep in getattr(lang.grpc, "compile_deps", [])]
4948
elif hasattr(lang, "protobuf"):
50-
proto_deps = [str(Label(dep)) for dep in getattr(lang.protobuf, "compile_deps", [])]
51-
52-
go_deps = list(set(deps + proto_deps))
49+
deps += [str(Label(dep)) for dep in getattr(lang.protobuf, "compile_deps", [])]
5350

54-
#print("go_deps %s" % dir(go_deps[0]))
55-
#go_deps = [d for d in go_deps if hasattr(d, "foo")]
51+
deps = list(set(deps + go_deps))
5652

5753
go_library(
5854
name = name,
59-
srcs = srcs + [ name + "_pb"],
60-
deps = go_deps,
55+
srcs = go_srcs + [ name + ".pb"],
56+
deps = deps,
6157
**kwargs
6258
)

bzl/grpc_gateway/rules.bzl

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

2020
result = protoc_genrule(
2121
spec = [lang],
22-
name = name + "_pb_gw",
22+
name = name + ".pb.gw",
2323
protos = protos,
2424
protoc = protoc,
2525
protobuf_plugin = protobuf_plugin,

bzl/java/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ load("@org_pubref_rules_protobuf//bzl:java/rules.bzl", "java_proto_library")
2424
```
2525

2626
Invoke the rule. Pass the set of protobuf source files to the
27-
`protos` attribute.
27+
`srcs` attribute.
2828

2929
```python
3030
java_proto_library(
3131
name = "protolib",
32-
protos = ["my.proto"],
32+
srcs = ["my.proto"],
3333
with_grpc = True,
3434
)
3535
```
@@ -64,8 +64,8 @@ One could also specify all the sources needed in the
6464
```python
6565
java_proto_library(
6666
name = "mylib",
67-
protos = ["my.proto"],
68-
srcs = ['MyApp.java'],
67+
srcs = ["my.proto"],
68+
java_srcs = ['MyApp.java'],
6969
with_grpc = True,
7070
)
7171
```

bzl/java/rules.bzl

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,40 @@ java_proto_compile = implement(["java"])
55

66
def java_proto_library(
77
name,
8-
protos,
9-
lang = JAVA,
10-
proto_compile = java_proto_compile,
11-
srcs = [],
8+
copy_protos_to_genfiles = False,
9+
deps = [],
10+
grpc_plugin = None,
11+
grpc_plugin_options = [],
1212
imports = [],
13-
visibility = None,
14-
testonly = 0,
15-
protoc = EXECUTABLE,
13+
lang = JAVA,
14+
paths = [],
1615
protobuf_plugin_options = [],
1716
protobuf_plugin = None,
18-
grpc_plugin = None,
19-
grpc_plugin_options = [],
17+
proto_compile = java_proto_compile,
18+
protoc = EXECUTABLE,
19+
srcs = [],
2020
verbose = 0,
21+
visibility = None,
2122
with_grpc = False,
22-
deps = [],
23+
java_deps = [],
24+
java_srcs = [],
2325
**kwargs):
2426

2527
args = {}
26-
args["name"] = name + "_pb"
27-
args["deps"] = deps
28+
args["name"] = name + ".pb"
2829
args["copy_protos_to_genfiles"] = False
29-
args["protos"] = protos
30-
args["verbose"] = verbose
30+
args["deps"] = [d + "_pb" for d in deps]
3131
args["imports"] = imports
32-
args["protoc"] = protoc
33-
args["with_grpc"] = with_grpc
3432
args["gen_" + lang.name] = True
3533
args["gen_grpc_" + lang.name] = with_grpc
3634
args["gen_protobuf_" + lang.name + "_plugin"] = protobuf_plugin
3735
args["gen_" + lang.name + "_plugin_options"] = protobuf_plugin_options
3836
args["gen_grpc_" + lang.name + "_plugin"] = grpc_plugin
37+
args["paths"] = paths
38+
args["protoc"] = protoc
39+
args["protos"] = srcs
40+
args["verbose"] = verbose
41+
args["with_grpc"] = with_grpc
3942

4043
proto_compile(**args)
4144

@@ -44,13 +47,11 @@ def java_proto_library(
4447
elif hasattr(lang, "protobuf"):
4548
deps += [str(Label(dep)) for dep in getattr(lang.protobuf, "compile_deps", [])]
4649

47-
#print("java_proto_library compile deps: %s" % deps)
48-
49-
deps = list(set(deps))
50+
deps = list(set(deps + java_deps))
5051

5152
native.java_library(
5253
name = name,
53-
srcs = srcs + [name + "_pb.srcjar"],
54+
srcs = java_srcs + [name + ".pb.srcjar"],
5455
deps = deps,
5556
**kwargs
5657
)

bzl/javanano/rules.bzl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ load("//bzl:java/rules.bzl", "java_proto_library")
44

55
android_proto_compile = implement(["javanano"])
66

7-
def android_proto_library(name, protos, **kwargs):
7+
def android_proto_library(name, **kwargs):
88
java_proto_library(name,
9-
protos,
109
lang=JAVANANO,
1110
proto_compile = android_proto_compile,
1211
**kwargs)

0 commit comments

Comments
 (0)