mirror of
https://github.com/apache/nuttx-apps.git
synced 2026-08-02 12:49:03 +00:00
examples/rust: Avoid the infinite loop on sim for hello_rust_cargo
On the sim target (Windows/macOS/Linux), hello_rust_cargo_main() ended
with an infinite `loop {}` that never returned control to NSH, so the
example could not exit and the shell prompt hung.
Introduce a Cargo `sim` feature that is enabled only when CONFIG_ARCH_SIM
is set, and use it to compile out the trailing infinite loop and return 0
instead. Other embedded targets keep the original looping behavior.
Signed-off-by: Shoji Tokunaga <toku@mac.com>
This commit is contained in:
parent
ee4727fdba
commit
d1202bd501
6 changed files with 35 additions and 7 deletions
|
|
@ -130,6 +130,8 @@ endfunction()
|
||||||
# hello
|
# hello
|
||||||
# CRATE_PATH
|
# CRATE_PATH
|
||||||
# ${CMAKE_CURRENT_SOURCE_DIR}/hello
|
# ${CMAKE_CURRENT_SOURCE_DIR}/hello
|
||||||
|
# FEATURES
|
||||||
|
# sim
|
||||||
# )
|
# )
|
||||||
# ~~~
|
# ~~~
|
||||||
|
|
||||||
|
|
@ -142,6 +144,8 @@ function(nuttx_add_rust)
|
||||||
ONE_VALUE
|
ONE_VALUE
|
||||||
CRATE_NAME
|
CRATE_NAME
|
||||||
CRATE_PATH
|
CRATE_PATH
|
||||||
|
MULTI_VALUE
|
||||||
|
FEATURES
|
||||||
REQUIRED
|
REQUIRED
|
||||||
CRATE_NAME
|
CRATE_NAME
|
||||||
CRATE_PATH
|
CRATE_PATH
|
||||||
|
|
@ -159,6 +163,10 @@ function(nuttx_add_rust)
|
||||||
set(RUST_PANIC_FLAGS "")
|
set(RUST_PANIC_FLAGS "")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
foreach(feature ${FEATURES})
|
||||||
|
list(APPEND RUST_FEATURE_FLAGS --features ${feature})
|
||||||
|
endforeach()
|
||||||
|
|
||||||
# Get the Rust target triple
|
# Get the Rust target triple
|
||||||
nuttx_rust_target_triple(${LLVM_ARCHTYPE} ${LLVM_ABITYPE} ${LLVM_CPUTYPE}
|
nuttx_rust_target_triple(${LLVM_ARCHTYPE} ${LLVM_ABITYPE} ${LLVM_CPUTYPE}
|
||||||
RUST_TARGET)
|
RUST_TARGET)
|
||||||
|
|
@ -198,7 +206,7 @@ function(nuttx_add_rust)
|
||||||
RUSTFLAGS=${RUST_PANIC_FLAGS} cargo build ${RUST_PROFILE_FLAG}
|
RUSTFLAGS=${RUST_PANIC_FLAGS} cargo build ${RUST_PROFILE_FLAG}
|
||||||
-Zbuild-std=std,panic_abort -Zjson-target-spec --manifest-path
|
-Zbuild-std=std,panic_abort -Zjson-target-spec --manifest-path
|
||||||
${CRATE_PATH}/Cargo.toml --target ${RUST_TARGET} --target-dir
|
${CRATE_PATH}/Cargo.toml --target ${RUST_TARGET} --target-dir
|
||||||
${RUST_BUILD_DIR}
|
${RUST_BUILD_DIR} ${RUST_FEATURE_FLAGS}
|
||||||
DEPENDS ${RUST_CRATE_SOURCES}
|
DEPENDS ${RUST_CRATE_SOURCES}
|
||||||
COMMENT "Building Rust crate ${CRATE_NAME}"
|
COMMENT "Building Rust crate ${CRATE_NAME}"
|
||||||
VERBATIM)
|
VERBATIM)
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,12 @@
|
||||||
if(CONFIG_EXAMPLES_HELLO_RUST_CARGO)
|
if(CONFIG_EXAMPLES_HELLO_RUST_CARGO)
|
||||||
|
|
||||||
# Build the Rust crate using nuttx_add_rust
|
# Build the Rust crate using nuttx_add_rust
|
||||||
nuttx_add_rust(CRATE_NAME hello CRATE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
|
if(CONFIG_ARCH_SIM)
|
||||||
|
nuttx_add_rust(CRATE_NAME hello CRATE_PATH ${CMAKE_CURRENT_SOURCE_DIR}
|
||||||
|
FEATURES sim)
|
||||||
|
else()
|
||||||
|
nuttx_add_rust(CRATE_NAME hello CRATE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
|
endif()
|
||||||
|
|
||||||
nuttx_add_application(
|
nuttx_add_application(
|
||||||
NAME ${CONFIG_EXAMPLES_HELLO_RUST_CARGO_PROGNAME} STACKSIZE
|
NAME ${CONFIG_EXAMPLES_HELLO_RUST_CARGO_PROGNAME} STACKSIZE
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,9 @@ edition = "2021"
|
||||||
[lib]
|
[lib]
|
||||||
crate-type = ["staticlib"]
|
crate-type = ["staticlib"]
|
||||||
|
|
||||||
|
[features]
|
||||||
|
sim = []
|
||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
panic = "abort"
|
panic = "abort"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,8 +30,12 @@ MODULE = $(CONFIG_EXAMPLES_HELLO_RUST_CARGO)
|
||||||
RUST_LIB := $(call RUST_GET_BINDIR,hello,$(APPDIR)/examples/rust)
|
RUST_LIB := $(call RUST_GET_BINDIR,hello,$(APPDIR)/examples/rust)
|
||||||
RUST_SRCS := $(call RUST_CARGO_SRCS,hello,$(APPDIR)/examples/rust)
|
RUST_SRCS := $(call RUST_CARGO_SRCS,hello,$(APPDIR)/examples/rust)
|
||||||
|
|
||||||
|
ifeq ($(CONFIG_ARCH_SIM),y)
|
||||||
|
HELLO_RUST_FEATURES := sim
|
||||||
|
endif
|
||||||
|
|
||||||
$(RUST_LIB): $(RUST_SRCS)
|
$(RUST_LIB): $(RUST_SRCS)
|
||||||
$(call RUST_CARGO_BUILD,hello,$(APPDIR)/examples/rust)
|
$(call RUST_CARGO_BUILD,hello,$(APPDIR)/examples/rust,$(HELLO_RUST_FEATURES))
|
||||||
|
|
||||||
context:: $(RUST_LIB)
|
context:: $(RUST_LIB)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
|
|
||||||
|
use core::ffi::{c_char, c_int};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
|
|
@ -11,7 +12,7 @@ struct Person {
|
||||||
|
|
||||||
// Function hello_rust_cargo without manglng
|
// Function hello_rust_cargo without manglng
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn hello_rust_cargo_main() {
|
pub extern "C" fn hello_rust_cargo_main(_argc: c_int, _argv: *mut *mut c_char) -> c_int {
|
||||||
// Print hello world to stdout
|
// Print hello world to stdout
|
||||||
|
|
||||||
let john = Person {
|
let john = Person {
|
||||||
|
|
@ -50,7 +51,11 @@ pub extern "C" fn hello_rust_cargo_main() {
|
||||||
println!("Hello world from tokio!");
|
println!("Hello world from tokio!");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
#[cfg(not(feature = "sim"))]
|
||||||
loop {
|
loop {
|
||||||
// Do nothing
|
// Do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "sim")]
|
||||||
|
0
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -90,11 +90,12 @@ endef
|
||||||
|
|
||||||
# Build Rust project using cargo
|
# Build Rust project using cargo
|
||||||
#
|
#
|
||||||
# Usage: $(call RUST_CARGO_BUILD,cratename,prefix)
|
# Usage: $(call RUST_CARGO_BUILD,cratename,prefix[,features])
|
||||||
#
|
#
|
||||||
# Inputs:
|
# Inputs:
|
||||||
# cratename - Name of the Rust crate (e.g. hello)
|
# cratename - Name of the Rust crate (e.g. hello)
|
||||||
# prefix - Path prefix to the crate (e.g. path/to/project)
|
# prefix - Path prefix to the crate (e.g. path/to/project)
|
||||||
|
# features - Optional Cargo features to enable
|
||||||
#
|
#
|
||||||
# Output:
|
# Output:
|
||||||
# None, builds the Rust project
|
# None, builds the Rust project
|
||||||
|
|
@ -105,7 +106,8 @@ define RUST_CARGO_BUILD
|
||||||
RUSTFLAGS="-Zunstable-options -Cpanic=immediate-abort" \
|
RUSTFLAGS="-Zunstable-options -Cpanic=immediate-abort" \
|
||||||
cargo build --release -Zbuild-std=std,panic_abort -Zjson-target-spec \
|
cargo build --release -Zbuild-std=std,panic_abort -Zjson-target-spec \
|
||||||
--manifest-path $(2)/$(1)/Cargo.toml \
|
--manifest-path $(2)/$(1)/Cargo.toml \
|
||||||
--target $(call RUST_TARGET_TRIPLE)
|
--target $(call RUST_TARGET_TRIPLE) \
|
||||||
|
$(if $(strip $(3)),--features $(3),)
|
||||||
endef
|
endef
|
||||||
else
|
else
|
||||||
define RUST_CARGO_BUILD
|
define RUST_CARGO_BUILD
|
||||||
|
|
@ -113,7 +115,8 @@ define RUST_CARGO_BUILD
|
||||||
NUTTX_INCLUDE_DIR=$(TOPDIR)/include:$(TOPDIR)/include/arch \
|
NUTTX_INCLUDE_DIR=$(TOPDIR)/include:$(TOPDIR)/include/arch \
|
||||||
cargo build -Zbuild-std=std,panic_abort -Zjson-target-spec \
|
cargo build -Zbuild-std=std,panic_abort -Zjson-target-spec \
|
||||||
--manifest-path $(2)/$(1)/Cargo.toml \
|
--manifest-path $(2)/$(1)/Cargo.toml \
|
||||||
--target $(call RUST_TARGET_TRIPLE)
|
--target $(call RUST_TARGET_TRIPLE) \
|
||||||
|
$(if $(strip $(3)),--features $(3),)
|
||||||
endef
|
endef
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue