背景
Rust是一門靜態(tài)強(qiáng)類型語言,具有更安全的內(nèi)存管理、更好的運行性能、原生支持多線程開發(fā)等優(yōu)勢。Rust官方也使用Cargo工具來專門為Rust代碼創(chuàng)建工程和構(gòu)建編譯。 OpenHarmony為了集成C/C++ 代碼和提升編譯速度,使用了GN + Ninja的編譯構(gòu)建系統(tǒng)。GN的構(gòu)建語言簡潔易讀,Ninja的匯編級編譯規(guī)則直接高效。 為了在OpenHarmony中集成Rust代碼,并最大程度發(fā)揮Rust和OpenHarmony中原有C/C++ 代碼的交互性,采用GN作為統(tǒng)一構(gòu)建工具,即通過GN構(gòu)建Rust源碼文件(xxx.rs),并增加與C/C++互操作、編譯時lint、測試、IDL轉(zhuǎn)換、三方庫集成、IDE等功能。同時擴(kuò)展gn框架,支持接口自動化轉(zhuǎn)換,最大程度簡化開發(fā)。
基本概念
術(shù)語 | 描述 |
---|---|
Cargo | Cargo是Rust官方使用的構(gòu)建工具,允許Rust項目聲明其各種依賴項,并確保您始終獲得可重復(fù)的構(gòu)建。 |
crate | crate是一個獨立的可編譯單元。 |
Lint | Lint是指出常見編程錯誤、錯誤、樣式錯誤和可疑結(jié)構(gòu)的工具??梢詫Τ绦蜻M(jìn)行更加廣泛的錯誤分析。 |
配置規(guī)則
OpenHarmony提供了用于Rust代碼編譯構(gòu)建的各類型GN模板,可以用于編譯Rust可執(zhí)行文件,動態(tài)庫和靜態(tài)庫等。各類型模板說明如下:
GN模板 | 功能 | 輸出 |
---|---|---|
ohos_rust_executable | rust可執(zhí)行文件 | rust可執(zhí)行文件,不帶后綴 |
ohos_rust_shared_liary | rust動態(tài)庫 | rust dylib動態(tài)庫,默認(rèn)后綴.dylib.so |
ohos_rust_static_liary | rust靜態(tài)庫 | rust rlib靜態(tài)庫,默認(rèn)后綴.rlib |
ohos_rust_proc_macro | rust proc_macro | rust proc_macro庫, 默認(rèn)后綴.so |
ohos_rust_shared_ffi | rust FFI動態(tài)庫 | rust cdylib動態(tài)庫,給C/C++模塊調(diào)用,默認(rèn)后綴.so |
ohos_rust_static_ffi | rust FFI靜態(tài)庫 | rust staticlib庫,給C/C++模塊調(diào)用,默認(rèn)后綴.a |
ohos_rust_cargo_crate | 三方包Cargo crate | rust三方crate,支持rlib、dylib、bin |
ohos_rust_systemtest | rust系統(tǒng)測試用例 | rust可執(zhí)行系統(tǒng)測試用例,不帶后綴 |
ohos_rust_unittest | rust單元測試用例 | rust可執(zhí)行單元測試用例,不帶后綴 |
ohos_rust_fuzztest | rust Fuzz測試用例 | rust可執(zhí)行Fuzz測試用例,不帶后綴 |
配置Rust靜態(tài)庫示例
該示例用于測試Rust可執(zhí)行bin文件和靜態(tài)庫rlib文件的編譯,以及可執(zhí)行文件對靜態(tài)庫的依賴,使用模板ohos_rust_executable和ohos_rust_static_library。操作步驟如下:
- 創(chuàng)建build/rust/tests/test_rlib_crate/src/simple_printer.rs,如下所示:
//! simple_printer /// struct RustLogMessage pub struct RustLogMessage { /// i32: id pub id: i32, /// String: msg pub msg: String, } /// function rust_log_rlib pub fn rust_log_rlib(msg: RustLogMessage) { println!("id:{} message:{:?}", msg.id, msg.msg) }
- 創(chuàng)建build/rust/tests/test_rlib_crate/src/main.rs,如下所示:
//! rlib_crate example for Rust. extern crate simple_printer_rlib; use simple_printer_rlib::rust_log_rlib; use simple_printer_rlib::RustLogMessage; fn main() { let msg: RustLogMessage = RustLogMessage { id: 0, msg: "string in rlib crate".to_string(), }; rust_log_rlib(msg); }
- 配置gn腳本build/rust/tests/test_rlib_crate/BUILD.gn,如下所示:
```json
import("http://build/ohos.gni")
ohos_rust_executable("test_rlib_crate") {
sources = [ "src/main.rs" ]
deps = [ ":simple_printer_rlib" ]
}
ohos_rust_static_library("simple_printer_rlib") {
sources = [ "src/simple_printer.rs" ]
crate_name = "simple_printer_rlib"
crate_type = "rlib"
features = [ "std" ]
}
4. 執(zhí)行編譯得到的可執(zhí)行文件,運行結(jié)果如下:
./build.sh --product-name rk3568 --build-target build/rust/tests:tests --no-prebuilt-sdk
hdc_std.exe shell mount -o rw,remount /
hdc_std.exe shell file send test_dylib_crate /data/local/tmp
hdc_std.exe file send libsimple_printer_dylib.dylib.so /system/lib
hdc_std.exe shell
# cd /data/local/tmp
# chmod +x test_dylib_crate
# ./test_dylib_crate
id:0 message:"string in rlib crate"
#### 配置Rust應(yīng)用系統(tǒng)庫示例
1. 增加依賴
// GN 里增加依賴
ohos_rust_executable("test_dylib_crate") {
sources = [ "src/main.rs" ]
deps = [ ":simple_printer_dylib" ]
# 增加外部依賴
external_deps = [ "hilog:hilog_rust" ]
}
// bundle.json 里增加依賴
"components": [
"hilog"
],
2. 增加調(diào)用
extern crate simple_printer_dylib;
use simple_printer_dylib::rust_log_dylib;
use simple_printer_dylib::RustLogMessage;
//! 增加引用
use std::ffi::{ c_char, CString };
use hilog_rust::{hilog, info, HiLogLabel, LogType};
const LOG_LABEL: HiLogLabel = HiLogLabel {
log_type: LogType::LogCore,
domain: 0xD002220,
tag: "TEST_RUST",
};
fn main() {
let msg: RustLogMessage = RustLogMessage {
id: 0,
msg: "string in rlib crate".to_string(),
};
rust_log_dylib(msg);
//! 增加調(diào)用
info!(LOG_LABEL, "Fnished enable all keys.");
}
3. 運行測試
// 運行
# ./test_dylib_crate
id:0 message:"string in rlib crate"
// 查看hilog
# hilog | grep Fnished
08-17 05:14:18.121 29293 29293 I C02220/TEST_RUST: Fnished enable all keys.
---
注意:rust和openharmony其他開源代碼可以混合使用,如rust可以生成C/C庫,給其他C/C應(yīng)用使用,反之C/C++庫也可以給rust應(yīng)用調(diào)用

審核編輯 黃宇
-
Rust
+關(guān)注
關(guān)注
1文章
232瀏覽量
6728 -
OpenHarmony
+關(guān)注
關(guān)注
25文章
3770瀏覽量
17052 -
鴻蒙OS
+關(guān)注
關(guān)注
0文章
190瀏覽量
4646
發(fā)布評論請先 登錄
相關(guān)推薦
鴻蒙OS應(yīng)用程序開發(fā)
鴻蒙OS適用的全場景到底什么意思?
鴻蒙os系統(tǒng)是什么意思 鴻蒙os系統(tǒng)有什么作用
鴻蒙 OS 應(yīng)用開發(fā)初體驗
華為鴻蒙OS又有嚇人的設(shè)計 蘋果的Carplay在鴻蒙OS面前真的自嘆不如
華為鴻蒙OS 2.0帶來哪些智慧體驗?
鴻蒙OS 2.0手機(jī)開發(fā)者Beta版發(fā)布會在京舉辦
華為正式推出鴻蒙OS的手機(jī)開發(fā)者Beta版
鴻蒙OS2.0手機(jī)開發(fā)者Beta版登場
華為發(fā)布鴻蒙OS Beta版
鴻蒙OS與Lite OS的區(qū)別是什么
華為鴻蒙OS 2.0開發(fā)者公測版本大批量向已申請開發(fā)者推送
鴻蒙os怎么升級
華為開發(fā)者大會2021鴻蒙os在哪場
[鴻蒙]OpenHarmony4.0的Rust開發(fā)
![[<b class='flag-5'>鴻蒙</b>]OpenHarmony4.0的<b class='flag-5'>Rust</b><b class='flag-5'>開發(fā)</b>](https://file1.elecfans.com/web2/M00/C1/DB/wKgaomXbKX-AAe6rAADEW5Pyw8c913.png)
評論