Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 1 | """Generates C++ grpc stubs from proto_library rules. |
| 2 | |
| 3 | This is an internal rule used by cc_grpc_library, and shouldn't be used |
| 4 | directly. |
| 5 | """ |
| 6 | |
| 7 | def generate_cc_impl(ctx): |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 8 | """Implementation of the generate_cc rule.""" |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 9 | protos = [f for src in ctx.attr.srcs for f in src.proto.direct_sources] |
| 10 | includes = [f for src in ctx.attr.srcs for f in src.proto.transitive_imports] |
| 11 | outs = [] |
Makarand Dharmapurikar | c872a99 | 2017-04-17 16:36:00 -0700 | [diff] [blame] | 12 | # label_len is length of the path from WORKSPACE root to the location of this build file |
| 13 | label_len = len(ctx.label.package) + 1 |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 14 | if ctx.executable.plugin: |
Makarand Dharmapurikar | c872a99 | 2017-04-17 16:36:00 -0700 | [diff] [blame] | 15 | outs += [proto.path[label_len:-len(".proto")] + ".grpc.pb.h" for proto in protos] |
| 16 | outs += [proto.path[label_len:-len(".proto")] + ".grpc.pb.cc" for proto in protos] |
Mahak Mukhi | 78f12e5 | 2017-04-16 21:24:16 -0700 | [diff] [blame] | 17 | if ctx.attr.generate_mock: |
Makarand Dharmapurikar | 2807951 | 2017-04-25 09:39:33 -0700 | [diff] [blame^] | 18 | outs += [proto.path[label_len:-len(".proto")] + "_mock.grpc.pb.h" for proto in protos] |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 19 | else: |
Makarand Dharmapurikar | c872a99 | 2017-04-17 16:36:00 -0700 | [diff] [blame] | 20 | outs += [proto.path[label_len:-len(".proto")] + ".pb.h" for proto in protos] |
| 21 | outs += [proto.path[label_len:-len(".proto")] + ".pb.cc" for proto in protos] |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 22 | out_files = [ctx.new_file(out) for out in outs] |
Makarand Dharmapurikar | c872a99 | 2017-04-17 16:36:00 -0700 | [diff] [blame] | 23 | dir_out = str(ctx.genfiles_dir.path) |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 24 | |
| 25 | arguments = [] |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 26 | if ctx.executable.plugin: |
| 27 | arguments += ["--plugin=protoc-gen-PLUGIN=" + ctx.executable.plugin.path] |
Mahak Mukhi | 459da91 | 2017-04-16 20:44:04 -0700 | [diff] [blame] | 28 | flags = list(ctx.attr.flags) |
Mahak Mukhi | 443a75d | 2017-04-14 15:33:55 -0700 | [diff] [blame] | 29 | if ctx.attr.generate_mock: |
Mahak Mukhi | 459da91 | 2017-04-16 20:44:04 -0700 | [diff] [blame] | 30 | flags.append("generate_mock_code=true") |
| 31 | arguments += ["--PLUGIN_out=" + ",".join(flags) + ":" + dir_out] |
Nicolas "Pixel" Noble | 24263c3 | 2017-01-11 01:03:09 +0100 | [diff] [blame] | 32 | additional_input = [ctx.executable.plugin] |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 33 | else: |
| 34 | arguments += ["--cpp_out=" + ",".join(ctx.attr.flags) + ":" + dir_out] |
Nicolas "Pixel" Noble | 24263c3 | 2017-01-11 01:03:09 +0100 | [diff] [blame] | 35 | additional_input = [] |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 36 | arguments += ["-I{0}={0}".format(include.path) for include in includes] |
| 37 | arguments += [proto.path for proto in protos] |
| 38 | |
Makarand Dharmapurikar | e3e3b2a | 2017-03-07 16:12:56 -0800 | [diff] [blame] | 39 | # create a list of well known proto files if the argument is non-None |
| 40 | well_known_proto_files = [] |
| 41 | if ctx.attr.well_known_protos: |
| 42 | f = ctx.attr.well_known_protos.files.to_list()[0].dirname |
Vijay Pai | f74eaa6 | 2017-04-14 17:19:52 -0700 | [diff] [blame] | 43 | if f != "external/com_google_protobuf/src/google/protobuf": |
| 44 | print("Error: Only @com_google_protobuf//:well_known_protos is supported") |
Makarand Dharmapurikar | e3e3b2a | 2017-03-07 16:12:56 -0800 | [diff] [blame] | 45 | else: |
Vijay Pai | f74eaa6 | 2017-04-14 17:19:52 -0700 | [diff] [blame] | 46 | # f points to "external/com_google_protobuf/src/google/protobuf" |
Makarand Dharmapurikar | e3e3b2a | 2017-03-07 16:12:56 -0800 | [diff] [blame] | 47 | # add -I argument to protoc so it knows where to look for the proto files. |
| 48 | arguments += ["-I{0}".format(f + "/../..")] |
| 49 | well_known_proto_files = [f for f in ctx.attr.well_known_protos.files] |
| 50 | |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 51 | ctx.action( |
Makarand Dharmapurikar | e3e3b2a | 2017-03-07 16:12:56 -0800 | [diff] [blame] | 52 | inputs = protos + includes + additional_input + well_known_proto_files, |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 53 | outputs = out_files, |
| 54 | executable = ctx.executable._protoc, |
| 55 | arguments = arguments, |
| 56 | ) |
| 57 | |
| 58 | return struct(files=set(out_files)) |
| 59 | |
| 60 | generate_cc = rule( |
| 61 | attrs = { |
| 62 | "srcs": attr.label_list( |
| 63 | mandatory = True, |
| 64 | non_empty = True, |
| 65 | providers = ["proto"], |
| 66 | ), |
| 67 | "plugin": attr.label( |
| 68 | executable = True, |
| 69 | providers = ["files_to_run"], |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 70 | cfg = "host", |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 71 | ), |
| 72 | "flags": attr.string_list( |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 73 | mandatory = False, |
| 74 | allow_empty = True, |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 75 | ), |
Makarand Dharmapurikar | e3e3b2a | 2017-03-07 16:12:56 -0800 | [diff] [blame] | 76 | "well_known_protos" : attr.label( |
| 77 | mandatory = False, |
| 78 | ), |
Mahak Mukhi | 443a75d | 2017-04-14 15:33:55 -0700 | [diff] [blame] | 79 | "generate_mock" : attr.bool( |
| 80 | default = False, |
| 81 | mandatory = False, |
| 82 | ), |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 83 | "_protoc": attr.label( |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 84 | default = Label("//external:protocol_compiler"), |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 85 | executable = True, |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 86 | cfg = "host", |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 87 | ), |
| 88 | }, |
| 89 | # We generate .h files, so we need to output to genfiles. |
| 90 | output_to_genfiles = True, |
| 91 | implementation = generate_cc_impl, |
| 92 | ) |