天天看點

#夏日挑戰賽#交叉編譯(一)-- musl庫淺析

【本文正在參加星光計劃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 搭建獨立的架構用于三方庫的管理這才是比較有意義的活。有感興趣的大牛可以一起參與整理一個。