build.rs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Specifies the minimum version needed to compile Ion.
  2. // NOTE: 1.19 is required due to the usage of `break` with values for
  3. // `loop` (RFC 1624, rust-lang/rust GitHub issue #37339).
  4. // const MIN_VERSION: &'static str = "1.19.0";
  5. use std::{
  6. env,
  7. fs::{self, File},
  8. io::{self, Read, Write},
  9. path::Path,
  10. process::Command,
  11. };
  12. fn main() {
  13. match write_version_file() {
  14. Ok(_) => {}
  15. Err(e) => panic!("Failed to create a version file: {:?}", e),
  16. }
  17. }
  18. fn write_version_file() -> io::Result<()> {
  19. let version = env::var("CARGO_PKG_VERSION").unwrap();
  20. let target = env::var("TARGET").unwrap();
  21. let version_fname = Path::new(&env::var("OUT_DIR").unwrap()).join("version_string");
  22. let mut version_file = File::create(&version_fname)?;
  23. write!(
  24. &mut version_file,
  25. "r#\"ion {} ({})\nrev {}\"#",
  26. version,
  27. target,
  28. get_git_rev()?.trim()
  29. )?;
  30. Ok(())
  31. }
  32. fn get_git_rev() -> io::Result<String> {
  33. let version_file = Path::new("git_revision.txt");
  34. if version_file.exists() {
  35. fs::read_to_string(&version_file)
  36. } else {
  37. Command::new("git")
  38. .arg("rev-parse")
  39. .arg("master")
  40. .output()
  41. .and_then(|out| {
  42. String::from_utf8(out.stdout).map_err(|_| {
  43. io::Error::new(
  44. io::ErrorKind::InvalidData,
  45. "git rev-parse master output was not UTF-8",
  46. )
  47. })
  48. })
  49. .or_else(|_| git_rev_from_file())
  50. }
  51. }
  52. fn git_rev_from_file() -> io::Result<String> {
  53. let git_file = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap())
  54. .join(".git")
  55. .join("refs")
  56. .join("heads")
  57. .join("master");
  58. let mut file = File::open(git_file)?;
  59. let mut rev = String::new();
  60. file.read_to_string(&mut rev)?;
  61. Ok(rev)
  62. }