一.环境准备
1.参考说明
NDK开发介绍:https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/ndk-development-overview.md
2.NDK下载
点击介绍页面中的链接可以跳转到相应下载页面:
下载相应版本:
下载完毕后解压到指定目录并且设置环境变量,比如:
# 解压
tar -xzvf ohos-sdk-windows_linux-public.tar.gz /home/ubuntu/develop/ohos-sdk-5.0
# 导出环境变量,后续编译脚本里面会使用这个变量
export OHOS_SDK=/home/ubuntu/develop/ohos-sdk-5.0/linux
二.编译openssl
1.修改编译配置文件,增加ohos编译配置
以openssl-1.1.1m举例,配置文件路径:openssl-1.1.1m\Configurations\10-main.conf
"ohos-aarch64" => {
inherit_from => [ "linux-aarch64" ],
shared_extension => ".so"
},
"ohos-arm" => {
inherit_from => [ "linux-generic32" ],
asm_arch => 'armv4',
perlasm_scheme => "linux32",
shared_extension => ".so"
},
"ohos-x86_64" => {
inherit_from => [ "linux-x86_64" ],
shared_extension => ".so"
},
2.使用编译脚本进行编译
编译脚本:
#!/bin/bash
if [[ -z "${OHOS_SDK}" ]]; then
echo "OHOS_SDK is not set or is empty"
exit 1
else
echo "harmony ndk path ${OHOS_SDK}"
fi
file_dir_name=$(dirname "$0")
cd $file_dir_name
func_err_check()
{
if [ $1 -ne 0 ]; then echo "exec err: $2"; exit 1; fi;
}
export OHOS_NDK_HOME=$OHOS_SDK
export CC=$OHOS_NDK_HOME/native/llvm/bin/clang
export CXX=$OHOS_NDK_HOME/native/llvm/bin/clang++
export AR=$OHOS_NDK_HOME/native/llvm/bin/llvm-ar
export AS=$OHOS_NDK_HOME/native/llvm/bin/llvm-as
export LD=$OHOS_NDK_HOME/native/llvm/bin/ld.lld
export STRIP=$OHOS_NDK_HOME/native/llvm/bin/llvm-strip
export RANLIB=$OHOS_NDK_HOME/native/llvm/bin/llvm-ranlib
export OBJDUMP=$OHOS_NDK_HOME/native/llvm/bin/llvm-objdump
export OBJCOPY=$OHOS_NDK_HOME/native/llvm/bin/llvm-objcopy
export NM=$OHOS_NDK_HOME/native/llvm/bin/llvm-nm
export CFLAGS="-target aarch64-linux-ohos --sysroot=${OHOS_NDK_HOME}/native/sysroot -D__MUSL__"
export CXXFLAGS="-target aarch64-linux-ohos --sysroot=${OHOS_NDK_HOME}/native/sysroot -D__MUSL__"
cd ../openssl-1.1.1m/
chmod +x ./Configure
./Configure no-zlib no-shared ohos-aarch64 --prefix=$PWD/../../Harmony_aarch64
func_err_check $? "check ohos-aarch64 config"
make
make install
$PWD/../../Harmony_aarch64 是一个用相对路径表示的统一编译安装目录,openssl和curl都会引用以及安装到这个目录下,所以要保证两个开源库使用的编译脚本中的这个相对路径指向同一目录。如果有什么问题的话,也可以自己手动改成想要的路径,两个脚本一致就行。
三.编译curl
1.使用编译脚本进行编译
curl使用的版本是 7.81
#!/bin/bash
if [[ -z "${OHOS_SDK}" ]]; then
echo "OHOS_SDK is not set or is empty"
exit 1
else
echo "harmony ndk path ${OHOS_SDK}"
fi
file_dir_name=$(dirname "$0")
cd $file_dir_name
func_err_check()
{
if [ $1 -ne 0 ]; then echo "exec err: $2"; exit 1; fi;
}
OPENSSL_DIR_ARM64="${PWD}/../../Harmony_aarch64"
export OHOS_NDK_HOME=$OHOS_SDK
export CC=$OHOS_NDK_HOME/native/llvm/bin/clang
export CXX=$OHOS_NDK_HOME/native/llvm/bin/clang++
export AR=$OHOS_NDK_HOME/native/llvm/bin/llvm-ar
export AS=$OHOS_NDK_HOME/native/llvm/bin/llvm-as
export LD=$OHOS_NDK_HOME/native/llvm/bin/ld.lld
export STRIP=$OHOS_NDK_HOME/native/llvm/bin/llvm-strip
export RANLIB=$OHOS_NDK_HOME/native/llvm/bin/llvm-ranlib
export OBJDUMP=$OHOS_NDK_HOME/native/llvm/bin/llvm-objdump
export OBJCOPY=$OHOS_NDK_HOME/native/llvm/bin/llvm-objcopy
export NM=$OHOS_NDK_HOME/native/llvm/bin/llvm-nm
export CFLAGS="-target aarch64-linux-ohos --sysroot=${OHOS_NDK_HOME}/native/sysroot -D__MUSL__"
export CXXFLAGS="-target aarch64-linux-ohos --sysroot=${OHOS_NDK_HOME}/native/sysroot -D__MUSL__"
cd ../curl-7.81.0/
chmod +x ./configure
./configure --host aarch64-linux-android --disable-rtsp --without-zlib --with-pic --disable-shared --disable-dependency-tracking --with-openssl="${OPENSSL_DIR_ARM64}" --prefix=$PWD/../../Harmony_aarch64
func_err_check $? "check ohos-aarch64 config"
make
make install
四.编译结果
如果中间没有错误的话,在 xxx/Harmony_aarch64/lib 下会生成以下文件和文件夹
目前我编译的全部都是静态库,如果需要编译动态库的话,还需要调整下编译脚本中的参数。具体的参数可以查看openssl和curl的编译说明。