1. libjpeg-turbo
前面已经编译过1.5.9
[1]版本的libjpeg-turbo,现在版本升级到2.0.1
,基于CMake
编译。
依然还是一样,按照官网介绍,libjpeg-turbo
比libjpeg
快2-6
倍,得益于它高度优化的哈夫曼算法。在许多情况下,libjpeg-turbo
的性能可与专有的高速JPEG编解码器相媲美.
On such systems, libjpeg-turbo is generally
2-6x
as fast as libjpeg, all else being equal. On other types of systems, libjpeg-turbo can still outperform libjpeg by a significant amount, by virtue of its highly-optimized Huffman coding routines. In many cases, the performance of libjpeg-turbo rivals that of proprietary high-speed JPEG codecs
2. 编译过程
2.1 编译环境
在Windows 10的虚拟机VM上的Ubuntu中编译
2.2 编译要求
我们将参照源码中的BUILDING.md
编译,github地址BUILDING.md
NDK
Linux 64 位 (x86) / android-ndk-r15c
CMake 或 android-cmake
cmake-3.12.1-Linux-x86_64
NASM 或者 YASM
如果需要编译x86
或者 x86-64
的库,需要安装NASM 2.10及以上或者YASM 2.10及以上版本。
2.3 配置环境变量
把Cmake配置进系统环境变量,当然这不是必须的。
2.4 编译
2.4.1 下载源码
下载并解压源码https://github.com/libjpeg-turbo/libjpeg-turbo
2.4.2 编译脚本
2.4.2.1 sh编译脚本(自定义编译链)
注意
这种方式是完全按照源码中的BUILDING.md
构建,稍显复杂,容易出错。但是不要求CMake的交叉编译工具链,也就是说,我们随便在CMake官网下载Linux版本就可以用了。
#!/bin/sh
# lib-name
MY_LIBS_NAME=libjpeg-turbo
MY_SOURCE_DIR=$(pwd)/libjpeg-turbo-master
#编译的过程中产生的中间件的存放目录,为了区分编译目录,源码目录,install目录
MY_BUILD_DIR=binary
export PATH=/home/as/Android/cmake-3.12.1-Linux-x86_64/bin:$PATH
NDK_PATH=/home/as/Android/android-ndk-r15c
BUILD_PLATFORM=linux-x86_64
TOOLCHAIN_VERSION=4.9
ANDROID_VERSION=24
ANDROID_ARMV5_CFLAGS="-march=armv5te"
ANDROID_ARMV7_CFLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=neon" # -mfpu=vfpv3-d16 -fexceptions -frtti
ANDROID_ARMV8_CFLAGS="-march=armv8-a" # -mfloat-abi=softfp -mfpu=neon -fexceptions -frtti
ANDROID_X86_CFLAGS="-march=i386 -mtune=intel -mssse3 -mfpmath=sse -m32"
ANDROID_X86_64_CFLAGS="-march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel"
# params($1:arch,$2:arch_abi,$3:host,$4:compiler,$5:cflags,$6:processor)
build_bin() {
echo "-------------------star build $2-------------------------"
ARCH=$1 # arm arm64 x86 x86_64
ANDROID_ARCH_ABI=$2 # armeabi armeabi-v7a x86 mips
# 最终编译的安装目录
PREFIX=$(pwd)/dist/${MY_LIBS_NAME}/${ANDROID_ARCH_ABI}/
HOST=$3
COMPILER=$4
PROCESSOR=$6
SYSROOT=${NDK_PATH}/platforms/android-${ANDROID_VERSION}/arch-${ARCH}
CFALGS="$5"
TOOLCHAIN=${NDK_PATH}/toolchains/${HOST}-${TOOLCHAIN_VERSION}/prebuilt/${BUILD_PLATFORM}
# build 中间件
BUILD_DIR=./${MY_BUILD_DIR}/${ANDROID_ARCH_ABI}
export CFLAGS="$5 -Os -D__ANDROID_API__=${ANDROID_VERSION} --sysroot=${SYSROOT} \
-isystem ${NDK_PATH}/sysroot/usr/include \
-isystem ${NDK_PATH}/sysroot/usr/include/${HOST} "
export LDFLAGS=-pie
echo "path==>$PATH"
echo "build_dir==>$BUILD_DIR"
echo "ARCH==>$ARCH"
echo "ANDROID_ARCH_ABI==>$ANDROID_ARCH_ABI"
echo "HOST==>$HOST"
echo "CFALGS==>$CFALGS"
echo "COMPILER==>$COMPILER-gcc"
echo "PROCESSOR==>$PROCESSOR"
mkdir -p ${BUILD_DIR} #创建当前arch_abi的编译目录,比如:binary/armeabi-v7a
cd ${BUILD_DIR} #此处 进了当前arch_abi的2级编译目录
cat >toolchain.cmake << EOF
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR $6)
set(CMAKE_C_COMPILER ${TOOLCHAIN}/bin/${COMPILER}-gcc)
set(CMAKE_FIND_ROOT_PATH ${TOOLCHAIN}/${COMPILER})
EOF # 此处不顶格写脚本会报一个错误
cmake -G"Unix Makefiles" \
-DCMAKE_TOOLCHAIN_FILE=toolchain.cmake \
-DCMAKE_POSITION_INDEPENDENT_CODE=1 \
-DCMAKE_INSTALL_PREFIX=${PREFIX} \
-DWITH_JPEG8=1 \
${MY_SOURCE_DIR}
make clean
make
make install
#从当前arch_abi编译目录跳出,对应上面的cd ${BUILD_DIR},以便function多次执行
cd ../../
echo "-------------------$2 build end-------------------------"
}
# build armeabi
build_bin arm armeabi arm-linux-androideabi arm-linux-androideabi "$ANDROID_ARMV5_CFLAGS" arm
#build armeabi-v7a
build_bin arm armeabi-v7a arm-linux-androideabi arm-linux-androideabi "$ANDROID_ARMV7_CFLAGS" arm
#build arm64-v8a
build_bin arm64 arm64-v8a aarch64-linux-android aarch64-linux-android "$ANDROID_ARMV8_CFLAGS" aarch64
#build x86
build_bin x86 x86 x86 i686-linux-android "$ANDROID_X86_CFLAGS" i386
#build x86_64
build_bin x86_64 x86_64 x86_64 x86_64-linux-android "$ANDROID_X86_64_CFLAGS" x86_64
2.4.2.2 android-cmake(自带编译链)
注意
这个脚本要求Linux环境的Android-cmake
,通过Android sdk-manager
下载更新cmake,android-cmake
已经自带交叉编译工具链,但通常我们不使用cmake带的,因为他脚本里写死了NDK版本12,而NDK的build/cmake
目录里也带了相应的编译链,所以,我们一般使用我们NDK自带的编译链。
# lib-name
MY_LIBS_NAME=libjpeg-turbo
MY_SOURCE_DIR=$(pwd)/libjpeg-turbo-master
MY_BUILD_DIR=binary
CMAKE_PATH=/home/as/Android/cmake-3.6.4111459-linux-x86_64
export PATH=${CMAKE_PATH}/bin:$PATH
NDK_PATH=/home/as/Android/android-ndk-r15c
BUILD_PLATFORM=linux-x86_64
TOOLCHAIN_VERSION=4.9
ANDROID_VERSION=24
ANDROID_ARMV5_CFLAGS="-march=armv5te"
ANDROID_ARMV7_CFLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=neon" # -mfpu=vfpv3-d16 -fexceptions -frtti
ANDROID_ARMV8_CFLAGS="-march=armv8-a " # -mfloat-abi=softfp -mfpu=neon -fexceptions -frtti
ANDROID_X86_CFLAGS="-march=i386 -mtune=intel -mssse3 -mfpmath=sse -m32"
ANDROID_X86_64_CFLAGS="-march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel"
# params($1:arch,$2:arch_abi,$3:host,$4:compiler,$5:cflags,$6:processor)
build_bin() {
echo "-------------------start build $1-------------------------"
ANDROID_ARCH_ABI=$1 # armeabi armeabi-v7a x86 mips
CFALGS="$2"
PREFIX=$(pwd)/dist/${MY_LIBS_NAME}/${ANDROID_ARCH_ABI}/
# build 中间件
BUILD_DIR=./${MY_BUILD_DIR}/${MY_LIBS_NAME}/${ANDROID_ARCH_ABI}
echo "path==>$PATH"
echo "build_dir==>$BUILD_DIR"
echo "ANDROID_ARCH_ABI==>$ANDROID_ARCH_ABI"
echo "CFALGS==>$CFALGS"
mkdir -p ${BUILD_DIR}
cd ${BUILD_DIR}
# -DCMAKE_MAKE_PROGRAM=${NDK_PATH}/prebuilt/${BUILD_PLATFORM}/bin/make \
# -DCMAKE_ASM_COMPILER=${NDK_PATH}/prebuilt/${BUILD_PLATFORM}/bin/yasm \
cmake -G"Unix Makefiles" \
-DANDROID_ABI=${ANDROID_ARCH_ABI} \
-DANDROID_PLATFORM=android-${ANDROID_VERSION} \
-DCMAKE_BUILD_TYPE=Release \
-DANDROID_NDK=${NDK_PATH} \
-DCMAKE_TOOLCHAIN_FILE=${NDK_PATH}/build/cmake/android.toolchain.cmake \
-DCMAKE_POSITION_INDEPENDENT_CODE=1 \
-DCMAKE_INSTALL_PREFIX=${PREFIX} \
-DANDROID_ARM_NEON=TRUE \
-DANDROID_TOOLCHAIN=clang \
-DANDROID_STL=c++_static \
-DCMAKE_C_FLAGS="${CFALGS} -Os -Wall -pipe -fPIC" \
-DCMAKE_CXX_FLAGS="${CFALGS} -Os -Wall -pipe -fPIC" \
-DANDROID_CPP_FEATURES=rtti exceptions \
-DWITH_JPEG8=1 \
${MY_SOURCE_DIR}
make clean
make
make install
cd ../../../
echo "-------------------$1 build end-------------------------"
}
# build armeabi
build_bin armeabi "$ANDROID_ARMV5_CFLAGS"
#build armeabi-v7a
build_bin armeabi-v7a "$ANDROID_ARMV7_CFLAGS"
#build arm64-v8a
build_bin arm64-v8a "$ANDROID_ARMV8_CFLAGS"
#build x86
build_bin x86 "$ANDROID_X86_CFLAGS"
#build x86_64
build_bin x86_64 "$ANDROID_X86_64_CFLAGS"
2.4.3 去掉版本号
动态库的版本号
安装规则一般so都会带版本号,但是如果我们在Android
上使用不带版本号。可以通过修改CMake
脚本,不带版本号。
动态库实现版本号一般使用SET_TARGET_PROPERTIES
指令定义一下语句:
SET_TARGET_PROPERTIES(target PROPERTIES VERSION 1.2 SOVERSION 1)
VERSION 指代动态库版本,SOVERSION 指代 API 版本
-
libjpeg-turbo-master/CMakeLists.txt
中注释
-
libjpeg-turbo-master/sharedlib/CMakeLists.txt
中注释
2.4.4 执行编译脚本
sh build.sh
注释版本号编译
2.4.5 编译可能遇到的问题
Syntax error: end of file unexpected (expecting "}")
编译脚本中 cat <<EOF >文件名 内容 EOF,最好顶格写,否则可能出现上述错误未识别的
cflags
参数
一些cflags参数可能导致编译识别,修改编译出错的参数即可
2.4.6 install结果
注释版本号的编译