Parser for CARGO_ENCODED_RUSTFLAGS
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:
- Flags passed via the RUSTFLAGS environment variable,
- Cargo config entries under
target.<triple>.rustflags
andtarget.<cfg>.rustflags
andbuild.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.
[]
= "0.1"
Examples
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.
// build.rs
use Flag;
// Look for `-Z allow-features=feature1,feature2`
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.
// build.rs
use Flag;
use env;
use fs;
use Path;
use ;
// 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 | _ => {}
}
}
"#;