10 | 10 |
===================================================================
|
11 | 11 |
--- nitrokey-sys.orig/build.rs
|
12 | 12 |
+++ nitrokey-sys/build.rs
|
13 | |
@@ -1,104 +1,3 @@
|
14 | |
-extern crate cc;
|
15 | |
-
|
|
13 |
@@ -1,94 +1,3 @@
|
16 | 14 |
-use std::env;
|
|
15 |
-use std::fs;
|
17 | 16 |
-use std::io;
|
18 | 17 |
-use std::io::{Read, Write};
|
19 | |
-use std::fs;
|
20 | 18 |
-use std::path;
|
|
19 |
-use std::string;
|
21 | 20 |
-
|
|
21 |
-use cc;
|
|
22 |
-
|
|
23 |
-#[derive(Clone, Copy, Debug, PartialEq)]
|
22 | 24 |
-struct Version {
|
23 | |
- major: String,
|
24 | |
- minor: String,
|
25 | |
- git: String,
|
|
25 |
- major: u32,
|
|
26 |
- minor: u32,
|
|
27 |
- patch: Option<u32>,
|
26 | 28 |
-}
|
27 | 29 |
-
|
28 | |
-fn stringify(err: env::VarError) -> String {
|
29 | |
- format!("{}", err)
|
|
30 |
-impl string::ToString for Version {
|
|
31 |
- fn to_string(&self) -> String {
|
|
32 |
- match self.patch {
|
|
33 |
- Some(patch) => format!("v{}.{}.{}", self.major, self.minor, patch),
|
|
34 |
- None => format!("v{}.{}", self.major, self.minor),
|
|
35 |
- }
|
|
36 |
- }
|
30 | 37 |
-}
|
31 | 38 |
-
|
32 | |
-fn extract_git_version(pre: &str) -> Result<String, String> {
|
33 | |
- // If a pre-release version is set, it is expected to have the format
|
34 | |
- // pre.v<maj>.<min>.<n>.g<hash>, where <maj> and <min> are the last major and minor version,
|
35 | |
- // <n> is the number of commits since this version and <hash> is the hash of the last commit.
|
36 | |
- let parts: Vec<&str> = pre.split('.').collect();
|
37 | |
- if parts.len() != 5 {
|
38 | |
- return Err(format!("'{}' is not a valid pre-release version", pre));
|
39 | |
- }
|
40 | |
- Ok(format!("{}.{}-{}-{}", parts[1], parts[2], parts[3], parts[4]))
|
41 | |
-}
|
42 | |
-
|
43 | |
-fn get_version() -> Result<Version, String> {
|
44 | |
- let major = env::var("CARGO_PKG_VERSION_MAJOR").map_err(stringify)?;
|
45 | |
- let minor = env::var("CARGO_PKG_VERSION_MINOR").map_err(stringify)?;
|
46 | |
- let patch = env::var("CARGO_PKG_VERSION_PATCH").map_err(stringify)?;
|
47 | |
- let pre = env::var("CARGO_PKG_VERSION_PRE").map_err(stringify)?;
|
48 | |
-
|
49 | |
- let git = match pre.is_empty() {
|
50 | |
- true => match patch.is_empty() {
|
51 | |
- true => format!("v{}.{}", major, minor),
|
52 | |
- false => format!("v{}.{}.{}", major, minor, patch),
|
53 | |
- },
|
54 | |
- false => extract_git_version(&pre)?,
|
55 | |
- };
|
56 | |
-
|
57 | |
- Ok(Version {
|
58 | |
- major,
|
59 | |
- minor,
|
60 | |
- git,
|
61 | |
- })
|
62 | |
-}
|
|
39 |
-const LIBNITROKEY_VERSION: Version = Version {
|
|
40 |
- major: 3,
|
|
41 |
- minor: 4,
|
|
42 |
- patch: Some(1),
|
|
43 |
-};
|
63 | 44 |
-
|
64 | 45 |
-fn prepare_version_source(
|
65 | |
- version: &Version,
|
|
46 |
- version: Version,
|
66 | 47 |
- out_path: &path::Path,
|
67 | |
- library_path: &path::Path
|
|
48 |
- library_path: &path::Path,
|
68 | 49 |
-) -> io::Result<path::PathBuf> {
|
69 | 50 |
- let out = out_path.join("version.cc");
|
70 | 51 |
- let template = library_path.join("version.cc.in");
|
|
75 | 56 |
- drop(file);
|
76 | 57 |
-
|
77 | 58 |
- let data = data
|
78 | |
- .replace("@PROJECT_VERSION_MAJOR@", &version.major)
|
79 | |
- .replace("@PROJECT_VERSION_MINOR@", &version.minor)
|
80 | |
- .replace("@PROJECT_VERSION_GIT@", &version.git);
|
|
59 |
- .replace("@PROJECT_VERSION_MAJOR@", &version.major.to_string())
|
|
60 |
- .replace("@PROJECT_VERSION_MINOR@", &version.minor.to_string())
|
|
61 |
- .replace("@PROJECT_VERSION_GIT@", &version.to_string());
|
81 | 62 |
-
|
82 | 63 |
- let mut file = fs::File::create(&out)?;
|
83 | 64 |
- file.write_all(data.as_bytes())?;
|
|
86 | 67 |
-}
|
87 | 68 |
-
|
88 | 69 |
fn main() {
|
|
70 |
- if env::var("USE_SYSTEM_LIBNITROKEY").is_ok() {
|
|
71 |
- println!("cargo:rustc-link-lib=nitrokey");
|
|
72 |
- return;
|
|
73 |
- }
|
|
74 |
-
|
89 | 75 |
- let out_dir = env::var("OUT_DIR").expect("Environment variable OUT_DIR is not set");
|
90 | 76 |
- let out_path = path::PathBuf::from(out_dir);
|
91 | |
-
|
92 | |
- let version = get_version().expect("Could not extract library version");
|
93 | 77 |
-
|
94 | 78 |
- let sources = [
|
95 | 79 |
- "DeviceCommunicationExceptions.cpp",
|
|
100 | 84 |
- "log.cc",
|
101 | 85 |
- "misc.cc",
|
102 | 86 |
- ];
|
103 | |
- let library_dir = format!("libnitrokey-{}", version.git);
|
|
87 |
- let library_dir = format!("libnitrokey-{}", LIBNITROKEY_VERSION.to_string());
|
104 | 88 |
- let library_path = path::Path::new(&library_dir);
|
105 | 89 |
-
|
106 | |
- let version_source = prepare_version_source(&version, &out_path, &library_path)
|
|
90 |
- let version_source = prepare_version_source(LIBNITROKEY_VERSION, &out_path, &library_path)
|
107 | 91 |
- .expect("Could not prepare the version source file");
|
108 | 92 |
-
|
109 | 93 |
- cc::Build::new()
|
110 | 94 |
- .cpp(true)
|
|
95 |
- .flag("-std=c++14")
|
111 | 96 |
- .include(library_path.join("libnitrokey"))
|
112 | 97 |
- .files(sources.iter().map(|s| library_path.join(s)))
|
113 | 98 |
- .file(version_source)
|
114 | 99 |
- .compile("libnitrokey.a");
|
115 | 100 |
-
|
116 | |
- println!("cargo:rustc-link-lib=hidapi-libusb");
|
117 | |
+ println!("cargo:rustc-link-lib=nitrokey");
|
|
101 |
- let hidapi_library_name = if cfg!(target_os = "linux") {
|
|
102 |
- "hidapi-libusb"
|
|
103 |
- } else {
|
|
104 |
- "hidapi"
|
|
105 |
- };
|
|
106 |
- println!("cargo:rustc-link-lib={}", hidapi_library_name);
|
|
107 |
+ println!("cargo:rustc-link-lib=libnitrokey");
|
118 | 108 |
}
|