Skip to content

Commit e7db03c

Browse files
committed
Add identifier syntax to conditional-compilation.md
1 parent cf88463 commit e7db03c

File tree

1 file changed

+131
-10
lines changed

1 file changed

+131
-10
lines changed

src/conditional-compilation.md

Lines changed: 131 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Conditional compilation
22

3+
r[cfg]
4+
5+
6+
r[cfg.syntax]
37
> **<sup>Syntax</sup>**\
48
> _ConfigurationPredicate_ :\
59
> &nbsp;&nbsp; &nbsp;&nbsp; _ConfigurationOption_\
@@ -22,30 +26,62 @@
2226
> _ConfigurationPredicateList_\
2327
> &nbsp;&nbsp; _ConfigurationPredicate_ (`,` _ConfigurationPredicate_)<sup>\*</sup> `,`<sup>?</sup>
2428
25-
*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.
29+
r[cfg.general]
30+
*Conditionally compiled source code* is source code that is compiled only under certain conditions.
31+
32+
r[cfg.attributes-macro]
33+
Source code can be made conditionally compiled using the [`cfg`] and [`cfg_attr`] [attributes] and the built-in [`cfg` macro].
2634

35+
r[cfg.conditional]
36+
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.
37+
38+
r[cfg.predicate]
2739
Each form of conditional compilation takes a _configuration predicate_ that
2840
evaluates to true or false. The predicate is one of the following:
2941

42+
r[cfg.predicate.option]
3043
* A configuration option. The predicate is true if the option is set, and false if it is unset.
44+
45+
r[cfg.predicate.all]
3146
* `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.
47+
48+
r[cfg.predicate.any]
3249
* `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.
50+
51+
r[cfg.predicate.not]
3352
* `not()` with a configuration predicate. It is true if its predicate is false and false if its predicate is true.
3453

54+
r[cfg.option-spec]
3555
_Configuration options_ are either names or key-value pairs, and are either set or unset.
56+
57+
r[cfg.option-name]
3658
Names are written as a single identifier, such as `unix`.
59+
60+
r[cfg.option-key-value]
3761
Key-value pairs are written as an identifier, `=`, and then a string, such as `target_arch = "x86_64"`.
3862

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

4368
## Set Configuration Options
4469

70+
r[cfg.options.set]
71+
72+
73+
r[cfg.options.general]
4574
Which configuration options are set is determined statically during the
46-
compilation of the crate. Some options are _compiler-set_ based on data
47-
about the compilation. Other options are _arbitrarily-set_ based on input
48-
passed to the compiler outside of the code. It is not possible to set a
75+
compilation of the crate.
76+
77+
r[cfg.options.target]
78+
Some options are _compiler-set_ based on data about the compilation.
79+
80+
r[cfg.options.other]
81+
Other options are _arbitrarily-set_ based on input passed to the compiler outside of the code.
82+
83+
r[cfg.options.crate]
84+
It is not possible to set a
4985
configuration option from within the source code of the crate being compiled.
5086

5187
> **Note**: For `rustc`, arbitrary-set configuration options are set using the
@@ -60,10 +96,15 @@ configuration option from within the source code of the crate being compiled.
6096
6197
### `target_arch`
6298

99+
r[cfg.target_arch]
100+
101+
102+
r[cfg.target_arch.gen]
63103
Key-value option set once with the target's CPU architecture. The value is
64104
similar to the first element of the platform's target triple, but not
65105
identical.
66106

107+
r[cfg.target_arch.values]
67108
Example values:
68109

69110
* `"x86"`
@@ -76,9 +117,14 @@ Example values:
76117

77118
### `target_feature`
78119

120+
r[cfg.target_feature]
121+
122+
123+
r[cfg.target_feature.general]
79124
Key-value option set for each platform feature available for the current
80125
compilation target.
81126

127+
r[cfg.target_feature.values]
82128
Example values:
83129

84130
* `"avx"`
@@ -90,14 +136,22 @@ Example values:
90136
* `"sse4.1"`
91137

92138
See the [`target_feature` attribute] for more details on the available
93-
features. An additional feature of `crt-static` is available to the
139+
features.
140+
141+
r[cfg.target_feature.crt_static]
142+
An additional feature of `crt-static` is available to the
94143
`target_feature` option to indicate that a [static C runtime] is available.
95144

96145
### `target_os`
97146

147+
r[cfg.target_os]
148+
149+
150+
r[cfg.target_os.general]
98151
Key-value option set once with the target's operating system. This value is
99152
similar to the second and third element of the platform's target triple.
100153

154+
r[cfg.target_os.values]
101155
Example values:
102156

103157
* `"windows"`
@@ -113,10 +167,15 @@ Example values:
113167

114168
### `target_family`
115169

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

178+
r[cfg.target_family.values]
120179
Example values:
121180

122181
* `"unix"`
@@ -126,11 +185,17 @@ Example values:
126185

127186
### `unix` and `windows`
128187

129-
`unix` is set if `target_family = "unix"` is set and `windows` is set if
130-
`target_family = "windows"` is set.
188+
r[cfg.target_family.unix]
189+
`unix` is set if `target_family = "unix"` is set.
190+
191+
r[cfg.target_family.windows]
192+
`windows` is set if `target_family = "windows"` is set.
131193

132194
### `target_env`
133195

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

207+
r[cfg.target_env.values]
142208
Example values:
143209

144210
* `""`
@@ -149,12 +215,19 @@ Example values:
149215

150216
### `target_abi`
151217

218+
r[cfg.target_abi]
219+
220+
221+
r[cfg.target_env.general]
152222
Key-value option set to further disambiguate the `target_env` with information
153-
about the target ABI. For historical reasons,
154-
this value is only defined as not the empty-string when actually needed for
155-
disambiguation. Thus, for example, on many GNU platforms, this value will be
223+
about the target ABI.
224+
225+
r[cfg.target_env.disambiguation]
226+
For historical reasons, this value is only defined as not the empty-string when actually
227+
needed for disambiguation. Thus, for example, on many GNU platforms, this value will be
156228
empty.
157229

230+
r[cfg.target_env.values]
158231
Example values:
159232

160233
* `""`
@@ -166,13 +239,19 @@ Example values:
166239

167240
### `target_endian`
168241

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

172246
### `target_pointer_width`
173247

248+
r[cfg.target_poitner_width]
249+
250+
251+
r[cfg.target_pointer_width.general]
174252
Key-value option set once with the target's pointer width in bits.
175253

254+
r[cfg.target_pointer_width.values]
176255
Example values:
177256

178257
* `"16"`
@@ -181,8 +260,13 @@ Example values:
181260

182261
### `target_vendor`
183262

263+
r[cfg.target_vendor]
264+
265+
266+
r[cfg.target_vendor.general]
184267
Key-value option set once with the vendor of the target.
185268

269+
r[cfg.target_vendor.values]
186270
Example values:
187271

188272
* `"apple"`
@@ -192,12 +276,19 @@ Example values:
192276

193277
### `target_has_atomic`
194278

279+
r[cfg.target_has_atomic]
280+
281+
282+
r[cfg.target_has_atomic.general]
195283
Key-value option set for each bit width that the target supports
196284
atomic loads, stores, and compare-and-swap operations.
197285

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

290+
291+
r[cfg.target_has_atomic.values]
201292
Possible values:
202293

203294
* `"8"`
@@ -209,25 +300,36 @@ Possible values:
209300

210301
### `test`
211302

303+
r[cfg.test]
304+
212305
Enabled when compiling the test harness. Done with `rustc` by using the
213306
[`--test`] flag. See [Testing] for more on testing support.
214307

215308
### `debug_assertions`
216309

310+
r[cfg.debug_assertions]
311+
217312
Enabled by default when compiling without optimizations.
218313
This can be used to enable extra debugging code in development but not in
219314
production. For example, it controls the behavior of the standard library's
220315
[`debug_assert!`] macro.
221316

222317
### `proc_macro`
223318

319+
r[cfg.proc_macro]
320+
224321
Set when the crate being compiled is being compiled with the `proc_macro`
225322
[crate type].
226323

227324
### `panic`
228325

326+
r[cfg.panic]
327+
328+
329+
r[cfg.panic.general]
229330
Key-value option set depending on the panic strategy. Note that more values may be added in the future.
230331

332+
r[cfg.panic.values]
231333
Example values:
232334

233335
* `"abort"`
@@ -237,20 +339,28 @@ Example values:
237339

238340
### The `cfg` attribute
239341

342+
r[cfg.attr]
343+
344+
345+
r[cfg.attr.syntax]
240346
> **<sup>Syntax</sup>**\
241347
> _CfgAttrAttribute_ :\
242348
> &nbsp;&nbsp; `cfg` `(` _ConfigurationPredicate_ `)`
243349
244350
<!-- should we say they're active attributes here? -->
245351

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

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

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

363+
r[cfg.attr.crate-level-attrs]
254364
When a crate-level `cfg` has a false predicate, the behavior is slightly
255365
different: any crate attributes preceding the `cfg` are kept, and any crate
256366
attributes following the `cfg` are removed. This allows `#![no_std]` and
@@ -293,20 +403,28 @@ fn when_unwinding() {
293403

294404
```
295405

406+
r[cfg.attr.restriction]
296407
The `cfg` attribute is allowed anywhere attributes are allowed.
297408

298409
### The `cfg_attr` attribute
299410

411+
r[cfg.cfg_attr]
412+
413+
414+
r[cfg.cfg_attr.syntax]
300415
> **<sup>Syntax</sup>**\
301416
> _CfgAttrAttribute_ :\
302417
> &nbsp;&nbsp; `cfg_attr` `(` _ConfigurationPredicate_ `,` _CfgAttrs_<sup>?</sup> `)`
303418
>
304419
> _CfgAttrs_ :\
305420
> &nbsp;&nbsp; [_Attr_]&nbsp;(`,` [_Attr_])<sup>\*</sup> `,`<sup>?</sup>
306421
422+
423+
r[cfg.cfg_attr.syntax]
307424
The `cfg_attr` [attribute] conditionally includes [attributes] based on a
308425
configuration predicate.
309426

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

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

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

342462
### The `cfg` macro
343463

464+
r[cfg.macro]
344465
The built-in `cfg` macro takes in a single configuration predicate and evaluates
345466
to the `true` literal when the predicate is true and the `false` literal when
346467
it is false.

0 commit comments

Comments
 (0)