build.rs 929 B

1234567891011121314151617181920212223242526272829303132333435
  1. use std::env;
  2. use std::io::Write;
  3. use std::process::{Command, Stdio};
  4. fn main() {
  5. if probe("fn main() { 0i128; }") {
  6. println!("cargo:rustc-cfg=has_i128");
  7. } else if env::var_os("CARGO_FEATURE_I128").is_some() {
  8. panic!("i128 support was not detected!");
  9. }
  10. }
  11. /// Test if a code snippet can be compiled
  12. fn probe(code: &str) -> bool {
  13. let rustc = env::var_os("RUSTC").unwrap_or_else(|| "rustc".into());
  14. let out_dir = env::var_os("OUT_DIR").expect("environment variable OUT_DIR");
  15. let mut child = Command::new(rustc)
  16. .arg("--out-dir")
  17. .arg(out_dir)
  18. .arg("--emit=obj")
  19. .arg("-")
  20. .stdin(Stdio::piped())
  21. .spawn()
  22. .expect("rustc probe");
  23. child
  24. .stdin
  25. .as_mut()
  26. .expect("rustc stdin")
  27. .write_all(code.as_bytes())
  28. .expect("write rustc stdin");
  29. child.wait().expect("rustc probe").success()
  30. }