Generating library with gir. Issue with glib_wrapper

Using gir to generate my first item, it will not compile because the generated glib_wrapper macro is not found. no rules expected this token in macro call

glib::glib_wrapper! {
    pub struct SparqlConnection(Object<ffi::TrackerSparqlConnection, ffi::TrackerSparqlConnectionClass>);

    match fn {
        get_type => || ffi::tracker_sparql_connection_get_type(),
    }
}

Looking at the tracker.gir file and gir’s source code I cannot figure out why it created an invalid macro?
Also, is ffi a new technique for rust 2018, because other libraries use their own namespaces and not ffi?

Can you put your code somewhere? That will make it easier to understand what exactly went wrong there. I have a few ideas :slight_smile:

1 Like

Yeap @sdroege , the code to the glib_wrapper. The Tracker-3.0.gir file is in tracker-sys/gir-files/

There are multiple problems here:

  • The tracker-sys needs to be renamed to ffi in the Cargo.toml
  • The dependencies were wrong: it’s all in the gtk-rs repository on github now instead of separate repositories, so you were using an old version. I guess something has to be updated for that in the docs. That was the reason why the macro did not compile.
  • Don’t do extern crate anymore with Rust 2018 edition (doesn’t break anything but is unneeded)
  • You need to add those assertion macros to your lib.rs or disable usage of them in Gir.toml. See my comment about that below.

With the following diff it compiles for me:

diff --git a/Cargo.toml b/Cargo.toml
index 0d20b8c..3f6eb82 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,14 +8,15 @@ edition = "2018"
 libc = "0.2"
 bitflags = "1.0"
 
-[dependencies.tracker-sys]
+[dependencies.ffi]
+package = "tracker-sys"
 path = "./tracker-sys"
 
 [dependencies.glib]
-git = "https://github.com/gtk-rs/glib"
+git = "https://github.com/gtk-rs/gtk-rs"
 
 [dependencies.glib-sys]
-git = "https://github.com/gtk-rs/sys" # all gtk-rs sys crates are in the sys repository
+git = "https://github.com/gtk-rs/gtk-rs"
 
 #[dependencies.gtk]
 #git = "https://github.com/gtk-rs/gtk"
diff --git a/src/lib.rs b/src/lib.rs
index 0ac5b45..f5863a0 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,12 +1,15 @@
-#[macro_use]
-extern crate glib;
-extern crate glib_sys;
-//extern crate gtk;
-extern crate tracker_sys;
-
-extern crate libc;
-#[macro_use]
-extern crate bitflags;
+macro_rules! assert_initialized_main_thread {
+    () => {
+        // TODO: Check if tracker is initialized and this is a thread where it's allowed to use the
+        // tracker API. Or if tracker does not have such constraints then set
+        // `generate_safety_asserts = false` in Gir.toml
+    };
+}
+
+macro_rules! skip_assert_initialized {
+    () => {};
+}
+
 
 pub use auto::*;
 
diff --git a/tracker-sys/Cargo.toml b/tracker-sys/Cargo.toml
index fa50274..6e215f9 100644
--- a/tracker-sys/Cargo.toml
+++ b/tracker-sys/Cargo.toml
@@ -17,13 +17,13 @@ name = "tracker_sys"
 libc = "0.2"
 
 [dependencies.gio-sys]
-git = "https://github.com/gtk-rs/sys"
+git = "https://github.com/gtk-rs/gtk-rs"
 
 [dependencies.glib-sys]
-git = "https://github.com/gtk-rs/sys"
+git = "https://github.com/gtk-rs/gtk-rs"
 
 [dependencies.gobject-sys]
-git = "https://github.com/gtk-rs/sys"
+git = "https://github.com/gtk-rs/gtk-rs"
 
 [build-dependencies]
 system-deps = "2.0"

We changed how to import the -sys crates together with the move to Rust 2018, yes. The main -sys crate is now imported as ffi, the other (dependency) -sys crates are available as foo::ffi so you don’t have to depend on both the -sys and non-sys crates anymore but only one of the two.

Thank you :blush:, I got it mostly working. Had to update rust from 1.47 to 1.48 to suppress errors, it’s staying on the bleeding edge!

The gir tutorial needs an overhaul. There are a few issues and PRs already related to fixing some old information. It shouldn’t take too long, just a paragraph about the macros might need a bit more time.

Yeah, documentation easily gets outdated :slight_smile: Can you create an issue on GitHub for the things you noticed?

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.