Skip to content

Commit

Permalink
[bazel] Remove bootstrap hack from cc_proto_library and add interop w…
Browse files Browse the repository at this point in the history
…ith proto_library

Bazel had a native `cc_proto_library` for more than 2 years now.
This is the first step towards removing that rule from the Protobuf
repo.
  • Loading branch information
Yannic authored and acozzette committed Mar 4, 2020
1 parent 2e51ad6 commit 723a85f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
15 changes: 9 additions & 6 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ LINK_OPTS = select({

load(
":protobuf.bzl",
"adapt_proto_library",
"cc_proto_library",
"internal_copied_filegroup",
"internal_gen_well_known_protos_java",
Expand Down Expand Up @@ -327,13 +328,15 @@ filegroup(
visibility = ["//visibility:public"],
)

cc_proto_library(
adapt_proto_library(
name = "cc_wkt_protos_genproto",
deps = [proto + "_proto" for proto in WELL_KNOWN_PROTO_MAP.keys()],
visibility = ["//visibility:public"],
)

cc_library(
name = "cc_wkt_protos",
srcs = WELL_KNOWN_PROTOS,
include = "src",
default_runtime = ":protobuf",
internal_bootstrap_hack = 1,
protoc = ":protoc",
deprecation = "Only for backward compatibility. Do not use.",
visibility = ["//visibility:public"],
)

Expand Down
49 changes: 24 additions & 25 deletions protobuf.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@bazel_skylib//lib:versions.bzl", "versions")
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_proto//proto:defs.bzl", "ProtoInfo")
load("@rules_python//python:defs.bzl", "py_library", "py_test")

def _GetPath(ctx, path):
Expand Down Expand Up @@ -224,14 +225,36 @@ Args:
outs: a list of labels of the expected outputs from the protocol compiler.
"""

def _adapt_proto_library_impl(ctx):
deps = [dep[ProtoInfo] for dep in ctx.attr.deps]

srcs = [src for dep in deps for src in dep.direct_sources]
return struct(
proto = struct(
srcs = srcs,
import_flags = ["-I{}".format(path) for dep in deps for path in dep.transitive_proto_path.to_list()],
deps = srcs,
),
)

adapt_proto_library = rule(
implementation = _adapt_proto_library_impl,
attrs = {
"deps": attr.label_list(
mandatory = True,
providers = [ProtoInfo],
),
},
doc = "Adapts `proto_library` from `@rules_proto` to be used with `{cc,py}_proto_library` from this file.",
)

def cc_proto_library(
name,
srcs = [],
deps = [],
cc_libs = [],
include = None,
protoc = "@com_google_protobuf//:protoc",
internal_bootstrap_hack = False,
use_grpc_plugin = False,
default_runtime = "@com_google_protobuf//:protobuf",
**kargs):
Expand All @@ -249,41 +272,17 @@ def cc_proto_library(
cc_library.
include: a string indicating the include path of the .proto files.
protoc: the label of the protocol compiler to generate the sources.
internal_bootstrap_hack: a flag indicate the cc_proto_library is used only
for bootstrapping. When it is set to True, no files will be generated.
The rule will simply be a provider for .proto files, so that other
cc_proto_library can depend on it.
use_grpc_plugin: a flag to indicate whether to call the grpc C++ plugin
when processing the proto files.
default_runtime: the implicitly default runtime which will be depended on by
the generated cc_library target.
**kargs: other keyword arguments that are passed to cc_library.
"""

includes = []
if include != None:
includes = [include]

if internal_bootstrap_hack:
# For pre-checked-in generated files, we add the internal_bootstrap_hack
# which will skip the codegen action.
proto_gen(
name = name + "_genproto",
srcs = srcs,
deps = [s + "_genproto" for s in deps],
includes = includes,
protoc = protoc,
visibility = ["//visibility:public"],
)

# An empty cc_library to make rule dependency consistent.
cc_library(
name = name,
**kargs
)
return

grpc_cpp_plugin = None
if use_grpc_plugin:
grpc_cpp_plugin = "//external:grpc_cpp_plugin"
Expand Down

0 comments on commit 723a85f

Please sign in to comment.