CARGO_ENCODED_RUSTFLAGS
is one of the environment variables provided by Cargo
to build scripts. It synthesizes several sources of flags affecting
Cargo's rustc invocations that build scripts might care about:
target.<triple>.rustflags
and
target.<cfg>.rustflags
and build.rustflags
, including from the
project-specific Cargo config file and the Cargo config in the user's
CARGO_HOME.If a build script needs to make some rustc invocations, or needs to characterize aspects of the upcoming rustc invocation, it likely needs these flags.
toml
[build-dependencies]
rustflags = "0.1"
This build script wants to know whether it is okay to enable
#![feature(proc_macro_span)]
. If the user is building with -Zallow-features
with a feature list that does not include proc_macro_span
, we need to not
enable the feature or the build will fail.
```rust // build.rs
use rustflags::Flag;
fn main() { if isnightly() && featureallowed("procmacrospan") { println!("cargo:rustc-cfg=procmacrospan"); } }
// Look for -Z allow-features=feature1,feature2
fn featureallowed(feature: &str) -> bool {
for flag in rustflags::fromenv() {
if let Flag::Z(option) = flag {
if option.starts_with("allow-features=") {
return option["allow-features=".len()..]
.split(',')
.any(|allowed| allowed == feature);
}
}
}
// No allow-features= flag, allowed by default.
true
} ```
This build scripts wants to try compiling a source file to figure out whether an unstable API is supported in the expected form by the current toolchain.
```rust // build.rs
use rustflags::Flag; use std::env; use std::fs; use std::path::Path; use std::process::{Command, ExitStatus, Stdio};
// This code exercises the surface area that we expect of the unstable // feature. If the current toolchain is able to compile it, we go ahead and // enable the feature. const PROBE: &str = r#" #![feature(backtrace)] #![allow(dead_code)]
use std::backtrace::{Backtrace, BacktraceStatus};
fn probe() {
let backtrace = Backtrace::capture();
match backtrace.status() {
BacktraceStatus::Captured | BacktraceStatus::Disabled | _ => {}
}
}
"#;
fn main() { match compile_probe() { Some(status) if status.success() => println!("cargo:rustc-cfg=backtrace"), _ => {} } }
fn compileprobe() -> Option
// Make sure to pick up Cargo rustc configuration.
let mut cmd = if let Some(wrapper) = env::var_os("CARGO_RUSTC_WRAPPER") {
let mut cmd = Command::new(wrapper);
// The wrapper's first argument is supposed to be the path to rustc.
cmd.arg(rustc);
cmd
} else {
Command::new(rustc)
};
cmd.stderr(Stdio::null())
.arg("--edition=2021")
.arg("--crate-name=try_backtrace")
.arg("--crate-type=lib")
.arg("--emit=metadata")
.arg("--out-dir")
.arg(out_dir)
.arg(probefile)
.args(rustflags::from_env()
.filter(|flag| matches!(flag, Flag::Cfg{..} | Flag::Z(_)))
.flatten())
.status()
.ok()
} ```
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.