Added build script to use git for the VERSION string

This commit is contained in:
Lionel Flandrin 2016-11-08 20:32:09 +01:00
parent 9848d673e3
commit 02a04f396b
3 changed files with 63 additions and 8 deletions

View file

@ -1,10 +1,12 @@
[package]
name = "rustation"
# Exported as VERSION and VERSION_CSTR in the library
version = "0.0.2"
authors = ["Lionel Flandrin <lionel.flandrin@gmail.com>"]
# Should match the latest git tag! This version info is used to set
# the internal `VERSION` string if "git describe" is unavailable.
version = "0.0.2"
authors = ["Lionel Flandrin <lionel.flandrin@gmail.com>"]
description = "A PlayStation emulator"
homepage = "https://github.com/simias/rustation"
repository = "https://github.com/simias/rustation"
@ -12,6 +14,7 @@ documentation = "https://github.com/simias/rustation/wiki"
readme = "README.md"
license = "GPL-2.0+"
keywords = ["emulator", "playstation"]
build = "build.rs"
[features]
trace = [ "lazy_static" ]

51
build.rs Normal file
View file

@ -0,0 +1,51 @@
//! Get the version string using `git describe --dirty` or, if it
//! fails, using the `CARGO_PKG_VERSION`.
//!
//! The `GIT` environment variable can be used to set an alternative
//! path to the git executable.
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::process::Command;
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("version.rs");
let mut f = File::create(&dest_path).unwrap();
let git =
env::var("GIT").unwrap_or("git".into());
let description =
Command::new(git)
.arg("describe")
.arg("--dirty")
.output();
let cargo_version = env!("CARGO_PKG_VERSION").to_owned();
let mut version =
match description {
Ok(output) => {
if output.status.success() {
format!("git-{}",
String::from_utf8(output.stdout).unwrap())
} else {
cargo_version
}
}
_ => cargo_version,
};
// Make sure version is on a single line
if let Some(l) = version.find('\n') {
version.truncate(l);
}
writeln!(f, "pub const VERSION: &'static str = \
\"{}\";", version).unwrap();
writeln!(f, "pub const VERSION_CSTR: &'static str = \
\"{}\\0\";", version).unwrap();
}

View file

@ -33,9 +33,10 @@ mod timekeeper;
mod spu;
mod mdec;
/// Version of the rustation library set in Cargo.toml
pub const VERSION: &'static str = env!("CARGO_PKG_VERSION");
mod version {
// VERSION and VERSION_CSTR are generated by build.rs
include!(concat!(env!("OUT_DIR"), "/version.rs"));
}
/// Like VERSION but as a `\0`-terminated C string. Useful when you
/// need a static string in C bindings.
pub const VERSION_CSTR: &'static str = concat!(env!("CARGO_PKG_VERSION"), '\0');
pub use version::VERSION;
pub use version::VERSION_CSTR;