build.rs 2.5 KB

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