7 releases

0.1.6 Apr 17, 2024
0.1.5 Mar 26, 2024
0.1.4 Jul 15, 2023
0.1.3 Mar 3, 2023
0.1.0 Jan 3, 2022

#190 in Build Utils

Download history 2406/week @ 2024-06-22 2968/week @ 2024-06-29 2326/week @ 2024-07-06 1795/week @ 2024-07-13 1538/week @ 2024-07-20 2876/week @ 2024-07-27 2962/week @ 2024-08-03 1859/week @ 2024-08-10 2564/week @ 2024-08-17 2566/week @ 2024-08-24 2840/week @ 2024-08-31 2931/week @ 2024-09-07 3199/week @ 2024-09-14 11376/week @ 2024-09-21 13325/week @ 2024-09-28 14913/week @ 2024-10-05

43,489 downloads per month
Used in 13 crates (5 directly)

MIT/Apache

49KB
1K SLoC

Parser for CARGO_ENCODED_RUSTFLAGS

github crates.io docs.rs build status

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

[build-dependencies]
rustflags = "0.1"

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();
}

License

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.

No runtime deps