From c15bcbf123bc0188bb26010d8366b75ca1829e4c Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Sun, 7 Jul 2024 23:23:59 +1000 Subject: [PATCH 1/2] Use raw-dylib for windows-sys To avoid having to declare `#[link(name = "...")]` Signed-off-by: Jiahao XU --- dev-tools/gen-windows-sys-binding/Cargo.toml | 1 - dev-tools/gen-windows-sys-binding/src/main.rs | 23 ------------------- src/windows/windows_sys.rs | 4 ---- src/windows/windows_targets.rs | 2 +- 4 files changed, 1 insertion(+), 29 deletions(-) diff --git a/dev-tools/gen-windows-sys-binding/Cargo.toml b/dev-tools/gen-windows-sys-binding/Cargo.toml index 12d9f276..15446016 100644 --- a/dev-tools/gen-windows-sys-binding/Cargo.toml +++ b/dev-tools/gen-windows-sys-binding/Cargo.toml @@ -7,4 +7,3 @@ publish = false [dependencies] windows-bindgen = "0.58" tempfile = "3" -regex = "1" diff --git a/dev-tools/gen-windows-sys-binding/src/main.rs b/dev-tools/gen-windows-sys-binding/src/main.rs index ef7c15f3..f7d21a52 100644 --- a/dev-tools/gen-windows-sys-binding/src/main.rs +++ b/dev-tools/gen-windows-sys-binding/src/main.rs @@ -6,8 +6,6 @@ use std::{ io::{BufWriter, Write as _}, }; -use regex::Regex; - /// This is printed to the file before the rest of the contents. const PRELUDE: &str = r#"// This file is autogenerated. // @@ -60,27 +58,6 @@ fn main() { write!(&mut f, "{PRELUDE}\n{bindings}\n").unwrap(); - let mut dll_names: Vec<&str> = Regex::new(r#"link!\("(.*)\.dll""#) - .unwrap() - .captures_iter(&bindings) - .map(|caps| caps.extract().1) - .map(|[dll_name]| dll_name) - .filter(|dll_name| *dll_name != "kernel32") - .collect(); - - if !dll_names.is_empty() { - dll_names.sort_unstable(); - dll_names.dedup(); - - for dll_name in dll_names { - write!(&mut f, r#"#[link(name = "{dll_name}")]"#).unwrap(); - f.write_all("\n".as_bytes()).unwrap(); - } - - f.write_all(r#"extern "C" {}"#.as_bytes()).unwrap(); - f.write_all("\n".as_bytes()).unwrap(); - } - f.write_all(r#"use super::windows_targets;"#.as_bytes()) .unwrap(); f.write_all("\n".as_bytes()).unwrap(); diff --git a/src/windows/windows_sys.rs b/src/windows/windows_sys.rs index fd177e65..9e06a237 100644 --- a/src/windows/windows_sys.rs +++ b/src/windows/windows_sys.rs @@ -114,8 +114,4 @@ pub const WAIT_OBJECT_0: WAIT_EVENT = 0u32; pub const WAIT_TIMEOUT: WAIT_EVENT = 258u32; pub type WIN32_ERROR = u32; -#[link(name = "advapi32")] -#[link(name = "ole32")] -#[link(name = "oleaut32")] -extern "C" {} use super::windows_targets; diff --git a/src/windows/windows_targets.rs b/src/windows/windows_targets.rs index d08affe0..fa0fa2fe 100644 --- a/src/windows/windows_targets.rs +++ b/src/windows/windows_targets.rs @@ -9,7 +9,7 @@ macro_rules! link_macro { // have in this repo. So instead we always link kernel32.lib and add the rest of the import // libraries below by using an empty extern block. This works because extern blocks are not // connected to the library given in the #[link] attribute. - #[link(name = "kernel32")] + #[link(name = "kernel32", kind = "raw-dylib")] extern $abi { $(#[link_name=$link_name])? pub fn $($function)*; From 02ecf8f3d5a97f5afa61f042e0d0ef18f9151b2d Mon Sep 17 00:00:00 2001 From: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> Date: Mon, 8 Jul 2024 00:16:04 +1000 Subject: [PATCH 2/2] Update windows_targets.rs Co-authored-by: Chris Denton --- src/windows/windows_targets.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/windows/windows_targets.rs b/src/windows/windows_targets.rs index fa0fa2fe..30ddf643 100644 --- a/src/windows/windows_targets.rs +++ b/src/windows/windows_targets.rs @@ -9,7 +9,8 @@ macro_rules! link_macro { // have in this repo. So instead we always link kernel32.lib and add the rest of the import // libraries below by using an empty extern block. This works because extern blocks are not // connected to the library given in the #[link] attribute. - #[link(name = "kernel32", kind = "raw-dylib")] + #[cfg_attr(not(target_arch = "x86"), link(name = $library, kind = "raw-dylib", modifiers = "+verbatim"))] + #[cfg_attr(target_arch = "x86", link(name = $library, kind = "raw-dylib", modifiers = "+verbatim", import_name_type = "undecorated"))] extern $abi { $(#[link_name=$link_name])? pub fn $($function)*;