Integration and Build

Concept

  • Compile as staticlib via cargo, then link with XWOS;
  • Since XWOS supports ARM/PowerPC/RISC-V, cross-compilation for multiple platforms is required via the --target= option.

Approach

  • XWOS starts an independent thread that calls Rust’s main function, thus entering the world of Rust;
  • Rust’s main function and middleware are an independently compiled XWOS module;
  • Since Rust compilation depends on the cargo tool, a new XWOS module build rule can be designed to invoke cargo build;
  • The Rust XWOS module is compiled as staticlib and ultimately statically linked with XWOS;
  • Since some unstable features are used, the nightly version of Rust must be installed.

Build Approach

  • The cargo build target should be set to PHONY, so that cargo build is invoked every time make is run, and cargo manages the compilation of rust source code;
  • The environment variable $(RUST_TARGET) is defined in cpu.mk;
  • Other build options are defined in .cargo/config or build.rs;
  • After cargo build completes, copy the output .a file to the output directory;

Installing the Rust Toolchain

  • Install Rust according to the official guide;
  • Switch mirror sources:
    • Ubuntu configuration file path: ~/.cargo/config
    • Windows configuration file path: C:\Users\username\.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: Install unstable version
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: Install unstable version

Note: Windows only supports using the gnu version of the toolchain

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 Middleware Template

  • Directory structure:
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
  • Build rule xwbs/xwmo.rust.mk