blob: 644bf75ffeeac6826528b31b0f21000fa76490a4 [file] [log] [blame]
David Tolnaye0ba9202018-10-06 20:09:08 -07001use std::env;
2use std::process::Command;
3use std::str::{self, FromStr};
4
5// The rustc-cfg strings below are *not* public API. Please let us know by
6// opening a GitHub issue if your build environment requires some way to enable
7// these cfgs other than by executing our build script.
8fn main() {
David Tolnayf83b6c92019-03-09 22:58:26 -08009 let compiler = match rustc_version() {
10 Some(compiler) => compiler,
David Tolnaye0ba9202018-10-06 20:09:08 -070011 None => return,
12 };
13
David Tolnayf83b6c92019-03-09 22:58:26 -080014 if compiler.minor >= 19 {
David Tolnay32874802018-11-11 08:52:19 -080015 println!("cargo:rustc-cfg=syn_can_use_thread_id");
16 }
17
David Tolnaye0ba9202018-10-06 20:09:08 -070018 // Macro modularization allows re-exporting the `quote!` macro in 1.30+.
David Tolnayf83b6c92019-03-09 22:58:26 -080019 if compiler.minor >= 30 {
David Tolnaye0ba9202018-10-06 20:09:08 -070020 println!("cargo:rustc-cfg=syn_can_call_macro_by_path");
21 }
David Tolnayf83b6c92019-03-09 22:58:26 -080022
23 if !compiler.nightly {
24 println!("cargo:rustc-cfg=syn_disable_nightly_tests");
25 }
David Tolnaye0ba9202018-10-06 20:09:08 -070026}
27
David Tolnayf83b6c92019-03-09 22:58:26 -080028struct Compiler {
29 minor: u32,
30 nightly: bool,
31}
32
33fn rustc_version() -> Option<Compiler> {
David Tolnaye0ba9202018-10-06 20:09:08 -070034 let rustc = match env::var_os("RUSTC") {
35 Some(rustc) => rustc,
36 None => return None,
37 };
38
39 let output = match Command::new(rustc).arg("--version").output() {
40 Ok(output) => output,
41 Err(_) => return None,
42 };
43
44 let version = match str::from_utf8(&output.stdout) {
45 Ok(version) => version,
46 Err(_) => return None,
47 };
48
49 let mut pieces = version.split('.');
50 if pieces.next() != Some("rustc 1") {
51 return None;
52 }
53
54 let next = match pieces.next() {
55 Some(next) => next,
56 None => return None,
57 };
58
David Tolnayf83b6c92019-03-09 22:58:26 -080059 let minor = match u32::from_str(next) {
60 Ok(minor) => minor,
61 Err(_) => return None,
62 };
63
64 Some(Compiler {
65 minor: minor,
66 nightly: version.contains("nightly"),
67 })
David Tolnaye0ba9202018-10-06 20:09:08 -070068}