一、 介紹
三方庫指其他公司或者組織提供的服務(wù)或者模塊,例如常見的開源 OpenCV 庫,能持續(xù)繁榮 OpenAtom OpenHarmony(簡稱“OpenHarmony”)的生態(tài)建設(shè)。本文介紹如何在 RK3568 開發(fā)板上成功移植 OpenJPEG 開源庫。OpenJPEG 是一個(gè)用 C 語言編寫的開源 jpeg 2000 編解碼器,可以對(duì)圖片進(jìn)行深度壓縮和解壓等。下面以 OpenJPEG 為例,總結(jié)移植三方庫到 OpenHarmony 的經(jīng)驗(yàn)。二、開發(fā)步驟
三方庫主要是基于標(biāo)準(zhǔn) Linux 系統(tǒng)的 c/c++ 開源庫,所以三方庫的移植工作,首先是在標(biāo)準(zhǔn) Linux 系統(tǒng)搭建環(huán)境、編譯與驗(yàn)證,然后將三方庫的編譯加入到 OpenHarmony 工程的構(gòu)建中。 三方庫移植的主要開發(fā)步驟如下:-
在標(biāo)準(zhǔn)Linux系統(tǒng)下載三方庫源碼,搭建開發(fā)環(huán)境,編譯整個(gè)源碼工程;
-
分析在標(biāo)準(zhǔn)Linux系統(tǒng)的編譯過程文件,提取編譯需要的源文件、編譯依賴等信息;
-
根據(jù)上一步分析結(jié)果,編寫gn文件,將三方庫加入到OpenHarmony的編譯體系;
-
驗(yàn)證編譯結(jié)果,在OpenHarmony的out目錄是否生成目標(biāo)庫;
-
測試demo/測試單元用例執(zhí)行。
三、開源庫的下載與編譯
下面以 OpenJPEG 開源庫為例,詳細(xì)的介紹開發(fā)流程。1.搜索OpenJPEG開源庫
本示例的代碼庫鏈接地址:https://github.com/uclouvain/openjpeg ?2.下載代碼
在 OpenHarmony/third_party 目錄下,輸入以下命令克隆 OpenJPEG 的源代碼:git clone git@github.com:uclouvain/openjpeg.git
然后切換到 openjpeg-2.1 分支:git checkout -b openjp-2.1
3. 執(zhí)行編譯
代碼倉庫的 INSTALL.md 中詳細(xì)介紹了使用 CMake 編譯 OpenJPEG 庫的步驟。本文參考該文檔編譯了 OpenJPEG 庫。若在移植過程中對(duì)該庫的編譯選項(xiàng)有疑惑的地方,可參考該文檔。INSTALL 文檔中詳細(xì)的介紹了編譯步驟,如下圖: ? ?可以按以上信息,根據(jù)如下步驟編譯 openjpeg 工程,具體如下:在 openjpeg 的目錄下,按以下步驟編譯:mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make
4. 編譯過程
整個(gè)編譯過程如下:water@ubuntu:~/openjpeg/openjpeg/build$ make
[ 1%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/thread.c.o
[ 2%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/bio.c.o
[ 4%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/cio.c.o
[ 5%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/dwt.c.o
[ 7%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/event.c.o
[ 8%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/ht_dec.c.o
[ 10%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/image.c.o
[ 11%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/invert.c.o
[ 13%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/j2k.c.o
[ 14%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/jp2.c.o
[ 16%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/mct.c.o
[ 17%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/mqc.c.o
[ 19%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/openjpeg.c.o
[ 20%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/opj_clock.c.o
[ 22%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/pi.c.o
[ 23%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/t1.c.o
[ 25%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/t2.c.o
[ 26%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/tcd.c.o
[ 28%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/tgt.c.o
[ 29%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/function_list.c.o
[ 31%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/opj_malloc.c.o
[ 32%] Building C object src/lib/openjp2/CMakeFiles/openjp2_static.dir/sparse_array.c.o
[ 34%] Linking C static library ../../../bin/libopenjp2.a
[ 34%] Built target openjp2_static
[ 35%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/thread.c.o
[ 37%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/bio.c.o
[ 38%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/cio.c.o
[ 40%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/dwt.c.o
[ 41%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/event.c.o
[ 43%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/ht_dec.c.o
[ 44%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/image.c.o
[ 46%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/invert.c.o
[ 47%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/j2k.c.o
[ 49%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/jp2.c.o
[ 50%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/mct.c.o
[ 52%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/mqc.c.o
[ 53%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/openjpeg.c.o
[ 55%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/opj_clock.c.o
[ 56%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/pi.c.o
[ 58%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/t1.c.o
[ 59%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/t2.c.o
[ 61%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/tcd.c.o
[ 62%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/tgt.c.o
[ 64%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/function_list.c.o
[ 65%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/opj_malloc.c.o
[ 67%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/sparse_array.c.o
[ 68%] Linking C shared library ../../../bin/libopenjp2.so
[ 68%] Built target openjp2
[ 70%] Building C object src/bin/jp2/CMakeFiles/opj_dump.dir/opj_dump.c.o
[ 71%] Building C object src/bin/jp2/CMakeFiles/opj_dump.dir/convert.c.o
[ 73%] Building C object src/bin/jp2/CMakeFiles/opj_dump.dir/convertbmp.c.o
[ 74%] Building C object src/bin/jp2/CMakeFiles/opj_dump.dir/index.c.o
[ 76%] Building C object src/bin/jp2/CMakeFiles/opj_dump.dir/__/common/color.c.o
[ 77%] Building C object src/bin/jp2/CMakeFiles/opj_dump.dir/__/common/opj_getopt.c.o
[ 79%] Linking C executable ../../../bin/opj_dump
[ 79%] Built target opj_dump
[ 80%] Building C object src/bin/jp2/CMakeFiles/opj_compress.dir/opj_compress.c.o
[ 82%] Building C object src/bin/jp2/CMakeFiles/opj_compress.dir/convert.c.o
[ 83%] Building C object src/bin/jp2/CMakeFiles/opj_compress.dir/convertbmp.c.o
[ 85%] Building C object src/bin/jp2/CMakeFiles/opj_compress.dir/index.c.o
[ 86%] Building C object src/bin/jp2/CMakeFiles/opj_compress.dir/__/common/color.c.o
[ 88%] Building C object src/bin/jp2/CMakeFiles/opj_compress.dir/__/common/opj_getopt.c.o
[ 89%] Linking C executable ../../../bin/opj_compress
[ 89%] Built target opj_compress
[ 91%] Building C object src/bin/jp2/CMakeFiles/opj_decompress.dir/opj_decompress.c.o
[ 92%] Building C object src/bin/jp2/CMakeFiles/opj_decompress.dir/convert.c.o
[ 94%] Building C object src/bin/jp2/CMakeFiles/opj_decompress.dir/convertbmp.c.o
[ 95%] Building C object src/bin/jp2/CMakeFiles/opj_decompress.dir/index.c.o
[ 97%] Building C object src/bin/jp2/CMakeFiles/opj_decompress.dir/__/common/color.c.o
[ 98%] Building C object src/bin/jp2/CMakeFiles/opj_decompress.dir/__/common/opj_getopt.c.o
[100%] Linking C executable ../../../bin/opj_decompress
[100%]Builttargetopj_decompress
依據(jù)上述信息中生成 openjp2 庫(libopenjp2.so)的過程可以知道編譯庫需要的 C 源文件,具體內(nèi)如下所示:[ 35%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/thread.c.o
[ 37%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/bio.c.o
[ 38%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/cio.c.o
[ 40%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/dwt.c.o
[ 41%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/event.c.o
[ 43%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/ht_dec.c.o
[ 44%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/image.c.o
[ 46%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/invert.c.o
[ 47%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/j2k.c.o
[ 49%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/jp2.c.o
[ 50%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/mct.c.o
[ 52%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/mqc.c.o
[ 53%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/openjpeg.c.o
[ 55%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/opj_clock.c.o
[ 56%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/pi.c.o
[ 58%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/t1.c.o
[ 59%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/t2.c.o
[ 61%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/tcd.c.o
[ 62%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/tgt.c.o
[ 64%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/function_list.c.o
[ 65%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/opj_malloc.c.o
[ 67%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/sparse_array.c.o
[ 68%] Linking C shared library ../../../bin/libopenjp2.so
[68%]Builttargetopenjp2
有了以上信息我們可以在后續(xù)分析的編譯文件中對(duì)比驗(yàn)證,編譯庫需要的源文件。
5. 查看結(jié)果
整個(gè)的 build 目錄結(jié)構(gòu)如下:water@ubuntu:~/openjpeg/openjpeg/build$ tree -L 2
.
├── bin
│ ├── libopenjp2.a
│ ├── libopenjp2.so -> libopenjp2.so.7
│ ├── libopenjp2.so.2.5.0
│ ├── libopenjp2.so.7 -> libopenjp2.so.2.5.0
│ ├── opj_compress
│ ├── opj_decompress
│ └── opj_dump
├── CMakeCache.txt
├── CMakeFiles
│ ├── 3.16.3
│ ├── CheckIncludeFiles
│ ├── CheckTypeSize
│ ├── cmake.check_cache
│ ├── CMakeDirectoryInformation.cmake
│ ├── CMakeError.log
│ ├── CMakeOutput.log
│ ├── CMakeTmp
│ ├── Export
│ ├── Makefile2
│ ├── Makefile.cmake
│ ├── progress.marks
│ ├── TargetDirectories.txt
│ ├── TestEndianess.bin
│ └── TestLargeFiles.c
├── cmake_install.cmake
├── CPackConfig.cmake
├── CPackSourceConfig.cmake
├── CTestCustom.cmake
├── libopenjp2.pc
├── LICENSE.txt
├── Makefile
├── OpenJPEGConfig.cmake
├── src
│ ├── bin
│ └── lib
├── thirdparty
│ ├── CMakeFiles
│ ├── cmake_install.cmake
│ └── Makefile
└── wrapping
├── CMakeFiles
├── cmake_install.cmake
└──Makefile
四、 分析編譯過程
1. 查看生成的庫
首先在 openjpeg/build/bin 目錄可以看出我們需要編譯生成的目標(biāo)文件,分別為 libopenjp2.a、libopenjp2.so.2.5.0、opj_compress、opj_decompress、opj_dump,還有一些鏈接符號(hào),經(jīng)過閱讀代碼發(fā)現(xiàn) opj_compress、opj_decompress 和 opj_dump 為一些測試相關(guān)的文件,因此可以不加入到代碼工程。 ? ?2. 分析生成過程
從 build 目錄下的 Makefile 開始分析生成 libopenjp2.so 所依賴的 C 源文件、cflag 標(biāo)志、需要包含的頭文件和需要鏈接的庫等信息。 在 openjpeg/build/Makefile 中,可以找到如下信息:#=============================================================================
# Target rules for targets named openjp2
# Build rule for target.
openjp2: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 openjp2
.PHONY : openjp2
# fast build rule for target.
openjp2/fast:
$(MAKE) -f src/lib/openjp2/CMakeFiles/openjp2.dir/build.make src/lib/openjp2/CMakeFiles/openjp2.dir/build
.PHONY:openjp2/fast
由上述信息可以發(fā)現(xiàn)生成 openjp2 目標(biāo)是依賴 CMakeFiles/Makefile2 文件。
因此再分析 openjpeg/build/CMakeFiles/Makefile2 這個(gè)文件,具體內(nèi)如下:#=============================================================================
# Target rules for target src/lib/openjp2/CMakeFiles/openjp2.dir
# All Build rule for target.
src/lib/openjp2/CMakeFiles/openjp2.dir/all:
$(MAKE) -f src/lib/openjp2/CMakeFiles/openjp2.dir/build.make src/lib/openjp2/CMakeFiles/openjp2.dir/depend
$(MAKE) -f src/lib/openjp2/CMakeFiles/openjp2.dir/build.make src/lib/openjp2/CMakeFiles/openjp2.dir/build
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --progress-dir=/home/water/openjpeg/openjpeg/build/CMakeFiles --progress-num=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 "Built target openjp2"
.PHONY : src/lib/openjp2/CMakeFiles/openjp2.dir/all
# Build rule for subdir invocation for target.
src/lib/openjp2/CMakeFiles/openjp2.dir/rule: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/water/openjpeg/openjpeg/build/CMakeFiles 23
$(MAKE) -f CMakeFiles/Makefile2 src/lib/openjp2/CMakeFiles/openjp2.dir/all
$(CMAKE_COMMAND) -E cmake_progress_start /home/water/openjpeg/openjpeg/build/CMakeFiles 0
.PHONY : src/lib/openjp2/CMakeFiles/openjp2.dir/rule
# Convenience name for target.
openjp2: src/lib/openjp2/CMakeFiles/openjp2.dir/rule
.PHONY : openjp2
# clean rule for target.
src/lib/openjp2/CMakeFiles/openjp2.dir/clean:
$(MAKE) -f src/lib/openjp2/CMakeFiles/openjp2.dir/build.make src/lib/openjp2/CMakeFiles/openjp2.dir/clean
.PHONY : src/lib/openjp2/CMakeFiles/openjp2.dir/clean
#=============================================================================
由以上內(nèi)容可以發(fā)現(xiàn)最終是根據(jù) src/lib/openjp2/CMakeFiles/openjp2.dir/build.make 來編譯生成 openjp2 庫。
因此需要再分析 openjpeg/build/src/lib/openjp2/CMakeFiles/openjp2.dir/build.mak。
build.make 文件節(jié)選://包含depend.make文件
# Include any dependencies generated for this target.
include src/lib/openjp2/CMakeFiles/openjp2.dir/depend.make
//包含flags.make文件,C_FLAG相關(guān)信息
# Include the compile flags for this target's objects.
include src/lib/openjp2/CMakeFiles/openjp2.dir/flags.make
//依賴link.txt,鏈接相關(guān)信息
cd /home/water/openharmony/third_party/openjpeg/build/src/lib/openjp2 && $(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/openjp2.dir/link.txt --verbose=$(VERBOSE)
cd /home/water/openharmony/third_party/openjpeg/build/src/lib/openjp2 && $(CMAKE_COMMAND) -E cmake_symlink_library ../../../bin/libopenjp2.so.2.5.0 ../../../bin/libopenjp2.so.7 ../../../bin/libopenjp2.so
//編譯依賴的C
# Object files for target openjp2
openjp2_OBJECTS =
"CMakeFiles/openjp2.dir/thread.c.o"
"CMakeFiles/openjp2.dir/bio.c.o"
"CMakeFiles/openjp2.dir/cio.c.o"
"CMakeFiles/openjp2.dir/dwt.c.o"
"CMakeFiles/openjp2.dir/event.c.o"
"CMakeFiles/openjp2.dir/ht_dec.c.o"
"CMakeFiles/openjp2.dir/image.c.o"
"CMakeFiles/openjp2.dir/invert.c.o"
"CMakeFiles/openjp2.dir/j2k.c.o"
"CMakeFiles/openjp2.dir/jp2.c.o"
"CMakeFiles/openjp2.dir/mct.c.o"
"CMakeFiles/openjp2.dir/mqc.c.o"
"CMakeFiles/openjp2.dir/openjpeg.c.o"
"CMakeFiles/openjp2.dir/opj_clock.c.o"
"CMakeFiles/openjp2.dir/pi.c.o"
"CMakeFiles/openjp2.dir/t1.c.o"
"CMakeFiles/openjp2.dir/t2.c.o"
"CMakeFiles/openjp2.dir/tcd.c.o"
"CMakeFiles/openjp2.dir/tgt.c.o"
"CMakeFiles/openjp2.dir/function_list.c.o"
"CMakeFiles/openjp2.dir/opj_malloc.c.o"
"CMakeFiles/openjp2.dir/sparse_array.c.o"
通過分析 build.make 文件發(fā)現(xiàn)需要依賴 depend.make、flags.make 和 link.txt,由以上內(nèi)容可以分析出如下信息:
?下面看下 flags.make 和 link.txt 文件的具體內(nèi)容。link.txt 的內(nèi)容如下:/usr/bin/cc-fPIC-O3-DNDEBUG-shared-Wl,-soname,libopenjp2.so.7-o../../../bin/libopenjp2.so.2.5.0CMakeFiles/openjp2.dir/thread.c.oCMakeFiles/openjp2.dir/bio.c.oCMakeFiles/openjp2.dir/cio.c.oCMakeFiles/openjp2.dir/dwt.c.oCMakeFiles/openjp2.dir/event.c.oCMakeFiles/openjp2.dir/ht_dec.c.oCMakeFiles/openjp2.dir/image.c.oCMakeFiles/openjp2.dir/invert.c.oCMakeFiles/openjp2.dir/j2k.c.oCMakeFiles/openjp2.dir/jp2.c.oCMakeFiles/openjp2.dir/mct.c.oCMakeFiles/openjp2.dir/mqc.c.oCMakeFiles/openjp2.dir/openjpeg.c.oCMakeFiles/openjp2.dir/opj_clock.c.oCMakeFiles/openjp2.dir/pi.c.oCMakeFiles/openjp2.dir/t1.c.oCMakeFiles/openjp2.dir/t2.c.oCMakeFiles/openjp2.dir/tcd.c.oCMakeFiles/openjp2.dir/tgt.c.oCMakeFiles/openjp2.dir/function_list.c.oCMakeFiles/openjp2.dir/opj_malloc.c.oCMakeFiles/openjp2.dir/sparse_array.c.o-lm-lpthread
從以上內(nèi)容可以發(fā)現(xiàn)需要鏈接 -lm、-lpthread 這兩個(gè)庫。flags.make 中的內(nèi)容如下:# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.16
# compile C with /usr/bin/cc
C_FLAGS = -fPIC -Wall -Wextra -Wconversion -Wunused-parameter -Wdeclaration-after-statement -Werror=declaration-after-statement
C_DEFINES = -DMUTEX_pthread -Dopenjp2_EXPORTS
C_INCLUDES=-I/home/water/openjpeg/openjpeg/build/src/lib/openjp2
從以上內(nèi)容可以發(fā)現(xiàn)編譯需要的 C_FLAGS 信息。
3.提取有效信息
從上述分析可以提取以下信息:需要包含的源文件:
openjpeg/src/lib/openjp2/bio.c
openjpeg/src/lib/openjp2/cio.c
openjpeg/src/lib/openjp2/dwt.c
openjpeg/src/lib/openjp2/event.c
openjpeg/src/lib/openjp2/function_list.c
openjpeg/src/lib/openjp2/ht_dec.c
openjpeg/src/lib/openjp2/image.c
openjpeg/src/lib/openjp2/invert.c
openjpeg/src/lib/openjp2/j2k.c
openjpeg/src/lib/openjp2/jp2.c
openjpeg/src/lib/openjp2/mct.c
openjpeg/src/lib/openjp2/mqc.c
openjpeg/src/lib/openjp2/openjpeg.c
openjpeg/src/lib/openjp2/opj_clock.c
openjpeg/src/lib/openjp2/opj_malloc.c
openjpeg/src/lib/openjp2/pi.c
openjpeg/src/lib/openjp2/sparse_array.c
openjpeg/src/lib/openjp2/t1.c
openjpeg/src/lib/openjp2/t2.c
openjpeg/src/lib/openjp2/tcd.c
openjpeg/src/lib/openjp2/tgt.c
openjpeg/src/lib/openjp2/thread.c
需要包含的頭文件:
src/lib/openjp2
需要鏈接的庫:
-lm -lpthread
編譯器需要添加的C_FLAGS標(biāo)記:
C_FLAGS=-O3-DNDEBUG-fPIC-ffast-math-Wall-Wextra-Wconversion-Wunused-parameter-Wdeclaration-after-statement-Werror=declaration-after-statement
從上述內(nèi)容中我們可以獲取到編譯 libopenjp2.so 所需的源文件、頭文件、鏈接庫、C_FLAGS 等信息。接下來就可以根據(jù)這些信息在 OpenHarmony 的三方庫文件夾下的 openjpeg 目錄下編寫 BUILD.gn 腳本,把 openjpeg 加入到 OpenHarmony 的編譯體系中。
五、將三方庫加入OpenHarmony的編譯體系
接下來演示一下如何把 OpenJPEG 開源庫通過 BUILD.gn 腳本添加到 OpenHarmony 工程中 表1:添加到工程后的目錄結(jié)構(gòu) ?1.OpenHarmony/build/subsystem_config.json 中添加 subsystem 節(jié)點(diǎn)。"openjpeg":{
"path":"third_party/openjpeg",
"name":"openjpeg"
}
2.OpenHarmony/productdefine/common/products/rk3568.json 文件里面加入如下內(nèi)容,添加板級(jí)相關(guān)的信息:{
"product_name": "rk3568",
"product_company": "hihope",
"product_device": "rk3568",
"version": "2.0",
"type": "standard",
"product_build_path": "device/hihope/build",
"parts":{
"openjpeg:openjpeg_lib":{} #新增openjpeg_lib
}
}
3.third_partyopenjpeg ests 下添加 ohos.build 文件,以便系統(tǒng)可以編譯 tests 目錄下的測試文件:third_partyopenjpeg ests下添加ohos.build文件:
{
"subsystem":"openjpeg",
"parts":{
"openjpeg_lib":{
"module_list":[
"http://third_party/openjpeg/tests:test_tile_decoder",#新增測試文件test_tile_decoder
"http://third_party/openjpeg/tests:test_tile_encoder", #新增測試文件test_tile_encoder
...........
]
}
}
}
4.OpenHarmony//third_party/openjpeg/ 目錄添加 BUILD.gn 文件,根據(jù)不同的系統(tǒng)生成靜態(tài)或者動(dòng)態(tài)庫,如下:if (defined(ohos_lite)) {
import("http://build/lite/config/component/lite_component.gni")
} else {
import("http://build/ohos.gni")
}
#編譯CFLAG標(biāo)記和需要鏈接的庫
config("openjpeg_cflag_config") {
cflags = [
"-O3" ,
"-DNDEBUG",
"-fPIC",
"-ffast-math",
"-Wall",
"-Wextra",
"-Wconversion",
"-Wunused-parameter",
"-Wdeclaration-after-statement",
"-Werror=declaration-after-statement",
]
ldflags = [
"-lm",
"-lpthread",
]
}
#需要包含的頭文件
config("openjpeg_config") {
include_dirs = [
"http://third_party/openjpeg/src/lib/openjp2"
]
}
#需要包含的源文件
ohos_source_set("libopenjpeg_source") {
sources = [
"http://third_party/openjpeg/src/lib/openjp2/thread.c",
"http://third_party/openjpeg/src/lib/openjp2/bio.c",
"http://third_party/openjpeg/src/lib/openjp2/cio.c",
"http://third_party/openjpeg/src/lib/openjp2/dwt.c",
"http://third_party/openjpeg/src/lib/openjp2/event.c",
"http://third_party/openjpeg/src/lib/openjp2/ht_dec.c",
"http://third_party/openjpeg/src/lib/openjp2/image.c",
"http://third_party/openjpeg/src/lib/openjp2/invert.c",
"http://third_party/openjpeg/src/lib/openjp2/j2k.c",
"http://third_party/openjpeg/src/lib/openjp2/jp2.c",
"http://third_party/openjpeg/src/lib/openjp2/mct.c",
"http://third_party/openjpeg/src/lib/openjp2/mqc.c",
"http://third_party/openjpeg/src/lib/openjp2/openjpeg.c",
"http://third_party/openjpeg/src/lib/openjp2/opj_clock.c",
"http://third_party/openjpeg/src/lib/openjp2/pi.c",
"http://third_party/openjpeg/src/lib/openjp2/t1.c",
"http://third_party/openjpeg/src/lib/openjp2/t2.c",
"http://third_party/openjpeg/src/lib/openjp2/tcd.c",
"http://third_party/openjpeg/src/lib/openjp2/tgt.c",
"http://third_party/openjpeg/src/lib/openjp2/function_list.c",
"http://third_party/linu/src/lib/openjp2/opj_malloc.c",
"http://third_party/openjpeg/src/lib/openjp2/sparse_array.c",
]
}
#根據(jù)不同系統(tǒng)編譯不同類型的庫lite類型的設(shè)備可以編程靜態(tài)庫,也可編成動(dòng)態(tài)庫
if (defined(ohos_lite)) {
lite_library("libjpeg") {
if (ohos_kernel_type == "liteos_m") {
target_type = "static_library" #編譯成靜態(tài)庫libopenjpeg.a
} else {
target_type = "shared_library" #編成動(dòng)態(tài)庫libopenjpeg.z.so
}
sources = libopenjpeg_source #依賴的源文件
public_configs = [ ":openjpeg_config" ] #依賴的頭文件
configs = [ ":openjpeg_cflag_config" ] #依賴的CFLAGS配置信息
}
} else {
#標(biāo)準(zhǔn)系統(tǒng)編成動(dòng)態(tài)庫,libopenjpeg.z.so
ohos_shared_library("openjpeg") {
deps = [ ":libopenjpeg_source" ] #依賴的源文件
public_configs = [ ":openjpeg_config" ] #依賴的頭文件配置信息
configs = [ ":openjpeg_cflag_config" ] #依賴的CFLAGS配置信息
part_name = "openjpeg_lib"
}
}
5.third_party/openjpeg/tests/ 目錄添加 BUILD.gn 文件,編譯測試文件,由于測試文件會(huì)依賴 OpenJPEG 庫,根據(jù)依賴關(guān)系(如下圖中的 deps)會(huì)鏈接到 thirdparty/openjpeg 下面的 BUILD.gn(即步驟 4 中的 BUILD.gn),最終此文件會(huì)編譯生成 libopenjpeg 庫。
third_party/openjpeg/tests/BUILD.gn 的內(nèi)容如下:
?以上幾個(gè)文件的具體調(diào)用關(guān)系如下圖所示:
?通過 git status --ignored 命令可以查看我們修改什么內(nèi)容,具體內(nèi)如下:
?通過以上操作以后,就可以在 OpenHarmony 編譯源碼了,編譯命令如下:./build.sh--product-namerk3568--ccache
編譯完成以后可以在 /out/rk3568/packages/phone/system/lib 目錄下看到編譯生成的 libopenjpeg.z.so 文件。water@ubuntu:~/OpenHarmony/out/rk3568/packages/phone/system/lib$ ls -l libopenjpeg.z.so
-rwxr-xr-x1rootroot286148Mar2318:59libopenjpeg.z.so
同時(shí),還可以在 out/rk3568/packages/phone/system/bin 目錄下看到我們編譯的測試文件 test_tile_encoder,如下所示:water@ubuntu:~/openharmony/out/rk3568/packages/phone/system/bin$ ls -l test_
test_decode_areatest_tile_decodertest_tile_encoder
把編譯完成的鏡像文件燒錄到 RK3568 開發(fā)板中,運(yùn)行測試文件驗(yàn)證整個(gè)庫的移植情況。
六、測試驗(yàn)證
在開源庫的 tests 目錄下有很多測試用例,在 test 目錄下的 CMakeLists.txt 文件會(huì)生成一些測試文件來進(jìn)行驗(yàn)證,其中會(huì)給出一些測試方法,具體如下: ? ?代碼工程編譯完成后,將鏡像文件燒錄到 RK3568 開發(fā)板中,在開發(fā)板 /system/bin 文件夾下可以發(fā)現(xiàn),有編譯 tests 目錄后生成的測試文件,如下所示:# ls -l test_*
test_decode_areatest_open_jpeg.shtest_tile_decodertest_tile_encoder
在 data 目錄下創(chuàng)建 test 目錄:# pwd
/data
#mkdirtest
把測試用例文件從 /bin 目錄下拷貝到 data/test 目錄下:# pwd
/data
#cp/bin/test_*./
添加可執(zhí)行權(quán)限:chmod+xtest_tile_decoder
輸入如下命令:./test_tile_encoder3200020001000100081tte1.j2k64644111256256
運(yùn)行 test_tile_encoder,測試結(jié)果如下:
?至此,整個(gè) OpenJPEG 開源庫在 OpenHarmony 上的移植就完畢,感興趣的開發(fā)者可以動(dòng)手移植自己的三方庫了。
附錄
開發(fā)過程中遇到的問題:In file included from ../../third_party/openjpeg/src/lib/openjp2/thread.c
../../third_party/openjpeg/src/lib/openjp2/opj_includes.h10: fatal error: 'opj_config_private.h' file not found
#include"opj_config_private.h"
由于 opj_config_private.h 是由 cmake 生成的,因此需要在 Linux 下編譯整個(gè) OpenJPEG 源碼,然后把生成的 opj_config_private.h 拷貝到相應(yīng)的目錄下:cp ./build/src/lib/openjp2/opj_config_private.h /src/lib/./build/src/bin/common/opj_apps_config.h/
cp ./build/src/lib/openjp2/opj_config.h /src/lib/openjp2/
cp./build/src/bin/common/opj_apps_config.h./src/bin/common/
遇到重定義問題,在 third_party/openjpeg/tests/pdf2jp2.c 文件中把這行定義注釋掉:../../third_party/openjpeg/tests/pdf2jp2.c9: error: '_GNU_SOURCE' macro redefined [-Werror,-Wmacro-redefined]
#define_GNU_SOURCE
原文標(biāo)題:一文帶你讀懂如何移植三方庫到OpenHarmony
文章出處:【微信公眾號(hào):HarmonyOS官方合作社區(qū)】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
-
開發(fā)板
+關(guān)注
關(guān)注
25文章
4959瀏覽量
97213 -
OpenHarmony
+關(guān)注
關(guān)注
25文章
3665瀏覽量
16161 -
RK3568
+關(guān)注
關(guān)注
4文章
501瀏覽量
4946
原文標(biāo)題:一文帶你讀懂如何移植三方庫到OpenHarmony
文章出處:【微信號(hào):HarmonyOS_Community,微信公眾號(hào):電子發(fā)燒友開源社區(qū)】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論