【入门到实战】OpenHarmony南向开发:Rust编译构建指南-Cargo2gn工具操作

往期鸿蒙5.0全套实战文章必看:(文中附带全栈鸿蒙5.0学习资料)


Cargo2gn工具操作

概述

rust三方库使用cargo编译,配置为Cargo.toml。集成到OpenHarmony上需要转换成BUILD.gn规则。为了满足这个需求,需要提供一个cargo2gn转换工具。当需要引入rust三方crate时使用cargo2gn转换工具来把三方库的Cargo.toml转换成BUILD.gn规则。cargo2gn可以单个库进行转换,也可以多个库进行批量转换。

单个库转换操作步骤

  1. 进入到需要转化的rust三方库的目录下,比如需要转化bindgen。

    cd openharmony/third_party/rust/bindgen
    
  2. 创建配置文件cargo2gn.json,可以参考如下配置。

    {
        "copy-out": true,
        "run": true,
        "add-workspace": true,
        "cargo-bin": "/mnt/xxx/openharmony/prebuilts/rustc/linux-x86_64/current/bin"
    }
    
  3. 执行以下命令进行转换。

    python3 /mnt/xxx/openharmony/build/scripts/cargo2gn.py --config cargo2gn.json
    

    转换结果

    import("//build/templates/rust/ohos_cargo_crate.gni")
    
    ohos_cargo_crate("lib") {
        crate_name = "bindgen"
        crate_type = "rlib"
        crate_root = "./lib.rs"
    
        sources = ["./lib.rs"]
        edition = "2018"
        cargo_pkg_version = "0.64.0"
        cargo_pkg_authors = "Jyun-Yan You <jyyou.tw@gmail.com>,  Emilio Cobos Álvarez <emilio@crisal.io>,  Nick Fitzgerald <fitzgen@gmail.com>,  The Servo project developers"
        cargo_pkg_name = "bindgen"
        cargo_pkg_description = "Automatically generates Rust FFI bindings to C and C++ libraries."
        deps = [
            "//third_party/rust/bitflags:lib",
            "//third_party/rust/cexpr:lib",
            "//third_party/rust/clang-sys:lib",
            "//third_party/rust/lazy_static:lib",
            "//third_party/rust/lazycell:lib",
            "//third_party/rust/log:lib",
            "//third_party/rust/peeking_take_while:lib",
            "//third_party/rust/proc-macro2:lib",
            "//third_party/rust/quote:lib",
            "//third_party/rust/regex:lib",
            "//third_party/rust/rustc-hash:lib",
            "//third_party/rust/shlex:lib",
            "//third_party/rust/syn:lib",
            "//third_party/rust/which:lib",
        ]
        features = [
            "default",
            "log",
            "logging",
            "static",
            "which",
            "which-rustfmt",
        ]
        build_root = "build.rs"
        build_sources = ["build.rs"]
        build_script_outputs = ["host-target.txt"]
    }
    

多个库批量转换操作步骤

  1. 进入到rust目录下。

    cd openharmony/third_party/rust
    
  2. 把所有需要转换的rust三方库添加到rust目录下的Cargo.toml的[workspace]里,如下所示。

    [workspace]
    members = [
        "aho-corasick",
        "memchr",
    ]
    
  3. 执行单个库转换操作步骤的2和3。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值