1. libjpeg-turbo
??前面已經(jīng)編譯過(guò)1.5.9
[1]版本的libjpeg-turbo,現(xiàn)在版本升級(jí)到2.0.1
,基于CMake
編譯枷踏。
??依然還是一樣,按照官網(wǎng)介紹反璃,libjpeg-turbo
比libjpeg
快2-6
倍,得益于它高度優(yōu)化的哈夫曼算法矫户。在許多情況下者娱,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. 編譯過(guò)程
2.1 編譯環(huán)境
在Windows 10的虛擬機(jī)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
的庫(kù)肮帐,需要安裝NASM 2.10及以上或者YASM 2.10及以上版本爬早。
2.3 配置環(huán)境變量
把Cmake配置進(jìn)系統(tǒng)環(huán)境變量哼丈,當(dāng)然這不是必須的。
2.4 編譯
2.4.1 下載源碼
下載并解壓源碼https://github.com/libjpeg-turbo/libjpeg-turbo
2.4.2 編譯腳本
2.4.2.1 sh編譯腳本(自定義編譯鏈)
注意
??這種方式是完全按照源碼中的BUILDING.md
構(gòu)建筛严,稍顯復(fù)雜醉旦,容易出錯(cuò)。但是不要求CMake的交叉編譯工具鏈桨啃,也就是說(shuō)车胡,我們隨便在CMake官網(wǎng)下載Linux版本就可以用了。
#!/bin/sh
# lib-name
MY_LIBS_NAME=libjpeg-turbo
MY_SOURCE_DIR=$(pwd)/libjpeg-turbo-master
#編譯的過(guò)程中產(chǎn)生的中間件的存放目錄照瘾,為了區(qū)分編譯目錄匈棘,源碼目錄,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} #創(chuàng)建當(dāng)前arch_abi的編譯目錄,比如:binary/armeabi-v7a
cd ${BUILD_DIR} #此處 進(jìn)了當(dāng)前arch_abi的2級(jí)編譯目錄
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 # 此處不頂格寫腳本會(huì)報(bào)一個(gè)錯(cuò)誤
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
#從當(dāng)前arch_abi編譯目錄跳出析命,對(duì)應(yīng)上面的cd ${BUILD_DIR},以便function多次執(zhí)行
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(自帶編譯鏈)
注意
??這個(gè)腳本要求Linux環(huán)境的Android-cmake
主卫,通過(guò)Android sdk-manager
下載更新cmake逃默,android-cmake
已經(jīng)自帶交叉編譯工具鏈,但通常我們不使用cmake帶的队秩,因?yàn)樗_本里寫死了NDK版本12笑旺,而NDK的build/cmake
目錄里也帶了相應(yīng)的編譯鏈,所以馍资,我們一般使用我們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 去掉版本號(hào)
動(dòng)態(tài)庫(kù)的版本號(hào)
??安裝規(guī)則一般so都會(huì)帶版本號(hào),但是如果我們?cè)?code>Android上使用不帶版本號(hào)鸟蟹∥诿睿可以通過(guò)修改CMake
腳本,不帶版本號(hào)建钥。
動(dòng)態(tài)庫(kù)實(shí)現(xiàn)版本號(hào)一般使用SET_TARGET_PROPERTIES
指令定義一下語(yǔ)句:
??SET_TARGET_PROPERTIES(target PROPERTIES VERSION 1.2 SOVERSION 1)
VERSION 指代動(dòng)態(tài)庫(kù)版本藤韵,SOVERSION 指代 API 版本
-
libjpeg-turbo-master/CMakeLists.txt
中注釋
-
libjpeg-turbo-master/sharedlib/CMakeLists.txt
中注釋
2.4.4 執(zhí)行編譯腳本
sh build.sh
注釋版本號(hào)編譯
2.4.5 編譯可能遇到的問(wèn)題
Syntax error: end of file unexpected (expecting "}")
編譯腳本中 cat <<EOF >文件名 內(nèi)容 EOF,最好頂格寫,否則可能出現(xiàn)上述錯(cuò)誤未識(shí)別的
cflags
參數(shù)
一些cflags參數(shù)可能導(dǎo)致編譯識(shí)別,修改編譯出錯(cuò)的參數(shù)即可
2.4.6 install結(jié)果
注釋版本號(hào)的編譯