build.rs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. use std::env;
  2. use std::process::Command;
  3. fn main() {
  4. // This allow using #[cfg(sqlite)] instead of #[cfg(feature = "sqlite")], which helps when trying to add them through macros
  5. #[cfg(feature = "sqlite")]
  6. println!("cargo:rustc-cfg=sqlite");
  7. #[cfg(feature = "mysql")]
  8. println!("cargo:rustc-cfg=mysql");
  9. #[cfg(feature = "postgresql")]
  10. println!("cargo:rustc-cfg=postgresql");
  11. #[cfg(feature = "query_logger")]
  12. println!("cargo:rustc-cfg=query_logger");
  13. #[cfg(not(any(feature = "sqlite", feature = "mysql", feature = "postgresql")))]
  14. compile_error!(
  15. "You need to enable one DB backend. To build with previous defaults do: cargo build --features sqlite"
  16. );
  17. #[cfg(all(not(debug_assertions), feature = "query_logger"))]
  18. compile_error!("Query Logging is only allowed during development, it is not intented for production usage!");
  19. // Support $BWRS_VERSION for legacy compatibility, but default to $VW_VERSION.
  20. // If neither exist, read from git.
  21. let maybe_vaultwarden_version =
  22. env::var("VW_VERSION").or_else(|_| env::var("BWRS_VERSION")).or_else(|_| version_from_git_info());
  23. if let Ok(version) = maybe_vaultwarden_version {
  24. println!("cargo:rustc-env=VW_VERSION={version}");
  25. println!("cargo:rustc-env=CARGO_PKG_VERSION={version}");
  26. }
  27. }
  28. fn run(args: &[&str]) -> Result<String, std::io::Error> {
  29. let out = Command::new(args[0]).args(&args[1..]).output()?;
  30. if !out.status.success() {
  31. use std::io::{Error, ErrorKind};
  32. return Err(Error::new(ErrorKind::Other, "Command not successful"));
  33. }
  34. Ok(String::from_utf8(out.stdout).unwrap().trim().to_string())
  35. }
  36. /// This method reads info from Git, namely tags, branch, and revision
  37. /// To access these values, use:
  38. /// - env!("GIT_EXACT_TAG")
  39. /// - env!("GIT_LAST_TAG")
  40. /// - env!("GIT_BRANCH")
  41. /// - env!("GIT_REV")
  42. /// - env!("VW_VERSION")
  43. fn version_from_git_info() -> Result<String, std::io::Error> {
  44. // The exact tag for the current commit, can be empty when
  45. // the current commit doesn't have an associated tag
  46. let exact_tag = run(&["git", "describe", "--abbrev=0", "--tags", "--exact-match"]).ok();
  47. if let Some(ref exact) = exact_tag {
  48. println!("cargo:rustc-env=GIT_EXACT_TAG={exact}");
  49. }
  50. // The last available tag, equal to exact_tag when
  51. // the current commit is tagged
  52. let last_tag = run(&["git", "describe", "--abbrev=0", "--tags"])?;
  53. println!("cargo:rustc-env=GIT_LAST_TAG={last_tag}");
  54. // The current branch name
  55. let branch = run(&["git", "rev-parse", "--abbrev-ref", "HEAD"])?;
  56. println!("cargo:rustc-env=GIT_BRANCH={branch}");
  57. // The current git commit hash
  58. let rev = run(&["git", "rev-parse", "HEAD"])?;
  59. let rev_short = rev.get(..8).unwrap_or_default();
  60. println!("cargo:rustc-env=GIT_REV={rev_short}");
  61. // Combined version
  62. if let Some(exact) = exact_tag {
  63. Ok(exact)
  64. } else if &branch != "main" && &branch != "master" {
  65. Ok(format!("{last_tag}-{rev_short} ({branch})"))
  66. } else {
  67. Ok(format!("{last_tag}-{rev_short}"))
  68. }
  69. }