build.rs 856 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. extern crate rand;
  2. extern crate vergen;
  3. use rand::Rng;
  4. use rand::distributions::Alphanumeric;
  5. use std::env;
  6. use std::fs::OpenOptions;
  7. use std::io::Write;
  8. use std::path::PathBuf;
  9. fn main() {
  10. let out = PathBuf::from(env::var("OUT_DIR").unwrap());
  11. vergen::vergen(vergen::OutputFns::all()).unwrap();
  12. let mut rng = rand::thread_rng();
  13. let build_id: String = ::std::iter::repeat(()).map(|()| rng.sample(Alphanumeric)).take(8).collect();
  14. let mut version_file = OpenOptions::new()
  15. .write(true)
  16. .append(true)
  17. .open(&out.join("version.rs"))
  18. .unwrap();
  19. let build_id_fn = format!(
  20. "
  21. /// Generate a random build id.
  22. pub fn build_id() -> &'static str {{
  23. \"{}\"
  24. }}
  25. ",
  26. build_id
  27. );
  28. if let Err(e) = version_file.write_all(build_id_fn.as_bytes()) {
  29. println!("{}", e);
  30. }
  31. }