Skip to content

Commit d785f9b

Browse files
committed
[AVR] Replace broken 'avr-unknown-unknown' target with 'avr-unknown-gnu-atmega328' target
The `avr-unknown-unknown` target has never worked correctly, always trying to invoke the host linker and failing. It aimed to be a mirror of AVR-GCC's default handling of the `avr-unknown-unknown' triple (assume bare minimum chip features, silently skip linking runtime libraries, etc). This behaviour is broken-by-default as it will cause a miscompiled executable when flashed. This patch improves the AVR builtin target specifications to instead expose only a 'avr-unknown-gnu-atmega328' target. This target system is `gnu`, as it uses the AVR-GCC frontend along with avr-binutils. The target triple ABI is 'atmega328'. In the future, it should be possible to replace the dependency on AVR-GCC and binutils by using the in-progress AVR LLD and compiler-rt support. Perhaps at that point it would make sense to add an 'avr-unknown-unknown-atmega328' target as a better default when implemented. There is no current intention to add in-tree AVR target specifications for other AVR microcontrollers - this one can serve as a reference implementation for other devices via `rustc --print target-spec-json avr-unknown-gnu-atmega328p`. There should be no users of the existing 'avr-unknown-unknown' Rust target as a custom target specification JSON has always been recommended, and the avr-unknown-unknown target could never pass the linking step anyway.
1 parent c4b6d94 commit d785f9b

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
2+
3+
/// A base target for AVR devices using the GNU toolchain.
4+
///
5+
/// Requires GNU avr-gcc and avr-binutils on the host system.
6+
pub fn target(target_cpu: String) -> TargetResult {
7+
Ok(Target {
8+
arch: "avr".to_string(),
9+
data_layout: "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8".to_string(),
10+
llvm_target: "avr-unknown-unknown".to_string(),
11+
target_endian: "little".to_string(),
12+
target_pointer_width: "16".to_string(),
13+
linker_flavor: LinkerFlavor::Gcc,
14+
target_os: "unknown".to_string(),
15+
target_env: "".to_string(),
16+
target_vendor: "unknown".to_string(),
17+
target_c_int_width: 16.to_string(),
18+
options: TargetOptions {
19+
cpu: target_cpu.clone(),
20+
exe_suffix: ".elf".to_string(),
21+
linker: Some("avr-gcc".to_owned()),
22+
pre_link_args: vec![(
23+
LinkerFlavor::Gcc,
24+
vec!["-Os".to_owned(), format!("-mmcu={}", target_cpu)],
25+
)]
26+
.into_iter()
27+
.collect(),
28+
late_link_args: vec![(LinkerFlavor::Gcc, vec!["-lc".to_owned(), "-lgcc".to_owned()])]
29+
.into_iter()
30+
.collect(),
31+
..super::freestanding_base::opts()
32+
},
33+
})
34+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use crate::spec::TargetResult;
2+
3+
pub fn target() -> TargetResult {
4+
super::avr_gnu_base::target("atmega328".to_owned())
5+
}

src/librustc_target/spec/avr_unknown_unknown.rs

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/librustc_target/spec/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ mod android_base;
5151
mod apple_base;
5252
mod apple_sdk_base;
5353
mod arm_base;
54+
mod avr_gnu_base;
5455
mod cloudabi_base;
5556
mod dragonfly_base;
5657
mod freebsd_base;
@@ -581,7 +582,7 @@ supported_targets! {
581582
("aarch64-fuchsia", aarch64_fuchsia),
582583
("x86_64-fuchsia", x86_64_fuchsia),
583584

584-
("avr-unknown-unknown", avr_unknown_unknown),
585+
("avr-unknown-gnu-atmega328", avr_unknown_gnu_atmega328),
585586

586587
("x86_64-unknown-l4re-uclibc", x86_64_unknown_l4re_uclibc),
587588

0 commit comments

Comments
 (0)