Skip to contentSkip to conversation

rust: do not generate GLib bindings

PATCH New Paolo Bonzini 1 participant 3 messages Oct 1, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover rust: do not generate GLib bindings

Don't generate FFI for glib, rely on glib-sys crate. This makes it possible to limit Rust to the same minimum version as C code.

The disadvantage is that each bindings module needs to import from glib explicitly.

The extra lines in Cargo.lock are all build dependencies that are not used by Meson. They are only used for glib-sys's build.rs rules, when running e.g. "cargo check".

Marc-André Lureau (1):
rust: use glib-sys
subprojects: add glib-sys-rs

 meson.build                                   |   1 +
 include/hw/core/cpu.h                         |   2 +-
 rust/Cargo.lock                               | 182 ++++++++++++++++++
 rust/Cargo.toml                               |   1 +
 rust/bql/Cargo.toml                           |   3 +
 rust/bql/meson.build                          |   1 +
 rust/bql/src/bindings.rs                      |   4 +
 rust/chardev/Cargo.toml                       |   1 +
 rust/chardev/meson.build                      |   2 +-
 rust/chardev/src/bindings.rs                  |   4 +
 rust/hw/char/pl011/Cargo.toml                 |   1 +
 rust/hw/char/pl011/meson.build                |   1 +
 rust/hw/char/pl011/src/bindings.rs            |   5 +
 rust/hw/core/Cargo.toml                       |   1 +
 rust/hw/core/meson.build                      |   2 +-
 rust/hw/core/src/bindings.rs                  |   3 +
 rust/meson.build                              |   2 +
 rust/migration/Cargo.toml                     |   1 +
 rust/migration/meson.build                    |   2 +-
 rust/migration/src/bindings.rs                |   1 +
 rust/qom/Cargo.toml                           |   1 +
 rust/qom/meson.build                          |   2 +-
 rust/qom/src/bindings.rs                      |   2 +
 rust/system/Cargo.toml                        |   1 +
 rust/system/meson.build                       |   2 +-
 rust/system/src/bindings.rs                   |   4 +
 rust/util/Cargo.toml                          |   1 +
 rust/util/meson.build                         |   2 +-
 rust/util/src/bindings.rs                     |   2 +
 scripts/archive-source.sh                     |   1 +
 subprojects/.gitignore                        |   1 +
 subprojects/glib-sys-0.21-rs.wrap             |   7 +
 .../packagefiles/glib-sys-0.21-rs/meson.build |  33 ++++
 33 files changed, 272 insertions(+), 7 deletions(-)
 create mode 100644 subprojects/glib-sys-0.21-rs.wrap
 create mode 100644 subprojects/packagefiles/glib-sys-0.21-rs/meson.build
Signature · 1 line
2.51.0
#1 lore raw

PATCH 1/2 subprojects: add glib-sys-rs

From: Marc-André Lureau <[email protected]>

---
 rust/meson.build                              |  2 ++
 scripts/archive-source.sh                     |  1 +
 subprojects/.gitignore                        |  1 +
 subprojects/glib-sys-0.21-rs.wrap             |  7 ++++
 .../packagefiles/glib-sys-0.21-rs/meson.build | 33 +++++++++++++++++++
 5 files changed, 44 insertions(+)
 create mode 100644 subprojects/glib-sys-0.21-rs.wrap
 create mode 100644 subprojects/packagefiles/glib-sys-0.21-rs/meson.build
rust/meson.build +2 -0
index 7fd857215b0..76e10699b37 100644
@@ -2,12 +2,14 @@ subproject('anyhow-1-rs', required: true)
 subproject('bilge-0.2-rs', required: true)
 subproject('bilge-impl-0.2-rs', required: true)
 subproject('foreign-0.3-rs', required: true)
+subproject('glib-sys-0.21-rs', required: true)
 subproject('libc-0.2-rs', required: true)
 
 anyhow_rs = dependency('anyhow-1-rs')
 bilge_rs = dependency('bilge-0.2-rs')
 bilge_impl_rs = dependency('bilge-impl-0.2-rs')
 foreign_rs = dependency('foreign-0.3-rs')
+glib_sys_rs = dependency('glib-sys-0.21-rs')
 libc_rs = dependency('libc-0.2-rs')
 
 subproject('proc-macro2-1-rs', required: true)
scripts/archive-source.sh +1 -0
index a725dd923dc..8f97b19a088 100755
@@ -36,6 +36,7 @@ subprojects=(
   bilge-impl-0.2-rs
   either-1-rs
   foreign-0.3-rs
+  glib-sys-0.21-rs
   itertools-0.11-rs
   keycodemapdb
   libc-0.2-rs
subprojects/.gitignore +1 -0
index 0b5d9634094..c00c8478372 100644
@@ -13,6 +13,7 @@
 /bilge-impl-*
 /either-*
 /foreign-*
+/glib-sys-*
 /itertools-*
 /libc-*
 /proc-macro-error-*
subprojects/glib-sys-0.21-rs.wrap new file +7 -0
new file mode 100644
index 00000000000..313ced731ac
@@ -0,0 +1,7 @@
+[wrap-file]
+directory = glib-sys-0.21.2
+source_url = https://crates.io/api/v1/crates/glib-sys/0.21.2/download
+source_filename = glib-sys-0.21.2.tar.gz
+source_hash = d09d3d0fddf7239521674e57b0465dfbd844632fec54f059f7f56112e3f927e1
+#method = cargo
+patch_directory = glib-sys-0.21-rs
subprojects/packagefiles/glib-sys-0.21-rs/meson.build new file +33 -0
new file mode 100644
index 00000000000..8c5483311ed
@@ -0,0 +1,33 @@
+project('glib-sys-0.21-rs', 'rust',
+  meson_version: '>=1.5.0',
+  version: '0.21.2',
+  license: 'MIT',
+  default_options: [])
+
+subproject('libc-0.2-rs', required: true)
+libc_rs = dependency('libc-0.2-rs')
+
+_glib_sys_rs = static_library(
+  'glib_sys',
+  files('src/lib.rs'),
+  gnu_symbol_visibility: 'hidden',
+  override_options: ['rust_std=2021', 'build.rust_std=2021'],
+  rust_abi: 'rust',
+  rust_args: [
+    '--cap-lints', 'allow',
+    '--cfg', 'feature="v2_66"',
+    '--cfg', 'feature="v2_64"',
+    '--cfg', 'feature="v2_62"',
+    '--cfg', 'feature="v2_60"',
+    '--cfg', 'feature="v2_58"',
+  ],
+  # should also link with glib; don't bother doing it here since all
+  # QEMU targets have it
+  dependencies: [libc_rs],
+)
+
+glib_sys_dep = declare_dependency(
+  link_with: _glib_sys_rs,
+)
+
+meson.override_dependency('glib-sys-0.21-rs', glib_sys_dep)
Signature · 1 line
2.51.0
#2 lore raw

PATCH 2/2 rust: use glib-sys

In reply to Paolo Bonzini

From: Marc-André Lureau <[email protected]>

Don't generate FFI for glib, rely on glib-sys crate.

---
 meson.build                        |   1 +
 include/hw/core/cpu.h              |   2 +-
 rust/Cargo.lock                    | 182 +++++++++++++++++++++++++++++
 rust/Cargo.toml                    |   1 +
 rust/bql/Cargo.toml                |   3 +
 rust/bql/meson.build               |   1 +
 rust/bql/src/bindings.rs           |   4 +
 rust/chardev/Cargo.toml            |   1 +
 rust/chardev/meson.build           |   2 +-
 rust/chardev/src/bindings.rs       |   4 +
 rust/hw/char/pl011/Cargo.toml      |   1 +
 rust/hw/char/pl011/meson.build     |   1 +
 rust/hw/char/pl011/src/bindings.rs |   5 +
 rust/hw/core/Cargo.toml            |   1 +
 rust/hw/core/meson.build           |   2 +-
 rust/hw/core/src/bindings.rs       |   3 +
 rust/migration/Cargo.toml          |   1 +
 rust/migration/meson.build         |   2 +-
 rust/migration/src/bindings.rs     |   1 +
 rust/qom/Cargo.toml                |   1 +
 rust/qom/meson.build               |   2 +-
 rust/qom/src/bindings.rs           |   2 +
 rust/system/Cargo.toml             |   1 +
 rust/system/meson.build            |   2 +-
 rust/system/src/bindings.rs        |   4 +
 rust/util/Cargo.toml               |   1 +
 rust/util/meson.build              |   2 +-
 rust/util/src/bindings.rs          |   2 +
 28 files changed, 228 insertions(+), 7 deletions(-)
meson.build +1 -0
index 2eebb96326a..0eb4f850582 100644
@@ -4239,6 +4239,7 @@ if have_rust
     '--no-prepend-enum-name',
     '--allowlist-file', meson.project_source_root() + '/include/.*',
     '--allowlist-file', meson.project_build_root() + '/.*',
+    '--blocklist-file', glib_pc.get_variable('includedir') + '/glib-2.0/.*',
     ]
   if not rustfmt.found()
     if bindgen.version().version_compare('<0.65.0')
include/hw/core/cpu.h +1 -1
index c9f40c25392..4196293ba1c 100644
@@ -169,7 +169,7 @@ struct CPUClass {
     vaddr (*gdb_adjust_breakpoint)(CPUState *cpu, vaddr addr);
 
     const char *gdb_core_xml_file;
-    const gchar * (*gdb_arch_name)(CPUState *cpu);
+    const char * (*gdb_arch_name)(CPUState *cpu);
     const char * (*gdb_get_core_xml_file)(CPUState *cpu);
 
     void (*disas_set_info)(CPUState *cpu, disassemble_info *info);
rust/Cargo.lock +182 -0

279 lines not on this page · raw message

rust/Cargo.toml +1 -0
index f372d7dbf70..783e626802c 100644
@@ -29,6 +29,7 @@ authors = ["The QEMU Project Developers <[email protected]>"]
 anyhow = "~1.0"
 foreign = "~0.3.1"
 libc = "0.2.162"
+glib-sys = { version = "0.21.2", features = ["v2_66"] }
 
 [workspace.lints.rust]
 unexpected_cfgs = { level = "deny", check-cfg = ['cfg(MESON)'] }
rust/bql/Cargo.toml +3 -0
index d87edf4c302..8fd81311028 100644
@@ -12,6 +12,9 @@ license.workspace = true
 repository.workspace = true
 rust-version.workspace = true
 
+[dependencies]
+glib-sys.workspace = true
+
 [features]
 default = ["debug_cell"]
 debug_cell = []
rust/bql/meson.build +1 -0
index fedb94da9fd..091372dd7b6 100644
@@ -37,6 +37,7 @@ _bql_rs = static_library(
   override_options: ['rust_std=2021', 'build.rust_std=2021'],
   rust_abi: 'rust',
   rust_args: _bql_cfg,
+  dependencies: [glib_sys_rs],
 )
 
 bql_rs = declare_dependency(link_with: [_bql_rs],
rust/bql/src/bindings.rs +4 -0
index 9ffff12cded..8c70f3a87ce 100644
@@ -18,6 +18,10 @@
     clippy::too_many_arguments
 )]
 
+use glib_sys::{
+    guint, GArray, GHashTable, GHashTableIter, GList, GPollFD, GPtrArray, GQueue, GSList, GSource,
+};
+
 #[cfg(MESON)]
 include!("bindings.inc.rs");
 
rust/chardev/Cargo.toml +1 -0
index 3e77972546e..f105189dccb 100644
@@ -13,6 +13,7 @@ repository.workspace = true
 rust-version.workspace = true
 
 [dependencies]
+glib-sys = { workspace = true }
 common = { path = "../common" }
 bql = { path = "../bql" }
 migration = { path = "../migration" }
rust/chardev/meson.build +1 -1
index e7ce02b3bc2..d365d8dd0f4 100644
@@ -36,7 +36,7 @@ _chardev_rs = static_library(
   override_options: ['rust_std=2021', 'build.rust_std=2021'],
   rust_abi: 'rust',
   link_with: [_bql_rs, _migration_rs, _qom_rs, _util_rs],
-  dependencies: [common_rs, qemu_macros],
+  dependencies: [glib_sys_rs, common_rs, qemu_macros],
 )
 
 chardev_rs = declare_dependency(link_with: [_chardev_rs], dependencies: [chardev, qemuutil])
rust/chardev/src/bindings.rs +4 -0
index 2d98026d627..c95dc89c56d 100644
@@ -19,6 +19,10 @@
 )]
 
 use common::Zeroable;
+use glib_sys::{
+    gboolean, guint, GArray, GHashTable, GHashTableIter, GIOCondition, GList, GMainContext,
+    GPollFD, GPtrArray, GQueue, GSList, GSource, GSourceFunc,
+};
 
 #[cfg(MESON)]
 include!("bindings.inc.rs");
rust/hw/char/pl011/Cargo.toml +1 -0
index dc41d0e499e..5b319455ee3 100644
@@ -13,6 +13,7 @@ repository.workspace = true
 rust-version.workspace = true
 
 [dependencies]
+glib-sys.workspace = true
 bilge = { version = "0.2.0" }
 bilge-impl = { version = "0.2.0" }
 bits = { path = "../../../bits" }
rust/hw/char/pl011/meson.build +1 -0
index 07b3da17e83..33b91f21911 100644
@@ -33,6 +33,7 @@ _libpl011_rs = static_library(
     bilge_impl_rs,
     bits_rs,
     common_rs,
+    glib_sys_rs,
     util_rs,
     migration_rs,
     bql_rs,
rust/hw/char/pl011/src/bindings.rs +5 -0
index bd5ea840cb2..52a76d0de5c 100644
@@ -20,6 +20,11 @@
 
 //! `bindgen`-generated declarations.
 
+use glib_sys::{
+    gboolean, guint, GArray, GByteArray, GHashTable, GHashTableIter, GIOCondition, GList,
+    GMainContext, GPollFD, GPtrArray, GQueue, GSList, GSource, GSourceFunc, GString,
+};
+
 #[cfg(MESON)]
 include!("bindings.inc.rs");
 
rust/hw/core/Cargo.toml +1 -0
index 9a9aa517082..ecfb5647184 100644
@@ -13,6 +13,7 @@ repository.workspace = true
 rust-version.workspace = true
 
 [dependencies]
+glib-sys.workspace = true
 qemu_macros = { path = "../../qemu-macros" }
 common = { path = "../../common" }
 bql = { path = "../../bql" }
rust/hw/core/meson.build +1 -1
index e1ae95ed61e..1560dd20c6b 100644
@@ -59,7 +59,7 @@ _hwcore_rs = static_library(
   override_options: ['rust_std=2021', 'build.rust_std=2021'],
   rust_abi: 'rust',
   link_with: [_bql_rs, _chardev_rs, _migration_rs, _qom_rs, _system_rs, _util_rs],
-  dependencies: [qemu_macros, common_rs],
+  dependencies: [glib_sys_rs, qemu_macros, common_rs],
 )
 
 hwcore_rs = declare_dependency(link_with: [_hwcore_rs],
rust/hw/core/src/bindings.rs +3 -0
index 919c02b56ae..65b9aae7536 100644
@@ -20,6 +20,9 @@
 
 use chardev::bindings::Chardev;
 use common::Zeroable;
+use glib_sys::{
+    GArray, GByteArray, GHashTable, GHashTableIter, GList, GPtrArray, GQueue, GSList, GString,
+};
 use migration::bindings::VMStateDescription;
 use qom::bindings::ObjectClass;
 use system::bindings::MemoryRegion;
rust/migration/Cargo.toml +1 -0
index 8efce7a72cb..415457496d6 100644
@@ -17,6 +17,7 @@ bql = { path = "../bql" }
 common = { path = "../common" }
 qemu_macros = { path = "../qemu-macros" }
 util = { path = "../util" }
+glib-sys.workspace = true
 
 [lints]
 workspace = true
rust/migration/meson.build +1 -1
index 362925e320f..444494700ad 100644
@@ -39,7 +39,7 @@ _migration_rs = static_library(
   override_options: ['rust_std=2021', 'build.rust_std=2021'],
   rust_abi: 'rust',
   link_with: [_util_rs, _bql_rs],
-  dependencies: [common_rs, qemu_macros],
+  dependencies: [common_rs, glib_sys_rs, qemu_macros],
 )
 
 migration_rs = declare_dependency(link_with: [_migration_rs],
rust/migration/src/bindings.rs +1 -0
index 8ce13a9000e..24503eb69bd 100644
@@ -19,6 +19,7 @@
 )]
 
 use common::Zeroable;
+use glib_sys::{GHashTable, GHashTableIter, GList, GPtrArray, GQueue, GSList};
 
 #[cfg(MESON)]
 include!("bindings.inc.rs");
rust/qom/Cargo.toml +1 -0
index 060ad2ec349..4be3c2541b6 100644
@@ -18,6 +18,7 @@ bql = { path = "../bql" }
 migration = { path = "../migration" }
 qemu_macros = { path = "../qemu-macros" }
 util = { path = "../util" }
+glib-sys.workspace = true
 
 [lints]
 workspace = true
rust/qom/meson.build +1 -1
index 71fdac696c3..e50f41858d6 100644
@@ -29,7 +29,7 @@ _qom_rs = static_library(
   override_options: ['rust_std=2021', 'build.rust_std=2021'],
   rust_abi: 'rust',
   link_with: [_bql_rs, _migration_rs],
-  dependencies: [common_rs, qemu_macros],
+  dependencies: [common_rs, glib_sys_rs, qemu_macros],
 )
 
 qom_rs = declare_dependency(link_with: [_qom_rs], dependencies: [qemu_macros, qom])
rust/qom/src/bindings.rs +2 -0
index 9ffff12cded..91de42f2426 100644
@@ -18,6 +18,8 @@
     clippy::too_many_arguments
 )]
 
+use glib_sys::{GHashTable, GHashTableIter, GList, GPtrArray, GQueue, GSList};
+
 #[cfg(MESON)]
 include!("bindings.inc.rs");
 
rust/system/Cargo.toml +1 -0
index 7fd369b9e32..186ea00bfff 100644
@@ -16,6 +16,7 @@ rust-version.workspace = true
 common = { path = "../common" }
 qom = { path = "../qom" }
 util = { path = "../util" }
+glib-sys.workspace = true
 
 [lints]
 workspace = true
rust/system/meson.build +1 -1
index 0859f397453..73d61991146 100644
@@ -36,7 +36,7 @@ _system_rs = static_library(
   override_options: ['rust_std=2021', 'build.rust_std=2021'],
   rust_abi: 'rust',
   link_with: [_bql_rs, _migration_rs, _qom_rs, _util_rs],
-  dependencies: [common_rs, qemu_macros],
+  dependencies: [glib_sys_rs, common_rs, qemu_macros],
 )
 
 system_rs = declare_dependency(link_with: [_system_rs],
rust/system/src/bindings.rs +4 -0
index 43edd98807a..6cbb588de3d 100644
@@ -19,6 +19,10 @@
 )]
 
 use common::Zeroable;
+use glib_sys::{
+    guint, GArray, GByteArray, GHashTable, GHashTableIter, GList, GPollFD, GPtrArray, GQueue,
+    GSList, GString,
+};
 
 #[cfg(MESON)]
 include!("bindings.inc.rs");
rust/util/Cargo.toml +1 -0
index 1f6767ed9d1..85f91436545 100644
@@ -15,6 +15,7 @@ rust-version.workspace = true
 [dependencies]
 anyhow = { workspace = true }
 foreign = { workspace = true }
+glib-sys = { workspace = true }
 libc = { workspace = true }
 common = { path = "../common" }
 
rust/util/meson.build +1 -1
index 094b43355aa..b0b75e93ff6 100644
@@ -40,7 +40,7 @@ _util_rs = static_library(
   ),
   override_options: ['rust_std=2021', 'build.rust_std=2021'],
   rust_abi: 'rust',
-  dependencies: [anyhow_rs, libc_rs, foreign_rs, common_rs, qom, qemuutil],
+  dependencies: [anyhow_rs, libc_rs, foreign_rs, glib_sys_rs, common_rs, qom, qemuutil],
 )
 
 util_rs = declare_dependency(link_with: [_util_rs], dependencies: [qemuutil, qom])
rust/util/src/bindings.rs +2 -0
index 9ffff12cded..c277a295add 100644
@@ -18,6 +18,8 @@
     clippy::too_many_arguments
 )]
 
+use glib_sys::{guint, GList, GPollFD, GQueue, GSList, GString};
+
 #[cfg(MESON)]
 include!("bindings.inc.rs");
 
Signature · 1 line
2.51.0