Skip to content

Commit

Permalink
Add support for raw-dylib on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
johnschug committed Oct 22, 2024
1 parent 61ae798 commit 7a46645
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 46 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
name = "gpgme"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
repository.workspace = true
categories = ["api-bindings"]
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"
Expand All @@ -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"]
Expand All @@ -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"
Expand All @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile.static
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
4 changes: 2 additions & 2 deletions docker/Dockerfile.windows
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"]
9 changes: 4 additions & 5 deletions gpgme-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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]
Expand Down
63 changes: 29 additions & 34 deletions gpgme-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
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(());
Expand All @@ -12,46 +16,37 @@ fn main() -> Result<(), Box<dyn Error>> {

#[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::<OsString, _>("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::<OsString, _>("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))
}
4 changes: 4 additions & 0 deletions gpgme-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 7a46645

Please sign in to comment.