1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
# Example Component - RT-Thread Rust Component Example
## Overview
`example_component` is a Rust component example based on RT-Thread, demonstrating how to implement, register, and manage Rust components in RT-Thread systems. This project adopts a unified component registration architecture that avoids component duplicate registration issues and supports conditional compilation based on RT-Thread configuration.
## Project Structure
```
example_component/
├── SConscript # SCons build script
├── component_registry/ # Unified component registry
│ ├── Cargo.toml # Registry configuration
│ └── src/
│ └── lib.rs # Registry implementation
├── log/ # Log component example
│ ├── Cargo.toml # Log component configuration
│ └── src/
│ ├── lib.rs # Log component implementation
│ └── logging.rs # Logging functionality module
└── tools/ # Build tools
├── build_component.py # Component build tool
└── feature_config_component.py # Feature configuration
```
## Core Features
### 1. Unified Component Registration Architecture
- **Avoid Duplicate Registration**: Unified management of all component registrations through `component_registry`, avoiding conflicts caused by multiple component duplicate registrations
- **Conditional Compilation Support**: Selectively compile and register components based on RT-Thread configuration
- **Modular Design**: Each component is implemented independently and managed uniformly through the registry
### 2. Feature Configuration System
- **Based on RT-Thread Configuration**: Feature enablement is entirely based on RT-Thread configuration items, requiring no additional configuration
- **Automatic Feature Mapping**: Build system automatically maps RT-Thread configuration to Rust features
- **Dependency Checking**: Automatically checks component dependencies to ensure build consistency
### 3. Build System Integration
- **SCons Integration**: Fully integrated with RT-Thread's SCons build system
- **Incremental Build**: Supports clean and incremental builds
- **Cross Compilation**: Supports cross-compilation for target architectures like RISC-V
## Component Details
### Component Registry
`component_registry` is the core of the entire architecture, responsible for unified management of all component registrations.
#### Main Functions
1. **Unified Entry Point**: Provides `rust_component_registry` as the unified registration entry for all components
2. **Conditional Compilation**: Only includes and registers corresponding components when respective features are enabled
3. **Avoid Conflicts**: Ensures each component is registered only once
### Log Component
The `log` component provides Rust-style logging functionality, including multiple log levels and command-line interface.
#### Main Functions
1. **Multi-level Logging**: Supports INFO, WARN, ERROR, DEBUG, TRACE and other log levels
2. **Command-line Interface**: Provides commands like `log_info`, `log_warn`, `log_error`
3. **Conditional Compilation**: Only compiles when `enable-log` feature is enabled
#### Usage Examples
```rust
// Using log macros in code
info!("This is an info log");
warn!("This is a warning log");
error!("This is an error log");
```
#### Command-line Usage
```bash
# Using in RT-Thread command line
msh> log_info # Print info log
msh> log_warn # Print warning log
msh> log_error # Print error log
```
## Feature Configuration
### Configuration Mapping
The mapping from RT-Thread configuration to Rust features is defined in `tools/feature_config_component.py`:
```python
CONFIG_COMPONENT_FEATURE_MAP = {
'RUST_LOG_COMPONENT': {
'feature': 'enable-log',
'dependency': 'em_component_log',
'description': 'Enable Rust log component integration'
}
}
```
### Enabling Components
To enable the log component, set in RT-Thread configuration:
```c
// Set through menuconfig
Enable Rust component support -> Auto-initialize Rust log component
```
## Development Guide
### Adding New Components
1. **Create Component Directory**
```bash
cargo new --lib example_component/new_component
cd example_component/new_component
```
2. **Create Cargo.toml**
```toml
[package]
name = "em_component_new"
version = "0.1.0"
edition = "2021"
[lib]
name = "em_component_new"
crate-type = ["staticlib"]
[dependencies]
rt-rust = { path = "../../rust" }
macro_main = { path = "../../rust/macro-main" }
[features]
default = []
enable-new = []
```
3. **Implement Component Functionality**
```rust
// src/new.rs
// New component implementation code
// src/lib.rs
#![no_std]
// Export required libraries
```
4. **Update Feature Configuration**
```python
# Add to tools/feature_config_component.py
CONFIG_COMPONENT_FEATURE_MAP = {
# ... existing configuration ...
'RUST_NEW_COMPONENT': {
'feature': 'enable-new',
'dependency': 'em_component_new',
'description': 'Enable new component integration'
}
}
```
5. **Update Registry**
```toml
# Add to component_registry/Cargo.toml
[dependencies]
em_component_new = { path = "../new_component", optional = true }
[features]
enable-new = ["em_component_new", "em_component_new/enable-new"]
```
```rust
// Register in component_registry/src/lib.rs
#[cfg(feature = "enable-new")]
use em_component_new;
fn component_registry_main() {
#[cfg(feature = "enable-new")]
{
println!("Component registry: New component enabled");
}
// ... other components ...
}
```
### Component Development Best Practices
1. **Use Conditional Compilation**: All functionality should be wrapped with `#[cfg(feature = "...")]`
2. **Avoid Direct Registration**: Don't use component registration macros directly in components, let `component_registry` manage uniformly
3. **Provide Command Interface**: Provide command-line interfaces for component functionality for easy testing and usage
4. **Complete Documentation**: Provide detailed documentation and usage examples for each component
## Troubleshooting
### Common Issues
1. **Compilation Failure**
- Check if Rust toolchain is correctly installed
- Confirm target architecture (e.g., `riscv64imac-unknown-none-elf`) is installed
- Check if dependency paths are correct
2. **Features Not Enabled**
- Confirm corresponding macro definitions are set in RT-Thread configuration
- Check mapping configuration in `feature_config_component.py`
- Review feature enablement information in build logs
3. **Linking Errors**
- Confirm all dependent components are correctly built
- Check dependency configuration in `Cargo.toml`
- Verify library file paths and names
## Dependency Relationships
```
RT-Thread Config
↓
Feature Mapping
↓
Rust Features
↓
Component Registry ← Individual Components
↓
RT-Thread System
```
|