aboutsummaryrefslogtreecommitdiffhomepage
path: root/bazel
diff options
context:
space:
mode:
Diffstat (limited to 'bazel')
-rw-r--r--bazel/BUILD9
-rw-r--r--bazel/cc_grpc_library.bzl62
-rw-r--r--bazel/generate_cc.bzl66
-rw-r--r--bazel/grpc_build_system.bzl68
4 files changed, 205 insertions, 0 deletions
diff --git a/bazel/BUILD b/bazel/BUILD
new file mode 100644
index 0000000000..940a379404
--- /dev/null
+++ b/bazel/BUILD
@@ -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,
+)
diff --git a/bazel/cc_grpc_library.bzl b/bazel/cc_grpc_library.bzl
new file mode 100644
index 0000000000..e1dd27b0c3
--- /dev/null
+++ b/bazel/cc_grpc_library.bzl
@@ -0,0 +1,62 @@
+"""Generates and compiles C++ grpc stubs from proto_library rules."""
+
+load("//:bazel/generate_cc.bzl", "generate_cc")
+
+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.
+
+ Arguments:
+ name: name of rule.
+ srcs: a single proto_library, which wraps the .proto files with services.
+ deps: a list of C++ proto_library (or cc_proto_library) which provides
+ the compiled code of any message that the services depend on.
+ **kwargs: rest of arguments, e.g., compatible_with and visibility.
+ """
+ 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]
+
+ native.proto_library(
+ name = proto_target,
+ srcs = srcs,
+ deps = proto_deps,
+ **kwargs
+ )
+
+ 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
+ )
diff --git a/bazel/generate_cc.bzl b/bazel/generate_cc.bzl
new file mode 100644
index 0000000000..3665733681
--- /dev/null
+++ b/bazel/generate_cc.bzl
@@ -0,0 +1,66 @@
+"""Generates C++ grpc stubs from proto_library rules.
+
+This is an internal rule used by cc_grpc_library, and shouldn't be used
+directly.
+"""
+
+def generate_cc_impl(ctx):
+ """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 = []
+ 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 = []
+ 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]
+
+ ctx.action(
+ inputs = protos + includes,
+ outputs = out_files,
+ executable = ctx.executable._protoc,
+ arguments = arguments,
+ )
+
+ return struct(files=set(out_files))
+
+generate_cc = rule(
+ attrs = {
+ "srcs": attr.label_list(
+ mandatory = True,
+ non_empty = True,
+ providers = ["proto"],
+ ),
+ "plugin": attr.label(
+ executable = True,
+ providers = ["files_to_run"],
+ cfg = "host",
+ ),
+ "flags": attr.string_list(
+ mandatory = False,
+ allow_empty = True,
+ ),
+ "_protoc": attr.label(
+ default = Label("//external:protocol_compiler"),
+ executable = True,
+ cfg = "host",
+ ),
+ },
+ # We generate .h files, so we need to output to genfiles.
+ output_to_genfiles = True,
+ implementation = generate_cc_impl,
+)
diff --git a/bazel/grpc_build_system.bzl b/bazel/grpc_build_system.bzl
new file mode 100644
index 0000000000..daf8b78527
--- /dev/null
+++ b/bazel/grpc_build_system.bzl
@@ -0,0 +1,68 @@
+# Copyright 2016, Google Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# * Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following disclaimer
+# in the documentation and/or other materials provided with the
+# distribution.
+# * Neither the name of Google Inc. nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#
+# This is for the gRPC build system. This isn't intended to be used outsite of
+# the BUILD file for gRPC. It contains the mapping for the template system we
+# use to generate other platform's build system files.
+#
+
+def grpc_cc_library(name, srcs = [], public_hdrs = [], hdrs = [], external_deps = [], deps = [], standalone = False, language = "C++"):
+ copts = []
+ if language.upper() == "C":
+ copts = ["-std=c99"]
+ native.cc_library(
+ name = name,
+ srcs = srcs,
+ hdrs = hdrs + public_hdrs,
+ deps = deps + ["//external:" + dep for dep in external_deps],
+ copts = copts,
+ linkopts = ["-pthread"],
+ includes = [
+ "include"
+ ]
+ )
+
+def grpc_proto_plugin(name, srcs = [], deps = []):
+ native.cc_binary(
+ name = name,
+ 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,
+ )
+