| 1 |
use std::env; |
| 2 |
use std::fs; |
| 3 |
use std::io; |
| 4 |
use std::io::{Read, Write}; |
| 5 |
use std::path; |
| 6 |
use std::string; |
| 7 |
|
| 8 |
use cc; |
| 9 |
|
| 10 |
#[derive(Clone, Copy, Debug, PartialEq)] |
| 11 |
struct Version { |
| 12 |
major: u32, |
| 13 |
minor: u32, |
| 14 |
patch: Option<u32>, |
| 15 |
} |
| 16 |
|
| 17 |
impl string::ToString for Version { |
| 18 |
fn to_string(&self) -> String { |
| 19 |
match self.patch { |
| 20 |
Some(patch) => format!("v{}.{}.{}", self.major, self.minor, patch), |
| 21 |
None => format!("v{}.{}", self.major, self.minor), |
| 22 |
} |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
const LIBNITROKEY_VERSION: Version = Version { |
| 27 |
major: 3, |
| 28 |
minor: 4, |
| 29 |
patch: Some(1), |
| 30 |
}; |
| 31 |
|
| 32 |
fn prepare_version_source( |
| 33 |
version: Version, |
| 34 |
out_path: &path::Path, |
| 35 |
library_path: &path::Path, |
| 36 |
) -> io::Result<path::PathBuf> { |
| 37 |
let out = out_path.join("version.cc"); |
| 38 |
let template = library_path.join("version.cc.in"); |
| 39 |
|
| 40 |
let mut file = fs::File::open(template)?; |
| 41 |
let mut data = String::new(); |
| 42 |
file.read_to_string(&mut data)?; |
| 43 |
drop(file); |
| 44 |
|
| 45 |
let data = data |
| 46 |
.replace("@PROJECT_VERSION_MAJOR@", &version.major.to_string()) |
| 47 |
.replace("@PROJECT_VERSION_MINOR@", &version.minor.to_string()) |
| 48 |
.replace("@PROJECT_VERSION_GIT@", &version.to_string()); |
| 49 |
|
| 50 |
let mut file = fs::File::create(&out)?; |
| 51 |
file.write_all(data.as_bytes())?; |
| 52 |
|
| 53 |
Ok(out) |
| 54 |
} |
| 55 |
|
| 56 |
fn main() { |
| 57 |
if env::var("USE_SYSTEM_LIBNITROKEY").is_ok() { |
| 58 |
println!("cargo:rustc-link-lib=nitrokey"); |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
let out_dir = env::var("OUT_DIR").expect("Environment variable OUT_DIR is not set"); |
| 63 |
let out_path = path::PathBuf::from(out_dir); |
| 64 |
|
| 65 |
let sources = [ |
| 66 |
"DeviceCommunicationExceptions.cpp", |
| 67 |
"NK_C_API.cc", |
| 68 |
"NitrokeyManager.cc", |
| 69 |
"command_id.cc", |
| 70 |
"device.cc", |
| 71 |
"log.cc", |
| 72 |
"misc.cc", |
| 73 |
]; |
| 74 |
let library_dir = format!("libnitrokey-{}", LIBNITROKEY_VERSION.to_string()); |
| 75 |
let library_path = path::Path::new(&library_dir); |
| 76 |
|
| 77 |
let version_source = prepare_version_source(LIBNITROKEY_VERSION, &out_path, &library_path) |
| 78 |
.expect("Could not prepare the version source file"); |
| 79 |
|
| 80 |
cc::Build::new() |
| 81 |
.cpp(true) |
| 82 |
.flag("-std=c++14") |
| 83 |
.include(library_path.join("libnitrokey")) |
| 84 |
.files(sources.iter().map(|s| library_path.join(s))) |
| 85 |
.file(version_source) |
| 86 |
.compile("libnitrokey.a"); |
| 87 |
|
| 88 |
let hidapi_library_name = if cfg!(target_os = "linux") { |
| 89 |
"hidapi-libusb" |
| 90 |
} else { |
| 91 |
"hidapi" |
| 92 |
}; |
| 93 |
println!("cargo:rustc-link-lib={}", hidapi_library_name); |
| 94 |
} |