集成与编译

思路

  • 通过 cargo 编译 staticlib ,再与XWOS进行链接;
  • 由于XWOS支持ARM/PowerPC/RISC-V,因此需要通过 --target= 选项来进行多平台的交叉编译。

方案

  • XWOS启动一个独立线程,调用RUST的主函数,由此进入RUST的世界;
  • RUST的主函数与中间件是一个独立编译的玄武模块;
  • 由于RUST的编译依赖工具 cargo ,可设计一个新的玄武模块编译规则调用 cargo build
  • RUST玄武模块以 staticlib 形式进行编译,最终与XWOS进行静态链接;
  • 因为会使用一些 unstable 的特性,所以需安装nightly的RUST。

编译方案

  • 应将 cargo build 目标设定为 PHONY ,即可每次 make 时都调用 cargo build , 由 cargo 来管理 rust 源码的编译;
  • 环境变量 $(RUST_TARGET)cpu.mk 中定义;
  • 其他编译选项,由 .cargo/configbuild.rs 定义;
  • cargo build 编译结束后,再将输出的.a文件拷贝到输出目录;

安装rust工具链

  • 根据官方指导安装Rust;
  • 切换镜像源
    • Ubuntu配置文件路径: ~/.cargo/config
    • Windows配置文件路进: C:\Users\用户名\.cargo\config
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'sjtu'

[source.ustc]
registry = "http://mirrors.ustc.edu.cn/crates.io-index"

[source.sjtu]
registry = "http://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"
  • Ubuntu安装unstable版本
rustup install nightly
rustup +nightly target add thumbv8m.main-none-eabihf
rustup +nightly target add thumbv8m.main-none-eabi
rustup +nightly target add thumbv8m.base-none-eabi
rustup +nightly target add thumbv7m-none-eabi
rustup +nightly target add thumbv7em-none-eabihf
rustup +nightly target add thumbv7em-none-eabi
rustup +nightly target add thumbv6m-none-eabi
rustup +nightly target add riscv32imac-unknown-none-elf
rustup +nightly component add rust-src
  • Windows安装unstable版本

注意:Windows只支持使用 gnu 版的工具链

rustup install nightly-x86_64-pc-windows-gnu
rustup +nightly-x86_64-pc-windows-gnu target add thumbv8m.main-none-eabihf
rustup +nightly-x86_64-pc-windows-gnu target add thumbv8m.main-none-eabi
rustup +nightly-x86_64-pc-windows-gnu target add thumbv8m.base-none-eabi
rustup +nightly-x86_64-pc-windows-gnu target add thumbv7m-none-eabi
rustup +nightly-x86_64-pc-windows-gnu target add thumbv7em-none-eabihf
rustup +nightly-x86_64-pc-windows-gnu target add thumbv7em-none-eabi
rustup +nightly-x86_64-pc-windows-gnu target add thumbv6m-none-eabi
rustup +nightly-x86_64-pc-windows-gnu target add riscv32imac-unknown-none-elf
rustup +nightly-x86_64-pc-windows-gnu component add rust-src

RUST中间件模板

  • 目录结构:
XWOS
└── xwmd
    └── rust
        └── xwos
            ├── Cargo.lock
            ├── Cargo.toml
            ├── .cargo
            │   └── config
            ├── src
            │   └── lib.rs
            └── xwmo.mk
  • Cargo.toml
[package]
name = "xwos"
version = "1.0.0"
edition = "2021"

[lib]
name = "xwos"
crate-type = ["staticlib"]

[dependencies]
  • .cargo/config.toml
[unstable]
build-std = ["core", "alloc", "std", "panic_abort"]
  • src/lib.rs
#![feature(restricted_std)]

#[no_mangle]
pub unsafe extern "C" fn xwrust_main() {
}
  • xwmo.mk
include $(XWOS_WKSPC_DIR)/XWOS.cfg
include xwbs/functions.mk
include xwbs/xwmo.rust.mk
  • 编译规则 xwbs/xwmo.rust.mk