Skip to content

Commit d07479b

Browse files
committed
docs: add Rust documentation
Most of the documentation for Rust is written within the source code itself, as it is idiomatic for Rust projects. This applies to both the shared infrastructure at `rust/` as well as any other Rust module (e.g. drivers) written across the kernel. However, these documents contain general information that does not fit particularly well in the source code, like the Quick Start guide. It also contains a few other small changes elsewhere in the documentation folder. Reviewed-by: Kees Cook <[email protected]> Co-developed-by: Alex Gaynor <[email protected]> Signed-off-by: Alex Gaynor <[email protected]> Co-developed-by: Finn Behrens <[email protected]> Signed-off-by: Finn Behrens <[email protected]> Co-developed-by: Adam Bratschi-Kaye <[email protected]> Signed-off-by: Adam Bratschi-Kaye <[email protected]> Co-developed-by: Wedson Almeida Filho <[email protected]> Signed-off-by: Wedson Almeida Filho <[email protected]> Co-developed-by: Michael Ellerman <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Co-developed-by: Sven Van Asbroeck <[email protected]> Signed-off-by: Sven Van Asbroeck <[email protected]> Co-developed-by: Wu XiangCheng <[email protected]> Signed-off-by: Wu XiangCheng <[email protected]> Co-developed-by: Gary Guo <[email protected]> Signed-off-by: Gary Guo <[email protected]> Co-developed-by: Boris-Chengbiao Zhou <[email protected]> Signed-off-by: Boris-Chengbiao Zhou <[email protected]> Co-developed-by: Yuki Okushi <[email protected]> Signed-off-by: Yuki Okushi <[email protected]> Co-developed-by: Wei Liu <[email protected]> Signed-off-by: Wei Liu <[email protected]> Co-developed-by: Daniel Xu <[email protected]> Signed-off-by: Daniel Xu <[email protected]> Co-developed-by: Julian Merkle <[email protected]> Signed-off-by: Julian Merkle <[email protected]> Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 2f7ab12 commit d07479b

File tree

10 files changed

+675
-4
lines changed

10 files changed

+675
-4
lines changed

Documentation/doc-guide/kernel-doc.rst

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ when it is embedded in source files.
1414
reasons. The kernel source contains tens of thousands of kernel-doc
1515
comments. Please stick to the style described here.
1616

17+
.. note:: kernel-doc does not cover Rust code: please see
18+
Documentation/rust/general-information.rst instead.
19+
1720
The kernel-doc structure is extracted from the comments, and proper
1821
`Sphinx C Domain`_ function and type descriptions with anchors are
1922
generated from them. The descriptions are filtered for special kernel-doc

Documentation/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ merged much easier.
8282
maintainer/index
8383
fault-injection/index
8484
livepatch/index
85+
rust/index
8586

8687

8788
Kernel API documentation

Documentation/kbuild/kbuild.rst

+17
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ KCFLAGS
4848
-------
4949
Additional options to the C compiler (for built-in and modules).
5050

51+
KRUSTFLAGS
52+
----------
53+
Additional options to the Rust compiler (for built-in and modules).
54+
5155
CFLAGS_KERNEL
5256
-------------
5357
Additional options for $(CC) when used to compile
@@ -57,6 +61,15 @@ CFLAGS_MODULE
5761
-------------
5862
Additional module specific options to use for $(CC).
5963

64+
RUSTFLAGS_KERNEL
65+
----------------
66+
Additional options for $(RUSTC) when used to compile
67+
code that is compiled as built-in.
68+
69+
RUSTFLAGS_MODULE
70+
----------------
71+
Additional module specific options to use for $(RUSTC).
72+
6073
LDFLAGS_MODULE
6174
--------------
6275
Additional options used for $(LD) when linking modules.
@@ -69,6 +82,10 @@ HOSTCXXFLAGS
6982
------------
7083
Additional flags to be passed to $(HOSTCXX) when building host programs.
7184

85+
HOSTRUSTFLAGS
86+
-------------
87+
Additional flags to be passed to $(HOSTRUSTC) when building host programs.
88+
7289
HOSTLDFLAGS
7390
-----------
7491
Additional flags to be passed when linking host programs.

Documentation/kbuild/makefiles.rst

+46-4
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ This document describes the Linux kernel Makefiles.
2929
--- 4.1 Simple Host Program
3030
--- 4.2 Composite Host Programs
3131
--- 4.3 Using C++ for host programs
32-
--- 4.4 Controlling compiler options for host programs
33-
--- 4.5 When host programs are actually built
32+
--- 4.4 Using Rust for host programs
33+
--- 4.5 Controlling compiler options for host programs
34+
--- 4.6 When host programs are actually built
3435
3536
=== 5 Userspace Program support
3637
--- 5.1 Simple Userspace Program
@@ -835,7 +836,24 @@ Both possibilities are described in the following.
835836
qconf-cxxobjs := qconf.o
836837
qconf-objs := check.o
837838

838-
4.4 Controlling compiler options for host programs
839+
4.4 Using Rust for host programs
840+
--------------------------------
841+
842+
Kbuild offers support for host programs written in Rust. However,
843+
since a Rust toolchain is not mandatory for kernel compilation,
844+
it may only be used in scenarios where Rust is required to be
845+
available (e.g. when ``CONFIG_RUST`` is enabled).
846+
847+
Example::
848+
849+
hostprogs := target
850+
target-rust := y
851+
852+
Kbuild will compile ``target`` using ``target.rs`` as the crate root,
853+
located in the same directory as the ``Makefile``. The crate may
854+
consist of several source files (see ``samples/rust/hostprogs``).
855+
856+
4.5 Controlling compiler options for host programs
839857
--------------------------------------------------
840858

841859
When compiling host programs, it is possible to set specific flags.
@@ -867,7 +885,7 @@ Both possibilities are described in the following.
867885
When linking qconf, it will be passed the extra option
868886
"-L$(QTDIR)/lib".
869887

870-
4.5 When host programs are actually built
888+
4.6 When host programs are actually built
871889
-----------------------------------------
872890

873891
Kbuild will only build host-programs when they are referenced
@@ -1181,6 +1199,17 @@ When kbuild executes, the following steps are followed (roughly):
11811199
The first example utilises the trick that a config option expands
11821200
to 'y' when selected.
11831201

1202+
KBUILD_RUSTFLAGS
1203+
$(RUSTC) compiler flags
1204+
1205+
Default value - see top level Makefile
1206+
Append or modify as required per architecture.
1207+
1208+
Often, the KBUILD_RUSTFLAGS variable depends on the configuration.
1209+
1210+
Note that target specification file generation (for ``--target``)
1211+
is handled in ``scripts/generate_rust_target.rs``.
1212+
11841213
KBUILD_AFLAGS_KERNEL
11851214
Assembler options specific for built-in
11861215

@@ -1208,6 +1237,19 @@ When kbuild executes, the following steps are followed (roughly):
12081237
are used for $(CC).
12091238
From commandline CFLAGS_MODULE shall be used (see kbuild.rst).
12101239

1240+
KBUILD_RUSTFLAGS_KERNEL
1241+
$(RUSTC) options specific for built-in
1242+
1243+
$(KBUILD_RUSTFLAGS_KERNEL) contains extra Rust compiler flags used to
1244+
compile resident kernel code.
1245+
1246+
KBUILD_RUSTFLAGS_MODULE
1247+
Options for $(RUSTC) when building modules
1248+
1249+
$(KBUILD_RUSTFLAGS_MODULE) is used to add arch-specific options that
1250+
are used for $(RUSTC).
1251+
From commandline RUSTFLAGS_MODULE shall be used (see kbuild.rst).
1252+
12111253
KBUILD_LDFLAGS_MODULE
12121254
Options for $(LD) when linking modules
12131255

Documentation/process/changes.rst

+41
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ you probably needn't concern yourself with pcmciautils.
3131
====================== =============== ========================================
3232
GNU C 5.1 gcc --version
3333
Clang/LLVM (optional) 11.0.0 clang --version
34+
Rust (optional) 1.62.0 rustc --version
35+
bindgen (optional) 0.56.0 bindgen --version
3436
GNU make 3.81 make --version
3537
bash 4.2 bash --version
3638
binutils 2.23 ld -v
@@ -80,6 +82,29 @@ kernels. Older releases aren't guaranteed to work, and we may drop workarounds
8082
from the kernel that were used to support older versions. Please see additional
8183
docs on :ref:`Building Linux with Clang/LLVM <kbuild_llvm>`.
8284

85+
Rust (optional)
86+
---------------
87+
88+
A particular version of the Rust toolchain is required. Newer versions may or
89+
may not work because the kernel depends on some unstable Rust features, for
90+
the moment.
91+
92+
Each Rust toolchain comes with several "components", some of which are required
93+
(like ``rustc``) and some that are optional. The ``rust-src`` component (which
94+
is optional) needs to be installed to build the kernel. Other components are
95+
useful for developing.
96+
97+
Please see Documentation/rust/quick-start.rst for instructions on how to
98+
satisfy the build requirements of Rust support. In particular, the ``Makefile``
99+
target ``rustavailable`` is useful to check why the Rust toolchain may not
100+
be detected.
101+
102+
bindgen (optional)
103+
------------------
104+
105+
``bindgen`` is used to generate the Rust bindings to the C side of the kernel.
106+
It depends on ``libclang``.
107+
83108
Make
84109
----
85110

@@ -348,6 +373,12 @@ Sphinx
348373
Please see :ref:`sphinx_install` in :ref:`Documentation/doc-guide/sphinx.rst <sphinxdoc>`
349374
for details about Sphinx requirements.
350375

376+
rustdoc
377+
-------
378+
379+
``rustdoc`` is used to generate the documentation for Rust code. Please see
380+
Documentation/rust/general-information.rst for more information.
381+
351382
Getting updated software
352383
========================
353384

@@ -364,6 +395,16 @@ Clang/LLVM
364395

365396
- :ref:`Getting LLVM <getting_llvm>`.
366397

398+
Rust
399+
----
400+
401+
- Documentation/rust/quick-start.rst.
402+
403+
bindgen
404+
-------
405+
406+
- Documentation/rust/quick-start.rst.
407+
367408
Make
368409
----
369410

Documentation/rust/arch-support.rst

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.. SPDX-License-Identifier: GPL-2.0
2+
3+
Arch Support
4+
============
5+
6+
Currently, the Rust compiler (``rustc``) uses LLVM for code generation,
7+
which limits the supported architectures that can be targeted. In addition,
8+
support for building the kernel with LLVM/Clang varies (please see
9+
Documentation/kbuild/llvm.rst). This support is needed for ``bindgen``
10+
which uses ``libclang``.
11+
12+
Below is a general summary of architectures that currently work. Level of
13+
support corresponds to ``S`` values in the ``MAINTAINERS`` file.
14+
15+
============ ================ ==============================================
16+
Architecture Level of support Constraints
17+
============ ================ ==============================================
18+
============ ================ ==============================================

0 commit comments

Comments
 (0)