build.rs 3.8 KB

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