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

相关推荐
anyup2 小时前
uniapp开发的鸿蒙应用上架后,竟然月入4000+
前端·vue.js·harmonyos
松叶似针2 小时前
Flutter三方库适配OpenHarmony【doc_text】— .docx 解析全流程:从 ZIP 解压到 XML 提取
xml·flutter·harmonyos
shimly1234563 小时前
(done) 速通 rustlings(4) 变量声明
rust
shimly1234564 小时前
(done) 速通 rustlings(11) 向量vector及其操作
rust
shimly1234565 小时前
(done) 速通 rustlings(3) intro1 println!()
rust
shimly1234565 小时前
(done) 速通 rustlings(12) 所有权
rust
星空22235 小时前
【HarmonyOS】RN_of_HarmonyOS实战项目:TextInput表情符号输入
华为·harmonyos
浩宇软件开发5 小时前
基于DevEco鸿蒙开垃圾分类APP实现
harmonyos·arkts·arkui·垃圾分类·鸿蒙开发·deveco
钟睿5 小时前
HarmonyOS花瓣地图自定义点聚合功能
android·harmonyos·arkts
星空22236 小时前
鸿蒙跨平台实战day47:React Native在OpenHarmony上的Font自定义字体注册详解
react native·华为·harmonyos