Adding shim for generating C++ protos.

pull/9240/head
Nicolas "Pixel" Noble 8 years ago
parent 4dc64310bb
commit 799bd5efb7
  1. 2
      BUILD
  2. 12
      WORKSPACE
  3. 9
      bazel/BUILD
  4. 45
      bazel/cc_grpc_library.bzl
  5. 27
      bazel/generate_cc.bzl
  6. 11
      bazel/grpc_build_system.bzl
  7. 69
      src/proto/grpc/testing/BUILD
  8. 10
      src/proto/grpc/testing/duplicate/BUILD

@ -35,7 +35,7 @@ exports_files(["LICENSE"])
package(default_visibility = ["//visibility:public"])
load("//:bazel/grpc_build_system.bzl", "grpc_cc_library", "grpc_proto_plugin")
load("//bazel:grpc_build_system.bzl", "grpc_cc_library", "grpc_proto_plugin")
g_stands_for = "good"

@ -3,16 +3,6 @@ bind(
actual = "//third_party/nanopb",
)
bind(
name = "grpc_cpp_plugin",
actual = "//:grpc_cpp_plugin",
)
bind(
name = "grpc++",
actual = "//:grpc++",
)
bind(
name = "libssl",
actual = "@submodule_boringssl//:ssl",
@ -34,7 +24,7 @@ bind(
)
bind(
name = "protobuf_compiler",
name = "protocol_compiler",
actual = "@submodule_protobuf//:protoc",
)

@ -0,0 +1,9 @@
package(default_visibility = ["//:__subpackages__"])
load(":cc_grpc_library.bzl", "cc_grpc_library")
cc_grpc_library(
name = "well_known_protos",
srcs = "@submodule_protobuf//:well_known_protos",
proto_only = True,
)

@ -2,7 +2,7 @@
load("//:bazel/generate_cc.bzl", "generate_cc")
def cc_grpc_library(name, srcs, deps, **kwargs):
def cc_grpc_library(name, srcs, deps, proto_only, **kwargs):
"""Generates C++ grpc classes from a .proto file.
Assumes the generated classes will be used in cc_api_version = 2.
@ -17,19 +17,46 @@ def cc_grpc_library(name, srcs, deps, **kwargs):
if len(srcs) > 1:
fail("Only one srcs value supported", "srcs")
proto_target = "_" + name + "_only"
codegen_target = "_" + name + "_codegen"
codegen_grpc_target = "_" + name + "_grpc_codegen"
proto_deps = ["_" + dep + "_only" for dep in deps if dep.find(':') == -1]
proto_deps += [dep.split(':')[0] + ':' + "_" + dep.split(':')[1] + "_only" for dep in deps if dep.find(':') != -1]
generate_cc(
name = codegen_target,
native.proto_library(
name = proto_target,
srcs = srcs,
plugin = "//external:grpc_cpp_plugin",
deps = proto_deps,
**kwargs
)
native.cc_library(
name = name,
srcs = [":" + codegen_target],
hdrs = [":" + codegen_target],
deps = deps + ["//external:grpc++"],
generate_cc(
name = codegen_target,
srcs = [proto_target],
**kwargs
)
if not proto_only:
generate_cc(
name = codegen_grpc_target,
srcs = [proto_target],
plugin = "//:grpc_cpp_plugin",
**kwargs
)
if not proto_only:
native.cc_library(
name = name,
srcs = [":" + codegen_grpc_target, ":" + codegen_target],
hdrs = [":" + codegen_grpc_target, ":" + codegen_target],
deps = deps + ["//:grpc++", "//:grpc++_codegen_proto", "//external:protobuf"],
**kwargs
)
else:
native.cc_library(
name = name,
srcs = [":" + codegen_target],
hdrs = [":" + codegen_target],
deps = deps + ["//external:protobuf"],
**kwargs
)

@ -5,20 +5,27 @@ directly.
"""
def generate_cc_impl(ctx):
"""Implementation of the gengrpccc rule."""
"""Implementation of the generate_cc rule."""
protos = [f for src in ctx.attr.srcs for f in src.proto.direct_sources]
includes = [f for src in ctx.attr.srcs for f in src.proto.transitive_imports]
outs = []
outs += [proto.basename[:-len(".proto")] + ".grpc.pb.h" for proto in protos]
outs += [proto.basename[:-len(".proto")] + ".grpc.pb.cc" for proto in protos]
if ctx.executable.plugin:
outs += [proto.basename[:-len(".proto")] + ".grpc.pb.h" for proto in protos]
outs += [proto.basename[:-len(".proto")] + ".grpc.pb.cc" for proto in protos]
else:
outs += [proto.basename[:-len(".proto")] + ".pb.h" for proto in protos]
outs += [proto.basename[:-len(".proto")] + ".pb.cc" for proto in protos]
out_files = [ctx.new_file(out) for out in outs]
# The following should be replaced with ctx.configuration.buildout
# whenever this is added to Skylark.
dir_out = out_files[0].dirname[:-len(protos[0].dirname)]
arguments = []
arguments += ["--plugin=protoc-gen-PLUGIN=" + ctx.executable.plugin.path]
arguments += ["--PLUGIN_out=" + ",".join(ctx.attr.flags) + ":" + dir_out]
if ctx.executable.plugin:
arguments += ["--plugin=protoc-gen-PLUGIN=" + ctx.executable.plugin.path]
arguments += ["--PLUGIN_out=" + ",".join(ctx.attr.flags) + ":" + dir_out]
else:
arguments += ["--cpp_out=" + ",".join(ctx.attr.flags) + ":" + dir_out]
arguments += ["-I{0}={0}".format(include.path) for include in includes]
arguments += [proto.path for proto in protos]
@ -41,16 +48,16 @@ generate_cc = rule(
"plugin": attr.label(
executable = True,
providers = ["files_to_run"],
cfg = HOST_CFG,
cfg = "host",
),
"flags": attr.string_list(
mandatory = True,
allow_empty = False
mandatory = False,
allow_empty = True,
),
"_protoc": attr.label(
default = Label("//extern:protocol_compiler"),
default = Label("//external:protocol_compiler"),
executable = True,
cfg = HOST_CFG,
cfg = "host",
),
},
# We generate .h files, so we need to output to genfiles.

@ -55,3 +55,14 @@ def grpc_proto_plugin(name, srcs = [], deps = []):
srcs = srcs,
deps = deps,
)
load("//:bazel/cc_grpc_library.bzl", "cc_grpc_library")
def grpc_proto_library(name, srcs = [], deps = [], well_known_deps = [], has_services = True):
cc_grpc_library(
name = name,
srcs = srcs,
deps = deps,
proto_only = not has_services,
)

@ -0,0 +1,69 @@
package(default_visibility = ["//visibility:public"])
load("//bazel:grpc_build_system.bzl", "grpc_proto_library")
grpc_proto_library(
name = "compiler_test_proto",
srcs = ["compiler_test.proto"],
)
grpc_proto_library(
name = "control_proto",
srcs = ["control.proto"],
deps = ["payloads_proto", "stats_proto"],
)
#grpc_proto_library(
# name = "echo_duplicate_proto",
# srcs = ["duplicate/echo_duplicate.proto"],
# deps = ["echo_messages_proto"],
#)
grpc_proto_library(
name = "echo_messages_proto",
srcs = ["echo_messages.proto"],
)
grpc_proto_library(
name = "echo_proto",
srcs = ["echo.proto"],
deps = ["echo_messages_proto"],
)
grpc_proto_library(
name = "empty_proto",
srcs = ["empty.proto"],
)
grpc_proto_library(
name = "messages_proto",
srcs = ["messages.proto"],
)
grpc_proto_library(
name = "metrics_proto",
srcs = ["metrics.proto"],
)
grpc_proto_library(
name = "payloads_proto",
srcs = ["payloads.proto"],
)
grpc_proto_library(
name = "services_proto",
srcs = ["services.proto"],
deps = ["control_proto", "messages_proto"],
)
grpc_proto_library(
name = "stats_proto",
srcs = ["stats.proto"],
)
grpc_proto_library(
name = "test_proto",
srcs = ["test.proto"],
deps = ["empty_proto", "messages_proto"],
)

@ -0,0 +1,10 @@
package(default_visibility = ["//visibility:public"])
load("//bazel:grpc_build_system.bzl", "grpc_proto_library")
grpc_proto_library(
name = "echo_duplicate_proto",
srcs = ["echo_duplicate.proto"],
deps = ["//src/proto/grpc/testing:echo_messages_proto"],
)
Loading…
Cancel
Save