1# Rust 工具链使用说明
2
3## 简介
4
5本文用于指导 Rust 语言开发者编译构建 OpenHarmony OS Rust 应用程序。
6
7Rust 是一门静态强类型语言,具有更安全的内存管理、更好的运行性能、原生支持多线程开发等优势。
8
9本工具链基于开源 rust 与 llvm 增量开发,适配了 OpenHarmony OS target 二进制构建。可将 rust 源码编译成能在 OpenHarmony OS 设备上使用的目标二进制。
10
11
12## 使用场景
13
14- 在 Linux x86环境本地编译 Linux x86 目标二进制或交叉编译 OpenHarmony OS 目标二进制。
15- 在 Mac x86 环境本地编译 Mac x86 目标二进制。
16- 在 Mac arm64 环境本地编译 Mac arm64 目标二进制。
17
18## 操作指导
19
20### OpenHarmony 社区代码编译
21
221. 下载或更新环境中 OpenHarmony 社区代码,下载方式可参考[获取源码](../get-code/sourcecode-acquire.md)。
23
242. 执行源码中脚本下载安装工具链。
25
26   ```shell
27   ./build/prebuilts_download.sh
28   ```
29
303. 准备待编译代码。
31
32   创建 build/rust/tests/test_bin_crate 目录,目录下新建如下所示文件与文件夹。
33
34   ```shell
35   ├── BUILD.gn
36   └── src
37        └── main.rs
38   ```
39
40   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   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. 执行编译命令。
67
68   ```shell
69   ./build.sh --product-name {product_name} --build-target
70   ```
71
72   以RK3568为例,若要编译,请执行如下命令。
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   编译生成的文件。
79
80   ```shell
81   ./out/rk3568/build/build_framework/test_bin_crate
82   ```
83
84###  非OpenHarmony 社区代码编译
85
86#### 安装 rust 工具链
87
881. 下载 build 仓代码。
89
90   ```shell
91   git clone git@gitee.com:openharmony/build.git
92   ```
93
942. 执行脚本下载安装工具链。
95
96   ```shell
97   ./build/prebuilts_download.sh
98   ```
99
1003. 查看安装情况。
101
102   ```shell
103   ./prebuilts/rustc/linux-x86_64/current/bin/rustc --version
104   ```
105
106   有类似如下显示表示安装成功。
107
108   ```shell
109   rustc 1.72.0-nightly (xxxx)
110   ```
111
112#### 安装 OpenHarmony OS Clang 工具
113
114*![icon-note](public_sys-resources/icon-note.gif)说明*
115
116> 用于在 Linux x86 环境下进行 OpenHarmony OS 的 target 交叉编译,不编译 OpenHarmony OS target 可不安装。
117
1181. 在 OpenHarmony 的最新[版本说明](../../release-notes/Readme.md)中获取 SDK 包下载路径
119
120   ![ohos_sdk_download](./figures/ohos_sdk_download.png)
121
1222. 选择 Linux 环境 SDK 包下载,依次解压下载的压缩包。
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#### 编译代码
133
1341. 代码用例main.rs135
136   ```rust
137   fn main() {
138     println!("hello world");
139   }
140   ```
141
1422. 编译 target 为本地环境时命令示例。
143
144   ```shell
145   ./prebuilts/rustc/linux-x86_64/current/bin/rustc main.rs
146   ```
147
148   执行构建结果。
149
150   ```shell
151   ./main
152   hello world
153   ```
154
1553. 编译 target 为 armv7-unknown-linux-ohos时命令示例。
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. 编译 target 为 aarch64-unknown-linux-ohos时命令示例。
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. 编译 target 为 x86_64-unknown-linux-ohos时命令示例。
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