鸿蒙交叉编译OSS_C_SDK,已编译未测试

avatar
作者
筋斗云
阅读量:0

鸿蒙交叉编译OSS_C_SDK,已编译未测试

最新进展

07.19 已成功编译,暂未测试,产物及patch见下方,伙伴们可以先用着,测试结果我会同步更新过来

说在前头

在编译oss_c_sdk中遇到的坑和问题,当前依然在坑中,旨在帮助有相同手动适配想法的兄弟减少工作量,也希望有新进展和我同步下,感激不尽

环境准备

  1. 准备Linux系统(尝试过MacOS,不太顺利)
  2. 下载工具链:https://developer.huawei.com/consumer/cn/download/
    在这里插入图片描述
  3. 直接贴命令行:
# 创建文件夹并进入 mkdir -p /home/ohos && cd /home/ohos  # 把下载好的command-line-toolsxxxx.zip放到这里来后执行 unzip command-line-toolxxxxxxx.zip  # 安装cmake, command-line-tool中的cmake版本过低, apt install cmake -y  # 或 dnf install cmake -y  # 配置环境变量,建议配置在 /etc/profile 或用户profile里 vi /etc/profile # 在最下面添加一行 export OHOS_SDK=/home/ohos/command-line-tools/sdk/HarmonyOS-NEXT-DB1/openharmony  # :wq!后 执行 source /etc/profile echo $OHOS_SDK # 验证  # tpc_c_cplusplus准备 cd /home/ohos && git clone https://gitee.com/openharmony-sig/tpc_c_cplusplus.git && cd tpc_c_cplusplus  # 进入到工具包目录 cd lycium/Buildtools # 若输出"toolchain.tar.gz: OK"则说明工具包正常 # 否则说明工具包异常,需重新下载 sha512sum -c SHA512SUM # 解压拷贝编译工具                    tar -zxvf toolchain.tar.gz # 将命令拷贝到工具链的native/llvm/bin目录下 cp toolchain/* ${OHOS_SDK}/native/llvm/bin  # 设置编译机cmake识别OHOS系统 # cp $OHOS_SDK/native/build-tools/cmake/share/cmake-3.16/Modules/Platform/OHOS.cmake \ # xxx(代表你编译机安装的cmake的路径)/Modules/Platform # 我这里是/usr/share/cmake/Modules/Platform cp $OHOS_SDK/native/build-tools/cmake/share/cmake-3.16/Modules/Platform/OHOS.cmake /usr/share/cmake/Modules/Platform 

了解lycium工具

https://gitee.com/openharmony-sig/tpc_c_cplusplus/tree/master/lycium

总结:
在thirdparty里面定义要编译的包信息,每次编译都会自动清理、自动下载(仅第一次),自动解压,就是依次执行HPKBUILD里面的clean, prepare, build 和 package

了解Aliyun_OSS_C_SDK

https://github.com/aliyun/aliyun-oss-c-sdk?tab=readme-ov-file

  1. 本次编译需要4个依赖库 curl, apr, apr-util, minixml
  2. 非交叉编译的情况非常简单,参考下即可
# 安装CMake sudo yum install cmake # 或 sudo apt-get install cmake # 或 sudo zypper install cmake  # 安装第三方库 (RedHat/Aliyun/CentOS) yum -y install libcurl-devel yum install apr-util yum install apr-devel yum install apr-util-devel wget "https://github.com/michaelrsweet/mxml/releases/download/release-2.9/mxml-2.9.tar.gz" tar zxf mxml-2.9.tar.gz cd mxml-2.9 ./configure make sudo make install  # 安装三方库 (Ubuntu/Debian) sudo apt-get install libcurl4-openssl-dev libapr1-dev libaprutil1-dev libmxml-dev  # 安装三方库(SuSE) sudo zypper install libcurl-devel libapr1-devel libapr-util1-devel mxml-devel   # 安装C SDK # https://github.com/aliyun/aliyun-oss-c-sdk/releases tar zxf aliyun-oss-c-sdk-3.10.0.tar.gz cd aliyun-oss-c-sdk-3.10.0 cmake . make sudo make install cmake -f CMakeLists.txt // 编译类型为Release。常用的编译类型为:Debug、Release、RelWithDebInfo和MinSizeRel,默认使用Debug。 -DCMAKE_BUILD_TYPE=Release // 自定义安装目录。 -DCMAKE_INSTALL_PREFIX=/usr/local/ // 指定curl、apr、apr-util和xml第三方库头文件和库文件的所在目录。 -DCURL_INCLUDE_DIR=/usr/include/curl -DCURL_LIBRARY=/usr/lib64/libcurl.so -DAPR_INCLUDE_DIR=/usr/include/apr-1 -DAPR_LIBRARY=/usr/lib64/libapr-1.so -DAPR_UTIL_INCLUDE_DIR=/usr/include/apr-1 -DAPR_UTIL_LIBRARY=/usr/lib64/libaprutil-1.so -DMINIXML_INCLUDE_DIR=/usr/include -DMINIXML_LIBRARY=/usr/lib64/libmxml.so // 编译时报错Could not find apr-config/apr-1-config,原因是在默认路径里面找不到apr-1-config文件,请添加该选项。 -DAPR_CONFIG_BIN=/path/to/bin/apr-1-config // 编译时报错Could not find apu-config/apu-1-config,原因是在默认路径里面找不到apu-1-config文件,请添加该选项。 -DAPU_CONFIG_BIN=/path/to/bin/apu-1-config  # 编译示例工程 wget https://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/assets/attach/32132/cn_zh/1487730555529/aliyun-oss-c-sdk-demo.tar.gz tar zxf aliyun-oss-c-sdk-demo.tar.gz  # 进入工程目录(oss-c-sdk-demo-xxx)执行make,编译示例工程。 cd oss-c-sdk-demo-xxx && make 

STEP BY STEP

接下来编译的都是arm64-v8a架构的,需要手动把thirdparty下对应的HPKBUILD里面的v7a干掉,参考

pkgname=minixml pkgver=3.3.1 pkgrel=0 pkgdesc="Mini-XML is a small XML parsing library that you can use to read XML data files or strings in your application without requiring large non-standard libraries." url="www.msweet.org/mxml" #重点看这里,只保留arm64-v8a archs=("arm64-v8a") license=("Apache License 2.0") depends=() makedepends=() 

编译curl

yum install perl -y yum install libtool -y yum install autoconf -y yum insatll m4 -y  # 执行这个顶掉yum安装的libtool, 有个依赖库需要2.2.6b版本 wget http://mirrors.kernel.org/gnu/libtool/libtool-2.2.6b.tar.gz \ tar xzvf libtool-2.2.6b.tar.gz \ cd libtool-2.2.6b \ ./configure --prefix=/usr make make install cd ..  # 进入lycium目录编译curl cd /home/ohos/tpc_c_cplusplus/lycium ./build.sh curl  # 注意观察libtool报错,版本过低的话,重新安装后重新执行编译 yum remove libtool yum install libtool -y ./build.sh curl 

编译apr

# 进入lycium目录编译apr cd /home/ohos/tpc_c_cplusplus/lycium ./build.sh curl  # 注意观察libtool报错,版本过低的话,重新安装后重新执行编译 yum remove libtool yum install libtool -y ./build.sh curl 

编译apr-util

包内不存在apr-util, 需要自己新增,直接贴代码

cd /home/ohos/tpc_c_cplusplus/thirdparty && mkdir apr-util && cd apr-util touch HPKBUILD touch SHA512SUM 

HPKBUILD内容

pkgname=apr-util pkgver=1.6.3 pkgrel=0 pkgdesc="APR-util provides a number of helpful abstractions on top of APR." url="https://apr.apache.org" archs=("arm64-v8a") license=("Apache License 2.0") depends=() makedepends=() source="https://dlcdn.apache.org//apr/apr-util-1.6.3.tar.gz" autounpack=true downloadpackage=true patchflag=true buildtools="configure"  builddir=$pkgname-${pkgver} packagename=$builddir.tar.gz  source envset.sh host=  prepare() {     if $patchflag     then         cd  $builddir         # 三方库中线程未实现,         # patch -p1 < `pwd`/../apr_oh_test.patch         # patch只需要打一次,关闭打patch         patchflag=false         cd $OLDPWD     fi      cp -rf ${builddir} $builddir-${ARCH}-build      if [ $ARCH == "armeabi-v7a" ]     then         setarm32ENV         host=arm-linux     elif [ $ARCH == "arm64-v8a" ]     then         setarm64ENV         host=aarch64-linux     else         echo "$ARCH not support!"         return -1     fi }  # ac_cv_file__dev_zero:表示检测系统中是否有名为/dev/zero的文件。 # ac_cv_func_setpgrp_void:表示检测系统中是否存在带有void类型参数的setpgrp函数。 # apr_cv_process_shared_works:表示检测系统中是否支持进程间共享内存。 # apr_cv_mutex_robust_shared:表示检测系统中是否支持共享互斥锁的鲁棒性。 # apr_cv_tcp_nodelay_with_cork:表示检测系统中是否支持同时启用TCP_NODELAY和TCP_CORK的选项。 # ap_void_ptr_lt_long:表示比较void*指针和long类型的整数时,void*是否小于long类型的整数。 # TCP_NODELAY=0 TCP_CORK=0:禁用 TCP_NODELAY 和 TCP_CORK 选项,以避免网络传输延迟。 # ac_cv_struct_rlimit:检查系统是否支持结构体 rlimit,用于限制进程的资源使用。 # ap_cv_void_ptr_lt_long:表示 `void*` 指针类型是否比 `long` 类型更小  build() {     cd $builddir-${ARCH}-build     ./configure "$@" --host=$host CFLAGS="-Wno-error=int-conversion" --enable-so  --with-mpm=worker --with-apr=$LYCIUM_ROOT/usr/apr/$ARCH --prefix=$LYCIUM_ROOT/usr/apr/$ARCH > `pwd`/build.log 2>&1     make -j4 VERBOSE=1 >> `pwd`/build.log 2>&1     ret=$?     cd $OLDPWD     cd $builddir-${ARCH}-build/test     make -j4 VERBOSE=1 >> `pwd`/build.log 2>&1     cd $OLDPWD     return $ret }  package() {     cd $builddir-${ARCH}-build     make install >> `pwd`/build.log 2>&1     ret=$?     cd $OLDPWD      unset host     if [ $ARCH == "armeabi-v7a" ]     then         unsetarm32ENV     elif [ $ARCH == "arm64-v8a" ]     then         unsetarm64ENV     else         echo "$ARCH not support!"         return -1     fi     return $ret }  check() {     echo "The test must be on an OpenHarmony device!"     # cd $builddir-${ARCH}-build/test     # ./testall }  # 清理环境 cleanbuild(){     rm -rf ${PWD}/${builddir} ${PWD}/${builddir}-armeabi-v7a-build ${PWD}/${builddir}-arm64-v8a-build #${PWD}/$packagename } 

SHA512SUM内容

25f078413dc552b3391845b3dc1da72773efe181634deb2f88a9c72a1a49c82883113704bf97db8327012ccafb84a68370267925e3c7cc092ed82fc33fd7954e  apr-util-1.6.3.tar.gz 

不对的话 自己手动下载后来一下,并粘贴到SHA512SUM去

shasum -a 512 apr-util-1.6.3.tar.gz 

最后还是一样的

# 进入lycium目录编译apr-util cd /home/ohos/tpc_c_cplusplus/lycium ./build.sh curl-util 

编译minixml

编译的是3.x.x版本,官方说4.x.x版本不向后兼容

cd /home/ohos/tpc_c_cplusplus/thirdparty && mkdir minixml && cd minixml touch HPKBUILD touch SHA512SUM 

HPKBUILD

pkgname=minixml pkgver=3.3.1 pkgrel=0 pkgdesc="Mini-XML is a small XML parsing library that you can use to read XML data files or strings in your application without requiring large non-standard libraries." url="www.msweet.org/mxml" archs=("arm64-v8a") license=("Apache License 2.0") depends=() makedepends=()  source="https://github.com/michaelrsweet/mxml/releases/download/v3.3.1/mxml-3.3.1.tar.gz"  autounpack=true downloadpackage=true patchflag=true buildtools="configure"  builddir=mxml-3.3.1 packagename=mxml-3.3.1.tar.gz  source envset.sh host=  prepare() {     if $patchflag     then         cd  $builddir         # 三方库中线程未实现,         # patch -p1 < `pwd`/../apr_oh_test.patch         # patch只需要打一次,关闭打patch         patchflag=false         cd $OLDPWD     fi      cp -rf ${builddir} $builddir-${ARCH}-build      if [ $ARCH == "armeabi-v7a" ]     then         setarm32ENV         host=arm-linux     elif [ $ARCH == "arm64-v8a" ]     then         setarm64ENV         host=aarch64-linux     else         echo "$ARCH not support!"         return -1     fi }  build() {     cd $builddir-${ARCH}-build     ./configure "$@" --host=$host CFLAGS="-Wno-error=int-conversion" --enable-so --prefix=$LYCIUM_ROOT/usr/$pkgname/$ARCH > `pwd`/build.log 2>&1     make -j4 VERBOSE=1 >> `pwd`/build.log 2>&1     ret=$?     cd $OLDPWD     cd $builddir-${ARCH}-build/test     make -j4 VERBOSE=1 >> `pwd`/build.log 2>&1     cd $OLDPWD     return $ret }  package() {     cd $builddir-${ARCH}-build     make install >> `pwd`/build.log 2>&1     ret=$?     cd $OLDPWD      unset host     if [ $ARCH == "armeabi-v7a" ]     then         unsetarm32ENV     elif [ $ARCH == "arm64-v8a" ]     then         unsetarm64ENV     else         echo "$ARCH not support!"         return -1     fi     return $ret }  check() {     echo "The test must be on an OpenHarmony device!"     # cd $builddir-${ARCH}-build/test     # ./testall }  # 清理环境 cleanbuild(){     rm -rf ${PWD}/${builddir} ${PWD}/${builddir}-armeabi-v7a-build ${PWD}/${builddir}-arm64-v8a-build #${PWD}/$packagename } 

SHA512SUM

ad72a19d4c1d06e44338b37e1ab3f13220b8bcba0bf5504cff9cef6e5eb0681aa51ec473219c20398ce3614c1bc1f1ca2fc490bf99b50a8896fb5ee3efb27c5c  mxml-3.3.1.tar.gz 

最后

# 进入lycium目录编译minixml cd /home/ohos/tpc_c_cplusplus/lycium ./build.sh minixml 

编译Aliyun_oss_c_sdk

新建third-party库

cd /home/ohos/tpc_c_cplusplus/thirdparty && mkdir aliyun-oss-c-sdk && cd aliyun-oss-c-sdk touch HPKBUILD touch SHA512SUM 

patch

需要对CMakeLists.txt打补丁
在tpc_c_cplusplus/thirdparty/aliyun-oss-c-sdk下新建oss_c_sdk_oh_pkg.patch,

touch oss_c_sdk_oh_pkg.patch 

并粘贴

diff -rupN aliyun-oss-c-sdk-3.11.0/CMakeLists.txt aliyun-oss-c-sdk-3.11.0_patch/CMakeLists.txt --- aliyun-oss-c-sdk-3.11.0/CMakeLists.txt	2023-03-20 21:49:10.000000000 +0800 +++ aliyun-oss-c-sdk-3.11.0/CMakeLists.txt	2023-05-10 14:51:49.579039952 +0800 @@ -117,4 +117,5 @@ include_directories(${APR_INCLUDE_DIR})  include_directories(${MINIXML_INCLUDE_DIR})  include_directories(${CURL_INCLUDE_DIR}) +link_libraries("-Wl,--allow-shlib-undefined" ${CURL_LIBRARY} ${APR_LIBRARY} ${APR_UTIL_LIBRARY} ${MINIXML_LIBRARY})   aux_source_directory(oss_c_sdk SRC_LIST) 

打补丁动作写在HPKBUILD内,构建时会自动打

SHA512SUM

0a382d3ac90f7ae40d70e200b06c26a869e890634ab696eb9c557300682dedd30cf1f84ddde9f6fa3ce789f4320da14713aa3650f55c89964206f087f630c648  aliyun-oss-c-sdk-3.11.0.tar.gz 8a253b847a8ba237b04192f3bcafe8cc20c29cba9d877ae6e9864e52c5662376607a0a64154e9a27e289b4058942ff1d4d97ea99c07eedd48455c748da15be9d  oss_c_sdk_oh_pkg.patch 

若oss_c_sdk_oh_pkg.patch不匹配,则手动搞一下再换一下,参考上面

HPKBUILD

pkgname=aliyun-oss-c-sdk pkgver=3.11.0 pkgrel=0 pkgdesc="Alibaba Cloud Object Storage Service (OSS) is a cloud storage service provided by Alibaba Cloud, featuring massive capacity, security, a low cost, and high reliability." url="http://oss.aliyun.com/" archs=("arm64-v8a") license=("Apache License 2.0") depends=("curl" "apr" "apr-util" "minixml") makedepends=()  source="https://github.com/aliyun/aliyun-oss-c-sdk/archive/refs/tags/3.11.0.tar.gz"  autounpack=true downloadpackage=true patchflag=true buildtools="cmake"  builddir=aliyun-oss-c-sdk-3.11.0 packagename=aliyun-oss-c-sdk-3.11.0.tar.gz patchflag=true  prepare() {     if $patchflag     then         cd $builddir         # 编译识别 OpenHarmony 系统         patch -p1 < `pwd`/../oss_c_sdk_oh_pkg.patch         # patch只需要打一次,关闭打patch         patchflag=false         cd $OLDPWD     fi      mkdir -p $builddir/$ARCH-build }  build() {     cd $builddir     cmake -DCMAKE_TOOLCHAIN_FILE=${OHOS_SDK}/native/build/cmake/ohos.toolchain.cmake "$@" -DOHOS_ARCH=$ARCH -DCURL_INCLUDE_DIR=$LYCIUM_ROOT/usr/curl/$ARCH/include -DCURL_LIBRARY=$LYCIUM_ROOT/usr/curl/$ARCH/lib/libcurl.so -DAPR_INCLUDE_DIR=$LYCIUM_ROOT/usr/apr/$ARCH/include/apr-1 -DAPR_LIBRARY=$LYCIUM_ROOT/usr/apr/$ARCH/lib/libapr-1.so -DAPR_UTIL_INCLUDE_DIR=$LYCIUM_ROOT/usr/apr/$ARCH/include/apr-1 -DAPR_UTIL_LIBRARY=$LYCIUM_ROOT/usr/apr/$ARCH/lib/libaprutil-1.so -DMINIXML_INCLUDE_DIR=$LYCIUM_ROOT/usr/minixml/$ARCH/include -DMINIXML_LIBRARY=$LYCIUM_ROOT/usr/minixml/$ARCH/lib/libmxml.so -DAPR_CONFIG_BIN=$LYCIUM_ROOT/usr/apr/$ARCH/bin/apr-1-config -DAPU_CONFIG_BIN=$LYCIUM_ROOT/usr/apr/$ARCH/bin/apu-1-config -B$ARCH-build -S./ -L > `pwd`/$ARCH-build/build.log 2>&1     make -j4 -C $ARCH-build >> `pwd`/$ARCH-build/build.log 2>&1     ret=$?     cd $OLDPWD     return $ret }  package() {     cd "$builddir"     make -C $ARCH-build install >> `pwd`/$ARCH-build/build.log 2>&1     cd $OLDPWD }  check() {     echo "The test must be on an OpenHarmony device!" }  # 清理环境 cleanbuild(){     rm -rf ${PWD}/$builddir #${PWD}/$packagename } 

最后

# 进入lycium目录编译aliyun-oss-c-sdk cd /home/ohos/tpc_c_cplusplus/lycium ./build.sh aliyun-oss-c-sdk 

产物

https://github.com/htliang128/tpc_c_cplusplus/tree/main/lycium/usr

参考

  1. tpc_c_cplusplus
  2. 阿里云OSS官方文档

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!