build.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. use std::env;
  2. use std::process::Command;
  3. use std::str;
  4. // The rustc-cfg strings below are *not* public API. Please let us know by
  5. // opening a GitHub issue if your build environment requires some way to enable
  6. // these cfgs other than by executing our build script.
  7. fn main() {
  8. let compiler = match rustc_version() {
  9. Some(compiler) => compiler,
  10. None => return,
  11. };
  12. if compiler.minor < 36 {
  13. println!("cargo:rustc-cfg=syn_omit_await_from_token_macro");
  14. }
  15. if compiler.minor < 39 {
  16. println!("cargo:rustc-cfg=syn_no_const_vec_new");
  17. }
  18. if compiler.minor < 40 {
  19. println!("cargo:rustc-cfg=syn_no_non_exhaustive");
  20. }
  21. if compiler.minor < 56 {
  22. println!("cargo:rustc-cfg=syn_no_negative_literal_parse");
  23. }
  24. if !compiler.nightly {
  25. println!("cargo:rustc-cfg=syn_disable_nightly_tests");
  26. }
  27. }
  28. struct Compiler {
  29. minor: u32,
  30. nightly: bool,
  31. }
  32. fn rustc_version() -> Option<Compiler> {
  33. let rustc = env::var_os("RUSTC")?;
  34. let output = Command::new(rustc).arg("--version").output().ok()?;
  35. let version = str::from_utf8(&output.stdout).ok()?;
  36. let mut pieces = version.split('.');
  37. if pieces.next() != Some("rustc 1") {
  38. return None;
  39. }
  40. let minor = pieces.next()?.parse().ok()?;
  41. let nightly = version.contains("nightly") || version.ends_with("-dev");
  42. Some(Compiler { minor, nightly })
  43. }