区长

CURL NDK 交叉编译

移植curl到android,且支持https和http2.0

依赖前两篇文章

创建工作目录,并进入

1
2
mkdir android
cd android

下载源码

1
2
wget https://curl.haxx.se/download/curl-7.53.1.tar.gz
tar xfz url-7.53.1.tar.gz

生成交叉编译工具链

1
$ANDROID_NDK/build/tools/make-standalone-toolchain.sh --arch=arm --install-dir=./toolchain

导出环境变量(armeabi)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
export ANDROID_HOME=`pwd`
export TOOLCHAIN=$ANDROID_HOME/toolchain
export PKG_CONFIG_LIBDIR=$TOOLCHAIN/lib/pkgconfig
export CROSS_SYSROOT=$TOOLCHAIN/sysroot
export PATH=$TOOLCHAIN/bin:$PATH
export TOOL=arm-linux-androideabi
export CC=$TOOLCHAIN/bin/${TOOL}-gcc
export CXX=$TOOLCHAIN/bin/${TOOL}-g++
export LINK=${CXX}
export LD=$TOOLCHAIN/bin/${TOOL}-ld
export AR=$TOOLCHAIN/bin/${TOOL}-ar
export RANLIB=$TOOLCHAIN/bin/${TOOL}-ranlib
export STRIP=$TOOLCHAIN/bin/${TOOL}-strip
export ARCH_FLAGS="-mthumb"
export ARCH_LINK=
export CFLAGS="${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64"
export CXXFLAGS="${CFLAGS} -frtti -fexceptions"
export LDFLAGS="${ARCH_LINK}"

编译并安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
cd curl-7.53.1
autoreconf -i
./configure --prefix=$TOOLCHAIN/sysroot/usr/local \
--with-sysroot=$TOOLCHAIN/sysroot \
--host=arm-linux-androideabi \
--with-ssl=$TOOLCHAIN/sysroot/usr/local \
--with-nghttp2=$TOOLCHAIN/sysroot/usr/local \
--enable-ipv6 \
--enable-static \
--enable-threaded-resolver \
--disable-dict \
--disable-gopher \
--disable-ldap --disable-ldaps \
--disable-manual \
--disable-pop3 --disable-smtp --disable-imap \
--disable-rtsp \
--disable-shared \
--disable-smb \
--disable-telnet \
--disable-verbose
make -j4
make install

卸载

1
make uninstall

configure完成后检查输出结果是否enable ssl, enable https, enable http2.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Configured to build curl/libcurl:

curl version: 7.53.1
Host setup: arm-unknown-linux-androideabi
Install prefix: /Users/lizhangqu/Desktop/android/toolchain/sysroot/usr/local
Compiler: /Users/lizhangqu/Desktop/android/toolchain/bin/arm-linux-androideabi-gcc
SSL support: enabled (OpenSSL)
SSH support: no (--with-libssh2)
zlib support: enabled
GSS-API support: no (--with-gssapi)
TLS-SRP support: enabled
resolver: POSIX threaded
IPv6 support: enabled
Unix sockets support: enabled
IDN support: no (--with-{libidn2,winidn})
Build libcurl: Shared=no, Static=yes
Built-in manual: no (--enable-manual)
--libcurl option: enabled (--disable-libcurl-option)
Verbose errors: no
SSPI support: no (--enable-sspi)
ca cert bundle: no
ca cert path: no
ca fallback: no
LDAP support: no (--enable-ldap / --with-ldap-lib / --with-lber-lib)
LDAPS support: no (--enable-ldaps)
RTSP support: no (--enable-rtsp)
RTMP support: no (--with-librtmp)
metalink support: no (--with-libmetalink)
PSL support: no (libpsl not found)
HTTP2 support: enabled (nghttp2)
Protocols: FILE FTP FTPS HTTP HTTPS TFTP

SONAME bump: yes - WARNING: this library will be built with the SONAME
number bumped due to (a detected) ABI breakage.
See lib/README.curl_off_t for details on this.

armeabi-v7a

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ANDROID_NDK/build/tools/make-standalone-toolchain.sh --arch=arm --install-dir=./toolchain
export TOOL=arm-linux-androideabi
export ARCH_FLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16"
export ARCH_LINK="-march=armv7-a -Wl,--fix-cortex-a8"

./configure --prefix=$TOOLCHAIN/sysroot/usr/local \
--with-sysroot=$TOOLCHAIN/sysroot \
--host=arm-linux-androideabi \
--with-ssl=$TOOLCHAIN/sysroot/usr/local \
--with-nghttp2=$TOOLCHAIN/sysroot/usr/local \
--enable-ipv6 \
--enable-static \
--enable-threaded-resolver \
--disable-dict \
--disable-gopher \
--disable-ldap --disable-ldaps \
--disable-manual \
--disable-pop3 --disable-smtp --disable-imap \
--disable-rtsp \
--disable-shared \
--disable-smb \
--disable-telnet \
--disable-verbose

x86

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ANDROID_NDK/build/tools/make-standalone-toolchain.sh --arch=x86 --install-dir=./toolchain
export TOOL=i686-linux-android
export ARCH_FLAGS="-march=i686 -msse3 -mstackrealign -mfpmath=sse"
export ARCH_LINK=""

./configure --prefix=$TOOLCHAIN/sysroot/usr/local \
--with-sysroot=$TOOLCHAIN/sysroot \
--host=i686-linux-android \
--with-ssl=$TOOLCHAIN/sysroot/usr/local \
--with-nghttp2=$TOOLCHAIN/sysroot/usr/local \
--enable-ipv6 \
--enable-static \
--enable-threaded-resolver \
--disable-dict \
--disable-gopher \
--disable-ldap --disable-ldaps \
--disable-manual \
--disable-pop3 --disable-smtp --disable-imap \
--disable-rtsp \
--disable-shared \
--disable-smb \
--disable-telnet \
--disable-verbose

何大仙提供的shell脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/sh

if [! -f "curl-7.53.1.tar.gz" ]; then
wget https://curl.haxx.se/download/curl-7.53.1.tar.gz
fi

if [! -d "curl-7.53.1" ]; then
tar zxf curl-7.53.1.tar.gz
fi

# env
if [-d "out/curl" ]; then
rm -fr "out/curl"
fi

mkdir "out"
mkdir "out/curl"

_compile() {
SURFIX=$1
TOOL=$2
ARCH_FLAGS=$3
ARCH_LINK=$4
ARCH=$5

if [! -d "out/curl/${SURFIX}" ]; then
mkdir "out/curl/${SURFIX}"
fi

if [! -d "toolchain_${SURFIX}" ]; then
$ANDROID_NDK/build/tools/make-standalone-toolchain.sh --arch=${ARCH} --install-dir=./toolchain_${SURFIX}
fi

export ANDROID_HOME=`pwd`
export TOOLCHAIN=$ANDROID_HOME/toolchain_${SURFIX}
export PKG_CONFIG_LIBDIR=$TOOLCHAIN/lib/pkgconfig
export CROSS_SYSROOT=$TOOLCHAIN/sysroot
export PATH=$TOOLCHAIN/bin:$PATH
export CC=$TOOLCHAIN/bin/${TOOL}-gcc
export CXX=$TOOLCHAIN/bin/${TOOL}-g++
export LINK=${CXX}
export LD=$TOOLCHAIN/bin/${TOOL}-ld
export AR=$TOOLCHAIN/bin/${TOOL}-ar
export RANLIB=$TOOLCHAIN/bin/${TOOL}-ranlib
export STRIP=$TOOLCHAIN/bin/${TOOL}-strip
export ARCH_FLAGS=$ARCH_FLAGS
export ARCH_LINK=$ARCH_LINK
export CFLAGS="${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64"
export CXXFLAGS="${CFLAGS} -frtti -fexceptions"
export LDFLAGS="${ARCH_LINK}"

cd curl-7.53.1/
autoreconf -i
./configure --prefix=$TOOLCHAIN/sysroot/usr/local \
--with-sysroot=$TOOLCHAIN/sysroot \
--host=${TOOL} \
--with-ssl=$TOOLCHAIN/sysroot/usr/local \
--with-nghttp2=$TOOLCHAIN/sysroot/usr/local \
--enable-ipv6 \
--enable-static \
--enable-threaded-resolver \
--disable-dict \
--disable-gopher \
--disable-ldap --disable-ldaps \
--disable-manual \
--disable-pop3 --disable-smtp --disable-imap \
--disable-rtsp \
--disable-shared \
--disable-smb \
--disable-telnet \
--disable-verbose
make clean
make -j4
make install
cd ..
mv curl-7.53.1/lib/.libs/libcurl.a out/curl/${SURFIX}/
}

# arm
_compile "armeabi" "arm-linux-androideabi" "-mthumb" "" "arm"

# armv7
_compile "armeabi-v7a" "arm-linux-androideabi" "-march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16" "-march=armv7-a -Wl,--fix-cortex-a8" "arm"

# arm64v8
_compile "arm64-v8a" "aarch64-linux-android" "" "" "arm64"

# x86
_compile "x86" "i686-linux-android" "-march=i686 -m32 -msse3 -mstackrealign -mfpmath=sse -mtune=intel" "" "x86"

# x86_64
_compile "x86_64" "x86_64-linux-android" "-march=x86-64 -m64 -msse4.2 -mpopcnt -mtune=intel" "" "x86_64"

# mips
_compile "mips" "mipsel-linux-android" "" "" "mips"

# mips64
_compile "mips64" "mips64el-linux-android" "" "" "mips64"

echo "done"
坚持原创技术分享,您的支持将鼓励我继续创作!
区长 WeChat Pay

微信打赏