Crate rustflags

Source
Expand description

githubcrates-iodocs-rs


Parser for CARGO_ENCODED_RUSTFLAGS.

This 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 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.

§Example

This build script uses the cmake crate to compile some C code, and must configure it with a particular C preprocessor #define if the Rust build is being performed with sanitizers.

// build.rs

use rustflags::Flag;
use std::env;
use std::path::PathBuf;

fn main() {
    let manifest_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
    let lib_source_dir = manifest_dir.join("lib");
    assert!(lib_source_dir.join("CMakeLists.txt").exists());

    let mut builder = cmake::Config::new(lib_source_dir);

    // Look for -Zsanitizer=address
    for flag in rustflags::from_env() {
        if matches!(flag, Flag::Z(z) if z == "sanitizer=address") {
            builder.define("ENABLE_SANITIZERS", "ON");
            builder.define("SANITIZERS", "address");
            break;
        }
    }

    builder.build();
}

Structs§

RustFlags
Iterator of rustc flags

Enums§

Color
Argument of --color
CrateType
Argument of --crate-type
Emit
Argument of --emit
ErrorFormat
Argument of --error-format
Flag
One flag recognized by rustc
LibraryKind
Argument of -L
LinkKind
Argument of -l
LinkModifier
Argument of -l
LinkModifierPrefix
Argument of -l
LintLevel
Argument of --cap-lints

Functions§

from_encoded
Parse flags from a string separated with ASCII unit separator (‘\x1f’).
from_env
Parse flags from CARGO_ENCODED_RUSTFLAGS environment variable.