Skip to content

Commit c792e52

Browse files
elmarcosdroege
authored andcommitted
glib-macros: make proc-macro-crate optional
The "proc-macro-crate" brings many dependencies (toml_edit, indexmap, hashbrow, winnow...) and is often needless. This helps bring glib-rs to projects like QEMU that try to use a minimal set of dependencies, and package dependencies manually with meson wraps. Signed-off-by: Marc-André Lureau <[email protected]>
1 parent 93fefd4 commit c792e52

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ pango = { path = "pango", version = "0.22" }
4848
pangocairo-sys = { path = "pangocairo/sys", version = "0.22" }
4949
cairo-sys-rs = { path = "cairo/sys", version = "0.22" }
5050
cairo-rs = { path = "cairo", version = "0.22" }
51-
glib-macros = { path = "glib-macros", version = "0.22" }
51+
glib-macros = { path = "glib-macros", version = "0.22", default-features = false }
5252
gdk-pixbuf-sys = { path = "gdk-pixbuf/sys", version = "0.22" }
5353
graphene-sys = { path = "graphene/sys", version = "0.22" }

glib-macros/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ repository.workspace = true
1212
rust-version.workspace = true
1313
version.workspace = true
1414

15+
[features]
16+
default = ["proc_macro_crate"]
17+
proc_macro_crate = ["proc-macro-crate"]
18+
1519
[dependencies]
1620
heck = "0.5"
1721
proc-macro2 = "1.0"
1822
quote = "1.0"
1923
syn = { version = "2.0.106", features = ["full"] }
20-
proc-macro-crate = "3.3"
24+
proc-macro-crate = { version = "3.3", optional = true }
2125

2226
[lib]
2327
proc-macro = true

glib-macros/src/utils.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

33
use proc_macro2::{Ident, Span, TokenStream};
4-
use proc_macro_crate::crate_name;
54
use quote::{quote, quote_spanned, ToTokens};
65
use syn::{
76
meta::ParseNestedMeta, parse::Parse, punctuated::Punctuated, spanned::Spanned, token::Comma,
@@ -185,8 +184,9 @@ pub fn parse_optional_nested_meta_items<'a>(
185184
}
186185
}
187186

187+
#[cfg(feature = "proc_macro_crate")]
188188
pub fn crate_ident_new() -> TokenStream {
189-
use proc_macro_crate::FoundCrate;
189+
use proc_macro_crate::{crate_name, FoundCrate};
190190

191191
match crate_name("glib") {
192192
Ok(FoundCrate::Name(name)) => Some(name),
@@ -214,6 +214,12 @@ pub fn crate_ident_new() -> TokenStream {
214214
})
215215
}
216216

217+
#[cfg(not(feature = "proc_macro_crate"))]
218+
pub fn crate_ident_new() -> TokenStream {
219+
let glib = Ident::new("glib", Span::call_site());
220+
quote!(#glib)
221+
}
222+
217223
// Generate i32 to enum mapping, used to implement
218224
// glib::translate::TryFromGlib<i32>, such as:
219225
//

glib/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ futures-channel = "0.3"
2626
futures-util = "0.3"
2727
glib-sys.workspace = true
2828
gobject-sys.workspace = true
29-
glib-macros.workspace = true
29+
glib-macros = { workspace = true, default-features = false }
3030
rs-log = { package = "log", version = "0.4", optional = true }
3131
smallvec = { version = "1.15", features = ["union", "const_generics", "const_new"] }
3232
gio-sys = { workspace = true, optional = true }

glib/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
//! [`translate`]: mod@translate
2727
#![doc = include_str!("../README.md")]
2828

29+
// for macros
30+
extern crate self as glib;
31+
2932
pub use bitflags;
3033
#[doc(hidden)]
3134
pub use glib_macros::cstr_bytes;

0 commit comments

Comments
 (0)