#夏日挑战赛#交叉编译(一)-- musl库浅析 原创 精华

挖墙脚的农民工
发布于 2022-7-13 15:52
浏览
7收藏

【本文正在参加星光计划3.0—夏日挑战赛】

背景

openharmony在编译框架采用了gn + ninja + llvm + clang + musl(当然这里排除内核的编译部分),musl是一个全新为 Linux 基本系统实现的标准库。特点是轻量级、快速、简单、免费、标准兼容和安全。当前rk3568的开发板编译的musl库采用arm的架构进行的编译。其中musl下载地址

编译用例

我们在三方库中增加一个简单的用例。用于当前编译过程分析。
在third_party/cJSON/BUILD.gn增加一个helloworld用例。
 #夏日挑战赛#交叉编译(一)-- musl库浅析-鸿蒙开发者社区
其中源文件为:
 #夏日挑战赛#交叉编译(一)-- musl库浅析-鸿蒙开发者社区
通过执行
./build.sh --product-name rk3568 --ccache --build-target=helloworld
编译之后我们会发现编译过程中会首先将通过clang 将编译./third_party/musl/crt/arm/crtn.s crti.s编译成对应的crtn.o crti.o 文件
 #夏日挑战赛#交叉编译(一)-- musl库浅析-鸿蒙开发者社区
 #夏日挑战赛#交叉编译(一)-- musl库浅析-鸿蒙开发者社区
编译过程产生的日志将会重定向输入到build.log .ninja_log,其中,ninja_log是ninja编译过程中产生的日志的输入。build.log 则是编译界面的日志重定向输入。当界面日志所产生参数并不方便了解当前编译信息,我们需要通过修改编译器输出参数来更好的了解编译过程。
build/toolchain/gcc_toolchain.gni文件里为编译相关信息。在asm、cc 等函数中增加日志输出,ninja日志输出通过description 标识来输出,command 为执行命令
 #夏日挑战赛#交叉编译(一)-- musl库浅析-鸿蒙开发者社区

description 增加 $command 之后build.log日志中将会记录整个编译过程中编译信息,
 #夏日挑战赛#交叉编译(一)-- musl库浅析-鸿蒙开发者社区

通过当前日志我们可以看到编译过程中的依赖关系。
 #夏日挑战赛#交叉编译(一)-- musl库浅析-鸿蒙开发者社区
为跟能明确方便了解其中的编译关系,我们可以通过提取命令的方式进行编译
编译helloworld.o文件

 /usr/bin/ccache ../../prebuilts/clang/ohos/linux-x86_64/llvm/bin/clang -MMD -MF helloworld/helloworld.o.d -DV8_DEPRECATION_WARNINGS -DNO_TCMALLOC -D_GNU_SOURCE -DHAVE_SYS_UIO_H -D__MUSL__ -D_LIBCPP_HAS_MUSL_LIBC -D__BUILD_LINUX_WITH_CLANG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D_FORTIFY_SOURCE=2 -DCOMPONENT_BUILD -D__GNU_SOURCE=1 -DCHROMIUM_CXX_TWEAK_INLINES -D__MUSL__ -D_LIBCPP_HAS_MUSL_LIBC -D__BUILD_LINUX_WITH_CLANG -DNDEBUG -DNVALGRIND -DDYNAMIC_ANNOTATIONS_ENABLED=0 -Iobj/third_party/musl/usr/include/arm-linux-ohos -I../../prebuilts/clang/ohos/linux-x86_64/llvm/include/c++/v1 -Ioverride/third_party -I../.. -Igen -fno-strict-aliasing --param=ssp-buffer-size=4 -fstack-protector-strong -Wno-builtin-macro-redefined -D__DATE__= -D__TIME__= -D__TIMESTAMP__= -funwind-tables -fPIC -fcolor-diagnostics -fmerge-all-constants -Xclang -mllvm -Xclang -instcombine-lower-dbg-declare=0 -no-canonical-prefixes -ffunction-sections -fno-short-enums --target=arm-linux-ohos -march=armv7-a -mfloat-abi=softfp -mtune=generic-armv7-a -mfpu=neon -mthumb -Wall -Werror -Wextra -Wimplicit-fallthrough -Wthread-safety -Wno-missing-field-initializers -Wno-unused-parameter -Wno-c++11-narrowing -Wno-unneeded-internal-declaration -Wno-error=c99-designator -Wno-error=anon-enum-enum-conversion -Wno-error=implicit-fallthrough -Wno-error=sizeof-array-div -Wno-error=reorder-init-list -Wno-error=range-loop-construct -Wno-error=deprecated-copy -Wno-error=implicit-int-float-conversion -Wno-error=inconsistent-dllimport -Wno-error=unknown-warning-option -Wno-error=abstract-final-class -Wno-error=sign-compare -Wno-error=int-in-bool-context -Wno-error=xor-used-as-pow -Wno-error=return-stack-address -Wno-error=dangling-gsl -Wno-undefined-var-template -Wno-nonportable-include-path -Wno-user-defined-warnings -Wno-unused-lambda-capture -Wno-null-pointer-arithmetic -Wno-enum-compare-switch -O2 -fno-ident -fdata-sections -ffunction-sections -fomit-frame-pointer -gdwarf-3 -g2 -ggnu-pubnames -fno-common -Wheader-hygiene -Wstring-conversion -Wtautological-overlap-compare -fPIE --sysroot=../musl -c ../../third_party/cJSON/helloworld.c -o helloworld/helloworld.o

编译成执行文件

 /usr/bin/env "../../build/toolchain/gcc_link_wrapper.py" --output="helloworld1" --strip="../../prebuilts/clang/ohos/linux-x86_64/llvm/bin/llvm-strip" --unstripped-file="helloworld1" -- ../../prebuilts/clang/ohos/linux-x86_64/llvm/bin/clang++ -Wl,--fatal-warnings -Wl,--build-id=md5 -fPIC -Wl,-z,noexecstack -Wl,-z,now -Wl,-z,relro -Wl,-z,defs -Wl,--as-needed -fuse-ld=lld -Wl,--icf=all -Wl,--color-diagnostics -Wl,--no-undefined -Wl,--exclude-libs=libunwind_llvm.a -Wl,--exclude-libs=libc++_static.a -Wl,--exclude-libs=libvpx_assembly_arm.a --target=arm-linux-ohos -Werror -Wl,--warn-shared-textrel -Wl,-O2 -Wl,--gc-sections -Wl,--gdb-index --sysroot=../musl -nostdlib -L../../prebuilts/clang/ohos/linux-x86_64/llvm/lib/arm-linux-ohos/c++ -L../musl/usr/lib/arm-linux-ohos -L../../prebuilts/clang/ohos/linux-x86_64/llvm/lib/clang/10.0.1/lib/arm-linux-ohos -Wl,--warn-shared-textrel -Bdynamic -Wl,-z,nocopyreloc -pie -o "helloworld3" ../musl/usr/lib/arm-linux-ohos/Scrt1.o ../musl/usr/lib/arm-linux-ohos/crti.o -Wl,--start-group  @"../helloworld.rsp"  -lunwind ../../prebuilts/clang/ohos/linux-x86_64/llvm/lib/clang/10.0.1/lib/arm-linux-ohos/libclang_rt.builtins.a -lc -lc++ -lc++abi -ldl -lm -Wl,--end-group  ../musl/usr/lib/arm-linux-ohos/crtn.o

当然其中的依赖关系记录在对应的rsp 文件中。编译的时候需要通过cp 命令复制出来
 #夏日挑战赛#交叉编译(一)-- musl库浅析-鸿蒙开发者社区
通过编译之后,生成对应arm 可以使用helloworld 执行文件。

总结

musl是openharmony 编译的基础,当然如何通过gn + ninja + llvm + clang + musl 搭建独立的框架用于三方库的管理这才是比较有意义的活。有感兴趣的大牛可以一起参与整理一个。

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
已于2022-7-13 15:52:53修改
8
收藏 7
回复
举报
3条回复
按时间正序
/
按时间倒序
碼磚民工
碼磚民工

666

1
回复
2022-7-13 16:05:06
红叶亦知秋
红叶亦知秋

期待大佬强强联手

1
回复
2022-7-13 16:08:07
民之码农
民之码农

牛叉,赞起!

2
回复
2022-7-18 08:10:10
回复
    相关推荐