Skip to content

Commit

Permalink
libjpeg-turbo: Change library name on Android
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dg0yt committed Oct 3, 2020
1 parent 0b56d18 commit 20d15fa
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 3 deletions.
82 changes: 82 additions & 0 deletions gdal-3.1.3+dfsg.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -103,13 +178,17 @@ superbuild_package(
tiff
zlib

SOURCE_WRITE
libjpeg.patch libjpeg_patch
SOURCE
URL ${base_url}gdal_${version}.orig.tar.xz
URL_HASH ${download_hash}
PATCH_COMMAND
"${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}
Expand All @@ -120,6 +199,9 @@ superbuild_package(
COMMAND
"${CMAKE_COMMAND}" -E copy_directory "${SOURCE_DIR}" "${BINARY_DIR}"
COMMAND
$<$<BOOL:@ANDROID@>:
"${CMAKE_COMMAND}" -E env LIBJPEG_SUFFIX=-turbo # needs libjpeg patch
>
"${BINARY_DIR}/configure"
"--prefix=${CMAKE_INSTALL_PREFIX}"
$<$<BOOL:@CMAKE_CROSSCOMPILING@>:
Expand Down
56 changes: 54 additions & 2 deletions libjpeg-turbo-2.0.5.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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} $<TARGET_OBJECTS:simd>
${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} $<TARGET_OBJECTS:simd>
${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}
Expand All @@ -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
Expand All @@ -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}
Expand All @@ -82,8 +135,7 @@ superbuild_package(
-DENABLE_SHARED=ON
-DENABLE_STATIC=OFF
$<$<BOOL:@ANDROID@>:
-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
Expand Down
2 changes: 1 addition & 1 deletion libwebp-1.1.0.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ superbuild_package(
VERSION ${patch_version}
DEPENDS
giflib
libjpeg
libpng
zlib

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions poppler-20.09.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ superbuild_package(
-DCMAKE_DISABLE_FIND_PACKAGE_NSS3=ON
${poppler_font_configuration}
$<$<BOOL:@ANDROID@>:
-DJPEG_NAMES=jpeg-turbo
-D_FILE_OFFSET_BITS=
>
INSTALL_COMMAND
Expand Down
3 changes: 3 additions & 0 deletions tiff-4.1.0.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ superbuild_package(
# We don't provide all possible sources (yet)
"-Djbig:BOOL=OFF"
"-Dzstd:BOOL=OFF"
$<$<BOOL:@ANDROID@>:
-DJPEG_NAMES=jpeg-turbo
>
# GNUInstallDirs doesn't work with CMAKE_STAGING_PREFIX
-UCMAKE_STAGING_PREFIX
INSTALL_COMMAND
Expand Down

0 comments on commit 20d15fa

Please sign in to comment.