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 |
Ian Sturdy | ec27b28 | 2018-03-07 11:27:59 -0800 | [diff] [blame] | 13 | label_len = 0 |
| 14 | # proto_root is the directory relative to which generated include paths should be |
| 15 | proto_root = "" |
| 16 | if ctx.label.package: |
| 17 | # The +1 is for the trailing slash. |
| 18 | label_len += len(ctx.label.package) + 1 |
| 19 | if ctx.label.workspace_root: |
| 20 | label_len += len(ctx.label.workspace_root) + 1 |
| 21 | proto_root = "/" + ctx.label.workspace_root |
| 22 | |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 23 | if ctx.executable.plugin: |
Makarand Dharmapurikar | c872a99 | 2017-04-17 16:36:00 -0700 | [diff] [blame] | 24 | outs += [proto.path[label_len:-len(".proto")] + ".grpc.pb.h" for proto in protos] |
| 25 | outs += [proto.path[label_len:-len(".proto")] + ".grpc.pb.cc" for proto in protos] |
yang-g | 502ba74 | 2018-03-21 14:46:44 -0700 | [diff] [blame] | 26 | if ctx.attr.generate_mocks: |
Makarand Dharmapurikar | 2807951 | 2017-04-25 09:39:33 -0700 | [diff] [blame] | 27 | 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] | 28 | else: |
Makarand Dharmapurikar | c872a99 | 2017-04-17 16:36:00 -0700 | [diff] [blame] | 29 | outs += [proto.path[label_len:-len(".proto")] + ".pb.h" for proto in protos] |
| 30 | 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] | 31 | out_files = [ctx.new_file(out) for out in outs] |
Ian Sturdy | ec27b28 | 2018-03-07 11:27:59 -0800 | [diff] [blame] | 32 | dir_out = str(ctx.genfiles_dir.path + proto_root) |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 33 | |
| 34 | arguments = [] |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 35 | if ctx.executable.plugin: |
| 36 | arguments += ["--plugin=protoc-gen-PLUGIN=" + ctx.executable.plugin.path] |
Mahak Mukhi | 459da91 | 2017-04-16 20:44:04 -0700 | [diff] [blame] | 37 | flags = list(ctx.attr.flags) |
yang-g | 502ba74 | 2018-03-21 14:46:44 -0700 | [diff] [blame] | 38 | if ctx.attr.generate_mocks: |
Mahak Mukhi | 459da91 | 2017-04-16 20:44:04 -0700 | [diff] [blame] | 39 | flags.append("generate_mock_code=true") |
| 40 | arguments += ["--PLUGIN_out=" + ",".join(flags) + ":" + dir_out] |
Nicolas "Pixel" Noble | 24263c3 | 2017-01-11 01:03:09 +0100 | [diff] [blame] | 41 | additional_input = [ctx.executable.plugin] |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 42 | else: |
| 43 | arguments += ["--cpp_out=" + ",".join(ctx.attr.flags) + ":" + dir_out] |
Nicolas "Pixel" Noble | 24263c3 | 2017-01-11 01:03:09 +0100 | [diff] [blame] | 44 | additional_input = [] |
Ian Sturdy | e6dfa9c | 2018-03-19 16:12:45 -0700 | [diff] [blame] | 45 | |
| 46 | # Import protos relative to their workspace root so that protoc prints the |
| 47 | # right include paths. |
| 48 | for include in includes: |
| 49 | directory = include.path |
| 50 | if directory.startswith("external"): |
| 51 | external_sep = directory.find("/") |
| 52 | repository_sep = directory.find("/", external_sep + 1) |
| 53 | arguments += ["--proto_path=" + directory[:repository_sep]] |
| 54 | else: |
| 55 | arguments += ["--proto_path=."] |
| 56 | # Include the output directory so that protoc puts the generated code in the |
| 57 | # right directory. |
Ian Sturdy | ec27b28 | 2018-03-07 11:27:59 -0800 | [diff] [blame] | 58 | arguments += ["--proto_path={0}{1}".format(dir_out, proto_root)] |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 59 | arguments += [proto.path for proto in protos] |
| 60 | |
Makarand Dharmapurikar | e3e3b2a | 2017-03-07 16:12:56 -0800 | [diff] [blame] | 61 | # create a list of well known proto files if the argument is non-None |
| 62 | well_known_proto_files = [] |
| 63 | if ctx.attr.well_known_protos: |
| 64 | f = ctx.attr.well_known_protos.files.to_list()[0].dirname |
Vijay Pai | f74eaa6 | 2017-04-14 17:19:52 -0700 | [diff] [blame] | 65 | if f != "external/com_google_protobuf/src/google/protobuf": |
| 66 | print("Error: Only @com_google_protobuf//:well_known_protos is supported") |
Makarand Dharmapurikar | e3e3b2a | 2017-03-07 16:12:56 -0800 | [diff] [blame] | 67 | else: |
Vijay Pai | f74eaa6 | 2017-04-14 17:19:52 -0700 | [diff] [blame] | 68 | # f points to "external/com_google_protobuf/src/google/protobuf" |
Makarand Dharmapurikar | e3e3b2a | 2017-03-07 16:12:56 -0800 | [diff] [blame] | 69 | # add -I argument to protoc so it knows where to look for the proto files. |
| 70 | arguments += ["-I{0}".format(f + "/../..")] |
| 71 | well_known_proto_files = [f for f in ctx.attr.well_known_protos.files] |
| 72 | |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 73 | ctx.action( |
Makarand Dharmapurikar | e3e3b2a | 2017-03-07 16:12:56 -0800 | [diff] [blame] | 74 | inputs = protos + includes + additional_input + well_known_proto_files, |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 75 | outputs = out_files, |
| 76 | executable = ctx.executable._protoc, |
| 77 | arguments = arguments, |
| 78 | ) |
| 79 | |
Vladimir Moskva | 759c702 | 2017-09-14 15:40:27 +0200 | [diff] [blame] | 80 | return struct(files=depset(out_files)) |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 81 | |
Craig Tiller | a753371 | 2017-05-16 13:09:33 -0700 | [diff] [blame] | 82 | _generate_cc = rule( |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 83 | attrs = { |
| 84 | "srcs": attr.label_list( |
| 85 | mandatory = True, |
| 86 | non_empty = True, |
| 87 | providers = ["proto"], |
| 88 | ), |
| 89 | "plugin": attr.label( |
| 90 | executable = True, |
| 91 | providers = ["files_to_run"], |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 92 | cfg = "host", |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 93 | ), |
| 94 | "flags": attr.string_list( |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 95 | mandatory = False, |
| 96 | allow_empty = True, |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 97 | ), |
Makarand Dharmapurikar | e3e3b2a | 2017-03-07 16:12:56 -0800 | [diff] [blame] | 98 | "well_known_protos" : attr.label( |
| 99 | mandatory = False, |
| 100 | ), |
yang-g | 502ba74 | 2018-03-21 14:46:44 -0700 | [diff] [blame] | 101 | "generate_mocks" : attr.bool( |
Mahak Mukhi | 443a75d | 2017-04-14 15:33:55 -0700 | [diff] [blame] | 102 | default = False, |
| 103 | mandatory = False, |
| 104 | ), |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 105 | "_protoc": attr.label( |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 106 | default = Label("//external:protocol_compiler"), |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 107 | executable = True, |
Nicolas "Pixel" Noble | 799bd5e | 2016-10-21 01:54:32 +0200 | [diff] [blame] | 108 | cfg = "host", |
Nicolas "Pixel" Noble | 4dc6431 | 2016-10-20 23:07:37 +0200 | [diff] [blame] | 109 | ), |
| 110 | }, |
| 111 | # We generate .h files, so we need to output to genfiles. |
| 112 | output_to_genfiles = True, |
| 113 | implementation = generate_cc_impl, |
| 114 | ) |
Craig Tiller | a753371 | 2017-05-16 13:09:33 -0700 | [diff] [blame] | 115 | |
| 116 | def generate_cc(well_known_protos, **kwargs): |
| 117 | if well_known_protos: |
| 118 | _generate_cc(well_known_protos="@com_google_protobuf//:well_known_protos", **kwargs) |
| 119 | else: |
| 120 | _generate_cc(**kwargs) |