1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
# Protobuf Rust runtime packages.
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup", "pkg_files", "strip_prefix")
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
load("//bazel/toolchains:proto_lang_toolchain.bzl", "proto_lang_toolchain")
licenses(["notice"])
package_group(
name = "protobuf_internal",
packages = [
"//rust/...",
"//src/google/protobuf/...",
],
)
# The current Rust Protobuf runtime for the build. Depending on the value of
# `:rust_proto_library_kernel` build setting it forwards to the cpp or upb kernels. This is the
# target that users are expected to depend on.
#
# Rust gencode (via `rust_upb_proto_library` and `rust_cc_proto_library`) doesn't depend on this
# target, instead, it depends on the kernel-specific libraries directly. The reason is that we need
# to be able to use both kernels in the same build. That allows us to have one TAP config for both
# kernels, and to run tests using a single Blaze invocation.
#
# WARNING: It's critical that users do not end up using Rust Protobufs with multiple kernels in
# their binaries. Currently this is achieved by using bzl visibility on kernel-specific rules.
# TODO: Hide the kernel-specific targets once we can restrict a target visibility to a
# rule.
rust_library(
name = "protobuf",
srcs = ["protobuf.rs"],
rustc_flags = select({
":use_upb_kernel": ["--cfg=upb_kernel"],
"//conditions:default": ["--cfg=cpp_kernel"],
}),
visibility = ["//visibility:public"],
deps = select({
":use_upb_kernel": [":protobuf_upb"],
"//conditions:default": [":protobuf_cpp"],
}),
)
# This list contains kernel-agnostic logic and simple kernel-specific logic controlled by
# `#[cfg(...)]` attributes. That forces us to compile these files twice, once for each kernel. As a
# result these files are included in both `:protobuf_upb` and `:protobuf_cpp`.
# This is in principle identical to how we compile regular Rust source files twice
# (once for production, and once for unit testing).
#
# shared.rs is the root of the crate and has public items re-exported in protobuf.rs for user use.
PROTOBUF_SHARED = [
# go/keep-sorted start
"codegen_traits.rs",
"cord.rs",
"enum.rs",
"internal.rs",
"map.rs",
"optional.rs",
"prelude.rs",
"primitive.rs",
"proxied.rs",
"repeated.rs",
"shared.rs",
"string.rs",
# go/keep-sorted end
]
# All Rust source files; this is used for packaging up a crate.
ALL_RUST_SRCS = PROTOBUF_SHARED + [
# go/keep-sorted start
"cpp.rs",
"gtest_matchers.rs",
"gtest_matchers_impl.rs",
"protobuf.rs",
"upb.rs",
"utf8.rs",
# go/keep-sorted end
]
# The Rust Protobuf runtime using the upb kernel.
#
# `rust_upb_proto_library` implicitly depends on this target. This target cannot depend on
# `:rust_proto_library_kernel` build setting; it has to be fully functional under any value of that
# setting.
rust_library(
name = "protobuf_upb",
srcs = PROTOBUF_SHARED + ["upb.rs"],
crate_root = "shared.rs",
proc_macro_deps = [
"//rust/proto_proc_macro",
"@crate_index//:paste",
],
rustc_flags = [
"--cfg=upb_kernel",
"--cfg=bzl",
],
visibility = [":protobuf_internal"],
deps = [
":utf8",
"//rust/upb",
],
)
rust_test(
name = "protobuf_upb_test",
crate = ":protobuf_upb",
rustc_flags = [
"--cfg=upb_kernel",
"--cfg=bzl",
],
deps = [
"@crate_index//:googletest",
],
)
# This provides an identical set of re-exports as `:protobuf` with `:use_upb_kernel` active.
# This is only used for tests shared between runtimes.
rust_library(
name = "protobuf_upb_export",
testonly = True,
srcs = ["protobuf.rs"],
rustc_flags = ["--cfg=upb_kernel"],
visibility = [":protobuf_internal"],
deps = [":protobuf_upb"],
)
# The Rust Protobuf runtime using the cpp kernel.
#
# `rust_cpp_proto_library` implicitly depends on this target. This target cannot depend on
# `:rust_proto_library_kernel` build setting; it has to be fully functional under any value of that
# setting.
rust_library(
name = "protobuf_cpp",
srcs = PROTOBUF_SHARED + ["cpp.rs"],
crate_root = "shared.rs",
proc_macro_deps = [
"//rust/proto_proc_macro",
"@crate_index//:paste",
],
rustc_flags = [
"--cfg=cpp_kernel",
"--cfg=bzl",
],
visibility = [":protobuf_internal"],
deps = [
":utf8",
"//rust/cpp_kernel:cpp_api",
],
)
rust_test(
name = "protobuf_cpp_test",
crate = ":protobuf_cpp",
rustc_flags = [
"--cfg=cpp_kernel",
"--cfg=bzl",
],
deps = [
"@crate_index//:googletest",
],
)
# This provides an identical set of re-exports as `:protobuf` with `:use_upb_kernel` inactive.
# This is only used for tests shared between runtimes.
rust_library(
name = "protobuf_cpp_export",
testonly = True,
srcs = ["protobuf.rs"],
rustc_flags = ["--cfg=cpp_kernel"],
visibility = [":protobuf_internal"],
deps = [":protobuf_cpp"],
)
rust_library(
name = "protobuf_gtest_matchers",
testonly = True,
srcs = ["gtest_matchers.rs"],
aliases = select({
":use_upb_kernel": {"//rust:protobuf_gtest_matchers_upb": "protobuf_gtest_matchers_impl"},
"//conditions:default": {"//rust:protobuf_gtest_matchers_cpp": "protobuf_gtest_matchers_impl"},
}),
visibility = ["//visibility:public"],
deps = select({
":use_upb_kernel": [":protobuf_gtest_matchers_upb"],
"//conditions:default": [":protobuf_gtest_matchers_cpp"],
}),
)
rust_library(
name = "protobuf_gtest_matchers_cpp",
testonly = True,
srcs = ["gtest_matchers_impl.rs"],
aliases = {
"//rust:protobuf_cpp": "protobuf",
},
visibility = [":protobuf_internal"],
deps = [
":protobuf_cpp",
"@crate_index//:googletest",
],
)
rust_library(
name = "protobuf_gtest_matchers_upb",
testonly = True,
srcs = ["gtest_matchers_impl.rs"],
aliases = {
"//rust:protobuf_upb": "protobuf",
},
visibility = [":protobuf_internal"],
deps = [
":protobuf_upb",
"@crate_index//:googletest",
],
)
rust_library(
name = "utf8",
srcs = ["utf8.rs"],
)
proto_lang_toolchain(
name = "proto_rust_upb_toolchain",
command_line = "--rust_out=$(OUT)",
output_files = "multiple",
progress_message = "Generating Rust proto_library %{label}",
runtime = ":protobuf_upb",
visibility = ["//visibility:public"],
)
proto_lang_toolchain(
name = "proto_rust_cpp_toolchain",
command_line = "--rust_out=$(OUT)",
output_files = "multiple",
progress_message = "Generating Rust proto_library %{label}",
runtime = ":protobuf_cpp",
visibility = ["//visibility:public"],
)
package_group(
name = "rust_proto_library_allowed_in_different_package",
packages = ["//rust/test"], # for unittest proto_libraries
)
# This flag controls what kernel all Rust Protobufs are using in the current build.
string_flag(
name = "rust_proto_library_kernel",
build_setting_default = "cpp",
values = [
"upb",
"cpp",
],
)
config_setting(
name = "use_upb_kernel",
flag_values = {
":rust_proto_library_kernel": "upb",
},
)
pkg_files(
name = "rust_protobuf_src",
srcs = ALL_RUST_SRCS + ["proto_macro.rs"],
strip_prefix = strip_prefix.from_root("rust"),
)
pkg_filegroup(
name = "rust_protobuf_src_dir",
srcs = [
":rust_protobuf_src",
"//rust/upb:rust_protobuf_upb_src",
],
prefix = "src",
visibility = ["//rust/release_crates:__subpackages__"],
)
pkg_files(
name = "amalgamated_upb",
srcs = ["//upb:gen_amalgamation"],
strip_prefix = strip_prefix.from_root(""),
)
pkg_filegroup(
name = "rust_protobuf_libupb_src",
srcs = [
":amalgamated_upb",
"//upb/cmake:upb_cmake_dist",
],
prefix = "libupb",
visibility = ["//rust/release_crates:__subpackages__"],
)
alias(
name = "release",
actual = "//rust/release_crates:crates",
)
|