blob: bbdb97e4afe68ded041db4c66bef7b9809b2a8c9 [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
Jannis Harder2ccec112019-06-25 16:00:22 +020014 if compiler.minor >= 17 {
15 println!("cargo:rustc-cfg=syn_can_match_trailing_dollar");
16 }
17
David Tolnayf83b6c92019-03-09 22:58:26 -080018 if compiler.minor >= 19 {
David Tolnay32874802018-11-11 08:52:19 -080019 println!("cargo:rustc-cfg=syn_can_use_thread_id");
20 }
21
David Tolnaye1da66f2019-04-22 14:41:06 -070022 if compiler.minor >= 20 {
23 println!("cargo:rustc-cfg=syn_can_use_associated_constants");
24 }
25
David Tolnaye0ba9202018-10-06 20:09:08 -070026 // Macro modularization allows re-exporting the `quote!` macro in 1.30+.
David Tolnayf83b6c92019-03-09 22:58:26 -080027 if compiler.minor >= 30 {
David Tolnaye0ba9202018-10-06 20:09:08 -070028 println!("cargo:rustc-cfg=syn_can_call_macro_by_path");
29 }
David Tolnayf83b6c92019-03-09 22:58:26 -080030
31 if !compiler.nightly {
32 println!("cargo:rustc-cfg=syn_disable_nightly_tests");
33 }
David Tolnaye0ba9202018-10-06 20:09:08 -070034}
35
David Tolnayf83b6c92019-03-09 22:58:26 -080036struct Compiler {
37 minor: u32,
38 nightly: bool,
39}
40
41fn rustc_version() -> Option<Compiler> {
David Tolnaye0ba9202018-10-06 20:09:08 -070042 let rustc = match env::var_os("RUSTC") {
43 Some(rustc) => rustc,
44 None => return None,
45 };
46
47 let output = match Command::new(rustc).arg("--version").output() {
48 Ok(output) => output,
49 Err(_) => return None,
50 };
51
52 let version = match str::from_utf8(&output.stdout) {
53 Ok(version) => version,
54 Err(_) => return None,
55 };
56
57 let mut pieces = version.split('.');
58 if pieces.next() != Some("rustc 1") {
59 return None;
60 }
61
62 let next = match pieces.next() {
63 Some(next) => next,
64 None => return None,
65 };
66
David Tolnayf83b6c92019-03-09 22:58:26 -080067 let minor = match u32::from_str(next) {
68 Ok(minor) => minor,
69 Err(_) => return None,
70 };
71
72 Some(Compiler {
73 minor: minor,
74 nightly: version.contains("nightly"),
75 })
David Tolnaye0ba9202018-10-06 20:09:08 -070076}