# `Loongnix`上编译安装Rust 在`Loongarch`架构上基于`rustc-1.85-stable`版本编译安装`rustc-1.86-Nightly`版本。 ## 编译安装 ### 安装`rustc-1.85-stable`版本 1. 在龙芯社区下载1.85版本 ``` wget https://ftp.loongnix.cn/toolchain/rust/rust-1.85/2025-02-24/abi1.0/rust-1.85.0-loongarch64-unknown-linux-gnu.tar.xz ``` 2. 解压并安装 ``` mkdir ~/rust_1.85 tar -xvf rust-1.85.0-loongarch64-unknown-linux-gnu.tar.xz -C ~/rust_1.85 ## 在解压目录下执行install.sh安装 chmod u+x install.sh ./install.sh 无报错表示成功 ``` 3. 验证`rustc`的版本 ``` 默认会安装在/usr/local/bin下,执行--version验证版本 rustc --version --verbose (venv) loongson@loongson-pc:~/rust/1.85$ /usr/local/bin/rustc --version --verbose rustc 1.85.0 (51753f45f 2025-02-24) binary: rustc commit-hash: 51753f45fa686f44e1e7c88fe74be79d95282077 commit-date: 2025-02-24 host: loongarch64-unknown-linux-gnu release: 1.85.0 LLVM version: 18.1.7 ``` 4. 验证cargo版本 ``` 默认会安装在/usr/local/bin下,执行--version验证版本 (venv) loongson@loongson-pc:~/rust/1.85$ /usr/local/bin/cargo --version --verbose cargo 1.85.0 (83d2e3910 2025-02-21) release: 1.85.0 commit-hash: 83d2e391040558e2d1b6a2f67f9f14ce43d64c66 commit-date: 2025-02-21 host: loongarch64-unknown-linux-gnu libgit2: 1.8.1 (sys:0.19.0 vendored) libcurl: 8.9.0-DEV (sys:0.4.74+curl-8.9.0 vendored ssl:OpenSSL/1.1.1w) ssl: OpenSSL 1.1.1w 11 Sep 2023 os: Linux 20.0.0 (DaoXiangHu) [64-bit] ``` 5. 添加stable版本的相关库文件 ``` wget https://rust-lang.loongnix.cn/dist/1.85.0/2025-02-24/rust-std-1.85.0-loongarch64-unknown-linux-gnu.tar.xz ## 找到 sysroot 路径 SYSROOT=$(/usr/local/bin/rustc --print sysroot) echo $SYSROOT ##复制标准库到 sysroot # 解压后的目录结构通常是 rust-std-xxx/rust-std-loongarch64-unknown-linux-gnu/lib/rustlib/ # 需要把里面的内容复制到 $SYSROOT/lib/rustlib/ sudo cp -r rust-std-*/rust-std-loongarch64-unknown-linux-gnu/lib/rustlib/loongarch64-unknown-linux-gnu $SYSROOT/lib/rustlib/ ``` ### 安装`rustc-1.86-Nightly`版本 1. 在龙芯社区下载rustc-1.86的源码包 ``` wget https://rust-lang.loongnix.cn/dist/1.86.0/2025-04-09/rust-src-1.86.0.tar.xz ## 解压 tar -xvf rust-src-1.86.0.tar.xz -C /home/loongson/rust/1.86/ cd /home/loongson/rust/1.86/rustc-1.86.0-src ``` 2. 编辑`config.toml`配置文件(没有该文件的话可以直接创建) ``` touch config,toml ## 添加相应内容 (venv) loongson@loongson-pc:~/rust/1.86/rustc-1.86.0-src$ cat config.toml # Use different pre-set defaults than the global defaults. # # See `src/bootstrap/defaults` for more information. # Note that this has no default value (x.py uses the defaults in `config.example.toml`). profile = 'dist' change-id = 136941 [llvm] #llvm-config = "/usr/local/llvm-20/bin/llvm-config" download-ci-llvm = false link-shared = false [build] jobs = 0 # Instead of downloading the src/stage0 version of Cargo specified, use # this Cargo binary instead to build all Rust code # If you set this, you likely want to set `rustc` as well. cargo = '/usr/local/bin/cargo' # Instead of downloading the src/stage0 version of the compiler # specified, use this rustc binary instead as the stage0 snapshot compiler. # If you set this, you likely want to set `cargo` as well. rustc = '/usr/local/bin/rustc' # Arguments passed to the `./configure` script, used during distcheck. You # probably won't fill this in but rather it's filled in by the `./configure` # script. Useful for debugging. [install] [rust] channel = "nightly" lto = "off" [target.loongarch64-unknown-linux-gnu] llvm-config = "/usr/local/llvm-20/bin/llvm-config" [dist] ``` - **因为是编译安装Nightly版本,一定要添加channel = “nightly”.** - 关于其他配置或者配置说明,可直接问AI 3. 直接编译 ``` ## 修改配置文件之后,在解压目录下执行安装 python3.9 setup.py build --verbose ``` - 此过程耗时较长,可能会遇到问题,基础依赖问题可以问AI,直接处理。 4. 验证编译版本 ``` 无报错的话及编译完成,随后会在build/stage2*相关目录下生成对应的二进制文件。 loongson@loongson-pc:~/rust/1.86/rustc-1.86.0-src$ ls -l build/loongarch64-unknown-linux-gnu/stage2/bin/ 总用量 16 -rwxr-xr-x 3 loongson loongson 21000 4月 7 03:28 rustc loongson@loongson-pc:~/rust/1.86/rustc-1.86.0-src$ ls -l build/loongarch64-unknown-linux-gnu/stage2-tools-bin/ 总用量 124884 -rwxr-xr-x 3 loongson loongson 34890680 4月 7 03:45 cargo -rwxr-xr-x 3 loongson loongson 1731160 4月 3 08:53 cargo-clippy -rwxr-xr-x 3 loongson loongson 1712104 4月 3 09:07 cargo-miri -rwxr-xr-x 3 loongson loongson 17052896 4月 3 08:53 clippy-driver -rwxr-xr-x 3 loongson loongson 12996064 4月 3 09:05 miri -rwxr-xr-x 3 loongson loongson 732664 4月 3 08:16 rls -rwxr-xr-x 3 loongson loongson 48955848 4月 3 08:34 rust-analyzer -rwxr-xr-x 3 loongson loongson 1818720 4月 3 08:36 rust-analyzer-proc-macro-srv -rwxr-xr-x 3 loongson loongson 8012928 4月 3 08:58 rustfmt loongson@loongson-pc:~/rust/1.86/rustc-1.86.0-src$ ## rustc的版本 loongson@loongson-pc:~/rust/1.86/rustc-1.86.0-src$ ./build/loongarch64-unknown-linux-gnu/stage2/bin/rustc --version --verbose rustc 1.86.0-nightly (5e25f8045 2025-04-09) (built from a source tarball) binary: rustc commit-hash: 5e25f804539f9bea182946a5914494bbdfd56998 commit-date: 2025-04-09 host: loongarch64-unknown-linux-gnu release: 1.86.0-nightly LLVM version: 20.1.8 loongson@loongson-pc:~/rust/1.86/rustc-1.86.0-src$ ## cargo的版本 loongson@loongson-pc:~/rust/1.86/rustc-1.86.0-src$ ./build/loongarch64-unknown-linux-gnu/stage2-tools-bin/cargo --version --verbose cargo 1.86.0-nightly (37587bcee 2025-04-09) release: 1.86.0-nightly commit-hash: 37587bceef35e93078508f9d00ab8c9d2004ecc1 commit-date: 2025-04-09 host: loongarch64-unknown-linux-gnu libgit2: 1.9.0 (sys:0.20.0 vendored) libcurl: 8.12.0-DEV (sys:0.4.79+curl-8.12.0 vendored ssl:OpenSSL/1.1.1d) os: Linux 20.0.0 (DaoXiangHu) [64-bit] loongson@loongson-pc:~/rust/1.86/rustc-1.86.0-src$ ``` 5. 设置环境变量 ``` export PATH="$HOME/rust/1.86/rustc-1.86.0-src/build/loongarch64-unknown-linux-gnu/stage2/bin:$HOME/rust/1.86/rustc-1.86.0-src/build/loongarch64-unknown-linux-gnu/stage2-tools-bin:$PATH" ``` 至此编译安装完成。。。。。