build.rs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. use std::process::Command;
  2. use std::env;
  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(not(any(feature = "sqlite", feature = "mysql", feature = "postgresql")))]
  12. compile_error!("You need to enable one DB backend. To build with previous defaults do: cargo build --features sqlite");
  13. if let Ok(version) = env::var("BWRS_VERSION") {
  14. println!("cargo:rustc-env=BWRS_VERSION={}", version);
  15. println!("cargo:rustc-env=CARGO_PKG_VERSION={}", version);
  16. } else {
  17. read_git_info().ok();
  18. }
  19. }
  20. fn run(args: &[&str]) -> Result<String, std::io::Error> {
  21. let out = Command::new(args[0]).args(&args[1..]).output()?;
  22. if !out.status.success() {
  23. use std::io::{Error, ErrorKind};
  24. return Err(Error::new(ErrorKind::Other, "Command not successful"));
  25. }
  26. Ok(String::from_utf8(out.stdout).unwrap().trim().to_string())
  27. }
  28. /// This method reads info from Git, namely tags, branch, and revision
  29. fn read_git_info() -> Result<(), std::io::Error> {
  30. // The exact tag for the current commit, can be empty when
  31. // the current commit doesn't have an associated tag
  32. let exact_tag = run(&["git", "describe", "--abbrev=0", "--tags", "--exact-match"]).ok();
  33. if let Some(ref exact) = exact_tag {
  34. println!("cargo:rustc-env=GIT_EXACT_TAG={}", exact);
  35. }
  36. // The last available tag, equal to exact_tag when
  37. // the current commit is tagged
  38. let last_tag = run(&["git", "describe", "--abbrev=0", "--tags"])?;
  39. println!("cargo:rustc-env=GIT_LAST_TAG={}", last_tag);
  40. // The current branch name
  41. let branch = run(&["git", "rev-parse", "--abbrev-ref", "HEAD"])?;
  42. println!("cargo:rustc-env=GIT_BRANCH={}", branch);
  43. // The current git commit hash
  44. let rev = run(&["git", "rev-parse", "HEAD"])?;
  45. let rev_short = rev.get(..8).unwrap_or_default();
  46. println!("cargo:rustc-env=GIT_REV={}", rev_short);
  47. // Combined version
  48. let version = if let Some(exact) = exact_tag {
  49. exact
  50. } else if &branch != "master" {
  51. format!("{}-{} ({})", last_tag, rev_short, branch)
  52. } else {
  53. format!("{}-{}", last_tag, rev_short)
  54. };
  55. println!("cargo:rustc-env=BWRS_VERSION={}", version);
  56. println!("cargo:rustc-env=CARGO_PKG_VERSION={}", version);
  57. // To access these values, use:
  58. // env!("GIT_EXACT_TAG")
  59. // env!("GIT_LAST_TAG")
  60. // env!("GIT_BRANCH")
  61. // env!("GIT_REV")
  62. // env!("BWRS_VERSION")
  63. Ok(())
  64. }