From 787a7eb45b67c4674da5d272066c226ed55a4a7b Mon Sep 17 00:00:00 2001 From: Sophie Herold Date: Thu, 5 Sep 2024 15:28:10 +0200 Subject: [PATCH 1/6] generator.py: Use cargo run instead of guessing path Currently, the script just guesses a target path based on the default config. This fails if the project or the user has configured a different target directory. This code is shorter and more correct. --- generator.py | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/generator.py b/generator.py index 950857329..f1385f9fd 100755 --- a/generator.py +++ b/generator.py @@ -8,7 +8,6 @@ DEFAULT_GIR_FILES_DIRECTORY = Path("./gir-files") DEFAULT_GIR_DIRECTORY = Path("./gir/") -DEFAULT_GIR_PATH = DEFAULT_GIR_DIRECTORY / "target/release/gir" def run_command(command, folder=None): @@ -30,8 +29,13 @@ async def spawn_process(exe, args): return stdout, stderr -async def spawn_gir(gir_exe, args): - stdout, stderr = await spawn_process(gir_exe, args) +async def spawn_gir(conf, args): + if conf.gir_path: + stdout, stderr = await spawn_process(conf.gir_path, args) + else: + cargo_args = ["run", "--release", "--quiet", "--manifest-path", DEFAULT_GIR_DIRECTORY / "Cargo.toml", "--"] + stdout, stderr = await spawn_process("cargo", cargo_args + args) + # Gir doesn't print anything to stdout. If it does, this is likely out of # order with stderr, unless the printer/logging flushes in between. assert not stdout, "`gir` printed unexpected stdout: {}".format(stdout) @@ -40,10 +44,6 @@ async def spawn_gir(gir_exe, args): return "" -def update_workspace(): - return run_command(["cargo", "build", "--release"], "gir") - - def ask_yes_no_question(question, conf): question = "{} [y/N] ".format(question) if conf.yes: @@ -71,12 +71,6 @@ def update_submodule(submodule_path, conf): return False -def build_gir(): - print("=> Building gir...") - update_workspace() - print("<= Done!") - - async def regenerate_crate_docs(conf, crate_dir, base_gir_args): doc_path = "docs.md" # Generate into docs.md instead of the default vendor.md @@ -99,7 +93,7 @@ async def regenerate_crate_docs(conf, crate_dir, base_gir_args): logs += "==> Regenerating documentation for `{}` into `{}`...\n".format( crate_dir, doc_path ) - logs += await spawn_gir(conf.gir_path, doc_args) + logs += await spawn_gir(conf, doc_args) logs += "==> Embedding documentation from `{}` into `{}`...\n".format( doc_path, crate_dir @@ -139,7 +133,7 @@ def regen_crates(path, conf): async def regenerate_crate(path, args): return "==> Regenerating `{}`...\n".format(path) + await spawn_gir( - conf.gir_path, args + conf, args ) processes.append(regenerate_crate(path, args)) @@ -194,7 +188,6 @@ def parse_args(): ) parser.add_argument( "--gir-path", - default=DEFAULT_GIR_PATH, type=file_path, help="Path of the gir executable to run", ) @@ -228,9 +221,8 @@ async def main(): if not conf.gir_files_paths: update_submodule(DEFAULT_GIR_FILES_DIRECTORY, conf) - if conf.gir_path == DEFAULT_GIR_PATH: + if conf.gir_path is None: update_submodule(DEFAULT_GIR_DIRECTORY, conf) - build_gir() print("=> Regenerating crates...") for path in conf.path: From cca9ec4c7c2034cff6f33e0964f8e27da21a1624 Mon Sep 17 00:00:00 2001 From: Thibault Saunier Date: Tue, 14 Mar 2023 15:39:04 -0300 Subject: [PATCH 2/6] Revert "Specify link attribute for Windows to be happy" This reverts commit a8dc21319bf6edaba19b2046ffd60c80fd91a2d7. Linking flags should be provided by the system-deps crate. Specifying linking with the attribute creates issue when statically linking system dependencies. --- src/codegen/sys/lib_.rs | 13 ------------- src/nameutil.rs | 29 ----------------------------- 2 files changed, 42 deletions(-) diff --git a/src/codegen/sys/lib_.rs b/src/codegen/sys/lib_.rs index c3f7b181f..abc97414d 100644 --- a/src/codegen/sys/lib_.rs +++ b/src/codegen/sys/lib_.rs @@ -25,18 +25,6 @@ pub fn generate(env: &Env) { save_to_file(&path, env.config.make_backup, |w| generate_lib(w, env)); } -fn write_link_attr(w: &mut dyn Write, shared_libs: &[String]) -> Result<()> { - for it in shared_libs { - writeln!( - w, - "#[link(name = \"{}\")]", - shared_lib_name_to_link_name(it) - )?; - } - - Ok(()) -} - fn generate_lib(w: &mut dyn Write, env: &Env) -> Result<()> { general::start_comments(w, &env.config)?; statics::begin(w)?; @@ -89,7 +77,6 @@ fn generate_lib(w: &mut dyn Write, env: &Env) -> Result<()> { } if !env.namespaces.main().shared_libs.is_empty() { - write_link_attr(w, &env.namespaces.main().shared_libs)?; writeln!(w, "extern \"C\" {{")?; functions::generate_enums_funcs(w, env, &enums)?; functions::generate_bitfields_funcs(w, env, &bitfields)?; diff --git a/src/nameutil.rs b/src/nameutil.rs index 9a5f6076d..09b2c85ef 100644 --- a/src/nameutil.rs +++ b/src/nameutil.rs @@ -129,25 +129,6 @@ pub fn lib_name_to_toml(name: &str) -> String { name.to_string().replace(['-', '.'], "_") } -pub fn shared_lib_name_to_link_name(name: &str) -> &str { - let mut s = name; - - if s.starts_with("lib") { - s = &s[3..]; - } - - if let Some(offset) = s.rfind(".so") { - s = &s[..offset]; - } else if let Some(offset) = s.rfind(".dll") { - s = &s[..offset]; - if let Some(offset) = s.rfind('-') { - s = &s[..offset]; - } - } - - s -} - pub fn use_glib_type(env: &crate::env::Env, import: &str) -> String { format!( "{}::{}", @@ -253,14 +234,4 @@ mod tests { fn lib_name_to_toml_works() { assert_eq!(lib_name_to_toml("gstreamer-1.0"), "gstreamer_1_0"); } - - #[test] - fn shared_lib_name_to_link_name_works() { - assert_eq!(shared_lib_name_to_link_name("libgtk-4-1.dll"), "gtk-4"); - assert_eq!(shared_lib_name_to_link_name("libatk-1.0.so.0"), "atk-1.0"); - assert_eq!( - shared_lib_name_to_link_name("libgdk_pixbuf-2.0.so.0"), - "gdk_pixbuf-2.0" - ); - } } From b4a8743772cd437f65cb13fd3315141ce9361835 Mon Sep 17 00:00:00 2001 From: Maximiliano Sandoval Date: Sat, 7 Sep 2024 21:07:32 +0200 Subject: [PATCH 3/6] ffi: Use std::ffi types instead of libc These aliases were added in rust 1.64. --- src/analysis/ffi_type.rs | 26 +++++++++++++------------- src/codegen/sys/statics.rs | 7 ++++--- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/analysis/ffi_type.rs b/src/analysis/ffi_type.rs index 3583ff28a..60926257a 100644 --- a/src/analysis/ffi_type.rs +++ b/src/analysis/ffi_type.rs @@ -72,7 +72,7 @@ fn ffi_inner(env: &Env, tid: TypeId, inner: &str) -> Result { Type::Basic(fund) => { use crate::library::Basic::*; let inner = match fund { - None => "libc::c_void", + None => "std::ffi::c_void", Boolean => return Ok(use_glib_if_needed(env, "ffi::gboolean").into()), Int8 => "i8", UInt8 => "u8", @@ -82,18 +82,18 @@ fn ffi_inner(env: &Env, tid: TypeId, inner: &str) -> Result { UInt32 => "u32", Int64 => "i64", UInt64 => "u64", - Char => "libc::c_char", - UChar => "libc::c_uchar", - Short => "libc::c_short", - UShort => "libc::c_ushort", - Int => "libc::c_int", - UInt => "libc::c_uint", - Long => "libc::c_long", - ULong => "libc::c_ulong", + Char => "std::ffi::c_char", + UChar => "std::ffi::c_uchar", + Short => "std::ffi::c_short", + UShort => "std::ffi::c_ushort", + Int => "std::ffi::c_int", + UInt => "std::ffi::c_uint", + Long => "std::ffi::c_long", + ULong => "std::ffi::c_ulong", Size => "libc::size_t", SSize => "libc::ssize_t", - Float => "libc::c_float", - Double => "libc::c_double", + Float => "std::ffi::c_float", + Double => "std::ffi::c_double", TimeT => "libc::time_t", OffT => "libc::off_t", DevT => "libc::dev_t", @@ -102,8 +102,8 @@ fn ffi_inner(env: &Env, tid: TypeId, inner: &str) -> Result { SockLenT => "libc::socklen_t", UidT => "libc::uid_t", UniChar => "u32", - Utf8 => "libc::c_char", - Filename => "libc::c_char", + Utf8 => "std::ffi::c_char", + Filename => "std::ffi::c_char", Type => return Ok(use_glib_if_needed(env, "ffi::GType").into()), IntPtr => "libc::intptr_t", UIntPtr => "libc::uintptr_t", diff --git a/src/codegen/sys/statics.rs b/src/codegen/sys/statics.rs index 94c637246..3ab802f1c 100644 --- a/src/codegen/sys/statics.rs +++ b/src/codegen/sys/statics.rs @@ -18,9 +18,10 @@ pub fn after_extern_crates(w: &mut dyn Write) -> Result<()> { let v = vec![ "", "#[allow(unused_imports)]", - "use libc::{c_int, c_char, c_uchar, c_float, c_uint, c_double,", - " c_short, c_ushort, c_long, c_ulong,", - " c_void, size_t, ssize_t, time_t, off_t, intptr_t, uintptr_t, FILE};", + "use std::ffi::{c_int, c_char, c_uchar, c_float, c_uint, c_double,", + " c_short, c_ushort, c_long, c_ulong, c_void};", + "#[allow(unused_imports)]", + "use libc::{size_t, ssize_t, time_t, off_t, intptr_t, uintptr_t, FILE};", "#[cfg(unix)]", "#[allow(unused_imports)]", "use libc::{dev_t, gid_t, pid_t, socklen_t, uid_t};", From d35c893fff6b0f3f6f10d6129c3bcda992e4e460 Mon Sep 17 00:00:00 2001 From: eri Date: Wed, 18 Sep 2024 10:48:38 +0200 Subject: [PATCH 4/6] dont override build script in cargo.toml --- src/codegen/sys/cargo_toml.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/codegen/sys/cargo_toml.rs b/src/codegen/sys/cargo_toml.rs index fab07dd35..ccfccb1be 100644 --- a/src/codegen/sys/cargo_toml.rs +++ b/src/codegen/sys/cargo_toml.rs @@ -99,7 +99,9 @@ fn fill_empty(root: &mut Table, env: &Env, crate_name: &str) { fn fill_in(root: &mut Table, env: &Env) { { let package = upsert_table(root, "package"); - set_string(package, "build", "build.rs"); + package + .entry("build") + .or_insert_with(|| Value::String("build.rs".into())); // set_string(package, "version", "0.2.0"); } From 711d39a02ffeb28145bb21c295eeda3da494b88e Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Thu, 19 Sep 2024 21:32:29 +0200 Subject: [PATCH 5/6] Add note to use ffi --- book/src/tutorial/high_level_rust_api.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/book/src/tutorial/high_level_rust_api.md b/book/src/tutorial/high_level_rust_api.md index 96afeb939..471daa64e 100644 --- a/book/src/tutorial/high_level_rust_api.md +++ b/book/src/tutorial/high_level_rust_api.md @@ -114,11 +114,13 @@ v1_52 = ["ffi/v1_52", "v1_50"] The lib.rs file will not be replaced when you run gir. All the code that gir will generate for us is going to be in src/auto. We need to include all `auto` files in our library. +Also it needs to include sys create so auto files can use it. To do so, let's update the `src/lib.rs` file as follows: ```rust #![cfg_attr(docsrs, feature(doc_cfg))] +use ffi; pub use auto::*; mod auto; ``` From 8d3ec8fe0717c40259b4950ef2f0a7fb3b240c47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Thu, 26 Sep 2024 09:38:20 +0300 Subject: [PATCH 6/6] Update Cargo.lock --- Cargo.lock | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6db97df6c..dc195a06a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -100,9 +100,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.3.0" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de3fc2e30ba82dd1b3911c8de1ffc143c74a914a14e99514d7637e3099df5ea0" +checksum = "68b900aa2f7301e21c36462b170ee99994de34dff39a4a6a528e80e7376d07e5" dependencies = [ "equivalent", "hashbrown", @@ -146,9 +146,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.36" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" dependencies = [ "proc-macro2", ] @@ -189,18 +189,18 @@ source = "git+https://github.com/GuillaumeGomez/rustdoc-stripper#1170265c0f2a372 [[package]] name = "serde" -version = "1.0.206" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b3e4cd94123dd520a128bcd11e34d9e9e423e7e3e50425cb1b4b1e3549d0284" +checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.206" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fabfb6138d2383ea8208cf98ccf69cdfb1aff4088460681d84189aa259762f97" +checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" dependencies = [ "proc-macro2", "quote", @@ -209,18 +209,18 @@ dependencies = [ [[package]] name = "serde_spanned" -version = "0.6.7" +version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb5b1b31579f3811bf615c144393417496f152e12ac8b7663bf664f4a815306d" +checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1" dependencies = [ "serde", ] [[package]] name = "syn" -version = "2.0.74" +version = "2.0.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fceb41e3d546d0bd83421d3409b1460cc7444cd389341a4c880fe7a042cb3d7" +checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" dependencies = [ "proc-macro2", "quote", @@ -251,9 +251,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.20" +version = "0.22.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "583c44c02ad26b0c3f3066fe629275e50627026c51ac2e595cca4c230ce1ce1d" +checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5" dependencies = [ "indexmap", "serde", @@ -264,27 +264,27 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.12" +version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" [[package]] name = "unicode-width" -version = "0.1.13" +version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" [[package]] name = "winnow" -version = "0.6.18" +version = "0.6.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68a9bda4691f099d435ad181000724da8e5899daa10713c2d432552b9ccd3a6f" +checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b" dependencies = [ "memchr", ] [[package]] name = "xml-rs" -version = "0.8.21" +version = "0.8.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "539a77ee7c0de333dcc6da69b177380a0b81e0dacfa4f7344c465a36871ee601" +checksum = "af4e2e2f7cba5a093896c1e150fbfe177d1883e7448200efb81d40b9d339ef26"