docs

你正在阅读 v0.1.2 的文档。Nabi 0.1.2 已不再提供支持。 最新版本:v0.1.3 →

本页尚未翻译,以下为英文原文。

#6.3. #[cfg] conditional compilation

本页内容

The #[cfg(...)] attribute includes or excludes items from the build according to compile-time conditions. The grammar is:

cfg_predicate   ::= identifier                              // flag
                  | identifier "=" string_lit               // key=value
                  | "all" "(" pred_list ")"
                  | "any" "(" pred_list ")"
                  | "not" "(" cfg_predicate ")"
pred_list       ::= cfg_predicate ("," cfg_predicate)* ","?
cfg_attr        ::= "#" "[" "cfg" "(" cfg_predicate ")" "]"
cfg_attr_attr   ::= "#" "[" "cfg_attr" "(" cfg_predicate ("," attr_body)+ ")" "]"
cfg_macro       ::= "cfg" "!" "(" cfg_predicate ")"

#6.3.1. Basic form

The simplest uses:

nabi
#[cfg(target_os = "linux")]
fn linux_specific() { ... }

#[cfg(target_arch = "x86_64")]
const HAS_AVX: bool = true;

#[cfg(test)]
mod tests { ... }

#[cfg(...)] takes a predicate as its argument. If the condition evaluates to true, the item is included in compilation; if false, it is excluded. An excluded item behaves as if it had never been written, so it is invisible to name resolution of other code as well.

#6.3.2. Predicate kinds

#key = value

A condition expressed by pairing a key with a value.

nabi
#[cfg(target_os = "linux")]
#[cfg(target_arch = "x86_64")]
#[cfg(target_pointer_width = "64")]
#[cfg(feature = "gpu")]

The standard keys are:

KeyPossible values
target_arch"x86_64", "aarch64", "x86", "arm", "wasm32", "riscv64", "riscv32", "powerpc64", "s390x"
target_os"linux", "android", "macos", "ios", "windows", "freebsd", "wasi", "unknown", "none"
target_pointer_width"64", "32"
target_endian"little", "big"
target_family"unix", "windows", "wasm"
target_env"gnu", "musl", "msvc"
target_abi"sim" (an Apple simulator: aarch64-apple-ios-sim)
featureUser-defined. Set by the CLI or the package manager

target_family / target_env / target_abi are absent when the target has none.

All target_* keys follow the build's --target (9.1.2). Without --target, the host target applies.

#Plain flags

There is also a boolean flag form with only a key and no value.

nabi
#[cfg(test)]                 // only in test builds
#[cfg(debug_assertions)]      // only in debug builds

#Combinators

To combine several conditions, use combinators.

nabi
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[cfg(not(target_os = "windows"))]

The meaning of each combinator:

CombinatorMeaning
all(...)Every argument is true
any(...)At least one is true
not(...)The argument is false

Combinators may be nested freely.

nabi
#[cfg(all(
    target_os = "linux",
    any(target_arch = "x86_64", target_arch = "aarch64"),
    not(feature = "minimal"),
))]
fn linux_native() { ... }

#6.3.3. Attachment positions

#[cfg] applies to items, fields (named, tuple, union, variant and tuple-variant fields), enum variants, items of a trait, impl or extern block, statements, match arms, struct literal fields and function parameters. A cfg-false parameter is removed from the signature; calls pass the arguments of the remaining parameters. Closure parameters take no attributes.

nabi
#[cfg(...)] fn f() { ... }
#[cfg(...)] struct S { ... }
#[cfg(...)] mod m { ... }
#[cfg(...)] use foo::bar;
#[cfg(...)] static X: i32 = 0;

struct Foo {
    #[cfg(target_pointer_width = "64")]
    big_field: u64;                              // exists only on 64-bit
}

fn run(op: Op) {
    #[cfg(test)] check();                        // statement
    match op {
        Op::Add => add(),
        #[cfg(debug_assertions)]
        Op::Trace => trace(),                    // match arm
        _ => {},
    }
}

If the condition is false, the item is treated as nonexistent: it is removed right after parsing, before derives and other attributes see it (6.3.7). The output of meta-block expansions is parsed with the build's cfg, so #[cfg] inside it applies the same way.

#6.3.4. #[cfg_attr(...)] conditional other attributes

#[cfg_attr] attaches another attribute conditionally.

nabi
#[cfg_attr(target_os = "windows", link("kernel32"))]
extern "C" { ... }

#[cfg_attr(test, derive(Debug))]
struct S { ... }

The form is cfg_attr(condition, attribute_to_apply); if the condition is true, the following attribute behaves as if attached directly. In the first example #[link("kernel32")] applies only in Windows builds; in the second, #[derive(Debug)] applies only in test builds.

Several attributes can be applied at once.

nabi
#[cfg_attr(test, derive(Debug), allow(unused))]
struct S { ... }

#6.3.5. cfg!(...) compile-time bool value

cfg!(...) appears in expression position and evaluates to a bool at compile time.

nabi
const IS_LINUX: bool = cfg!(target_os = "linux");

if cfg!(feature = "gpu") {
    use_gpu()
} else {
    use_cpu()
}

It takes the same predicate as #[cfg], and the result is a const expression in the sense of const, so it can be used in other const positions as well. cfg!, like include_str! / include_bytes! (6.11), is a compiler built-in form, not a macro (the call-triggered meta definition, 6.8.12): it is fixed in the compiler and cannot be redefined or shadowed, and its name is reserved.

The difference between #[cfg] and cfg!():

  • #[cfg]: includes or excludes an entire item from the build.
  • cfg!(): looks like a runtime branch but is actually a compile-time constant, so one branch is removed by dead-code elimination.
nabi
// #[cfg]: only one of the two functions is compiled
#[cfg(feature = "gpu")] fn run() { use_gpu() }
#[cfg(not(feature = "gpu"))] fn run() { use_cpu() }

// cfg!(): branch within one function; the compiler removes one side
fn run() {
    if cfg!(feature = "gpu") { use_gpu() } else { use_cpu() }
}

Choose between the two forms according to convenience and code structure.

#6.3.6. Where parameters come from

#target_* keys

The target_* keys are set automatically according to the build's target platform. They have well-defined values for every supported target in platform targets. The values "riscv64", "riscv32", "powerpc64", "s390x" and target_os = "none" come only from targets in progress (0.6.2).

#The feature key

The feature key is set by a CLI option or by the package manager and build tools.

bash
nabic build --cfg feature=gpu

The package manager nabi sets it per package: each package is compiled with feature = "<f>" for exactly the features the build enables for it (9.10.4c). nabic --cfg feature=<f> sets a feature of the package being compiled only; a dependency's features are given with --extern-feature <name>=<f> (9.1), and the standard library has none.

#test / debug_assertions

These two flags are determined by the build mode.

  • test: true when building with nabic test or an equivalent command.
  • debug_assertions: true at -O0.

#6.3.7. When it is evaluated

#[cfg] is evaluated right after parsing. Items with false conditions are removed from the AST immediately, and later passes (name resolution, type checking, code generation) see only the remaining AST.

The compiler remembers the names it removed and their conditions. When a lookup of a method, free function, type or module (or an import) fails and a removed item has that name, the error says where it exists:

error[E0046]: no method `pidfd` found for type `Child`
  --> src/main.nabi:4:13
   |
 4 |     let p = c.pidfd();
   |             ^^^^^^^^^
   |
   = help: `Child` is a standard type; call one of its methods, such as `id`, `into`, `kill`, `fmt`, `handle`, `wait`
   = note: `pidfd` is only available when its `cfg` holds (`#[cfg(any(target_os = "linux", target_os = "android"))]`)
   = help: put the code that uses it under the same `#[cfg(any(target_os = "linux", target_os = "android"))]`

This precedes the meta-expansion pass (template tokens and expansion), so user-defined derives and attributes attached to items whose cfg is false are not processed either.

#6.3.8. Negated / conflicting cfgs

If conditions are true simultaneously in the same build and two definitions survive, the ordinary duplicate definition error occurs.

nabi
#[cfg(target_os = "linux")]
fn impl_a() { ... }

#[cfg(target_os = "linux")]    // same cfg + same name → conflict
fn impl_a() { ... }            // ERROR: both definitions exist in the same build

The following is fine, because the two cfgs are mutually exclusive and only one definition is active in any build.

nabi
#[cfg(target_os = "linux")]
fn impl_a() { ... }

#[cfg(not(target_os = "linux"))]
fn impl_a() { ... }            // OK: the two cfgs are mutually exclusive

#6.3.9. cfg and modules/files

A cfg can be attached to a whole module.

nabi
#[cfg(target_os = "linux")]
mod linux_only {
    // this whole module exists only in Linux builds
}

The module's files and directories still exist on disk, but if the cfg is false they are not read, or are discarded immediately if read.

#6.3.10. Macros inside cfg (none)

Arbitrary expressions or macro calls cannot appear inside a cfg predicate. Only the fixed key-value forms and combinators are allowed.

#[cfg(custom_macro!())]                // not adopted

#6.3.11. Debugging tools

nabic --print cfg prints the cfg values active in the build: the target keys of --target, the --cfg flags and debug_assertions below -O1 (9.1.3).

Found a mistake? Edit this page on GitHub. Last updated