OpenHarmony编译构建系统[浅谈与实践] 原创 精华
OpenHarmony编译构建系统[浅谈与实践]
前言
 经过一段时间的南向学习,基于Hi3861智能家居开发套件的内核编程,驱动开发已经基本解决了。这篇来聊聊OpenHarmony的编译构建,经过前面的实践,再来看编译构建。会对之前的编译流程做一些解释,实践一个基于Hispark_pegasus的自己的解决方案。
编译构建概述
 在官网中提到了,OpenHarmony编译子系统是以GN和Ninja构建为基座,对构建和配置粒度进行部件化抽象、对内建模块进行功能增强、对业务模块进行功能扩展的系统,该系统提供以下基本功能:
- 以部件为最小粒度拼装产品和独立编译。
 - 支持轻量、小型、标准三种系统的解决方案级版本构建,以及用于支撑应用开发者使用IDE开发的SDK开发套件的构建。
 - 支持芯片解决方案厂商的灵活定制和独立编译。
 
hb、GN、Ninja
		回想我们在OpenHarmony搭建编译环境的时候,进行了编译操作是怎么进行的了吗?首先是hb set 选择了wifiiot_hispark_pegasus,然后进行了全量编译操作hb build -f 。
hb set
 选择产品或者说选择一个编译的目录,我们可以自己创建自己的产品,哪怕他只有一个hello,world的功能。而其他的产品或者说代码都不会参与编译,这也解释了什么是最小的产品独立编译。编译什么是我们手动选择的,功能可大可小。
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/d56e35e3479b0b41f0390871bc44be100ba18a.png?x-oss-process=image/resize,w_820,h_460)
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/f3dc3fc54b1f260a38d696db3136aa0414006b.png?x-oss-process=image/resize,w_820,h_70)
hb build
 编译指定的产品(代码),根据指定的产品开发板,读取开发板config.gni文件的内容,主要是一些编译工具链和编译的配置选项。
我们也可以用-T修饰命令,让他只编译某一个源文件
hb build -T 路径:目标
BUILD.gn
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/b9c3734090938b95e85998439e2764e5373ed2.png?x-oss-process=image/resize,w_670,h_236)
 这个文件应该说很熟悉了,每一个案例都要去写这个gn文件,gn是Generate ninja的缩写,用于产生ninja文件。在我们之前简单案例的开发中,如“hello,world”,gn文件就是一个编译脚本。
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/f75b1fa797fc6f8863d692649e5913fd4f90f9.png?x-oss-process=image/resize,w_257,h_266)
 我们对nijia的印象不是很深,因为他是自动执行的,我们作为开发者没有去人工干涉他。
编译小总结
 总结来说,hb就是OpenHarmony的命令行工具,用来执行编译命令。gn生成nijia文件,nijia是一个专注于速度的小型编译构建系统。他们三者在整个编译中的流程如下图所示:
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/716b4c50435dfc9e69325094b20538ebe7c1ab.png?x-oss-process=image/resize,w_763,h_594)
整个编译构建的流程图如下:
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/124426604c2f3dde6f663507827e55a538be97.png?x-oss-process=image/resize,w_820,h_819)
OpenHarmony系统
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/765928e76f0f66cc4371646d814d258cac1ead.png?x-oss-process=image/resize,w_734,h_464)
 OpenHarmony整体遵从分层设计,系统功能按照“系统 > 子系统 > 组件”逐级展开,在多设备部署场景下,支持根据实际需求裁剪某些非必要的子系统或部件,非常的灵活,高内聚低耦合。
配置规则
组件配置规则
遵循:{领域(子系统集)}/{子系统}/{组件}的一个规则,从下面的源码中可以看出:
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/e1ca9cb7390d6f33fa4483e28ab5ce9a02c7ac.png?x-oss-process=image/resize,w_533,h_721)
组件定义
组件定义在build/lite/components/下:
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/b7016fe51d1a210dc6a4953ce65c5c940ef5be.png?x-oss-process=image/resize,w_536,h_1106)
 定义就是一个JSON文件,由一个总的components数组包含每一个component对象,对象中包含了组件的所有属性。
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/07de32483fdf61ef17b701fd2ffb8e697084a6.png?x-oss-process=image/resize,w_820,h_1231)
 至此,我们知道怎么去定义组件,定义在哪里,也就能新建组件了。但是新出现的组件,怎么能后加入到编译中呢,targets参数其实已经说明清楚了,下面通过Wifi组件的案例做具体解释。
WiFi组件
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/b2ac6bd80de072f72d37470dd9fa73d8a7c0c8.png?x-oss-process=image/resize,w_820,h_1182)
 我们可以根据targets参数追踪到目录中/foundation/communication/wifi/BUILD.gn文件中的wifi
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/99b12de678beffdb58d6859b4c3cd9eaef57cd.png?x-oss-process=image/resize,w_820,h_1443)
$WIFI_ROOT_DIR表示/foundation/communication/wifi,之后继续跟踪,这些dependences,完成相应BUILD.gn脚本的执行,也就让组件被编译系统所识别,完成组件的编译了。
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/f183ae3420262bdeada674462c454b5452ee5c.png?x-oss-process=image/resize,w_820,h_1460)
组件总结
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/a5e305222cb4e15c7ab07304a2452ff1867fee.png?x-oss-process=image/resize,w_820,h_752)
芯片解决方案配置规则
芯片解决方案的路径如下图所示:
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/780e4ec674f4849a5826354a986a178b4951c4.png?x-oss-process=image/resize,w_657,h_642)
 芯片解决方案组件会随产品选择的开发板默认编译。
产品解决方案配置规则
 产品解决方案的路径如下图所示:
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/82b3eef717a56cfbb7494020853450ac196904.png?x-oss-process=image/resize,w_643,h_511)
 产品解决方案,在config.json文件中进行配置:
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/92e77315884ae86b728483671e64e1c45c2114.png?x-oss-process=image/resize,w_820,h_1463)
- “product_name”: 产品名称,指定为"wifiiot_hispark_pegasus"。
 - “type”: 产品类型,被标记为"mini"。
 - “version”: 产品版本号,标记为"3.0"。
 - “ohos_version”: 操作系统版本,使用的是OpenHarmony 1.0。
 - “device_company”: 设备制造公司,此产品由"hisilicon"制造。
 - “device_build_path”: 设备构建路径,指定为"device/board/hisilicon/hispark_pegasus"。
 - “board”: 开发板名称,被标记为"hispark_pegasus"。
 - “kernel_type”: 内核类型,使用的是"liteos_m"。
 - “kernel_is_prebuilt”: 内核是否预构建,被标记为true。
 - “kernel_version”: 内核版本号,此处为空。
 - “subsystems”: 子系统列表,包含了产品的不同子系统及其组件信息。
- “subsystem”: 子系统名称,表示不同的功能区域。
 - “components”: 组件列表,表示在该子系统中使用的组件及其特性。
- “component”: 组件名称,表示不同的功能组件。
 - “features”: 特性列表,描述了组件的不同特性。
 
 
 - “third_party_dir”: 第三方库路径,指定为"//device/soc/hisilicon/hi3861v100/sdk_liteos/third_party"。
 - “product_adapter_dir”: 产品适配层路径,指定为"//vendor/hisilicon/hispark_pegasus/hals"。
 
 最后,也就能看到我们的hb set从顶层,选择vendor下的产品解决方案,通过方案中的各个子系统集,子系统,组件,进行编译。
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/959b185721f06bfa5af0928081e6a840ed0f65.png?x-oss-process=image/resize,w_820,h_784)
新增自己的产品解决方案
组件定义
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/492a30990aceec1fcd10411c1e0b3ea3327b65.png?x-oss-process=image/resize,w_820,h_875)
- 首先,在application/sample下创建一个
myComponent等如下目录 
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/14d83b532a54e4820eb2877d284fa4558029f9.png?x-oss-process=image/resize,w_626,h_269)
- 完成组件功能的编写
 
component.c
#include <stdio.h>
#include "ohos_init.h"
void entry(void){
    printf("test component!"); // 哪怕这个解决方案是个hello,world呢
}
SYS_RUN(entry);
static_library(test){
    sources = [
        "component.c"
    ]
    include_dirs = [
        "//commonlibrary/utils_lite/include"
    ]
}
- 定义组件
 
		在build/lite/components/创建application1.json编写如下代码:
{
    "components": [
      {
        "component": "myComponent",
        "description": "a test component",
        "optional": "true",
        "dirs": [
          "applications/sample/myComponent"
        ],
        "targets": [
          "//applications/sample/myComponent:test"
        ],
        "adapted_kernel": [ "liteos_m" ]
        }
    ]
}
 我们可以使用 -T 修饰我们的编译命令,实现指定文件编译
hb build -f -T //applications/sample/myComponent:test
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/78366d3368d58ebb396184a976595875ec13e9.png?x-oss-process=image/resize,w_820,h_722)
 说明我们的组件编写没什么问题
解决方案定义
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/c8141fd44039d524b10707d29f43b721585e1e.png?x-oss-process=image/resize,w_820,h_876)
- 创建如下目录,并编写config.json配置文件
 
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/14088bc6902eb25cf78382141525de8cce05b6.png?x-oss-process=image/resize,w_648,h_254)
config.json
{
    "product_name": "product",
    "type": "mini",
    "version": "3.0",
    "ohos_version": "OpenHarmony 3.2",
    "device_company": "hisilicon",
    "device_build_path": "device/board/hisilicon/hispark_pegasus",
    "board": "hispark_pegasus",
    "kernel_type": "liteos_m",
    "kernel_is_prebuilt": true,
    "kernel_version": "",
    "subsystems": [
      {
        "subsystem": "applications1", // 用我们自己定义的子系统的组件
        "components": [
          { "component": "myComponent", "features":[] }
        ]
      },
      {
        "subsystem": "iothardware",
        "components": [
          { "component": "peripheral", "features":[] }
        ]
      },
      {
        "subsystem": "hiviewdfx",
        "components": [
          { "component": "hilog_lite", "features":[] },
          { "component": "hievent_lite", "features":[] },
          { "component": "blackbox", "features":[] },
          { "component": "hidumper_mini", "features":[] }
        ]
      },
      {
        "subsystem": "systemabilitymgr",
        "components": [
          { "component": "samgr_lite", "features":[] }
        ]
      },
      {
        "subsystem": "security",
        "components": [
          { "component": "device_auth", "features":[] },
          { "component": "huks", "features":
            [
              "disable_huks_binary = false",
              "disable_authenticate = false",
              "huks_use_lite_storage = true",
              "huks_use_hardware_root_key = true",
              "huks_config_file = \"hks_config_lite.h\"",
              "ohos_security_huks_mbedtls_porting_path = \"//device/soc/hisilicon/hi3861v100/sdk_liteos/third_party/mbedtls\""
            ]
          }
        ]
      },
      {
        "subsystem": "startup",
        "components": [
          { "component": "bootstrap_lite", "features":[] },
          { "component": "syspara_lite", "features":[] },
          { "component": "init_lite", "features":
            [
              "enable_ohos_startup_init_feature_begetctl_liteos = true",
              "enable_ohos_startup_init_lite_use_thirdparty_mbedtls = true"
            ]
          }
        ]
      },
      {
        "subsystem": "communication",
        "components": [
          { "component": "wifi_lite", "features":[] },
          { "component": "dsoftbus", "features":[] },
          { "component": "wifi_aware", "features":[]}
        ]
      },
      {
        "subsystem": "updater",
        "components": [
          { "component": "ota_lite", "features":[] }
        ]
      },
      {
        "subsystem": "commonlibrary",
        "components": [
          { "component": "file", "features":[] }
        ]
      },
      {
       "subsystem": "xts",
       "components": [
         { "component": "xts_acts", "features":
          [
            "enable_ohos_test_xts_acts_use_thirdparty_lwip = false"
          ]
         },
         { "component": "xts_tools", "features":[] },
         { "component": "device_attest_lite", "features":[] }
        ]
      }
    ],
    "third_party_dir": "//device/soc/hisilicon/hi3861v100/sdk_liteos/third_party",
    "product_adapter_dir": "//vendor/hisilicon/hispark_pegasus/hals"
  }
- 将hispark_pegasus下的hal/utils复制到我们自己的产品解决方案中
 
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/b51fc6d014da8f27ed86198c508724b2338d9c.png?x-oss-process=image/resize,w_649,h_495)
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/6465e6070e511171762527b5d97f91e563b543.png?x-oss-process=image/resize,w_405,h_187)
- 创建BUILD.gn文件编写编译脚本
 
group("product"){
    
}
编译检验
执行hb set命令,观察产品解决方案
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/46c65f516098dca8a45521205ffd508fe8bd73.png?x-oss-process=image/resize,w_820,h_1238)
完成编译
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/3947b6a671c3031e27d5599a7903ee577c41e8.png?x-oss-process=image/resize,w_820,h_770)
烧录测试
选择我们的产品解决方案product
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/d6670b582adab3b8e735977c10a2c185f96fe5.png?x-oss-process=image/resize,w_820,h_764)
串口调试,观察控制台输出
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/58e7ab427b49d1bb0906422af8a29a1adc8d3e.png?x-oss-process=image/resize,w_757,h_512)
产品解决方案总结
![OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区 OpenHarmony编译构建系统[浅谈与实践]-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202306/8588747993b73f0e87696886864bc5b2c78937.png?x-oss-process=image/resize,w_820,h_1468)
结束语
 希望能够帮助到大家,对OpenHarmony的编译过程有一个全面的感知。





















有时我常怀疑我和大佬们读的不是一个文档,太多细节都没关注到
感谢支持,距离大佬还有很大一段距离,我也得继续努力
不错不错,加油!