loongarch*-unknown-linux-*

Tier: 2 (带有主机工具)

LoongArch Linux 目标平台。LoongArch 是由龙芯中科技术股份有限公司开发的 RISC ISA。

目标平台描述
loongarch64-unknown-linux-gnuLoongArch64 Linux,LP64D ABI (内核 5.19,glibc 2.36)
loongarch64-unknown-linux-muslLoongArch64 Linux,LP64D ABI (内核 5.19,musl 1.2.5)

这些目标平台支持本机构建和交叉构建,并完全支持 std

参考资料

目标平台维护者

要求

操作系统版本

最低支持的 Linux 版本为 5.19。

一些 Linux 发行版,尤其是商业发行版,可能会为其 LoongArch 移植提供版本号低于 5.19 的分支 Linux 内核。这些内核可能仍然会被修补以兼容上游 Linux 5.19 UAPI,从而支持本文档中描述的目标平台,但这并非总是如此。rustup 安装程序包含对此项的检查,如果检测到不兼容性,将会中止安装。

主机工具链

这些目标平台需要在主机上安装相当新的 LoongArch 工具链。目前,Rust CI 使用以下组件来构建目标平台,这些版本可以被视为最低要求

  • GNU Binutils 2.40
  • GCC 13.x
  • glibc 2.36
  • linux-headers 5.19

其中,glibc 和 linux-headers 是各自支持主线 LoongArch 的最早版本,因此不可能使用这些的旧版本。旧版本的 Binutils 和 GCC 也无法工作,因为它们缺乏对较新的 LoongArch ELF 重定位类型以及其他功能的支持。

最新的 LLVM/Clang 工具链可能能够构建这些目标平台,但目前尚未进行积极测试。

构建

这些目标平台通过 rustup 分发,除此之外,不需要特殊的配置。

但是,如果由于某些原因你需要构建自己的 Rust,则可以在 config.toml 中简单地启用这些目标平台。例如

[build]
target = ["loongarch64-unknown-linux-gnu"]

确保 LoongArch 工具链二进制文件可以从 $PATH 访问。或者,你可以在 config.toml 中显式配置路径

[target.loongarch64-unknown-linux-gnu]
# Adjust the paths to point at your toolchain
# Suppose the toolchain is placed at /TOOLCHAIN_PATH, and the cross prefix is
# "loongarch64-unknown-linux-gnu-":
cc = "/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-gcc"
cxx = "/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-g++"
ar = "/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-ar"
ranlib = "/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-ranlib"
linker = "/TOOLCHAIN_PATH/bin/loongarch64-unknown-linux-gnu-gcc"

交叉编译

此目标平台可以在 x86_64-unknown-linux-gnu 主机上进行交叉编译。其他主机也可能可以工作,但未进行积极测试。

借助 QEMU linux-user 模拟,你可以直接在主机上测试交叉构建。下面给出一个示例

# Suppose the cross toolchain is placed at $TOOLCHAIN_PATH, with a cross prefix
# of "loongarch64-unknown-linux-gnu-".
export CC_loongarch64_unknown_linux_gnu="$TOOLCHAIN_PATH"/bin/loongarch64-unknown-linux-gnu-gcc
export CXX_loongarch64_unknown_linux_gnu="$TOOLCHAIN_PATH"/bin/loongarch64-unknown-linux-gnu-g++
export AR_loongarch64_unknown_linux_gnu="$TOOLCHAIN_PATH"/bin/loongarch64-unknown-linux-gnu-gcc-ar
export CARGO_TARGET_LOONGARCH64_UNKNOWN_LINUX_GNU_LINKER="$TOOLCHAIN_PATH"/bin/loongarch64-unknown-linux-gnu-gcc

# Point qemu-loongarch64 to the LoongArch sysroot.
# Suppose the sysroot is located at "sysroot" below the toolchain root:
export CARGO_TARGET_LOONGARCH64_UNKNOWN_LINUX_GNU_RUNNER="qemu-loongarch64 -L $TOOLCHAIN_PATH/sysroot"
# Or alternatively, if binfmt_misc is set up for running LoongArch binaries
# transparently:
export QEMU_LD_PREFIX="$TOOLCHAIN_PATH"/sysroot

cargo run --target loongarch64-unknown-linux-gnu --release

测试

测试和运行这些目标平台没有特殊要求。对于在主机上测试交叉构建,请参考上面的“交叉编译”部分。

构建 Rust 程序

由于这些目标平台通过 rustup 提供,因此为这些目标平台构建 Rust 程序非常容易:与其他架构相同。请注意,你需要 LoongArch C/C++ 工具链进行链接,或者如果你想将 C 代码与 Rust 一起编译(例如,对于具有 C 依赖项的 Rust crate)。

rustup target add loongarch64-unknown-linux-gnu
cargo build --target loongarch64-unknown-linux-gnu

通过 rustup 预构建工件的可用性如下:

  • loongarch64-unknown-linux-gnu:自 Rust 1.71 起;
  • loongarch64-unknown-linux-musl:自 Rust 1.81 起。