Skip to content

Commit 5b32d9f

Browse files
Folkert de Vriesfolkertdev
authored andcommitted
add cfg(target_object_format = "...")
1 parent 25a54d4 commit 5b32d9f

31 files changed

Lines changed: 223 additions & 48 deletions

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const GATED_CFGS: &[GatedCfg] = &[
6262
sym::cfg_target_has_reliable_f16_f128,
6363
Features::cfg_target_has_reliable_f16_f128,
6464
),
65+
(sym::target_object_format, sym::cfg_target_object_format, Features::cfg_target_object_format),
6566
];
6667

6768
/// Find a gated cfg determined by the `pred`icate which is given the cfg's name.

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ declare_features! (
410410
(unstable, cfg_target_has_atomic, "1.60.0", Some(94039)),
411411
/// Allows `cfg(target_has_atomic_equal_alignment = "...")`.
412412
(unstable, cfg_target_has_atomic_equal_alignment, "1.60.0", Some(93822)),
413+
/// Allows `cfg(target_object_format = "...")`.
414+
(internal, cfg_target_object_format, "CURRENT_RUSTC_VERSION", Some(152586)),
413415
/// Allows `cfg(target_thread_local)`.
414416
(unstable, cfg_target_thread_local, "1.7.0", Some(29594)),
415417
/// Allows the use of `#[cfg(ub_checks)` to check if UB checks are enabled.

compiler/rustc_session/src/config/cfg.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ pub(crate) fn disallow_cfgs(sess: &Session, user_cfgs: &Cfg) {
144144
| (sym::target_endian, Some(_))
145145
| (sym::target_env, None | Some(_))
146146
| (sym::target_family, Some(_))
147+
| (sym::target_object_format, Some(_))
147148
| (sym::target_os, Some(_))
148149
| (sym::target_pointer_width, Some(_))
149150
| (sym::target_vendor, None | Some(_))
@@ -254,6 +255,7 @@ pub(crate) fn default_configuration(sess: &Session) -> Cfg {
254255
ins_sym!(sym::target_arch, sess.target.arch.desc_symbol());
255256
ins_str!(sym::target_endian, sess.target.endian.as_str());
256257
ins_sym!(sym::target_env, sess.target.env.desc_symbol());
258+
ins_str!(sym::target_object_format, sess.target.options.binary_format.as_str());
257259

258260
for family in sess.target.families.as_ref() {
259261
ins_str!(sym::target_family, family);
@@ -420,12 +422,13 @@ impl CheckCfg {
420422

421423
// sym::target_*
422424
{
423-
const VALUES: [&Symbol; 8] = [
425+
const VALUES: [&Symbol; 9] = [
424426
&sym::target_abi,
425427
&sym::target_arch,
426428
&sym::target_endian,
427429
&sym::target_env,
428430
&sym::target_family,
431+
&sym::target_object_format,
429432
&sym::target_os,
430433
&sym::target_pointer_width,
431434
&sym::target_vendor,
@@ -449,6 +452,7 @@ impl CheckCfg {
449452
Some(values_target_endian),
450453
Some(values_target_env),
451454
Some(values_target_family),
455+
Some(values_target_object_format),
452456
Some(values_target_os),
453457
Some(values_target_pointer_width),
454458
Some(values_target_vendor),
@@ -465,6 +469,8 @@ impl CheckCfg {
465469
values_target_family.extend(
466470
target.options.families.iter().map(|family| Symbol::intern(family)),
467471
);
472+
values_target_object_format
473+
.insert(Symbol::intern(target.options.binary_format.as_str()));
468474
values_target_os.insert(target.options.os.desc_symbol());
469475
values_target_pointer_width.insert(sym::integer(target.pointer_width));
470476
values_target_vendor.insert(target.vendor_symbol());

compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ symbols! {
587587
cfg_target_has_atomic,
588588
cfg_target_has_atomic_equal_alignment,
589589
cfg_target_has_reliable_f16_f128,
590+
cfg_target_object_format,
590591
cfg_target_thread_local,
591592
cfg_target_vendor,
592593
cfg_trace: "<cfg_trace>", // must not be a valid identifier
@@ -2009,6 +2010,7 @@ symbols! {
20092010
target_has_reliable_f16_math,
20102011
target_has_reliable_f128,
20112012
target_has_reliable_f128_math,
2013+
target_object_format,
20122014
target_os,
20132015
target_pointer_width,
20142016
target_thread_local,

compiler/rustc_target/src/spec/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,16 @@ impl BinaryFormat {
13951395
Self::Xcoff => object::BinaryFormat::Xcoff,
13961396
}
13971397
}
1398+
1399+
pub fn as_str(&self) -> &str {
1400+
match self {
1401+
Self::Coff => "coff",
1402+
Self::Elf => "elf",
1403+
Self::MachO => "mach-o",
1404+
Self::Wasm => "wasm",
1405+
Self::Xcoff => "xcoff",
1406+
}
1407+
}
13981408
}
13991409

14001410
impl ToJson for Align {

src/librustdoc/clean/cfg.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,13 @@ impl fmt::Display for Display<'_> {
423423
(sym::unix, None) => "Unix",
424424
(sym::windows, None) => "Windows",
425425
(sym::debug_assertions, None) => "debug-assertions enabled",
426+
(sym::target_object_format, Some(format)) => match self.1 {
427+
Format::LongHtml => {
428+
return write!(fmt, "object format <code>{format}</code>");
429+
}
430+
Format::LongPlain => return write!(fmt, "object format `{format}`"),
431+
Format::ShortHtml => return write!(fmt, "<code>{format}</code>"),
432+
},
426433
(sym::target_os, Some(os)) => human_readable_target_os(*os).unwrap_or_default(),
427434
(sym::target_arch, Some(arch)) => {
428435
human_readable_target_arch(*arch).unwrap_or_default()

tests/auxiliary/minicore.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,20 @@ macro_rules! stringify {
187187
};
188188
}
189189

190+
#[rustc_builtin_macro]
191+
#[macro_export]
192+
macro_rules! compile_error {
193+
($msg:expr $(,)?) => {{ /* compiler built-in */ }};
194+
}
195+
196+
#[rustc_builtin_macro]
197+
#[macro_export]
198+
macro_rules! cfg {
199+
($($cfg:tt)*) => {
200+
/* compiler built-in */
201+
};
202+
}
203+
190204
#[lang = "add"]
191205
pub trait Add<Rhs = Self> {
192206
type Output;

tests/rustdoc-ui/doc-cfg-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `foo`
44
LL | #[doc(cfg(foo), cfg(bar))]
55
| ^^^
66
|
7-
= help: expected names are: `FALSE` and `test` and 31 more
7+
= help: expected names are: `FALSE` and `test` and 32 more
88
= help: to expect this configuration use `--check-cfg=cfg(foo)`
99
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
1010
= note: `#[warn(unexpected_cfgs)]` on by default
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//@ add-minicore
2+
//@ check-pass
3+
//
4+
//@ revisions: linux_gnu linux_musl linux_ohos linux_powerpc
5+
//@[linux_gnu] compile-flags: --target aarch64-unknown-linux-gnu
6+
//@[linux_gnu] needs-llvm-components: aarch64
7+
//@[linux_musl] compile-flags: --target aarch64-unknown-linux-musl
8+
//@[linux_musl] needs-llvm-components: aarch64
9+
//@[linux_ohos] compile-flags: --target aarch64-unknown-linux-ohos
10+
//@[linux_ohos] needs-llvm-components: aarch64
11+
//@[linux_powerpc] compile-flags: --target powerpc-unknown-linux-gnu
12+
//@[linux_powerpc] needs-llvm-components: powerpc
13+
//
14+
//@ revisions: darwin ios
15+
//@[darwin] compile-flags: --target aarch64-apple-darwin
16+
//@[darwin] needs-llvm-components: aarch64
17+
//@[ios] compile-flags: --target aarch64-apple-ios
18+
//@[ios] needs-llvm-components: aarch64
19+
//
20+
//@ revisions: win_msvc win_gnu
21+
//@[win_msvc] compile-flags: --target aarch64-pc-windows-msvc
22+
//@[win_msvc] needs-llvm-components: aarch64
23+
//@[win_gnu] compile-flags: --target x86_64-pc-windows-gnu
24+
//@[win_gnu] needs-llvm-components: x86
25+
//
26+
//@ revisions: wasm32 wasm64
27+
//@[wasm32] compile-flags: --target wasm32-unknown-unknown
28+
//@[wasm32] needs-llvm-components: webassembly
29+
//@[wasm64] compile-flags: --target wasm64-unknown-unknown
30+
//@[wasm64] needs-llvm-components: webassembly
31+
//
32+
//@ revisions: aix
33+
//@[aix] compile-flags: --target powerpc64-ibm-aix
34+
//@[aix] needs-llvm-components: powerpc
35+
//
36+
//@ revisions: hermit sgx uefi
37+
//@[hermit] compile-flags: --target x86_64-unknown-hermit
38+
//@[hermit] needs-llvm-components: x86
39+
//@[sgx] compile-flags: --target x86_64-fortanix-unknown-sgx
40+
//@[sgx] needs-llvm-components: x86
41+
//@[uefi] compile-flags: --target x86_64-unknown-uefi
42+
//@[uefi] needs-llvm-components: x86
43+
//
44+
//@ revisions: bpfeb bpfel
45+
//@[bpfeb] compile-flags: --target bpfeb-unknown-none
46+
//@[bpfeb] needs-llvm-components: bpf
47+
//@[bpfel] compile-flags: --target bpfel-unknown-none
48+
//@[bpfel] needs-llvm-components: bpf
49+
//
50+
//@ revisions: avr
51+
//@[avr] compile-flags: --target avr-none -Ctarget-cpu=atmega328
52+
//@[avr] needs-llvm-components: avr
53+
//
54+
//@ revisions: msp430
55+
//@[msp430] compile-flags: --target msp430-none-elf
56+
//@[msp430] needs-llvm-components: msp430
57+
//
58+
//@ revisions: thumb
59+
//@[thumb] compile-flags: --target thumbv7m-none-eabi
60+
//@[thumb] needs-llvm-components: arm
61+
#![crate_type = "lib"]
62+
#![feature(no_core, lang_items, cfg_target_object_format)]
63+
#![no_core]
64+
65+
extern crate minicore;
66+
use minicore::*;
67+
68+
macro_rules! assert_cfg {
69+
($rhs:ident = $rhs_val:literal) => {
70+
#[cfg(not($rhs = $rhs_val))]
71+
compile_error!(concat!("expected `", stringify!($rhs), " = ", $rhs_val, "`",));
72+
};
73+
}
74+
75+
const _: () = {
76+
cfg_select!(
77+
target_os = "linux" => assert_cfg!(target_object_format = "elf"),
78+
target_os = "aix" => assert_cfg!(target_object_format = "xcoff"),
79+
target_os = "uefi" => assert_cfg!(target_object_format = "coff"),
80+
target_os = "windows" => assert_cfg!(target_object_format = "coff"),
81+
target_os = "hermit" => assert_cfg!(target_object_format = "elf"),
82+
83+
target_arch = "bpf" => assert_cfg!(target_object_format = "elf"),
84+
target_arch = "avr" => assert_cfg!(target_object_format = "elf"),
85+
target_arch = "msp430" => assert_cfg!(target_object_format = "elf"),
86+
target_abi = "eabi" => assert_cfg!(target_object_format = "elf"),
87+
88+
target_vendor = "apple" => assert_cfg!(target_object_format = "mach-o"),
89+
target_family = "wasm" => assert_cfg!(target_object_format = "wasm"),
90+
91+
windows => assert_cfg!(target_object_format = "coff"),
92+
93+
_ => {}
94+
);
95+
};
96+
97+
const _: () = {
98+
cfg_select!(
99+
target_object_format = "mach-o" => assert_cfg!(target_vendor = "apple"),
100+
target_object_format = "wasm" => assert_cfg!(target_family = "wasm"),
101+
target_object_format = "xcoff" => assert_cfg!(target_os = "aix"),
102+
_ => {}
103+
);
104+
};

tests/ui/check-cfg/cargo-build-script.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `has_foo`
44
LL | #[cfg(has_foo)]
55
| ^^^^^^^
66
|
7-
= help: expected names are: `has_bar` and 31 more
7+
= help: expected names are: `has_bar` and 32 more
88
= help: consider using a Cargo feature instead
99
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
1010
[lints.rust]

0 commit comments

Comments
 (0)