From 4615f5ecf96ef4637d814a4dfeab9c404b4a3667 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Mon, 10 Dec 2018 12:42:48 +0000 Subject: Update to libnitrokey v3.4 Besides the changes listed in the changelog, this patch also changes the build system to be able to generate the version.cc file containing the library version. --- build.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'build.rs') diff --git a/build.rs b/build.rs index e44b3c0..162cbbd 100644 --- a/build.rs +++ b/build.rs @@ -1,6 +1,9 @@ extern crate cc; use std::env; +use std::io; +use std::io::{Read, Write}; +use std::fs; use std::path; struct Version { @@ -41,7 +44,34 @@ fn get_version() -> Result { }) } +fn prepare_version_source( + version: &Version, + out_path: &path::Path, + library_path: &path::Path +) -> io::Result { + 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) + .replace("@PROJECT_VERSION_MINOR@", &version.minor) + .replace("@PROJECT_VERSION_GIT@", &version.git); + + let mut file = fs::File::create(&out)?; + file.write_all(data.as_bytes())?; + + Ok(out) +} + fn main() { + let out_dir = env::var("OUT_DIR").expect("Environment variable OUT_DIR is not set"); + let out_path = path::PathBuf::from(out_dir); + let version = get_version().expect("Could not extract library version"); let sources = [ @@ -56,10 +86,14 @@ fn main() { let library_dir = format!("libnitrokey-{}", version.git); let library_path = path::Path::new(&library_dir); + let version_source = prepare_version_source(&version, &out_path, &library_path) + .expect("Could not prepare the version source file"); + cc::Build::new() .cpp(true) .include(library_path.join("libnitrokey")) .files(sources.iter().map(|s| library_path.join(s))) + .file(version_source) .compile("libnitrokey.a"); println!("cargo:rustc-link-lib=hidapi-libusb"); -- cgit v1.2.1