diff options
author | Robin Krahl <me@robin-krahl.de> | 2018-07-05 19:19:55 +0200 |
---|---|---|
committer | Robin Krahl <me@robin-krahl.de> | 2018-07-05 19:19:55 +0200 |
commit | b126c5ba1e5a9f00859868d75900043d02a0031c (patch) | |
tree | d35fcce5322caf414d3cd3a04a8999ef78e32a08 | |
parent | 4cf21377525af162b3117523673dafab12bc388e (diff) | |
download | nitrokey-sys-rs-b126c5ba1e5a9f00859868d75900043d02a0031c.tar.gz nitrokey-sys-rs-b126c5ba1e5a9f00859868d75900043d02a0031c.tar.bz2 |
Extract libnitrokey version from crate version
This patch uses the nitrokey-sys-rs crate version to extract the version
of the contained libnitrokey and to deduce the name of the libnitrokey
directory.
The implementation uses these assumptions:
1. The crate major and minor version match the libnitrokey major and
minor version.
2. If the crate has a pre-release version, it is assumed that this
version has the format pre.<d>, where <d> is the output of `git
describe` with all dashes replaced by dots.
3. The name of the libnitrokey directory is libnitrokey-<tag>, where
<tag> is the corresponding Git version tag.
4. libnitrokey uses version tags of the format v<maj>.<min>, where <maj>
the major version and <min> is the minor version.
-rw-r--r-- | build.rs | 50 |
1 files changed, 46 insertions, 4 deletions
@@ -1,8 +1,49 @@ extern crate cc; -use std::path::Path; +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", @@ -12,12 +53,13 @@ fn main() { "log.cc", "misc.cc", ]; - let library_dir = Path::new("libnitrokey-v3.3"); + let library_dir = format!("libnitrokey-{}", version.git); + let library_path = path::Path::new(&library_dir); cc::Build::new() .cpp(true) - .include(library_dir.join("libnitrokey")) - .files(sources.iter().map(|s| library_dir.join(s))) + .include(library_path.join("libnitrokey")) + .files(sources.iter().map(|s| library_path.join(s))) .compile("libnitrokey.a"); println!("cargo:rustc-link-lib=hidapi-libusb"); |