diff --git a/.gitignore b/.gitignore index 85873b59..1674601d 100644 --- a/.gitignore +++ b/.gitignore @@ -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. diff --git a/CMakeLists.txt b/CMakeLists.txt index 75ece412..156b17da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() @@ -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") @@ -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, @@ -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} ) @@ -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, @@ -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} ) diff --git a/Kconfig b/Kconfig index 0b2d057b..b400a600 100644 --- a/Kconfig +++ b/Kconfig @@ -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 @@ -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 diff --git a/dt-rust.yaml b/dt-rust.yaml index 01c747ab..d083c3ac 100644 --- a/dt-rust.yaml +++ b/dt-rust.yaml @@ -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" diff --git a/samples/async-philosophers/sample.yaml b/samples/async-philosophers/sample.yaml index 1b960183..8d399143 100644 --- a/samples/async-philosophers/sample.yaml +++ b/samples/async-philosophers/sample.yaml @@ -15,6 +15,8 @@ common: - qemu_riscv32 - qemu_riscv64 - nrf52840dk/nrf52840 + - nrf54l15dk/nrf54l15/cpuapp + - nrf54l15dk/nrf54l15/cpuflpr tests: sample.rust.work-philosopher: tags: introduction diff --git a/samples/async-philosophers/src/lib.rs b/samples/async-philosophers/src/lib.rs index 0cf4b3b7..dbc4e8b6 100644 --- a/samples/async-philosophers/src/lib.rs +++ b/samples/async-philosophers/src/lib.rs @@ -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()); diff --git a/samples/bench/sample.yaml b/samples/bench/sample.yaml index 0d8e5c1e..87832aab 100644 --- a/samples/bench/sample.yaml +++ b/samples/bench/sample.yaml @@ -16,6 +16,7 @@ common: - qemu_riscv32 - qemu_riscv64 - nrf52840dk/nrf52840 + - nrf54l15dk/nrf54l15/cpuapp tests: sample.rust/bench.plain: tags: benchmark diff --git a/samples/blinky/sample.yaml b/samples/blinky/sample.yaml index 169fa5ed..7e1ea701 100644 --- a/samples/blinky/sample.yaml +++ b/samples/blinky/sample.yaml @@ -9,6 +9,7 @@ common: - qemu_riscv32 - qemu_riscv64 - nrf52840dk/nrf52840 + - nrf54l15dk/nrf54l15/cpuapp tests: sample.rust.basic.blinky: tags: diff --git a/samples/embassy/sample.yaml b/samples/embassy/sample.yaml index c4a24cf5..b4f76c9f 100644 --- a/samples/embassy/sample.yaml +++ b/samples/embassy/sample.yaml @@ -15,6 +15,7 @@ common: - qemu_riscv32 - qemu_riscv64 - nrf52840dk/nrf52840 + - nrf54l15dk/nrf54l15/cpuapp tests: sample.rust.embassyhello: tags: introduction diff --git a/samples/embassy/src/lib.rs b/samples/embassy/src/lib.rs index 8bfd14be..e8145819 100644 --- a/samples/embassy/src/lib.rs +++ b/samples/embassy/src/lib.rs @@ -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()); diff --git a/samples/hello_world/sample.yaml b/samples/hello_world/sample.yaml index 48d2a383..4c5e1382 100644 --- a/samples/hello_world/sample.yaml +++ b/samples/hello_world/sample.yaml @@ -15,6 +15,8 @@ common: - qemu_riscv32 - qemu_riscv64 - nrf52840dk/nrf52840 + - nrf54l15dk/nrf54l15/cpuapp + - nrf54l15dk/nrf54l15/cpuflpr tests: sample.rust.helloworld: tags: introduction diff --git a/samples/hello_world/src/lib.rs b/samples/hello_world/src/lib.rs index ce2a33e6..34ddd203 100644 --- a/samples/hello_world/src/lib.rs +++ b/samples/hello_world/src/lib.rs @@ -11,5 +11,8 @@ 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 + ); } diff --git a/samples/philosophers/sample.yaml b/samples/philosophers/sample.yaml index 8738850c..75feb3a2 100644 --- a/samples/philosophers/sample.yaml +++ b/samples/philosophers/sample.yaml @@ -17,6 +17,7 @@ common: - qemu_riscv32 - qemu_riscv64 - nrf52840dk/nrf52840 + - nrf54l15dk/nrf54l15/cpuapp tests: sample.rust.philosopher.semaphore: tags: introduction diff --git a/samples/philosophers/src/lib.rs b/samples/philosophers/src/lib.rs index 473e84b5..a576bb9a 100644 --- a/samples/philosophers/src/lib.rs +++ b/samples/philosophers/src/lib.rs @@ -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;