Skip to content
Open
Show file tree
Hide file tree
Changes from 10 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Normal west builds will place the Rust target directory under the build directory. However,
# sometimes IDEs and such will litter these target directories as well.
build*/
target/

# None of this code is an application, so don't capture any of the dependencies in the lock file.
Expand Down
67 changes: 61 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,51 @@ function(_rust_map_target)
elseif(CONFIG_RISCV)
if(CONFIG_RISCV_ISA_RV64I)
# TODO: Should fail if the extensions don't match.
set(RUST_TARGET "riscv64imac-unknown-none-elf" PARENT_SCOPE)
elseif(CONFIG_RISCV_ISA_RV32I)
# TODO: We have multiple choices, try to pick the best.
set(RUST_TARGET "riscv32i-unknown-none-elf" PARENT_SCOPE)
# We have multiple choices, try to pick the best.
# Note that Rust currently only supports "riscv64gc" and "riscv64imac" targets!
set(RUST_TARGET "riscv64")
if(CONFIG_RISCV_ISA_EXT_G)
set(RUST_TARGET "${RUST_TARGET}g")
else()
set(RUST_TARGET "${RUST_TARGET}i")
if(CONFIG_RISCV_ISA_EXT_M)
set(RUST_TARGET "${RUST_TARGET}m")
endif()
if(CONFIG_RISCV_ISA_EXT_A)
set(RUST_TARGET "${RUST_TARGET}a")
endif()
if(CONFIG_RISCV_ISA_EXT_F)
set(RUST_TARGET "${RUST_TARGET}f")
endif()
if(CONFIG_RISCV_ISA_EXT_D)
set(RUST_TARGET "${RUST_TARGET}d")
endif()
endif()
if(CONFIG_RISCV_ISA_EXT_C)
set(RUST_TARGET "${RUST_TARGET}c")
endif()
set(RUST_TARGET "${RUST_TARGET}-unknown-none-elf" PARENT_SCOPE)
elseif(CONFIG_RISCV_ISA_RV32I OR CONFIG_RISCV_ISA_RV32E)
# We have multiple choices, try to pick the best.
# Note that Rust currently only supports subsets of "riscv32imafc" targets!
if(CONFIG_RISCV_ISA_RV32I)
set(RUST_TARGET "riscv32i")
else()
set(RUST_TARGET "riscv32e")
endif()
if(CONFIG_RISCV_ISA_EXT_M)
set(RUST_TARGET "${RUST_TARGET}m")
endif()
if(CONFIG_RISCV_ISA_EXT_A)
set(RUST_TARGET "${RUST_TARGET}a")
endif()
if(CONFIG_RISCV_ISA_EXT_F)
set(RUST_TARGET "${RUST_TARGET}f")
endif()
if(CONFIG_RISCV_ISA_EXT_C)
set(RUST_TARGET "${RUST_TARGET}c")
endif()
set(RUST_TARGET "${RUST_TARGET}-unknown-none-elf" PARENT_SCOPE)
else()
message(FATAL_ERROR "Rust: Unsupported riscv ISA")
endif()
Expand Down Expand Up @@ -93,6 +134,18 @@ function(rust_cargo_application)
endif()
set(BUILD_LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${RUST_TARGET}/${RUST_BUILD_TYPE}")

if(CONFIG_RUST_NIGHTLY)
set(CARGO_TOOLCHAIN_OVERRIDE "+nightly")
else()
set(CARGO_TOOLCHAIN_OVERRIDE "")
endif()

if(CONFIG_RUST_BUILD_STD)
set(CARGO_ARGS "-Zbuild-std=core,alloc")
else()
set(CARGO_ARGS "")
endif()

set(CARGO_TARGET_DIR "${CMAKE_CURRENT_BINARY_DIR}/rust/target")
set(RUST_LIBRARY "${CARGO_TARGET_DIR}/${RUST_TARGET}/${RUST_BUILD_TYPE}/librustapp.a")
set(SAMPLE_CARGO_CONFIG "${CMAKE_CURRENT_BINARY_DIR}/rust/sample-cargo-config.toml")
Expand Down Expand Up @@ -173,7 +226,7 @@ ${config_paths}
WRAPPER_FILE="${WRAPPER_FILE}"
DT_AUGMENTS="${DT_AUGMENTS}"
BINARY_DIR_INCLUDE_GENERATED="${BINARY_DIR_INCLUDE_GENERATED}"
cargo build
cargo ${CARGO_TOOLCHAIN_OVERRIDE} build
${rust_build_type_arg}

# Override the features according to the shield given. For a general case,
Expand All @@ -187,6 +240,7 @@ ${config_paths}
${command_paths}
--target ${RUST_TARGET}
--target-dir ${CARGO_TARGET_DIR}
${CARGO_ARGS}
COMMENT "Building Rust application"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
Expand Down Expand Up @@ -217,7 +271,7 @@ ${config_paths}
WRAPPER_FILE="${WRAPPER_FILE}"
DT_AUGMENTS="${DT_AUGMENTS}"
BINARY_DIR_INCLUDE_GENERATED="${BINARY_DIR_INCLUDE_GENERATED}"
cargo doc
cargo ${CARGO_TOOLCHAIN_OVERRIDE} doc
${rust_build_type_arg}

# Override the features according to the shield given. For a general case,
Expand All @@ -231,6 +285,7 @@ ${config_paths}
${command_paths}
--target ${RUST_TARGET}
--target-dir ${CARGO_TARGET_DIR}
${CARGO_ARGS}
COMMENT "Building Rust documentation"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
Expand Down
21 changes: 20 additions & 1 deletion Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ menu "Rust Language Support"
config RUST_SUPPORTED
bool
default y if ((CPU_CORTEX_M || \
(RISCV && !RISCV_ISA_RV32E && !RISCV_ISA_RV128I) || \
(RISCV && !RISCV_ISA_RV128I) || \
(ARCH_POSIX && 64BIT)) && \
!TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
help
Expand All @@ -31,6 +31,25 @@ config RUST_ALLOC
Zephyr allocator (malloc/free). This this enabled, Rust
applications can use the `alloc` crate.

config RUST_NIGHTLY
bool "Use nightly for building the target"
default y if RISCV_ISA_RV32E
help
If enabled, nightly edition will be used for building.

if RUST_NIGHTLY

config RUST_BUILD_STD
bool "Build standard library from source"
default y if RISCV_ISA_RV32E
help
Some targets require building standard library from source.
If enabled, this will add `-Z build-std=core,alloc` to cargo build.
Depends on nightly edition.
You need to run `rustup +nightly component add rust-src` before building.

endif # RUST_NIGHTLY

endif # RUST

endmenu
1 change: 1 addition & 0 deletions dt-rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
names:
- "nordic,nrf52-flash-controller"
- "nordic,nrf51-flash-controller"
- "nordic,rram-controller"
- "raspberrypi,pico-flash-controller"
- "zephyr,sim-flash"
- "st,stm32-flash-controller"
Expand Down
2 changes: 2 additions & 0 deletions samples/async-philosophers/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ common:
- qemu_riscv32
- qemu_riscv64
- nrf52840dk/nrf52840
- nrf54l15dk/nrf54l15/cpuapp
- nrf54l15dk/nrf54l15/cpuflpr
tests:
sample.rust.work-philosopher:
tags: introduction
Expand Down
5 changes: 4 additions & 1 deletion samples/async-philosophers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ const NUM_PHIL: usize = 6;

#[no_mangle]
extern "C" fn rust_main() {
printkln!("Async dining philosophers{}", zephyr::kconfig::CONFIG_BOARD);
printkln!(
"Async dining philosophers {}",
zephyr::kconfig::CONFIG_BOARD_TARGET
);
printkln!("Time tick: {}", zephyr::time::SYS_FREQUENCY);

let executor = EXECUTOR.init(Executor::new());
Expand Down
1 change: 1 addition & 0 deletions samples/bench/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ common:
- qemu_riscv32
- qemu_riscv64
- nrf52840dk/nrf52840
- nrf54l15dk/nrf54l15/cpuapp
tests:
sample.rust/bench.plain:
tags: benchmark
Expand Down
1 change: 1 addition & 0 deletions samples/blinky/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ common:
- qemu_riscv32
- qemu_riscv64
- nrf52840dk/nrf52840
- nrf54l15dk/nrf54l15/cpuapp
tests:
sample.rust.basic.blinky:
tags:
Expand Down
1 change: 1 addition & 0 deletions samples/embassy/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ common:
- qemu_riscv32
- qemu_riscv64
- nrf52840dk/nrf52840
- nrf54l15dk/nrf54l15/cpuapp
tests:
sample.rust.embassyhello:
tags: introduction
2 changes: 1 addition & 1 deletion samples/embassy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ extern "C" fn rust_main() {

info!(
"Starting Embassy executor on {}",
zephyr::kconfig::CONFIG_BOARD
zephyr::kconfig::CONFIG_BOARD_TARGET
);

let executor = EXECUTOR_MAIN.init(Executor::new());
Expand Down
2 changes: 2 additions & 0 deletions samples/hello_world/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ common:
- qemu_riscv32
- qemu_riscv64
- nrf52840dk/nrf52840
- nrf54l15dk/nrf54l15/cpuapp
- nrf54l15dk/nrf54l15/cpuflpr
tests:
sample.rust.helloworld:
tags: introduction
2 changes: 1 addition & 1 deletion samples/hello_world/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ extern "C" fn rust_main() {
zephyr::set_logger().unwrap();
}

info!("Hello world from Rust on {}", zephyr::kconfig::CONFIG_BOARD);
info!("Hello world from Rust on {}", zephyr::kconfig::CONFIG_BOARD_TARGET);
Copy link
Collaborator

Choose a reason for hiding this comment

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

You'll need to either format these manual, or just use cargo fmt to reflow them. With the longer name, it breaks this across multiple lines.

}
1 change: 1 addition & 0 deletions samples/philosophers/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ common:
- qemu_riscv32
- qemu_riscv64
- nrf52840dk/nrf52840
- nrf54l15dk/nrf54l15/cpuapp
tests:
sample.rust.philosopher.semaphore:
tags: introduction
Expand Down
5 changes: 4 additions & 1 deletion samples/philosophers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ trait ForkSync: core::fmt::Debug + Sync + Send {

#[no_mangle]
extern "C" fn rust_main() {
printkln!("Hello world from Rust on {}", zephyr::kconfig::CONFIG_BOARD);
printkln!(
"Hello world from Rust on {}",
zephyr::kconfig::CONFIG_BOARD_TARGET
);
printkln!("Time tick: {}", zephyr::time::SYS_FREQUENCY);

let stats = &STAT_MUTEX;
Expand Down
Loading