2020-03-22 18:13:34 +03:00
|
|
|
use std::env;
|
2021-03-31 23:18:35 +03:00
|
|
|
use std::process::Command;
|
2018-12-30 02:04:22 +03:00
|
|
|
|
2021-03-31 23:18:35 +03:00
|
|
|
fn main() {
|
2020-08-18 18:15:44 +03: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");
|
2022-12-03 20:25:59 +03:00
|
|
|
#[cfg(feature = "query_logger")]
|
|
|
|
println!("cargo:rustc-cfg=query_logger");
|
2019-07-09 18:26:34 +03:00
|
|
|
|
2019-09-12 23:12:22 +03:00
|
|
|
#[cfg(not(any(feature = "sqlite", feature = "mysql", feature = "postgresql")))]
|
2021-03-31 23:18:35 +03:00
|
|
|
compile_error!(
|
|
|
|
"You need to enable one DB backend. To build with previous defaults do: cargo build --features sqlite"
|
|
|
|
);
|
|
|
|
|
2023-12-10 01:04:33 +03:00
|
|
|
// Rerun when these paths are changed.
|
|
|
|
// Someone could have checked-out a tag or specific commit, but no other files changed.
|
|
|
|
println!("cargo:rerun-if-changed=.git");
|
|
|
|
println!("cargo:rerun-if-changed=.git/HEAD");
|
|
|
|
println!("cargo:rerun-if-changed=.git/index");
|
|
|
|
println!("cargo:rerun-if-changed=.git/refs/tags");
|
|
|
|
|
2022-12-03 20:25:59 +03:00
|
|
|
#[cfg(all(not(debug_assertions), feature = "query_logger"))]
|
2023-10-05 20:08:26 +03:00
|
|
|
compile_error!("Query Logging is only allowed during development, it is not intended for production usage!");
|
2022-12-03 20:25:59 +03:00
|
|
|
|
2022-01-24 01:40:59 +03:00
|
|
|
// Support $BWRS_VERSION for legacy compatibility, but default to $VW_VERSION.
|
|
|
|
// If neither exist, read from git.
|
|
|
|
let maybe_vaultwarden_version =
|
|
|
|
env::var("VW_VERSION").or_else(|_| env::var("BWRS_VERSION")).or_else(|_| version_from_git_info());
|
|
|
|
|
|
|
|
if let Ok(version) = maybe_vaultwarden_version {
|
2022-12-03 20:25:59 +03:00
|
|
|
println!("cargo:rustc-env=VW_VERSION={version}");
|
|
|
|
println!("cargo:rustc-env=CARGO_PKG_VERSION={version}");
|
2020-03-22 18:13:34 +03:00
|
|
|
}
|
2018-12-30 02:04:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(args: &[&str]) -> Result<String, std::io::Error> {
|
|
|
|
let out = Command::new(args[0]).args(&args[1..]).output()?;
|
2019-02-10 17:46:51 +03:00
|
|
|
if !out.status.success() {
|
|
|
|
use std::io::{Error, ErrorKind};
|
|
|
|
return Err(Error::new(ErrorKind::Other, "Command not successful"));
|
|
|
|
}
|
2018-12-30 02:04:22 +03:00
|
|
|
Ok(String::from_utf8(out.stdout).unwrap().trim().to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This method reads info from Git, namely tags, branch, and revision
|
2022-01-24 01:40:59 +03:00
|
|
|
/// To access these values, use:
|
|
|
|
/// - env!("GIT_EXACT_TAG")
|
|
|
|
/// - env!("GIT_LAST_TAG")
|
|
|
|
/// - env!("GIT_BRANCH")
|
|
|
|
/// - env!("GIT_REV")
|
|
|
|
/// - env!("VW_VERSION")
|
|
|
|
fn version_from_git_info() -> Result<String, std::io::Error> {
|
2018-12-30 02:04:22 +03:00
|
|
|
// The exact tag for the current commit, can be empty when
|
|
|
|
// the current commit doesn't have an associated tag
|
2019-02-10 17:46:51 +03:00
|
|
|
let exact_tag = run(&["git", "describe", "--abbrev=0", "--tags", "--exact-match"]).ok();
|
|
|
|
if let Some(ref exact) = exact_tag {
|
2022-12-03 20:25:59 +03:00
|
|
|
println!("cargo:rustc-env=GIT_EXACT_TAG={exact}");
|
2019-02-10 17:46:51 +03:00
|
|
|
}
|
2018-12-30 02:04:22 +03:00
|
|
|
|
|
|
|
// The last available tag, equal to exact_tag when
|
|
|
|
// the current commit is tagged
|
|
|
|
let last_tag = run(&["git", "describe", "--abbrev=0", "--tags"])?;
|
2022-12-03 20:25:59 +03:00
|
|
|
println!("cargo:rustc-env=GIT_LAST_TAG={last_tag}");
|
2018-12-30 02:04:22 +03:00
|
|
|
|
|
|
|
// The current branch name
|
|
|
|
let branch = run(&["git", "rev-parse", "--abbrev-ref", "HEAD"])?;
|
2022-12-03 20:25:59 +03:00
|
|
|
println!("cargo:rustc-env=GIT_BRANCH={branch}");
|
2018-12-30 02:04:22 +03:00
|
|
|
|
|
|
|
// The current git commit hash
|
|
|
|
let rev = run(&["git", "rev-parse", "HEAD"])?;
|
2019-02-10 17:46:51 +03:00
|
|
|
let rev_short = rev.get(..8).unwrap_or_default();
|
2022-12-03 20:25:59 +03:00
|
|
|
println!("cargo:rustc-env=GIT_REV={rev_short}");
|
2018-12-30 02:04:22 +03:00
|
|
|
|
2019-02-10 17:46:51 +03:00
|
|
|
// Combined version
|
2022-01-24 01:40:59 +03:00
|
|
|
if let Some(exact) = exact_tag {
|
|
|
|
Ok(exact)
|
2023-07-05 01:08:52 +03:00
|
|
|
} else if &branch != "main" && &branch != "master" && &branch != "HEAD" {
|
2022-12-03 20:25:59 +03:00
|
|
|
Ok(format!("{last_tag}-{rev_short} ({branch})"))
|
2019-02-10 17:46:51 +03:00
|
|
|
} else {
|
2022-12-03 20:25:59 +03:00
|
|
|
Ok(format!("{last_tag}-{rev_short}"))
|
2022-01-24 01:40:59 +03:00
|
|
|
}
|
2018-12-30 02:04:22 +03:00
|
|
|
}
|