积分1635 / 贡献20

提问18答案被采纳66文章40

[经验分享] OpenHarmony的2种快速编译方法 原创

深开鸿_王石 显示全部楼层 发表于 2023-12-18 16:20:45

简介

通过./build.sh脚本进行编译,是OpenHarmony官方推荐的编译程序的方法。但是这样的方法牵扯方便广,速度慢,接下来我们来熟悉下OpenHarmony的另外两种编译方法。

GCC编译方法

  1. 工具链位置:
    [源码路径]/prebuilts/gcc/linux-x86/arm/gcc-linaro-7.5.0-arm-linux-gnueabi/bin/
  2. 编译命令:
    arm-linux-gnueabi-gcc -static helloworld.c -o gcchelloworld
  3. 方法解析: **使用gcc工具链的编译方法是嵌入式开发的通用方法。但是这里有个特殊的地方-static。 gcc 加上 -**static,会在链接阶段, 查找对应模块的静态库, 而非动态库, 并把需要的东西,都带进目标文件),编译好后,文件会非常大,但是,运行时就不需要依赖。这个原因则是在OpenHarmony的镜像文件里放的是musl的lib库,没有glibc的lib库,所以如果不用-static标志则有可能在运行时找其他运行库,找不到则运行不成功。

CLang编译方法

  1. 工具链位置:[源码路径]/prebuilts/clang/ohos/linux-x86_64/llvm/bin

  2. 编译命令:

    • clang --sysroot=[ohos代码路径]/out/rk3568/obj/third_party/musl/ --target=arm-linux-ohosmusl -D**clang** -march=armv7-a -mfloat-abi=softfp -mfpu=neon-vfpv4 -w -o helloworld.c.o -c helloworld.c
    • clang --sysroot=[ohos代码路径]/out/rk3568/obj/third_party/musl/ --target=arm-linux-ohosmusl -D**clang** -march=armv7-a -mfloat-abi=softfp -mfpu=neon-vfpv4 -w -Wl,--dynamic-linker,/lib/ld-musl-arm.so.1 -rdynamic helloworld.c.o -o muslhelloworld
  3. 方法解析: clang编译器是APPLE公司的编译器大牛Chris Lattner主导下编写的,其目标是替换大名鼎鼎的GCC编译器。一般编译器的流程如下:

    <pre class="md-fences md-end-block md-diagram md-fences-advanced ty-contain-cm modeLoaded" spellcheck="false" lang="mermaid" cid="n32" mdtype="fences" mermaid-type="graph"><div class="md-diagram-panel md-fences-adv-panel"><div class="md-diagram-panel-header"></div><div class="md-diagram-panel-preview"><svg id="mermaidChart0" width="100%" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="61.59375" viewBox="0 0 744 61.59375" class="in-text-selection"><g><g class="output"><g class="clusters"></g><g class="edgePaths"><g class="edgePath LS-源代码 LE-预处理" id="L-源代码-预处理"><path class="path" d="M76,30.796875L101,30.796875L126,30.796875" marker-end="url(#arrowhead32)"></path><defs><marker id="arrowhead32" viewBox="0 0 10 10" refX="9" refY="5" markerUnits="strokeWidth" markerWidth="8" markerHeight="6" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" class="arrowheadPath"></path></marker></defs></g><g class="edgePath LS-预处理 LE-前端" id="L-预处理-前端"><path class="path" d="M194,30.796875L219,30.796875L244,30.796875" marker-end="url(#arrowhead33)"></path><defs><marker id="arrowhead33" viewBox="0 0 10 10" refX="9" refY="5" markerUnits="strokeWidth" markerWidth="8" markerHeight="6" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" class="arrowheadPath"></path></marker></defs></g><g class="edgePath LS-前端 LE-优化" id="L-前端-优化"><path class="path" d="M296,30.796875L321,30.796875L346,30.796875" marker-end="url(#arrowhead34)"></path><defs><marker id="arrowhead34" viewBox="0 0 10 10" refX="9" refY="5" markerUnits="strokeWidth" markerWidth="8" markerHeight="6" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" class="arrowheadPath"></path></marker></defs></g><g class="edgePath LS-优化 LE-后端" id="L-优化-后端"><path class="path" d="M398,30.796875L423,30.796875L448,30.796875" marker-end="url(#arrowhead35)"></path><defs><marker id="arrowhead35" viewBox="0 0 10 10" refX="9" refY="5" markerUnits="strokeWidth" markerWidth="8" markerHeight="6" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" class="arrowheadPath"></path></marker></defs></g><g class="edgePath LS-后端 LE-链接" id="L-后端-链接"><path class="path" d="M500,30.796875L525,30.796875L550,30.796875" marker-end="url(#arrowhead36)"></path><defs><marker id="arrowhead36" viewBox="0 0 10 10" refX="9" refY="5" markerUnits="strokeWidth" markerWidth="8" markerHeight="6" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" class="arrowheadPath"></path></marker></defs></g><g class="edgePath LS-链接 LE-生成文件" id="L-链接-生成文件"><path class="path" d="M602,30.796875L627,30.796875L652,30.796875" marker-end="url(#arrowhead37)"></path><defs><marker id="arrowhead37" viewBox="0 0 10 10" refX="9" refY="5" markerUnits="strokeWidth" markerWidth="8" markerHeight="6" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" class="arrowheadPath"></path></marker></defs></g></g><g class="edgeLabels"><g class="edgeLabel" transform=""><g transform="translate(0,0)" class="label"><rect rx="0" ry="0" width="0" height="0"></rect><foreignObject width="0" height="0"><div xmlns="http://www.w3.org/1999/xhtml"><span id="L-L-源代码-预处理" class="edgeLabel L-LS-源代码' L-LE-预处理"></span></div></foreignObject></g></g><g class="edgeLabel" transform=""><g transform="translate(0,0)" class="label"><rect rx="0" ry="0" width="0" height="0"></rect><foreignObject width="0" height="0"><div xmlns="http://www.w3.org/1999/xhtml"><span id="L-L-预处理-前端" class="edgeLabel L-LS-预处理' L-LE-前端"></span></div></foreignObject></g></g><g class="edgeLabel" transform=""><g transform="translate(0,0)" class="label"><rect rx="0" ry="0" width="0" height="0"></rect><foreignObject width="0" height="0"><div xmlns="http://www.w3.org/1999/xhtml"><span id="L-L-前端-优化" class="edgeLabel L-LS-前端' L-LE-优化"></span></div></foreignObject></g></g><g class="edgeLabel" transform=""><g transform="translate(0,0)" class="label"><rect rx="0" ry="0" width="0" height="0"></rect><foreignObject width="0" height="0"><div xmlns="http://www.w3.org/1999/xhtml"><span id="L-L-优化-后端" class="edgeLabel L-LS-优化' L-LE-后端"></span></div></foreignObject></g></g><g class="edgeLabel" transform=""><g transform="translate(0,0)" class="label"><rect rx="0" ry="0" width="0" height="0"></rect><foreignObject width="0" height="0"><div xmlns="http://www.w3.org/1999/xhtml"><span id="L-L-后端-链接" class="edgeLabel L-LS-后端' L-LE-链接"></span></div></foreignObject></g></g><g class="edgeLabel" transform=""><g transform="translate(0,0)" class="label"><rect rx="0" ry="0" width="0" height="0"></rect><foreignObject width="0" height="0"><div xmlns="http://www.w3.org/1999/xhtml"><span id="L-L-链接-生成文件" class="edgeLabel L-LS-链接' L-LE-生成文件"></span></div></foreignObject></g></g></g><g class="nodes"><g class="node default" id="flowchart-源代码-7" transform="translate(42,30.796875)"><rect rx="0" ry="0" x="-34" y="-22.796875" width="68" height="45.59375" class="label-container"></rect><g class="label" transform="translate(0,0)"><g transform="translate(-24,-12.796875)"><foreignObject width="48" height="25.59375"><div xmlns="http://www.w3.org/1999/xhtml">源代码</div></foreignObject></g></g></g><g class="node default" id="flowchart-预处理-8" transform="translate(160,30.796875)"><rect rx="0" ry="0" x="-34" y="-22.796875" width="68" height="45.59375" class="label-container"></rect><g class="label" transform="translate(0,0)"><g transform="translate(-24,-12.796875)"><foreignObject width="48" height="25.59375"><div xmlns="http://www.w3.org/1999/xhtml">预处理</div></foreignObject></g></g></g><g class="node default" id="flowchart-前端-9" transform="translate(270,30.796875)"><rect rx="0" ry="0" x="-26" y="-22.796875" width="52" height="45.59375" class="label-container"></rect><g class="label" transform="translate(0,0)"><g transform="translate(-16,-12.796875)"><foreignObject width="32" height="25.59375"><div xmlns="http://www.w3.org/1999/xhtml">前端</div></foreignObject></g></g></g><g class="node default" id="flowchart-优化-10" transform="translate(372,30.796875)"><rect rx="0" ry="0" x="-26" y="-22.796875" width="52" height="45.59375" class="label-container"></rect><g class="label" transform="translate(0,0)"><g transform="translate(-16,-12.796875)"><foreignObject width="32" height="25.59375"><div xmlns="http://www.w3.org/1999/xhtml">优化</div></foreignObject></g></g></g><g class="node default" id="flowchart-后端-11" transform="translate(474,30.796875)"><rect rx="0" ry="0" x="-26" y="-22.796875" width="52" height="45.59375" class="label-container"></rect><g class="label" transform="translate(0,0)"><g transform="translate(-16,-12.796875)"><foreignObject width="32" height="25.59375"><div xmlns="http://www.w3.org/1999/xhtml">后端</div></foreignObject></g></g></g><g class="node default" id="flowchart-链接-12" transform="translate(576,30.796875)"><rect rx="0" ry="0" x="-26" y="-22.796875" width="52" height="45.59375" class="label-container"></rect><g class="label" transform="translate(0,0)"><g transform="translate(-16,-12.796875)"><foreignObject width="32" height="25.59375"><div xmlns="http://www.w3.org/1999/xhtml">链接</div></foreignObject></g></g></g><g class="node default" id="flowchart-生成文件-13" transform="translate(694,30.796875)"><rect rx="0" ry="0" x="-42" y="-22.796875" width="84" height="45.59375" class="label-container"></rect><g class="label" transform="translate(0,0)"><g transform="translate(-32,-12.796875)"><foreignObject width="64" height="25.59375"><div xmlns="http://www.w3.org/1999/xhtml">生成文件</div></foreignObject></g></g></g></g></g></g></svg></div><div class="md-diagram-panel-error"></div></div></pre>

    通过上面的命令分析分了两部,第一步编译生成中间文件:helloworld.c.o,也就是从预处理一直到后端的过程;第二步链接helloworld.c.o和/lib/ld-musl-arm.so.1生成目标文件muslhelloworld也就是链接和生成文件。这部分和上部分的区别就是手动引用musl库。那么musl库和glibc的库的区别是什么呢?

    1. 开源版本:glibc(LGPLv2.1),musl(MIT)
    2. 运行速度:glibc优于musl,同时稳定性也是glibc更好
    3. 代码可读性:musl由于glibc

总结

OpenHarmony的官方编译方法就是使用./build.sh脚本文件,调用顺序大致如下:

<pre class="md-fences md-end-block md-diagram md-fences-advanced ty-contain-cm modeLoaded" spellcheck="false" lang="mermaid" cid="n44" mdtype="fences" mermaid-type="graph"><div class="md-diagram-panel md-fences-adv-panel"><div class="md-diagram-panel-header"></div><div class="md-diagram-panel-preview"><svg id="mermaidChart1" width="100%" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="61.59375" viewBox="0 0 736.078125 61.59375" class="in-text-selection"><g><g class="output"><g class="clusters"></g><g class="edgePaths"><g class="edgePath LS-build.sh LE-entry.py" id="L-build.sh-entry.py"><path class="path" d="M87.9375,30.796875L112.9375,30.796875L137.9375,30.796875" marker-end="url(#arrowhead64)"></path><defs><marker id="arrowhead64" viewBox="0 0 10 10" refX="9" refY="5" markerUnits="strokeWidth" markerWidth="8" markerHeight="6" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" class="arrowheadPath"></path></marker></defs></g><g class="edgePath LS-entry.py LE-vendor和product" id="L-entry.py-vendor和product"><path class="path" d="M219.578125,30.796875L244.578125,30.796875L269.578125,30.796875" marker-end="url(#arrowhead65)"></path><defs><marker id="arrowhead65" viewBox="0 0 10 10" refX="9" refY="5" markerUnits="strokeWidth" markerWidth="8" markerHeight="6" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" class="arrowheadPath"></path></marker></defs></g><g class="edgePath LS-vendor和product LE-hb" id="L-vendor和product-hb"><path class="path" d="M419.6875,30.796875L444.6875,30.796875L469.6875,30.796875" marker-end="url(#arrowhead66)"></path><defs><marker id="arrowhead66" viewBox="0 0 10 10" refX="9" refY="5" markerUnits="strokeWidth" markerWidth="8" markerHeight="6" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" class="arrowheadPath"></path></marker></defs></g><g class="edgePath LS-hb LE-ninja" id="L-hb-ninja"><path class="path" d="M509.765625,30.796875L534.765625,30.796875L559.765625,30.796875" marker-end="url(#arrowhead67)"></path><defs><marker id="arrowhead67" viewBox="0 0 10 10" refX="9" refY="5" markerUnits="strokeWidth" markerWidth="8" markerHeight="6" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" class="arrowheadPath"></path></marker></defs></g><g class="edgePath LS-ninja LE-clang" id="L-ninja-clang"><path class="path" d="M616.859375,30.796875L641.859375,30.796875L666.859375,30.796875" marker-end="url(#arrowhead68)"></path><defs><marker id="arrowhead68" viewBox="0 0 10 10" refX="9" refY="5" markerUnits="strokeWidth" markerWidth="8" markerHeight="6" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" class="arrowheadPath"></path></marker></defs></g></g><g class="edgeLabels"><g class="edgeLabel" transform=""><g transform="translate(0,0)" class="label"><rect rx="0" ry="0" width="0" height="0"></rect><foreignObject width="0" height="0"><div xmlns="http://www.w3.org/1999/xhtml"><span id="L-L-build.sh-entry.py" class="edgeLabel L-LS-build.sh' L-LE-entry.py"></span></div></foreignObject></g></g><g class="edgeLabel" transform=""><g transform="translate(0,0)" class="label"><rect rx="0" ry="0" width="0" height="0"></rect><foreignObject width="0" height="0"><div xmlns="http://www.w3.org/1999/xhtml"><span id="L-L-entry.py-vendor和product" class="edgeLabel L-LS-entry.py' L-LE-vendor和product"></span></div></foreignObject></g></g><g class="edgeLabel" transform=""><g transform="translate(0,0)" class="label"><rect rx="0" ry="0" width="0" height="0"></rect><foreignObject width="0" height="0"><div xmlns="http://www.w3.org/1999/xhtml"><span id="L-L-vendor和product-hb" class="edgeLabel L-LS-vendor和product' L-LE-hb"></span></div></foreignObject></g></g><g class="edgeLabel" transform=""><g transform="translate(0,0)" class="label"><rect rx="0" ry="0" width="0" height="0"></rect><foreignObject width="0" height="0"><div xmlns="http://www.w3.org/1999/xhtml"><span id="L-L-hb-ninja" class="edgeLabel L-LS-hb' L-LE-ninja"></span></div></foreignObject></g></g><g class="edgeLabel" transform=""><g transform="translate(0,0)" class="label"><rect rx="0" ry="0" width="0" height="0"></rect><foreignObject width="0" height="0"><div xmlns="http://www.w3.org/1999/xhtml"><span id="L-L-ninja-clang" class="edgeLabel L-LS-ninja' L-LE-clang"></span></div></foreignObject></g></g></g><g class="nodes"><g class="node default" id="flowchart-build.sh-20" transform="translate(47.96875,30.796875)"><rect rx="0" ry="0" x="-39.96875" y="-22.796875" width="79.9375" height="45.59375" class="label-container"></rect><g class="label" transform="translate(0,0)"><g transform="translate(-29.96875,-12.796875)"><foreignObject width="59.9375" height="25.59375"><div xmlns="http://www.w3.org/1999/xhtml">build.sh</div></foreignObject></g></g></g><g class="node default" id="flowchart-entry.py-21" transform="translate(178.7578125,30.796875)"><rect rx="0" ry="0" x="-40.8203125" y="-22.796875" width="81.640625" height="45.59375" class="label-container"></rect><g class="label" transform="translate(0,0)"><g transform="translate(-30.8203125,-12.796875)"><foreignObject width="61.640625" height="25.59375"><div xmlns="http://www.w3.org/1999/xhtml">entry.py</div></foreignObject></g></g></g><g class="node default" id="flowchart-vendor和product-22" transform="translate(344.6328125,30.796875)"><rect rx="0" ry="0" x="-75.0546875" y="-22.796875" width="150.109375" height="45.59375" class="label-container"></rect><g class="label" transform="translate(0,0)"><g transform="translate(-65.0546875,-12.796875)"><foreignObject width="130.109375" height="25.59375"><div xmlns="http://www.w3.org/1999/xhtml">vendor和product</div></foreignObject></g></g></g><g class="node default" id="flowchart-hb-23" transform="translate(489.7265625,30.796875)"><rect rx="0" ry="0" x="-20.0390625" y="-22.796875" width="40.078125" height="45.59375" class="label-container"></rect><g class="label" transform="translate(0,0)"><g transform="translate(-10.0390625,-12.796875)"><foreignObject width="20.078125" height="25.59375"><div xmlns="http://www.w3.org/1999/xhtml">hb</div></foreignObject></g></g></g><g class="node default" id="flowchart-ninja-24" transform="translate(588.3125,30.796875)"><rect rx="0" ry="0" x="-28.546875" y="-22.796875" width="57.09375" height="45.59375" class="label-container"></rect><g class="label" transform="translate(0,0)"><g transform="translate(-18.546875,-12.796875)"><foreignObject width="37.09375" height="25.59375"><div xmlns="http://www.w3.org/1999/xhtml">ninja</div></foreignObject></g></g></g><g class="node default" id="flowchart-clang-25" transform="translate(697.46875,30.796875)"><rect rx="0" ry="0" x="-30.609375" y="-22.796875" width="61.21875" height="45.59375" class="label-container"></rect><g class="label" transform="translate(0,0)"><g transform="translate(-20.609375,-12.796875)"><foreignObject width="41.21875" height="25.59375"><div xmlns="http://www.w3.org/1999/xhtml">clang</div></foreignObject></g></g></g></g></g></g></svg></div><div class="md-diagram-panel-error"></div></div></pre>

所以如果要缩减编译时间还有一些特殊方法:

  • ./build.sh --product-name 【产品名称如:rk3568】--ccache --build-target 【编译对象模块,如:moduleb_lib】 此方法也是官方的一种编译方法,虽说只编译一个模块,但是流程是完整的,即:
    • 用BUILD.gn生成ninja文件,然后通过上面的流程编译最总的模块二进制文件
  • ninja -C /out/rk3568(根据编译的productname来填) moduleb_lib(编译对象模块,如:moduleb_lib) 此方法的前提是首先用./build.sh编译过一遍,然后因为out目录里提前生成了build.ninja,所以可以直接用ninja命令直接编译对应模块

©著作权归作者所有,转载或内容合作请联系作者

您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部