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 = [] |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 12 | if ctx.executable.plugin: |
| 13 | outs += [proto.basename[:-len(".proto")] + ".grpc.pb.h" for proto in protos] |
| 14 | outs += [proto.basename[:-len(".proto")] + ".grpc.pb.cc" for proto in protos] |
| 15 | else: |
| 16 | outs += [proto.basename[:-len(".proto")] + ".pb.h" for proto in protos] |
| 17 | outs += [proto.basename[:-len(".proto")] + ".pb.cc" for proto in protos] |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 18 | out_files = [ctx.new_file(out) for out in outs] |
| 19 | # The following should be replaced with ctx.configuration.buildout |
| 20 | # whenever this is added to Skylark. |
| 21 | dir_out = out_files[0].dirname[:-len(protos[0].dirname)] |
| 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 | |
| 34 | ctx.action( |
Nicolas "Pixel" Noble | 24263c3 | 2017-01-11 01:03:09 +0100 | [diff] [blame] | 35 | inputs = protos + includes + additional_input, |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 36 | outputs = out_files, |
| 37 | executable = ctx.executable._protoc, |
| 38 | arguments = arguments, |
| 39 | ) |
| 40 | |
| 41 | return struct(files=set(out_files)) |
| 42 | |
| 43 | generate_cc = rule( |
| 44 | attrs = { |
| 45 | "srcs": attr.label_list( |
| 46 | mandatory = True, |
| 47 | non_empty = True, |
| 48 | providers = ["proto"], |
| 49 | ), |
| 50 | "plugin": attr.label( |
| 51 | executable = True, |
| 52 | providers = ["files_to_run"], |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 53 | cfg = "host", |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 54 | ), |
| 55 | "flags": attr.string_list( |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 56 | mandatory = False, |
| 57 | allow_empty = True, |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 58 | ), |
| 59 | "_protoc": attr.label( |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 60 | default = Label("//external:protocol_compiler"), |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 61 | executable = True, |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 62 | cfg = "host", |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 63 | ), |
| 64 | }, |
| 65 | # We generate .h files, so we need to output to genfiles. |
| 66 | output_to_genfiles = True, |
| 67 | implementation = generate_cc_impl, |
| 68 | ) |