-
Notifications
You must be signed in to change notification settings - Fork 169
Bug 2018944 - glean-sym - a Rust API built on top of the Glean UniFFI C FFI #3426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
badboy
wants to merge
6
commits into
main
Choose a base branch
from
glean-sys-generated
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f68d9c4
A parser & compiler to generate the bare minimum to call UniFFI-gener…
badboy 1a1a7a5
A new crate: glean-sym - a Rust API built on top of the Glean UniFFI …
badboy 3d8407c
Update dependencies to avoid duplications
badboy a866734
Dependency vetting
badboy 58c1c90
A test for glean-sym: A Python sample that loads the libraries and in…
badboy 85d08d6
[testing] get glean_parser from an upstream branch for testing
badboy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| [package] | ||
| name = "glean-sym" | ||
| version = "0.1.0" | ||
| authors = ["The Glean Team <glean-team@mozilla.com>"] | ||
| description = "A Rust API on top of the Glean UniFFI C FFI" | ||
| repository = "https://github.com/mozilla/glean" | ||
| license = "MPL-2.0" | ||
| edition = "2024" | ||
|
|
||
| [dependencies] | ||
| bytes = "1.11.1" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see this being |
||
| libloading = "0.9.0" | ||
| once_cell = "1.21.3" | ||
| serde = { version = "1.0.228", features = ["derive"] } | ||
| uniffi = "0.31.0" | ||
|
|
||
| [dev-dependencies] | ||
| xshell = "0.2.7" | ||
| glean-sym-parser = { path = "../../tools/glean-sym-parser" } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
|
||
| use std::sync::LazyLock; | ||
|
|
||
| #[doc(hidden)] | ||
| pub extern crate serde as __serde; | ||
|
|
||
| #[doc(hidden)] | ||
| pub mod __serde_helper { | ||
| /// Deserialize value to a `Vec<T>`. | ||
| /// | ||
| /// If the value was `null` return an empty vector. | ||
| pub fn vec_null<'de, D: serde::Deserializer<'de>, T: serde::Deserialize<'de>>( | ||
| d: D, | ||
| ) -> Result<Vec<T>, D::Error> { | ||
| let res: Option<Vec<T>> = serde::Deserialize::deserialize(d)?; | ||
| Ok(res.unwrap_or_default()) | ||
| } | ||
| } | ||
|
|
||
| // Re-export types that are used by the glean_parser-generated code. | ||
| #[doc(hidden)] | ||
| pub mod __export { | ||
| pub use once_cell::sync::Lazy; | ||
| } | ||
|
|
||
| // Based on https://searchfox.org/firefox-main/rev/6193f8fd1172486e3ce941b8de251b9d1a4dfa0b/toolkit/crashreporter/client/app/src/net/libcurl.rs#84 | ||
| macro_rules! library_binding { | ||
| ( $localname:ident members[$($members:tt)*] load[$($load:tt)*] fn $name:ident $args:tt $( -> $ret:ty )? ; $($rest:tt)* ) => { | ||
| library_binding! { | ||
| $localname | ||
| members[ | ||
| $($members)* | ||
| pub(crate) $name: libloading::Symbol<'static, unsafe extern "C" fn $args $(->$ret)?>, | ||
| ] | ||
| load[ | ||
| $($load)* | ||
| $name: unsafe { | ||
| let symbol = $localname.get::<unsafe extern "C" fn $args $(->$ret)?>(stringify!($name).as_bytes()) | ||
| .map_err(|e| std::io::Error::new(std::io::ErrorKind::NotFound, e))?; | ||
| // All symbols refer to library, so `'static` lifetimes are safe (`library` | ||
| // will outlive them). | ||
| std::mem::transmute::<_, libloading::Symbol<'static, _>>(symbol) | ||
| }, | ||
| ] | ||
| $($rest)* | ||
| } | ||
| }; | ||
| ( $localname:ident members[$($members:tt)*] load[$($load:tt)*] ) => { | ||
| pub struct GleanSym { | ||
| $($members)* | ||
| _library: libloading::Library, | ||
| } | ||
|
|
||
| impl GleanSym { | ||
| #[cfg(unix)] | ||
| pub fn load() -> std::io::Result<Self> { | ||
| let name = libloading::library_filename("xul"); | ||
| let library = match unsafe { libloading::Library::new(name) } { | ||
|
badboy marked this conversation as resolved.
|
||
| Ok(lib) => Some(lib), | ||
| Err(_e) => None, | ||
| }; | ||
| // Try each of the libraries, debug-logging load failures. | ||
| let $localname = library.ok_or_else(|| { | ||
| std::io::Error::new(std::io::ErrorKind::NotFound, "failed to find glean library") | ||
| })?; | ||
| Ok(GleanSym { $($load)* _library: $localname }) | ||
| } | ||
|
|
||
| #[cfg(not(unix))] | ||
| pub fn load() -> std::io::Result<Self> { | ||
| compile_error!("This crate is not implemented for Windows"); | ||
| } | ||
| } | ||
| }; | ||
| ( $($rest:tt)* ) => { | ||
| library_binding! { | ||
| library members[] load[] $($rest)* | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub mod metrics; | ||
| pub mod types; | ||
| mod util; | ||
|
|
||
| static GLEAN: LazyLock<metrics::GleanSym> = LazyLock::new(|| metrics::GleanSym::load().unwrap()); | ||
|
|
||
| // This boilerplate is usually generated by `uniffi::setup_scaffolding!()` | ||
| // along with all the extra FFI functions, like wrappers around Rust buffer allocation, async handling, etc. | ||
| // We don't want to export those, so we do only the minimal part to support what `glean-sym` calls when used in other | ||
| // crates. | ||
|
|
||
| /// The crate-specific tag to implement traits. | ||
| #[doc(hidden)] | ||
| pub struct UniFfiTag; | ||
|
|
||
| /// The namespace glean-sym FFI items are placed in. | ||
| /// Required by UniFFI to match up items. | ||
| const UNIFFI_META_CONST_NAMESPACE_GLEAN_SYM: ::uniffi::MetadataBuffer = | ||
| ::uniffi::MetadataBuffer::from_code(::uniffi::metadata::codes::NAMESPACE) | ||
| .concat_str("glean_sym") | ||
| .concat_str("glean_sym"); | ||
| #[doc(hidden)] | ||
| #[unsafe(no_mangle)] | ||
| pub static UNIFFI_META_NAMESPACE_GLEAN_SYM: [::std::primitive::u8; | ||
| UNIFFI_META_CONST_NAMESPACE_GLEAN_SYM.size] = | ||
| UNIFFI_META_CONST_NAMESPACE_GLEAN_SYM.into_array(); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth filing a bug to update this once it's no longer experimental? I expect this will eventually be pinned to a proper version