From 723a85f7975634396af1654d5ee09742316d9eec Mon Sep 17 00:00:00 2001 From: Yannic Bonenberger Date: Sat, 15 Feb 2020 13:26:56 +0100 Subject: [PATCH] [bazel] Remove bootstrap hack from cc_proto_library and add interop with 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. --- BUILD | 15 +++++++++------ protobuf.bzl | 49 ++++++++++++++++++++++++------------------------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/BUILD b/BUILD index f7cf3e9dd2..a462935cac 100644 --- a/BUILD +++ b/BUILD @@ -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", @@ -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"], ) diff --git a/protobuf.bzl b/protobuf.bzl index 26e84f5ad8..e2821f5b5e 100644 --- a/protobuf.bzl +++ b/protobuf.bzl @@ -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): @@ -224,6 +225,29 @@ 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 = [], @@ -231,7 +255,6 @@ def cc_proto_library( cc_libs = [], include = None, protoc = "@com_google_protobuf//:protoc", - internal_bootstrap_hack = False, use_grpc_plugin = False, default_runtime = "@com_google_protobuf//:protobuf", **kargs): @@ -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"