| 1 | |
| 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 13 (Trixie), as well as Testing and Debian Unstable (Sid) provide recent
|
| 43 | 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 provides recent Rust releases and thus it should generally work out
|
| 61 | 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 provides recent Rust releases and thus it should generally work out of the
|
| 72 | 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 rust-src rust-bindgen clang
|
| 88 |
|
| 89 |
|
| 90 | Ubuntu
|
| 91 | ******
|
| 92 |
|
| 93 | Ubuntu 26.04 LTS provides recent Rust releases and thus it should
|
| 94 | generally work out of the box, e.g.::
|
| 95 |
|
| 96 | apt install rustc rust-src bindgen rustfmt rust-clippy
|
| 97 |
|
| 98 | In addition, ``RUST_LIB_SRC`` needs to be set, e.g.::
|
| 99 |
|
| 100 | RUST_LIB_SRC=/usr/src/rustc-$(rustc --version | cut -d' ' -f2)/library
|
| 101 |
|
| 102 | For convenience, ``RUST_LIB_SRC`` can be exported to the global environment.
|
| 103 |
|
| 104 |
|
| 105 | 24.04 LTS and older
|
| 106 | ~~~~~~~~~~~~~~~~~~~
|
| 107 |
|
| 108 | Though Ubuntu 24.04 LTS and older versions still provide recent Rust
|
| 109 | releases, they require some additional configuration to be set, using
|
| 110 | the versioned packages, e.g.::
|
| 111 |
|
| 112 | apt install rustc-1.85 rust-1.85-src bindgen-0.71 rustfmt-1.85 \
|
| 113 | rust-1.85-clippy
|
| 114 | ln -s /usr/lib/rust-1.85/bin/rustfmt /usr/bin/rustfmt-1.85
|
| 115 | ln -s /usr/lib/rust-1.85/bin/clippy-driver /usr/bin/clippy-driver-1.85
|
| 116 |
|
| 117 | None of these packages set their tools as defaults; therefore they should be
|
| 118 | specified explicitly, e.g.::
|
| 119 |
|
| 120 | make LLVM=1 RUSTC=rustc-1.85 RUSTDOC=rustdoc-1.85 RUSTFMT=rustfmt-1.85 \
|
| 121 | CLIPPY_DRIVER=clippy-driver-1.85 BINDGEN=bindgen-0.71
|
| 122 |
|
| 123 | Alternatively, modify the ``PATH`` variable to place the Rust 1.85 binaries
|
| 124 | first and set ``bindgen`` as the default, e.g.::
|
| 125 |
|
| 126 | PATH=/usr/lib/rust-1.85/bin:$PATH
|
| 127 | update-alternatives --install /usr/bin/bindgen bindgen \
|
| 128 | /usr/bin/bindgen-0.71 100
|
| 129 | update-alternatives --set bindgen /usr/bin/bindgen-0.71
|
| 130 |
|
| 131 | ``RUST_LIB_SRC`` may need to be set when using the versioned packages, e.g.::
|
| 132 |
|
| 133 | RUST_LIB_SRC=/usr/src/rustc-$(rustc-1.85 --version | cut -d' ' -f2)/library
|
| 134 |
|
| 135 | For convenience, ``RUST_LIB_SRC`` can be exported to the global environment.
|
| 136 |
|
| 137 | In addition, ``bindgen-0.71`` is available in newer releases (24.04 LTS),
|
| 138 | but it may not be available in older ones (20.04 LTS and 22.04 LTS),
|
| 139 | thus ``bindgen`` may need to be built manually (please see below).
|
| 140 |
|
| 141 |
|
| 142 | Requirements: Building
|
| 143 | ----------------------
|
| 144 |
|
| 145 | This section explains how to fetch the tools needed for building.
|
| 146 |
|
| 147 | To easily check whether the requirements are met, the following target
|
| 148 | can be used::
|
| 149 |
|
| 150 | make LLVM=1 rustavailable
|
| 151 |
|
| 152 | This triggers the same logic used by Kconfig to determine whether
|
| 153 | ``RUST_IS_AVAILABLE`` should be enabled; but it also explains why not
|
| 154 | if that is the case.
|
| 155 |
|
| 156 |
|
| 157 | rustc
|
| 158 | *****
|
| 159 |
|
| 160 | A recent version of the Rust compiler is required.
|
| 161 |
|
| 162 | If ``rustup`` is being used, enter the kernel build directory (or use
|
| 163 | ``--path=<build-dir>`` argument to the ``set`` sub-command) and run,
|
| 164 | for instance::
|
| 165 |
|
| 166 | rustup override set stable
|
| 167 |
|
| 168 | This will configure your working directory to use the given version of
|
| 169 | ``rustc`` without affecting your default toolchain.
|
| 170 |
|
| 171 | Note that the override applies to the current working directory (and its
|
| 172 | sub-directories).
|
| 173 |
|
| 174 | If you are not using ``rustup``, fetch a standalone installer from:
|
| 175 |
|
| 176 | https://forge.rust-lang.org/infra/other-installation-methods.html#standalone
|
| 177 |
|
| 178 |
|
| 179 | Rust standard library source
|
| 180 | ****************************
|
| 181 |
|
| 182 | The Rust standard library source is required because the build system will
|
| 183 | cross-compile ``core``.
|
| 184 |
|
| 185 | If ``rustup`` is being used, run::
|
| 186 |
|
| 187 | rustup component add rust-src
|
| 188 |
|
| 189 | The components are installed per toolchain, thus upgrading the Rust compiler
|
| 190 | version later on requires re-adding the component.
|
| 191 |
|
| 192 | Otherwise, if a standalone installer is used, the Rust source tree may be
|
| 193 | downloaded into the toolchain's installation folder::
|
| 194 |
|
| 195 | curl -L "https://static.rust-lang.org/dist/rust-src-$(rustc --version | cut -d' ' -f2).tar.gz" |
|
| 196 | tar -xzf - -C "$(rustc --print sysroot)/lib" \
|
| 197 | "rust-src-$(rustc --version | cut -d' ' -f2)/rust-src/lib/" \
|
| 198 | --strip-components=3
|
| 199 |
|
| 200 | In this case, upgrading the Rust compiler version later on requires manually
|
| 201 | updating the source tree (this can be done by removing ``$(rustc --print
|
| 202 | sysroot)/lib/rustlib/src/rust`` then rerunning the above command).
|
| 203 |
|
| 204 |
|
| 205 | libclang
|
| 206 | ********
|
| 207 |
|
| 208 | ``libclang`` (part of LLVM) is used by ``bindgen`` to understand the C code
|
| 209 | in the kernel, which means LLVM needs to be installed; like when the kernel
|
| 210 | is compiled with ``LLVM=1``.
|
| 211 |
|
| 212 | Linux distributions are likely to have a suitable one available, so it is
|
| 213 | best to check that first.
|
| 214 |
|
| 215 | There are also some binaries for several systems and architectures uploaded at:
|
| 216 |
|
| 217 | https://releases.llvm.org/download.html
|
| 218 |
|
| 219 | Otherwise, building LLVM takes quite a while, but it is not a complex process:
|
| 220 |
|
| 221 | https://llvm.org/docs/GettingStarted.html#getting-the-source-code-and-building-llvm
|
| 222 |
|
| 223 | Please see Documentation/kbuild/llvm.rst for more information and further ways
|
| 224 | to fetch pre-built releases and distribution packages.
|
| 225 |
|
| 226 |
|
| 227 | bindgen
|
| 228 | *******
|
| 229 |
|
| 230 | The bindings to the C side of the kernel are generated at build time using
|
| 231 | the ``bindgen`` tool.
|
| 232 |
|
| 233 | Install it, for instance, via (note that this will download and build the tool
|
| 234 | from source)::
|
| 235 |
|
| 236 | cargo install --locked bindgen-cli
|
| 237 |
|
| 238 | ``bindgen`` uses the ``clang-sys`` crate to find a suitable ``libclang`` (which
|
| 239 | may be linked statically, dynamically or loaded at runtime). By default, the
|
| 240 | ``cargo`` command above will produce a ``bindgen`` binary that will load
|
| 241 | ``libclang`` at runtime. If it is not found (or a different ``libclang`` than
|
| 242 | the one found should be used), the process can be tweaked, e.g. by using the
|
| 243 | ``LIBCLANG_PATH`` environment variable. For details, please see ``clang-sys``'s
|
| 244 | documentation at:
|
| 245 |
|
| 246 | https://github.com/KyleMayes/clang-sys#linking
|
| 247 |
|
| 248 | https://github.com/KyleMayes/clang-sys#environment-variables
|
| 249 |
|
| 250 |
|
| 251 | Requirements: Developing
|
| 252 | ------------------------
|
| 253 |
|
| 254 | This section explains how to fetch the tools needed for developing. That is,
|
| 255 | they are not needed when just building the kernel.
|
| 256 |
|
| 257 |
|
| 258 | rustfmt
|
| 259 | *******
|
| 260 |
|
| 261 | The ``rustfmt`` tool is used to automatically format all the Rust kernel code,
|
| 262 | including the generated C bindings (for details, please see
|
| 263 | coding-guidelines.rst).
|
| 264 |
|
| 265 | If ``rustup`` is being used, its ``default`` profile already installs the tool,
|
| 266 | thus nothing needs to be done. If another profile is being used, the component
|
| 267 | can be installed manually::
|
| 268 |
|
| 269 | rustup component add rustfmt
|
| 270 |
|
| 271 | The standalone installers also come with ``rustfmt``.
|
| 272 |
|
| 273 |
|
| 274 | clippy
|
| 275 | ******
|
| 276 |
|
| 277 | ``clippy`` is a Rust linter. Running it provides extra warnings for Rust code.
|
| 278 | It can be run by passing ``CLIPPY=1`` to ``make`` (for details, please see
|
| 279 | general-information.rst).
|
| 280 |
|
| 281 | If ``rustup`` is being used, its ``default`` profile already installs the tool,
|
| 282 | thus nothing needs to be done. If another profile is being used, the component
|
| 283 | can be installed manually::
|
| 284 |
|
| 285 | rustup component add clippy
|
| 286 |
|
| 287 | The standalone installers also come with ``clippy``.
|
| 288 |
|
| 289 |
|
| 290 | rustdoc
|
| 291 | *******
|
| 292 |
|
| 293 | ``rustdoc`` is the documentation tool for Rust. It generates pretty HTML
|
| 294 | documentation for Rust code (for details, please see
|
| 295 | general-information.rst).
|
| 296 |
|
| 297 | ``rustdoc`` is also used to test the examples provided in documented Rust code
|
| 298 | (called doctests or documentation tests). The ``rusttest`` Make target uses
|
| 299 | this feature.
|
| 300 |
|
| 301 | If ``rustup`` is being used, all the profiles already install the tool,
|
| 302 | thus nothing needs to be done.
|
| 303 |
|
| 304 | The standalone installers also come with ``rustdoc``.
|
| 305 |
|
| 306 |
|
| 307 | rust-analyzer
|
| 308 | *************
|
| 309 |
|
| 310 | The `rust-analyzer <https://rust-analyzer.github.io/>`_ language server can
|
| 311 | be used with many editors to enable syntax highlighting, completion, go to
|
| 312 | definition, and other features.
|
| 313 |
|
| 314 | ``rust-analyzer`` needs a configuration file, ``rust-project.json``, which
|
| 315 | can be generated by the ``rust-analyzer`` Make target::
|
| 316 |
|
| 317 | make LLVM=1 rust-analyzer
|
| 318 |
|
| 319 |
|
| 320 | Configuration
|
| 321 | -------------
|
| 322 |
|
| 323 | ``Rust support`` (``CONFIG_RUST``) needs to be enabled in the ``General setup``
|
| 324 | menu. The option is only shown if a suitable Rust toolchain is found (see
|
| 325 | above), as long as the other requirements are met. In turn, this will make
|
| 326 | visible the rest of options that depend on Rust.
|
| 327 |
|
| 328 | Afterwards, go to::
|
| 329 |
|
| 330 | Kernel hacking
|
| 331 | -> Sample kernel code
|
| 332 | -> Rust samples
|
| 333 |
|
| 334 | And enable some sample modules either as built-in or as loadable.
|
| 335 |
|
| 336 |
|
| 337 | Building
|
| 338 | --------
|
| 339 |
|
| 340 | Building a kernel with a complete LLVM toolchain is the best supported setup
|
| 341 | at the moment. That is::
|
| 342 |
|
| 343 | make LLVM=1
|
| 344 |
|
| 345 | Using GCC also works for some configurations, but it is very experimental at
|
| 346 | the moment.
|
| 347 |
|
| 348 |
|
| 349 | Hacking
|
| 350 | -------
|
| 351 |
|
| 352 | To dive deeper, take a look at the source code of the samples
|
| 353 | at ``samples/rust/``, the Rust support code under ``rust/`` and
|
| 354 | the ``Rust hacking`` menu under ``Kernel hacking``.
|