File: README.md

package info (click to toggle)
rust-rustflags 0.1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 172 kB
  • sloc: makefile: 2
file content (79 lines) | stat: -rw-r--r-- 2,948 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
Parser for CARGO_ENCODED_RUSTFLAGS
==================================

[<img alt="github" src="https://img.shields.io/badge/github-dtolnay/rustflags-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/dtolnay/rustflags)
[<img alt="crates.io" src="https://img.shields.io/crates/v/rustflags.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/rustflags)
[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-rustflags-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" height="20">](https://docs.rs/rustflags)
[<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/dtolnay/rustflags/ci.yml?branch=master&style=for-the-badge" height="20">](https://github.com/dtolnay/rustflags/actions?query=branch%3Amaster)

`CARGO_ENCODED_RUSTFLAGS` is one of the environment variables [provided by Cargo
to build scripts][reference]. 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.

[reference]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts

```toml
[build-dependencies]
rustflags = "0.1"
```

<br>

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

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

<br>

#### License

<sup>
Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
</sup>

<br>

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