一个基于编译时配置选项生成 Rust 代码的库。 | A library that generates Rust code based on compile-time configuration options.
| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 10 个月前 | ||
| 10 个月前 | ||
| 10 个月前 | ||
| 10 个月前 | ||
| 10 个月前 | ||
| 8 年前 | ||
| 7 个月前 | ||
| 10 个月前 | ||
| 8 年前 | ||
| 8 年前 | ||
| 3 年前 | ||
| 3 个月前 | ||
| 10 个月前 | ||
| 7 个月前 |
autocfg
一个用于构建脚本的 Rust 库,可根据编译器支持自动配置代码。通过动态测试代码片段来检测 rustc 是否接受它们,而非硬编码特定的版本支持。
使用方法
将以下内容添加到您的 Cargo.toml 中:
[build-dependencies]
autocfg = "1"
然后在您的 build.rs 脚本中使用它来检测编译器特性。例如,要测试 128 位整数支持,代码可能如下所示:
extern crate autocfg;
fn main() {
let ac = autocfg::new();
ac.emit_has_type("i128");
// (optional) We don't need to rerun for anything external.
autocfg::rerun_path("build.rs");
}
若类型测试成功,该操作会向 Cargo 写入一行 cargo:rustc-cfg=has_i128 配置,
这相当于传递了 Rust 参数 --cfg has_i128。此后在其余的 Rust 代码中,
您可以为仅在编译器支持时才使用的代码添加 #[cfg(has_i128)] 条件注解。
版本发布说明
-
1.4.0 (2024-09-26)
- 为 Rust 1.80 的检查性配置项新增
emit_possibility功能,并通过 @Techcable 的贡献, 在条件性调用emit的方法中自动执行此功能。
- 为 Rust 1.80 的检查性配置项新增
-
1.3.0 (2024-05-03)
- 新增
probe_raw方法,用于直接控制待测试编译的代码。 - 在查询
rustc版本信息时也使用封装器。
- 新增
-
1.2.0 (2024-03-25)
- 新增
no_std和set_no_std方法,用于控制探测代码中#![no_std]的使用。 - 当设置了
RUSTC_WRAPPER和RUSTC_WORKSPACE_WRAPPER时使用它们。
- 新增
-
1.1.0 (2022-02-07)
- 当设置了
CARGO_ENCODED_RUSTFLAGS时使用该环境变量。
- 当设置了
-
1.0.1 (2020-08-20)
- 通过 @adamreichold 的贡献,在更多
--target场景下应用RUSTFLAGS。
- 通过 @adamreichold 的贡献,在更多
-
1.0.0 (2020-01-08)
- 🎉 发布 1.0 版本!🎉(无破坏性变更)
- 新增
probe_expression和emit_expression_cfg以测试任意表达式。 - 新增
probe_constant和emit_constant_cfg以测试任意常量表达式。
-
0.1.7 (2019-10-20)
- 通过 @roblabla 的贡献,在探测
$TARGET != $HOST时应用RUSTFLAGS,主要用于系统根目录设置。
- 通过 @roblabla 的贡献,在探测
-
0.1.6 (2019-08-19)
- 通过 @leo60228 的贡献,新增
probe/emit_sysroot_crate方法。
- 通过 @leo60228 的贡献,新增
-
0.1.5 (2019-07-16)
- 屏蔽新版本 rustc 产生的部分警告。
-
0.1.4 (2019-05-22)
- 将
std/no_std探测的严格程度从错误降级为警告。 - 提升与
rustc引导程序的兼容性。
- 将
-
0.1.3 (2019-05-21)
- 自动检测
$TARGET是否需要#![no_std]。
- 自动检测
-
0.1.2 (2019-01-16)
- 新增
rerun_env(ENV)以输出cargo:rerun-if-env-changed=ENV。 - 新增
rerun_path(PATH)以输出cargo:rerun-if-changed=PATH。
- 新增
最低 Rust 版本支持政策
本库最低支持的 rustc 版本为 1.0.0。兼容性是其存在的根本原因,
因此本库对提升最低版本要求将持极端保守态度。若确有必要提升,
将会根据语义化版本规范将其视为重大破坏性变更。
许可证
本项目采用以下任一许可证:
- Apache 许可证 2.0 版本(LICENSE-APACHE 或 https://www.apache.org/licenses/LICENSE-2.0)
- MIT 许可证(LICENSE-MIT 或 https://opensource.org/licenses/MIT)
您可自行选择适用条款。
项目介绍
一个基于编译时配置选项生成 Rust 代码的库。 | A library that generates Rust code based on compile-time configuration options.
定制我的领域