blob: d49cbe8d7289e8f4355f59f8f794d5dcaa2b303b [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
34 ctx.action(
Nicolas "Pixel" Noble24263c32017-01-11 01:03:09 +010035 inputs = protos + includes + additional_input,
Nicolas "Pixel" Noble4dc64312016-10-20 23:07:37 +020036 outputs = out_files,
37 executable = ctx.executable._protoc,
38 arguments = arguments,
39 )
40
41 return struct(files=set(out_files))
42
43generate_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" Noble799bd5e2016-10-21 01:54:32 +020053 cfg = "host",
Nicolas "Pixel" Noble4dc64312016-10-20 23:07:37 +020054 ),
55 "flags": attr.string_list(
Nicolas "Pixel" Noble799bd5e2016-10-21 01:54:32 +020056 mandatory = False,
57 allow_empty = True,
Nicolas "Pixel" Noble4dc64312016-10-20 23:07:37 +020058 ),
59 "_protoc": attr.label(
Nicolas "Pixel" Noble799bd5e2016-10-21 01:54:32 +020060 default = Label("//external:protocol_compiler"),
Nicolas "Pixel" Noble4dc64312016-10-20 23:07:37 +020061 executable = True,
Nicolas "Pixel" Noble799bd5e2016-10-21 01:54:32 +020062 cfg = "host",
Nicolas "Pixel" Noble4dc64312016-10-20 23:07:37 +020063 ),
64 },
65 # We generate .h files, so we need to output to genfiles.
66 output_to_genfiles = True,
67 implementation = generate_cc_impl,
68)