Adding Rust Language Support for RT-Thread #10910
ToolsCI / Tools (push) Has been cancelled
RT-Thread BSP Static Build Check / 🔍 Summary of Git Diff Changes (push) Has been cancelled
RT-Thread BSP Static Build Check / ${{ matrix.legs.RTT_BSP }} (push) Has been cancelled
RT-Thread BSP Static Build Check / collect-artifacts (push) Has been cancelled
pkgs_test / change (push) Has been cancelled
utest_auto_run / A9 :components/dfs.cfg (push) Has been cancelled
utest_auto_run / A9 :components/lwip.cfg (push) Has been cancelled
utest_auto_run / A9 :components/netdev.cfg (push) Has been cancelled
utest_auto_run / A9 :components/sal.cfg (push) Has been cancelled
utest_auto_run / A9 :cpp11/cpp11.cfg (push) Has been cancelled
utest_auto_run / AARCH64-rtsmart :default.cfg (push) Has been cancelled
utest_auto_run / A9-rtsmart :default.cfg (push) Has been cancelled
utest_auto_run / RISCV-rtsmart :default.cfg (push) Has been cancelled
utest_auto_run / XUANTIE-rtsmart :default.cfg (push) Has been cancelled
utest_auto_run / AARCH64 :default.cfg (push) Has been cancelled
utest_auto_run / AARCH64-smp :default.cfg (push) Has been cancelled
utest_auto_run / A9 :default.cfg (push) Has been cancelled
utest_auto_run / A9-smp :default.cfg (push) Has been cancelled
utest_auto_run / RISCV :default.cfg (push) Has been cancelled
utest_auto_run / RISCV-smp :default.cfg (push) Has been cancelled
utest_auto_run / A9 :kernel/atomic_c11.cfg (push) Has been cancelled
utest_auto_run / RISCV :kernel/atomic_c11.cfg (push) Has been cancelled
utest_auto_run / A9 :kernel/ipc.cfg (push) Has been cancelled
utest_auto_run / A9 :kernel/kernel_basic.cfg (push) Has been cancelled
utest_auto_run / A9 :kernel/mem.cfg (push) Has been cancelled
Weekly CI Scheduler / Trigger and Monitor CIs (push) Has been cancelled
Weekly CI Scheduler / Create Discussion Report (push) Has been cancelled

This commit is contained in:
zhang san
2025-12-08 18:34:25 +08:00
committed by GitHub
parent cd1d47b87c
commit 69980f8b9d
88 changed files with 6734 additions and 4 deletions
@@ -0,0 +1,70 @@
import os
import sys
from building import *
cwd = GetCurrentDir()
# Import usrapp build module and build support
sys.path.append(os.path.join(cwd, '../../tools'))
from build_usrapp import build_example_usrapp
from build_support import clean_rust_build
def _has(sym: str) -> bool:
try:
return bool(GetDepend([sym]))
except Exception:
return bool(GetDepend(sym))
def load_extended_feature_configs():
"""
Load extended feature configurations if available.
This allows users to add custom configuration mappings.
"""
try:
from feature_config_examples import setup_all_example_features
setup_all_example_features()
print("Extended feature configurations loaded successfully.")
except ImportError:
print("Using default feature configurations.")
group = []
if not _has('RT_RUST_BUILD_APPLICATIONS'):
Return('group')
# Load extended feature configurations
load_extended_feature_configs()
src = []
LIBS = []
LIBPATH = []
LINKFLAGS = ""
if GetOption('clean'):
app_build_dir = clean_rust_build(Dir('#').abspath, "example_usrapp")
if os.path.exists(app_build_dir):
print(f'Registering {app_build_dir} for cleanup')
Clean('.', app_build_dir)
else:
print('No example_usrapp build artifacts to clean')
else:
import rtconfig
LIBS, LIBPATH, LINKFLAGS = build_example_usrapp(
cwd=cwd,
has_func=_has,
rtconfig=rtconfig,
build_root=os.path.join(Dir('#').abspath, "build", "example_usrapp")
)
group = DefineGroup(
'example_usrapp',
src,
depend=['RT_USING_RUST'],
LIBS=LIBS,
LIBPATH=LIBPATH,
LINKFLAGS=LINKFLAGS
)
Return('group')
@@ -0,0 +1,17 @@
[package]
name = "em_fs"
version = "0.1.0"
edition = "2021"
[lib]
name = "em_fs"
crate-type = ["staticlib"]
[features]
default = []
enable-log = ["em_component_log/enable-log"]
[dependencies]
rt-rust = { path = "../../../core" }
rt_macros = { path = "../../../rt_macros" }
em_component_log = { path = "../../component/log"}
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author notes
* 2025-10-20 foxglove Rust file operation test.
* 2025-10-29 foxglove Updated to demonstrate new modular macro interface
*/
#![no_std]
extern crate alloc;
use rt_macros::msh_cmd_export;
use rt_rust::{fs, println};
use rt_rust::param::Param;
use em_component_log::{info, error};
#[msh_cmd_export(name = "rust_file_demo", desc = "Rust example app.")]
fn main(_param: Param) {
println!("[rust_file_test] start");
let mut file = match fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.append(false)
.truncate(true)
.open("test.txt")
{
Ok(f) => {
info!("open test.txt ok");
f
}
Err(e) => {
error!("open error: {:?}", e);
return;
}
};
if let Err(e) = file.write_all("Hello from FS wrapper!\n") {
error!("write_all error: {:?}", e);
return;
}
info!("write_all done");
if let Err(e) = file.flush() {
error!("flush error: {:?}", e);
} else {
info!("flush ok");
}
match file.read_to_string() {
Ok(s) => info!("read_back: {}", s),
Err(e) => error!("read_to_string error: {:?}", e),
}
if let Err(e) = file.set_len(5) {
error!("truncate error: {:?}", e);
} else {
info!("truncate to 5 ok");
}
if let Err(e) = file.close() {
error!("close error: {:?}", e);
} else {
info!("close ok");
}
info!("[rust_file_test] end");
}
@@ -0,0 +1,12 @@
[package]
name = "em_loadlib"
version = "0.1.0"
edition = "2021"
[lib]
name = "em_loadlib"
crate-type = ["staticlib"]
[dependencies]
rt-rust = { path = "../../../core" }
rt_macros = { path = "../../../rt_macros" }
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author notes
* 2025-10-10 foxglove load library example
* 2025-10-29 foxglove Updated to demonstrate new modular macro interface
*/
#![no_std]
extern crate alloc;
use rt_macros::msh_cmd_export;
use rt_rust::println;
use rt_rust::get_libfn;
use core::ffi::{c_int, c_char};
use rt_rust::param::Param;
#[msh_cmd_export(name = "rust_dl_demo", desc = "Rust dynamic library demo")]
fn main(_param: Param) {
println!("\n=== Dynamic Library Demo ===");
get_libfn!("/hello.mo", "main", my_hello, ());
my_hello();
get_libfn!("/libmylib.mo", "rust_mylib_add", my_add, c_int, a: c_int, b: c_int);
let s = my_add(15, 20);
println!("my_add(15, 20) = {}", s);
get_libfn!("/libmylib.mo", "rust_mylib_println", my_println, (), s: *const c_char);
my_println(b"rustlib: Hello World\0".as_ptr() as *const c_char);
}
@@ -0,0 +1,12 @@
[package]
name = "em_mutex"
version = "0.1.0"
edition = "2021"
[lib]
name = "em_mutex"
crate-type = ["staticlib"]
[dependencies]
rt-rust = { path = "../../../core" }
rt_macros = { path = "../../../rt_macros" }
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author notes
* 2025-10-10 foxglove mutex test demo
* 2025-10-29 foxglove Updated to demonstrate new modular macro interface
*/
#![no_std]
extern crate alloc;
use alloc::sync::Arc;
use core::time::Duration;
use rt_macros::msh_cmd_export;
use rt_rust::mutex::Mutex;
use rt_rust::param::Param;
use rt_rust::println;
use rt_rust::thread;
use rt_rust::time;
#[msh_cmd_export(name = "rust_mutex_demo", desc = "Rust example app.")]
fn main(_param: Param) {
let counter = Arc::new(Mutex::new(0).unwrap());
let run = move || loop {
time::sleep(Duration::new(2, 0));
{
let mut c = counter.lock().unwrap();
*c += 1;
println!("{}", *c);
}
};
let _ = thread::Thread::new()
.name("thread 1")
.stack_size(2048)
.start(run.clone());
time::sleep(Duration::new(1, 0));
let _ = thread::Thread::new()
.name("thread 2")
.stack_size(2048)
.start(run.clone());
}
@@ -0,0 +1,13 @@
[package]
name = "em_param"
version = "0.1.0"
edition = "2021"
[lib]
name = "em_param"
crate-type = ["staticlib"]
[dependencies]
rt-rust = { path = "../../../core" }
rt_macros = { path = "../../../rt_macros" }
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author notes
* 2025-10-10 foxglove param test demo
* 2025-10-29 foxglove Updated to demonstrate new modular macro interface
*/
#![no_std]
extern crate alloc;
use alloc::string::String;
use rt_macros::{rt_app_export, msh_cmd_export};
use rt_rust::param::Param;
use rt_rust::println;
// 演示新的模块化宏接口 - 使用 msh_cmd_export 导出参数测试命令
#[msh_cmd_export(name = "rust_param_demo", desc = "Rust parameter demo")]
fn main(param: Param) {
for i in param {
println!("{}", String::from_utf8_lossy(&*i))
}
}
@@ -0,0 +1,12 @@
[package]
name = "em_queue"
version = "0.1.0"
edition = "2021"
[lib]
name = "em_queue"
crate-type = ["staticlib"]
[dependencies]
rt-rust = { path = "../../../core" }
rt_macros = { path = "../../../rt_macros" }
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author notes
* 2025-10-10 foxglove queue test demo
* 2025-10-29 foxglove Updated to demonstrate new modular macro interface
*/
#![no_std]
extern crate alloc;
use alloc::string::String;
use alloc::sync::Arc;
use core::time::Duration;
use rt_macros::msh_cmd_export;
use rt_rust::queue::Queue;
use rt_rust::param::Param;
use rt_rust::println;
use rt_rust::thread;
use rt_rust::time;
#[msh_cmd_export(name = "rust_queue_demo", desc = "Rust example app.")]
fn main(_param: Param) {
let send = Arc::new(Queue::new(2).unwrap());
let recv = send.clone();
let _ = thread::Thread::new()
.name("thread 1")
.stack_size(1024)
.start(move || {
loop {
time::sleep(Duration::new(1, 0));
send.send(String::from("msg"), 0).unwrap();
}
});
time::sleep(Duration::new(1, 0));
let _ = thread::Thread::new()
.name("thread 2")
.stack_size(1024)
.start(move || {
loop {
println!("waiting!");
let a = recv.recv_wait_forever().unwrap();
println!("recv {}", a);
}
});
}
@@ -0,0 +1,12 @@
[package]
name = "em_sem"
version = "0.1.0"
edition = "2021"
[lib]
name = "em_sem"
crate-type = ["staticlib"]
[dependencies]
rt-rust = { path = "../../../core" }
rt_macros = { path = "../../../rt_macros" }
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author notes
* 2025-10-10 foxglove semaphore test demo
* 2025-10-29 foxglove Updated to demonstrate new modular macro interface
*/
#![no_std]
extern crate alloc;
use alloc::sync::Arc;
use core::time::Duration;
use rt_macros::msh_cmd_export;
use rt_rust::sem::Semaphore;
use rt_rust::param::Param;
use rt_rust::println;
use rt_rust::thread;
use rt_rust::time;
#[msh_cmd_export(name = "rust_sem_demo", desc = "Rust example app.")]
fn main(_param: Param) {
let send = Arc::new(Semaphore::new().unwrap());
let recv = send.clone();
let _ = thread::Thread::new()
.name("thread 1")
.stack_size(1024)
.start(move || {
loop {
time::sleep(Duration::new(1, 0));
send.release()
}
});
time::sleep(Duration::new(1, 0));
let _ = thread::Thread::new()
.name("thread 2")
.stack_size(1024)
.start(move || {
loop {
println!("waiting!");
recv.take_wait_forever().unwrap();
println!("recv a sem!")
}
});
}
@@ -0,0 +1,12 @@
[package]
name = "em_thread"
version = "0.1.0"
edition = "2021"
[lib]
name = "em_thread"
crate-type = ["staticlib"]
[dependencies]
rt-rust = { path = "../../../core" }
rt_macros = { path = "../../../rt_macros" }
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author notes
* 2025-10-10 foxglove thread test demo
* 2025-10-29 foxglove Updated to demonstrate new modular macro interface
*/
#![no_std]
use core::time::Duration;
use rt_macros::msh_cmd_export;
use rt_rust::param::Param;
use rt_rust::println;
use rt_rust::thread;
use rt_rust::time;
#[msh_cmd_export(name = "rust_thread_demo", desc = "Rust example app.")]
fn main(_param: Param) {
let _ = thread::Thread::new()
.name("thread 1")
.stack_size(1024)
.start(move || {
loop {
println!("thread a will sleep 1s");
time::sleep(Duration::new(1, 0));
}
});
let _ = thread::Thread::new()
.name("thread 2")
.stack_size(1024)
.start(move || {
loop {
println!("thread b will sleep 3s");
time::sleep(Duration::new(3, 0));
}
});
}