美文网首页
鸿蒙开发之- 配置Rust 的NAPI开发环境

鸿蒙开发之- 配置Rust 的NAPI开发环境

作者: xiaolei123 | 来源:发表于2024-06-13 23:25 被阅读0次

鸿蒙开发之- 配置Rust 的NAPI开发环境

1.安装Rust

  • 1.1 配置环境变量
export RUSTUP_DIST_SERVER="https://rsproxy.cn"
export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"
  • 1.2.安装 Rust
curl --proto '=https' --tlsv1.2 -sSf https://rsproxy.cn/rustup-init.sh | sh

2.配置 .cargo/config.toml

[source.crates-io]
replace-with = 'rsproxy-sparse'
[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"
[source.rsproxy-sparse]
registry = "sparse+https://rsproxy.cn/index/"
[registries.rsproxy]
index = "https://rsproxy.cn/crates.io-index"
[net]
git-fetch-with-cli = true

3.添加Rust的鸿蒙的编译目标

rustup target add aarch64-unknown-linux-ohos
rustup target add armv7-unknown-linux-ohos
rustup target add x86_64-unknown-linux-ohos

4. 配置鸿蒙的Native的环境变量

export OHOS_NDK_HOME=/Users/xiaolei/OpenHarmony/sdk/HarmonyOS-NEXT-DB1/openharmony

5.再次配置 .cargo/config.toml 鸿蒙的Native编译环境

这一段配置,添加到 config.toml 尾部

[target.aarch64-unknown-linux-ohos]
cc = { path = "$OHOS_NDK_HOME/native/llvm/bin/aarch64-unknown-linux-ohos-clang" }
cxx = { path = "$OHOS_NDK_HOME/native/llvm/bin/aarch64-unknown-linux-ohos-clang++" }
ar = "$OHOS_NDK_HOME/native/llvm/bin/llvm-ar"
ranlib = { path = "$OHOS_NDK_HOME/native/llvm/bin/llvm-ranlib" }
linker = "/Users/xiaolei/OpenHarmony/sdk/HarmonyOS-NEXT-DB1/openharmony/native/llvm/bin/aarch64-unknown-linux-ohos-clang"

[target.armv7-unknown-linux-ohos]
cc = { path = "$OHOS_NDK_HOME/native/llvm/bin/armv7-unknown-linux-ohos-clang" }
cxx = { path = "$OHOS_NDK_HOME/native/llvm/bin/armv7-unknown-linux-ohos-clang++" }
ar = "$OHOS_NDK_HOME/native/llvm/bin/llvm-ar"
ranlib = { path = "$OHOS_NDK_HOME/native/llvm/bin/llvm-ranlib" }
linker = "/Users/xiaolei/OpenHarmony/sdk/HarmonyOS-NEXT-DB1/openharmony/native/llvm/bin/armv7-unknown-linux-ohos-clang"

[target.x86_64-unknown-linux-ohos]
cc = { path = "$OHOS_NDK_HOME/native/llvm/bin/x86_64-unknown-linux-ohos-clang" }
cxx = { path = "$OHOS_NDK_HOME/native/llvm/bin/x86_64-unknown-linux-ohos-clang++" }
ar = "$OHOS_NDK_HOME/native/llvm/bin/llvm-ar"
ranlib = { path = "$OHOS_NDK_HOME/native/llvm/bin/llvm-ranlib" }
linker = "/Users/xiaolei/OpenHarmony/sdk/HarmonyOS-NEXT-DB1/openharmony/native/llvm/bin/x86_64-unknown-linux-ohos-clang"

6. 创建Lib项目

  • 6.1 创建项目
cargo new <库名称> --lib
  • 6.2 配置最终目标为动态库
# Cargo.toml

[lib]
crate-type=["cdylib"]

7.添加依赖

cargo add napi-ohos napi-derive-ohos
cargo add napi-build-ohos --build

8.编辑项目文件

  • 8.1 新增build.rs文件在根目录下
// build.rs
fn main () {
    napi_build_ohos::setup();
}

项目结构如下:

.
├── Cargo.lock
├── Cargo.toml
├── build.rs
└── src
    └── lib.rs
  • 8.2 修改 src/lib.rs文件
use napi_derive_ohos::napi;

#[napi]
pub fn add(a: u32,b: u32) -> u32 {
    a + b
}

9.编译鸿蒙目标动态库

  • 9.1 普通编译
 build --target aarch64-unknown-linux-ohos
  • 9.2 编译release
 build --target aarch64-unknown-linux-ohos --release
  • 9.3 修改Cargo.toml优化产物大小

在项目内Cargo.toml尾部追加:

[profile.release]
strip = true  # 自动从二进制文件去除符号信息.
lto = true # 删除无用代码,并且通常可以减小二进制
opt-level = "z"  #  二进制文件大小优化

10.重新编译即可

End.

相关文章

网友评论

      本文标题:鸿蒙开发之- 配置Rust 的NAPI开发环境

      本文链接:https://www.haomeiwen.com/subject/tlboqjtx.html