blob: 2b2a419645c99cf4432195c40890c5fae2810edf [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 Tolnaye1da66f2019-04-22 14:41:06 -070018 if compiler.minor >= 20 {
19 println!("cargo:rustc-cfg=syn_can_use_associated_constants");
20 }
21
David Tolnaye0ba9202018-10-06 20:09:08 -070022 // Macro modularization allows re-exporting the `quote!` macro in 1.30+.
David Tolnayf83b6c92019-03-09 22:58:26 -080023 if compiler.minor >= 30 {
David Tolnaye0ba9202018-10-06 20:09:08 -070024 println!("cargo:rustc-cfg=syn_can_call_macro_by_path");
25 }
David Tolnayf83b6c92019-03-09 22:58:26 -080026
27 if !compiler.nightly {
28 println!("cargo:rustc-cfg=syn_disable_nightly_tests");
29 }
David Tolnaye0ba9202018-10-06 20:09:08 -070030}
31
David Tolnayf83b6c92019-03-09 22:58:26 -080032struct Compiler {
33 minor: u32,
34 nightly: bool,
35}
36
37fn rustc_version() -> Option<Compiler> {
David Tolnaye0ba9202018-10-06 20:09:08 -070038 let rustc = match env::var_os("RUSTC") {
39 Some(rustc) => rustc,
40 None => return None,
41 };
42
43 let output = match Command::new(rustc).arg("--version").output() {
44 Ok(output) => output,
45 Err(_) => return None,
46 };
47
48 let version = match str::from_utf8(&output.stdout) {
49 Ok(version) => version,
50 Err(_) => return None,
51 };
52
53 let mut pieces = version.split('.');
54 if pieces.next() != Some("rustc 1") {
55 return None;
56 }
57
58 let next = match pieces.next() {
59 Some(next) => next,
60 None => return None,
61 };
62
David Tolnayf83b6c92019-03-09 22:58:26 -080063 let minor = match u32::from_str(next) {
64 Ok(minor) => minor,
65 Err(_) => return None,
66 };
67
68 Some(Compiler {
69 minor: minor,
70 nightly: version.contains("nightly"),
71 })
David Tolnaye0ba9202018-10-06 20:09:08 -070072}