From 7a46645d70336c7887e2bf268d111f941c065f0e Mon Sep 17 00:00:00 2001 From: John Schug Date: Tue, 22 Oct 2024 15:08:04 -0700 Subject: [PATCH] Add support for raw-dylib on windows --- Cargo.toml | 8 ++--- docker/Dockerfile.static | 2 +- docker/Dockerfile.windows | 4 +-- gpgme-sys/Cargo.toml | 9 +++--- gpgme-sys/build.rs | 63 ++++++++++++++++++--------------------- gpgme-sys/src/lib.rs | 4 +++ 6 files changed, 44 insertions(+), 46 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 068a3b11..a6f6fece 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,6 @@ name = "gpgme" version.workspace = true edition.workspace = true -rust-version.workspace = true license.workspace = true repository.workspace = true categories = ["api-bindings"] @@ -10,7 +9,7 @@ keywords = ["gpg", "gpgme", "crypto", "cryptography"] description = "GPGme bindings for Rust" [package.metadata.docs.rs] -all-features = true +features = "latest" [package.metadata.cargo-semver-checks.lints] function_must_use_added = "allow" @@ -21,6 +20,8 @@ trait_must_use_added = "allow" union_must_use_added = "allow" [features] +windows_raw_dylib = ["ffi/windows_raw_dylib"] +latest = ["v1_23"] "v1_23" = ["v1_22"] "v1_22" = ["v1_21"] "v1_21" = ["v1_20"] @@ -42,7 +43,7 @@ bitflags = "2" cfg-if = "1" conv = "0.3" cstr-argument = "0.1" -gpg-error = "0.6.1" +gpg-error = "0.6.2" libc.workspace = true smallvec = "1" static_assertions = "1.1" @@ -65,7 +66,6 @@ useless_conversion = "allow" [workspace.package] version = "0.11.0" edition = "2021" -rust-version = "1.81" license = "LGPL-2.1" repository = "https://github.com/gpg-rs/gpgme" diff --git a/docker/Dockerfile.static b/docker/Dockerfile.static index 85883a8e..42a359af 100644 --- a/docker/Dockerfile.static +++ b/docker/Dockerfile.static @@ -43,4 +43,4 @@ RUN make -j$(nproc) install FROM builder WORKDIR /root/ws COPY ./ ./ -CMD ["cargo", "test", "--no-fail-fast", "--all-features"] +CMD ["cargo", "test", "--no-fail-fast", "--features", "latest"] diff --git a/docker/Dockerfile.windows b/docker/Dockerfile.windows index e0d6959c..979e61f7 100644 --- a/docker/Dockerfile.windows +++ b/docker/Dockerfile.windows @@ -1,5 +1,5 @@ # escape=` -ARG WIN_VARIANT=1909 +ARG WIN_VARIANT=ltsc2022 FROM mcr.microsoft.com/windows/servercore:${WIN_VARIANT} ENV RUSTUP_HOME=C:\rustup CARGO_HOME=C:\cargo @@ -20,4 +20,4 @@ RUN C:\TEMP\gnupg-w32.exe /S WORKDIR C:\workspace COPY ./ ./ ENV GPGME_DEBUG 9 -CMD ["cargo", "test", "--no-fail-fast", "--all-features"] +CMD ["cargo", "test", "--no-fail-fast", "--features", "latest"] diff --git a/gpgme-sys/Cargo.toml b/gpgme-sys/Cargo.toml index 472b4282..dda2c51c 100644 --- a/gpgme-sys/Cargo.toml +++ b/gpgme-sys/Cargo.toml @@ -2,7 +2,6 @@ name = "gpgme-sys" version.workspace = true edition.workspace = true -rust-version.workspace = true license.workspace = true repository.workspace = true categories = ["external-ffi-bindings"] @@ -13,18 +12,18 @@ links = "gpgme" [package.metadata.system-deps] gpgme = "1.13" -[badges] -maintenance = { status = "experimental" } +[features] +windows_raw_dylib = ["libgpg-error-sys/windows_raw_dylib"] [build-dependencies] +build-rs = "0.1.2" system-deps = "6.2.2" [dependencies] libc.workspace = true -libgpg-error-sys = "0.6.1" +libgpg-error-sys = "0.6.2" [target.'cfg(windows)'.build-dependencies] -build-rs = "0.1.2" winreg = "0.52.0" [lints.rust] diff --git a/gpgme-sys/build.rs b/gpgme-sys/build.rs index 26227577..3a00eca0 100644 --- a/gpgme-sys/build.rs +++ b/gpgme-sys/build.rs @@ -1,6 +1,10 @@ use std::error::Error; fn main() -> Result<(), Box> { + if build::cargo_cfg_windows() && build::cargo_feature("windows_raw_dylib") { + return Ok(()); // neccessary linker args set in lib.rs + } + #[cfg(windows)] if try_registry() { return Ok(()); @@ -12,46 +16,37 @@ fn main() -> Result<(), Box> { #[cfg(windows)] fn try_registry() -> bool { - use std::{ffi::OsString, fs, path::PathBuf}; + use std::{ffi::OsString, path::PathBuf}; use winreg::{enums::*, RegKey}; + fn try_key(path: &str) -> bool { + let hklm = RegKey::predef(HKEY_LOCAL_MACHINE); + let Ok(key) = hklm + .open_subkey_with_flags(path, KEY_WOW64_32KEY | KEY_READ) + .inspect_err(|e| eprintln!("unable to retrieve install location: {e}")) + else { + return false; + }; + let Ok(root) = key + .get_value::("Install Directory") + .map(PathBuf::from) + .inspect_err(|e| eprintln!("unable to retrieve install location: {e}")) + else { + return false; + }; + + println!("detected install via registry: {}", root.display()); + build::rustc_link_search(root.join("lib")); + build::rustc_link_lib("dylib:+verbatim=libgpgme.imp"); + true + } if !build::cargo_cfg_windows() { eprintln!("cross compiling. disabling registry detection."); return false; } - let hklm = RegKey::predef(HKEY_LOCAL_MACHINE); - let key = match hklm.open_subkey_with_flags(r"SOFTWARE\GnuPG", KEY_WOW64_32KEY | KEY_READ) { - Ok(x) => x, - Err(e) => { - eprintln!("Unable to retrieve install location: {e}"); - return false; - } - }; - let root = match key - .get_value::("Install Directory") - .map(PathBuf::from) - { - Ok(x) => x, - Err(e) => { - eprintln!("Unable to retrieve install location: {e}"); - return false; - } - }; - println!("detected install via registry: {}", root.display()); - - if root.join("lib/libgpgme.imp").exists() { - if let Err(e) = fs::copy( - root.join("lib/libgpgme.imp"), - build::out_dir().join("libgpgme.a"), - ) { - eprintln!("Unable to rename library: {e}"); - return false; - } - } - - build::rustc_link_search(build::out_dir()); - build::rustc_link_lib("gpgme"); - true + [r"SOFTWARE\Gpg4win", r"SOFTWARE\GnuPG"] + .iter() + .any(|s| try_key(s)) } diff --git a/gpgme-sys/src/lib.rs b/gpgme-sys/src/lib.rs index db9e038b..12dbcaa1 100644 --- a/gpgme-sys/src/lib.rs +++ b/gpgme-sys/src/lib.rs @@ -1122,6 +1122,10 @@ impl _gpgme_op_query_swdb_result { } pub type gpgme_query_swdb_result_t = *mut _gpgme_op_query_swdb_result; +#[cfg_attr( + all(windows, feature = "windows_raw_dylib"), + link(name = "libgpgme-11.dll", kind = "raw-dylib", modifiers = "+verbatim") +)] extern "C" { pub fn gpgme_set_global_flag(name: *const c_char, value: *const c_char) -> c_int;