Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 37b2ebc

Browse files
committedFeb 7, 2025
Adds binary_format to rustc target specs
1 parent 64e06c0 commit 37b2ebc

File tree

8 files changed

+73
-9
lines changed

8 files changed

+73
-9
lines changed
 

‎compiler/rustc_target/src/spec/base/aix.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use rustc_abi::Endian;
22

3-
use crate::spec::{Cc, CodeModel, LinkOutputKind, LinkerFlavor, TargetOptions, crt_objects, cvs};
3+
use crate::spec::{
4+
BinaryFormat, Cc, CodeModel, LinkOutputKind, LinkerFlavor, TargetOptions, crt_objects, cvs,
5+
};
46

57
pub(crate) fn opts() -> TargetOptions {
68
TargetOptions {
@@ -21,6 +23,7 @@ pub(crate) fn opts() -> TargetOptions {
2123
linker: Some("ld".into()),
2224
eh_frame_header: false,
2325
is_like_aix: true,
26+
binary_format: BinaryFormat::Xcoff,
2427
default_dwarf_version: 3,
2528
function_sections: true,
2629
pre_link_objects: crt_objects::new(&[

‎compiler/rustc_target/src/spec/base/apple/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use std::borrow::Cow;
22
use std::env;
33

44
use crate::spec::{
5-
Cc, DebuginfoKind, FloatAbi, FramePointer, LinkerFlavor, Lld, SplitDebuginfo, StackProbeType,
6-
StaticCow, TargetOptions, cvs,
5+
BinaryFormat, Cc, DebuginfoKind, FloatAbi, FramePointer, LinkerFlavor, Lld, SplitDebuginfo,
6+
StackProbeType, StaticCow, TargetOptions, cvs,
77
};
88

99
#[cfg(test)]
@@ -116,6 +116,7 @@ pub(crate) fn base(
116116
dynamic_linking: true,
117117
families: cvs!["unix"],
118118
is_like_osx: true,
119+
binary_format: BinaryFormat::MachO,
119120
// LLVM notes that macOS 10.11+ and iOS 9+ default
120121
// to v4, so we do the same.
121122
// https://github.com/llvm/llvm-project/blob/378778a0d10c2f8d5df8ceff81f95b6002984a4b/clang/lib/Driver/ToolChains/Darwin.cpp#L1203

‎compiler/rustc_target/src/spec/base/msvc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::borrow::Cow;
22

3-
use crate::spec::{DebuginfoKind, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions};
3+
use crate::spec::{BinaryFormat, DebuginfoKind, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions};
44

55
pub(crate) fn opts() -> TargetOptions {
66
// Suppress the verbose logo and authorship debugging output, which would needlessly
@@ -12,6 +12,7 @@ pub(crate) fn opts() -> TargetOptions {
1212
dll_tls_export: false,
1313
is_like_windows: true,
1414
is_like_msvc: true,
15+
binary_format: BinaryFormat::Coff,
1516
pre_link_args,
1617
abi_return_struct_as_int: true,
1718
emit_debug_gdb_scripts: false,

‎compiler/rustc_target/src/spec/base/wasm.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::spec::{
2-
Cc, LinkSelfContainedDefault, LinkerFlavor, PanicStrategy, RelocModel, TargetOptions, TlsModel,
3-
add_link_args, cvs,
2+
BinaryFormat, Cc, LinkSelfContainedDefault, LinkerFlavor, PanicStrategy, RelocModel,
3+
TargetOptions, TlsModel, add_link_args, cvs,
44
};
55

66
pub(crate) fn options() -> TargetOptions {
@@ -53,6 +53,7 @@ pub(crate) fn options() -> TargetOptions {
5353

5454
TargetOptions {
5555
is_like_wasm: true,
56+
binary_format: BinaryFormat::Wasm,
5657
families: cvs!["wasm"],
5758

5859
// we allow dynamic linking, but only cdylibs. Basically we allow a

‎compiler/rustc_target/src/spec/base/windows_gnu.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::borrow::Cow;
22

33
use crate::spec::{
4-
Cc, DebuginfoKind, LinkSelfContainedDefault, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions,
5-
add_link_args, crt_objects, cvs,
4+
BinaryFormat, Cc, DebuginfoKind, LinkSelfContainedDefault, LinkerFlavor, Lld, SplitDebuginfo,
5+
TargetOptions, add_link_args, crt_objects, cvs,
66
};
77

88
pub(crate) fn opts() -> TargetOptions {
@@ -83,6 +83,7 @@ pub(crate) fn opts() -> TargetOptions {
8383
exe_suffix: ".exe".into(),
8484
families: cvs!["windows"],
8585
is_like_windows: true,
86+
binary_format: BinaryFormat::Coff,
8687
allows_weak_linkage: false,
8788
pre_link_args,
8889
pre_link_objects: crt_objects::pre_mingw(),

‎compiler/rustc_target/src/spec/base/windows_gnullvm.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::borrow::Cow;
22

3-
use crate::spec::{Cc, DebuginfoKind, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions, cvs};
3+
use crate::spec::{
4+
BinaryFormat, Cc, DebuginfoKind, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions, cvs,
5+
};
46

57
pub(crate) fn opts() -> TargetOptions {
68
// We cannot use `-nodefaultlibs` because compiler-rt has to be passed
@@ -33,6 +35,7 @@ pub(crate) fn opts() -> TargetOptions {
3335
exe_suffix: ".exe".into(),
3436
families: cvs!["windows"],
3537
is_like_windows: true,
38+
binary_format: BinaryFormat::Coff,
3639
allows_weak_linkage: false,
3740
pre_link_args,
3841
late_link_args,

‎compiler/rustc_target/src/spec/json.rs

+15
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,19 @@ impl Target {
103103
base.$key_name = Some(s);
104104
}
105105
} );
106+
($key_name:ident, BinaryFormat) => ( {
107+
let name = (stringify!($key_name)).replace("_", "-");
108+
obj.remove(&name).and_then(|f| f.as_str().and_then(|s| {
109+
match s.parse::<super::BinaryFormat>() {
110+
Ok(binary_format) => base.$key_name = binary_format,
111+
_ => return Some(Err(format!(
112+
"'{s}' is not a valid value for binary_format. \
113+
Use 'Coff', 'Elf', 'MachO', 'Wasm' or 'Xcoff' "
114+
))),
115+
}
116+
Some(Ok(()))
117+
})).unwrap_or(Ok(()))
118+
} );
106119
($key_name:ident, MergeFunctions) => ( {
107120
let name = (stringify!($key_name)).replace("_", "-");
108121
obj.remove(&name).and_then(|o| o.as_str().and_then(|s| {
@@ -585,6 +598,7 @@ impl Target {
585598
key!(is_like_msvc, bool);
586599
key!(is_like_wasm, bool);
587600
key!(is_like_android, bool);
601+
key!(binary_format, BinaryFormat)?;
588602
key!(default_dwarf_version, u32);
589603
key!(allows_weak_linkage, bool);
590604
key!(has_rpath, bool);
@@ -762,6 +776,7 @@ impl ToJson for Target {
762776
target_option_val!(is_like_msvc);
763777
target_option_val!(is_like_wasm);
764778
target_option_val!(is_like_android);
779+
target_option_val!(binary_format);
765780
target_option_val!(default_dwarf_version);
766781
target_option_val!(allows_weak_linkage);
767782
target_option_val!(has_rpath);

‎compiler/rustc_target/src/spec/mod.rs

+39
Original file line numberDiff line numberDiff line change
@@ -2076,6 +2076,42 @@ enum TargetKind {
20762076
Builtin,
20772077
}
20782078

2079+
#[derive(PartialEq, Clone, Debug)]
2080+
pub enum BinaryFormat {
2081+
Coff,
2082+
Elf,
2083+
MachO,
2084+
Wasm,
2085+
Xcoff,
2086+
}
2087+
2088+
impl FromStr for BinaryFormat {
2089+
type Err = ();
2090+
fn from_str(s: &str) -> Result<Self, Self::Err> {
2091+
match s {
2092+
"Coff" => Ok(Self::Coff),
2093+
"Elf" => Ok(Self::Elf),
2094+
"MachO" => Ok(Self::MachO),
2095+
"Wasm" => Ok(Self::Wasm),
2096+
"Xcoff" => Ok(Self::Xcoff),
2097+
_ => Err(()),
2098+
}
2099+
}
2100+
}
2101+
2102+
impl ToJson for BinaryFormat {
2103+
fn to_json(&self) -> Json {
2104+
match self {
2105+
Self::Coff => "Coff",
2106+
Self::Elf => "Elf",
2107+
Self::MachO => "MachO",
2108+
Self::Wasm => "Wasm",
2109+
Self::Xcoff => "Xcoff",
2110+
}
2111+
.to_json()
2112+
}
2113+
}
2114+
20792115
/// Everything `rustc` knows about how to compile for a specific target.
20802116
///
20812117
/// Every field here must be specified, and has no default value.
@@ -2369,6 +2405,8 @@ pub struct TargetOptions {
23692405
pub is_like_wasm: bool,
23702406
/// Whether a target toolchain is like Android, implying a Linux kernel and a Bionic libc
23712407
pub is_like_android: bool,
2408+
/// Default used is BinaryFormat::Elf
2409+
pub binary_format: BinaryFormat,
23722410
/// Default supported version of DWARF on this platform.
23732411
/// Useful because some platforms (osx, bsd) only want up to DWARF2.
23742412
pub default_dwarf_version: u32,
@@ -2744,6 +2782,7 @@ impl Default for TargetOptions {
27442782
is_like_msvc: false,
27452783
is_like_wasm: false,
27462784
is_like_android: false,
2785+
binary_format: BinaryFormat::Elf,
27472786
default_dwarf_version: 4,
27482787
allows_weak_linkage: true,
27492788
has_rpath: false,

0 commit comments

Comments
 (0)
Failed to load comments.