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