瀏覽代碼

[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.
Yannic Bonenberger 5 年之前
父節點
當前提交
723a85f797
共有 2 個文件被更改,包括 33 次插入31 次删除
  1. 9 6
      BUILD
  2. 24 25
      protobuf.bzl

+ 9 - 6
BUILD

@@ -151,6 +151,7 @@ LINK_OPTS = select({
 
 
 load(
 load(
     ":protobuf.bzl",
     ":protobuf.bzl",
+    "adapt_proto_library",
     "cc_proto_library",
     "cc_proto_library",
     "internal_copied_filegroup",
     "internal_copied_filegroup",
     "internal_gen_well_known_protos_java",
     "internal_gen_well_known_protos_java",
@@ -327,13 +328,15 @@ filegroup(
     visibility = ["//visibility:public"],
     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",
     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"],
     visibility = ["//visibility:public"],
 )
 )
 
 

+ 24 - 25
protobuf.bzl

@@ -1,5 +1,6 @@
 load("@bazel_skylib//lib:versions.bzl", "versions")
 load("@bazel_skylib//lib:versions.bzl", "versions")
 load("@rules_cc//cc:defs.bzl", "cc_library")
 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")
 load("@rules_python//python:defs.bzl", "py_library", "py_test")
 
 
 def _GetPath(ctx, path):
 def _GetPath(ctx, path):
@@ -224,6 +225,29 @@ Args:
   outs: a list of labels of the expected outputs from the protocol compiler.
   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(
 def cc_proto_library(
         name,
         name,
         srcs = [],
         srcs = [],
@@ -231,7 +255,6 @@ def cc_proto_library(
         cc_libs = [],
         cc_libs = [],
         include = None,
         include = None,
         protoc = "@com_google_protobuf//:protoc",
         protoc = "@com_google_protobuf//:protoc",
-        internal_bootstrap_hack = False,
         use_grpc_plugin = False,
         use_grpc_plugin = False,
         default_runtime = "@com_google_protobuf//:protobuf",
         default_runtime = "@com_google_protobuf//:protobuf",
         **kargs):
         **kargs):
@@ -249,41 +272,17 @@ def cc_proto_library(
           cc_library.
           cc_library.
       include: a string indicating the include path of the .proto files.
       include: a string indicating the include path of the .proto files.
       protoc: the label of the protocol compiler to generate the sources.
       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
       use_grpc_plugin: a flag to indicate whether to call the grpc C++ plugin
           when processing the proto files.
           when processing the proto files.
       default_runtime: the implicitly default runtime which will be depended on by
       default_runtime: the implicitly default runtime which will be depended on by
           the generated cc_library target.
           the generated cc_library target.
       **kargs: other keyword arguments that are passed to cc_library.
       **kargs: other keyword arguments that are passed to cc_library.
-
     """
     """
 
 
     includes = []
     includes = []
     if include != None:
     if include != None:
         includes = [include]
         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
     grpc_cpp_plugin = None
     if use_grpc_plugin:
     if use_grpc_plugin:
         grpc_cpp_plugin = "//external:grpc_cpp_plugin"
         grpc_cpp_plugin = "//external:grpc_cpp_plugin"