blob: f3961f0a4197f5e2bb3e0d7ce1857bd120fe3eaf [file] [log] [blame]
Nicolas "Pixel" Noble4dc64312016-10-20 23:07:37 +02001"""Generates C++ grpc stubs from proto_library rules.
2
3This is an internal rule used by cc_grpc_library, and shouldn't be used
4directly.
5"""
6
7def generate_cc_impl(ctx):
Nicolas "Pixel" Noble799bd5e2016-10-21 01:54:32 +02008 """Implementation of the generate_cc rule."""
Nicolas "Pixel" Noble4dc64312016-10-20 23:07:37 +02009 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" Noble799bd5e2016-10-21 01:54:32 +020012 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" Noble4dc64312016-10-20 23:07:37 +020018 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" Noble799bd5e2016-10-21 01:54:32 +020024 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" Noble24263c32017-01-11 01:03:09 +010027 additional_input = [ctx.executable.plugin]
Nicolas "Pixel" Noble799bd5e2016-10-21 01:54:32 +020028 else:
29 arguments += ["--cpp_out=" + ",".join(ctx.attr.flags) + ":" + dir_out]
Nicolas "Pixel" Noble24263c32017-01-11 01:03:09 +010030 additional_input = []
Nicolas "Pixel" Noble4dc64312016-10-20 23:07:37 +020031 arguments += ["-I{0}={0}".format(include.path) for include in includes]
32 arguments += [proto.path for proto in protos]
33
Makarand Dharmapurikare3e3b2a2017-03-07 16:12:56 -080034 # 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" Noble4dc64312016-10-20 23:07:37 +020046 ctx.action(
Makarand Dharmapurikare3e3b2a2017-03-07 16:12:56 -080047 inputs = protos + includes + additional_input + well_known_proto_files,
Nicolas "Pixel" Noble4dc64312016-10-20 23:07:37 +020048 outputs = out_files,
49 executable = ctx.executable._protoc,
50 arguments = arguments,
51 )
52
53 return struct(files=set(out_files))
54
55generate_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" Noble799bd5e2016-10-21 01:54:32 +020065 cfg = "host",
Nicolas "Pixel" Noble4dc64312016-10-20 23:07:37 +020066 ),
67 "flags": attr.string_list(
Nicolas "Pixel" Noble799bd5e2016-10-21 01:54:32 +020068 mandatory = False,
69 allow_empty = True,
Nicolas "Pixel" Noble4dc64312016-10-20 23:07:37 +020070 ),
Makarand Dharmapurikare3e3b2a2017-03-07 16:12:56 -080071 "well_known_protos" : attr.label(
72 mandatory = False,
73 ),
Nicolas "Pixel" Noble4dc64312016-10-20 23:07:37 +020074 "_protoc": attr.label(
Nicolas "Pixel" Noble799bd5e2016-10-21 01:54:32 +020075 default = Label("//external:protocol_compiler"),
Nicolas "Pixel" Noble4dc64312016-10-20 23:07:37 +020076 executable = True,
Nicolas "Pixel" Noble799bd5e2016-10-21 01:54:32 +020077 cfg = "host",
Nicolas "Pixel" Noble4dc64312016-10-20 23:07:37 +020078 ),
79 },
80 # We generate .h files, so we need to output to genfiles.
81 output_to_genfiles = True,
82 implementation = generate_cc_impl,
83)