third_party_rust_autocfg:基于 Rust 的编译器特性自动配置库项目

一个基于编译时配置选项生成 Rust 代码的库。 | A library that generates Rust code based on compile-time configuration options.

分支231Tags30
文件最后提交记录最后更新时间
10 个月前
10 个月前
10 个月前
10 个月前
10 个月前
8 年前
7 个月前
10 个月前
8 年前
8 年前
3 年前
3 个月前
10 个月前
7 个月前

autocfg

autocfg crate autocfg 文档 最低支持 rustc 1.0 构建状态

一个用于构建脚本的 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 的方法中自动执行此功能。
  • 1.3.0 (2024-05-03)

    • 新增 probe_raw 方法,用于直接控制待测试编译的代码。
    • 在查询 rustc 版本信息时也使用封装器。
  • 1.2.0 (2024-03-25)

    • 新增 no_stdset_no_std 方法,用于控制探测代码中 #![no_std] 的使用。
    • 当设置了 RUSTC_WRAPPERRUSTC_WORKSPACE_WRAPPER 时使用它们。
  • 1.1.0 (2022-02-07)

    • 当设置了 CARGO_ENCODED_RUSTFLAGS 时使用该环境变量。
  • 1.0.1 (2020-08-20)

    • 通过 @adamreichold 的贡献,在更多 --target 场景下应用 RUSTFLAGS
  • 1.0.0 (2020-01-08)

    • 🎉 发布 1.0 版本!🎉(无破坏性变更)
    • 新增 probe_expressionemit_expression_cfg 以测试任意表达式。
    • 新增 probe_constantemit_constant_cfg 以测试任意常量表达式。
  • 0.1.7 (2019-10-20)

    • 通过 @roblabla 的贡献,在探测 $TARGET != $HOST 时应用 RUSTFLAGS,主要用于系统根目录设置。
  • 0.1.6 (2019-08-19)

    • 通过 @leo60228 的贡献,新增 probe/emit_sysroot_crate 方法。
  • 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。兼容性是其存在的根本原因, 因此本库对提升最低版本要求将持极端保守态度。若确有必要提升, 将会根据语义化版本规范将其视为重大破坏性变更。

许可证

本项目采用以下任一许可证:

您可自行选择适用条款。

项目介绍

一个基于编译时配置选项生成 Rust 代码的库。 | A library that generates Rust code based on compile-time configuration options.

定制我的领域