Skip to content

Commit 84d6e8a

Browse files
committed
Implement cfg(target_abi) (RFC 2992)
Add an `abi` field to `TargetOptions`, defaulting to "". Support using `cfg(target_abi = "...")` for conditional compilation on that field. Gated by `feature(cfg_target_abi)`. Add a test for `target_abi`, and a test for the feature gate. Add `target_abi` to tidy as a platform-specific cfg. This does not add an abi to any existing target.
1 parent c0bd5a5 commit 84d6e8a

File tree

9 files changed

+76
-1
lines changed

9 files changed

+76
-1
lines changed

compiler/rustc_feature/src/active.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,9 @@ declare_features! (
687687
/// Allows qualified paths in struct expressions, struct patterns and tuple struct patterns.
688688
(active, more_qualified_paths, "1.54.0", Some(80080), None),
689689

690+
/// Allows `cfg(target_abi = "...")`.
691+
(active, cfg_target_abi, "1.55.0", Some(80970), None),
692+
690693
// -------------------------------------------------------------------------
691694
// feature-group-end: actual feature gates
692695
// -------------------------------------------------------------------------

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub type GatedCfg = (Symbol, Symbol, GateFn);
2323
/// `cfg(...)`'s that are feature gated.
2424
const GATED_CFGS: &[GatedCfg] = &[
2525
// (name in cfg, feature, function to check if the feature is enabled)
26+
(sym::target_abi, sym::cfg_target_abi, cfg_fn!(cfg_target_abi)),
2627
(sym::target_thread_local, sym::cfg_target_thread_local, cfg_fn!(cfg_target_thread_local)),
2728
(sym::target_has_atomic, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)),
2829
(sym::target_has_atomic_load_store, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)),

compiler/rustc_session/src/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,7 @@ fn default_configuration(sess: &Session) -> CrateConfig {
805805
let wordsz = sess.target.pointer_width.to_string();
806806
let os = &sess.target.os;
807807
let env = &sess.target.env;
808+
let abi = &sess.target.abi;
808809
let vendor = &sess.target.vendor;
809810
let min_atomic_width = sess.target.min_atomic_width();
810811
let max_atomic_width = sess.target.max_atomic_width();
@@ -814,7 +815,7 @@ fn default_configuration(sess: &Session) -> CrateConfig {
814815
});
815816

816817
let mut ret = FxHashSet::default();
817-
ret.reserve(6); // the minimum number of insertions
818+
ret.reserve(7); // the minimum number of insertions
818819
// Target bindings.
819820
ret.insert((sym::target_os, Some(Symbol::intern(os))));
820821
for fam in &sess.target.families {
@@ -829,6 +830,7 @@ fn default_configuration(sess: &Session) -> CrateConfig {
829830
ret.insert((sym::target_endian, Some(Symbol::intern(end.as_str()))));
830831
ret.insert((sym::target_pointer_width, Some(Symbol::intern(&wordsz))));
831832
ret.insert((sym::target_env, Some(Symbol::intern(env))));
833+
ret.insert((sym::target_abi, Some(Symbol::intern(abi))));
832834
ret.insert((sym::target_vendor, Some(Symbol::intern(vendor))));
833835
if sess.target.has_elf_tls {
834836
ret.insert((sym::target_thread_local, None));

compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ symbols! {
353353
cfg_eval,
354354
cfg_panic,
355355
cfg_sanitize,
356+
cfg_target_abi,
356357
cfg_target_feature,
357358
cfg_target_has_atomic,
358359
cfg_target_thread_local,
@@ -1199,6 +1200,7 @@ symbols! {
11991200
sync,
12001201
sync_trait,
12011202
t32,
1203+
target_abi,
12021204
target_arch,
12031205
target_endian,
12041206
target_env,

compiler/rustc_target/src/spec/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,9 @@ pub struct TargetOptions {
10251025
pub os: String,
10261026
/// Environment name to use for conditional compilation (`target_env`). Defaults to "".
10271027
pub env: String,
1028+
/// ABI name to distinguish multiple ABIs on the same OS and architecture. For instance, `"eabi"`
1029+
/// or `"eabihf"`. Defaults to "".
1030+
pub abi: String,
10281031
/// Vendor name to use for conditional compilation (`target_vendor`). Defaults to "unknown".
10291032
pub vendor: String,
10301033
/// Default linker flavor used if `-C linker-flavor` or `-C linker` are not passed
@@ -1342,6 +1345,7 @@ impl Default for TargetOptions {
13421345
c_int_width: "32".to_string(),
13431346
os: "none".to_string(),
13441347
env: String::new(),
1348+
abi: String::new(),
13451349
vendor: "unknown".to_string(),
13461350
linker_flavor: LinkerFlavor::Gcc,
13471351
linker: option_env!("CFG_DEFAULT_LINKER").map(|s| s.to_string()),
@@ -1919,6 +1923,7 @@ impl Target {
19191923
key!(c_int_width = "target-c-int-width");
19201924
key!(os);
19211925
key!(env);
1926+
key!(abi);
19221927
key!(vendor);
19231928
key!(linker_flavor, LinkerFlavor)?;
19241929
key!(linker, optional);
@@ -2152,6 +2157,7 @@ impl ToJson for Target {
21522157
target_option_val!(c_int_width, "target-c-int-width");
21532158
target_option_val!(os);
21542159
target_option_val!(env);
2160+
target_option_val!(abi);
21552161
target_option_val!(vendor);
21562162
target_option_val!(linker_flavor);
21572163
target_option_val!(linker);

src/test/ui/cfg/cfg-target-abi.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// run-pass
2+
#![feature(cfg_target_abi)]
3+
4+
#[cfg(target_abi = "eabihf")]
5+
pub fn main() {
6+
}
7+
8+
#[cfg(not(target_abi = "eabihf"))]
9+
pub fn main() {
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#[cfg(target_abi = "x")] //~ ERROR `cfg(target_abi)` is experimental
2+
#[cfg_attr(target_abi = "x", x)] //~ ERROR `cfg(target_abi)` is experimental
3+
struct Foo(u64, u64);
4+
5+
#[cfg(not(any(all(target_abi = "x"))))] //~ ERROR `cfg(target_abi)` is experimental
6+
fn foo() {}
7+
8+
fn main() {
9+
cfg!(target_abi = "x");
10+
//~^ ERROR `cfg(target_abi)` is experimental and subject to change
11+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0658]: `cfg(target_abi)` is experimental and subject to change
2+
--> $DIR/feature-gate-cfg-target-abi.rs:2:12
3+
|
4+
LL | #[cfg_attr(target_abi = "x", x)]
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #80970 <https://github.com/rust-lang/rust/issues/80970> for more information
8+
= help: add `#![feature(cfg_target_abi)]` to the crate attributes to enable
9+
10+
error[E0658]: `cfg(target_abi)` is experimental and subject to change
11+
--> $DIR/feature-gate-cfg-target-abi.rs:1:7
12+
|
13+
LL | #[cfg(target_abi = "x")]
14+
| ^^^^^^^^^^^^^^^^
15+
|
16+
= note: see issue #80970 <https://github.com/rust-lang/rust/issues/80970> for more information
17+
= help: add `#![feature(cfg_target_abi)]` to the crate attributes to enable
18+
19+
error[E0658]: `cfg(target_abi)` is experimental and subject to change
20+
--> $DIR/feature-gate-cfg-target-abi.rs:5:19
21+
|
22+
LL | #[cfg(not(any(all(target_abi = "x"))))]
23+
| ^^^^^^^^^^^^^^^^
24+
|
25+
= note: see issue #80970 <https://github.com/rust-lang/rust/issues/80970> for more information
26+
= help: add `#![feature(cfg_target_abi)]` to the crate attributes to enable
27+
28+
error[E0658]: `cfg(target_abi)` is experimental and subject to change
29+
--> $DIR/feature-gate-cfg-target-abi.rs:9:10
30+
|
31+
LL | cfg!(target_abi = "x");
32+
| ^^^^^^^^^^^^^^^^
33+
|
34+
= note: see issue #80970 <https://github.com/rust-lang/rust/issues/80970> for more information
35+
= help: add `#![feature(cfg_target_abi)]` to the crate attributes to enable
36+
37+
error: aborting due to 4 previous errors
38+
39+
For more information about this error, try `rustc --explain E0658`.

src/tools/tidy/src/pal.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ fn check_cfgs(
122122

123123
let contains_platform_specific_cfg = cfg.contains("target_os")
124124
|| cfg.contains("target_env")
125+
|| cfg.contains("target_abi")
125126
|| cfg.contains("target_vendor")
126127
|| cfg.contains("unix")
127128
|| cfg.contains("windows");

0 commit comments

Comments
 (0)