File: build.rs

package info (click to toggle)
rust-libgpg-error-sys 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 140 kB
  • sloc: makefile: 4
file content (53 lines) | stat: -rw-r--r-- 1,490 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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(());
    }

    system_deps::Config::new().probe()?;
    Ok(())
}

#[cfg(windows)]
fn try_registry() -> bool {
    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=libgpg-error.imp");
        true
    }

    if !build::cargo_cfg_windows() {
        eprintln!("cross compiling. disabling registry detection.");
        return false;
    }

    [r"SOFTWARE\Gpg4win", r"SOFTWARE\GnuPG"]
        .iter()
        .any(|s| try_key(s))
}