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编译。

相关推荐
国科安芯8 小时前
国产RISC-V架构MCU在工控系统中的节能性分析
网络·单片机·嵌入式硬件·fpga开发·性能优化·架构·risc-v
遇到困难睡大觉哈哈12 小时前
HarmonyOS —— Remote Communication Kit 拦截器(Interceptor)高阶定制能力笔记
笔记·华为·harmonyos
遇到困难睡大觉哈哈13 小时前
HarmonyOS —— Remote Communication Kit 定制处理行为(ProcessingConfiguration)速记笔记
笔记·华为·harmonyos
氤氲息13 小时前
鸿蒙 ArkTs 的WebView如何与JS交互
javascript·交互·harmonyos
遇到困难睡大觉哈哈13 小时前
HarmonyOS支付接入证书准备与生成指南
华为·harmonyos
赵浩生13 小时前
鸿蒙技术干货10:鸿蒙图形渲染基础,Canvas绘图与自定义组件实战
harmonyos
赵浩生13 小时前
鸿蒙技术干货9:deviceInfo 设备信息获取与位置提醒 APP 整合
harmonyos
BlackWolfSky14 小时前
鸿蒙暂未归类知识记录
华为·harmonyos
L、21816 小时前
Flutter 与开源鸿蒙(OpenHarmony):跨平台开发的新未来
flutter·华为·开源·harmonyos