-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break dependency on protobuf.bzl from google/protobuf for C++ (#92)
* Break dependency on protobuf.bzl from google/protobuf for C++ This replaces the use of cc_proto_library / proto_gen from the @com_google_protobuf//:protobuf.bzl. The reason for doing so is that newer versions of google/protobuf have changed cc_proto_library + proto_gen so that they no longer work with pgv_cc_proto_library. pgv_proto_library now takes a proto_library containing the protos instead of a list of proto files. The approach for generating the protoc command line arguments was taken from bazelbuild/rules_go The py_proto_library imported from @com_google_protobuf//:protobuf.bzl is used by Envoy, so we left it in. Signed-off-by: Andrew Keesler <[email protected]> Signed-off-by: Amin Jamali <[email protected]> * Fix nits - change mnemonic to ProtoGenValidateCcGenerate - accept array of proto_library rules as argument Signed-off-by: Sam Smith <[email protected]>
- Loading branch information
1 parent
05ff6bc
commit 8f40688
Showing
7 changed files
with
120 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
def _proto_path(proto): | ||
""" | ||
The proto path is not really a file path | ||
It's the path to the proto that was seen when the descriptor file was generated. | ||
""" | ||
path = proto.path | ||
root = proto.root.path | ||
ws = proto.owner.workspace_root | ||
if path.startswith(root): | ||
path = path[len(root):] | ||
if path.startswith("/"): | ||
path = path[1:] | ||
if path.startswith(ws): | ||
path = path[len(ws):] | ||
if path.startswith("/"): | ||
path = path[1:] | ||
return path | ||
|
||
def _protoc_gen_validate_impl(ctx): | ||
"""Generate protos using protoc-gen-validate plugin""" | ||
protos = [] | ||
for dep in ctx.attr.deps: | ||
protos += [f for f in dep.proto.direct_sources] | ||
|
||
cc_hdrs = [p.basename[:-len(".proto")] + ".pb.validate.h" for p in protos] | ||
cc_hdrs += [p.basename[:-len(".proto")] + ".pb.h" for p in protos] | ||
|
||
cc_srcs = [p.basename[:-len(".proto")] + ".pb.validate.cc" for p in protos] | ||
cc_srcs += [p.basename[:-len(".proto")] + ".pb.cc" for p in protos] | ||
|
||
out_files = [ctx.actions.declare_file(out) for out in cc_hdrs+cc_srcs] | ||
|
||
dir_out = ctx.genfiles_dir.path | ||
if ctx.label.workspace_root: | ||
dir_out += ("/"+ctx.label.workspace_root) | ||
|
||
args = [ | ||
"--cpp_out="+dir_out, | ||
"--plugin=protoc-gen-validate="+ctx.executable._plugin.path, | ||
"--validate_out=lang=cc:"+ dir_out, | ||
] | ||
|
||
tds = depset([], transitive = [dep.proto.transitive_descriptor_sets for dep in ctx.attr.deps]) | ||
descriptor_args = [ds.path for ds in tds] | ||
|
||
if len(descriptor_args) != 0: | ||
args += ["--descriptor_set_in=%s" % ":".join(descriptor_args)] | ||
|
||
ctx.action( | ||
inputs=protos + tds.to_list() + [ctx.executable._plugin], | ||
outputs=out_files, | ||
arguments=args + [_proto_path(proto) for proto in protos], | ||
executable=ctx.executable._protoc, | ||
mnemonic="ProtoGenValidateCcGenerate", | ||
use_default_shell_env=True, | ||
) | ||
|
||
return struct( | ||
files=depset(out_files) | ||
) | ||
|
||
cc_proto_gen_validate = rule( | ||
attrs = { | ||
"deps": attr.label_list( | ||
mandatory = True, | ||
providers = ["proto"], | ||
), | ||
|
||
"_protoc": attr.label( | ||
cfg = "host", | ||
default = Label("@com_google_protobuf//:protoc"), | ||
executable = True, | ||
single_file = True, | ||
), | ||
"_plugin": attr.label( | ||
cfg = "host", | ||
default = Label("@com_lyft_protoc_gen_validate//:protoc-gen-validate"), | ||
allow_files = True, | ||
executable = True, | ||
), | ||
}, | ||
output_to_genfiles = True, | ||
implementation = _protoc_gen_validate_impl, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters