1 # Protobuf Rust runtime packages.
3 load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
4 load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup", "pkg_files", "strip_prefix")
5 load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
6 load("//bazel/toolchains:proto_lang_toolchain.bzl", "proto_lang_toolchain")
7 load("//rust:dist.bzl", "pkg_cross_compiled_binaries")
12 name = "protobuf_internal",
15 "//src/google/protobuf/...",
19 # The current Rust Protobuf runtime for the build. Depending on the value of
20 # `:rust_proto_library_kernel` build setting it forwards to the cpp or upb kernels. This is the
21 # target that users are expected to depend on.
23 # Rust gencode (via `rust_upb_proto_library` and `rust_cc_proto_library`) doesn't depend on this
24 # target, instead, it depends on the kernel-specific libraries directly. The reason is that we need
25 # to be able to use both kernels in the same build. That allows us to have one TAP config for both
26 # kernels, and to run tests using a single Blaze invocation.
28 # WARNING: It's critical that users do not end up using Rust Protobufs with multiple kernels in
29 # their binaries. Currently this is achieved by using bzl visibility on kernel-specific rules.
30 # TODO: Hide the kernel-specific targets once we can restrict a target visibility to a
34 srcs = ["protobuf.rs"],
35 rustc_flags = select({
36 ":use_upb_kernel": ["--cfg=upb_kernel"],
37 "//conditions:default": ["--cfg=cpp_kernel"],
39 visibility = ["//visibility:public"],
41 ":use_upb_kernel": [":protobuf_upb"],
42 "//conditions:default": [":protobuf_cpp"],
46 # This list contains kernel-agnostic logic and simple kernel-specific logic controlled by
47 # `#[cfg(...)]` attributes. That forces us to compile these files twice, once for each kernel. As a
48 # result these files are included in both `:protobuf_upb` and `:protobuf_cpp`.
49 # This is in principle identical to how we compile regular Rust source files twice
50 # (once for production, and once for unit testing).
52 # shared.rs is the root of the crate and has public items re-exported in protobuf.rs for user use.
54 # go/keep-sorted start
71 # All Rust source files; this is used for packaging up a crate.
72 ALL_RUST_SRCS = PROTOBUF_SHARED + [
73 # go/keep-sorted start
76 "gtest_matchers_impl.rs",
83 # The Rust Protobuf runtime using the upb kernel.
85 # `rust_upb_proto_library` implicitly depends on this target. This target cannot depend on
86 # `:rust_proto_library_kernel` build setting; it has to be fully functional under any value of that
89 name = "protobuf_upb",
90 srcs = PROTOBUF_SHARED + ["upb.rs"],
91 crate_root = "shared.rs",
93 "@crate_index//:paste",
99 visibility = [":protobuf_internal"],
107 name = "protobuf_upb_test",
108 crate = ":protobuf_upb",
114 "@crate_index//:googletest",
118 # This provides an identical set of re-exports as `:protobuf` with `:use_upb_kernel` active.
119 # This is only used for tests shared between runtimes.
121 name = "protobuf_upb_export",
123 srcs = ["protobuf.rs"],
124 rustc_flags = ["--cfg=upb_kernel"],
125 visibility = [":protobuf_internal"],
126 deps = [":protobuf_upb"],
129 # The Rust Protobuf runtime using the cpp kernel.
131 # `rust_cpp_proto_library` implicitly depends on this target. This target cannot depend on
132 # `:rust_proto_library_kernel` build setting; it has to be fully functional under any value of that
135 name = "protobuf_cpp",
136 srcs = PROTOBUF_SHARED + ["cpp.rs"],
137 crate_root = "shared.rs",
139 "@crate_index//:paste",
145 visibility = [":protobuf_internal"],
148 "//rust/cpp_kernel:cpp_api",
153 name = "protobuf_cpp_test",
154 crate = ":protobuf_cpp",
160 "@crate_index//:googletest",
164 # This provides an identical set of re-exports as `:protobuf` with `:use_upb_kernel` inactive.
165 # This is only used for tests shared between runtimes.
167 name = "protobuf_cpp_export",
169 srcs = ["protobuf.rs"],
170 rustc_flags = ["--cfg=cpp_kernel"],
171 visibility = [":protobuf_internal"],
172 deps = [":protobuf_cpp"],
176 name = "protobuf_gtest_matchers",
178 srcs = ["gtest_matchers.rs"],
180 ":use_upb_kernel": {"//rust:protobuf_gtest_matchers_upb": "protobuf_gtest_matchers_impl"},
181 "//conditions:default": {"//rust:protobuf_gtest_matchers_cpp": "protobuf_gtest_matchers_impl"},
183 visibility = ["//visibility:public"],
185 ":use_upb_kernel": [":protobuf_gtest_matchers_upb"],
186 "//conditions:default": [":protobuf_gtest_matchers_cpp"],
191 name = "protobuf_gtest_matchers_cpp",
193 srcs = ["gtest_matchers_impl.rs"],
195 "//rust:protobuf_cpp": "protobuf",
197 visibility = [":protobuf_internal"],
200 "@crate_index//:googletest",
205 name = "protobuf_gtest_matchers_upb",
207 srcs = ["gtest_matchers_impl.rs"],
209 "//rust:protobuf_upb": "protobuf",
211 visibility = [":protobuf_internal"],
214 "@crate_index//:googletest",
223 proto_lang_toolchain(
224 name = "proto_rust_upb_toolchain",
225 command_line = "--rust_out=$(OUT)",
226 output_files = "multiple",
227 progress_message = "Generating Rust proto_library %{label}",
228 runtime = ":protobuf_upb",
229 visibility = ["//visibility:public"],
232 proto_lang_toolchain(
233 name = "proto_rust_cpp_toolchain",
234 command_line = "--rust_out=$(OUT)",
235 output_files = "multiple",
236 progress_message = "Generating Rust proto_library %{label}",
237 runtime = ":protobuf_cpp",
238 visibility = ["//visibility:public"],
242 name = "rust_proto_library_allowed_in_different_package",
243 packages = ["//rust/test"], # for unittest proto_libraries
246 # This flag controls what kernel all Rust Protobufs are using in the current build.
248 name = "rust_proto_library_kernel",
249 build_setting_default = "cpp",
257 name = "use_upb_kernel",
259 ":rust_proto_library_kernel": "upb",
264 name = "rust_protobuf_src",
265 srcs = ALL_RUST_SRCS,
266 strip_prefix = strip_prefix.from_root("rust"),
270 name = "rust_protobuf_src_dir",
272 ":rust_protobuf_src",
273 "//rust/upb:rust_protobuf_upb_src",
276 visibility = ["//rust/release_crates:__subpackages__"],
280 name = "amalgamated_upb",
281 srcs = ["//upb:gen_amalgamation"],
282 strip_prefix = strip_prefix.from_root(""),
286 name = "rust_protobuf_libupb_src",
289 "//upb/cmake:upb_cmake_dist",
292 visibility = ["//rust/release_crates:__subpackages__"],