1fad3a1d3Sopenharmony_ciuse std::env;
2fad3a1d3Sopenharmony_ciuse std::ffi::OsString;
3fad3a1d3Sopenharmony_ciuse std::process::{self, Command, Stdio};
4fad3a1d3Sopenharmony_ci
5fad3a1d3Sopenharmony_ci// The rustc-cfg strings below are *not* public API. Please let us know by
6fad3a1d3Sopenharmony_ci// opening a GitHub issue if your build environment requires some way to enable
7fad3a1d3Sopenharmony_ci// these cfgs other than by executing our build script.
8fad3a1d3Sopenharmony_cifn main() {
9fad3a1d3Sopenharmony_ci    println!("cargo:rerun-if-changed=build.rs");
10fad3a1d3Sopenharmony_ci
11fad3a1d3Sopenharmony_ci    // Note: add "/build.rs" to package.include in Cargo.toml if adding any
12fad3a1d3Sopenharmony_ci    // conditional compilation within the library.
13fad3a1d3Sopenharmony_ci
14fad3a1d3Sopenharmony_ci    if !unstable() {
15fad3a1d3Sopenharmony_ci        println!("cargo:rustc-cfg=syn_disable_nightly_tests");
16fad3a1d3Sopenharmony_ci    }
17fad3a1d3Sopenharmony_ci}
18fad3a1d3Sopenharmony_ci
19fad3a1d3Sopenharmony_cifn unstable() -> bool {
20fad3a1d3Sopenharmony_ci    let rustc = cargo_env_var("RUSTC");
21fad3a1d3Sopenharmony_ci
22fad3a1d3Sopenharmony_ci    // Pick up Cargo rustc configuration.
23fad3a1d3Sopenharmony_ci    let mut cmd = if let Some(wrapper) = env::var_os("RUSTC_WRAPPER") {
24fad3a1d3Sopenharmony_ci        let mut cmd = Command::new(wrapper);
25fad3a1d3Sopenharmony_ci        // The wrapper's first argument is supposed to be the path to rustc.
26fad3a1d3Sopenharmony_ci        cmd.arg(rustc);
27fad3a1d3Sopenharmony_ci        cmd
28fad3a1d3Sopenharmony_ci    } else {
29fad3a1d3Sopenharmony_ci        Command::new(rustc)
30fad3a1d3Sopenharmony_ci    };
31fad3a1d3Sopenharmony_ci
32fad3a1d3Sopenharmony_ci    cmd.stdin(Stdio::null());
33fad3a1d3Sopenharmony_ci    cmd.stdout(Stdio::null());
34fad3a1d3Sopenharmony_ci    cmd.stderr(Stdio::null());
35fad3a1d3Sopenharmony_ci    cmd.arg("-");
36fad3a1d3Sopenharmony_ci
37fad3a1d3Sopenharmony_ci    // Find out whether this is a nightly or dev build.
38fad3a1d3Sopenharmony_ci    cmd.env_remove("RUSTC_BOOTSTRAP");
39fad3a1d3Sopenharmony_ci    cmd.arg("-Zcrate-attr=feature(rustc_private)");
40fad3a1d3Sopenharmony_ci
41fad3a1d3Sopenharmony_ci    // Pass `-Zunpretty` to terminate earlier without writing out any "emit"
42fad3a1d3Sopenharmony_ci    // files. Use `expanded` to proceed far enough to actually apply crate
43fad3a1d3Sopenharmony_ci    // attrs. With `unpretty=normal` or `--print`, not enough compilation
44fad3a1d3Sopenharmony_ci    // happens to recognize that the feature attribute is unstable.
45fad3a1d3Sopenharmony_ci    cmd.arg("-Zunpretty=expanded");
46fad3a1d3Sopenharmony_ci
47fad3a1d3Sopenharmony_ci    // Set #![no_std] to bypass loading libstd.rlib. This is a 7.5% speedup.
48fad3a1d3Sopenharmony_ci    cmd.arg("-Zcrate-attr=no_std");
49fad3a1d3Sopenharmony_ci
50fad3a1d3Sopenharmony_ci    cmd.arg("--crate-type=lib");
51fad3a1d3Sopenharmony_ci    cmd.arg("--edition=2021");
52fad3a1d3Sopenharmony_ci
53fad3a1d3Sopenharmony_ci    if let Some(target) = env::var_os("TARGET") {
54fad3a1d3Sopenharmony_ci        cmd.arg("--target").arg(target);
55fad3a1d3Sopenharmony_ci    }
56fad3a1d3Sopenharmony_ci
57fad3a1d3Sopenharmony_ci    // If Cargo wants to set RUSTFLAGS, use that.
58fad3a1d3Sopenharmony_ci    if let Ok(rustflags) = env::var("CARGO_ENCODED_RUSTFLAGS") {
59fad3a1d3Sopenharmony_ci        if !rustflags.is_empty() {
60fad3a1d3Sopenharmony_ci            for arg in rustflags.split('\x1f') {
61fad3a1d3Sopenharmony_ci                cmd.arg(arg);
62fad3a1d3Sopenharmony_ci            }
63fad3a1d3Sopenharmony_ci        }
64fad3a1d3Sopenharmony_ci    }
65fad3a1d3Sopenharmony_ci
66fad3a1d3Sopenharmony_ci    // This rustc invocation should take around 0.03 seconds.
67fad3a1d3Sopenharmony_ci    match cmd.status() {
68fad3a1d3Sopenharmony_ci        Ok(status) => status.success(),
69fad3a1d3Sopenharmony_ci        Err(_) => false,
70fad3a1d3Sopenharmony_ci    }
71fad3a1d3Sopenharmony_ci}
72fad3a1d3Sopenharmony_ci
73fad3a1d3Sopenharmony_cifn cargo_env_var(key: &str) -> OsString {
74fad3a1d3Sopenharmony_ci    env::var_os(key).unwrap_or_else(|| {
75fad3a1d3Sopenharmony_ci        eprintln!(
76fad3a1d3Sopenharmony_ci            "Environment variable ${} is not set during execution of build script",
77fad3a1d3Sopenharmony_ci            key,
78fad3a1d3Sopenharmony_ci        );
79fad3a1d3Sopenharmony_ci        process::exit(1);
80fad3a1d3Sopenharmony_ci    })
81fad3a1d3Sopenharmony_ci}
82