2020-03-22 15:13:34 +00:00
|
|
|
use std::env;
|
2021-03-31 20:18:35 +00:00
|
|
|
use std::process::Command;
|
2018-12-29 23:04:22 +00:00
|
|
|
|
2021-03-31 20:18:35 +00:00
|
|
|
fn main() {
|
2020-08-18 15:15:44 +00:00
|
|
|
// This allow using #[cfg(sqlite)] instead of #[cfg(feature = "sqlite")], which helps when trying to add them through macros
|
|
|
|
#[cfg(feature = "sqlite")]
|
|
|
|
println!("cargo:rustc-cfg=sqlite");
|
|
|
|
#[cfg(feature = "mysql")]
|
|
|
|
println!("cargo:rustc-cfg=mysql");
|
|
|
|
#[cfg(feature = "postgresql")]
|
|
|
|
println!("cargo:rustc-cfg=postgresql");
|
2019-07-09 15:26:34 +00:00
|
|
|
|
2019-09-12 20:12:22 +00:00
|
|
|
#[cfg(not(any(feature = "sqlite", feature = "mysql", feature = "postgresql")))]
|
2021-03-31 20:18:35 +00:00
|
|
|
compile_error!(
|
|
|
|
"You need to enable one DB backend. To build with previous defaults do: cargo build --features sqlite"
|
|
|
|
);
|
|
|
|
|
2020-03-22 15:13:34 +00:00
|
|
|
if let Ok(version) = env::var("BWRS_VERSION") {
|
|
|
|
println!("cargo:rustc-env=BWRS_VERSION={}", version);
|
|
|
|
println!("cargo:rustc-env=CARGO_PKG_VERSION={}", version);
|
|
|
|
} else {
|
|
|
|
read_git_info().ok();
|
|
|
|
}
|
2018-12-29 23:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(args: &[&str]) -> Result<String, std::io::Error> {
|
|
|
|
let out = Command::new(args[0]).args(&args[1..]).output()?;
|
2019-02-10 14:46:51 +00:00
|
|
|
if !out.status.success() {
|
|
|
|
use std::io::{Error, ErrorKind};
|
|
|
|
return Err(Error::new(ErrorKind::Other, "Command not successful"));
|
|
|
|
}
|
2018-12-29 23:04:22 +00:00
|
|
|
Ok(String::from_utf8(out.stdout).unwrap().trim().to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This method reads info from Git, namely tags, branch, and revision
|
|
|
|
fn read_git_info() -> Result<(), std::io::Error> {
|
|
|
|
// The exact tag for the current commit, can be empty when
|
|
|
|
// the current commit doesn't have an associated tag
|
2019-02-10 14:46:51 +00:00
|
|
|
let exact_tag = run(&["git", "describe", "--abbrev=0", "--tags", "--exact-match"]).ok();
|
|
|
|
if let Some(ref exact) = exact_tag {
|
|
|
|
println!("cargo:rustc-env=GIT_EXACT_TAG={}", exact);
|
|
|
|
}
|
2018-12-29 23:04:22 +00:00
|
|
|
|
|
|
|
// The last available tag, equal to exact_tag when
|
|
|
|
// the current commit is tagged
|
|
|
|
let last_tag = run(&["git", "describe", "--abbrev=0", "--tags"])?;
|
|
|
|
println!("cargo:rustc-env=GIT_LAST_TAG={}", last_tag);
|
|
|
|
|
|
|
|
// The current branch name
|
|
|
|
let branch = run(&["git", "rev-parse", "--abbrev-ref", "HEAD"])?;
|
|
|
|
println!("cargo:rustc-env=GIT_BRANCH={}", branch);
|
|
|
|
|
|
|
|
// The current git commit hash
|
|
|
|
let rev = run(&["git", "rev-parse", "HEAD"])?;
|
2019-02-10 14:46:51 +00:00
|
|
|
let rev_short = rev.get(..8).unwrap_or_default();
|
2018-12-29 23:04:22 +00:00
|
|
|
println!("cargo:rustc-env=GIT_REV={}", rev_short);
|
|
|
|
|
2019-02-10 14:46:51 +00:00
|
|
|
// Combined version
|
|
|
|
let version = if let Some(exact) = exact_tag {
|
|
|
|
exact
|
2021-04-28 19:46:20 +00:00
|
|
|
} else if &branch != "main" && &branch != "master" {
|
2019-02-10 14:46:51 +00:00
|
|
|
format!("{}-{} ({})", last_tag, rev_short, branch)
|
|
|
|
} else {
|
|
|
|
format!("{}-{}", last_tag, rev_short)
|
|
|
|
};
|
2021-03-31 20:18:35 +00:00
|
|
|
|
2020-03-22 15:13:34 +00:00
|
|
|
println!("cargo:rustc-env=BWRS_VERSION={}", version);
|
|
|
|
println!("cargo:rustc-env=CARGO_PKG_VERSION={}", version);
|
2019-02-10 14:46:51 +00:00
|
|
|
|
2018-12-29 23:04:22 +00:00
|
|
|
// To access these values, use:
|
|
|
|
// env!("GIT_EXACT_TAG")
|
|
|
|
// env!("GIT_LAST_TAG")
|
|
|
|
// env!("GIT_BRANCH")
|
|
|
|
// env!("GIT_REV")
|
2020-03-22 15:13:34 +00:00
|
|
|
// env!("BWRS_VERSION")
|
2019-02-10 14:46:51 +00:00
|
|
|
|
2018-12-29 23:04:22 +00:00
|
|
|
Ok(())
|
|
|
|
}
|