1.[XWOS-RUST]集成与编译

思路

  • 通过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;
  • 切换镜像源,编辑~/.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版本
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
[unstable]
build-std = ["core", "alloc", "std", "panic_abort"]
  • src/lib.rs
#![feature(restricted_std)]

#[no_mangle]
pub unsafe extern "C" fn rust_main() {
}
  • xwmo.mk
include $(XuanWuOS_WKSPC_DIR)/XuanWuOS.cfg
include $(XWBS_UTIL_MK_XWMO)
include xwbs/xwmo.rust.mk
  • 编译规则xwbs/xwmo.rust.mk
include $(XuanWuOS_ARCH_DIR)/arch.mk
include $(XuanWuOS_CPU_DIR)/cpu.mk
include $(XuanWuOS_SOC_DIR)/soc.mk
include $(XuanWuOS_BRD_DIR)/brd.mk
include $(XuanWuOS_XWOS_DIR)/xwos.mk
include $(XuanWuOS_BRD_DIR)/lib.mk
include $(XWBS_UTIL_MK_XWMO)
include xwbs/$(XuanWuOS_CFG_ARCH).$(XuanWuOS_CFG_COMPILER).rule

XWMO_NAME := $(call getXwmoName)
XWMO_DIR := $(call getXwmoDir)
XWMO_OBJ_DIR ?= $(XWMO_DIR)
XWMO_RUSTLIB_NAME := lib$(notdir $(XWMO_DIR)).a

ifeq ($(XuanWuOS_CFG_HOSTOS),windows-nt)
  RUST_TOOLCHAIN := +nightly-x86_64-pc-windows-gnu
else
  RUST_TOOLCHAIN := +nightly
endif

ifeq ($(~D),0)
  CARGO_BUILD_FLAGS += --release
  XWMO_RUSTLIB := $(XWMO_DIR)/target/$(RUST_TARGET)/release/$(XWMO_RUSTLIB_NAME)
else
  XWMO_RUSTLIB := $(XWMO_DIR)/target/$(RUST_TARGET)/debug/$(XWMO_RUSTLIB_NAME)
endif

$(XuanWuOS_OBJ_DIR)/$(XWMO_OBJ_DIR)/$(XWMO_NAME): $(XWMO_RUSTLIB) $(XuanWuOS_OBJ_DIR)/$(XWMO_OBJ_DIR)
	$(SHOW_CP) $(CP) $< $@

$(XuanWuOS_OBJ_DIR)/$(XWMO_OBJ_DIR):
	@[ ! -d $@ ] && mkdir -p $@ || true

$(XWMO_RUSTLIB):
	cd $(XWMO_DIR); cargo $(RUST_TOOLCHAIN) build $(CARGO_BUILD_FLAGS) --target=$(RUST_TARGET)

clean:
	@$(RM) -f $(XuanWuOS_OBJ_DIR)/$(XWMO_OBJ_DIR)/$(XWMO_NAME)
	@cd $(XWMO_DIR); cargo $(RUST_TOOLCHAIN) clean

distclean:
	$(RM) -rf $(XuanWuOS_OBJ_DIR)/$(XWMO_OBJ_DIR)
	@cd $(XWMO_DIR); cargo $(RUST_TOOLCHAIN) clean

.PHONY: dsm clean distclean $(XWMO_RUSTLIB)

你可能感兴趣的:(XWOS-RUST,XWOS,rust,XWOS,xwrust)