HarmonyOS实战开发-方舟运行时使用指南

火影  金牌会员 | 2024-8-25 15:41:07 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 544|帖子 544|积分 1632

方舟编译器简介

方舟编译器(ArkCompiler)是为支持多种编程语言、多种芯片平台的联合编译、运行而计划的同一编译运行时平台。


  • ArkCompiler主要分成两个部分:编译工具链与运行时。 ArkCompiler eTS Runtime主要由四个子体系构成:Core
  • Subsystem、Execution Subsystem、Compiler Subsystem、Runtime subsystem。
  • ArkCompiler 的计划特点:原生支持类型、并发模型优化与并发API、安全。
环境搭建和编译

环境配置
搭建Ubuntu环境


  • 初始环境软件安装(Ubuntu版本推荐18.04或20.04)
  1. sudo apt-get update
  2. sudo apt-get upgrade
  3. sudo apt-get install git-lfs git bison flex gnupg build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses-dev x11proto-core-dev libx11-dev libc++1 lib32z1-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip m4 libtinfo5 bc npm genext2fs liblz4-tool libssl-dev ruby openjdk-8-jre-headless gdb python3-pip libelf-dev libxcursor-dev libxrandr-dev libxinerama-dev
复制代码
代码编译

  1. 注意:下列命令需在源码根目录下执行
复制代码
1.初次编译:
  1. ./build.sh --product-name rk3568
复制代码
2.初次编译后增量编译方舟运行时:
编译linux-x86版本:
  1. ./build.sh --product-name rk3568
  2. --build-target ark_js_host_linux_tools_packages
复制代码
编译oh-arm64版本:
  1. ./build.sh --product-name rk3568
  2. --gn-args use_musl=true --target-cpu arm64 --build-target ark_js_packages
复制代码
编译oh-arm32版本:
  1. ./build.sh --product-name rk3568
  2. --build-target  ark_js_packages
复制代码
3.初次编译后增量编译方舟前端:
  1. ./build.sh --product-name rk3568
  2. --build-target ets_frontend_build
复制代码
阐明:上述编译命令为release版本,编译debug版本需增加编译选项:–gn-args is_debug=true。
方舟相干的二进制文件在如下路径:
  1. out/rk3568/arkcompiler/runtime_core/
  2. out/rk3568/arkcompiler/ets_frontend/
  3. out/rk3568/arkcompiler/ets_runtime/
  4. out/rk3568/clang_x64/arkcompiler/runtime_core/
  5. out/rk3568/clang_x64/arkcompiler/ets_frontend/
  6. out/rk3568/clang_x64/arkcompiler/ets_runtime
复制代码
开发实例

本章节将介绍基于方舟运行时的开发测试实例。
HelloWorld
运行前准备


  • 编译编译方舟运行时和方舟前端
  1. ./build.sh --product-name rk3568
  2. --build-target ark_js_host_linux_tools_packages --build-target ets_frontend_build
复制代码
运行hello-world.js
新建hello-world.js文件,写入以下源码:
  1. print("Hello World!!!
  2. ");
复制代码
运行步骤:
1.通过方舟前端天生hello-world.abc文件,编译命令:
  1. /your_code_path/out/rk3568/clang_x64/arkcompiler/ets_frontend/es2abc hello-world.js
复制代码
2.执行hello-world.abc文件:
1.设置搜索路径:
  1. export LD_LIBRARY_PATH=/your_code_path/out/rk3568/clang_x64/arkcompiler/ets_runtime:/your_code_path/prebuilts/clang/ohos/linux-x86_64/llvm/lib:/your_code_path/out/rk3568/clang_x64/thirdparty/zlib
复制代码
2.执行ark_js_vm:
  1. /your_code_path/out/rk3568/clang_x64/arkcompiler/ets_runtime/ark_js_vm hello-world.abc
复制代码
执行效果如下:
  1. Hello World!!!
复制代码
阐明:此处“your_code_path”为源码目录路径。
反汇编hello-world.abc
编译天生反汇编工具:
  1. ./build.sh --product-name rk3568
  2. --build-target arkcompiler/runtime_core:ark_host_linux_tools_packages
复制代码
执行如下命令,效果输出到output.pa文件中:
  1. /your_code_path/out/rk3568/clang_x64/arkcompiler/runtime_core/ark_disasm hello-world.abc output.pa
复制代码
hello-world.abc反汇编效果如下:
  1. ## source binary: hello-world.abc## ====================# LITERALS# ====================# RECORDS.record _ESAnnotation <external>.record _ESModuleMode {        u8 isModule}# ====================# METHODS.function any func_main_0_any_any_any_any_(any a0, any a1, any a2) <static> {        mov v2, a2        mov v1, a1        mov v0, a0        builtin.acc        sta v5        builtin.idi "print", 0x0 // 加载print函数        sta v3        lda.str "Hello World!!!
  2. "  // 加载Hello World!!!
  3. 字符串        sta v4        builtin.tern3 v3, v4  // 调用print函数        builtin.acc}
复制代码
运行Test262测试用例

运行前准备
1.编译方舟运行时,编译命令:
  1. ./build.sh --product-name rk3568
  2. --build-target ark_js_host_linux_tools_packages
复制代码
2.编译方舟前端,编译命令:
  1. ./build.sh --product-name rk3568
  2. --build-target ets_frontend_build
复制代码
阐明:编译命令执行路径为项目根目录。
运行Test262

运行run_test262.py脚本,下载及运行Test262用例。
命令行格式:
  1. python3 test262/run_test262.py [options]
复制代码
执行路径为:项目根目录/arkcompiler/ets_frontend。

测试运行示例


  • 运行ES51测试用例:
  1. python3 test262/run_test262.py --es51
复制代码


  • 仅运行ES2015测试用例:
  1. python3 test262/run_test262.py --es2015
复制代码


  • 仅运行ES2021测试用例:
  1. python3 test262/run_test262.py --es2021 only
复制代码


  • 运行ES2015和ES51和ES2021所有测试用例:
  1. python3 test262/run_test262.py --es2021 all
复制代码


  • 运行单一测试用例:
  1. python3 test262/run_test262.py --file test262/data/test_es5/language/statements/break/12.8-1.js
复制代码


  • 运行某目录下所有测试用例:
  1. python3 test262/run_test262.py --dir test262/data/test_es5/language/statements
复制代码


  • 使用babel把单个测试用例转换成es5后再运行:
  1. python3 test262/run_test262.py  --babel --file test262/data/test_es5/language/statements/break/12.8-1.js
复制代码
测试输出

Test262所有用例的测试效果位于项目根目录/arkcompiler/ets_frontend/out下。shell中测试输出效果如下:
  1. $python3 test262/run_test262.py --file test262/data/test_es2015/built-ins/Array/15.4.5.1-5-1.js
  2. Wait a moment..........
  3. Test command:
  4. node
  5.         test262/harness/bin/run.js
  6.         --hostType=panda
  7.         --hostPath=python3
  8.         --hostArgs='-B test262/run_sunspider.py --ark-tool=/your_code_path/out/rk3568/clang_x64/arkcompiler/ets_runtime/ark_js_vm --ark-frontend-tool=/your_code_path/out/rk3568/clang_x64/arkcompiler/ets_frontend/build/src/index.js --libs-dir=/your_code_path/out/rk3568/clang_x64/global/i18n:/your_code_path/prebuilts/clang/ohos/linux-x86_64/llvm/lib:/your_code_path/out/rk3568/clang_x64/thirdparty/zlib/ --ark-frontend=ts2panda'
  9.         --threads=15
  10.         --mode=only strict mode
  11.         --timeout=60000
  12.         --tempDir=build/test262
  13.         --test262Dir=test262/data
  14.         --saveCompiledTests
  15.         test262/data/test_es5/language/statements/break/12.8-1.js
  16. PASS test262/data/test_es2015/built-ins/Array/15.4.5.1-5-1.js (strict mode)
  17. Ran 1 tests
  18. 1 passed
  19. 0 failed
  20. used time is: 0:01:04.439642
复制代码
AOT执行

运行前准备


  • 编译天生AOT编译器:
  1. ./build.sh --product-name rk3568
  2.   --build-target ets_frontend_build --build-target ark_js_host_linux_tools_packages --build-target arkcompiler/runtime_core:ark_host_linux_tools_packages
复制代码


  • 新建hello-world.ts文件,写入以下源码:
  1. declare function print(arg:any):string;print('Hello World!!!
  2. ')
复制代码
运行步骤

1.通过方舟前端天生hello-world.abc文件,编译命令:
  1. /your_code_path/out/rk3568/clang_x64/arkcompiler/ets_frontend/es2abc --module --merge-abc hello-world.ts
复制代码
2.设置搜索路径:
  1. export LD_LIBRARY_PATH=/your_code_path/out/rk3568/clang_x64/arkcompiler/ets_runtime:/your_code_path/prebuilts/clang/ohos/linux-x86_64/llvm/lib:/your_code_path/out/rk3568/clang_x64/thirdparty/icu:/your_code_path/out/rk3568/clang_x64/thirdparty/zlib
复制代码
3.运行ark_js_vm,采集PGO信息,天生的PGO信息落盘在文件hello-world.ap:
  1. /your_code_path/out/rk3568/clang_x64/arkcompiler/ets_runtime/ark_js_vm --enable-pgo-profiler=true --compiler-pgo-profiler-path=hello-world.ap --entry-point=hello-world hello-world.abc
复制代码
4.通过AOT编译器天生an和ai文件,可以通过info日志观察到被编译的函数列表:
  1. /your_code_path/out/rk3568/clang_x64/arkcompiler/ets_runtime/ark_aot_compiler  --enable-pgo-profiler=true --compiler-pgo-profiler-path=hello-world.ap --log-level=info --aot-file=./hello-world hello-world.abc
复制代码
5.执行ark_js_vm:
  1. /your_code_path/out/rk3568/clang_x64/arkcompiler/ets_runtime/ark_js_vm --aot-file=./hello-world --entry-point=hello-world hello-world.abc
复制代码
执行效果如下:
  1. Hello World!!!
复制代码
阐明:此处“your_code_path”为源码目录路径。
工具链使用

方舟前端工具采用命令行交互方式,支持将ArkTS代码转换为方舟字节码,使其可以或许在方舟运行时上运行。支持Windows/Linux/Mac平台。
ArkTS字节码编译工具概述

使用前端工具将ArkTS文件转换为方舟字节码文件。方舟前端工具在linux平台上可通过全量编译或指定编译前端工具链获取。
构建编译:
  1. $ ./build.sh --product-name rk3568
  2. --build-target ets_frontend_build
复制代码
  1. $ cd out/rk3568/clang_x64/arkcompiler/ets_frontend/
  2. $ ./es2abc [options] file.js
复制代码

反汇编器工具概述

工具名称为ark_disasm,用于将二进制格式的方舟字节码文件转换为文本格式的方舟字节码文件。
编译天生反汇编工具:
  1. ./build.sh --product-name rk3568
  2. --build-target arkcompiler/runtime_core:ark_host_linux_tools_packages
复制代码
命令行格式:
  1. ark_disasm [选项] 输入文件 输出文件
复制代码

输入文件:二进制格式的方舟字节码
输出文件:文本格式的方舟字节码
AOT工具概述

AOT为Ahead Of Time的简称,也即提前编译,可以或许在Host端将字节码提前编译成Target端可运行的机器码,如许字节码可以获得充分编译优化,从而加快Target端的运行速率。
编译天生aot编译器:
  1. ./build.sh --product-name rk3568
  2. --build-target ark_js_host_linux_tools_packages
复制代码
命令行格式:
  1. ark_aot_compiler [选项] 输入文件
复制代码

输入文件:二进制格式的方舟字节码
输出文件:直接执行的机器码的an文件、存储序列化后ConstPool的ai文件(输出文件路径需使用–aot-file选项指定)
PGO工具概述

PGO为Profile-guided optimization的简称,也即配置文件引导的优化。该工具可以或许纪录应用启动和性能场景中的高频(热点)函数,并将信息纪录于对应的PGO Profiler文件。AOT编译器则可通过这些信息决策部分函数进行编译,从而在基本不影相应用运行性能的情况下,缩短编译时间,减少.an文件的大小。
编译天生aot编译器和ark js虚拟机
  1. ./build.sh --product-name rk3568
  2. --build-target ark_js_host_linux_tools_packages
复制代码
天生PGO Profiler文件

命令行格式:
  1. ark_js_vm [选项] 输入文件
复制代码

输入文件:二进制格式的方舟字节码
输出文件:生存有热点函数信息的ap文件
基于PGO Profiler的AOT编译

命令行格式:
  1. ark_aot_compiler [选项] 输入文件
复制代码

输入文件:二进制格式的方舟字节码,ap文件
输出文件:直接执行的机器码的an文件(基于pgo)
如果各人还没有把握鸿蒙,现在想要在最短的时间里吃透它,我这边特意整理了《鸿蒙语法ArkTS、TypeScript、ArkUI等…视频教程》以及《鸿蒙开发学习手册》(共计890页),希望对各人有所资助:https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3

鸿蒙语法ArkTS、TypeScript、ArkUI等…视频教程:https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3


OpenHarmony APP应用开发教程步骤:https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3


《鸿蒙开发学习手册》:

如何快速入门:https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3

1.基本概念
2.构建第一个ArkTS应用
3.……


开发基础知识:https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3

1.应用基础知识
2.配置文件
3.应用数据管理
4.应用安全管理
5.应用隐私掩护
6.三方应用调用管控机制
7.资源分类与访问
8.学习ArkTS语言
9.……


基于ArkTS 开发:https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3

1.Ability开发
2.UI开发
3.公共事件与通知
4.窗口管理
5.媒体
6.安全
7.网络与链接
8.电话服务
9.数据管理
10.后台任务(Background Task)管理
11.设备管理
12.设备使用信息统计
13.DFX
14.国际化开发
15.折叠屏系列
16.……


鸿蒙生态应用开发白皮书V2.0PDF:https://docs.qq.com/doc/DZVVkRGRUd3pHSnFG



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

火影

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表