Skip to content

Commit 537a05b

Browse files
committed
{kconfig, cmake}: add options to build nightly and std from source
Some targets need to be built with nightly and `-Z build-std=core,alloc`
1 parent 2427d1a commit 537a05b

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

CMakeLists.txt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ function(rust_cargo_application)
134134
endif()
135135
set(BUILD_LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${RUST_TARGET}/${RUST_BUILD_TYPE}")
136136

137+
if(CONFIG_RUST_NIGHTLY)
138+
set(CARGO_TOOLCHAIN_OVERRIDE "+nightly")
139+
else()
140+
set(CARGO_TOOLCHAIN_OVERRIDE "")
141+
endif()
142+
143+
if(CONFIG_RUST_BUILD_STD)
144+
set(CARGO_ARGS "-Zbuild-std=core,alloc")
145+
else()
146+
set(CARGO_ARGS "")
147+
endif()
148+
137149
set(CARGO_TARGET_DIR "${CMAKE_CURRENT_BINARY_DIR}/rust/target")
138150
set(RUST_LIBRARY "${CARGO_TARGET_DIR}/${RUST_TARGET}/${RUST_BUILD_TYPE}/librustapp.a")
139151
set(SAMPLE_CARGO_CONFIG "${CMAKE_CURRENT_BINARY_DIR}/rust/sample-cargo-config.toml")
@@ -214,7 +226,7 @@ ${config_paths}
214226
WRAPPER_FILE="${WRAPPER_FILE}"
215227
DT_AUGMENTS="${DT_AUGMENTS}"
216228
BINARY_DIR_INCLUDE_GENERATED="${BINARY_DIR_INCLUDE_GENERATED}"
217-
cargo build
229+
cargo ${CARGO_TOOLCHAIN_OVERRIDE} build
218230
${rust_build_type_arg}
219231

220232
# Override the features according to the shield given. For a general case,
@@ -228,6 +240,7 @@ ${config_paths}
228240
${command_paths}
229241
--target ${RUST_TARGET}
230242
--target-dir ${CARGO_TARGET_DIR}
243+
${CARGO_ARGS}
231244
COMMENT "Building Rust application"
232245
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
233246
)
@@ -258,7 +271,7 @@ ${config_paths}
258271
WRAPPER_FILE="${WRAPPER_FILE}"
259272
DT_AUGMENTS="${DT_AUGMENTS}"
260273
BINARY_DIR_INCLUDE_GENERATED="${BINARY_DIR_INCLUDE_GENERATED}"
261-
cargo doc
274+
cargo ${CARGO_TOOLCHAIN_OVERRIDE} doc
262275
${rust_build_type_arg}
263276

264277
# Override the features according to the shield given. For a general case,
@@ -272,6 +285,7 @@ ${config_paths}
272285
${command_paths}
273286
--target ${RUST_TARGET}
274287
--target-dir ${CARGO_TARGET_DIR}
288+
${CARGO_ARGS}
275289
COMMENT "Building Rust documentation"
276290
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
277291
)

Kconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@ config RUST_ALLOC
3131
Zephyr allocator (malloc/free). This this enabled, Rust
3232
applications can use the `alloc` crate.
3333

34+
config RUST_NIGHTLY
35+
bool "Use nightly for building the target"
36+
default y if RISCV_ISA_RV32E
37+
help
38+
If enabled, nightly edition will be used for building.
39+
40+
if RUST_NIGHTLY
41+
42+
config RUST_BUILD_STD
43+
bool "Build standard library from source"
44+
default y if RISCV_ISA_RV32E
45+
help
46+
Some targets require building standard library from source.
47+
If enabled, this will add `-Z build-std=core,alloc` to cargo build.
48+
Depends on nightly edition.
49+
You need to run `rustup +nightly component add rust-src` before building.
50+
51+
endif # RUST_NIGHTLY
52+
3453
endif # RUST
3554

3655
endmenu

0 commit comments

Comments
 (0)