quick-start.rst 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 Testing and Debian Unstable (Sid), outside of the freeze period, provide
  29. recent 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. Requirements: Building
  57. ----------------------
  58. This section explains how to fetch the tools needed for building.
  59. To easily check whether the requirements are met, the following target
  60. can be used::
  61. make LLVM=1 rustavailable
  62. This triggers the same logic used by Kconfig to determine whether
  63. ``RUST_IS_AVAILABLE`` should be enabled; but it also explains why not
  64. if that is the case.
  65. rustc
  66. *****
  67. A recent version of the Rust compiler is required.
  68. If ``rustup`` is being used, enter the kernel build directory (or use
  69. ``--path=<build-dir>`` argument to the ``set`` sub-command) and run,
  70. for instance::
  71. rustup override set stable
  72. This will configure your working directory to use the given version of
  73. ``rustc`` without affecting your default toolchain.
  74. Note that the override applies to the current working directory (and its
  75. sub-directories).
  76. If you are not using ``rustup``, fetch a standalone installer from:
  77. https://forge.rust-lang.org/infra/other-installation-methods.html#standalone
  78. Rust standard library source
  79. ****************************
  80. The Rust standard library source is required because the build system will
  81. cross-compile ``core``.
  82. If ``rustup`` is being used, run::
  83. rustup component add rust-src
  84. The components are installed per toolchain, thus upgrading the Rust compiler
  85. version later on requires re-adding the component.
  86. Otherwise, if a standalone installer is used, the Rust source tree may be
  87. downloaded into the toolchain's installation folder::
  88. curl -L "https://static.rust-lang.org/dist/rust-src-$(rustc --version | cut -d' ' -f2).tar.gz" |
  89. tar -xzf - -C "$(rustc --print sysroot)/lib" \
  90. "rust-src-$(rustc --version | cut -d' ' -f2)/rust-src/lib/" \
  91. --strip-components=3
  92. In this case, upgrading the Rust compiler version later on requires manually
  93. updating the source tree (this can be done by removing ``$(rustc --print
  94. sysroot)/lib/rustlib/src/rust`` then rerunning the above command).
  95. libclang
  96. ********
  97. ``libclang`` (part of LLVM) is used by ``bindgen`` to understand the C code
  98. in the kernel, which means LLVM needs to be installed; like when the kernel
  99. is compiled with ``LLVM=1``.
  100. Linux distributions are likely to have a suitable one available, so it is
  101. best to check that first.
  102. There are also some binaries for several systems and architectures uploaded at:
  103. https://releases.llvm.org/download.html
  104. Otherwise, building LLVM takes quite a while, but it is not a complex process:
  105. https://llvm.org/docs/GettingStarted.html#getting-the-source-code-and-building-llvm
  106. Please see Documentation/kbuild/llvm.rst for more information and further ways
  107. to fetch pre-built releases and distribution packages.
  108. bindgen
  109. *******
  110. The bindings to the C side of the kernel are generated at build time using
  111. the ``bindgen`` tool.
  112. Install it, for instance, via (note that this will download and build the tool
  113. from source)::
  114. cargo install --locked bindgen-cli
  115. ``bindgen`` uses the ``clang-sys`` crate to find a suitable ``libclang`` (which
  116. may be linked statically, dynamically or loaded at runtime). By default, the
  117. ``cargo`` command above will produce a ``bindgen`` binary that will load
  118. ``libclang`` at runtime. If it is not found (or a different ``libclang`` than
  119. the one found should be used), the process can be tweaked, e.g. by using the
  120. ``LIBCLANG_PATH`` environment variable. For details, please see ``clang-sys``'s
  121. documentation at:
  122. https://github.com/KyleMayes/clang-sys#linking
  123. https://github.com/KyleMayes/clang-sys#environment-variables
  124. Requirements: Developing
  125. ------------------------
  126. This section explains how to fetch the tools needed for developing. That is,
  127. they are not needed when just building the kernel.
  128. rustfmt
  129. *******
  130. The ``rustfmt`` tool is used to automatically format all the Rust kernel code,
  131. including the generated C bindings (for details, please see
  132. coding-guidelines.rst).
  133. If ``rustup`` is being used, its ``default`` profile already installs the tool,
  134. thus nothing needs to be done. If another profile is being used, the component
  135. can be installed manually::
  136. rustup component add rustfmt
  137. The standalone installers also come with ``rustfmt``.
  138. clippy
  139. ******
  140. ``clippy`` is a Rust linter. Running it provides extra warnings for Rust code.
  141. It can be run by passing ``CLIPPY=1`` to ``make`` (for details, please see
  142. general-information.rst).
  143. If ``rustup`` is being used, its ``default`` profile already installs the tool,
  144. thus nothing needs to be done. If another profile is being used, the component
  145. can be installed manually::
  146. rustup component add clippy
  147. The standalone installers also come with ``clippy``.
  148. rustdoc
  149. *******
  150. ``rustdoc`` is the documentation tool for Rust. It generates pretty HTML
  151. documentation for Rust code (for details, please see
  152. general-information.rst).
  153. ``rustdoc`` is also used to test the examples provided in documented Rust code
  154. (called doctests or documentation tests). The ``rusttest`` Make target uses
  155. this feature.
  156. If ``rustup`` is being used, all the profiles already install the tool,
  157. thus nothing needs to be done.
  158. The standalone installers also come with ``rustdoc``.
  159. rust-analyzer
  160. *************
  161. The `rust-analyzer <https://rust-analyzer.github.io/>`_ language server can
  162. be used with many editors to enable syntax highlighting, completion, go to
  163. definition, and other features.
  164. ``rust-analyzer`` needs a configuration file, ``rust-project.json``, which
  165. can be generated by the ``rust-analyzer`` Make target::
  166. make LLVM=1 rust-analyzer
  167. Configuration
  168. -------------
  169. ``Rust support`` (``CONFIG_RUST``) needs to be enabled in the ``General setup``
  170. menu. The option is only shown if a suitable Rust toolchain is found (see
  171. above), as long as the other requirements are met. In turn, this will make
  172. visible the rest of options that depend on Rust.
  173. Afterwards, go to::
  174. Kernel hacking
  175. -> Sample kernel code
  176. -> Rust samples
  177. And enable some sample modules either as built-in or as loadable.
  178. Building
  179. --------
  180. Building a kernel with a complete LLVM toolchain is the best supported setup
  181. at the moment. That is::
  182. make LLVM=1
  183. Using GCC also works for some configurations, but it is very experimental at
  184. the moment.
  185. Hacking
  186. -------
  187. To dive deeper, take a look at the source code of the samples
  188. at ``samples/rust/``, the Rust support code under ``rust/`` and
  189. the ``Rust hacking`` menu under ``Kernel hacking``.
  190. If GDB/Binutils is used and Rust symbols are not getting demangled, the reason
  191. is the toolchain does not support Rust's new v0 mangling scheme yet.
  192. There are a few ways out:
  193. - Install a newer release (GDB >= 10.2, Binutils >= 2.36).
  194. - Some versions of GDB (e.g. vanilla GDB 10.1) are able to use
  195. the pre-demangled names embedded in the debug info (``CONFIG_DEBUG_INFO``).