aboutsummaryrefslogtreecommitdiff
path: root/build.rs
blob: e44b3c0775f2f6931d2aeab4ebf092e694a03c1e (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
extern crate cc;

use std::env;
use std::path;

struct Version {
    major: String,
    minor: String,
    git: String,
}

fn stringify(err: env::VarError) -> String {
    format!("{}", err)
}

fn extract_git_version(pre: &str) -> Result<String, String> {
    // If a pre-release version is set, it is expected to have the format
    // pre.v<maj>.<min>.<n>.g<hash>, where <maj> and <min> are the last major and minor version,
    // <n> is the number of commits since this version and <hash> is the hash of the last commit.
    let parts: Vec<&str> = pre.split('.').collect();
    if parts.len() != 5 {
        return Err(format!("'{}' is not a valid pre-release version", pre));
    }
    Ok(format!("{}.{}-{}-{}", parts[1], parts[2], parts[3], parts[4]))
}

fn get_version() -> Result<Version, String> {
    let major = env::var("CARGO_PKG_VERSION_MAJOR").map_err(stringify)?;
    let minor = env::var("CARGO_PKG_VERSION_MINOR").map_err(stringify)?;
    let pre = env::var("CARGO_PKG_VERSION_PRE").map_err(stringify)?;

    let git = match pre.is_empty() {
        true => format!("v{}.{}", major, minor),
        false => extract_git_version(&pre)?,
    };

    Ok(Version {
        major,
        minor,
        git,
    })
}

fn main() {
    let version = get_version().expect("Could not extract library version");

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

    cc::Build::new()
        .cpp(true)
        .include(library_path.join("libnitrokey"))
        .files(sources.iter().map(|s| library_path.join(s)))
        .compile("libnitrokey.a");

    println!("cargo:rustc-link-lib=hidapi-libusb");
}