From 20d15faee7abda202f52c61b5db198cb70bcda01 Mon Sep 17 00:00:00 2001 From: Kai Pastor Date: Sat, 3 Oct 2020 14:50:42 +0200 Subject: [PATCH] libjpeg-turbo: Change library name on Android When using libjpeg-turbo as a shared object (libjpeg.so) on Android, there are crashes when accessing JPEG functions on some arm devices. Static linking increases binary size when the library is used by multiple other shared objects (Qt plugin, GDAL, Poppler, TIFF). Assuming that the issue is related to some other "libjpeg.so", try to resolve the dynamic linking issue by changing the filename to "libjpeg-turbo.so" in Android builds. Some depending libraries can find the alternative name via pkg-config. Depending CMake projects must be configured with -DJPEG_NAMES=jpeg-turbo. GDAL needs an individual patch. --- gdal-3.1.3+dfsg.cmake | 82 +++++++++++++++++++++++++++++++++++++++ libjpeg-turbo-2.0.5.cmake | 56 +++++++++++++++++++++++++- libwebp-1.1.0.cmake | 2 +- poppler-20.09.cmake | 1 + tiff-4.1.0.cmake | 3 ++ 5 files changed, 141 insertions(+), 3 deletions(-) diff --git a/gdal-3.1.3+dfsg.cmake b/gdal-3.1.3+dfsg.cmake index 6d75a09a..89470968 100644 --- a/gdal-3.1.3+dfsg.cmake +++ b/gdal-3.1.3+dfsg.cmake @@ -71,6 +71,81 @@ set(test_system_gdal [[ set(extra_cxxflags "-Wno-strict-overflow -Wno-null-dereference -Wno-old-style-cast" PARENT_SCOPE) ]]) +set(libjpeg_patch [[ +--- a/configure ++++ b/configure +@@ -1247,6 +1247,7 @@ + CXXCPP + PKG_CONFIG + bashcompdir ++LIBJPEG_SUFFIX + PQ_CFLAGS + PQ_LIBS + OGDI_CFLAGS +@@ -30093,13 +30093,13 @@ + + elif test "$with_jpeg" = "yes" -o "$with_jpeg" = "" ; then + +- { $as_echo "$as_me:${as_lineno-$LINENO}: checking for jpeg_read_scanlines in -ljpeg" >&5 +-$as_echo_n "checking for jpeg_read_scanlines in -ljpeg... " >&6; } ++ { $as_echo "$as_me:${as_lineno-$LINENO}: checking for jpeg_read_scanlines in -ljpeg${LIBJPEG_SUFFIX}" >&5 ++$as_echo_n "checking for jpeg_read_scanlines in -ljpeg${LIBJPEG_SUFFIX}... " >&6; } + if ${ac_cv_lib_jpeg_jpeg_read_scanlines+:} false; then : + $as_echo_n "(cached) " >&6 + else + ac_check_lib_save_LIBS=$LIBS +-LIBS="-ljpeg $LIBS" ++LIBS="-ljpeg${LIBJPEG_SUFFIX} $LIBS" + cat confdefs.h - <<_ACEOF >conftest.$ac_ext + /* end confdefs.h. */ + +@@ -30172,7 +30172,7 @@ + fi + + if test "$JPEG_SETTING" = "external" ; then +- LIBS="-ljpeg $LIBS" ++ LIBS="-ljpeg${LIBJPEG_SUFFIX} $LIBS" + echo "using pre-installed libjpeg." + else + echo "using internal jpeg code." +@@ -30187,7 +30187,7 @@ + else + + JPEG_SETTING=external +- LIBS="-L$with_jpeg -L$with_jpeg/lib -ljpeg $LIBS" ++ LIBS="-L$with_jpeg -L$with_jpeg/lib -ljpeg${LIBJPEG_SUFFIX} $LIBS" + EXTRA_INCLUDES="-I$with_jpeg -I$with_jpeg/include $EXTRA_INCLUDES" + + echo "using libjpeg from $with_jpeg." +@@ -31550,7 +31550,7 @@ + $as_echo_n "(cached) " >&6 + else + ac_check_lib_save_LIBS=$LIBS +-LIBS="-lmfhdfalt -ldfalt -ljpeg -lz $LIBS" ++LIBS="-lmfhdfalt -ldfalt -ljpeg${LIBJPEG_SUFFIX} -lz $LIBS" + cat confdefs.h - <<_ACEOF >conftest.$ac_ext + /* end confdefs.h. */ + +@@ -31596,7 +31596,7 @@ + $as_echo_n "(cached) " >&6 + else + ac_check_lib_save_LIBS=$LIBS +-LIBS="-lmfhdfalt -ldfalt -lsz -ljpeg -lz $LIBS" ++LIBS="-lmfhdfalt -ldfalt -lsz -ljpeg${LIBJPEG_SUFFIX} -lz $LIBS" + cat confdefs.h - <<_ACEOF >conftest.$ac_ext + /* end confdefs.h. */ + +@@ -40801,7 +40801,7 @@ + PDFIUM_LIB="-L$with_pdfium/lib -lpdfium" + fi + +- PDFIUM_LIB="$PDFIUM_LIB -ljpeg -lpng -lz -llcms2 -lpthread -lm -lstdc++" ++ PDFIUM_LIB="$PDFIUM_LIB -ljpeg${LIBJPEG_SUFFIX} -lpng -lz -llcms2 -lpthread -lm -lstdc++" + + if test ! -z "`uname | grep Darwin`" ; then + PDFIUM_LIB="-stdlib=libstdc++ $PDFIUM_LIB" +]]) + superbuild_package( NAME gdal-patches VERSION ${patch_version} @@ -103,6 +178,8 @@ superbuild_package( tiff zlib + SOURCE_WRITE + libjpeg.patch libjpeg_patch SOURCE URL ${base_url}gdal_${version}.orig.tar.xz URL_HASH ${download_hash} @@ -110,6 +187,8 @@ superbuild_package( "${CMAKE_COMMAND}" -Dpackage=gdal-patches-${patch_version} -P "${APPLY_PATCHES_SERIES}" + COMMAND + patch -p1 < libjpeg.patch USING USE_SYSTEM_GDAL patch_version extra_cflags extra_cxxflags BUILD_CONDITION ${test_system_gdal} @@ -120,6 +199,9 @@ superbuild_package( COMMAND "${CMAKE_COMMAND}" -E copy_directory "${SOURCE_DIR}" "${BINARY_DIR}" COMMAND + $<$: + "${CMAKE_COMMAND}" -E env LIBJPEG_SUFFIX=-turbo # needs libjpeg patch + > "${BINARY_DIR}/configure" "--prefix=${CMAKE_INSTALL_PREFIX}" $<$: diff --git a/libjpeg-turbo-2.0.5.cmake b/libjpeg-turbo-2.0.5.cmake index 5d573f93..fbeb6947 100644 --- a/libjpeg-turbo-2.0.5.cmake +++ b/libjpeg-turbo-2.0.5.cmake @@ -48,6 +48,55 @@ set(test_system_jpeg [[ endif() ]]) +set(suffix_patch [[ +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -189,6 +189,13 @@ + report_option(ENABLE_SHARED "Shared libraries") + report_option(ENABLE_STATIC "Static libraries") + ++if(NOT MSVC) ++ set(LIBJPEG_SUFFIX "" CACHE STRING "Suffix to be added the library name") ++ report_option(LIBJPEG_SUFFIX "Library name suffix") ++else() ++ set(LIBJPEG_SUFFIX "") ++endif() ++ + if(WITH_12BIT) + set(WITH_ARITH_DEC 0) + set(WITH_ARITH_ENC 0) +@@ -566,7 +573,7 @@ + add_library(jpeg-static STATIC ${JPEG_SOURCES} $ + ${SIMD_OBJS}) + if(NOT MSVC) +- set_target_properties(jpeg-static PROPERTIES OUTPUT_NAME jpeg) ++ set_target_properties(jpeg-static PROPERTIES OUTPUT_NAME jpeg${LIBJPEG_SUFFIX}) + endif() + endif() + +--- a/release/libjpeg.pc.in ++++ b/release/libjpeg.pc.in +@@ -6,5 +6,5 @@ + Name: libjpeg + Description: A SIMD-accelerated JPEG codec that provides the libjpeg API + Version: @VERSION@ +-Libs: -L${libdir} -ljpeg ++Libs: -L${libdir} -ljpeg@LIBJPEG_SUFFIX@ + Cflags: -I${includedir} +--- a/sharedlib/CMakeLists.txt ++++ b/sharedlib/CMakeLists.txt +@@ -38,6 +38,9 @@ + add_library(jpeg SHARED ${JPEG_SRCS} ${DEFFILE} $ + ${SIMD_OBJS}) + ++if(NOT MSVC) ++ set_target_properties(jpeg PROPERTIES OUTPUT_NAME jpeg${LIBJPEG_SUFFIX}) ++endif() + set_target_properties(jpeg PROPERTIES SOVERSION ${SO_MAJOR_VERSION} + VERSION ${SO_MAJOR_VERSION}.${SO_AGE}.${SO_MINOR_VERSION}) + if(APPLE AND (NOT CMAKE_OSX_DEPLOYMENT_TARGET OR +]]) + superbuild_package( NAME libjpeg-turbo-patches VERSION ${patch_version} @@ -64,6 +113,8 @@ superbuild_package( source:libjpeg-turbo-patches-${patch_version} host:nasm + SOURCE_WRITE + suffix.patch suffix_patch SOURCE DOWNLOAD_NAME libjpeg-turbo_${version}.orig.tar.gz URL ${base_url}libjpeg-turbo_${version}.orig.tar.gz @@ -72,6 +123,8 @@ superbuild_package( "${CMAKE_COMMAND}" -Dpackage=libjpeg-turbo-patches-${patch_version} -P "${APPLY_PATCHES_SERIES}" + COMMAND + patch -p1 < suffix.patch USING USE_SYSTEM_LIBJPEG patch_version BUILD_CONDITION ${test_system_jpeg} @@ -82,8 +135,7 @@ superbuild_package( -DENABLE_SHARED=ON -DENABLE_STATIC=OFF $<$: - -DENABLE_SHARED=OFF - -DENABLE_STATIC=ON + -DLIBJPEG_SUFFIX=-turbo # needs suffix patch -DCMAKE_ASM_FLAGS="--target=${SUPERBUILD_TOOLCHAIN_TRIPLET}${ANDROID_NATIVE_API_LEVEL}" > INSTALL_COMMAND diff --git a/libwebp-1.1.0.cmake b/libwebp-1.1.0.cmake index bfbf8c2e..6a3530b8 100644 --- a/libwebp-1.1.0.cmake +++ b/libwebp-1.1.0.cmake @@ -57,7 +57,6 @@ superbuild_package( VERSION ${patch_version} DEPENDS giflib - libjpeg libpng zlib @@ -84,6 +83,7 @@ superbuild_package( -DWEBP_BUILD_WEBPMUX=OFF -DWEBP_BUILD_EXTRAS=OFF -DWEBP_BUILD_WEBP_JS=OFF + -DCMAKE_DISABLE_FIND_PACKAGE_JPEG=ON -DCMAKE_DISABLE_FIND_PACKAGE_TIFF=ON # Our libtiff depends on libwebp. INSTALL_COMMAND "${CMAKE_COMMAND}" --build . --target install/strip/fast diff --git a/poppler-20.09.cmake b/poppler-20.09.cmake index 3028336f..8b54b2ea 100644 --- a/poppler-20.09.cmake +++ b/poppler-20.09.cmake @@ -118,6 +118,7 @@ superbuild_package( -DCMAKE_DISABLE_FIND_PACKAGE_NSS3=ON ${poppler_font_configuration} $<$: + -DJPEG_NAMES=jpeg-turbo -D_FILE_OFFSET_BITS= > INSTALL_COMMAND diff --git a/tiff-4.1.0.cmake b/tiff-4.1.0.cmake index dd63604c..9b516772 100644 --- a/tiff-4.1.0.cmake +++ b/tiff-4.1.0.cmake @@ -107,6 +107,9 @@ superbuild_package( # We don't provide all possible sources (yet) "-Djbig:BOOL=OFF" "-Dzstd:BOOL=OFF" + $<$: + -DJPEG_NAMES=jpeg-turbo + > # GNUInstallDirs doesn't work with CMAKE_STAGING_PREFIX -UCMAKE_STAGING_PREFIX INSTALL_COMMAND