请先关注 [低调大师] 公众号 优秀的自媒体个人博客,低调大师,许军

低调大师

您现在的位置是:首页>文章详情

文章详情

鸿蒙内核源码分析(编译环境篇) | 编译鸿蒙看这篇或许真的够了 | 百篇博客分析HarmonyOS源码 | v50.03

2021-04-23 57热度

百万汉字注解 >> 精读鸿蒙源码,中文注解分析, 深挖地基工程,大脑永久记忆,四大码仓每日同步更新< gitee | github | csdn | coding >

百篇博客分析 >> 故事说内核,问答式导读,生活式比喻,表格化说明,图形化展示,主流站点定期更新中< oschina | 51cto | csdn | harmony >

编译鸿蒙

sourecinsight多年的使用习惯,不舍得丢弃, 所以选择在windows + docker 编译鸿蒙. kernel_liteos_a_note

本篇记录编译鸿蒙的过程,以备后续不用再去一大堆无效的误导式软文中搜寻芝麻大点有用的信息,那样真挺费时费心力.

编译环境

先安装 Docker Desktop 下载windows版本一直下一步.

在windows下拉取openharmony-docker官方镜像,Docker方式获取编译环境 强烈推荐这么做.

docker pull swr.cn-south-1.myhuaweicloud.com/openharmony-docker/openharmony-docker:0.0.3 

2.36G, 拉取看网速, 大概10分钟后成功了,有了镜像

PS E:\harmony\kernel_liteos_a_note> docker images REPOSITORY TAG IMAGE ID CREATED SIZE swr.cn-south-1.myhuaweicloud.com/openharmony-docker/openharmony-docker 0.0.3 50d0aa6ea9ba 2 weeks ago 2.36GB 

vscode对docker的管理插件非常的强大,管理镜像和容器的工作就交给它了.

启动docker,创建好容器,本文的选择是这样的,当然大家可以灵活处理,命名.

kernel_liteos_a_note

容器创建成功后可以在 vscode 右键容器inspect查看到绑定的目录.

"HostConfig": { "Binds": [ "E:\\harmony\\code-1.0:/home/harmony", "E:\\harmony\\docker:/home/docker" ], 

本文这样做的目的为了在windows上能方便的查看文件.harmony目录用于下载编译源码目录,另外一个docker放其他无关文件 repo方式下载源码 进入容器,vscode右键容器 Attach shell,再进入/home/harmony目录

root@5e3abe332c5a:/home/harmony#repo init -u https://gitee.com/openharmony/manifest.git -b master --no-repo-verify .... root@5e3abe332c5a:/home/harmony#repo sync -c 

这个过程也是比网速.慢慢等吧. 下载完成之后的样子

root@5e3abe332c5a:/home/harmony# ls applications base build build.py developtools device docs domains drivers foundation kernel ohos_config.json prebuilts test third_party utils vendor 

编译过程

在源码的根目录执行如下命令安装鸿蒙的编译模块hb

root@5e3abe332c5a:/home/harmony#python3 -m pip install --user build/lite 

hb全称应该是harmonyos build system,在鸿蒙内核源码分析(编译过程篇)中有详细介绍.

root@5e3abe332c5a:/home/harmony/# hb -v [OHOS INFO] hb version 0.3.8 root@5e3abe332c5a:/home/harmony/# hb -h usage: hb OHOS build system positional arguments: {build,set,env,clean,deps} build Build source code set OHOS build settings env Show OHOS build env clean Clean output deps OHOS components deps optional arguments: -h, --help show this help message and exit -v, --version show program's version number and exit 

设置编译路径,选择当前路径 .

root@5e3abe332c5a:/home/harmony#hb set [OHOS INFO] Input code path: . OHOS Which product do you need? (Use arrow keys) hisilicon ❯ ipcamera_hispark_aries wifiiot_hispark_pegasus ipcamera_hispark_taurus 

直接回车,代表选择了ipcamera_hispark_aries,这三个对应平台的关系如下

Hi3518:ipcamera_hispark_aries@hisilicon Hi3861:wifiiot_hispark_pegasus@hisilicon Hi3516:ipcamera_hispark_taurus@hisilicon 

hb build -*为编译方式,可以选择全编,编单个模块,只编译ndk,看具体的开发场景.

root@5e3abe332c5a:/home/harmony# hb build -h usage: hb build [-h] [-b BUILD_TYPE] [-c COMPILER] [-t [TEST [TEST ...]]] [--dmverity] [--tee] [-p PRODUCT] [-f] [-n] [-T [TARGET [TARGET ...]]] [-v] [-shs] [component [component ...]] positional arguments: component name of the component optional arguments: -h, --help show this help message and exit -b BUILD_TYPE, --build_type BUILD_TYPE release or debug version -c COMPILER, --compiler COMPILER specify compiler -t [TEST [TEST ...]], --test [TEST [TEST ...]] compile test suit --dmverity Enable dmverity --tee Enable tee -p PRODUCT, --product PRODUCT build a specified product with {product_name}@{company}, eg: camera@huawei -f, --full full code compilation -n, --ndk compile ndk -T [TARGET [TARGET ...]], --target [TARGET [TARGET ...]] Compile single target -v, --verbose show all command lines while building -shs, --sign_haps_by_server sign haps by server 

本文选择全编 -f,拼电脑性能,搞开发一定要搞台好电脑,时间就是生命,内存最好32G,过程大概10分钟.

root@5e3abe332c5a:/home/harmony#hb build -f 

查看编译结果

kernel_liteos_a_note

每个的目录含义如下

目录名 描述 applications 应用程序样例,包括wifi-iot,camera等 base 基础软件服务子系统集&硬件服务子系统集 build 组件化编译、构建和配置脚本 docs 说明文档 domains 增强软件服务子系统集 drivers 驱动子系统 foundation 系统基础能力子系统集 kernel 内核子系统 prebuilts 编译器及工具链子系统 test 测试子系统 third_party 开源第三方组件 utils 常用的工具集 vendor 厂商提供的软件 build.py 编译脚本文件 out 编译后生成 

编译输出

out为编译结果输出目录

out/hispark_aries/ipcamera_hispark_aries 

进入目录

root@5e3abe332c5a:/home/harmony/out/hispark_aries/ipcamera_hispark_aries# ls args.gn build.log bundle_daemon_tool.map dev_tools libs NOTICE_FILE OHOS_Image.asm rootfs suites toggleButtonTest.map userfs vendor bin build.ninja config etc liteos.bin obj OHOS_Image.bin rootfs_jffs2.img test toolchain.ninja userfs_jffs2.img bm_tool.map build.ninja.d data foundation.map media_server.map OHOS_Image OHOS_Image.map server.map test_info unstripped usr 

系列篇会详细讲解启动过程,此处进入bin目录瞅瞅都有些啥好宝贝.

root@5e3abe332c5a:/home/harmony/out/hispark_aries/ipcamera_hispark_aries/bin# ls ai_server module_ActsAbilityMgrTest.bin module_ActsGraphVersionTest.bin module_ActsJFFS2CapabilityTest.bin module_ActsNFSTest.bin module_ActsSurfaceTest.bin os_dump apphilogcat module_ActsBootstrapTest.bin module_ActsHeapBaseTest.bin module_ActsJFFS2DACTest.bin module_ActsParameterTest.bin module_ActsSysApiTest.bin query.bin appspawn module_ActsBundleMgrTest.bin module_ActsHilogTest.bin module_ActsJFFS2Test.bin module_ActsPMSTest.bin module_ActsTimeApiTest.bin shell bundle_daemon module_ActsColorTest.bin module_ActsIoApiTest.bin module_ActsKvStoreTest.bin module_ActsProcessApiTest.bin module_ActsTransformTest.bin tftp CalcSubTest.bin module_ActsDyloadTest.bin module_ActsIpcMqTest.bin module_ActsListTest.bin module_ActsRectTest.bin module_ActsUiInterfaceTest1.bin wms_server foundation module_ActsFutexApiTest.bin module_ActsIpcPipeTest.bin module_ActsLwipTest.bin module_ActsSamgrTest.bin module_ActsUtilApiTest.bin hilogcat module_ActsGeometyr2dTest.bin module_ActsIpcSemTest.bin module_ActsMathApiTest.bin module_ActsSchedApiTest.bin module_ActsVFATCapabilityTest.bin init module_ActsGraphicMathTest.bin module_ActsIpcShmTest.bin module_ActsMemApiTest.bin module_ActsSecurityDataTest.bin module_ActsVFATDACTest.bin media_server module_ActsGraphMemApiTest.bin module_ActsIpcSignalTest.bin module_ActsNetTest.bin module_ActsSoftBusTest.bin module_ActsVFATTest.bin 

这难道都是ELF格式可执行程序? 用readelf命令试下shell就知道了.果然是shell程序,加载它将创建激动人心的shell进程

root@5e3abe332c5a:/home/harmony/out/hispark_aries/ipcamera_hispark_aries/bin# readelf -h shell ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: DYN (Shared object file) Machine: ARM Version: 0x1 Entry point address: 0x1000 Start of program headers: 52 (bytes into file) Start of section headers: 25268 (bytes into file) Flags: 0x5000200, Version5 EABI, soft-float ABI Size of this header: 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 11 Size of section headers: 40 (bytes) Number of section headers: 27 Section header string table index: 26 

再随便选择一个module_ActsListTest.bin看下,这些是鸿蒙用于测试的代码生成的.也是一个个的独立程序.可以在工程里找到他们的身影list_test.cpp,ActsListTest.json等文件

root@5e3abe332c5a:/home/harmony/out/hispark_aries/ipcamera_hispark_aries/bin# readelf -h module_ActsListTest.bin ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: DYN (Shared object file) Machine: ARM Version: 0x1 Entry point address: 0xb000 Start of program headers: 52 (bytes into file) Start of section headers: 172256 (bytes into file) Flags: 0x5000200, Version5 EABI, soft-float ABI Size of this header: 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 11 Size of section headers: 40 (bytes) Number of section headers: 27 Section header string table index: 26 

解读

仔细比较下这两个ELF的头文件看哪里不一样

 Entry point address: 0x1000 Entry point address: 0xb000 

这是程序的入口地址,也就是大家熟悉的main()函数的地址,用户程序是从这个位置开始执行.

readelf的功能很强大,有兴趣的可以玩下这个命令,看看elf里面究竟装的啥.

root@5e3abe332c5a:/home/harmony/out/hispark_aries/ipcamera_hispark_aries/bin# readelf -help readelf: option requires an argument -- 'p' Usage: readelf <option(s)> elf-file(s) Display information about the contents of ELF format files Options are: -a --all Equivalent to: -h -l -S -s -r -d -V -A -I -h --file-header Display the ELF file header -l --program-headers Display the program headers --segments An alias for --program-headers -S --section-headers Display the sections' header --sections An alias for --section-headers -g --section-groups Display the section groups -t --section-details Display the section details -e --headers Equivalent to: -h -l -S -s --syms Display the symbol table --symbols An alias for --syms --dyn-syms Display the dynamic symbol table -n --notes Display the core notes (if present) -r --relocs Display the relocations (if present) -u --unwind Display the unwind info (if present) -d --dynamic Display the dynamic section (if present) -V --version-info Display the version sections (if present) -A --arch-specific Display architecture specific information (if any) -c --archive-index Display the symbol/file index in an archive -D --use-dynamic Use the dynamic section info when displaying symbols -x --hex-dump=<number|name> Dump the contents of section <number|name> as bytes -p --string-dump=<number|name> Dump the contents of section <number|name> as strings -R --relocated-dump=<number|name> Dump the contents of section <number|name> as relocated bytes -z --decompress Decompress section before dumping it -w[lLiaprmfFsoRtUuTgAckK] or --debug-dump[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames, =frames-interp,=str,=loc,=Ranges,=pubtypes, =gdb_index,=trace_info,=trace_abbrev,=trace_aranges, =addr,=cu_index,=links,=follow-links] Display the contents of DWARF debug sections --dwarf-depth=N Do not display DIEs at depth N or greater --dwarf-start=N Display DIEs starting with N, at the same depth or deeper --ctf=<number|name> Display CTF info from section <number|name> --ctf-parent=<number|name> Use section <number|name> as the CTF parent --ctf-symbols=<number|name> Use section <number|name> as the CTF external symtab --ctf-strings=<number|name> Use section <number|name> as the CTF external strtab -I --histogram Display histogram of bucket list lengths -W --wide Allow output width to exceed 80 characters @<file> Read options from <file> -H --help Display this information -v --version Display the version number of readelf 

具体的加载过程和elf格式后续有专门的篇幅详细介绍,此处不做说明.

用户进程

grep过滤下干扰的*.bin,剩下的就是平台(Hi3518)需要内核创建的用户态进程.

root@5e3abe332c5a:/home/harmony/out/hispark_aries/ipcamera_hispark_aries/bin# ls | grep -v .bin ai_server apphilogcat appspawn bundle_daemon foundation hilogcat init media_server os_dump shell tftp wms_server 

这些服务含义和仓库如下.

ai_server AI业务子系统 https://gitee.com/openharmony/ai_engine apphilogcat 小型系统的流水日志功能 https://gitee.com/openharmony/hiviewdfx_hilog_lite base\hiviewdfx\hilog_lite\services\apphilogcat\hiview_applogcat.c appspawn 应用孵化模块进程 https://gitee.com/openharmony/startup_appspawn_lite bundle_daemon 用户程序框架内部工具接口 https://gitee.com/openharmony/appexecfwk_appexecfwk_lite foundation foundation系统进程 https://gitee.com/openharmony/distributedschedule_safwk_lite hilogcat 管理日志打印 https://gitee.com/openharmony/hiviewdfx_hilog_lite base\hiviewdfx\hilog_lite\services\hilogcat\hiview_logcat.c init 用户态祖宗进程 1号进程, -> 鸿蒙内核源码分析(特殊进程篇) media_server 播放模块框架实现 https://gitee.com/openharmony/multimedia_media_lite os_dump 备份文件系统 utils\native\lite\os_dump\os_dump.c shell 窥视内核的窗口 3号进程, -> 鸿蒙内核源码分析(shell篇) tftp 传输文件 third_party\curl\lib\tftp.c wms_server 窗口管理服务 https://gitee.com/openharmony/graphic_wms 

在整个项目工程中能轻易找到他们的入口函数.比如appspawn的启动过程

//base\startup\appspawn_lite\services\src\main.c int main(int argc, char const argv[]) { sleep(1); HILOG_INFO(HILOG_MODULE_HIVIEW, "[appspawn] main, enter."); // 1. ipc module init HOS_SystemInit(); // 2. register signal for SIGCHLD SignalRegist(); // 3. keep process alive HILOG_INFO(HILOG_MODULE_HIVIEW, "[appspawn] main, entering wait."); while (1) { // pause only returns when a signal was caught and the signal-catching function returned. // pause only returns -1, no need to process the return value. (void)pause(); } } 

鸿蒙将服务做成了组件,为最上层的应用程度提供管理/工具类的服务.但这些都是framework层的工作,超出了内核源码分析的范畴.希望后续有机会能去剖析它们.

鸿蒙源码百篇博客 往期回顾

参与贡献

喜欢请「点赞+关注+收藏」

收藏 (0)

相关文章

    文章评论

    共有0条评论来说两句吧...