OpenHarmony4.1上初体验rust

OpenHarmony4.1上初体验rust


前言

由于后续可能要做rust基于OpenHarmony的开发工作,于是先开始一些调研性的工作,rust了解的内容可以参考该文章:
ubuntu20.04搭建RUST开发环境并与C语言交互


一、准备工作

OpenHarnmony4.1版本,有设备的话可以用riscv的荔枝派,rk3568的dayu系列开发板,没有设备的话可以用QEMU。

本文以下的尝试均基于riscv的OpenHarnmony4.1版本在荔枝派上进行。

整编OH版本并烧录至荔枝派设备,确保可以正常启动桌面。

二、初步调研

rust相关的目录主要有以下几个:

  1. build\rust:有一些rust相关的测试demo,比如和c、c++代码进行互调的测试等,可以使用以下命令进行编译,其中build-target可以使用build\rust\tests\BUILD.gn中的group。

./build.sh --product-name rvbook --no-prebuilt-sdk --build-target ohos_rust_xxxtest_tests

./build.sh --product-name rvbook --no-prebuilt-sdk --build-target cargo_crate_test

./build.sh --product-name rvbook --no-prebuilt-sdk --build-target cxx_test_host_and_target

  1. third_party\rust\crates 里面东西很多,需要慢慢看。
  2. build\templates\rust
  3. prebuilts\rustc-riscv
  4. 其他的发现以后再进行补充。

三、初步尝试

运行测试demo

使用上面的编译命令将相关的demo编译出后通过HDC命令将相关的进程和库文件拷贝至荔枝派设备的"/data "目录。

其中编译ohos_rust_xxxtest_tests 目标时会有如下报错:

应该是编译器限制的比较严格,不能有warning导致,如下修改代码即可

build\rust\tests\test_proc_macro_crate\test_derive_macro\src\main.rs

c 复制代码
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

extern crate derive_macro;
use derive_macro::AnswerFn;

#[derive(AnswerFn)]
#[allow(dead_code)] //新增此行
struct Struct;

fn main() {
    assert_eq!(42, answer());
}

生成的相关二进制位于out/build/build_framework/

在荔枝派上./运行,除了一个需要设置一下环境变量,其他都可以正常跑出结果。

export LD_LIBRARY_PATH=/data/build_framework

四、尝试构建自己的hello world

就在build/rust目录下开始编写自己的hello world。

  1. 新建目录,目录下新增main.rsbuild.rsBUILD.gn
    main.rs就只有一个println!,build.rs从test的其他demo直接拿过来用,BUILD.gn也可以仿照其他的demo。
  2. 修改build目录下的bundle.json,增加新的build-target。运行编译命令(其中rust_liusai为我新添加到bundle.json的group):

./build.sh --product-name rvbook --no-prebuilt-sdk --build-target rust_liusai

原因是"-D missing-docs",找一下解决方法。

把编译命令中的"-D missing-docs"直接删除,然后cd到目录out/rvbook编译,成功得到进程文件。HDC到设备上./运行:

成功。

五、与C、C++的交互

参考OH自带的文档

third_party\rust\crates\cxx\readme_zh.md

third_party\rust\crates\bindgen\readme_zh.md

docs\zh-cn\device-dev\subsystems\subsys-build-rust-compilation.md

明天搞出来以后进行补充。

总结

需要了解OopenHarmony,HDC,ninja编译体系构建。

熟悉rust语言,rust编译。

相关推荐
肖祥12 小时前
Rust入门
rust
坚果的博客12 小时前
技术解析:鸿蒙 PC 为什么采用 aarch64 架构?
华为·架构·harmonyos
林太白13 小时前
rust16-职位管理模块
后端·rust
ifeng091813 小时前
HarmonyOS实战项目:AI健康助手(影像识别与健康分析)
人工智能·华为·wpf·harmonyos
爱笑的眼睛1113 小时前
HarmonyOS NFC应用开发:构建分布式近场通信解决方案
华为·harmonyos
国服第二切图仔14 小时前
Rust开发实战之简单游戏开发(piston游戏引擎)
开发语言·rust·游戏引擎
星释19 小时前
鸿蒙Flutter三方库适配指南:09.版本升级适配
flutter·华为·harmonyos
史不了1 天前
静态交叉编译rust程序
开发语言·后端·rust
Johnny.Cheung1 天前
非常好的Rust自动管理内存的例子
rust·内存管理·析构函数
码界奇点1 天前
Rust 性能优化全流程从 flamegraph 定位瓶颈到 unsafe 与 SIMD 加速响应快
开发语言·性能优化·rust·simulated annealing