首页 > 其他分享 >OpenHarmony 4.0的Rust开发

OpenHarmony 4.0的Rust开发

时间:2024-01-29 12:02:26浏览次数:21  
标签:OpenHarmony 4.0 dylib crate rlib Rust ohos rust

OH4.0的Rust开发

背景

Rust是一门静态强类型语言,具有更安全的内存管理、更好的运行性能、原生支持多线程开发等优势。Rust官方也使用Cargo工具来专门为Rust代码创建工程和构建编译。 OpenHarmony为了集成C/C++ 代码和提升编译速度,使用了GN + Ninja的编译构建系统。GN的构建语言简洁易读,Ninja的汇编级编译规则直接高效。 为了在OpenHarmony中集成Rust代码,并最大程度发挥Rust和OpenHarmony中原有C/C++ 代码的交互性,采用GN作为统一构建工具,即通过GN构建Rust源码文件(xxx.rs),并增加与C/C++互操作、编译时lint、测试、IDL转换、三方库集成、IDE等功能。同时扩展gn框架,支持接口自动化转换,最大程度简化开发。


基本概念

术语 描述
Cargo Cargo是Rust官方使用的构建工具,允许Rust项目声明其各种依赖项,并确保您始终获得可重复的构建。
crate crate是一个独立的可编译单元。
Lint Lint是指出常见编程错误、错误、样式错误和可疑结构的工具。可以对程序进行更加广泛的错误分析。

配置规则

OpenHarmony提供了用于Rust代码编译构建的各类型GN模板,可以用于编译Rust可执行文件,动态库和静态库等。各类型模板说明如下:

GN模板 功能 输出
ohos_rust_executable rust可执行文件 rust可执行文件,不带后缀
ohos_rust_shared_library rust动态库 rust dylib动态库,默认后缀.dylib.so
ohos_rust_static_library rust静态库 rust rlib静态库,默认后缀.rlib
ohos_rust_proc_macro rust proc_macro rust proc_macro库, 默认后缀.so
ohos_rust_shared_ffi rust FFI动态库 rust cdylib动态库,给C/C++模块调用,默认后缀.so
ohos_rust_static_ffi rust FFI静态库 rust staticlib库,给C/C++模块调用,默认后缀.a
ohos_rust_cargo_crate 三方包Cargo crate rust三方crate,支持rlib、dylib、bin
ohos_rust_systemtest rust系统测试用例 rust可执行系统测试用例,不带后缀
ohos_rust_unittest rust单元测试用例 rust可执行单元测试用例,不带后缀
ohos_rust_fuzztest rust Fuzz测试用例 rust可执行Fuzz测试用例,不带后缀

配置Rust静态库示例

该示例用于测试Rust可执行bin文件和静态库rlib文件的编译,以及可执行文件对静态库的依赖,使用模板ohos_rust_executable和ohos_rust_static_library。操作步骤如下:

  1. 创建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)
    }
    
  2. 创建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);
    }
    
  3. 配置gn脚本build/rust/tests/test_rlib_crate/BUILD.gn,如下所示:

    import("//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. 执行编译得到的可执行文件,运行结果如下:

    ./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应用系统库示例

  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. 增加调用

    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);
        //! 增加调用
        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++应用使用,反之C/C++库也可以给rust应用调用

总结

本文作者:左翼风发

想了解更多关于鸿蒙的内容,请访问:​

​51CTO鸿蒙开发者社区

​https://ost.51cto.com/#bkwz​

标签:OpenHarmony,4.0,dylib,crate,rlib,Rust,ohos,rust
From: https://blog.51cto.com/harmonyos/9462374

相关文章

  • 写一段rust代码,两个线程共享一个bool变量,一个写,一个读
    usestd::sync::{Arc,Mutex};usestd::thread;fnmain(){//创建一个布尔变量并用Arc和Mutex包装,使其可在多个线程间共享和修改letshared_bool=Arc::new(Mutex::new(false));//克隆Arc变量,以便在两个线程之间共享letwriter_shared_bool=Ar......
  • 26从零开始用Rust编写nginx,如何发布Rust项目到Docker
    wmproxywmproxy已用Rust实现http/https代理,socks5代理,反向代理,静态文件服务器,四层TCP/UDP转发,内网穿透,后续将实现websocket代理等,会将实现过程分享出来,感兴趣的可以一起造个轮子项目地址国内:https://gitee.com/tickbh/wmproxygithub:https://github.com/tickbh/wmproxy容......
  • Rust学习笔记
    迭代器迭代器是一个值,它可以生成一系列值Iterrator类型和IntoIterator类型迭代器是实现了Iterator类型的任意值IntoIterator是迭代器本身类型,Item是它生成的值的类型。任意实现了IntoIterator的类型都可以成为可迭代者,可以通过into_iter获得一个迭代器迭代器能生成值迭......
  • OpenHarmony—不支持解构赋值
    规则:arkts-no-destruct-assignment级别:错误ArkTS不支持解构赋值。可使用其他替代方法,例如,使用临时变量。TypeScriptlet[one,two]=[1,2];//此处需要分号[one,two]=[two,one];lethead,tail[head,...tail]=[1,2,3,4];ArkTSletarr:number[]=[1,2];......
  • OpenHarmony—仅允许在表达式中使用typeof运算符
    规则:arkts-no-type-query级别:错误ArkTS仅支持在表达式中使用typeof运算符,不允许使用typeof作为类型。TypeScriptletn1=42;lets1='foo';console.log(typeofn1);//'number'console.log(typeofs1);//'string'letn2:typeofn1lets2:typeofs1ArkTS......
  • Slint-UI移植到任意平台-概述-Rust
    前言本文仅为笔者记忆,个人经验写着玩,目前1.3.2版本。注:本文尚未完成。注:本文尚未完成。注:本文尚未完成。本人目前想要移植一种贴近前端技术的GUI到裸机上,但是裸机不支持UNIX环境,所以绝大部分框架都用不了(如Flutter/skia等),最后发现Slint最合适。Slint有三种渲染器{femtovg/Op......
  • Rust学习之Diesel setup报错解决
    Dieselsetup报错解决Diesel是一个安全、可扩展的RustORM和查询生成器。Diesel是Rust中与数据库交互最高效的方式,因为它对查询进行了安全且可组合的抽象。1.报错信息diesel_demoonmaster[?]via......
  • OpenHarmony—类型转换仅支持as T语法
    规则:arkts-as-casts级别:错误在ArkTS中,as关键字是类型转换的唯一语法,错误的类型转换会导致编译时错误或者运行时抛出ClassCastException异常。ArkTS不支持使用语法进行类型转换。当需要将primitive类型(如number或boolean)转换成引用类型时,请使用new表达式。TypeScriptclassSha......
  • 2024.01.25 近期练习
    CF1856E2如果\(n\le5000\)考虑怎么做。首先我们对于每个节点只考虑大小关系,最后只需要从小到大标号即可。我们考虑把答案放到LCA处统计。若其只有两个儿子\(v_1,v_2\),那最多只有\(siz_{v_1}\timessiz_{v_2}\)个会被统计,即令\(v_1\)所有数大于\(v_2\)。若有多个儿......
  • 您有一份OpenHarmony开发者论坛2023年度总结,请查收~
    2023年11月,OpenHarmony开发者论坛1.0版本正式上线。 感谢各位开发者对OpenHarmony的大力支持和热爱,成为OpenHarmony开发者论坛的第一批体验用户,并迅速在论坛开启了OpenHarmony技术交流。  通过开发者们在论坛进行提问、答疑、分享技术文章、技术资料等方式......