@@ -8,6 +8,14 @@ fn cfg(name: &str) -> String {
88 std:: env:: var ( format ! ( "CARGO_CFG_{}" , name. to_uppercase( ) ) ) . unwrap_or_default ( )
99}
1010
11+ fn make_overridable_cfg ( name : & str , logic : impl FnOnce ( ) -> & ' static str ) -> String {
12+ let env_name = format ! ( "LITHIUM_{}" , name. to_uppercase( ) ) ;
13+ println ! ( "cargo::rerun-if-env-changed={env_name}" ) ;
14+ let value = std:: env:: var ( env_name) . unwrap_or_else ( |_| logic ( ) . to_string ( ) ) ;
15+ println ! ( "cargo::rustc-cfg={name}=\" {value}\" " ) ;
16+ value
17+ }
18+
1119fn main ( ) {
1220 println ! ( "cargo::rerun-if-env-changed=MIRIFLAGS" ) ;
1321 let is_miri = has_cfg ( "miri" ) ;
@@ -17,66 +25,49 @@ fn main() {
1725 println ! ( "cargo::rustc-cfg=feature=\" sound-under-stacked-borrows\" " ) ;
1826 }
1927
20- let ac = autocfg:: new ( ) ;
2128 let is_nightly = version_meta ( ) . unwrap ( ) . channel == Channel :: Nightly ;
2229
23- // `ac.probe_raw` calls need to be very careful here: under certain configurations, like
24- // `cargo clippy -- -D warnings`, warnigns in the tested snippets can cause probing to give
25- // false negatives. It's important to make sure that, for example, there's no unused items or
26- // variables. See https://github.com/cuviper/autocfg/issues/41.
30+ // We've previously used `autocfg` to check if `std` is available, and emitted errors when
31+ // compiling without `std` on stable. But that didn't work well: when std is available due to
32+ // `-Z build-std`, autocfg doesn't notice it [1], so the tests within this build script would
33+ // fail even though using `std` from within the crate would work. So instead, we just assume
34+ // that `std` is present if nothing else works -- that leads to worse diagnostics in the failure
35+ // case, but makes the common one actually work.
36+ //
37+ // [1]: https://github.com/cuviper/autocfg/issues/34
2738
28- println ! ( "cargo::rerun-if-env-changed=LITHIUM_THREAD_LOCAL" ) ;
29- if let Ok ( thread_local) = std:: env:: var ( "LITHIUM_THREAD_LOCAL" ) {
30- println ! ( "cargo::rustc-cfg=thread_local=\" {thread_local}\" " ) ;
31- } else if is_nightly && has_cfg ( "target_thread_local" ) {
32- println ! ( "cargo::rustc-cfg=thread_local=\" attribute\" " ) ;
33- } else if ac
34- . probe_raw (
35- r"
36- #![no_std]
37- extern crate std;
38- std::thread_local! {
39- static FOO: () = const {};
39+ make_overridable_cfg ( "thread_local" , || {
40+ if is_nightly && has_cfg ( "target_thread_local" ) {
41+ "attribute"
42+ } else {
43+ "std"
4044 }
41- " ,
42- )
43- . is_ok ( )
44- {
45- println ! ( "cargo::rustc-cfg=thread_local=\" std\" " ) ;
46- } else {
47- println ! ( "cargo::rustc-cfg=thread_local=\" unimplemented\" " ) ;
48- }
45+ } ) ;
4946
50- println ! ( "cargo::rerun-if-env-changed=LITHIUM_BACKEND" ) ;
51- if let Ok ( backend) = std:: env:: var ( "LITHIUM_BACKEND" ) {
52- println ! ( "cargo::rustc-cfg=backend=\" {backend}\" " ) ;
53- } else if is_nightly && cfg ( "target_os" ) == "emscripten" && !has_cfg ( "emscripten_wasm_eh" ) {
54- println ! ( "cargo::rustc-cfg=backend=\" emscripten\" " ) ;
55- } else if is_nightly && cfg ( "target_arch" ) == "wasm32" {
56- println ! ( "cargo::rustc-cfg=backend=\" wasm\" " ) ;
57- } else if is_nightly && ( has_cfg ( "unix" ) || ( has_cfg ( "windows" ) && cfg ( "target_env" ) == "gnu" ) )
58- {
59- println ! ( "cargo::rustc-cfg=backend=\" itanium\" " ) ;
60- } else if is_nightly && ( has_cfg ( "windows" ) && cfg ( "target_env" ) == "msvc" ) && !is_miri {
61- println ! ( "cargo::rustc-cfg=backend=\" seh\" " ) ;
62- } else if ac
63- . probe_raw (
64- r"
65- #![no_std]
66- extern crate std;
67- pub use std::panic::{catch_unwind, resume_unwind};
68- " ,
69- )
70- . is_ok ( )
71- {
72- println ! ( "cargo::rustc-cfg=backend=\" panic\" " ) ;
73- } else {
74- println ! ( "cargo::rustc-cfg=backend=\" unimplemented\" " ) ;
75- }
47+ let backend = make_overridable_cfg ( "backend" , || {
48+ if is_nightly && cfg ( "target_os" ) == "emscripten" && !has_cfg ( "emscripten_wasm_eh" ) {
49+ "emscripten"
50+ } else if is_nightly && cfg ( "target_arch" ) == "wasm32" {
51+ "wasm"
52+ } else if is_nightly
53+ && ( has_cfg ( "unix" ) || ( has_cfg ( "windows" ) && cfg ( "target_env" ) == "gnu" ) )
54+ {
55+ "itanium"
56+ } else if is_nightly && has_cfg ( "windows" ) && cfg ( "target_env" ) == "msvc" && !is_miri {
57+ "seh"
58+ } else {
59+ "panic"
60+ }
61+ } ) ;
7662
77- if ac
78- . probe_raw (
79- r#"
63+ // Since the panic backend can use `abort` and is available on stable, we need to set
64+ // `abort = "std"` whenever the panic backend is used, even if we don't readily know if `std` is
65+ // available. But that's fine, since the panic backend requires `std` anyway.
66+ let ac = autocfg:: new ( ) ;
67+ if backend == "panic"
68+ || ac
69+ . probe_raw (
70+ r#"
8071 #![no_std]
8172 extern crate std;
8273 use std::io::Write;
@@ -85,8 +76,8 @@ fn main() {
8576 std::process::abort();
8677 }
8778 "# ,
88- )
89- . is_ok ( )
79+ )
80+ . is_ok ( )
9081 {
9182 println ! ( "cargo::rustc-cfg=abort=\" std\" " ) ;
9283 } else {
0 commit comments