Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ commands:
name: Run Rust sample
command: |
cargo run -p sample
- run:
name: Run glean-sym sample
command: |
cd samples/glean-sym-test
python3 -m venv ${PWD}/venv
venv/bin/pip install "git+ssh://git@github.com/mozilla/glean_parser@push-lskqopuplvts#egg=glean-parser"
Copy link
Copy Markdown
Member

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

export GLEAN_PYTHON_VENV_DIR=${PWD}/venv
make run

install-rustup:
steps:
Expand Down
95 changes: 66 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ members = [
"samples/rust",
"samples/rapid-metrics",
"tools/embedded-uniffi-bindgen",
"tools/glean-sym-parser",
"glean-core/glean-sym",
]

default-members = [
Expand Down
7 changes: 4 additions & 3 deletions deny.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[licenses]
allow = [
"MPL-2.0",
"Apache-2.0",
"MIT",
"BSD-2-Clause",
"Zlib",
"ISC",
"MIT",
"MPL-2.0",
"Unicode-3.0",
"Zlib",
]

[bans]
Expand Down
19 changes: 19 additions & 0 deletions glean-core/glean-sym/Cargo.toml
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"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this being useed anywhere

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" }
110 changes: 110 additions & 0 deletions glean-core/glean-sym/src/lib.rs
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) } {
Comment thread
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();
Loading
Loading