开发环境搭建
概述
Rust SDK 的编译依赖两套工具链:
工具链 |
来源 |
用途 |
|---|---|---|
Rust 工具链 |
Rust 官方 |
编译 Rust 代码、链接 ELF 固件 |
ARM 裸机工具链 |
Realtek |
提供 |
安装 Rust 工具链
Linux / WSL:
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain none --profile minimal
$ source ~/.cargo/env
备注
中国大陆用户可设置以下链接后再安装:
$ export RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup
$ export RUSTUP_UPDATE_ROOT=https://mirrors.tuna.tsinghua.edu.cn/rustup/rustup
Windows:
安装 MSYS2:
winget install MSYS2.MSYS2
打开开始菜单中的 MSYS2 MSYS,执行:
$ pacman -Syu
$ pacman -S --needed mingw-w64-x86_64-gcc
如果未设置环境变量,需要打开系统属性 → 高级 → 环境变量,在用户 Path 中新增 C:\msys64\mingw64\bin。
安装 rustup,在 PowerShell 中执行:
Invoke-WebRequest https://win.rustup.rs/x86_64 -OutFile "$env:TEMP\rustup-init.exe"
& "$env:TEMP\rustup-init.exe" -y --default-host x86_64-pc-windows-gnu --default-toolchain none --profile minimal
安装完成后重开一个新的 PowerShell 窗口。
备注
中国大陆用户可设置以下链接后再安装:
$env:RUSTUP_DIST_SERVER = "https://mirrors.tuna.tsinghua.edu.cn/rustup"
$env:RUSTUP_UPDATE_ROOT = "https://mirrors.tuna.tsinghua.edu.cn/rustup/rustup"
Windows 上也可将上述两项加入系统环境变量,永久生效。
macOS:
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain none --profile minimal
$ source ~/.cargo/env
备注
中国大陆用户可设置以下链接后再安装:
$ export RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup
$ export RUSTUP_UPDATE_ROOT=https://mirrors.tuna.tsinghua.edu.cn/rustup/rustup
安装 nightly 工具链
rust-toolchain.toml 在仓库根目录中定义了所需的 nightly 版本、组件及交叉编译 target,进入仓库目录后首次运行 rustc 时 rustup 会自动完成安装。
进入 Rust SDK 根目录执行:
Linux / WSL:
$ cd <workdir>/rust
$ rustc --version
Windows:
cd <workdir>/rust
rustc --version
macOS:
$ cd <workdir>/rust
$ rustc --version
首次执行会自动下载安装(约需数分钟)。
安装 ARM 裸机工具链
安装 ARM 工具链请参考:
验证安装
Linux / WSL:
$ rustup show active-toolchain
nightly-x86_64-unknown-linux-gnu (overridden by '.../rust/rust-toolchain.toml')
$ rustup target list --installed | grep thumbv8m.main-none-eabi
thumbv8m.main-none-eabi
thumbv8m.main-none-eabihf
$ arm-none-eabi-nm --version
GNU nm (Realtek ASDK-12.x.x ...) 2.xx.x
Windows:
> rustup show active-toolchain
nightly-x86_64-pc-windows-gnu (overridden by '.../rust/rust-toolchain.toml')
> rustup target list --installed | findstr thumbv8m
thumbv8m.main-none-eabi
thumbv8m.main-none-eabihf
> where.exe x86_64-w64-mingw32-gcc
C:\msys64\mingw64\bin\x86_64-w64-mingw32-gcc.exe
> arm-none-eabi-nm --version
GNU nm (Realtek ASDK-12.x.x ...) 2.xx.x
macOS:
$ rustup show active-toolchain
nightly-aarch64-apple-darwin (overridden by '.../rust/rust-toolchain.toml')
$ rustup target list --installed | grep thumbv8m.main-none-eabi
thumbv8m.main-none-eabi
thumbv8m.main-none-eabihf
$ arm-none-eabi-nm --version
GNU nm (Realtek ASDK-12.x.x ...) 2.xx.x