| 1 |
.. SPDX-License-Identifier: GPL-2.0 |
| 2 |
.. include:: ../disclaimer-zh_CN.rst |
| 3 |
|
| 4 |
:Original: Documentation/rust/quick-start.rst |
| 5 |
|
| 6 |
:翻译: |
| 7 |
|
| 8 |
司延腾 Yanteng Si <[email protected]> |
| 9 |
|
| 10 |
|
| 11 |
快速入门 |
| 12 |
======== |
| 13 |
|
| 14 |
本文介绍了如何开始使用Rust进行内核开发。 |
| 15 |
|
| 16 |
安装内核开发所需的 Rust 工具链有几种方式。一种简单的方式是使用 Linux 发行版的软件包 |
| 17 |
(如果它们合适的话)——下面的第一节解释了这种方法。这种方法的一个优势是,通常发行版会 |
| 18 |
匹配 Rust 和 Clang 所使用的 LLVM。 |
| 19 |
|
| 20 |
另一种方式是使用 `kernel.org <https://kernel.org/pub/tools/llvm/rust/>`_ 上提 |
| 21 |
供的预构建稳定版本的 LLVM+Rust。这些与 :ref:`获取 LLVM <zh_cn_getting_llvm>` 中的精 |
| 22 |
简快速 LLVM 工具链相同,并添加了 Rust for Linux 支持的 Rust 版本。提供了两套工具 |
| 23 |
链:"最新 LLVM" 和 "匹配 LLVM"(请参阅链接了解更多信息)。 |
| 24 |
|
| 25 |
或者,接下来的两个 "依赖" 章节将解释每个组件以及如何通过 ``rustup``、Rust 的独立 |
| 26 |
安装程序或从源码构建来安装它们。 |
| 27 |
|
| 28 |
本文档的其余部分解释了有关如何入门的其他方面。 |
| 29 |
|
| 30 |
|
| 31 |
发行版 |
| 32 |
------ |
| 33 |
|
| 34 |
Arch Linux |
| 35 |
********** |
| 36 |
|
| 37 |
Arch Linux 提供较新的 Rust 版本,因此通常开箱即用,例如:: |
| 38 |
|
| 39 |
pacman -S rust rust-src rust-bindgen |
| 40 |
|
| 41 |
|
| 42 |
Debian |
| 43 |
****** |
| 44 |
|
| 45 |
Debian 13(Trixie)以及 Testing 和 Debian Unstable(Sid)提供较新的 Rust 版 |
| 46 |
本,因此通常开箱即用,例如:: |
| 47 |
|
| 48 |
apt install rustc rust-src bindgen rustfmt rust-clippy |
| 49 |
|
| 50 |
|
| 51 |
Fedora Linux |
| 52 |
************ |
| 53 |
|
| 54 |
Fedora Linux 提供较新的 Rust 版本,因此通常开箱即用,例如:: |
| 55 |
|
| 56 |
dnf install rust rust-src bindgen-cli rustfmt clippy |
| 57 |
|
| 58 |
|
| 59 |
Gentoo Linux |
| 60 |
************ |
| 61 |
|
| 62 |
Gentoo Linux(尤其是 testing 分支)提供较新的 Rust 版本,因此通常开箱即用, |
| 63 |
例如:: |
| 64 |
|
| 65 |
USE='rust-src rustfmt clippy' emerge dev-lang/rust dev-util/bindgen |
| 66 |
|
| 67 |
可能需要设置 ``LIBCLANG_PATH``。 |
| 68 |
|
| 69 |
|
| 70 |
Nix |
| 71 |
*** |
| 72 |
|
| 73 |
Nix(unstable 频道)提供较新的 Rust 版本,因此通常开箱即用,例如:: |
| 74 |
|
| 75 |
{ pkgs ? import <nixpkgs> {} }: |
| 76 |
pkgs.mkShell { |
| 77 |
nativeBuildInputs = with pkgs; [ rustc rust-bindgen rustfmt clippy ]; |
| 78 |
RUST_LIB_SRC = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
openSUSE |
| 83 |
******** |
| 84 |
|
| 85 |
openSUSE Slowroll 和 openSUSE Tumbleweed 提供较新的 Rust 版本,因此通常开箱 |
| 86 |
即用,例如:: |
| 87 |
|
| 88 |
zypper install rust rust1.79-src rust-bindgen clang |
| 89 |
|
| 90 |
|
| 91 |
Ubuntu |
| 92 |
****** |
| 93 |
|
| 94 |
25.04 |
| 95 |
~~~~~ |
| 96 |
|
| 97 |
最新的 Ubuntu 版本提供较新的 Rust 版本,因此通常开箱即用,例如:: |
| 98 |
|
| 99 |
apt install rustc rust-src bindgen rustfmt rust-clippy |
| 100 |
|
| 101 |
此外,需要设置 ``RUST_LIB_SRC``,例如:: |
| 102 |
|
| 103 |
RUST_LIB_SRC=/usr/src/rustc-$(rustc --version | cut -d' ' -f2)/library |
| 104 |
|
| 105 |
为方便起见,可以将 ``RUST_LIB_SRC`` 导出到全局环境中。 |
| 106 |
|
| 107 |
|
| 108 |
24.04 LTS 及更早版本 |
| 109 |
~~~~~~~~~~~~~~~~~~~~ |
| 110 |
|
| 111 |
虽然 Ubuntu 24.04 LTS 及更早版本仍然提供较新的 Rust 版本,但它们需要一些额外的配 |
| 112 |
置,使用带版本号的软件包,例如:: |
| 113 |
|
| 114 |
apt install rustc-1.80 rust-1.80-src bindgen-0.65 rustfmt-1.80 \ |
| 115 |
rust-1.80-clippy |
| 116 |
ln -s /usr/lib/rust-1.80/bin/rustfmt /usr/bin/rustfmt-1.80 |
| 117 |
ln -s /usr/lib/rust-1.80/bin/clippy-driver /usr/bin/clippy-driver-1.80 |
| 118 |
|
| 119 |
这些软件包都不会将其工具设置为默认值;因此应该显式指定它们,例如:: |
| 120 |
|
| 121 |
make LLVM=1 RUSTC=rustc-1.80 RUSTDOC=rustdoc-1.80 RUSTFMT=rustfmt-1.80 \ |
| 122 |
CLIPPY_DRIVER=clippy-driver-1.80 BINDGEN=bindgen-0.65 |
| 123 |
|
| 124 |
或者,修改 ``PATH`` 变量将 Rust 1.80 的二进制文件放在前面,并将 ``bindgen`` 设 |
| 125 |
置为默认值,例如:: |
| 126 |
|
| 127 |
PATH=/usr/lib/rust-1.80/bin:$PATH |
| 128 |
update-alternatives --install /usr/bin/bindgen bindgen \ |
| 129 |
/usr/bin/bindgen-0.65 100 |
| 130 |
update-alternatives --set bindgen /usr/bin/bindgen-0.65 |
| 131 |
|
| 132 |
使用带版本号的软件包时需要设置 ``RUST_LIB_SRC``,例如:: |
| 133 |
|
| 134 |
RUST_LIB_SRC=/usr/src/rustc-$(rustc-1.80 --version | cut -d' ' -f2)/library |
| 135 |
|
| 136 |
为方便起见,可以将 ``RUST_LIB_SRC`` 导出到全局环境中。 |
| 137 |
|
| 138 |
此外, ``bindgen-0.65`` 在较新的版本(24.04 LTS 和 24.10)中可用,但在更早的版 |
| 139 |
本(20.04 LTS 和 22.04 LTS)中可能不可用,因此可能需要手动构建 ``bindgen`` |
| 140 |
(请参见下文)。 |
| 141 |
|
| 142 |
|
| 143 |
构建依赖 |
| 144 |
-------- |
| 145 |
|
| 146 |
本节描述了如何获取构建所需的工具。 |
| 147 |
|
| 148 |
为了方便检查是否满足要求,可以使用以下目标:: |
| 149 |
|
| 150 |
make LLVM=1 rustavailable |
| 151 |
|
| 152 |
这会触发与Kconfig用来确定是否应该启用 ``RUST_IS_AVAILABLE`` 相同的逻辑;不过,如 |
| 153 |
果Kconfig认为不该启用,它会列出未满足的条件。 |
| 154 |
|
| 155 |
|
| 156 |
rustc |
| 157 |
***** |
| 158 |
|
| 159 |
需要一个较新版本的Rust编译器。 |
| 160 |
|
| 161 |
如果使用的是 ``rustup`` ,请进入内核编译目录(或者用 ``--path=<build-dir>`` 参数 |
| 162 |
来 ``设置`` sub-command),例如运行:: |
| 163 |
|
| 164 |
rustup override set stable |
| 165 |
|
| 166 |
这将配置你的工作目录使用给定版本的 ``rustc``,而不影响你的默认工具链。 |
| 167 |
|
| 168 |
请注意覆盖应用当前的工作目录(和它的子目录)。 |
| 169 |
|
| 170 |
如果你使用 ``rustup``, 可以从下面的链接拉取一个单独的安装程序: |
| 171 |
|
| 172 |
https://forge.rust-lang.org/infra/other-installation-methods.html#standalone |
| 173 |
|
| 174 |
|
| 175 |
Rust标准库源代码 |
| 176 |
**************** |
| 177 |
|
| 178 |
Rust标准库的源代码是必需的,因为构建系统会交叉编译 ``core`` 。 |
| 179 |
|
| 180 |
如果正在使用 ``rustup`` ,请运行:: |
| 181 |
|
| 182 |
rustup component add rust-src |
| 183 |
|
| 184 |
这些组件是按工具链安装的,因此以后升级Rust编译器版本需要重新添加组件。 |
| 185 |
|
| 186 |
否则,如果使用独立的安装程序,可以将Rust源码树下载到安装工具链的文件夹中:: |
| 187 |
|
| 188 |
curl -L "https://static.rust-lang.org/dist/rust-src-$(rustc --version | cut -d' ' -f2).tar.gz" | |
| 189 |
tar -xzf - -C "$(rustc --print sysroot)/lib" \ |
| 190 |
"rust-src-$(rustc --version | cut -d' ' -f2)/rust-src/lib/" \ |
| 191 |
--strip-components=3 |
| 192 |
|
| 193 |
在这种情况下,以后升级Rust编译器版本需要手动更新这个源代码树(这可以通过移除 |
| 194 |
``$(rustc --print sysroot)/lib/rustlib/src/rust`` ,然后重新执行上 |
| 195 |
面的命令做到)。 |
| 196 |
|
| 197 |
|
| 198 |
libclang |
| 199 |
******** |
| 200 |
|
| 201 |
``bindgen`` 使用 ``libclang`` (LLVM的一部分)来理解内核中的C代码,这意味着需要安 |
| 202 |
装LLVM;同在开启``LLVM=1`` 时编译内核一样。 |
| 203 |
|
| 204 |
Linux发行版中可能会有合适的包,所以最好先检查一下。 |
| 205 |
|
| 206 |
适用于部分系统和架构的二进制文件也可到以下网址下载: |
| 207 |
|
| 208 |
https://releases.llvm.org/download.html |
| 209 |
|
| 210 |
或者自行构建LLVM,这需要相当长的时间,但并不是一个复杂的过程: |
| 211 |
|
| 212 |
https://llvm.org/docs/GettingStarted.html#getting-the-source-code-and-building-llvm |
| 213 |
|
| 214 |
请参阅 Documentation/kbuild/llvm.rst 了解更多信息,以及获取预构建版本和发行包 |
| 215 |
的进一步方法。 |
| 216 |
|
| 217 |
|
| 218 |
bindgen |
| 219 |
******* |
| 220 |
|
| 221 |
内核的C端绑定是在构建时使用 ``bindgen`` 工具生成的。 |
| 222 |
|
| 223 |
例如,通过以下方式安装它(注意,这将从源码下载并构建该工具):: |
| 224 |
|
| 225 |
cargo install --locked bindgen-cli |
| 226 |
|
| 227 |
``bindgen`` 使用 ``clang-sys`` crate 来查找合适的 ``libclang`` (可以静态链 |
| 228 |
接、动态链接或在运行时加载)。默认情况下,上面的 ``cargo`` 命令会生成一个在运行时 |
| 229 |
加载 ``libclang`` 的 ``bindgen`` 二进制文件。如果没有找到(或者应该使用与找到的 |
| 230 |
不同的 ``libclang``),可以调整该过程,例如使用 ``LIBCLANG_PATH`` 环境变量。详 |
| 231 |
情请参阅 ``clang-sys`` 的文档: |
| 232 |
|
| 233 |
https://github.com/KyleMayes/clang-sys#linking |
| 234 |
|
| 235 |
https://github.com/KyleMayes/clang-sys#environment-variables |
| 236 |
|
| 237 |
|
| 238 |
开发依赖 |
| 239 |
-------- |
| 240 |
|
| 241 |
本节解释了如何获取开发所需的工具。也就是说,在构建内核时不需要这些工具。 |
| 242 |
|
| 243 |
|
| 244 |
rustfmt |
| 245 |
******* |
| 246 |
|
| 247 |
``rustfmt`` 工具被用来自动格式化所有的Rust内核代码,包括生成的C绑定(详情请见 |
| 248 |
coding-guidelines.rst )。 |
| 249 |
|
| 250 |
如果使用的是 ``rustup`` ,它的 ``默认`` 配置文件已经安装了这个工具,因此不需要做什么。 |
| 251 |
如果使用的是其他配置文件,可以手动安装该组件:: |
| 252 |
|
| 253 |
rustup component add rustfmt |
| 254 |
|
| 255 |
独立的安装程序也带有 ``rustfmt`` 。 |
| 256 |
|
| 257 |
|
| 258 |
clippy |
| 259 |
****** |
| 260 |
|
| 261 |
``clippy`` 是一个Rust linter。运行它可以为Rust代码提供额外的警告。它可以通过向 ``make`` |
| 262 |
传递 ``CLIPPY=1`` 来运行(关于细节,详见 general-information.rst )。 |
| 263 |
|
| 264 |
如果正在使用 ``rustup`` ,它的 ``默认`` 配置文件已经安装了这个工具,因此不需要做什么。 |
| 265 |
如果使用的是另一个配置文件,该组件可以被手动安装:: |
| 266 |
|
| 267 |
rustup component add clippy |
| 268 |
|
| 269 |
独立的安装程序也带有 ``clippy`` 。 |
| 270 |
|
| 271 |
|
| 272 |
rustdoc |
| 273 |
******* |
| 274 |
|
| 275 |
``rustdoc`` 是Rust的文档工具。它为Rust代码生成漂亮的HTML文档(详情请见 general-information.rst )。 |
| 276 |
|
| 277 |
``rustdoc`` 也被用来测试文档化的Rust代码中提供的例子(称为doctests或文档测试)。 |
| 278 |
``rusttest`` 是本功能的Make目标。 |
| 279 |
|
| 280 |
如果使用的是 ``rustup`` ,所有的配置文件都已经安装了这个工具,因此不需要做什么。 |
| 281 |
|
| 282 |
独立的安装程序也带有 ``rustdoc`` 。 |
| 283 |
|
| 284 |
|
| 285 |
rust-analyzer |
| 286 |
************* |
| 287 |
|
| 288 |
`rust-analyzer <https://rust-analyzer.github.io/>`_ 语言服务器可以和许多编辑器 |
| 289 |
一起使用,以实现语法高亮、补全、转到定义和其他功能。 |
| 290 |
|
| 291 |
``rust-analyzer`` 需要一个配置文件, ``rust-project.json``, 它可以由 ``rust-analyzer`` |
| 292 |
Make 目标生成:: |
| 293 |
|
| 294 |
make LLVM=1 rust-analyzer |
| 295 |
|
| 296 |
|
| 297 |
配置 |
| 298 |
---- |
| 299 |
|
| 300 |
Rust支持(CONFIG_RUST)需要在 ``General setup`` 菜单中启用。在其他要求得到满足的情 |
| 301 |
况下,该选项只有在找到合适的Rust工具链时才会显示(见上文)。相应的,这将使依赖Rust的其 |
| 302 |
他选项可见。 |
| 303 |
|
| 304 |
之后,进入:: |
| 305 |
|
| 306 |
Kernel hacking |
| 307 |
-> Sample kernel code |
| 308 |
-> Rust samples |
| 309 |
|
| 310 |
并启用一些内置或可加载的样例模块。 |
| 311 |
|
| 312 |
|
| 313 |
构建 |
| 314 |
---- |
| 315 |
|
| 316 |
用完整的LLVM工具链构建内核是目前支持的最佳设置。即:: |
| 317 |
|
| 318 |
make LLVM=1 |
| 319 |
|
| 320 |
使用GCC对某些配置也是可行的,但目前它是非常试验性的。 |
| 321 |
|
| 322 |
|
| 323 |
折腾 |
| 324 |
---- |
| 325 |
|
| 326 |
要想深入了解,请看 ``samples/rust/`` 下的样例源代码、 ``rust/`` 下的Rust支持代码和 |
| 327 |
``Kernel hacking`` 下的 ``Rust hacking`` 菜单。 |
| 328 |
|
| 329 |
如果使用的是GDB/Binutils,而Rust符号没有被demangled,原因是工具链还不支持Rust的新v0 |
| 330 |
mangling方案。有几个办法可以解决: |
| 331 |
|
| 332 |
- 安装一个较新的版本(GDB >= 10.2, Binutils >= 2.36)。 |
| 333 |
|
| 334 |
- 一些版本的GDB(例如vanilla GDB 10.1)能够使用嵌入在调试信息(``CONFIG_DEBUG_INFO``) |
| 335 |
中的pre-demangled的名字。 |