Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ function(_rust_map_target)
else()
message(FATAL_ERROR "Rust: Unsupported riscv ISA")
endif()
elseif(CONFIG_XTENSA)
# Xtensa targets exist upstream as tier 3 (rust-lang/rust #125141) but
# official rustc ships without the Xtensa LLVM backend; these require
# the esp-rs Rust toolchain on PATH.
if(CONFIG_SOC_SERIES_ESP32S3)
set(RUST_TARGET "xtensa-esp32s3-none-elf" PARENT_SCOPE)
elseif(CONFIG_SOC_SERIES_ESP32S2)
set(RUST_TARGET "xtensa-esp32s2-none-elf" PARENT_SCOPE)
elseif(CONFIG_SOC_SERIES_ESP32)
set(RUST_TARGET "xtensa-esp32-none-elf" PARENT_SCOPE)
else()
message(FATAL_ERROR "Rust: Unsupported Xtensa SoC series")
endif()
elseif(CONFIG_ARCH_POSIX AND CONFIG_64BIT AND (${CMAKE_HOST_SYSTEM_PROCESSOR} MATCHES "x86_64"))
set(RUST_TARGET "x86_64-unknown-none" PARENT_SCOPE)
elseif(CONFIG_ARCH_POSIX AND CONFIG_64BIT AND (${CMAKE_HOST_SYSTEM_PROCESSOR} MATCHES "aarch64"))
Expand Down
1 change: 1 addition & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ config RUST_SUPPORTED
bool
default y if ((CPU_CORTEX_M || \
(RISCV && !RISCV_ISA_RV32E && !RISCV_ISA_RV128I) || \
XTENSA || \
(ARCH_POSIX && 64BIT)) && \
!TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
help
Expand Down
6 changes: 5 additions & 1 deletion zephyr-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ pub fn build_kconfig_mod() {
let line = line.expect("reading line from dotconfig");
if let Some(caps) = config_hex.captures(&line) {
writeln!(&mut f, "#[allow(dead_code)]").unwrap();
writeln!(&mut f, "pub const {}: usize = {};", &caps[1], &caps[2]).unwrap();
// Hex values wider than 32 bits (e.g. ESP32-S3 GPIO masks) don't
// fit usize on 32-bit targets; widen the type to match the value.
let value = u64::from_str_radix(&caps[2][2..], 16).unwrap_or(u64::MAX);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this also emit u64 instead of usize even on 64-bit targets? Just curious if this was considered and if it could cause any issues.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but only for hex values above u32::MAX, and it keys on the value rather than the target, so a given constant is u64 everywhere or usize everywhere. That keeps the type portable instead of target-conditional. On 64-bit there's no truncation either way since usize is already 64-bit; on 32-bit the u64 is what avoids the silent truncation this fixes. The only cost is that a >u32 hex const is u64, so a usize consumer needs a cast, but those are rare (large masks like GPIO). If uniformity matters more than that, always emitting u64 for hex is the alternative, at the cost of small values losing the ergonomic usize.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it's a bit annoying that you'd get usize mixed with u64 on 64-bit systems where a uniform representation is available.

I think one of the following would be more sane,

  1. u32 with promotion to u64 when necessary - for all targets
  2. usize with promotion to u64 only when there is a target limitation

@d3zd3z What do you think?

let ty = if value > u32::MAX as u64 { "u64" } else { "usize" };
writeln!(&mut f, "pub const {}: {} = {};", &caps[1], ty, &caps[2]).unwrap();
} else if let Some(caps) = config_int.captures(&line) {
writeln!(&mut f, "#[allow(dead_code)]").unwrap();
writeln!(&mut f, "pub const {}: isize = {};", &caps[1], &caps[2]).unwrap();
Expand Down