• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // rustc-cfg emitted by the build script:
2 //
3 // "use_proc_macro"
4 //     Link to extern crate proc_macro. Available on any compiler and any target
5 //     except wasm32. Requires "proc-macro" Cargo cfg to be enabled (default is
6 //     enabled). On wasm32 we never link to proc_macro even if "proc-macro" cfg
7 //     is enabled.
8 //
9 // "wrap_proc_macro"
10 //     Wrap types from libproc_macro rather than polyfilling the whole API.
11 //     Enabled on rustc 1.29+ as long as procmacro2_semver_exempt is not set,
12 //     because we can't emulate the unstable API without emulating everything
13 //     else. Also enabled unconditionally on nightly, in which case the
14 //     procmacro2_semver_exempt surface area is implemented by using the
15 //     nightly-only proc_macro API.
16 //
17 // "hygiene"
18 //    Enable Span::mixed_site() and non-dummy behavior of Span::resolved_at
19 //    and Span::located_at. Enabled on Rust 1.45+.
20 //
21 // "proc_macro_span"
22 //     Enable non-dummy behavior of Span::start and Span::end methods which
23 //     requires an unstable compiler feature. Enabled when building with
24 //     nightly, unless `-Z allow-feature` in RUSTFLAGS disallows unstable
25 //     features.
26 //
27 // "super_unstable"
28 //     Implement the semver exempt API in terms of the nightly-only proc_macro
29 //     API. Enabled when using procmacro2_semver_exempt on a nightly compiler.
30 //
31 // "span_locations"
32 //     Provide methods Span::start and Span::end which give the line/column
33 //     location of a token. Enabled by procmacro2_semver_exempt or the
34 //     "span-locations" Cargo cfg. This is behind a cfg because tracking
35 //     location inside spans is a performance hit.
36 //
37 // "is_available"
38 //     Use proc_macro::is_available() to detect if the proc macro API is
39 //     available or needs to be polyfilled instead of trying to use the proc
40 //     macro API and catching a panic if it isn't available. Enabled on Rust
41 //     1.57+.
42 
43 use std::env;
44 use std::process::{self, Command};
45 use std::str;
46 
main()47 fn main() {
48     println!("cargo:rerun-if-changed=build.rs");
49 
50     let version = match rustc_version() {
51         Some(version) => version,
52         None => return,
53     };
54 
55     if version.minor < 31 {
56         eprintln!("Minimum supported rustc version is 1.31");
57         process::exit(1);
58     }
59 
60     let docs_rs = env::var_os("DOCS_RS").is_some();
61     let semver_exempt = cfg!(procmacro2_semver_exempt) || docs_rs;
62     if semver_exempt {
63         // https://github.com/dtolnay/proc-macro2/issues/147
64         println!("cargo:rustc-cfg=procmacro2_semver_exempt");
65     }
66 
67     if semver_exempt || cfg!(feature = "span-locations") {
68         println!("cargo:rustc-cfg=span_locations");
69     }
70 
71     if version.minor < 32 {
72         println!("cargo:rustc-cfg=no_libprocmacro_unwind_safe");
73     }
74 
75     if version.minor < 39 {
76         println!("cargo:rustc-cfg=no_bind_by_move_pattern_guard");
77     }
78 
79     if version.minor < 44 {
80         println!("cargo:rustc-cfg=no_lexerror_display");
81     }
82 
83     if version.minor < 45 {
84         println!("cargo:rustc-cfg=no_hygiene");
85     }
86 
87     if version.minor < 47 {
88         println!("cargo:rustc-cfg=no_ident_new_raw");
89     }
90 
91     if version.minor < 54 {
92         println!("cargo:rustc-cfg=no_literal_from_str");
93     }
94 
95     if version.minor < 55 {
96         println!("cargo:rustc-cfg=no_group_open_close");
97     }
98 
99     if version.minor < 57 {
100         println!("cargo:rustc-cfg=no_is_available");
101     }
102 
103     if version.minor < 66 {
104         println!("cargo:rustc-cfg=no_source_text");
105     }
106 
107     let target = env::var("TARGET").unwrap();
108     if !enable_use_proc_macro(&target) {
109         return;
110     }
111 
112     println!("cargo:rustc-cfg=use_proc_macro");
113 
114     if version.nightly || !semver_exempt {
115         println!("cargo:rustc-cfg=wrap_proc_macro");
116     }
117 
118     if version.nightly
119         && feature_allowed("proc_macro_span")
120         && feature_allowed("proc_macro_span_shrink")
121     {
122         println!("cargo:rustc-cfg=proc_macro_span");
123     }
124 
125     if semver_exempt && version.nightly {
126         println!("cargo:rustc-cfg=super_unstable");
127     }
128 }
129 
enable_use_proc_macro(target: &str) -> bool130 fn enable_use_proc_macro(target: &str) -> bool {
131     // wasm targets don't have the `proc_macro` crate, disable this feature.
132     if target.contains("wasm32") {
133         return false;
134     }
135 
136     // Otherwise, only enable it if our feature is actually enabled.
137     cfg!(feature = "proc-macro")
138 }
139 
140 struct RustcVersion {
141     minor: u32,
142     nightly: bool,
143 }
144 
rustc_version() -> Option<RustcVersion>145 fn rustc_version() -> Option<RustcVersion> {
146     let rustc = env::var_os("RUSTC")?;
147     let output = Command::new(rustc).arg("--version").output().ok()?;
148     let version = str::from_utf8(&output.stdout).ok()?;
149     let nightly = version.contains("nightly") || version.contains("dev");
150     let mut pieces = version.split('.');
151     if pieces.next() != Some("rustc 1") {
152         return None;
153     }
154     let minor = pieces.next()?.parse().ok()?;
155     Some(RustcVersion { minor, nightly })
156 }
157 
feature_allowed(feature: &str) -> bool158 fn feature_allowed(feature: &str) -> bool {
159     // Recognized formats:
160     //
161     //     -Z allow-features=feature1,feature2
162     //
163     //     -Zallow-features=feature1,feature2
164 
165     let flags_var;
166     let flags_var_string;
167     let flags = if let Some(encoded_rustflags) = env::var_os("CARGO_ENCODED_RUSTFLAGS") {
168         flags_var = encoded_rustflags;
169         flags_var_string = flags_var.to_string_lossy();
170         flags_var_string.split('\x1f')
171     } else {
172         return true;
173     };
174 
175     for mut flag in flags {
176         if flag.starts_with("-Z") {
177             flag = &flag["-Z".len()..];
178         }
179         if flag.starts_with("allow-features=") {
180             flag = &flag["allow-features=".len()..];
181             return flag.split(',').any(|allowed| allowed == feature);
182         }
183     }
184 
185     // No allow-features= flag, allowed by default.
186     true
187 }
188