条件编译

语法
ConfigurationPredicate :
      ConfigurationOption
   | ConfigurationAll
   | ConfigurationAny
   | ConfigurationNot

ConfigurationOption :
   标识符 (= (字符串字面量 | 原始字符串字面量))?

ConfigurationAll
   all ( ConfigurationPredicateList? )

ConfigurationAny
   any ( ConfigurationPredicateList? )

ConfigurationNot
   not ( ConfigurationPredicate )

ConfigurationPredicateList
   ConfigurationPredicate (, ConfigurationPredicate)* ,?

条件编译的源代码 是仅在特定条件下编译的源代码。

源代码可以使用 cfgcfg_attr 属性 以及内置的 cfg 进行条件编译。

是否编译取决于被编译 crate 的目标架构、传递给编译器的任意值以及下面进一步描述的其他因素。

每种形式的条件编译都接受一个评估为 true 或 false 的 配置谓词。该谓词是以下之一

  • 配置选项。如果选项已设置,则谓词为 true;如果未设置,则为 false。
  • all() 带有一个逗号分隔的配置谓词列表。如果给定谓词全部为 true,或者列表为空,则它为 true。
  • any() 带有一个逗号分隔的配置谓词列表。如果给定谓词中至少有一个为 true,则它为 true。如果没有谓词,则为 false。
  • not() 带有一个配置谓词。如果其谓词为 false,则它为 true;如果其谓词为 true,则为 false。

配置选项 是名称或键值对,它们要么被设置,要么未被设置。

名称写为单个标识符,例如 unix

键值对写为标识符、=,然后是字符串,例如 target_arch = "x86_64"

注意

= 周围的空白被忽略,因此 foo="bar"foo = "bar" 是等效的。

键不必唯一。例如,feature = "std"feature = "serde" 可以同时设置。

已设置的配置选项

哪些配置选项被设置是在 crate 编译期间静态确定的。

一些选项是根据编译信息由 编译器设置 的。

其他选项是根据代码外部传递给编译器的输入而 任意设置 的。

无法在被编译 crate 的源代码中设置配置选项。

注意

对于 rustc,任意设置的配置选项使用 --cfg 标志设置。可以通过 rustc --print cfg --target $TARGET 显示指定目标的配置值。

注意

键为 feature 的配置选项是 Cargo 用于指定编译时选项和可选依赖项的约定。

target_arch

键值选项,一次设置目标的 CPU 架构。该值类似于平台目标三元组的第一个元素,但不完全相同。

示例值

  • "x86"
  • "x86_64"
  • "mips"
  • "powerpc"
  • "powerpc64"
  • "arm"
  • "aarch64"

target_feature

键值选项,为当前编译目标可用的每个平台特性设置。

示例值

  • "avx"
  • "avx2"
  • "crt-static"
  • "rdrand"
  • "sse"
  • "sse2"
  • "sse4.1"

有关可用特性的更多详细信息,请参阅 target_feature 属性

crt-statictarget_feature 选项的一个额外特性,用于指示 静态 C 运行时 可用。

target_os

键值选项,一次设置目标的操作系统。该值类似于平台目标三元组的第二和第三个元素。

示例值

  • "windows"
  • "macos"
  • "ios"
  • "linux"
  • "android"
  • "freebsd"
  • "dragonfly"
  • "openbsd"
  • "netbsd"
  • "none" (通常用于嵌入式目标)

target_family

键值选项,提供目标的更通用描述,例如目标通常属于的操作系统或架构系列。可以设置任意数量的 target_family 键值对。

示例值

  • "unix"
  • "windows"
  • "wasm"
  • "unix""wasm" 都设置

unixwindows

如果设置了 target_family = "unix",则 unix 被设置。

如果设置了 target_family = "windows",则 windows 被设置。

target_env

键值选项,提供了关于目标平台 ABI 或使用的 libc 的进一步区分信息。出于历史原因,该值仅在实际需要区分时才被定义为非空字符串。因此,例如,在许多 GNU 平台,该值将为空。该值类似于平台目标三元组的第四个元素。一个区别是,像 gnueabihf 这样的嵌入式 ABI 将简单地将 target_env 定义为 "gnu"

示例值

  • ""
  • "gnu"
  • "msvc"
  • "musl"
  • "sgx"

target_abi

键值选项,通过目标 ABI 的信息进一步区分 target_env

出于历史原因,该值仅在实际需要区分时才被定义为非空字符串。因此,例如,在许多 GNU 平台,该值将为空。

示例值

  • ""
  • "llvm"
  • "eabihf"
  • "abi64"
  • "sim"
  • "macabi"

target_endian

键值选项,一次设置目标的 CPU 字节序,“little” 或 “big”。

target_pointer_width

键值选项,一次设置目标的指针宽度(位数)。

示例值

  • "16"
  • "32"
  • "64"

target_vendor

键值选项,一次设置目标的供应商。

示例值

  • "apple"
  • "fortanix"
  • "pc"
  • "unknown"

target_has_atomic

键值选项,为目标支持原子加载、存储和比较交换操作的每个位宽设置。

当此 cfg 存在时,core::sync::atomic 的所有稳定 API 对于相关的原子位宽都可用。

可能的值

  • "8"
  • "16"
  • "32"
  • "64"
  • "128"
  • "ptr"

test

在编译测试 harness 时启用。通过使用 rustc--test 标志完成。有关测试支持的更多信息,请参阅测试

debug_assertions

在不优化编译时默认启用。这可用于在开发中启用额外的调试代码,但在生产中不启用。例如,它控制标准库的 debug_assert! 宏的行为。

proc_macro

当被编译的 crate 以 proc_macro crate 类型 编译时设置。

panic

键值选项,根据 panic 策略 设置。请注意,将来可能会添加更多值。

示例值

  • "abort"
  • "unwind"

条件编译的形式

cfg 属性

语法
CfgAttrAttribute :
   cfg ( ConfigurationPredicate )

cfg 属性 根据配置谓词有条件地包含它所附加的内容。

它被写为 cfg(、一个配置谓词,最后是 )

如果谓词为 true,则该项会被重写,不再带有 cfg 属性。如果谓词为 false,则该项会从源代码中移除。

当 crate 级别的 cfg 谓词为 false 时,行为略有不同:位于 cfg 之前的任何 crate 属性都会被保留,而位于 cfg 之后的任何 crate 属性都会被移除。这允许 #![no_std]#![no_core] crates 避免链接 std/core,即使 #![cfg(...)] 已经移除了整个 crate。

函数的一些示例

#![allow(unused)]
fn main() {
// The function is only included in the build when compiling for macOS
#[cfg(target_os = "macos")]
fn macos_only() {
  // ...
}

// This function is only included when either foo or bar is defined
#[cfg(any(foo, bar))]
fn needs_foo_or_bar() {
  // ...
}

// This function is only included when compiling for a unixish OS with a 32-bit
// architecture
#[cfg(all(unix, target_pointer_width = "32"))]
fn on_32bit_unix() {
  // ...
}

// This function is only included when foo is not defined
#[cfg(not(foo))]
fn needs_not_foo() {
  // ...
}

// This function is only included when the panic strategy is set to unwind
#[cfg(panic = "unwind")]
fn when_unwinding() {
  // ...
}

}

cfg 属性允许出现在允许属性的任何位置。

cfg_attr 属性

语法
CfgAttrAttribute :
   cfg_attr ( ConfigurationPredicate , CfgAttrs? )

CfgAttrs :
   属性 (, 属性)* ,?

cfg_attr 属性 根据配置谓词有条件地包含 属性

当配置谓词为 true 时,此属性会展开为谓词后列出的属性。例如,以下模块将根据目标位于 linux.rswindows.rs

#[cfg_attr(target_os = "linux", path = "linux.rs")]
#[cfg_attr(windows, path = "windows.rs")]
mod os;

可以列出零个、一个或多个属性。多个属性将分别展开为独立的属性。例如

#[cfg_attr(feature = "magic", sparkles, crackles)]
fn bewitched() {}

// When the `magic` feature flag is enabled, the above will expand to:
#[sparkles]
#[crackles]
fn bewitched() {}

注意

cfg_attr 可以展开为另一个 cfg_attr。例如,#[cfg_attr(target_os = "linux", cfg_attr(feature = "multithreaded", some_other_attribute))] 是有效的。此示例等同于 #[cfg_attr(all(target_os = "linux", feature ="multithreaded"), some_other_attribute)]

cfg_attr 属性允许出现在允许属性的任何位置。

crate_typecrate_name 属性不能与 cfg_attr 一起使用。

cfg

内置的 cfg 宏接受单个配置谓词,当谓词为 true 时评估为 true 字面量,当谓词为 false 时评估为 false 字面量。

例如

#![allow(unused)]
fn main() {
let machine_kind = if cfg!(unix) {
  "unix"
} else if cfg!(windows) {
  "windows"
} else {
  "unknown"
};

println!("I'm running on a {} machine!", machine_kind);
}