使用NDK编译OpenSSL与curl
0. 编译环境
-
Ubuntu 20.04
-
OpenSSL-1.1.0i
-
curl-7.76.0
1. 声明编译环境
新建编译环境相关变量设置脚本,并执行该脚本:
hyj@hyj-ubuntu:~/workspace/curl_x64_r12b$ cat envsetup.sh
#!/bin/sh
#adjust the following two line definition to your develop environment
NDK_ROOT=/home/hyj/workspace/android-ndk-r12b
HOST_PLATFORM=linux-x86_64
TARGET_PLATFORM=ARM_V3S
#below lines always keep not changed
PROJECT_ROOT=$(pwd)
INSTALL_ROOT=${PROJECT_ROOT}/output
SYSROOT=${NDK_ROOT}/platforms/android-21/arch-arm64
CROSS_TOOLCHAIN=${NDK_ROOT}/toolchains/aarch64-linux-android-4.9/prebuilt/${HOST_PLATFORM}/bin/aarch64-linux-android-
CONFIGURE_PARAMETERS="--with-sysroot={SYSROOT} --host=arm-linux --prefix=${LIBRARY_INSTALL_PATH}"
CFLAGS="-D$TARGET_PLATFORM --sysroot=${SYSROOT} \
-Wformat -Werror=format-security -fexceptions -Os -fno-omit-frame-pointer -ggdb3 \
-ffunction-sections -funwind-tables -fstack-protector -no-canonical-prefixes -Os -g -DNDEBUG \
-fno-strict-aliasing -finline-limit=64 -O3 -I${SYSROOT}/usr/include \
-I${NDK_ROOT}/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x/include -I${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.9/include \
-I${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include/"
LDFLAGS="-lgcc ${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libgnustl_static.a \
-no-canonical-prefixes -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now \
-L${NDK_ROOT}/platforms/android-21/arch-arm64/usr/lib \
${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libsupc++.a -lc -lm \
--sysroot=${NDK_ROOT}/platforms/android-21/arch-arm64"
mkdir -p $INSTALL_ROOT
CC=${CROSS_TOOLCHAIN}gcc
CPP="${CROSS_TOOLCHAIN}cpp --sysroot=${SYSROOT}"
CXX=${CROSS_TOOLCHAIN}g++
CXXCPP="${CROSS_TOOLCHAIN}cpp --sysroot=${SYSROOT}"
RANLIB=${CROSS_TOOLCHAIN}ranlib
LD=${CROSS_TOOLCHAIN}ld
AR=${CROSS_TOOLCHAIN}ar
STRIP=${CROSS_TOOLCHAIN}strip
PROJECT_ROOT=${PROJECT_ROOT} \
INSTALL_ROOT=${INSTALL_ROOT} \
SYSROOT=${SYSROOT} \
CFLAGS=${CFLAGS} \
LDFLAGS=${LDFLAGS} \
CONFIGURE_PARAMETERS=${CONFIGURE_PARAMETERS} \
CROSS_TOOLCHAIN=${CROSS_TOOLCHAIN} \
CC=${CC} \
CPP=${CPP} \
CXX=${CXX} \
CXXCPP=${CXXCPP} \
LD=${LD} \
RANLIB=${RANLIB} \
AR=${AR} \
STRIP=${STRIP} \
bash
hyj@hyj-ubuntu:~/workspace/curl_x64_r12b$ ./envsetup.sh
hyj@hyj-ubuntu:~/workspace/curl_x64_r12b$
将编译链与相关源码下载并解压到本地之后,工程目录下的内容如下:
hyj@hyj-ubuntu:~/workspace/curl_x64_r12b$ tree -L 1
.
├── curl-7.76.0
├── curl-7.76.0.tar.bz2
├── envsetup.sh
├── openssl-1.1.0i
├── openssl-1.1.0i.tar.gz
└── output
3 directories, 3 files
2. 编译OpenSSL
进入OpenSSL目录,执行工程配置命令:
cd openssl-1.1.0i
./config --prefix=${INSTALL_ROOT}/openssl no-asm no-threads -shared --sysroot=${SYSROOT}
编辑openssl-1.1.0i/Makefile,删除该文件中所有的"-m64"选项,并进行编译与安装相关文件:
make -j8
make install
${CROSS_TOOLCHAIN}strip ${INSTALL_ROOT}/openssl/lib/*.so*
进入output目录,能够看到适用于ARM x64架构的OpenSSL的相关文件已经生成:
hyj@hyj-ubuntu:~/workspace/curl_x64_r12b/output$ tree -L 3
.
└── openssl
├── bin
│ ├── c_rehash
│ └── openssl
├── include
│ └── openssl
├── lib
│ ├── engines-1.1
│ ├── libcrypto.a
│ ├── libcrypto.so -> libcrypto.so.1.1
│ ├── libcrypto.so.1.1
│ ├── libssl.a
│ ├── libssl.so -> libssl.so.1.1
│ ├── libssl.so.1.1
│ └── pkgconfig
├── share
│ ├── doc
│ └── man
└── ssl
├── certs
├── misc
├── openssl.cnf
├── openssl.cnf.dist
└── private
14 directories, 10 files
hyj@hyj-ubuntu:~/workspace/curl_x64_r12b/output$ file openssl/lib/*
openssl/lib/engines-1.1: directory
openssl/lib/libcrypto.a: current ar archive
openssl/lib/libcrypto.so: symbolic link to libcrypto.so.1.1
openssl/lib/libcrypto.so.1.1: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, stripped
openssl/lib/libssl.a: current ar archive
openssl/lib/libssl.so: symbolic link to libssl.so.1.1
openssl/lib/libssl.so.1.1: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, stripped
openssl/lib/pkgconfig: directory
3. 编译curl
进入curl源码目录,执行工程配置命令,并进行编译与安装:
./configure --with-sysroot=${SYSROOT} --host=arm-linux --prefix=${INSTALL_ROOT}/curl --with-ssl=${INSTALL_ROOT}/openssl --disable-all --enable-shared --enable-static --without-libidn --without-librtmp --without-gnutls --without-nss --without-libssh2 --without-zlib --without-winidn --disable-rtsp --disable-ldap --disable-ldaps --disable-ipv6
make clean
make -j8
make install
${CROSS_TOOLCHAIN}strip ${INSTALL_ROOT}/curl/lib/*.so*
进入output目录,能够看到适用于ARM x64架构的curl的相关文件已经生成:
hyj@hyj-ubuntu:~/workspace/curl_x64_r12b/output$ tree -L 3
.
├── curl
│ ├── bin
│ │ ├── curl
│ │ └── curl-config
│ ├── include
│ │ └── curl
│ ├── lib
│ │ ├── libcurl.a
│ │ ├── libcurl.la
│ │ ├── libcurl.so -> libcurl.so.4.7.0
│ │ ├── libcurl.so.4 -> libcurl.so.4.7.0
│ │ ├── libcurl.so.4.7.0
│ │ └── pkgconfig
│ └── share
│ ├── aclocal
│ └── man
└── openssl
├── bin
│ ├── c_rehash
│ └── openssl
├── include
│ └── openssl
├── lib
│ ├── engines-1.1
│ ├── libcrypto.a
│ ├── libcrypto.so -> libcrypto.so.1.1
│ ├── libcrypto.so.1.1
│ ├── libssl.a
│ ├── libssl.so -> libssl.so.1.1
│ ├── libssl.so.1.1
│ └── pkgconfig
├── share
│ ├── doc
│ └── man
└── ssl
├── certs
├── misc
├── openssl.cnf
├── openssl.cnf.dist
└── private
23 directories, 17 files
hyj@hyj-ubuntu:~/workspace/curl_x64_r12b/output$ file openssl/lib/*.so*
openssl/lib/libcrypto.so: symbolic link to libcrypto.so.1.1
openssl/lib/libcrypto.so.1.1: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, stripped
openssl/lib/libssl.so: symbolic link to libssl.so.1.1
openssl/lib/libssl.so.1.1: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, stripped
hyj@hyj-ubuntu:~/workspace/curl_x64_r12b/output$