quick-start.rst 11 KB

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