| 1 | autocfg |
| 2 | ======= |
| 3 | |
| 4 | [](https://crates.io/crates/autocfg) |
| 5 | [](https://docs.rs/autocfg) |
| 6 |  |
| 7 |  |
| 8 | |
| 9 | A Rust library for build scripts to automatically configure code based on |
| 10 | compiler support. Code snippets are dynamically tested to see if the `rustc` |
| 11 | will accept them, rather than hard-coding specific version support. |
| 12 | |
| 13 | |
| 14 | ## Usage |
| 15 | |
| 16 | Add this to your `Cargo.toml`: |
| 17 | |
| 18 | ```toml |
| 19 | [build-dependencies] |
| 20 | autocfg = "1" |
| 21 | ``` |
| 22 | |
| 23 | Then use it in your `build.rs` script to detect compiler features. For |
| 24 | example, to test for 128-bit integer support, it might look like: |
| 25 | |
| 26 | ```rust |
| 27 | extern crate autocfg; |
| 28 | |
| 29 | fn main() { |
| 30 | let ac = autocfg::new(); |
| 31 | ac.emit_has_type("i128"); |
| 32 | |
| 33 | // (optional) We don't need to rerun for anything external. |
| 34 | autocfg::rerun_path("build.rs"); |
| 35 | } |
| 36 | ``` |
| 37 | |
| 38 | If the type test succeeds, this will write a `cargo:rustc-cfg=has_i128` line |
| 39 | for Cargo, which translates to Rust arguments `--cfg has_i128`. Then in the |
| 40 | rest of your Rust code, you can add `#[cfg(has_i128)]` conditions on code that |
| 41 | should only be used when the compiler supports it. |
| 42 | |
| 43 | |
| 44 | ## Release Notes |
| 45 | |
| 46 | - 1.5.0 (2025-06-17) |
| 47 | |
| 48 | - Add `edition` and `set_edition` to control the Rust edition used in probes. |
| 49 | - Remove probe result files so they don't pollute the output directory. |
| 50 | |
| 51 | - 1.4.0 (2024-09-26) |
| 52 | |
| 53 | - Add `emit_possibility` for Rust 1.80's [checked cfgs], and call that |
| 54 | automatically for methods that conditionally `emit`, by @Techcable. |
| 55 | |
| 56 | [checked cfgs]: https://blog.rust-lang.org/2024/05/06/check-cfg.html |
| 57 | |
| 58 | - 1.3.0 (2024-05-03) |
| 59 | |
| 60 | - Add `probe_raw` for direct control of the code that will be test-compiled. |
| 61 | - Use wrappers when querying the `rustc` version information too. |
| 62 | |
| 63 | - 1.2.0 (2024-03-25) |
| 64 | |
| 65 | - Add `no_std` and `set_no_std` to control the use of `#![no_std]` in probes. |
| 66 | - Use `RUSTC_WRAPPER` and `RUSTC_WORKSPACE_WRAPPER` when they are set. |
| 67 | |
| 68 | - 1.1.0 (2022-02-07) |
| 69 | - Use `CARGO_ENCODED_RUSTFLAGS` when it is set. |
| 70 | |
| 71 | - 1.0.1 (2020-08-20) |
| 72 | - Apply `RUSTFLAGS` for more `--target` scenarios, by @adamreichold. |
| 73 | |
| 74 | - 1.0.0 (2020-01-08) |
| 75 | - 🎉 Release 1.0! 🎉 (no breaking changes) |
| 76 | - Add `probe_expression` and `emit_expression_cfg` to test arbitrary expressions. |
| 77 | - Add `probe_constant` and `emit_constant_cfg` to test arbitrary constant expressions. |
| 78 | |
| 79 | - 0.1.7 (2019-10-20) |
| 80 | - Apply `RUSTFLAGS` when probing `$TARGET != $HOST`, mainly for sysroot, by @roblabla. |
| 81 | |
| 82 | - 0.1.6 (2019-08-19) |
| 83 | - Add `probe`/`emit_sysroot_crate`, by @leo60228. |
| 84 | |
| 85 | - 0.1.5 (2019-07-16) |
| 86 | - Mask some warnings from newer rustc. |
| 87 | |
| 88 | - 0.1.4 (2019-05-22) |
| 89 | - Relax `std`/`no_std` probing to a warning instead of an error. |
| 90 | - Improve `rustc` bootstrap compatibility. |
| 91 | |
| 92 | - 0.1.3 (2019-05-21) |
| 93 | - Auto-detects if `#![no_std]` is needed for the `$TARGET`. |
| 94 | |
| 95 | - 0.1.2 (2019-01-16) |
| 96 | - Add `rerun_env(ENV)` to print `cargo:rerun-if-env-changed=ENV`. |
| 97 | - Add `rerun_path(PATH)` to print `cargo:rerun-if-changed=PATH`. |
| 98 | |
| 99 | |
| 100 | ## Minimum Rust version policy |
| 101 | |
| 102 | This crate's minimum supported `rustc` version is `1.0.0`. Compatibility is |
| 103 | its entire reason for existence, so this crate will be extremely conservative |
| 104 | about raising this requirement. If this is ever deemed necessary, it will be |
| 105 | treated as a major breaking change for semver purposes. |
| 106 | |
| 107 | |
| 108 | ## License |
| 109 | |
| 110 | This project is licensed under either of |
| 111 | |
| 112 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or |
| 113 | https://www.apache.org/licenses/LICENSE-2.0) |
| 114 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or |
| 115 | https://opensource.org/licenses/MIT) |
| 116 | |
| 117 | at your option. |