You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*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].
26
34
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]
27
39
Each form of conditional compilation takes a _configuration predicate_ that
28
40
evaluates to true or false. The predicate is one of the following:
29
41
42
+
r[cfg.predicate.option]
30
43
* A configuration option. The predicate is true if the option is set, and false if it is unset.
44
+
45
+
r[cfg.predicate.all]
31
46
*`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]
32
49
*`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]
33
52
*`not()` with a configuration predicate. It is true if its predicate is false and false if its predicate is true.
34
53
54
+
r[cfg.option-spec]
35
55
_Configuration options_ are either names or key-value pairs, and are either set or unset.
56
+
57
+
r[cfg.option-name]
36
58
Names are written as a single identifier, such as `unix`.
59
+
60
+
r[cfg.option-key-value]
37
61
Key-value pairs are written as an identifier, `=`, and then a string, such as `target_arch = "x86_64"`.
38
62
39
63
> **Note**: Whitespace around the `=` is ignored, so `foo="bar"` and `foo = "bar"` are equivalent.
40
64
65
+
r[cfg.option-key-uniqueness]
41
66
Keys do not need to be unique. For example, both `feature = "std"` and `feature = "serde"` can be set at the same time.
42
67
43
68
## Set Configuration Options
44
69
70
+
r[cfg.options.set]
71
+
72
+
73
+
r[cfg.options.general]
45
74
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
49
85
configuration option from within the source code of the crate being compiled.
50
86
51
87
> **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.
60
96
61
97
### `target_arch`
62
98
99
+
r[cfg.target_arch]
100
+
101
+
102
+
r[cfg.target_arch.gen]
63
103
Key-value option set once with the target's CPU architecture. The value is
64
104
similar to the first element of the platform's target triple, but not
65
105
identical.
66
106
107
+
r[cfg.target_arch.values]
67
108
Example values:
68
109
69
110
*`"x86"`
@@ -76,9 +117,14 @@ Example values:
76
117
77
118
### `target_feature`
78
119
120
+
r[cfg.target_feature]
121
+
122
+
123
+
r[cfg.target_feature.general]
79
124
Key-value option set for each platform feature available for the current
80
125
compilation target.
81
126
127
+
r[cfg.target_feature.values]
82
128
Example values:
83
129
84
130
*`"avx"`
@@ -90,14 +136,22 @@ Example values:
90
136
*`"sse4.1"`
91
137
92
138
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
94
143
`target_feature` option to indicate that a [static C runtime] is available.
95
144
96
145
### `target_os`
97
146
147
+
r[cfg.target_os]
148
+
149
+
150
+
r[cfg.target_os.general]
98
151
Key-value option set once with the target's operating system. This value is
99
152
similar to the second and third element of the platform's target triple.
100
153
154
+
r[cfg.target_os.values]
101
155
Example values:
102
156
103
157
*`"windows"`
@@ -113,10 +167,15 @@ Example values:
113
167
114
168
### `target_family`
115
169
170
+
r[cfg.target_family]
171
+
172
+
173
+
r[cfg.target_family.general]
116
174
Key-value option providing a more generic description of a target, such as the family of the
117
175
operating systems or architectures that the target generally falls into. Any number of
118
176
`target_family` key-value pairs can be set.
119
177
178
+
r[cfg.target_family.values]
120
179
Example values:
121
180
122
181
*`"unix"`
@@ -126,11 +185,17 @@ Example values:
126
185
127
186
### `unix` and `windows`
128
187
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.
131
193
132
194
### `target_env`
133
195
196
+
r[cfg.target_env]
197
+
198
+
r[cfg.target_env.values]
134
199
Key-value option set with further disambiguating information about the target
135
200
platform with information about the ABI or `libc` used. For historical reasons,
136
201
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
139
204
triple. One difference is that embedded ABIs such as `gnueabihf` will simply
140
205
define `target_env` as `"gnu"`.
141
206
207
+
r[cfg.target_env.values]
142
208
Example values:
143
209
144
210
*`""`
@@ -149,12 +215,19 @@ Example values:
149
215
150
216
### `target_abi`
151
217
218
+
r[cfg.target_abi]
219
+
220
+
221
+
r[cfg.target_env.general]
152
222
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
156
228
empty.
157
229
230
+
r[cfg.target_env.values]
158
231
Example values:
159
232
160
233
*`""`
@@ -166,13 +239,19 @@ Example values:
166
239
167
240
### `target_endian`
168
241
242
+
r[cfg.target_endian]
169
243
Key-value option set once with either a value of "little" or "big" depending
170
244
on the endianness of the target's CPU.
171
245
172
246
### `target_pointer_width`
173
247
248
+
r[cfg.target_poitner_width]
249
+
250
+
251
+
r[cfg.target_pointer_width.general]
174
252
Key-value option set once with the target's pointer width in bits.
175
253
254
+
r[cfg.target_pointer_width.values]
176
255
Example values:
177
256
178
257
*`"16"`
@@ -181,8 +260,13 @@ Example values:
181
260
182
261
### `target_vendor`
183
262
263
+
r[cfg.target_vendor]
264
+
265
+
266
+
r[cfg.target_vendor.general]
184
267
Key-value option set once with the vendor of the target.
185
268
269
+
r[cfg.target_vendor.values]
186
270
Example values:
187
271
188
272
*`"apple"`
@@ -192,12 +276,19 @@ Example values:
192
276
193
277
### `target_has_atomic`
194
278
279
+
r[cfg.target_has_atomic]
280
+
281
+
282
+
r[cfg.target_has_atomic.general]
195
283
Key-value option set for each bit width that the target supports
196
284
atomic loads, stores, and compare-and-swap operations.
197
285
286
+
r[cfg.target_has_atomic.stdlib]
198
287
When this cfg is present, all of the stable [`core::sync::atomic`] APIs are available for
199
288
the relevant atomic width.
200
289
290
+
291
+
r[cfg.target_has_atomic.values]
201
292
Possible values:
202
293
203
294
*`"8"`
@@ -209,25 +300,36 @@ Possible values:
209
300
210
301
### `test`
211
302
303
+
r[cfg.test]
304
+
212
305
Enabled when compiling the test harness. Done with `rustc` by using the
213
306
[`--test`] flag. See [Testing] for more on testing support.
214
307
215
308
### `debug_assertions`
216
309
310
+
r[cfg.debug_assertions]
311
+
217
312
Enabled by default when compiling without optimizations.
218
313
This can be used to enable extra debugging code in development but not in
219
314
production. For example, it controls the behavior of the standard library's
220
315
[`debug_assert!`] macro.
221
316
222
317
### `proc_macro`
223
318
319
+
r[cfg.proc_macro]
320
+
224
321
Set when the crate being compiled is being compiled with the `proc_macro`
225
322
[crate type].
226
323
227
324
### `panic`
228
325
326
+
r[cfg.panic]
327
+
328
+
329
+
r[cfg.panic.general]
229
330
Key-value option set depending on the panic strategy. Note that more values may be added in the future.
230
331
332
+
r[cfg.panic.values]
231
333
Example values:
232
334
233
335
*`"abort"`
@@ -237,20 +339,28 @@ Example values:
237
339
238
340
### The `cfg` attribute
239
341
342
+
r[cfg.attr]
343
+
344
+
345
+
r[cfg.attr.syntax]
240
346
> **<sup>Syntax</sup>**\
241
347
> _CfgAttrAttribute_ :\
242
348
> `cfg``(`_ConfigurationPredicate_`)`
243
349
244
350
<!-- should we say they're active attributes here? -->
245
351
352
+
r[cfg.attr.general]
246
353
The `cfg`[attribute] conditionally includes the thing it is attached to based
247
354
on a configuration predicate.
248
355
356
+
r[cfg.attr.syntax-explanation]
249
357
It is written as `cfg`, `(`, a configuration predicate, and finally `)`.
250
358
359
+
r[cfg.attr.effect]
251
360
If the predicate is true, the thing is rewritten to not have the `cfg` attribute
252
361
on it. If the predicate is false, the thing is removed from the source code.
253
362
363
+
r[cfg.attr.crate-level-attrs]
254
364
When a crate-level `cfg` has a false predicate, the behavior is slightly
255
365
different: any crate attributes preceding the `cfg` are kept, and any crate
256
366
attributes following the `cfg` are removed. This allows `#![no_std]` and
@@ -293,20 +403,28 @@ fn when_unwinding() {
293
403
294
404
```
295
405
406
+
r[cfg.attr.restriction]
296
407
The `cfg` attribute is allowed anywhere attributes are allowed.
0 commit comments