Skip to content

Commit 206755b

Browse files
authored
Merge pull request #1564 from chorman0773/spec-add-identifiers-conditional-compilation
Add spec identifier syntax to conditional-compilation.md
2 parents 369f949 + 1749989 commit 206755b

File tree

1 file changed

+117
-10
lines changed

1 file changed

+117
-10
lines changed

src/conditional-compilation.md

+117-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Conditional compilation
22

3+
r[cfg]
4+
5+
r[cfg.syntax]
36
> **<sup>Syntax</sup>**\
47
> _ConfigurationPredicate_ :\
58
> &nbsp;&nbsp; &nbsp;&nbsp; _ConfigurationOption_\
@@ -22,30 +25,61 @@
2225
> _ConfigurationPredicateList_\
2326
> &nbsp;&nbsp; _ConfigurationPredicate_ (`,` _ConfigurationPredicate_)<sup>\*</sup> `,`<sup>?</sup>
2427
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.
28+
r[cfg.general]
29+
*Conditionally compiled source code* is source code that is compiled only under certain conditions.
30+
31+
r[cfg.attributes-macro]
32+
Source code can be made conditionally compiled using the [`cfg`] and [`cfg_attr`] [attributes] and the built-in [`cfg` macro].
33+
34+
r[cfg.conditional]
35+
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.
2636

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

41+
r[cfg.predicate.option]
3042
* A configuration option. The predicate is true if the option is set, and false if it is unset.
43+
44+
r[cfg.predicate.all]
3145
* `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.
46+
47+
r[cfg.predicate.any]
3248
* `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.
49+
50+
r[cfg.predicate.not]
3351
* `not()` with a configuration predicate. It is true if its predicate is false and false if its predicate is true.
3452

53+
r[cfg.option-spec]
3554
_Configuration options_ are either names or key-value pairs, and are either set or unset.
55+
56+
r[cfg.option-name]
3657
Names are written as a single identifier, such as `unix`.
58+
59+
r[cfg.option-key-value]
3760
Key-value pairs are written as an identifier, `=`, and then a string, such as `target_arch = "x86_64"`.
3861

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

4367
## Set Configuration Options
4468

69+
r[cfg.options.set]
70+
71+
r[cfg.options.general]
4572
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
73+
compilation of the crate.
74+
75+
r[cfg.options.target]
76+
Some options are _compiler-set_ based on data about the compilation.
77+
78+
r[cfg.options.other]
79+
Other options are _arbitrarily-set_ based on input passed to the compiler outside of the code.
80+
81+
r[cfg.options.crate]
82+
It is not possible to set a
4983
configuration option from within the source code of the crate being compiled.
5084

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

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

104+
r[cfg.target_arch.values]
67105
Example values:
68106

69107
* `"x86"`
@@ -76,9 +114,13 @@ Example values:
76114

77115
### `target_feature`
78116

117+
r[cfg.target_feature]
118+
119+
r[cfg.target_feature.general]
79120
Key-value option set for each platform feature available for the current
80121
compilation target.
81122

123+
r[cfg.target_feature.values]
82124
Example values:
83125

84126
* `"avx"`
@@ -90,14 +132,21 @@ Example values:
90132
* `"sse4.1"`
91133

92134
See the [`target_feature` attribute] for more details on the available
93-
features. An additional feature of `crt-static` is available to the
135+
features.
136+
137+
r[cfg.target_feature.crt_static]
138+
An additional feature of `crt-static` is available to the
94139
`target_feature` option to indicate that a [static C runtime] is available.
95140

96141
### `target_os`
97142

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

149+
r[cfg.target_os.values]
101150
Example values:
102151

103152
* `"windows"`
@@ -113,10 +162,14 @@ Example values:
113162

114163
### `target_family`
115164

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

172+
r[cfg.target_family.values]
120173
Example values:
121174

122175
* `"unix"`
@@ -126,11 +179,17 @@ Example values:
126179

127180
### `unix` and `windows`
128181

129-
`unix` is set if `target_family = "unix"` is set and `windows` is set if
130-
`target_family = "windows"` is set.
182+
r[cfg.target_family.unix]
183+
`unix` is set if `target_family = "unix"` is set.
184+
185+
r[cfg.target_family.windows]
186+
`windows` is set if `target_family = "windows"` is set.
131187

132188
### `target_env`
133189

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

201+
r[cfg.target_env.values]
142202
Example values:
143203

144204
* `""`
@@ -149,12 +209,18 @@ Example values:
149209

150210
### `target_abi`
151211

212+
r[cfg.target_abi]
213+
214+
r[cfg.target_abi.general]
152215
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
216+
about the target ABI.
217+
218+
r[cfg.target_abi.disambiguation]
219+
For historical reasons, this value is only defined as not the empty-string when actually
220+
needed for disambiguation. Thus, for example, on many GNU platforms, this value will be
156221
empty.
157222

223+
r[cfg.target_abi.values]
158224
Example values:
159225

160226
* `""`
@@ -166,13 +232,18 @@ Example values:
166232

167233
### `target_endian`
168234

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

172239
### `target_pointer_width`
173240

241+
r[cfg.target_poitner_width]
242+
243+
r[cfg.target_pointer_width.general]
174244
Key-value option set once with the target's pointer width in bits.
175245

246+
r[cfg.target_pointer_width.values]
176247
Example values:
177248

178249
* `"16"`
@@ -181,8 +252,12 @@ Example values:
181252

182253
### `target_vendor`
183254

255+
r[cfg.target_vendor]
256+
257+
r[cfg.target_vendor.general]
184258
Key-value option set once with the vendor of the target.
185259

260+
r[cfg.target_vendor.values]
186261
Example values:
187262

188263
* `"apple"`
@@ -192,12 +267,18 @@ Example values:
192267

193268
### `target_has_atomic`
194269

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

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

280+
281+
r[cfg.target_has_atomic.values]
201282
Possible values:
202283

203284
* `"8"`
@@ -209,25 +290,35 @@ Possible values:
209290

210291
### `test`
211292

293+
r[cfg.test]
294+
212295
Enabled when compiling the test harness. Done with `rustc` by using the
213296
[`--test`] flag. See [Testing] for more on testing support.
214297

215298
### `debug_assertions`
216299

300+
r[cfg.debug_assertions]
301+
217302
Enabled by default when compiling without optimizations.
218303
This can be used to enable extra debugging code in development but not in
219304
production. For example, it controls the behavior of the standard library's
220305
[`debug_assert!`] macro.
221306

222307
### `proc_macro`
223308

309+
r[cfg.proc_macro]
310+
224311
Set when the crate being compiled is being compiled with the `proc_macro`
225312
[crate type].
226313

227314
### `panic`
228315

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

321+
r[cfg.panic.values]
231322
Example values:
232323

233324
* `"abort"`
@@ -237,20 +328,27 @@ Example values:
237328

238329
### The `cfg` attribute
239330

331+
r[cfg.attr]
332+
333+
r[cfg.attr.syntax]
240334
> **<sup>Syntax</sup>**\
241335
> _CfgAttrAttribute_ :\
242336
> &nbsp;&nbsp; `cfg` `(` _ConfigurationPredicate_ `)`
243337
244338
<!-- should we say they're active attributes here? -->
245339

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

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

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

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

294392
```
295393

394+
r[cfg.attr.restriction]
296395
The `cfg` attribute is allowed anywhere attributes are allowed.
297396

298397
### The `cfg_attr` attribute
299398

399+
r[cfg.cfg_attr]
400+
401+
r[cfg.cfg_attr.syntax]
300402
> **<sup>Syntax</sup>**\
301403
> _CfgAttrAttribute_ :\
302404
> &nbsp;&nbsp; `cfg_attr` `(` _ConfigurationPredicate_ `,` _CfgAttrs_<sup>?</sup> `)`
303405
>
304406
> _CfgAttrs_ :\
305407
> &nbsp;&nbsp; [_Attr_]&nbsp;(`,` [_Attr_])<sup>\*</sup> `,`<sup>?</sup>
306408
409+
r[cfg.cfg_attr.general]
307410
The `cfg_attr` [attribute] conditionally includes [attributes] based on a
308411
configuration predicate.
309412

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

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

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

342448
### The `cfg` macro
343449

450+
r[cfg.macro]
344451
The built-in `cfg` macro takes in a single configuration predicate and evaluates
345452
to the `true` literal when the predicate is true and the `false` literal when
346453
it is false.

0 commit comments

Comments
 (0)