diff --git a/src/targets.rs b/src/targets.rs index c79aeca..d14a760 100644 --- a/src/targets.rs +++ b/src/targets.rs @@ -1006,7 +1006,9 @@ pub(crate) fn default_binary_format(triple: &Triple) -> BinaryFormat { | OperatingSystem::Wasi | OperatingSystem::Unknown => match triple.architecture { Architecture::Wasm32 | Architecture::Wasm64 => BinaryFormat::Wasm, - _ => BinaryFormat::Unknown, + Architecture::Unknown => BinaryFormat::Unknown, + // Default to ELF, following `getDefaultFormat` in LLVM. + _ => BinaryFormat::Elf, }, _ => BinaryFormat::Elf, } @@ -1820,6 +1822,19 @@ mod tests { } } + #[test] + fn default_format_to_elf() { + let t = Triple::from_str("riscv64").expect("can't parse target"); + assert_eq!( + t.architecture, + Architecture::Riscv64(Riscv64Architecture::Riscv64), + ); + assert_eq!(t.vendor, Vendor::Unknown); + assert_eq!(t.operating_system, OperatingSystem::Unknown); + assert_eq!(t.environment, Environment::Unknown); + assert_eq!(t.binary_format, BinaryFormat::Elf); + } + #[test] fn thumbv7em_none_eabihf() { let t = Triple::from_str("thumbv7em-none-eabihf").expect("can't parse target"); @@ -1915,7 +1930,7 @@ mod tests { ); assert_eq!(t.operating_system, OperatingSystem::Unknown); assert_eq!(t.environment, Environment::Unknown); - assert_eq!(t.binary_format, BinaryFormat::Unknown); + assert_eq!(t.binary_format, BinaryFormat::Elf); assert_eq!( Triple::from_str("unknown-foo"), diff --git a/src/triple.rs b/src/triple.rs index b39eb12..947fedd 100644 --- a/src/triple.rs +++ b/src/triple.rs @@ -261,13 +261,37 @@ impl fmt::Display for Triple { } } - if self.binary_format != implied_binary_format { + if self.binary_format != implied_binary_format || show_binary_format_with_no_os(self) { + // As a special case, omit a non-default binary format for some + // targets which happen to exclude it. write!(f, "-{}", self.binary_format)?; } Ok(()) } } +fn show_binary_format_with_no_os(triple: &Triple) -> bool { + if triple.binary_format == BinaryFormat::Unknown { + return false; + } + + #[cfg(feature = "arch_zkasm")] + { + if triple.architecture == Architecture::ZkAsm { + return false; + } + } + + triple.environment != Environment::Eabi + && triple.environment != Environment::Eabihf + && triple.environment != Environment::Sgx + && triple.architecture != Architecture::Avr + && triple.architecture != Architecture::Wasm32 + && triple.architecture != Architecture::Wasm64 + && (triple.operating_system == OperatingSystem::None_ + || triple.operating_system == OperatingSystem::Unknown) +} + impl FromStr for Triple { type Err = ParseError;