Skip to content

Commit

Permalink
Merge pull request #1564 from chorman0773/spec-add-identifiers-condit…
Browse files Browse the repository at this point in the history
…ional-compilation

Add spec identifier syntax to conditional-compilation.md
  • Loading branch information
traviscross authored Oct 21, 2024
2 parents 369f949 + 1749989 commit 206755b
Showing 1 changed file with 117 additions and 10 deletions.
127 changes: 117 additions & 10 deletions src/conditional-compilation.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Conditional compilation

r[cfg]

r[cfg.syntax]
> **<sup>Syntax</sup>**\
> _ConfigurationPredicate_ :\
> &nbsp;&nbsp; &nbsp;&nbsp; _ConfigurationOption_\
Expand All @@ -22,30 +25,61 @@
> _ConfigurationPredicateList_\
> &nbsp;&nbsp; _ConfigurationPredicate_ (`,` _ConfigurationPredicate_)<sup>\*</sup> `,`<sup>?</sup>
*Conditionally compiled source code* is source code that is compiled only under certain conditions. Source code can be made conditionally compiled using the [`cfg`] and [`cfg_attr`] [attributes] and the built-in [`cfg` macro]. Whether to compile can depend on the target architecture of the compiled crate, arbitrary values passed to the compiler, and other things further described below.
r[cfg.general]
*Conditionally compiled source code* is source code that is compiled only under certain conditions.

r[cfg.attributes-macro]
Source code can be made conditionally compiled using the [`cfg`] and [`cfg_attr`] [attributes] and the built-in [`cfg` macro].

r[cfg.conditional]
Whether to compile can depend on the target architecture of the compiled crate, arbitrary values passed to the compiler, and other things further described below.

r[cfg.predicate]
Each form of conditional compilation takes a _configuration predicate_ that
evaluates to true or false. The predicate is one of the following:

r[cfg.predicate.option]
* A configuration option. The predicate is true if the option is set, and false if it is unset.

r[cfg.predicate.all]
* `all()` with a comma-separated list of configuration predicates. It is true if all of the given predicates are true, or if the list is empty.

r[cfg.predicate.any]
* `any()` with a comma-separated list of configuration predicates. It is true if at least one of the given predicates is true. If there are no predicates, it is false.

r[cfg.predicate.not]
* `not()` with a configuration predicate. It is true if its predicate is false and false if its predicate is true.

r[cfg.option-spec]
_Configuration options_ are either names or key-value pairs, and are either set or unset.

r[cfg.option-name]
Names are written as a single identifier, such as `unix`.

r[cfg.option-key-value]
Key-value pairs are written as an identifier, `=`, and then a string, such as `target_arch = "x86_64"`.

> **Note**: Whitespace around the `=` is ignored, so `foo="bar"` and `foo = "bar"` are equivalent.
r[cfg.option-key-uniqueness]
Keys do not need to be unique. For example, both `feature = "std"` and `feature = "serde"` can be set at the same time.

## Set Configuration Options

r[cfg.options.set]

r[cfg.options.general]
Which configuration options are set is determined statically during the
compilation of the crate. Some options are _compiler-set_ based on data
about the compilation. Other options are _arbitrarily-set_ based on input
passed to the compiler outside of the code. It is not possible to set a
compilation of the crate.

r[cfg.options.target]
Some options are _compiler-set_ based on data about the compilation.

r[cfg.options.other]
Other options are _arbitrarily-set_ based on input passed to the compiler outside of the code.

r[cfg.options.crate]
It is not possible to set a
configuration option from within the source code of the crate being compiled.

> **Note**: For `rustc`, arbitrary-set configuration options are set using the
Expand All @@ -60,10 +94,14 @@ configuration option from within the source code of the crate being compiled.
### `target_arch`

r[cfg.target_arch]

r[cfg.target_arch.gen]
Key-value option set once with the target's CPU architecture. The value is
similar to the first element of the platform's target triple, but not
identical.

r[cfg.target_arch.values]
Example values:

* `"x86"`
Expand All @@ -76,9 +114,13 @@ Example values:

### `target_feature`

r[cfg.target_feature]

r[cfg.target_feature.general]
Key-value option set for each platform feature available for the current
compilation target.

r[cfg.target_feature.values]
Example values:

* `"avx"`
Expand All @@ -90,14 +132,21 @@ Example values:
* `"sse4.1"`

See the [`target_feature` attribute] for more details on the available
features. An additional feature of `crt-static` is available to the
features.

r[cfg.target_feature.crt_static]
An additional feature of `crt-static` is available to the
`target_feature` option to indicate that a [static C runtime] is available.

### `target_os`

r[cfg.target_os]

r[cfg.target_os.general]
Key-value option set once with the target's operating system. This value is
similar to the second and third element of the platform's target triple.

r[cfg.target_os.values]
Example values:

* `"windows"`
Expand All @@ -113,10 +162,14 @@ Example values:

### `target_family`

r[cfg.target_family]

r[cfg.target_family.general]
Key-value option providing a more generic description of a target, such as the family of the
operating systems or architectures that the target generally falls into. Any number of
`target_family` key-value pairs can be set.

r[cfg.target_family.values]
Example values:

* `"unix"`
Expand All @@ -126,11 +179,17 @@ Example values:

### `unix` and `windows`

`unix` is set if `target_family = "unix"` is set and `windows` is set if
`target_family = "windows"` is set.
r[cfg.target_family.unix]
`unix` is set if `target_family = "unix"` is set.

r[cfg.target_family.windows]
`windows` is set if `target_family = "windows"` is set.

### `target_env`

r[cfg.target_env]

r[cfg.target_env.general]
Key-value option set with further disambiguating information about the target
platform with information about the ABI or `libc` used. For historical reasons,
this value is only defined as not the empty-string when actually needed for
Expand All @@ -139,6 +198,7 @@ empty. This value is similar to the fourth element of the platform's target
triple. One difference is that embedded ABIs such as `gnueabihf` will simply
define `target_env` as `"gnu"`.

r[cfg.target_env.values]
Example values:

* `""`
Expand All @@ -149,12 +209,18 @@ Example values:

### `target_abi`

r[cfg.target_abi]

r[cfg.target_abi.general]
Key-value option set to further disambiguate the `target_env` with information
about the target ABI. For historical reasons,
this value is only defined as not the empty-string when actually needed for
disambiguation. Thus, for example, on many GNU platforms, this value will be
about the target ABI.

r[cfg.target_abi.disambiguation]
For historical reasons, this value is only defined as not the empty-string when actually
needed for disambiguation. Thus, for example, on many GNU platforms, this value will be
empty.

r[cfg.target_abi.values]
Example values:

* `""`
Expand All @@ -166,13 +232,18 @@ Example values:

### `target_endian`

r[cfg.target_endian]
Key-value option set once with either a value of "little" or "big" depending
on the endianness of the target's CPU.

### `target_pointer_width`

r[cfg.target_poitner_width]

r[cfg.target_pointer_width.general]
Key-value option set once with the target's pointer width in bits.

r[cfg.target_pointer_width.values]
Example values:

* `"16"`
Expand All @@ -181,8 +252,12 @@ Example values:

### `target_vendor`

r[cfg.target_vendor]

r[cfg.target_vendor.general]
Key-value option set once with the vendor of the target.

r[cfg.target_vendor.values]
Example values:

* `"apple"`
Expand All @@ -192,12 +267,18 @@ Example values:

### `target_has_atomic`

r[cfg.target_has_atomic]

r[cfg.target_has_atomic.general]
Key-value option set for each bit width that the target supports
atomic loads, stores, and compare-and-swap operations.

r[cfg.target_has_atomic.stdlib]
When this cfg is present, all of the stable [`core::sync::atomic`] APIs are available for
the relevant atomic width.


r[cfg.target_has_atomic.values]
Possible values:

* `"8"`
Expand All @@ -209,25 +290,35 @@ Possible values:

### `test`

r[cfg.test]

Enabled when compiling the test harness. Done with `rustc` by using the
[`--test`] flag. See [Testing] for more on testing support.

### `debug_assertions`

r[cfg.debug_assertions]

Enabled by default when compiling without optimizations.
This can be used to enable extra debugging code in development but not in
production. For example, it controls the behavior of the standard library's
[`debug_assert!`] macro.

### `proc_macro`

r[cfg.proc_macro]

Set when the crate being compiled is being compiled with the `proc_macro`
[crate type].

### `panic`

r[cfg.panic]

r[cfg.panic.general]
Key-value option set depending on the panic strategy. Note that more values may be added in the future.

r[cfg.panic.values]
Example values:

* `"abort"`
Expand All @@ -237,20 +328,27 @@ Example values:

### The `cfg` attribute

r[cfg.attr]

r[cfg.attr.syntax]
> **<sup>Syntax</sup>**\
> _CfgAttrAttribute_ :\
> &nbsp;&nbsp; `cfg` `(` _ConfigurationPredicate_ `)`
<!-- should we say they're active attributes here? -->

r[cfg.attr.general]
The `cfg` [attribute] conditionally includes the thing it is attached to based
on a configuration predicate.

r[cfg.attr.syntax-explanation]
It is written as `cfg`, `(`, a configuration predicate, and finally `)`.

r[cfg.attr.effect]
If the predicate is true, the thing is rewritten to not have the `cfg` attribute
on it. If the predicate is false, the thing is removed from the source code.

r[cfg.attr.crate-level-attrs]
When a crate-level `cfg` has a false predicate, the behavior is slightly
different: any crate attributes preceding the `cfg` are kept, and any crate
attributes following the `cfg` are removed. This allows `#![no_std]` and
Expand Down Expand Up @@ -293,20 +391,26 @@ fn when_unwinding() {

```

r[cfg.attr.restriction]
The `cfg` attribute is allowed anywhere attributes are allowed.

### The `cfg_attr` attribute

r[cfg.cfg_attr]

r[cfg.cfg_attr.syntax]
> **<sup>Syntax</sup>**\
> _CfgAttrAttribute_ :\
> &nbsp;&nbsp; `cfg_attr` `(` _ConfigurationPredicate_ `,` _CfgAttrs_<sup>?</sup> `)`
>
> _CfgAttrs_ :\
> &nbsp;&nbsp; [_Attr_]&nbsp;(`,` [_Attr_])<sup>\*</sup> `,`<sup>?</sup>
r[cfg.cfg_attr.general]
The `cfg_attr` [attribute] conditionally includes [attributes] based on a
configuration predicate.

r[cfg.cfg_attr.behaviour]
When the configuration predicate is true, this attribute expands out to the
attributes listed after the predicate. For example, the following module will
either be found at `linux.rs` or `windows.rs` based on the target.
Expand All @@ -318,6 +422,7 @@ either be found at `linux.rs` or `windows.rs` based on the target.
mod os;
```

r[cfg.cfg_attr.attribute-list]
Zero, one, or more attributes may be listed. Multiple attributes will each be
expanded into separate attributes. For example:

Expand All @@ -337,10 +442,12 @@ fn bewitched() {}
> is valid. This example would be equivalent to
> `#[cfg_attr(all(target_os = "linux", feature ="multithreaded"), some_other_attribute)]`.
r[cfg.cfg_attr.restriction]
The `cfg_attr` attribute is allowed anywhere attributes are allowed.

### The `cfg` macro

r[cfg.macro]
The built-in `cfg` macro takes in a single configuration predicate and evaluates
to the `true` literal when the predicate is true and the `false` literal when
it is false.
Expand Down

0 comments on commit 206755b

Please sign in to comment.