1# 轻量级编译构建组件<a name="ZH-CN_TOPIC_0000001130006475"></a> 2 3- [简介](#section11660541593) 4- [目录](#section161941989596) 5- [说明](#section1312121216216) 6 - [使用说明](#section129654513264) 7 8- [相关仓](#section1371113476307) 9 10## 简介<a name="section11660541593"></a> 11 12一个基于gn和ninja的支持OpenHarmony组件化开发的编译框架,主要提供以下功能: 13 14- 构建产品。 15 16- 独立构建芯片厂商组件。 17- 独立构建单个组件。 18 19在开发编译构建前,应了解如下基本概念: 20 21- 组件 22 23 可复用的软件单元,它可包含源码、配置文件、资源文件和编译脚本等。 24 25- gn 26 27 Generate ninja的缩写,一种元构建系统,用于产生ninja文件。 28 29- ninja 30 31 ninja是一个专注于速度的小型构建系统。 32 33 34构建流程如下: 35 36编译构建流程如图1所示,主要包括设置和编译两步: 37 38**图 1** 编译构建流程<a name="fig1531311552204"></a> 39 40 411. hb set: 设置OpenHarmony源码目录和要编译的产品。 422. hb build: 编译产品、开发板或者组件。解决方案编译实现如下: 43 - 读取开发板配置:主要包括开发板使用的编译工具链、编译链接命令和选项等。 44 - 调用gn: 调用gn gen命令,读取产品配置\(主要包括开发板、内核、选择的组件等\)生成解决方案out目录和ninja文件。 45 - 调用ninja:调用ninja -C out/company/product启动编译。 46 - 系统镜像打包:将组件编译产物打包,制作文件系统镜像。 47 48 49## 目录<a name="section161941989596"></a> 50 51``` 52build/lite # 编译构建主目录 53├── components # 组件描述文件。 54├── hb # hb pip安装包源码。 55├── make_rootfs # 文件系统制作脚本。 56├── config # 编译相关的配置项 57│ ├── component # 组件相关的模板定义。包括:静态库、动态库、扩展组件、模拟器库等 58│ ├── kernel # 内核的编译配置参数 59│ └── subsystem # 子系统模板 60├── ndk # Native API相关编译脚本与配置参数 61└── toolchain # 编译工具链相关,包括:编译器路径、编译选项、链接选项等。 62``` 63 64## 说明<a name="section1312121216216"></a> 65 66### 使用说明<a name="section129654513264"></a> 67 681. **前提条件** 69 - Linux服务器,Ubuntu16.04及以上64位系统版本。 70 - Python 3.7.4及以上。 71 - OpenHarmony源码build\_lite仓下载成功。 72 732. **安装hb** 74 - 在源码根目录下执行: 75 76 ``` 77 python3 -m pip install --user build/lite 78 ``` 79 80 - 执行hb -h有相关帮助信息,有打印信息即表示安装成功: 81 82 ``` 83 usage: hb 84 85 OHOS build system 86 87 positional arguments: 88 {build,set,env,clean} 89 build Build source code 90 set OHOS build settings 91 env Show OHOS build env 92 clean Clean output 93 94 optional arguments: 95 -h, --help show this help message and exit 96 ``` 97 98 - 卸载方法: 99 100 ``` 101 python3 -m pip uninstall ohos-build 102 ``` 103 104 1053. **编译命令** 106 1. **hb set** 107 108 ``` 109 hb set -h 110 usage: hb set [-h] [-root [ROOT_PATH]] [-p] 111 112 optional arguments: 113 -h, --help Show this help message and exit. 114 -root [ROOT_PATH], --root_path [ROOT_PATH] 115 Set OHOS root path. 116 -p, --product Set OHOS board and kernel. 117 ``` 118 119 - hb set 后无参数,进入默认设置流程 120 - hb set -root \[ROOT\_PATH\] 直接设置代码根目录 121 - hb set -p --product 设置要编译的产品 122 123 2. **hb env** 124 125 查看当前设置信息 126 127 ``` 128 hb env 129 [OHOS INFO] root path: xxx 130 [OHOS INFO] board: hispark_taurus 131 [OHOS INFO] kernel: liteos 132 [OHOS INFO] product: ipcamera 133 [OHOS INFO] product path: xxx/vendor/hisilicon/ipcamera 134 [OHOS INFO] device path: xxx/device/hisilicon/hispark_taurus/sdk_linux_4.19 135 ``` 136 137 3. **hb build** 138 139 ``` 140 hb build -h 141 usage: hb build [-h] [-b BUILD_TYPE] [-c COMPILER] [-t [TEST [TEST ...]]] 142 [--dmverity] [-p PRODUCT] [-f] [-n] 143 [component [component ...]] 144 145 positional arguments: 146 component Name of the component. 147 148 optional arguments: 149 -h, --help Show this help message and exit. 150 -b BUILD_TYPE, --build_type BUILD_TYPE 151 Release or debug version. 152 -c COMPILER, --compiler COMPILER 153 Specify compiler. 154 -t [TEST [TEST ...]], --test [TEST [TEST ...]] 155 Compile test suit. 156 --dmverity Enable dmverity. 157 -p PRODUCT, --product PRODUCT 158 Build a specified product with 159 {product_name}@{company}, eg: ipcamera@hisilcon. 160 -f, --full Full code compilation. 161 -T [TARGET [TARGET ...]], --target [TARGET [TARGET ...]] 162 Compile single target 163 ``` 164 165 - hb build后无参数,会按照设置好的代码路径、产品进行编译,编译选项使用与之前保持一致。 166 - hb build component:基于设置好的产品对应的单板、内核,单独编译组件(e.g.:hb build kv\_store\)。 167 - hb build -p PRODUCT:免set编译产品,该命令可以跳过set步骤,直接编译产品。 168 - 在device/device\_company/board下单独执行hb build会进入内核选择界面,选择完成后会根据当前路径的单板、选择的内核编译出仅包含内核、驱动的镜像。 169 170 4. **hb clean** 171 172 清除out目录对应产品的编译产物,仅剩下args.gn、build.log。清除指定路径可输入路径参数:hb clean xxx/out/xxx,否则将清除hb set的产品对应out路径 173 174 ``` 175 hb clean 176 usage: hb clean [-h] [out_path] 177 178 positional arguments: 179 out_path Clean a specified path. 180 181 optional arguments: 182 -h, --help Show this help message and exit. 183 ``` 184 185 186 187## 相关仓<a name="section1371113476307"></a> 188 189**[编译构建子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/编译构建子系统.md)** 190 191**[build](https://gitee.com/openharmony/build/blob/master/README_zh.md)**