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] |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 17 | else: |
Makarand Dharmapurikar | c872a99 | 2017-04-17 16:36:00 -0700 | [diff] [blame] | 18 | outs += [proto.path[label_len:-len(".proto")] + ".pb.h" for proto in protos] |
| 19 | 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] | 20 | out_files = [ctx.new_file(out) for out in outs] |
Makarand Dharmapurikar | c872a99 | 2017-04-17 16:36:00 -0700 | [diff] [blame] | 21 | dir_out = str(ctx.genfiles_dir.path) |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 22 | |
| 23 | arguments = [] |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 24 | if ctx.executable.plugin: |
| 25 | arguments += ["--plugin=protoc-gen-PLUGIN=" + ctx.executable.plugin.path] |
| 26 | arguments += ["--PLUGIN_out=" + ",".join(ctx.attr.flags) + ":" + dir_out] |
Nicolas "Pixel" Noble | 24263c3 | 2017-01-11 01:03:09 +0100 | [diff] [blame] | 27 | additional_input = [ctx.executable.plugin] |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 28 | else: |
| 29 | arguments += ["--cpp_out=" + ",".join(ctx.attr.flags) + ":" + dir_out] |
Nicolas "Pixel" Noble | 24263c3 | 2017-01-11 01:03:09 +0100 | [diff] [blame] | 30 | additional_input = [] |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 31 | arguments += ["-I{0}={0}".format(include.path) for include in includes] |
| 32 | arguments += [proto.path for proto in protos] |
| 33 | |
Makarand Dharmapurikar | e3e3b2a | 2017-03-07 16:12:56 -0800 | [diff] [blame] | 34 | # create a list of well known proto files if the argument is non-None |
| 35 | well_known_proto_files = [] |
| 36 | if ctx.attr.well_known_protos: |
| 37 | f = ctx.attr.well_known_protos.files.to_list()[0].dirname |
| 38 | if f != "external/submodule_protobuf/src/google/protobuf": |
| 39 | print("Error: Only @submodule_protobuf//:well_known_protos is supported") |
| 40 | else: |
| 41 | # f points to "external/submodule_protobuf/src/google/protobuf" |
| 42 | # add -I argument to protoc so it knows where to look for the proto files. |
| 43 | arguments += ["-I{0}".format(f + "/../..")] |
| 44 | well_known_proto_files = [f for f in ctx.attr.well_known_protos.files] |
| 45 | |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 46 | ctx.action( |
Makarand Dharmapurikar | e3e3b2a | 2017-03-07 16:12:56 -0800 | [diff] [blame] | 47 | inputs = protos + includes + additional_input + well_known_proto_files, |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 48 | outputs = out_files, |
| 49 | executable = ctx.executable._protoc, |
| 50 | arguments = arguments, |
| 51 | ) |
| 52 | |
| 53 | return struct(files=set(out_files)) |
| 54 | |
| 55 | generate_cc = rule( |
| 56 | attrs = { |
| 57 | "srcs": attr.label_list( |
| 58 | mandatory = True, |
| 59 | non_empty = True, |
| 60 | providers = ["proto"], |
| 61 | ), |
| 62 | "plugin": attr.label( |
| 63 | executable = True, |
| 64 | providers = ["files_to_run"], |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 65 | cfg = "host", |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 66 | ), |
| 67 | "flags": attr.string_list( |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 68 | mandatory = False, |
| 69 | allow_empty = True, |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 70 | ), |
Makarand Dharmapurikar | e3e3b2a | 2017-03-07 16:12:56 -0800 | [diff] [blame] | 71 | "well_known_protos" : attr.label( |
| 72 | mandatory = False, |
| 73 | ), |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 74 | "_protoc": attr.label( |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 75 | default = Label("//external:protocol_compiler"), |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 76 | executable = True, |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 77 | cfg = "host", |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 78 | ), |
| 79 | }, |
| 80 | # We generate .h files, so we need to output to genfiles. |
| 81 | output_to_genfiles = True, |
| 82 | implementation = generate_cc_impl, |
| 83 | ) |