Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 1 | """Generates and compiles C++ grpc stubs from proto_library rules.""" |
| 2 | |
| 3 | load("//:bazel/generate_cc.bzl", "generate_cc") |
| 4 | |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 5 | def cc_grpc_library(name, srcs, deps, proto_only, **kwargs): |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 6 | """Generates C++ grpc classes from a .proto file. |
| 7 | |
| 8 | Assumes the generated classes will be used in cc_api_version = 2. |
| 9 | |
| 10 | Arguments: |
| 11 | name: name of rule. |
| 12 | srcs: a single proto_library, which wraps the .proto files with services. |
| 13 | deps: a list of C++ proto_library (or cc_proto_library) which provides |
| 14 | the compiled code of any message that the services depend on. |
| 15 | **kwargs: rest of arguments, e.g., compatible_with and visibility. |
| 16 | """ |
| 17 | if len(srcs) > 1: |
| 18 | fail("Only one srcs value supported", "srcs") |
| 19 | |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 20 | proto_target = "_" + name + "_only" |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 21 | codegen_target = "_" + name + "_codegen" |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 22 | codegen_grpc_target = "_" + name + "_grpc_codegen" |
| 23 | proto_deps = ["_" + dep + "_only" for dep in deps if dep.find(':') == -1] |
| 24 | proto_deps += [dep.split(':')[0] + ':' + "_" + dep.split(':')[1] + "_only" for dep in deps if dep.find(':') != -1] |
| 25 | |
| 26 | native.proto_library( |
| 27 | name = proto_target, |
| 28 | srcs = srcs, |
| 29 | deps = proto_deps, |
| 30 | **kwargs |
| 31 | ) |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 32 | |
| 33 | generate_cc( |
| 34 | name = codegen_target, |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 35 | srcs = [proto_target], |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 36 | **kwargs |
| 37 | ) |
| 38 | |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 39 | if not proto_only: |
| 40 | generate_cc( |
| 41 | name = codegen_grpc_target, |
| 42 | srcs = [proto_target], |
| 43 | plugin = "//:grpc_cpp_plugin", |
| 44 | **kwargs |
| 45 | ) |
| 46 | |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 47 | native.cc_library( |
| 48 | name = name, |
| 49 | srcs = [":" + codegen_grpc_target, ":" + codegen_target], |
| 50 | hdrs = [":" + codegen_grpc_target, ":" + codegen_target], |
| 51 | deps = deps + ["//:grpc++", "//:grpc++_codegen_proto", "//external:protobuf"], |
| 52 | **kwargs |
| 53 | ) |
| 54 | else: |
| 55 | native.cc_library( |
| 56 | name = name, |
| 57 | srcs = [":" + codegen_target], |
| 58 | hdrs = [":" + codegen_target], |
| 59 | deps = deps + ["//external:protobuf"], |
| 60 | **kwargs |
| 61 | ) |