build.rs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 = "s3")]
  12. println!("cargo:rustc-cfg=s3");
  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. // Use check-cfg to let cargo know which cfg's we define,
  18. // and avoid warnings when they are used in the code.
  19. println!("cargo::rustc-check-cfg=cfg(sqlite)");
  20. println!("cargo::rustc-check-cfg=cfg(mysql)");
  21. println!("cargo::rustc-check-cfg=cfg(postgresql)");
  22. println!("cargo::rustc-check-cfg=cfg(s3)");
  23. // Rerun when these paths are changed.
  24. // Someone could have checked-out a tag or specific commit, but no other files changed.
  25. println!("cargo:rerun-if-changed=.git");
  26. println!("cargo:rerun-if-changed=.git/HEAD");
  27. println!("cargo:rerun-if-changed=.git/index");
  28. println!("cargo:rerun-if-changed=.git/refs/tags");
  29. // Support $BWRS_VERSION for legacy compatibility, but default to $VW_VERSION.
  30. // If neither exist, read from git.
  31. let maybe_vaultwarden_version =
  32. env::var("VW_VERSION").or_else(|_| env::var("BWRS_VERSION")).or_else(|_| version_from_git_info());
  33. if let Ok(version) = maybe_vaultwarden_version {
  34. println!("cargo:rustc-env=VW_VERSION={version}");
  35. println!("cargo:rustc-env=CARGO_PKG_VERSION={version}");
  36. }
  37. }
  38. fn run(args: &[&str]) -> Result<String, std::io::Error> {
  39. let out = Command::new(args[0]).args(&args[1..]).output()?;
  40. if !out.status.success() {
  41. use std::io::Error;
  42. return Err(Error::other("Command not successful"));
  43. }
  44. Ok(String::from_utf8(out.stdout).unwrap().trim().to_string())
  45. }
  46. /// This method reads info from Git, namely tags, branch, and revision
  47. /// To access these values, use:
  48. /// - `env!("GIT_EXACT_TAG")`
  49. /// - `env!("GIT_LAST_TAG")`
  50. /// - `env!("GIT_BRANCH")`
  51. /// - `env!("GIT_REV")`
  52. /// - `env!("VW_VERSION")`
  53. fn version_from_git_info() -> Result<String, std::io::Error> {
  54. // The exact tag for the current commit, can be empty when
  55. // the current commit doesn't have an associated tag
  56. let exact_tag = run(&["git", "describe", "--abbrev=0", "--tags", "--exact-match"]).ok();
  57. if let Some(ref exact) = exact_tag {
  58. println!("cargo:rustc-env=GIT_EXACT_TAG={exact}");
  59. }
  60. // The last available tag, equal to exact_tag when
  61. // the current commit is tagged
  62. let last_tag = run(&["git", "describe", "--abbrev=0", "--tags"])?;
  63. println!("cargo:rustc-env=GIT_LAST_TAG={last_tag}");
  64. // The current branch name
  65. let branch = run(&["git", "rev-parse", "--abbrev-ref", "HEAD"])?;
  66. println!("cargo:rustc-env=GIT_BRANCH={branch}");
  67. // The current git commit hash
  68. let rev = run(&["git", "rev-parse", "HEAD"])?;
  69. let rev_short = rev.get(..8).unwrap_or_default();
  70. println!("cargo:rustc-env=GIT_REV={rev_short}");
  71. // Combined version
  72. if let Some(exact) = exact_tag {
  73. Ok(exact)
  74. } else if &branch != "main" && &branch != "master" && &branch != "HEAD" {
  75. Ok(format!("{last_tag}-{rev_short} ({branch})"))
  76. } else {
  77. Ok(format!("{last_tag}-{rev_short}"))
  78. }
  79. }