Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
8f271f9
update config of git
foxg1ove1 Sep 29, 2025
ed5cd4c
新建rust项目架构,包括项目编译脚本、config配置以及rust编译配置
foxg1ove1 Sep 29, 2025
76110e8
增加新模块,包括string、memory和thread等实现
foxg1ove1 Sep 29, 2025
e7b9b67
补充配置文件
foxg1ove1 Sep 29, 2025
684ebd2
新增动态加载模块
foxg1ove1 Sep 29, 2025
7884895
动态加载模块
foxg1ove1 Sep 29, 2025
6ed3785
完善脚本和文档说明
foxg1ove1 Sep 30, 2025
aafa522
email填写
foxg1ove1 Sep 30, 2025
ea6ea82
不重要的修改
foxg1ove1 Sep 30, 2025
55c0dd4
补全基本接口
foxg1ove1 Oct 10, 2025
1d21348
Fix compilation errors in dlmodule_demo.rs
foxg1ove1 Oct 11, 2025
42e3b4a
更成熟的libload封装,同时添加了bench测试
foxg1ove1 Oct 15, 2025
4703ab5
修改了项目结构,增加了macro-main便于用户使用
foxg1ove1 Oct 19, 2025
bdd34b0
恢复RT-Thread config配置
foxg1ove1 Oct 19, 2025
c9f068b
删去不使用的函数
foxg1ove1 Oct 19, 2025
7bac9b3
rust debug模式
foxg1ove1 Oct 19, 2025
a016a8b
删去不必要的代码
foxg1ove1 Oct 19, 2025
83f236f
重命名rust库
foxg1ove1 Oct 21, 2025
e8da6e2
设置.gitignore
foxg1ove1 Oct 21, 2025
d449a40
修复了一些线程的bug
foxg1ove1 Oct 21, 2025
090c74d
添加macro-main注册组件功能,并使用logging测试
foxg1ove1 Oct 22, 2025
3bca57d
删除不需要的文件
foxg1ove1 Oct 22, 2025
7315a3b
完善macro-main
foxg1ove1 Oct 22, 2025
737ac0b
增加文件操作
foxg1ove1 Oct 22, 2025
74c4386
增加macro-main说明
foxg1ove1 Oct 23, 2025
a1a7234
增加日志组件测试,分离应用与组件
foxg1ove1 Oct 24, 2025
c34ea9e
改变组件注册方式
foxg1ove1 Oct 24, 2025
b4355aa
配置.gitignore
foxg1ove1 Oct 25, 2025
3fd0715
恢复初始配置
foxg1ove1 Oct 25, 2025
8da8444
解决合并冲突:保留 Kconfig 文件
foxg1ove1 Oct 25, 2025
97aac92
恢复applications
foxg1ove1 Oct 25, 2025
f2b7230
恢复./gitmodules
foxg1ove1 Oct 25, 2025
140278e
配置 .gitignore:忽略 .config, Kconfig, link.lds, rtconfig.h, rtconfig.py
foxg1ove1 Oct 27, 2025
989d3b1
./gitignore Kconfig
foxg1ove1 Oct 27, 2025
82b549d
集成examples
foxg1ove1 Oct 29, 2025
dc04dae
解耦合过程宏,分解各种功能的rust形式export
foxg1ove1 Oct 29, 2025
2eb6fd2
Remove rust-analyzer temporary files and update .gitignore
foxg1ove1 Oct 29, 2025
d39848b
重命名core组件
foxg1ove1 Oct 29, 2025
e75bcf6
修复小bug、注释
foxg1ove1 Oct 29, 2025
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
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,12 @@ vdso.lds
# stm32cubemx
**/CubeMX_Config/Drivers/
**/CubeMX_Config/MDK-ARM/

# Rust
target/
Cargo.lock
*.rs.bk
*.pdb

# rust-analyzer
**/target/rust-analyzer/
13 changes: 13 additions & 0 deletions machines/qemu-virt-riscv64/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
mnt.c
romfs_data.c
opensbi
build
/*.config
/*.lds
/*.sh
/rtconfig*
text.txt
fat
applications/*
/.config
/link.lds
/rtconfig.h
/rtconfig.py
/Kconfig
1 change: 1 addition & 0 deletions machines/qemu-virt-riscv64/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ PKGS_DIR := packages
source "$(RTT_DIR)/Kconfig"
osource "$PKGS_DIR/Kconfig"
rsource "driver/Kconfig"
rsource "rust/Kconfig"

config BOARD_QEMU_VIRT_RV64
bool
Expand Down
26 changes: 26 additions & 0 deletions machines/qemu-virt-riscv64/rust/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
menuconfig RT_USING_RUST
bool "Enable Rust component support"
default n
help
Enable Rust programming language support for RT-Thread.
This allows you to write RT-Thread components using Rust.

if RT_USING_RUST
config RT_RUST_CORE
bool "Enable Rust Core Library"
default y
config RUST_DEBUG_BUILD
bool "Build Rust code in debug mode"
default n
help
Build Rust code with debug symbols and without optimizations.
This increases binary size but helps with debugging.
config RUST_INIT_COMPONENT
bool "Auto-initialize Rust component"
default y
help
Automatically initialize Rust component during RT-Thread startup.

rsource "examples/Kconfig"

endif
160 changes: 160 additions & 0 deletions machines/qemu-virt-riscv64/rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# RT-Thread Rust组件目录结构建议

## 一、目录结构

`rust/` 为以下结构:

```
rust/
├── Kconfig # 组件配置
├── SConscript # 构建脚本
├── README.md # 组件说明
├── docs/ # 文档
│ ├── api_reference.md
│ ├── abi_compatibility.md
│ └── performance_report.md
├── core/ # 核心支持库
│ ├── Cargo.toml
│ ├── src/
│ │ ├── lib.rs
│ │ ├── bindings/ # C接口绑定
│ │ │ ├── kernel.rs # 内核API
│ │ │ ├── thread.rs # 线程API
│ │ │ ├── ipc.rs # IPC机制
│ │ │ ├── memory.rs # 内存管理
│ │ │ └── device.rs # 设备API
│ │ ├── rt_prelude.rs # 预导入模块
│ │ ├── allocator.rs # 内存分配器
│ │ ├── panic.rs # panic处理
│ │ └── macros/ # 宏支持
│ │ ├── thread_entry.rs # 线程入口宏
│ │ └── msh_export.rs # Shell命令导出宏
├── runtime/ # 运行时支持
│ ├── Cargo.toml
│ ├── src/
│ │ ├── no_std_support.rs # no_std模式
│ │ ├── start.rs # 启动代码
│ │ └── lang_items.rs # 语言项
│ └── linker/
│ └── rust_module.ld # 链接脚本
├── shell/ # Shell命令支持
│ ├── Cargo.toml
│ └── src/
│ └── commands.rs
├── examples/ # 示例代码
│ ├── README.md
│ ├── applications/ # 应用示例
│ │ ├── hello_world/
│ │ ├── thread_sync/
│ │ └── device_io/
│ ├── components/ # 组件示例
│ │ ├── logger/
│ │ ├── sensor_driver/
│ │ └── protocol_stack/
│ └── modules/ # 内核模块示例
│ ├── simple_module/
│ └── device_module/
├── tools/ # 工具脚本
│ ├── build_rust.py
│ ├── gen_bindings.sh
│ └── cargo_wrapper.py
└── tests/ # 测试
├── integration/
└── unit/
```

## 二、各部分功能说明

### 2.1 核心支持库 (core/)

**提供Rust本身的基础支持和RT-Thread系统服务绑定:**

1. **bindings/** - C接口的安全封装
- 按功能模块划分(kernel、thread、ipc、memory、device)
- 使用bindgen自动生成或手工编写
- 提供类型安全的Rust接口

2. **rt_prelude.rs** - 预导入模块
- 类似std::prelude,导入常用类型和trait
- 简化Rust代码编写

3. **allocator.rs** - 全局内存分配器
- 实现`GlobalAlloc` trait
- 对接RT-Thread的内存管理系统

4. **macros/** - 宏定义
- `rt_thread_main!` - 程序入口宏,标记Rust的main函数
- `rt_component_export!` - 导出组件初始化入口的宏
- `rt_app_export!` - 导出应用初始化入口的宏
- `msh_cmd_export!` - 导出shell命令的宏
- 简化在no_std模式下的开发

### 2.2 运行时支持 (runtime/)

**解决no_std模式下的运行时问题:**

- **no_std_support.rs** - no_std环境支持
- **start.rs** - 启动代码,处理main函数到RT-Thread线程的转换
- **lang_items.rs** - 必要的语言项实现
- **linker/** - 链接脚本,用于模块加载

### 2.3 Shell命令支持 (shell/)

**导出Rust命令到RT-Thread shell:**

- 提供`MSH_CMD_EXPORT`宏的Rust版本
- 自动处理参数解析
- 与finsh组件集成

### 2.4 示例代码 (examples/)

**包含三类示例:**

1. **applications/** - 使用Rust编写应用
- `hello_world/` - 基础示例
- `thread_sync/` - 线程同步示例
- `device_io/` - 设备IO示例

2. **components/** - 使用Rust编写组件/软件包
- `logger/` - 日志组件
- `sensor_driver/` - 传感器驱动
- `protocol_stack/` - 协议栈

3. **modules/** - 使用Rust编写内核动态模块
- `simple_module/` - 简单模块示例
- `device_module/` - 设备驱动模块示例

## 三、构建系统集成

### 3.1 Kconfig配置示例

```kconfig
menuconfig RT_USING_RUST
bool "Rust Language Support"
default n
if RT_USING_RUST
config RT_RUST_CORE
bool "Enable Rust Core Library"
default y
config RT_RUST_RUNTIME
bool "Enable Rust Runtime Support"
default y
select RT_RUST_CORE
config RT_RUST_MSH_SUPPORT
bool "Enable Rust MSH Command Support"
default y
depends on RT_USING_FINSH
menu "Rust Examples"
config RT_RUST_EXAMPLES_APPS
bool "Build Application Examples"
default n
config RT_RUST_EXAMPLES_COMPONENTS
bool "Build Component Examples"
default n
config RT_RUST_EXAMPLES_MODULES
bool "Build Module Examples"
default n
depends on RT_USING_LWP && RT_USING_MODULE
endmenu
endif
```
14 changes: 14 additions & 0 deletions machines/qemu-virt-riscv64/rust/SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# for module compiling
import os
from building import *

cwd = GetCurrentDir()
objs = []
list = os.listdir(cwd)

for d in list:
path = os.path.join(cwd, d)
if os.path.isfile(os.path.join(path, 'SConscript')):
objs = objs + SConscript(os.path.join(d, 'SConscript'))

Return('objs')
37 changes: 37 additions & 0 deletions machines/qemu-virt-riscv64/rust/core/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Rust build artifacts (now in build/rust)
/target/
**/*.rs.bk
*.pdb

# Cargo lock file (optional - uncomment if you want to exclude it)
# Cargo.lock

# Build directories
/build/
*.o
*.a
*.so
*.dylib
*.dll

# IDE specific files
.idea/
.vscode/
*.swp
*.swo
*~
.DS_Store

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Backup files
*.bak
*.tmp
*.temp

# Log files
*.log
18 changes: 18 additions & 0 deletions machines/qemu-virt-riscv64/rust/core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "rt-rust"
version = "0.1.0"
edition = "2021"

[lib]
name = "rt_rust"
crate-type = ["rlib", "staticlib"]

[features]
default = []
smp = []

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"
71 changes: 71 additions & 0 deletions machines/qemu-virt-riscv64/rust/core/SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import os
from building import *

cwd = GetCurrentDir()

sys.path.append(os.path.join(cwd, '../tools'))
from build_support import (
detect_rust_target,
make_rustflags,
collect_features,
verify_rust_toolchain,
ensure_rust_target_installed,
cargo_build_staticlib,
clean_rust_build,
)
def _has(sym: str) -> bool:
try:
return bool(GetDepend([sym]))
except Exception:
return bool(GetDepend(sym))


# Source files – MSH command glue
src = ['rust_cmd.c']
LIBS = []
LIBPATH = []

if GetOption('clean'):
# Register Rust artifacts for cleaning
rust_build_dir = clean_rust_build(Dir('#').abspath)
if os.path.exists(rust_build_dir):
print(f'Registering {rust_build_dir} for cleanup')
Clean('.', rust_build_dir)
else:
print('No rust build artifacts to clean')
else:
if verify_rust_toolchain():
import rtconfig

target = detect_rust_target(_has, rtconfig)
if not target:
print('Error: Unable to detect Rust target; please check configuration')
else:
print(f'Detected Rust target: {target}')

# Optional hint if target missing
ensure_rust_target_installed(target)

# Build mode and features
debug = bool(_has('RUST_DEBUG_BUILD'))
features = collect_features(_has)

rustflags = make_rustflags(rtconfig, target)
rust_lib = cargo_build_staticlib(
rust_dir=cwd, target=target, features=features, debug=debug, rustflags=rustflags
)

if rust_lib:
LIBS = ['rt_rust']
LIBPATH = [os.path.dirname(rust_lib)]
print('Rust library linked successfully')
else:
print('Warning: Failed to build Rust library')
else:
print('Warning: Rust toolchain not found')
print('Please install Rust from https://rustup.rs')

# Define component group for SCons
group = DefineGroup('rust', src, depend=['RT_USING_RUST'], LIBS=LIBS, LIBPATH=LIBPATH)

Return('group')
Loading