162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ciQuick Start
462306a36Sopenharmony_ci===========
562306a36Sopenharmony_ci
662306a36Sopenharmony_ciThis document describes how to get started with kernel development in Rust.
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci
962306a36Sopenharmony_ciRequirements: Building
1062306a36Sopenharmony_ci----------------------
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ciThis section explains how to fetch the tools needed for building.
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ciSome of these requirements might be available from Linux distributions
1562306a36Sopenharmony_ciunder names like ``rustc``, ``rust-src``, ``rust-bindgen``, etc. However,
1662306a36Sopenharmony_ciat the time of writing, they are likely not to be recent enough unless
1762306a36Sopenharmony_cithe distribution tracks the latest releases.
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ciTo easily check whether the requirements are met, the following target
2062306a36Sopenharmony_cican be used::
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci	make LLVM=1 rustavailable
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ciThis triggers the same logic used by Kconfig to determine whether
2562306a36Sopenharmony_ci``RUST_IS_AVAILABLE`` should be enabled; but it also explains why not
2662306a36Sopenharmony_ciif that is the case.
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_cirustc
3062306a36Sopenharmony_ci*****
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ciA particular version of the Rust compiler is required. Newer versions may or
3362306a36Sopenharmony_cimay not work because, for the moment, the kernel depends on some unstable
3462306a36Sopenharmony_ciRust features.
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciIf ``rustup`` is being used, enter the checked out source code directory
3762306a36Sopenharmony_ciand run::
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci	rustup override set $(scripts/min-tool-version.sh rustc)
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ciThis will configure your working directory to use the correct version of
4262306a36Sopenharmony_ci``rustc`` without affecting your default toolchain. If you are not using
4362306a36Sopenharmony_ci``rustup``, fetch a standalone installer from:
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci	https://forge.rust-lang.org/infra/other-installation-methods.html#standalone
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ciRust standard library source
4962306a36Sopenharmony_ci****************************
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ciThe Rust standard library source is required because the build system will
5262306a36Sopenharmony_cicross-compile ``core`` and ``alloc``.
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ciIf ``rustup`` is being used, run::
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	rustup component add rust-src
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ciThe components are installed per toolchain, thus upgrading the Rust compiler
5962306a36Sopenharmony_civersion later on requires re-adding the component.
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ciOtherwise, if a standalone installer is used, the Rust source tree may be
6262306a36Sopenharmony_cidownloaded into the toolchain's installation folder::
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	curl -L "https://static.rust-lang.org/dist/rust-src-$(scripts/min-tool-version.sh rustc).tar.gz" |
6562306a36Sopenharmony_ci		tar -xzf - -C "$(rustc --print sysroot)/lib" \
6662306a36Sopenharmony_ci		"rust-src-$(scripts/min-tool-version.sh rustc)/rust-src/lib/" \
6762306a36Sopenharmony_ci		--strip-components=3
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ciIn this case, upgrading the Rust compiler version later on requires manually
7062306a36Sopenharmony_ciupdating the source tree (this can be done by removing ``$(rustc --print
7162306a36Sopenharmony_cisysroot)/lib/rustlib/src/rust`` then rerunning the above command).
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_cilibclang
7562306a36Sopenharmony_ci********
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci``libclang`` (part of LLVM) is used by ``bindgen`` to understand the C code
7862306a36Sopenharmony_ciin the kernel, which means LLVM needs to be installed; like when the kernel
7962306a36Sopenharmony_ciis compiled with ``CC=clang`` or ``LLVM=1``.
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ciLinux distributions are likely to have a suitable one available, so it is
8262306a36Sopenharmony_cibest to check that first.
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ciThere are also some binaries for several systems and architectures uploaded at:
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	https://releases.llvm.org/download.html
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ciOtherwise, building LLVM takes quite a while, but it is not a complex process:
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	https://llvm.org/docs/GettingStarted.html#getting-the-source-code-and-building-llvm
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ciPlease see Documentation/kbuild/llvm.rst for more information and further ways
9362306a36Sopenharmony_cito fetch pre-built releases and distribution packages.
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_cibindgen
9762306a36Sopenharmony_ci*******
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ciThe bindings to the C side of the kernel are generated at build time using
10062306a36Sopenharmony_cithe ``bindgen`` tool. A particular version is required.
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ciInstall it via (note that this will download and build the tool from source)::
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci	cargo install --locked --version $(scripts/min-tool-version.sh bindgen) bindgen-cli
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci``bindgen`` needs to find a suitable ``libclang`` in order to work. If it is
10762306a36Sopenharmony_cinot found (or a different ``libclang`` than the one found should be used),
10862306a36Sopenharmony_cithe process can be tweaked using the environment variables understood by
10962306a36Sopenharmony_ci``clang-sys`` (the Rust bindings crate that ``bindgen`` uses to access
11062306a36Sopenharmony_ci``libclang``):
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci* ``LLVM_CONFIG_PATH`` can be pointed to an ``llvm-config`` executable.
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci* Or ``LIBCLANG_PATH`` can be pointed to a ``libclang`` shared library
11562306a36Sopenharmony_ci  or to the directory containing it.
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci* Or ``CLANG_PATH`` can be pointed to a ``clang`` executable.
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ciFor details, please see ``clang-sys``'s documentation at:
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	https://github.com/KyleMayes/clang-sys#environment-variables
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ciRequirements: Developing
12562306a36Sopenharmony_ci------------------------
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ciThis section explains how to fetch the tools needed for developing. That is,
12862306a36Sopenharmony_cithey are not needed when just building the kernel.
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_cirustfmt
13262306a36Sopenharmony_ci*******
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ciThe ``rustfmt`` tool is used to automatically format all the Rust kernel code,
13562306a36Sopenharmony_ciincluding the generated C bindings (for details, please see
13662306a36Sopenharmony_cicoding-guidelines.rst).
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ciIf ``rustup`` is being used, its ``default`` profile already installs the tool,
13962306a36Sopenharmony_cithus nothing needs to be done. If another profile is being used, the component
14062306a36Sopenharmony_cican be installed manually::
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci	rustup component add rustfmt
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ciThe standalone installers also come with ``rustfmt``.
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ciclippy
14862306a36Sopenharmony_ci******
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci``clippy`` is a Rust linter. Running it provides extra warnings for Rust code.
15162306a36Sopenharmony_ciIt can be run by passing ``CLIPPY=1`` to ``make`` (for details, please see
15262306a36Sopenharmony_cigeneral-information.rst).
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ciIf ``rustup`` is being used, its ``default`` profile already installs the tool,
15562306a36Sopenharmony_cithus nothing needs to be done. If another profile is being used, the component
15662306a36Sopenharmony_cican be installed manually::
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	rustup component add clippy
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ciThe standalone installers also come with ``clippy``.
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_cicargo
16462306a36Sopenharmony_ci*****
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci``cargo`` is the Rust native build system. It is currently required to run
16762306a36Sopenharmony_cithe tests since it is used to build a custom standard library that contains
16862306a36Sopenharmony_cithe facilities provided by the custom ``alloc`` in the kernel. The tests can
16962306a36Sopenharmony_cibe run using the ``rusttest`` Make target.
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ciIf ``rustup`` is being used, all the profiles already install the tool,
17262306a36Sopenharmony_cithus nothing needs to be done.
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ciThe standalone installers also come with ``cargo``.
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_cirustdoc
17862306a36Sopenharmony_ci*******
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci``rustdoc`` is the documentation tool for Rust. It generates pretty HTML
18162306a36Sopenharmony_cidocumentation for Rust code (for details, please see
18262306a36Sopenharmony_cigeneral-information.rst).
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci``rustdoc`` is also used to test the examples provided in documented Rust code
18562306a36Sopenharmony_ci(called doctests or documentation tests). The ``rusttest`` Make target uses
18662306a36Sopenharmony_cithis feature.
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ciIf ``rustup`` is being used, all the profiles already install the tool,
18962306a36Sopenharmony_cithus nothing needs to be done.
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ciThe standalone installers also come with ``rustdoc``.
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_cirust-analyzer
19562306a36Sopenharmony_ci*************
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ciThe `rust-analyzer <https://rust-analyzer.github.io/>`_ language server can
19862306a36Sopenharmony_cibe used with many editors to enable syntax highlighting, completion, go to
19962306a36Sopenharmony_cidefinition, and other features.
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci``rust-analyzer`` needs a configuration file, ``rust-project.json``, which
20262306a36Sopenharmony_cican be generated by the ``rust-analyzer`` Make target::
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci	make LLVM=1 rust-analyzer
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ciConfiguration
20862306a36Sopenharmony_ci-------------
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci``Rust support`` (``CONFIG_RUST``) needs to be enabled in the ``General setup``
21162306a36Sopenharmony_cimenu. The option is only shown if a suitable Rust toolchain is found (see
21262306a36Sopenharmony_ciabove), as long as the other requirements are met. In turn, this will make
21362306a36Sopenharmony_civisible the rest of options that depend on Rust.
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ciAfterwards, go to::
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci	Kernel hacking
21862306a36Sopenharmony_ci	    -> Sample kernel code
21962306a36Sopenharmony_ci	        -> Rust samples
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ciAnd enable some sample modules either as built-in or as loadable.
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ciBuilding
22562306a36Sopenharmony_ci--------
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ciBuilding a kernel with a complete LLVM toolchain is the best supported setup
22862306a36Sopenharmony_ciat the moment. That is::
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci	make LLVM=1
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ciFor architectures that do not support a full LLVM toolchain, use::
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci	make CC=clang
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ciUsing GCC also works for some configurations, but it is very experimental at
23762306a36Sopenharmony_cithe moment.
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ciHacking
24162306a36Sopenharmony_ci-------
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ciTo dive deeper, take a look at the source code of the samples
24462306a36Sopenharmony_ciat ``samples/rust/``, the Rust support code under ``rust/`` and
24562306a36Sopenharmony_cithe ``Rust hacking`` menu under ``Kernel hacking``.
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ciIf GDB/Binutils is used and Rust symbols are not getting demangled, the reason
24862306a36Sopenharmony_ciis the toolchain does not support Rust's new v0 mangling scheme yet.
24962306a36Sopenharmony_ciThere are a few ways out:
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci  - Install a newer release (GDB >= 10.2, Binutils >= 2.36).
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci  - Some versions of GDB (e.g. vanilla GDB 10.1) are able to use
25462306a36Sopenharmony_ci    the pre-demangled names embedded in the debug info (``CONFIG_DEBUG_INFO``).
255