|
1 | 1 | // Copyright (c) 2024 Linaro LTD
|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
4 |
| -//! Zephyr application support for Rust |
| 4 | +//! # Zephyr application support for Rust |
5 | 5 | //!
|
6 |
| -//! This crates provides the core functionality for applications written in Rust that run on top of |
7 |
| -//! Zephyr. |
| 6 | +//! Rust is a systems programming language focused on safety, speed, and concurrency. Designed to |
| 7 | +//! prevent common programming errors such as null pointer deferencing and buffer overflows, Rust |
| 8 | +//! emphasizes memory safety without sacrificing performance. Its powerful type system and |
| 9 | +//! ownership model ensure thread-safe programming, making it an ideal choice for developing |
| 10 | +//! reliable and efficient low-level code. Rust's expressive syntax and modern features make it a |
| 11 | +//! robust alternative for developers working on embedded systems, operating systems, and other |
| 12 | +//! performance-critial applications. |
| 13 | +//! |
| 14 | +//! # Enabling Rust Support |
| 15 | +//! |
| 16 | +//! Zephyr currently supports applications that are written in Rust and C. To enable Rust support, |
| 17 | +//! you must select the `CONFIG_RUST` option in the application configuration file. |
| 18 | +//! |
| 19 | +//! The Rust toolchain is separate from the rest of the Zephyr SDK. It is recommended to use the |
| 20 | +//! [rustup](https://rustup.rs) tool to install the Rust toolchain. In addition to the base |
| 21 | +//! compiler, you will need to install core libraries for the target(s) you wish to compile. The |
| 22 | +//! easiest way to determine what needs to be installed is to attempt a build. Compilation |
| 23 | +//! diagnostics will indicate the `rustup` command needed to install the appropriate target |
| 24 | +//! support: |
| 25 | +//! |
| 26 | +//! ```text |
| 27 | +//! $ west build ... |
| 28 | +//! ... |
| 29 | +//! error[E0463]: can't find crate for `core` |
| 30 | +//! | |
| 31 | +//! = note: the `thumbv7m-none-eabi` target may not be installed |
| 32 | +//! = help: consider downloading the target with `rustup target add thumbv7-none-eabi` |
| 33 | +//! ``` |
| 34 | +//! |
| 35 | +//! in this case, the provided `rustup` command will install the necessary target support. The |
| 36 | +//! target required depends on both the board selected and certain configuration choices, such as |
| 37 | +//! whether floating point is enabled. |
| 38 | +//! |
| 39 | +//! # Writing a Rust Application |
| 40 | +//! |
| 41 | +//! See the `samples` directory in the zephyr-lang-rust repo for examples of Rust applications. The |
| 42 | +//! cmake build system is used to build the majority of Zephyr. The declarations in the sample |
| 43 | +//! `CMakeLists.txt` files will show how to have cmake invoke `cargo build` at the right time, with |
| 44 | +//! the right settings to build for your target. The rest of the directory is a typical Rust |
| 45 | +//! crate, with a few special caveats: |
| 46 | +//! |
| 47 | +//! * The crate must currently be named `"rustapp"`. This is so that the cmake rules can find the |
| 48 | +//! build. |
| 49 | +//! * The crate must be a staticlib crate. |
| 50 | +//! * The crate must depend on a "zephyr" crate. This crate does not come from crates.io, but will |
| 51 | +//! be located by cmake. This documentation is for this "zephyr" crate. |
| 52 | +//! |
| 53 | +//! cmake and/or the `west build` command will place the build in a directory (`build` by default) |
| 54 | +//! and the rules will direct cargo to also place its build output there (`build/rust/target` for |
| 55 | +//! the majority of the output). |
| 56 | +//! |
| 57 | +//! The build process will also generate a template for a `.cargo/config.toml` that will configure |
| 58 | +//! cargo so that tools such as `cargo check` and `rust analyzer` will work with your project. You |
| 59 | +//! can make a symlink for this file to allow these tools to work |
| 60 | +//! |
| 61 | +//! ```bash |
| 62 | +//! $ mkdir .cargo |
| 63 | +//! $ cd .cargo |
| 64 | +//! $ ln -s ../build/rust/sample-cargo-config.toml config.coml |
| 65 | +//! $ cd .. |
| 66 | +//! $ cargo check |
| 67 | +//! ``` |
| 68 | +//! |
| 69 | +//! # Zephyr Functionality |
| 70 | +//! |
| 71 | +//! The bindings to Rust for Zephyr are still under development and are currently minimal. However, |
| 72 | +//! some Zephyr functionality is available. |
| 73 | +//! |
| 74 | +//! ## Bool Kconfig Settings |
| 75 | +//! |
| 76 | +//! Boolean Kconfig settings can be accessed from within Rust code. However, since Rust requires |
| 77 | +//! certain compilation decisions, accessing these with `#[cfg...]` directives requires a small |
| 78 | +//! addition to `build.rs`. See the docs in the [`zephyr-build` crate](../zephyr-build/index.html). |
| 79 | +//! |
| 80 | +//! # Other Kconfig Settings |
| 81 | +//! |
| 82 | +//! All boolean, numeric, and string Kconfig settings are available through the `kconfig` module. |
8 | 83 |
|
9 | 84 | #![no_std]
|
10 | 85 | #![allow(unexpected_cfgs)]
|
|
0 commit comments