• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::env;
2 use std::process::Command;
3 use 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.
main()8 fn main() {
9     let compiler = match rustc_version() {
10         Some(compiler) => compiler,
11         None => return,
12     };
13 
14     if compiler.minor >= 17 {
15         println!("cargo:rustc-cfg=syn_can_match_trailing_dollar");
16     }
17 
18     if compiler.minor >= 19 {
19         println!("cargo:rustc-cfg=syn_can_use_thread_id");
20     }
21 
22     if compiler.minor >= 20 {
23         println!("cargo:rustc-cfg=syn_can_use_associated_constants");
24     }
25 
26     // Macro modularization allows re-exporting the `quote!` macro in 1.30+.
27     if compiler.minor >= 30 {
28         println!("cargo:rustc-cfg=syn_can_call_macro_by_path");
29     }
30 
31     if !compiler.nightly {
32         println!("cargo:rustc-cfg=syn_disable_nightly_tests");
33     }
34 }
35 
36 struct Compiler {
37     minor: u32,
38     nightly: bool,
39 }
40 
rustc_version() -> Option<Compiler>41 fn rustc_version() -> Option<Compiler> {
42     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 
67     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     })
76 }
77