-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add documentation for Rust support #83715
Open
d3zd3z
wants to merge
3
commits into
zephyrproject-rtos:main
Choose a base branch
from
d3zd3z:r/docs-2025-01-08
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+184
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ Language Support | |
|
||
c/index.rst | ||
cpp/index.rst | ||
rust/index.rst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
.. _language_rust: | ||
|
||
Rust Language Support | ||
##################### | ||
|
||
Rust is a modern systems programming language focused on safety, speed, and concurrency. Unlike | ||
traditional systems programming languages such as C and C++, Rust ensures memory safety without | ||
relying on garbage colleciton, thanks to its ownership model and borrow checker. Rust also offers a | ||
rich type system, expressive syntax, and support for zero-cost abstractions, making it well-suited | ||
for embedded development. | ||
|
||
Rust’s emphasis on reliability and performance aligns well with Zephyr. By combining Rust’s | ||
guarantees with Zephyr’s robust ecosystem, developers can create efficient and maintainable embedded | ||
applications. The integration of Rust with Zephyr allows developers to leverage Rust’s language | ||
features while benefiting from Zephyr’s drivers, APIs, and multi-threading capabilities. | ||
|
||
At this point, Rust support in Zephyr is entirely for those wishing to write applications in Rust | ||
that run *on* Zephyr. Any efforts to add code *to* Zephyr, written in Rust (such as device drivers) | ||
would be an independent effort. | ||
|
||
Enabling Rust Support | ||
********************* | ||
|
||
Both Rust and Zephyr have their own build systems, along with an ecosystem around this. For Rust, | ||
this is the "crate" system of managing external dependencies. Because this support is for | ||
applications written in Rust, Zephyr's rust support leverages crates, and the cargo tool to build | ||
the Rust part of the application. | ||
|
||
Enabling the module | ||
------------------- | ||
|
||
Before getting very far, it is important to make sure that the Rust support module is included. By | ||
default, rust support is listed in the project manifest, but is marked as optional. It is easy to | ||
enable the module using ``west``: | ||
|
||
.. code-block:: console | ||
|
||
west config manifest.project-filter +zephyr-lang-rust | ||
west update | ||
|
||
This should enable the module, and then sync modules, which will add the module to your modules | ||
directory. | ||
|
||
Setting up the environment | ||
-------------------------- | ||
|
||
Before starting with Rust, make sure you are able to build a simple Zephyr application, such as | ||
"blinky". It is easier to debug Zephyr build issues before introducing Rust. | ||
|
||
After this, you will need to have a recent Rust toolchain install, as well as target support for | ||
your desired target. It is generally easiest to use `Rustup`_ to install an maintain a rust | ||
installation. After following these instructions, you will need to install target support for your | ||
target. | ||
|
||
Because Zephyr and LLVM use different naming conventions for targets, and can be a little | ||
challenging to determine the right target to install. Sometimes, it is easiest to proceed with | ||
building a Rust sample with your desired target, and the toolchain will emit an error message giving | ||
the appropriate command to run. The target needed will depend both on the board/soc selected, as | ||
well as certain configuration choices (such as whether floating point is enabled). Please see the | ||
function ``_rust_map_target`` in :module_file:`zephyr-lang-rust:CMakeLists.txt` for details on how | ||
this is computed. As an example, a particular Cortex-M target may need the following command: | ||
|
||
.. code-block:: console | ||
|
||
rustup target add thumbv7m-none-eabi | ||
|
||
.. _`Rustup`: https://rustup.rs/ | ||
|
||
Building a sample | ||
----------------- | ||
|
||
.. This file (a directory in samples) and the above CMakeLists.txt should be done with the | ||
module_file reference. However, the current doc build doesn't seem to be including modules, so | ||
for now, just make these regular file references. | ||
|
||
The directory :file:`zephyr-lang-rust:samples` contains some samples that can be useful for | ||
testing. Hello world is a good basic test, although blinky may also be useful if your target has an | ||
LED defined. Please make sure that :file:`samples/basic/blink` works with your board, | ||
however, before trying it with Rust. | ||
|
||
Following from the getting-started guide, you can build the hello world sample in rust with: | ||
|
||
.. code-block: console | ||
|
||
cd ~/zephyrproject/modules/lang/rust | ||
west build -p always -b <your-board-name> samples/blinky | ||
|
||
Provided this is successful, the image can be flashed and tested as per the getting started guide. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is west really needed to build rust docs? Also, why is a board required? I'm a bit surprised that rust docs are not generated on its own repo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do generate rust docs in that repo, but they aren't correlated with any particular Zephyr release. This builds the rust docs that are specifically the version brought in.
As far as why west and a board are needed:
zephyr::raw
also specific to that board. I intend to have instructions in the docs for Rust explain how to generate and view the docs locally, so developers can have those be accurate for their configuration and board. But, we need something so that we can reference the docs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are. But, using them in the main docs would be a bit complicated logistically. Namely, we currently generate docs for each PR in the rust repo. These will stick around for I think two weeks. We also generate docs for main (well, we will, there is a small issue to fix that), but these are replaced each time a change is merged. If we wanted to directly use that artifact from here, we'd need to also generate them for specific shas, and somehow know to only retire those when a manifest doesn't refer to them. It would have to check every possible branch's manifest (there will be backport branches after the first release containing this).
It seems just easier to regenerate them in the Zephyr repo for the correct version, that way they'll persist according to the rules here (for each PR, the two weeks, and latest getting updated).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand you need a build target, but picking a nordic board feels weird unless explained. I'd rather choose some vendor-agnostic target.
Is it using https://github.com/zephyrproject-rtos/zephyr/blob/main/doc/_extensions/zephyr/kconfig/__init__.py#L133 ?
Maybe you should instead create a "rustdoc" board as a build target that enables as much zephyr raw as possible for docs purposes. In general, I don't think rust users should really bother about the APIs exposed depending on the board... And zephyr::raw should likely be kept as something internal people should not rely on their apps.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could do one of the qemu targets. I think this will be reasonably complete, as most shouldn't depend on the particular devices that are present. Someone wanting to navigate the rust version of their devicetree really needs to be generating docs locally for their specific build anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where this really matters is with the generated modules representing their current device tree. The user will need to use this to be able to get instances of the devices on their platform. And, this doesn't make sense to have it be generic.