SDL_image: autotools: set CMake libs in configure.ac

From dd59cc53667563119cea0c582edb1f9131dc77b1 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Sun, 5 Jan 2025 01:52:49 +0100
Subject: [PATCH] autotools: set CMake libs in configure.ac

---
 Makefile.am                |  2 ++
 Makefile.in                |  3 +++
 configure                  | 37 ++++++++++++++++++++++--------------
 configure.ac               |  8 ++++++++
 sdl2_image-config.cmake.in | 39 ++++++++++++++++++++++++++++++++------
 test/Makefile.in           |  1 +
 6 files changed, 70 insertions(+), 20 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index bb8ba737..b13763fe 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -109,6 +109,8 @@ install-data-local:
 	$(MKDIR_P) $(DESTDIR)$(libdir)/cmake/SDL2_image
 	$(INSTALL) -m 644 sdl2_image-config.cmake $(DESTDIR)$(libdir)/cmake/SDL2_image
 	$(INSTALL) -m 644 sdl2_image-config-version.cmake $(DESTDIR)$(libdir)/cmake/SDL2_image
+	$(INSTALL) -m 644 $(srcdir)/cmake/Findlibjxl.cmake $(DESTDIR)$(libdir)/cmake/SDL2_image
+	$(INSTALL) -m 644 $(srcdir)/cmake/Findwebp.cmake $(DESTDIR)$(libdir)/cmake/SDL2_image
 
 uninstall-hook:
 	rm $(DESTDIR)$(libdir)/cmake/SDL2_image/sdl2_image-config.cmake
diff --git a/Makefile.in b/Makefile.in
index 1736fb4e..d6b6ffcb 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -375,6 +375,7 @@ AWK = @AWK@
 CC = @CC@
 CCDEPMODE = @CCDEPMODE@
 CFLAGS = @CFLAGS@
+CMAKE_LIBS = @CMAKE_LIBS@
 CPPFLAGS = @CPPFLAGS@
 CSCOPE = @CSCOPE@
 CTAGS = @CTAGS@
@@ -1603,6 +1604,8 @@ install-data-local:
 	$(MKDIR_P) $(DESTDIR)$(libdir)/cmake/SDL2_image
 	$(INSTALL) -m 644 sdl2_image-config.cmake $(DESTDIR)$(libdir)/cmake/SDL2_image
 	$(INSTALL) -m 644 sdl2_image-config-version.cmake $(DESTDIR)$(libdir)/cmake/SDL2_image
+	$(INSTALL) -m 644 $(srcdir)/cmake/Findlibjxl.cmake $(DESTDIR)$(libdir)/cmake/SDL2_image
+	$(INSTALL) -m 644 $(srcdir)/cmake/Findwebp.cmake $(DESTDIR)$(libdir)/cmake/SDL2_image
 
 uninstall-hook:
 	rm $(DESTDIR)$(libdir)/cmake/SDL2_image/sdl2_image-config.cmake
diff --git a/configure b/configure
index 0df39a8c..b31b1dac 100755
--- a/configure
+++ b/configure
@@ -809,6 +809,7 @@ BUILD_TESTS_FALSE
 BUILD_TESTS_TRUE
 PC_REQUIRES
 PC_LIBS
+CMAKE_LIBS
 IMG_LIBS
 SDL2IMAGE_JPG_SAVE
 SDL2IMAGE_PNG_SAVE
@@ -4718,13 +4719,13 @@ then :
 else $as_nop
   lt_cv_nm_interface="BSD nm"
   echo "int some_variable = 0;" > conftest.$ac_ext
-  (eval echo "\"\$as_me:4721: $ac_compile\"" >&5)
+  (eval echo "\"\$as_me:4722: $ac_compile\"" >&5)
   (eval "$ac_compile" 2>conftest.err)
   cat conftest.err >&5
-  (eval echo "\"\$as_me:4724: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
+  (eval echo "\"\$as_me:4725: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
   (eval "$NM \"conftest.$ac_objext\"" 2>conftest.err > conftest.out)
   cat conftest.err >&5
-  (eval echo "\"\$as_me:4727: output\"" >&5)
+  (eval echo "\"\$as_me:4728: output\"" >&5)
   cat conftest.out >&5
   if $GREP 'External.*some_variable' conftest.out > /dev/null; then
     lt_cv_nm_interface="MS dumpbin"
@@ -5983,7 +5984,7 @@ ia64-*-hpux*)
   ;;
 *-*-irix6*)
   # Find out which ABI we are using.
-  echo '#line 5986 "configure"' > conftest.$ac_ext
+  echo '#line 5987 "configure"' > conftest.$ac_ext
   if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_compile\""; } >&5
   (eval $ac_compile) 2>&5
   ac_status=$?
@@ -7656,11 +7657,11 @@ else $as_nop
    -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
    -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
    -e 's:$: $lt_compiler_flag:'`
-   (eval echo "\"\$as_me:7659: $lt_compile\"" >&5)
+   (eval echo "\"\$as_me:7660: $lt_compile\"" >&5)
    (eval "$lt_compile" 2>conftest.err)
    ac_status=$?
    cat conftest.err >&5
-   echo "$as_me:7663: \$? = $ac_status" >&5
+   echo "$as_me:7664: \$? = $ac_status" >&5
    if (exit $ac_status) && test -s "$ac_outfile"; then
      # The compiler can only warn and ignore the option if not recognized
      # So say no if there are warnings other than the usual output.
@@ -8006,11 +8007,11 @@ else $as_nop
    -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
    -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
    -e 's:$: $lt_compiler_flag:'`
-   (eval echo "\"\$as_me:8009: $lt_compile\"" >&5)
+   (eval echo "\"\$as_me:8010: $lt_compile\"" >&5)
    (eval "$lt_compile" 2>conftest.err)
    ac_status=$?
    cat conftest.err >&5
-   echo "$as_me:8013: \$? = $ac_status" >&5
+   echo "$as_me:8014: \$? = $ac_status" >&5
    if (exit $ac_status) && test -s "$ac_outfile"; then
      # The compiler can only warn and ignore the option if not recognized
      # So say no if there are warnings other than the usual output.
@@ -8113,11 +8114,11 @@ else $as_nop
    -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
    -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
    -e 's:$: $lt_compiler_flag:'`
-   (eval echo "\"\$as_me:8116: $lt_compile\"" >&5)
+   (eval echo "\"\$as_me:8117: $lt_compile\"" >&5)
    (eval "$lt_compile" 2>out/conftest.err)
    ac_status=$?
    cat out/conftest.err >&5
-   echo "$as_me:8120: \$? = $ac_status" >&5
+   echo "$as_me:8121: \$? = $ac_status" >&5
    if (exit $ac_status) && test -s out/conftest2.$ac_objext
    then
      # The compiler can only warn and ignore the option if not recognized
@@ -8169,11 +8170,11 @@ else $as_nop
    -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
    -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
    -e 's:$: $lt_compiler_flag:'`
-   (eval echo "\"\$as_me:8172: $lt_compile\"" >&5)
+   (eval echo "\"\$as_me:8173: $lt_compile\"" >&5)
    (eval "$lt_compile" 2>out/conftest.err)
    ac_status=$?
    cat out/conftest.err >&5
-   echo "$as_me:8176: \$? = $ac_status" >&5
+   echo "$as_me:8177: \$? = $ac_status" >&5
    if (exit $ac_status) && test -s out/conftest2.$ac_objext
    then
      # The compiler can only warn and ignore the option if not recognized
@@ -10612,7 +10613,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 10615 "configure"
+#line 10616 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -10709,7 +10710,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 10712 "configure"
+#line 10713 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -13566,6 +13567,7 @@ printf %s "checking for ImageIO support... " >&6; }
 printf "%s\n" "$enable_imageio" >&6; }
         if test x$enable_imageio = xyes; then
             IMG_LIBS="-Wl,-framework,ApplicationServices -lobjc $IMG_LIBS"
+            CMAKE_LIBS="-Wl,-framework,ApplicationServices;objc;$CMAKE_LIBS"
         else
             CFLAGS="$CFLAGS -DSDL_IMAGE_USE_COMMON_BACKEND"
         fi
@@ -15968,6 +15970,7 @@ if test x$enable_webp = xyes -a x$have_webp_hdr = xyes -a x$have_webp_lib -a x$h
 
     else
         IMG_LIBS="$LIBWEBPDEMUX_LIBS $LIBWEBP_LIBS $IMG_LIBS"
+        CMAKE_LIBS="WebP::webp;WebP::webpdemux;$CMAKE_LIBS"
         if test x$have_webp_pc = xyes; then
             PC_REQUIRES="libwebpdemux libwebp $PC_REQUIRES"
         else
@@ -15984,6 +15987,7 @@ if test x$enable_avif = xyes -a x$have_avif_hdr = xyes -a x$have_avif_lib = xyes
 
     else
         IMG_LIBS="$LIBAVIF_LIBS $IMG_LIBS"
+        CMAKE_LIBS="avif;$CMAKE_LIBS"
         if test x$have_avif_pc = xyes; then
             PC_REQUIRES="libavif $PC_REQUIRES"
         else
@@ -16003,6 +16007,7 @@ if test x$enable_tif = xyes -a x$have_tif_hdr = xyes -a x$have_tif_lib = xyes; t
             jpg_lib=''
         fi
         IMG_LIBS="$LIBTIFF_LIBS $IMG_LIBS"
+        CMAKE_LIBS="TIFF::TIFF;$CMAKE_LIBS"
         if test x$have_tif_pc = xyes; then
             PC_REQUIRES="libtiff-4 $PC_REQUIRES"
         else
@@ -16018,6 +16023,7 @@ if test x$enable_jpg = xyes -a x$have_jpg_hdr = xyes -a x$have_jpg_lib = xyes; t
 
     else
         IMG_LIBS="$LIBJPEG_LIBS $IMG_LIBS"
+        CMAKE_LIBS="JPEG::JPEG;$CMAKE_LIBS"
         if test x$have_jpg_pc = xyes; then
             PC_REQUIRES="libjpeg $PC_REQUIRES"
         else
@@ -16033,6 +16039,7 @@ if test x$enable_jxl = xyes -a x$have_jxl_hdr = xyes -a x$have_jxl_lib = xyes; t
 
     else
         IMG_LIBS="$LIBJXL_LIBS $IMG_LIBS"
+        CMAKE_LIBS="libjxl::libjxl;$CMAKE_LIBS"
         if test x$have_jxl_pc = xyes; then
             PC_REQUIRES="libjxl $PC_REQUIRES"
         else
@@ -16048,6 +16055,7 @@ if test x$enable_png = xyes -a x$have_png_hdr = xyes -a x$have_png_lib = xyes; t
 
     else
         IMG_LIBS="$LIBPNG_LIBS $IMG_LIBS"
+        CMAKE_LIBS="PNG::PNG;$CMAKE_LIBS"
         if test x$have_png_pc = xyes; then
             PC_REQUIRES="libpng $PC_REQUIRES"
         else
@@ -16082,6 +16090,7 @@ fi
 
 
 
+
  if test "x$enable_tests" = xyes; then
   BUILD_TESTS_TRUE=
   BUILD_TESTS_FALSE='#'
diff --git a/configure.ac b/configure.ac
index 8523cdbe..c0da8312 100644
--- a/configure.ac
+++ b/configure.ac
@@ -106,6 +106,7 @@ case "$host" in
         AC_MSG_RESULT($enable_imageio)
         if test x$enable_imageio = xyes; then
             IMG_LIBS="-Wl,-framework,ApplicationServices -lobjc $IMG_LIBS"
+            CMAKE_LIBS="-Wl,-framework,ApplicationServices;objc;$CMAKE_LIBS"
         else
             CFLAGS="$CFLAGS -DSDL_IMAGE_USE_COMMON_BACKEND"
         fi
@@ -737,6 +738,7 @@ if test x$enable_webp = xyes -a x$have_webp_hdr = xyes -a x$have_webp_lib -a x$h
         AC_DEFINE_UNQUOTED(LOAD_WEBPDEMUX_DYNAMIC, "$webpdemux_lib")
     else
         IMG_LIBS="$LIBWEBPDEMUX_LIBS $LIBWEBP_LIBS $IMG_LIBS"
+        CMAKE_LIBS="WebP::webp;WebP::webpdemux;$CMAKE_LIBS"
         if test x$have_webp_pc = xyes; then
             PC_REQUIRES="libwebpdemux libwebp $PC_REQUIRES"
         else
@@ -752,6 +754,7 @@ if test x$enable_avif = xyes -a x$have_avif_hdr = xyes -a x$have_avif_lib = xyes
         AC_DEFINE_UNQUOTED(LOAD_AVIF_DYNAMIC, "$avif_lib")
     else
         IMG_LIBS="$LIBAVIF_LIBS $IMG_LIBS"
+        CMAKE_LIBS="avif;$CMAKE_LIBS"
         if test x$have_avif_pc = xyes; then
             PC_REQUIRES="libavif $PC_REQUIRES"
         else
@@ -770,6 +773,7 @@ if test x$enable_tif = xyes -a x$have_tif_hdr = xyes -a x$have_tif_lib = xyes; t
             jpg_lib=''
         fi
         IMG_LIBS="$LIBTIFF_LIBS $IMG_LIBS"
+        CMAKE_LIBS="TIFF::TIFF;$CMAKE_LIBS"
         if test x$have_tif_pc = xyes; then
             PC_REQUIRES="libtiff-4 $PC_REQUIRES"
         else
@@ -784,6 +788,7 @@ if test x$enable_jpg = xyes -a x$have_jpg_hdr = xyes -a x$have_jpg_lib = xyes; t
         AC_DEFINE_UNQUOTED(LOAD_JPG_DYNAMIC, "$jpg_lib")
     else
         IMG_LIBS="$LIBJPEG_LIBS $IMG_LIBS"
+        CMAKE_LIBS="JPEG::JPEG;$CMAKE_LIBS"
         if test x$have_jpg_pc = xyes; then
             PC_REQUIRES="libjpeg $PC_REQUIRES"
         else
@@ -798,6 +803,7 @@ if test x$enable_jxl = xyes -a x$have_jxl_hdr = xyes -a x$have_jxl_lib = xyes; t
         AC_DEFINE_UNQUOTED(LOAD_JXL_DYNAMIC, "$jxl_lib")
     else
         IMG_LIBS="$LIBJXL_LIBS $IMG_LIBS"
+        CMAKE_LIBS="libjxl::libjxl;$CMAKE_LIBS"
         if test x$have_jxl_pc = xyes; then
             PC_REQUIRES="libjxl $PC_REQUIRES"
         else
@@ -812,6 +818,7 @@ if test x$enable_png = xyes -a x$have_png_hdr = xyes -a x$have_png_lib = xyes; t
         AC_DEFINE_UNQUOTED(LOAD_PNG_DYNAMIC, "$png_lib")
     else
         IMG_LIBS="$LIBPNG_LIBS $IMG_LIBS"
+        CMAKE_LIBS="PNG::PNG;$CMAKE_LIBS"
         if test x$have_png_pc = xyes; then
             PC_REQUIRES="libpng $PC_REQUIRES"
         else
@@ -835,6 +842,7 @@ else
 fi
 
 AC_SUBST([IMG_LIBS])
+AC_SUBST([CMAKE_LIBS])
 AC_SUBST([PC_LIBS])
 AC_SUBST([PC_REQUIRES])
 
diff --git a/sdl2_image-config.cmake.in b/sdl2_image-config.cmake.in
index 3cb43ae1..eb0f9d60 100644
--- a/sdl2_image-config.cmake.in
+++ b/sdl2_image-config.cmake.in
@@ -41,17 +41,44 @@ set(exec_prefix "@exec_prefix@")
 set(bindir "@bindir@")
 set(includedir "@includedir@")
 set(libdir "@libdir@")
-set(_sdl2image_extra_static_libraries "@IMG_LIBS@ @PC_LIBS@")
-string(STRIP "${_sdl2image_extra_static_libraries}" _sdl2image_extra_static_libraries)
+set(_sdl2image_extra_static_libraries "@CMAKE_LIBS@")
+string(REGEX REPLACE "(^[;]+|[;]+$)" "" _sdl2image_extra_static_libraries "${_sdl2image_extra_static_libraries}")
 
 set(_sdl2image_bindir   "${bindir}")
 set(_sdl2image_libdir   "${libdir}")
 set(_sdl2image_incdir   "${includedir}/SDL2")
 
-# Convert _sdl2image_extra_static_libraries to list and keep only libraries
-string(REGEX MATCHALL "(-[lm]([-a-zA-Z0-9._]+))|(-Wl,[^ ]*framework[^ ]*)" _sdl2image_extra_static_libraries "${_sdl2image_extra_static_libraries}")
-string(REGEX REPLACE "^-l" "" _sdl2image_extra_static_libraries "${_sdl2image_extra_static_libraries}")
-string(REGEX REPLACE ";-l" ";" _sdl2image_extra_static_libraries "${_sdl2image_extra_static_libraries}")
+include(CMakeFindDependencyMacro)
+
+set(_original_cmake_module_path "${CMAKE_MODULE_PATH}")
+list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
+
+if("WebP::webp" IN_LIST _sdl2image_extra_static_libraries)
+    find_dependency(webp)
+endif()
+
+if("avif" IN_LIST _sdl2image_extra_static_libraries)
+    find_dependency(libavif)
+endif()
+
+if("TIFF::TIFF" IN_LIST _sdl2image_extra_static_libraries)
+    find_dependency(TIFF)
+endif()
+
+if("JPEG::JPEG" IN_LIST _sdl2image_extra_static_libraries)
+    find_dependency(JPEG)
+endif()
+
+if("libjxl::libjxl" IN_LIST _sdl2image_extra_static_libraries)
+    find_dependency(libjxl)
+endif()
+
+if("PNG::PNG" IN_LIST _sdl2image_extra_static_libraries)
+    find_dependency(PNG)
+endif()
+
+set(CMAKE_MODULE_PATH "${_original_cmake_module_path}")
+unset(_original_cmake_module_path)
 
 unset(prefix)
 unset(exec_prefix)
diff --git a/test/Makefile.in b/test/Makefile.in
index 0432b34a..56e98c2e 100644
--- a/test/Makefile.in
+++ b/test/Makefile.in
@@ -409,6 +409,7 @@ AWK = @AWK@
 CC = @CC@
 CCDEPMODE = @CCDEPMODE@
 CFLAGS = @CFLAGS@
+CMAKE_LIBS = @CMAKE_LIBS@
 CPPFLAGS = @CPPFLAGS@
 CSCOPE = @CSCOPE@
 CTAGS = @CTAGS@