• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using Rust Toolchain
2
3## Introduction
4
5This document walks you through on how to compile Rust applications to make them suitable for running with OpenHarmony OS.
6
7Rust is a static, strongly typed programming language. It has advantages such as secure memory management, high running performance, and native support for multi-thread development.
8
9This toolchain is developed based on the open-source Rust and LLVM and adapts to OpenHarmony OS target binary builds. You can use it to convert build Rust source code into the binary code that can run on OpenHarmony devices.
10
11
12## When to Use
13
14- Compile Linux x86 target binaries or cross-compile OpenHarmony target binaries in a Linux x86 environment.
15- Compile MacOS x86 target binaries in a MacOS x86 environment.
16- Compile MacOS arm64 target binaries in a MacOS arm64 environment.
17
18## How to Use
19
20### Compiling OpenHarmony Community Code
21
221. Download or update the OpenHarmony community code. For details, see [Obtaining Source Code](../get-code/sourcecode-acquire.md).
23
242. Download and install the toolchain.
25
26   ```shell
27   ./build/prebuilts_download.sh
28   ```
29
303. Prepare the code to be compiled.
31
32   Create the **build/rust/tests/test_bin_crate** directory and the following files and folders in the directory:
33
34   ```shell
35   ├── BUILD.gn
36   └── src
37        └── main.rs
38   ```
39
40   Sample code of **main.rs**:
41
42   ```rust
43   //! Hello world example for Rust.
44
45   fn main() {
46        println!("Hello, world!");
47        println!(env!("RUSTENV_TEST"));
48   }
49   ```
50
51   Sample code of **BUILD.gn**:
52
53   ```shell
54   import("//build/ohos.gni")
55
56   ohos_rust_executable("test_bin_crate") {
57     sources = [ "src/main.rs" ]
58     rustenv = [ "RUSTENV_TEST=123" ]
59     features = [ "std" ]
60     if (is_mingw) {
61       rust_static_link = true
62     }
63   }
64   ```
65
664. Run the following command to build the target:
67
68   ```shell
69   ./build.sh --product-name {product_name} --build-target
70   ```
71
72   The following uses RK3568 as an example.
73
74   ```shell
75   ./build.sh --product-name rk3568 --build-target build/rust/tests/test_bin_crate:test_bin_crate –no-prebuilt-sdk
76   ```
77
78   You can find the file generated in the following directory:
79
80   ```shell
81   ./out/rk3568/build/build_framework/test_bin_crate
82   ```
83
84###  Compiling Non-OpenHarmony Community Code
85
86#### Installing the Rust Toolchain
87
881. Download the build repository code.
89
90   ```shell
91   git clone git@gitee.com:openharmony/build.git
92   ```
93
942. Download and install the toolchain.
95
96   ```shell
97   ./build/prebuilts_download.sh
98   ```
99
1003. Check whether the toolchain is successfully installed.
101
102   ```shell
103   ./prebuilts/rustc/linux-x86_64/current/bin/rustc --version
104   ```
105
106   The toolchain is installed if information similar to the following is displayed:
107
108   ```shell
109   rustc 1.72.0-nightly (xxxx)
110   ```
111
112#### Installing OpenHarmony Clang
113
114>**NOTE**
115>
116>This tool is used to cross-compile the OpenHarmony target in a Linux x86 environment. You do not need to install it if you do not want to compile the OpenHarmony target.
117
1181. Obtain the SDK download path from the latest OpenHarmony [release notes](../../release-notes/Readme.md).
119
120   ![ohos_sdk_download](./figures/ohos_sdk_download.png)
121
1222. Download the SDK package for Linux and decompress the packages in sequence.
123
124   ```shell
125   mv ohos-sdk-windows_linux-public.tar.gz /opt/
126   cd /opt/
127   tar -zxvf ohos-sdk-windows_linux-public.tar.gz
128   cd ohos-sdk/linux
129   unzip native-linux-x64-4.1.7.5-Release.zip
130   ```
131
132#### Compiling Source Code
133
1341. Obtain the Rust source file **main.rs**.
135
136   ```rust
137   fn main() {
138     println!("hello world");
139   }
140   ```
141
1422. Compile the Rust source file **main.rs** for the host platform, which is **linux-x86_64**.
143
144   ```shell
145   ./prebuilts/rustc/linux-x86_64/current/bin/rustc main.rs
146   ```
147
148   The build result is as follows:
149
150   ```shell
151   ./main
152   hello world
153   ```
154
1553. Cross-compile the Rust source file **main.rs** for the target architecture **armv7-unknown-linux-ohos**, which is armv7 with the OpenHarmony OS.
156
157   ```shell
158   ./prebuilts/rustc/linux-x86_64/current/bin/rustc main.rs --target=armv7-unknown-linux-ohos -C linker=/opt/ohos-sdk/linux/native/llvm/bin/armv7-unknown-linux-ohos-clang
159   ```
160
1614. Cross-compile the Rust source file **main.rs** for the target architecture **aarch64-unknown-linux-ohos**, which is aarch64 with the OpenHarmony OS.
162
163   ```shell
164   ./prebuilts/rustc/linux-x86_64/current/bin/rustc main.rs --target=aarch64-unknown-linux-ohos -C linker=/opt/ohos-sdk/linux/native/llvm/bin/aarch64-unknown-linux-ohos-clang
165   ```
166
1675. Cross-compile the Rust source file **main.rs** for the target architecture **x86_64-unknown-linux-ohos**, which is x86_64 with the OpenHarmony OS.
168
169   ```shell
170   ./prebuilts/rustc/linux-x86_64/current/bin/rustc main.rs --target=x86_64-unknown-linux-ohos -C linker=/opt/ohos-sdk/linux/native/llvm/bin/x86_64-unknown-linux-ohos-clang
171   ```
172