aboutsummaryrefslogtreecommitdiff
path: root/build.rs
blob: d097021a0b2b71c09e058e0031fdf180e9c07043 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use std::env;
use std::fmt;
use std::fs;
use std::io;
use std::io::{Read, Write};
use std::path;

use cc;

#[derive(Clone, Copy, Debug, PartialEq)]
struct Version {
    major: u32,
    minor: u32,
    patch: Option<u32>,
}

impl fmt::Display for Version {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "v{}.{}", self.major, self.minor)?;
        if let Some(patch) = self.patch {
            write!(f, ".{}", patch)?;
        }
        Ok(())
    }
}

const LIBNITROKEY_VERSION: Version = Version {
    major: 3,
    minor: 6,
    patch: None,
};

fn prepare_version_source(
    version: Version,
    out_path: &path::Path,
    library_path: &path::Path,
) -> io::Result<path::PathBuf> {
    let out = out_path.join("version.cc");
    let template = library_path.join("version.cc.in");

    let mut file = fs::File::open(template)?;
    let mut data = String::new();
    file.read_to_string(&mut data)?;
    drop(file);

    let data = data
        .replace("@PROJECT_VERSION_MAJOR@", &version.major.to_string())
        .replace("@PROJECT_VERSION_MINOR@", &version.minor.to_string())
        .replace("@PROJECT_VERSION_GIT@", &version.to_string());

    let mut file = fs::File::create(&out)?;
    file.write_all(data.as_bytes())?;

    Ok(out)
}

#[cfg(feature = "bindgen")]
fn generate_bindings(library_path: &path::Path, out_path: &path::Path) {
    let header_path = library_path.join("NK_C_API.h");
    let header_str = header_path
        .to_str()
        .expect("Header path contains invalid UTF-8");

    let include_path = library_path.join("libnitrokey");
    let include_str = include_path
        .to_str()
        .expect("Include path contains invalid UTF-8");

    println!("cargo:rerun-if-changed={}", header_str);

    // always keep options in sync with Makefile
    let bindings = bindgen::Builder::default()
        .header(header_str)
        .whitelist_function("NK_.*")
        .whitelist_var("NK_.*")
        .whitelist_var("MAXIMUM_STR_REPLY_LENGTH")
        .derive_default(true)
        .clang_arg(&format!("-I{}", include_str))
        .generate()
        .expect("Unable to generate bindings");
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Could not write bindings");
}

fn main() {
    if env::var("USE_SYSTEM_LIBNITROKEY").is_ok() {
        println!("cargo:rustc-link-lib=nitrokey");
        return;
    }

    let out_dir = env::var("OUT_DIR").expect("Environment variable OUT_DIR is not set");
    let out_path = path::PathBuf::from(out_dir);

    let sources = [
        "DeviceCommunicationExceptions.cpp",
        "NK_C_API.cc",
        "NitrokeyManager.cc",
        "command_id.cc",
        "device.cc",
        "log.cc",
        "misc.cc",
    ];
    let library_dir = format!("libnitrokey-{}", LIBNITROKEY_VERSION.to_string());
    let library_path = path::Path::new(&library_dir);

    let version_source = prepare_version_source(LIBNITROKEY_VERSION, &out_path, &library_path)
        .expect("Could not prepare the version source file");

    #[cfg(feature = "bindgen")]
    generate_bindings(library_path, &out_path);

    cc::Build::new()
        .cpp(true)
        .flag("-std=c++14")
        .include(library_path.join("libnitrokey"))
        .files(sources.iter().map(|s| library_path.join(s)))
        .file(version_source)
        .compile("libnitrokey.a");

    let hidapi_library_name = if cfg!(target_os = "linux") {
        "hidapi-libusb"
    } else {
        "hidapi"
    };
    println!("cargo:rustc-link-lib={}", hidapi_library_name);
}