-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Make cc_grpc_library compatible with native proto_library and cc_proto_library rules #18955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4269ce0
Make cc_grpc_library compatible with native proto_library and cc_prot…
vam-google f4affee
Merge remote-tracking branch 'upstream/master'
vam-google db7fd70
Fix examples (forgot to rename usages of the renamed target)
vam-google 4c0d9e2
Fix well_known_protos issue
vam-google 5c15471
Fix generated files as input issue (generated files were supported ne…
vam-google 0f7f745
Merge remote-tracking branch 'upstream/master'
vam-google File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,71 +1,105 @@ | ||
| """Generates and compiles C++ grpc stubs from proto_library rules.""" | ||
|
|
||
| load("//bazel:generate_cc.bzl", "generate_cc") | ||
| load("//bazel:protobuf.bzl", "well_known_proto_libs") | ||
|
|
||
| def cc_grpc_library(name, srcs, deps, proto_only, well_known_protos, generate_mocks = False, use_external = False, **kwargs): | ||
| """Generates C++ grpc classes from a .proto file. | ||
| def cc_grpc_library( | ||
| name, | ||
| srcs, | ||
| deps, | ||
| proto_only = False, | ||
| well_known_protos = False, | ||
| generate_mocks = False, | ||
| use_external = False, | ||
| grpc_only = False, | ||
| **kwargs): | ||
| """Generates C++ grpc classes for services defined in a proto file. | ||
|
|
||
| Assumes the generated classes will be used in cc_api_version = 2. | ||
| If grpc_only is True, this rule is compatible with proto_library and | ||
| cc_proto_library native rules such that it expects proto_library target | ||
| as srcs argument and generates only grpc library classes, expecting | ||
| protobuf messages classes library (cc_proto_library target) to be passed in | ||
| deps argument. By default grpc_only is False which makes this rule to behave | ||
| in a backwards-compatible mode (trying to generate both proto and grpc | ||
| classes). | ||
|
|
||
| Arguments: | ||
| name: name of rule. | ||
| srcs: a single proto_library, which wraps the .proto files with services. | ||
| deps: a list of C++ proto_library (or cc_proto_library) which provides | ||
| the compiled code of any message that the services depend on. | ||
| well_known_protos: Should this library additionally depend on well known | ||
| protos | ||
| use_external: When True the grpc deps are prefixed with //external. This | ||
| allows grpc to be used as a dependency in other bazel projects. | ||
| generate_mocks: When True, Google Mock code for client stub is generated. | ||
| **kwargs: rest of arguments, e.g., compatible_with and visibility. | ||
| """ | ||
| if len(srcs) > 1: | ||
| fail("Only one srcs value supported", "srcs") | ||
| Assumes the generated classes will be used in cc_api_version = 2. | ||
|
|
||
| proto_target = "_" + name + "_only" | ||
| codegen_target = "_" + name + "_codegen" | ||
| codegen_grpc_target = "_" + name + "_grpc_codegen" | ||
| proto_deps = ["_" + dep + "_only" for dep in deps if dep.find(':') == -1] | ||
| proto_deps += [dep.split(':')[0] + ':' + "_" + dep.split(':')[1] + "_only" for dep in deps if dep.find(':') != -1] | ||
| Args: | ||
| name (str): Name of rule. | ||
| srcs (list): A single .proto file which contains services definitions, | ||
| or if grpc_only parameter is True, a single proto_library which | ||
| contains services descriptors. | ||
| deps (list): A list of C++ proto_library (or cc_proto_library) which | ||
| provides the compiled code of any message that the services depend on. | ||
| proto_only (bool): If True, create only C++ proto classes library, | ||
| avoid creating C++ grpc classes library (expect it in deps). | ||
| Deprecated, use native cc_proto_library instead. False by default. | ||
| well_known_protos (bool): Should this library additionally depend on | ||
| well known protos. Deprecated, the well known protos should be | ||
| specified as explicit dependencies of the proto_library target | ||
| (passed in srcs parameter) instead. False by default. | ||
| generate_mocks (bool): when True, Google Mock code for client stub is | ||
| generated. False by default. | ||
| use_external (bool): Not used. | ||
| grpc_only (bool): if True, generate only grpc library, expecting | ||
| protobuf messages library (cc_proto_library target) to be passed as | ||
| deps. False by default (will become True by default eventually). | ||
| **kwargs: rest of arguments, e.g., compatible_with and visibility | ||
| """ | ||
| if len(srcs) > 1: | ||
| fail("Only one srcs value supported", "srcs") | ||
| if grpc_only and proto_only: | ||
| fail("A mutualy exclusive configuration is specified: grpc_only = True and proto_only = True") | ||
|
|
||
| native.proto_library( | ||
| name = proto_target, | ||
| srcs = srcs, | ||
| deps = proto_deps, | ||
| **kwargs | ||
| ) | ||
| extra_deps = [] | ||
| proto_targets = [] | ||
|
|
||
| generate_cc( | ||
| name = codegen_target, | ||
| srcs = [proto_target], | ||
| well_known_protos = well_known_protos, | ||
| **kwargs | ||
| ) | ||
| if not grpc_only: | ||
| proto_target = "_" + name + "_only" | ||
| cc_proto_target = name if proto_only else "_" + name + "_cc_proto" | ||
|
|
||
| if not proto_only: | ||
| plugin = "@com_github_grpc_grpc//:grpc_cpp_plugin" | ||
| generate_cc( | ||
| name = codegen_grpc_target, | ||
| srcs = [proto_target], | ||
| plugin = plugin, | ||
| well_known_protos = well_known_protos, | ||
| generate_mocks = generate_mocks, | ||
| **kwargs | ||
| ) | ||
| grpc_deps = ["@com_github_grpc_grpc//:grpc++_codegen_proto", | ||
| "//external:protobuf"] | ||
| native.cc_library( | ||
| name = name, | ||
| srcs = [":" + codegen_grpc_target, ":" + codegen_target], | ||
| hdrs = [":" + codegen_grpc_target, ":" + codegen_target], | ||
| deps = deps + grpc_deps, | ||
| **kwargs | ||
| ) | ||
| else: | ||
| native.cc_library( | ||
| name = name, | ||
| srcs = [":" + codegen_target], | ||
| hdrs = [":" + codegen_target], | ||
| deps = deps + ["//external:protobuf"], | ||
| **kwargs | ||
| ) | ||
| proto_deps = ["_" + dep + "_only" for dep in deps if dep.find(":") == -1] | ||
| proto_deps += [dep.split(":")[0] + ":" + "_" + dep.split(":")[1] + "_only" for dep in deps if dep.find(":") != -1] | ||
| if well_known_protos: | ||
| proto_deps += well_known_proto_libs() | ||
|
|
||
| native.proto_library( | ||
| name = proto_target, | ||
| srcs = srcs, | ||
| deps = proto_deps, | ||
| **kwargs | ||
| ) | ||
|
|
||
| native.cc_proto_library( | ||
| name = cc_proto_target, | ||
| deps = [":" + proto_target], | ||
| **kwargs | ||
| ) | ||
| extra_deps.append(":" + cc_proto_target) | ||
| proto_targets.append(proto_target) | ||
| else: | ||
| if not srcs: | ||
| fail("srcs cannot be empty", "srcs") | ||
| proto_targets += srcs | ||
|
|
||
| if not proto_only: | ||
| codegen_grpc_target = "_" + name + "_grpc_codegen" | ||
| generate_cc( | ||
| name = codegen_grpc_target, | ||
| srcs = proto_targets, | ||
| plugin = "@com_github_grpc_grpc//:grpc_cpp_plugin", | ||
| well_known_protos = well_known_protos, | ||
| generate_mocks = generate_mocks, | ||
| **kwargs | ||
| ) | ||
|
|
||
| native.cc_library( | ||
| name = name, | ||
| srcs = [":" + codegen_grpc_target], | ||
| hdrs = [":" + codegen_grpc_target], | ||
| deps = deps + | ||
| extra_deps + | ||
| ["@com_github_grpc_grpc//:grpc++_codegen_proto"], | ||
| **kwargs | ||
| ) | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.