← Documents Documentation/rust/quick-start.rst GitHub 원문 ↗

Linux 6.18.37 · Rust

Rust 커널 개발 빠른 시작

배포판별 toolchain 설치부터 요구사항 확인, CONFIG_RUST 설정, LLVM 빌드와 소스 탐색까지 안내합니다.

Source pathDocumentation/rust/quick-start.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약·해설

quick-start.rst:1-366

배포판별 toolchain 설치부터 요구사항 확인, CONFIG_RUST 설정, LLVM 빌드와 소스 탐색까지 안내합니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 .. SPDX-License-Identifier: GPL-2.0
2
3 Quick Start
4 ===========
5
6 This document describes how to get started with kernel development in Rust.
7
8 There are a few ways to install a Rust toolchain needed for kernel development.
9 A simple way is to use the packages from your Linux distribution if they are
10 suitable -- the first section below explains this approach. An advantage of this
11 approach is that, typically, the distribution will match the LLVM used by Rust
12 and Clang.
13
14 Another way is using the prebuilt stable versions of LLVM+Rust provided on
15 `kernel.org <https://kernel.org/pub/tools/llvm/rust/>`_. These are the same slim
16 and fast LLVM toolchains from :ref:`Getting LLVM <getting_llvm>` with versions
17 of Rust added to them that Rust for Linux supports. Two sets are provided: the
18 "latest LLVM" and "matching LLVM" (please see the link for more information).
19
20 Alternatively, the next two "Requirements" sections explain each component and
21 how to install them through ``rustup``, the standalone installers from Rust
22 and/or building them.
23
24 The rest of the document explains other aspects on how to get started.
25
26
27 Distributions
28 -------------
29
30 Arch Linux
31 **********
32
33 Arch Linux provides recent Rust releases and thus it should generally work out
34 of the box, e.g.::
35
36 pacman -S rust rust-src rust-bindgen
37
38
39 Debian
40 ******
41
42 Debian Testing and Debian Unstable (Sid), outside of the freeze period, provide
43 recent Rust releases and thus they should generally work out of the box, e.g.::
44
45 apt install rustc rust-src bindgen rustfmt rust-clippy
46
47
48 Fedora Linux
49 ************
50
51 Fedora Linux provides recent Rust releases and thus it should generally work out
52 of the box, e.g.::
53
54 dnf install rust rust-src bindgen-cli rustfmt clippy
55
56
57 Gentoo Linux
58 ************
59
60 Gentoo Linux (and especially the testing branch) provides recent Rust releases
61 and thus it should generally work out of the box, e.g.::
62
63 USE='rust-src rustfmt clippy' emerge dev-lang/rust dev-util/bindgen
64
65 ``LIBCLANG_PATH`` may need to be set.
66
67
68 Nix
69 ***
70
71 Nix (unstable channel) provides recent Rust releases and thus it should
72 generally work out of the box, e.g.::
73
74 { pkgs ? import <nixpkgs> {} }:
75 pkgs.mkShell {
76 nativeBuildInputs = with pkgs; [ rustc rust-bindgen rustfmt clippy ];
77 RUST_LIB_SRC = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
78 }
79
80
81 openSUSE
82 ********
83
84 openSUSE Slowroll and openSUSE Tumbleweed provide recent Rust releases and thus
85 they should generally work out of the box, e.g.::
86
87 zypper install rust rust1.79-src rust-bindgen clang
88
89
90 Ubuntu
91 ******
92
93 25.04
94 ~~~~~
95
96 The latest Ubuntu releases provide recent Rust releases and thus they should
97 generally work out of the box, e.g.::
98
99 apt install rustc rust-src bindgen rustfmt rust-clippy
100
101 In addition, ``RUST_LIB_SRC`` needs to be set, e.g.::
102
103 RUST_LIB_SRC=/usr/src/rustc-$(rustc --version | cut -d' ' -f2)/library
104
105 For convenience, ``RUST_LIB_SRC`` can be exported to the global environment.
106
107
108 24.04 LTS and older
109 ~~~~~~~~~~~~~~~~~~~
110
111 Though Ubuntu 24.04 LTS and older versions still provide recent Rust
112 releases, they require some additional configuration to be set, using
113 the versioned packages, e.g.::
114
115 apt install rustc-1.80 rust-1.80-src bindgen-0.65 rustfmt-1.80 \
116 rust-1.80-clippy
117 ln -s /usr/lib/rust-1.80/bin/rustfmt /usr/bin/rustfmt-1.80
118 ln -s /usr/lib/rust-1.80/bin/clippy-driver /usr/bin/clippy-driver-1.80
119
120 None of these packages set their tools as defaults; therefore they should be
121 specified explicitly, e.g.::
122
123 make LLVM=1 RUSTC=rustc-1.80 RUSTDOC=rustdoc-1.80 RUSTFMT=rustfmt-1.80 \
124 CLIPPY_DRIVER=clippy-driver-1.80 BINDGEN=bindgen-0.65
125
126 Alternatively, modify the ``PATH`` variable to place the Rust 1.80 binaries
127 first and set ``bindgen`` as the default, e.g.::
128
129 PATH=/usr/lib/rust-1.80/bin:$PATH
130 update-alternatives --install /usr/bin/bindgen bindgen \
131 /usr/bin/bindgen-0.65 100
132 update-alternatives --set bindgen /usr/bin/bindgen-0.65
133
134 ``RUST_LIB_SRC`` needs to be set when using the versioned packages, e.g.::
135
136 RUST_LIB_SRC=/usr/src/rustc-$(rustc-1.80 --version | cut -d' ' -f2)/library
137
138 For convenience, ``RUST_LIB_SRC`` can be exported to the global environment.
139
140 In addition, ``bindgen-0.65`` is available in newer releases (24.04 LTS and
141 24.10), but it may not be available in older ones (20.04 LTS and 22.04 LTS),
142 thus ``bindgen`` may need to be built manually (please see below).
143
144
145 Requirements: Building
146 ----------------------
147
148 This section explains how to fetch the tools needed for building.
149
150 To easily check whether the requirements are met, the following target
151 can be used::
152
153 make LLVM=1 rustavailable
154
155 This triggers the same logic used by Kconfig to determine whether
156 ``RUST_IS_AVAILABLE`` should be enabled; but it also explains why not
157 if that is the case.
158
159
160 rustc
161 *****
162
163 A recent version of the Rust compiler is required.
164
165 If ``rustup`` is being used, enter the kernel build directory (or use
166 ``--path=<build-dir>`` argument to the ``set`` sub-command) and run,
167 for instance::
168
169 rustup override set stable
170
171 This will configure your working directory to use the given version of
172 ``rustc`` without affecting your default toolchain.
173
174 Note that the override applies to the current working directory (and its
175 sub-directories).
176
177 If you are not using ``rustup``, fetch a standalone installer from:
178
179 https://forge.rust-lang.org/infra/other-installation-methods.html#standalone
180
181
182 Rust standard library source
183 ****************************
184
185 The Rust standard library source is required because the build system will
186 cross-compile ``core``.
187
188 If ``rustup`` is being used, run::
189
190 rustup component add rust-src
191
192 The components are installed per toolchain, thus upgrading the Rust compiler
193 version later on requires re-adding the component.
194
195 Otherwise, if a standalone installer is used, the Rust source tree may be
196 downloaded into the toolchain's installation folder::
197
198 curl -L "https://static.rust-lang.org/dist/rust-src-$(rustc --version | cut -d' ' -f2).tar.gz" |
199 tar -xzf - -C "$(rustc --print sysroot)/lib" \
200 "rust-src-$(rustc --version | cut -d' ' -f2)/rust-src/lib/" \
201 --strip-components=3
202
203 In this case, upgrading the Rust compiler version later on requires manually
204 updating the source tree (this can be done by removing ``$(rustc --print
205 sysroot)/lib/rustlib/src/rust`` then rerunning the above command).
206
207
208 libclang
209 ********
210
211 ``libclang`` (part of LLVM) is used by ``bindgen`` to understand the C code
212 in the kernel, which means LLVM needs to be installed; like when the kernel
213 is compiled with ``LLVM=1``.
214
215 Linux distributions are likely to have a suitable one available, so it is
216 best to check that first.
217
218 There are also some binaries for several systems and architectures uploaded at:
219
220 https://releases.llvm.org/download.html
221
222 Otherwise, building LLVM takes quite a while, but it is not a complex process:
223
224 https://llvm.org/docs/GettingStarted.html#getting-the-source-code-and-building-llvm
225
226 Please see Documentation/kbuild/llvm.rst for more information and further ways
227 to fetch pre-built releases and distribution packages.
228
229
230 bindgen
231 *******
232
233 The bindings to the C side of the kernel are generated at build time using
234 the ``bindgen`` tool.
235
236 Install it, for instance, via (note that this will download and build the tool
237 from source)::
238
239 cargo install --locked bindgen-cli
240
241 ``bindgen`` uses the ``clang-sys`` crate to find a suitable ``libclang`` (which
242 may be linked statically, dynamically or loaded at runtime). By default, the
243 ``cargo`` command above will produce a ``bindgen`` binary that will load
244 ``libclang`` at runtime. If it is not found (or a different ``libclang`` than
245 the one found should be used), the process can be tweaked, e.g. by using the
246 ``LIBCLANG_PATH`` environment variable. For details, please see ``clang-sys``'s
247 documentation at:
248
249 https://github.com/KyleMayes/clang-sys#linking
250
251 https://github.com/KyleMayes/clang-sys#environment-variables
252
253
254 Requirements: Developing
255 ------------------------
256
257 This section explains how to fetch the tools needed for developing. That is,
258 they are not needed when just building the kernel.
259
260
261 rustfmt
262 *******
263
264 The ``rustfmt`` tool is used to automatically format all the Rust kernel code,
265 including the generated C bindings (for details, please see
266 coding-guidelines.rst).
267
268 If ``rustup`` is being used, its ``default`` profile already installs the tool,
269 thus nothing needs to be done. If another profile is being used, the component
270 can be installed manually::
271
272 rustup component add rustfmt
273
274 The standalone installers also come with ``rustfmt``.
275
276
277 clippy
278 ******
279
280 ``clippy`` is a Rust linter. Running it provides extra warnings for Rust code.
281 It can be run by passing ``CLIPPY=1`` to ``make`` (for details, please see
282 general-information.rst).
283
284 If ``rustup`` is being used, its ``default`` profile already installs the tool,
285 thus nothing needs to be done. If another profile is being used, the component
286 can be installed manually::
287
288 rustup component add clippy
289
290 The standalone installers also come with ``clippy``.
291
292
293 rustdoc
294 *******
295
296 ``rustdoc`` is the documentation tool for Rust. It generates pretty HTML
297 documentation for Rust code (for details, please see
298 general-information.rst).
299
300 ``rustdoc`` is also used to test the examples provided in documented Rust code
301 (called doctests or documentation tests). The ``rusttest`` Make target uses
302 this feature.
303
304 If ``rustup`` is being used, all the profiles already install the tool,
305 thus nothing needs to be done.
306
307 The standalone installers also come with ``rustdoc``.
308
309
310 rust-analyzer
311 *************
312
313 The `rust-analyzer <https://rust-analyzer.github.io/>`_ language server can
314 be used with many editors to enable syntax highlighting, completion, go to
315 definition, and other features.
316
317 ``rust-analyzer`` needs a configuration file, ``rust-project.json``, which
318 can be generated by the ``rust-analyzer`` Make target::
319
320 make LLVM=1 rust-analyzer
321
322
323 Configuration
324 -------------
325
326 ``Rust support`` (``CONFIG_RUST``) needs to be enabled in the ``General setup``
327 menu. The option is only shown if a suitable Rust toolchain is found (see
328 above), as long as the other requirements are met. In turn, this will make
329 visible the rest of options that depend on Rust.
330
331 Afterwards, go to::
332
333 Kernel hacking
334 -> Sample kernel code
335 -> Rust samples
336
337 And enable some sample modules either as built-in or as loadable.
338
339
340 Building
341 --------
342
343 Building a kernel with a complete LLVM toolchain is the best supported setup
344 at the moment. That is::
345
346 make LLVM=1
347
348 Using GCC also works for some configurations, but it is very experimental at
349 the moment.
350
351
352 Hacking
353 -------
354
355 To dive deeper, take a look at the source code of the samples
356 at ``samples/rust/``, the Rust support code under ``rust/`` and
357 the ``Rust hacking`` menu under ``Kernel hacking``.
358
359 If GDB/Binutils is used and Rust symbols are not getting demangled, the reason
360 is the toolchain does not support Rust's new v0 mangling scheme yet.
361 There are a few ways out:
362
363 - Install a newer release (GDB >= 10.2, Binutils >= 2.36).
364
365 - Some versions of GDB (e.g. vanilla GDB 10.1) are able to use
366 the pre-demangled names embedded in the debug info (``CONFIG_DEBUG_INFO``).
367

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

Toolchain 선택

1-26

이 문서는 Rust로 커널 개발을 시작하는 절차를 설명한다. 가장 간단한 방법은 적합한 Linux 배포판 package를 쓰는 것이며, 배포판이 Rust가 사용하는 LLVM과 Clang 버전을 함께 맞춰 주는 장점이 있다.

다른 방법으로 `kernel.org/pub/tools/llvm/rust/`의 prebuilt stable LLVM+Rust를 사용할 수 있다. 이는 커널이 제공하는 작고 빠른 LLVM toolchain에 Rust for Linux가 지원하는 Rust 버전을 더한 것으로, 최신 LLVM 조합과 버전이 맞는 LLVM 조합 두 세트를 제공한다.

배포판이나 prebuilt 조합을 쓰지 않으면 뒤의 Requirements 절에 따라 `rustup`, Rust standalone installer 또는 직접 빌드로 각 구성요소를 설치한다. 나머지 절은 설정, 빌드와 실제 소스 탐색을 안내한다.

Toolchain 선택
배포판 package 확인적합하면 distro Rust/LLVM 사용아니면 kernel.org prebuilt LLVM+Rust아니면 rustup/standalone/direct buildrustavailable로 검증

환경에 맞는 가장 짧은 설치 경로를 고릅니다.

.. SPDX-License-Identifier: GPL-2.0

Quick Start
===========

This document describes how to get started with kernel development in Rust.

There are a few ways to install a Rust toolchain needed for kernel development.
A simple way is to use the packages from your Linux distribution if they are
suitable -- the first section below explains this approach. An advantage of this
approach is that, typically, the distribution will match the LLVM used by Rust
and Clang.

Another way is using the prebuilt stable versions of LLVM+Rust provided on
`kernel.org <https://kernel.org/pub/tools/llvm/rust/>`_. These are the same slim
and fast LLVM toolchains from :ref:`Getting LLVM <getting_llvm>` with versions
of Rust added to them that Rust for Linux supports. Two sets are provided: the
"latest LLVM" and "matching LLVM" (please see the link for more information).

Alternatively, the next two "Requirements" sections explain each component and
how to install them through ``rustup``, the standalone installers from Rust
and/or building them.

The rest of the document explains other aspects on how to get started.

배포판별 설치

27-144

Arch Linux, Debian Testing·Unstable, Fedora, Gentoo testing, Nix unstable, openSUSE Slowroll·Tumbleweed와 최신 Ubuntu는 대체로 충분히 새 Rust를 제공한다. 각 배포판의 package manager로 compiler, source, bindgen, rustfmt와 Clippy를 설치한다.

Gentoo에서는 `rust-src rustfmt clippy` USE flag를 사용하며 환경에 따라 `LIBCLANG_PATH`가 필요하다. Nix shell은 `rustc`, `rust-bindgen`, `rustfmt`, `clippy`를 native build input으로 넣고 `RUST_LIB_SRC`를 stable toolchain source 경로로 지정한다.

Ubuntu 25.04에서는 package 설치 뒤 `RUST_LIB_SRC=/usr/src/rustc-$(rustc --version | cut -d' ' -f2)/library`를 설정한다. 자주 쓴다면 전역 환경에 export할 수 있다.

Ubuntu 24.04 LTS와 그 이전은 `rustc-1.80`, `rust-1.80-src`, `bindgen-0.65`, `rustfmt-1.80`, `rust-1.80-clippy`처럼 versioned package를 설치한다. 기본 도구가 되지 않으므로 Make 변수로 각 실행 파일을 명시하거나 Rust 1.80 경로를 `PATH` 앞에 두고 `update-alternatives`로 bindgen을 선택한다.

Versioned Ubuntu package에서도 `RUST_LIB_SRC`를 해당 rustc source 경로로 지정한다. `bindgen-0.65`는 24.04와 24.10에는 있지만 20.04와 22.04에는 없을 수 있으므로 오래된 release에서는 아래 절에 따라 직접 빌드해야 할 수 있다.

배포판 설치 핵심
배포판주요 package/설정
Archrust, rust-src, rust-bindgen
Debian Testing/Sidrustc, rust-src, bindgen, rustfmt, rust-clippy
Fedorarust, rust-src, bindgen-cli, rustfmt, clippy
GentooUSE='rust-src rustfmt clippy', 필요 시 LIBCLANG_PATH
Nix unstablemkShell과 RUST_LIB_SRC
openSUSErust, rust1.79-src, rust-bindgen, clang
Ubuntu 25.04일반 package와 RUST_LIB_SRC
Ubuntu 24.04 이하versioned package, 명시적 Make 변수 또는 PATH

원문의 명령을 유지하면서 package 이름과 추가 설정을 비교합니다.

Distributions
-------------

Arch Linux
**********

Arch Linux provides recent Rust releases and thus it should generally work out
of the box, e.g.::

        pacman -S rust rust-src rust-bindgen


Debian
******

Debian Testing and Debian Unstable (Sid), outside of the freeze period, provide
recent Rust releases and thus they should generally work out of the box, e.g.::

        apt install rustc rust-src bindgen rustfmt rust-clippy


Fedora Linux
************

Fedora Linux provides recent Rust releases and thus it should generally work out
of the box, e.g.::

        dnf install rust rust-src bindgen-cli rustfmt clippy


Gentoo Linux
************

Gentoo Linux (and especially the testing branch) provides recent Rust releases
and thus it should generally work out of the box, e.g.::

        USE='rust-src rustfmt clippy' emerge dev-lang/rust dev-util/bindgen

``LIBCLANG_PATH`` may need to be set.


Nix
***

Nix (unstable channel) provides recent Rust releases and thus it should
generally work out of the box, e.g.::

        { pkgs ? import <nixpkgs> {} }:
        pkgs.mkShell {
          nativeBuildInputs = with pkgs; [ rustc rust-bindgen rustfmt clippy ];
          RUST_LIB_SRC = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
        }


openSUSE
********

openSUSE Slowroll and openSUSE Tumbleweed provide recent Rust releases and thus
they should generally work out of the box, e.g.::

        zypper install rust rust1.79-src rust-bindgen clang


Ubuntu
******

25.04
~~~~~

The latest Ubuntu releases provide recent Rust releases and thus they should
generally work out of the box, e.g.::

        apt install rustc rust-src bindgen rustfmt rust-clippy

In addition, ``RUST_LIB_SRC`` needs to be set, e.g.::

        RUST_LIB_SRC=/usr/src/rustc-$(rustc --version | cut -d' ' -f2)/library

For convenience, ``RUST_LIB_SRC`` can be exported to the global environment.


24.04 LTS and older
~~~~~~~~~~~~~~~~~~~

Though Ubuntu 24.04 LTS and older versions still provide recent Rust
releases, they require some additional configuration to be set, using
the versioned packages, e.g.::

        apt install rustc-1.80 rust-1.80-src bindgen-0.65 rustfmt-1.80 \
                rust-1.80-clippy
        ln -s /usr/lib/rust-1.80/bin/rustfmt /usr/bin/rustfmt-1.80
        ln -s /usr/lib/rust-1.80/bin/clippy-driver /usr/bin/clippy-driver-1.80

None of these packages set their tools as defaults; therefore they should be
specified explicitly, e.g.::

        make LLVM=1 RUSTC=rustc-1.80 RUSTDOC=rustdoc-1.80 RUSTFMT=rustfmt-1.80 \
                CLIPPY_DRIVER=clippy-driver-1.80 BINDGEN=bindgen-0.65

Alternatively, modify the ``PATH`` variable to place the Rust 1.80 binaries
first and set ``bindgen`` as the default, e.g.::

        PATH=/usr/lib/rust-1.80/bin:$PATH
        update-alternatives --install /usr/bin/bindgen bindgen \
                /usr/bin/bindgen-0.65 100
        update-alternatives --set bindgen /usr/bin/bindgen-0.65

``RUST_LIB_SRC`` needs to be set when using the versioned packages, e.g.::

        RUST_LIB_SRC=/usr/src/rustc-$(rustc-1.80 --version | cut -d' ' -f2)/library

For convenience, ``RUST_LIB_SRC`` can be exported to the global environment.

In addition, ``bindgen-0.65`` is available in newer releases (24.04 LTS and
24.10), but it may not be available in older ones (20.04 LTS and 22.04 LTS),
thus ``bindgen`` may need to be built manually (please see below).

빌드에 필요한 구성요소

145-253

빌드 요구사항은 `make LLVM=1 rustavailable`로 한 번에 확인한다. 이는 Kconfig가 `RUST_IS_AVAILABLE`을 정할 때와 같은 논리를 실행하고, 사용할 수 없다면 이유도 설명한다.

최근 `rustc`가 필요하다. `rustup` 사용자는 커널 build 디렉터리에서 `rustup override set stable`을 실행하거나 `set --path=<build-dir>`로 범위를 지정한다. Override는 현재 디렉터리와 하위 디렉터리에만 적용되어 기본 toolchain을 바꾸지 않는다. `rustup`을 쓰지 않으면 Rust standalone installer를 받을 수 있다.

빌드 시스템이 `core`를 cross-compile하므로 Rust standard library source가 필요하다. `rustup component add rust-src`로 설치하며 toolchain마다 별도이므로 compiler를 올린 뒤 다시 추가해야 한다. Standalone 설치에서는 compiler 버전에 맞는 `rust-src` archive를 sysroot 아래에 풀고, compiler upgrade 때 기존 source tree를 지운 뒤 다시 설치한다.

`bindgen`은 커널 C 코드를 이해하기 위해 LLVM의 `libclang`을 사용한다. 먼저 배포판 package를 확인하고, 필요하면 LLVM release binary를 받거나 직접 빌드한다. 다른 prebuilt 방법과 package 정보는 `Documentation/kbuild/llvm.rst`에 있다.

C 쪽 binding은 빌드 때 `bindgen`으로 생성한다. `cargo install --locked bindgen-cli`로 source에서 설치할 수 있다. 이 binary는 기본적으로 실행 시 `libclang`을 불러오며, 찾지 못하거나 다른 library를 써야 하면 `LIBCLANG_PATH` 등 `clang-sys`의 linking과 environment variable 설정을 사용한다.

필수 빌드 도구
구성요소역할대표 설치
rustcRust compilerrustup override 또는 standalone
rust-srccore cross-compile sourcerustup component add rust-src
libclangbindgen의 C parser배포판 LLVM 또는 prebuilt/build
bindgenC binding 생성cargo install --locked bindgen-cli

rustavailable가 확인하는 각 구성요소와 역할입니다.

Requirements: Building
----------------------

This section explains how to fetch the tools needed for building.

To easily check whether the requirements are met, the following target
can be used::

        make LLVM=1 rustavailable

This triggers the same logic used by Kconfig to determine whether
``RUST_IS_AVAILABLE`` should be enabled; but it also explains why not
if that is the case.


rustc
*****

A recent version of the Rust compiler is required.

If ``rustup`` is being used, enter the kernel build directory (or use
``--path=<build-dir>`` argument to the ``set`` sub-command) and run,
for instance::

        rustup override set stable

This will configure your working directory to use the given version of
``rustc`` without affecting your default toolchain.

Note that the override applies to the current working directory (and its
sub-directories).

If you are not using ``rustup``, fetch a standalone installer from:

        https://forge.rust-lang.org/infra/other-installation-methods.html#standalone


Rust standard library source
****************************

The Rust standard library source is required because the build system will
cross-compile ``core``.

If ``rustup`` is being used, run::

        rustup component add rust-src

The components are installed per toolchain, thus upgrading the Rust compiler
version later on requires re-adding the component.

Otherwise, if a standalone installer is used, the Rust source tree may be
downloaded into the toolchain's installation folder::

        curl -L "https://static.rust-lang.org/dist/rust-src-$(rustc --version | cut -d' ' -f2).tar.gz" |
                tar -xzf - -C "$(rustc --print sysroot)/lib" \
                "rust-src-$(rustc --version | cut -d' ' -f2)/rust-src/lib/" \
                --strip-components=3

In this case, upgrading the Rust compiler version later on requires manually
updating the source tree (this can be done by removing ``$(rustc --print
sysroot)/lib/rustlib/src/rust`` then rerunning the above command).


libclang
********

``libclang`` (part of LLVM) is used by ``bindgen`` to understand the C code
in the kernel, which means LLVM needs to be installed; like when the kernel
is compiled with ``LLVM=1``.

Linux distributions are likely to have a suitable one available, so it is
best to check that first.

There are also some binaries for several systems and architectures uploaded at:

        https://releases.llvm.org/download.html

Otherwise, building LLVM takes quite a while, but it is not a complex process:

        https://llvm.org/docs/GettingStarted.html#getting-the-source-code-and-building-llvm

Please see Documentation/kbuild/llvm.rst for more information and further ways
to fetch pre-built releases and distribution packages.


bindgen
*******

The bindings to the C side of the kernel are generated at build time using
the ``bindgen`` tool.

Install it, for instance, via (note that this will download and build the tool
from source)::

        cargo install --locked bindgen-cli

``bindgen`` uses the ``clang-sys`` crate to find a suitable ``libclang`` (which
may be linked statically, dynamically or loaded at runtime). By default, the
``cargo`` command above will produce a ``bindgen`` binary that will load
``libclang`` at runtime. If it is not found (or a different ``libclang`` than
the one found should be used), the process can be tweaked, e.g. by using the
``LIBCLANG_PATH`` environment variable. For details, please see ``clang-sys``'s
documentation at:

        https://github.com/KyleMayes/clang-sys#linking

        https://github.com/KyleMayes/clang-sys#environment-variables

개발용 도구

254-322

이 절의 도구는 커널을 단순히 빌드할 때는 필요하지 않고 개발할 때 사용한다. `rustfmt`는 생성된 C binding을 포함한 모든 커널 Rust 코드의 서식을 맞춘다. `rustup` default profile에는 들어 있으며 다른 profile에서는 `rustup component add rustfmt`로 추가한다. Standalone installer에도 포함된다.

Clippy는 Rust 코드에 추가 warning을 제공하는 linter다. Make에 `CLIPPY=1`을 넘겨 실행한다. `rustup` default profile에는 기본 설치되고 다른 profile에서는 `rustup component add clippy`를 사용하며 standalone installer에도 포함된다.

`rustdoc`은 Rust 코드의 HTML 문서를 만들고 문서 속 예제인 doctest도 검사한다. `rusttest` Make target이 이 기능을 사용한다. 모든 rustup profile과 standalone installer에 포함된다.

`rust-analyzer` language server는 여러 편집기에서 syntax highlight, completion, go to definition 등을 제공한다. 커널에 필요한 `rust-project.json`은 `make LLVM=1 rust-analyzer`로 생성한다.

개발 도구
도구용도설정
rustfmt커널 Rust와 생성 binding 서식rustup component add rustfmt
Clippy추가 lintCLIPPY=1 또는 component add clippy
rustdocHTML 문서와 doctestrusttest target에서 사용
rust-analyzerIDE language serverrust-analyzer target으로 project JSON 생성

빌드 필수 도구와 별도로 코드 품질과 편집 경험을 지원합니다.

Requirements: Developing
------------------------

This section explains how to fetch the tools needed for developing. That is,
they are not needed when just building the kernel.


rustfmt
*******

The ``rustfmt`` tool is used to automatically format all the Rust kernel code,
including the generated C bindings (for details, please see
coding-guidelines.rst).

If ``rustup`` is being used, its ``default`` profile already installs the tool,
thus nothing needs to be done. If another profile is being used, the component
can be installed manually::

        rustup component add rustfmt

The standalone installers also come with ``rustfmt``.


clippy
******

``clippy`` is a Rust linter. Running it provides extra warnings for Rust code.
It can be run by passing ``CLIPPY=1`` to ``make`` (for details, please see
general-information.rst).

If ``rustup`` is being used, its ``default`` profile already installs the tool,
thus nothing needs to be done. If another profile is being used, the component
can be installed manually::

        rustup component add clippy

The standalone installers also come with ``clippy``.


rustdoc
*******

``rustdoc`` is the documentation tool for Rust. It generates pretty HTML
documentation for Rust code (for details, please see
general-information.rst).

``rustdoc`` is also used to test the examples provided in documented Rust code
(called doctests or documentation tests). The ``rusttest`` Make target uses
this feature.

If ``rustup`` is being used, all the profiles already install the tool,
thus nothing needs to be done.

The standalone installers also come with ``rustdoc``.


rust-analyzer
*************

The `rust-analyzer <https://rust-analyzer.github.io/>`_ language server can
be used with many editors to enable syntax highlighting, completion, go to
definition, and other features.

``rust-analyzer`` needs a configuration file, ``rust-project.json``, which
can be generated by the ``rust-analyzer`` Make target::

        make LLVM=1 rust-analyzer

Kconfig에서 Rust와 sample 활성화

323-339

`General setup` 메뉴에서 `Rust support` 즉 `CONFIG_RUST`를 켠다. 알맞은 Rust toolchain이 발견되고 다른 요구사항도 충족될 때만 이 항목이 보이며, 활성화하면 Rust에 의존하는 나머지 option이 나타난다.

그 다음 `Kernel hacking -> Sample kernel code -> Rust samples`로 이동해 예제 module을 built-in 또는 loadable module로 선택한다.

Rust Kconfig 활성화
rustavailable 요구사항 충족General setupRust support (CONFIG_RUST)=yRust 의존 option 표시Kernel hackingRust samples 선택

도구 검증 결과가 설정 항목의 표시와 sample 선택으로 이어집니다.

Configuration
-------------

``Rust support`` (``CONFIG_RUST``) needs to be enabled in the ``General setup``
menu. The option is only shown if a suitable Rust toolchain is found (see
above), as long as the other requirements are met. In turn, this will make
visible the rest of options that depend on Rust.

Afterwards, go to::

        Kernel hacking
            -> Sample kernel code
                -> Rust samples

And enable some sample modules either as built-in or as loadable.

첫 커널 빌드

340-351

현재 가장 잘 지원되는 구성은 완전한 LLVM toolchain으로 커널을 빌드하는 것이다. 명령은 `make LLVM=1`이다. 일부 configuration에서는 GCC도 동작하지만 현재는 매우 실험적이다.

컴파일러 선택
조합상태
make LLVM=1가장 잘 지원됨
GCC 기반 일부 구성동작할 수 있으나 매우 실험적

첫 빌드에는 지원 수준이 가장 높은 조합을 사용합니다.

Building
--------

Building a kernel with a complete LLVM toolchain is the best supported setup
at the moment. That is::

        make LLVM=1

Using GCC also works for some configurations, but it is very experimental at
the moment.

소스 탐색과 symbol demangling

352-366

더 깊이 살펴보려면 `samples/rust/`의 sample, `rust/` 아래 지원 코드, `Kernel hacking`의 `Rust hacking` 메뉴부터 읽는다.

GDB 또는 Binutils에서 Rust symbol이 demangle되지 않는다면 toolchain이 새 v0 mangling scheme을 지원하지 않는 것이 원인일 수 있다. GDB 10.2 이상과 Binutils 2.36 이상으로 올리거나, 일부 GDB 버전에서는 `CONFIG_DEBUG_INFO`에 포함된 미리 demangle된 이름을 사용한다.

첫 코드 탐색
samples/rust/rust/ 지원 코드Rust hacking KconfigGDB로 symbol 확인필요하면 GDB/Binutils upgrade 또는 DEBUG_INFO 사용

실행 가능한 sample에서 추상화와 디버깅 정보로 범위를 넓힙니다.

Hacking
-------

To dive deeper, take a look at the source code of the samples
at ``samples/rust/``, the Rust support code under ``rust/`` and
the ``Rust hacking`` menu under ``Kernel hacking``.

If GDB/Binutils is used and Rust symbols are not getting demangled, the reason
is the toolchain does not support Rust's new v0 mangling scheme yet.
There are a few ways out:

- Install a newer release (GDB >= 10.2, Binutils >= 2.36).

- Some versions of GDB (e.g. vanilla GDB 10.1) are able to use
  the pre-demangled names embedded in the debug info (``CONFIG_DEBUG_INFO``).