diff --git a/rust/BUILD b/rust/BUILD index 73acbf31ac..d403bb3d5c 100644 --- a/rust/BUILD +++ b/rust/BUILD @@ -77,7 +77,10 @@ rust_library( proc_macro_deps = [ "@crate_index//:paste", ], - rustc_flags = ["--cfg=upb_kernel"], + rustc_flags = [ + "--cfg=upb_kernel", + "--cfg=bzl", + ], visibility = [ "//rust:__subpackages__", "//src/google/protobuf:__subpackages__", @@ -93,6 +96,7 @@ rust_test( crate = ":protobuf_upb", rustc_flags = [ "--cfg=upb_kernel", + "--cfg=bzl", ], deps = [ "@crate_index//:googletest", @@ -136,6 +140,7 @@ rust_test( crate = ":protobuf_cpp", rustc_flags = [ "--cfg=cpp_kernel", + "--cfg=bzl", ], deps = [ "@crate_index//:googletest", diff --git a/rust/cargo/Cargo.toml b/rust/cargo/Cargo.toml new file mode 100644 index 0000000000..2d8e44bb9d --- /dev/null +++ b/rust/cargo/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "protobuf" +version = "4.27.3-beta.0" +edition = "2021" +links = "upb" + +[lib] +path = "src/shared.rs" + +[dependencies] +paste = "1.0.15" + +[dev-dependencies] +googletest = {git = "https://github.com/google/googletest-rust.git" } + +[build-dependencies] +cmake = "0.1.50" + +[lints.rust] +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(bzl)', 'cfg(cpp_kernel)', 'cfg(upb_kernel)'] } \ No newline at end of file diff --git a/rust/cargo/build.rs b/rust/cargo/build.rs new file mode 100644 index 0000000000..e400e15471 --- /dev/null +++ b/rust/cargo/build.rs @@ -0,0 +1,16 @@ +fn main() { + cc::Build::new() + .flag("-std=c99") + // TODO: Come up with a way to enable lto + // .flag("-flto=thin") + .warnings(false) + .include("src") + .file("src/upb.c") + .file("src/utf8_range.c") + .file("src/upb/upb_api.c") + .define("UPB_BUILD_API", Some("1")) + .define("PROTOBUF_CARGO", Some("1")) + .compile("upb"); + let path = std::path::Path::new("src"); + println!("cargo:include={}", path.canonicalize().unwrap().display()) +} diff --git a/rust/proxied.rs b/rust/proxied.rs index be941fa3cd..0267b5fc71 100644 --- a/rust/proxied.rs +++ b/rust/proxied.rs @@ -108,7 +108,7 @@ pub trait AsView { /// /// For example, the call to `.as_view()` in the following snippet /// wouldn't be necessary in concrete code: - /// ``` + /// ```ignore /// fn reborrow<'a, 'b, T>(x: &'b View<'a, T>) -> View<'b, T> /// where 'a: 'b, T: Proxied /// { @@ -136,7 +136,7 @@ pub trait IntoView<'msg>: AsView { /// `into_view` to explicitly perform the operation that in concrete /// code coercion would perform implicitly. /// - /// ``` + /// ```ignore /// fn reborrow_generic_view_into_view<'a, 'b, T>( /// x: View<'a, T>, /// y: View<'b, T>, @@ -182,7 +182,7 @@ pub trait IntoMut<'msg>: AsMut { /// `into_mut` to explicitly perform the operation that in concrete code /// coercion would perform implicitly. /// - /// ``` + /// ```ignore /// fn reborrow_generic_mut_into_mut<'a, 'b, T>(x: Mut<'a, T>, y: Mut<'b, T>) -> [Mut<'b, T>; 2] /// where /// T: Proxied, diff --git a/rust/shared.rs b/rust/shared.rs index 4df25a9bff..80dd3827dd 100644 --- a/rust/shared.rs +++ b/rust/shared.rs @@ -56,10 +56,10 @@ pub mod __internal; /// Everything in `__runtime` is allowed to change without it being considered /// a breaking change for the protobuf library. Nothing in here should be /// exported in `protobuf.rs`. -#[cfg(cpp_kernel)] +#[cfg(all(bzl, cpp_kernel))] #[path = "cpp.rs"] pub mod __runtime; -#[cfg(upb_kernel)] +#[cfg(any(not(bzl), upb_kernel))] #[path = "upb.rs"] pub mod __runtime; @@ -75,6 +75,18 @@ mod proxied; mod repeated; mod string; +#[cfg(not(bzl))] +#[path = "upb/lib.rs"] +mod upb; + +#[cfg(not(bzl))] +mod utf8; + +// Forces the utf8 crate to be accessible from crate::. +#[cfg(bzl)] +#[allow(clippy::single_component_path_imports)] +use utf8; + // If the Upb and C++ kernels are both linked into the same binary, this symbol // will be defined twice and cause a link error. #[no_mangle] diff --git a/rust/string.rs b/rust/string.rs index 748fe9b71e..b25e548392 100644 --- a/rust/string.rs +++ b/rust/string.rs @@ -12,8 +12,8 @@ use crate::__internal::Private; use crate::__runtime::{InnerProtoString, PtrAndLen, RawMessage}; use crate::{ - AsView, IntoProxied, IntoView, Mut, MutProxied, MutProxy, Optional, Proxied, Proxy, View, - ViewProxy, + utf8::Utf8Chunks, AsView, IntoProxied, IntoView, Mut, MutProxied, MutProxy, Optional, Proxied, + Proxy, View, ViewProxy, }; use std::borrow::Cow; use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd}; @@ -26,7 +26,6 @@ use std::ops::{Deref, DerefMut}; use std::ptr; use std::rc::Rc; use std::sync::Arc; -use utf8::Utf8Chunks; pub struct ProtoBytes { pub(crate) inner: InnerProtoString, diff --git a/rust/upb.rs b/rust/upb.rs index 0af32ba382..eda6fc4e27 100644 --- a/rust/upb.rs +++ b/rust/upb.rs @@ -19,7 +19,10 @@ use std::ptr::{self, NonNull}; use std::slice; use std::sync::OnceLock; +#[cfg(bzl)] extern crate upb; +#[cfg(not(bzl))] +use crate::upb; // Temporarily 'pub' since a lot of gencode is directly calling any of the ffi // fns. diff --git a/rust/upb/BUILD b/rust/upb/BUILD index 8fa34f6237..3cd2c5e84b 100644 --- a/rust/upb/BUILD +++ b/rust/upb/BUILD @@ -28,6 +28,7 @@ rust_library( "text.rs", "wire.rs", ], + rustc_flags = ["--cfg=bzl"], visibility = [ "//rust:__subpackages__", "//src/google/protobuf:__subpackages__", diff --git a/rust/upb/lib.rs b/rust/upb/lib.rs index 650baec854..67319afe6d 100644 --- a/rust/upb/lib.rs +++ b/rust/upb/lib.rs @@ -5,6 +5,7 @@ // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd #![deny(unsafe_op_in_unsafe_fn)] +#![cfg_attr(not(bzl), allow(unused_imports))] mod arena; diff --git a/rust/utf8.rs b/rust/utf8.rs index a452e8f916..7a169f0941 100644 --- a/rust/utf8.rs +++ b/rust/utf8.rs @@ -30,7 +30,7 @@ use std::str::from_utf8_unchecked; /// /// # Examples /// -/// ``` +/// ```ignore /// use googletest::prelude::*; /// use utf8::Utf8Chunks; /// @@ -133,7 +133,7 @@ impl fmt::Debug for Debug<'_> { /// This can be used to create functionality similar to /// [`String::from_utf8_lossy`] without allocating heap memory: /// -/// ``` +/// ```ignore /// use utf8::Utf8Chunks; /// /// fn from_utf8_lossy(input: &[u8], mut push: F) where F: FnMut(&str) {