build.rs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // 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(query_logger)");
  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. #[cfg(all(not(debug_assertions), feature = "query_logger"))]
  30. compile_error!("Query Logging is only allowed during development, it is not intended for production usage!");
  31. // Support $BWRS_VERSION for legacy compatibility, but default to $VW_VERSION.
  32. // If neither exist, read from git.
  33. let maybe_vaultwarden_version =
  34. env::var("VW_VERSION").or_else(|_| env::var("BWRS_VERSION")).or_else(|_| version_from_git_info());
  35. if let Ok(version) = maybe_vaultwarden_version {
  36. println!("cargo:rustc-env=VW_VERSION={version}");
  37. println!("cargo:rustc-env=CARGO_PKG_VERSION={version}");
  38. }
  39. }
  40. fn run(args: &[&str]) -> Result<String, std::io::Error> {
  41. let out = Command::new(args[0]).args(&args[1..]).output()?;
  42. if !out.status.success() {
  43. use std::io::{Error, ErrorKind};
  44. return Err(Error::new(ErrorKind::Other, "Command not successful"));
  45. }
  46. Ok(String::from_utf8(out.stdout).unwrap().trim().to_string())
  47. }
  48. /// This method reads info from Git, namely tags, branch, and revision
  49. /// To access these values, use:
  50. /// - `env!("GIT_EXACT_TAG")`
  51. /// - `env!("GIT_LAST_TAG")`
  52. /// - `env!("GIT_BRANCH")`
  53. /// - `env!("GIT_REV")`
  54. /// - `env!("VW_VERSION")`
  55. fn version_from_git_info() -> Result<String, std::io::Error> {
  56. // The exact tag for the current commit, can be empty when
  57. // the current commit doesn't have an associated tag
  58. let exact_tag = run(&["git", "describe", "--abbrev=0", "--tags", "--exact-match"]).ok();
  59. if let Some(ref exact) = exact_tag {
  60. println!("cargo:rustc-env=GIT_EXACT_TAG={exact}");
  61. }
  62. // The last available tag, equal to exact_tag when
  63. // the current commit is tagged
  64. let last_tag = run(&["git", "describe", "--abbrev=0", "--tags"])?;
  65. println!("cargo:rustc-env=GIT_LAST_TAG={last_tag}");
  66. // The current branch name
  67. let branch = run(&["git", "rev-parse", "--abbrev-ref", "HEAD"])?;
  68. println!("cargo:rustc-env=GIT_BRANCH={branch}");
  69. // The current git commit hash
  70. let rev = run(&["git", "rev-parse", "HEAD"])?;
  71. let rev_short = rev.get(..8).unwrap_or_default();
  72. println!("cargo:rustc-env=GIT_REV={rev_short}");
  73. // Combined version
  74. if let Some(exact) = exact_tag {
  75. Ok(exact)
  76. } else if &branch != "main" && &branch != "master" && &branch != "HEAD" {
  77. Ok(format!("{last_tag}-{rev_short} ({branch})"))
  78. } else {
  79. Ok(format!("{last_tag}-{rev_short}"))
  80. }
  81. }