Commit 72c71e0d3 for imagemagick.org

commit 72c71e0d38f7af991dc9a5a33d62fb31055e6577
Author: Greg B <64932474+gregbenz@users.noreply.github.com>
Date:   Sat Sep 5 14:40:24 2026 -0500

    Fix static libuhdr and module linking (#8940)

    * build: fix static libuhdr and module linking

    * build: preserve user libraries in UHDR probe

    ---------

    Co-authored-by: Cristy <12486802+urban-warrior@users.noreply.github.com>

diff --git a/Makefile.in b/Makefile.in
index d7de93569..bf451aea2 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -1864,10 +1864,6 @@ coders_uhdr_la_DEPENDENCIES = $(MAGICKCORE_LIBS) $(am__DEPENDENCIES_1) \
 	$(am__DEPENDENCIES_1)
 am_coders_uhdr_la_OBJECTS = coders/uhdr_la-uhdr.lo
 coders_uhdr_la_OBJECTS = $(am_coders_uhdr_la_OBJECTS)
-coders_uhdr_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC \
-	$(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link $(CCLD) \
-	$(AM_CFLAGS) $(CFLAGS) $(coders_uhdr_la_LDFLAGS) $(LDFLAGS) -o \
-	$@
 @UHDR_DELEGATE_TRUE@@WITH_MODULES_TRUE@am_coders_uhdr_la_rpath =  \
 @UHDR_DELEGATE_TRUE@@WITH_MODULES_TRUE@	-rpath $(codersdir)
 coders_uil_la_DEPENDENCIES = $(MAGICKCORE_LIBS)
@@ -3529,6 +3525,10 @@ TextEncodeDelegate = @TextEncodeDelegate@
 TraceEncodeDelegate = @TraceEncodeDelegate@
 UHDR_CFLAGS = @UHDR_CFLAGS@
 UHDR_LIBS = @UHDR_LIBS@
+UHDR_SHARED_CFLAGS = @UHDR_SHARED_CFLAGS@
+UHDR_SHARED_LIBS = @UHDR_SHARED_LIBS@
+UHDR_STATIC_CFLAGS = @UHDR_STATIC_CFLAGS@
+UHDR_STATIC_LIBS = @UHDR_STATIC_LIBS@
 UMEM_LIBS = @UMEM_LIBS@
 VERSION = @VERSION@
 VIDEODecodeDelegate = @VIDEODecodeDelegate@
@@ -5182,6 +5182,11 @@ coders_uhdr_la_SOURCES = coders/uhdr.c
 coders_uhdr_la_CPPFLAGS = $(MAGICK_CODER_CPPFLAGS)
 coders_uhdr_la_LDFLAGS = $(MODULECOMMONFLAGS)
 coders_uhdr_la_LIBADD = $(MAGICKCORE_LIBS) $(UHDR_LIBS) $(MATH_LIBS)
+coders_uhdr_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CXX \
+  $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link $(CXXLD) \
+  $(AM_CFLAGS) $(CFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) \
+  $(coders_uhdr_la_LDFLAGS) $(LDFLAGS) -o $@
+

 # UIL coder module
 coders_uil_la_SOURCES = coders/uil.c
@@ -8033,7 +8038,7 @@ coders/uhdr_la-uhdr.lo: coders/$(am__dirstamp) \
 	coders/$(DEPDIR)/$(am__dirstamp)

 coders/uhdr.la: $(coders_uhdr_la_OBJECTS) $(coders_uhdr_la_DEPENDENCIES) $(EXTRA_coders_uhdr_la_DEPENDENCIES) coders/$(am__dirstamp)
-	$(AM_V_CCLD)$(coders_uhdr_la_LINK) $(am_coders_uhdr_la_rpath) $(coders_uhdr_la_OBJECTS) $(coders_uhdr_la_LIBADD) $(LIBS)
+	$(AM_V_GEN)$(coders_uhdr_la_LINK) $(am_coders_uhdr_la_rpath) $(coders_uhdr_la_OBJECTS) $(coders_uhdr_la_LIBADD) $(LIBS)
 coders/uil_la-uil.lo: coders/$(am__dirstamp) \
 	coders/$(DEPDIR)/$(am__dirstamp)

diff --git a/coders/Makefile.am b/coders/Makefile.am
index 6da57f495..5f60b07c7 100644
--- a/coders/Makefile.am
+++ b/coders/Makefile.am
@@ -1440,6 +1440,10 @@ coders_uhdr_la_SOURCES     = coders/uhdr.c
 coders_uhdr_la_CPPFLAGS    = $(MAGICK_CODER_CPPFLAGS)
 coders_uhdr_la_LDFLAGS     = $(MODULECOMMONFLAGS)
 coders_uhdr_la_LIBADD      = $(MAGICKCORE_LIBS) $(UHDR_LIBS) $(MATH_LIBS)
+coders_uhdr_la_LINK        = $(LIBTOOL) $(AM_V_lt) --tag=CXX \
+  $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link $(CXXLD) \
+  $(AM_CFLAGS) $(CFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) \
+  $(coders_uhdr_la_LDFLAGS) $(LDFLAGS) -o $@

 # UIL coder module
 coders_uil_la_SOURCES      = coders/uil.c
diff --git a/configure b/configure
index c9f24ba6a..be26bad65 100755
--- a/configure
+++ b/configure
@@ -797,10 +797,14 @@ WEBPMUX_LIBS
 WEBPMUX_CFLAGS
 WEBP_LIBS
 WEBP_CFLAGS
-UHDR_DELEGATE_FALSE
-UHDR_DELEGATE_TRUE
 UHDR_LIBS
 UHDR_CFLAGS
+UHDR_DELEGATE_FALSE
+UHDR_DELEGATE_TRUE
+UHDR_STATIC_LIBS
+UHDR_STATIC_CFLAGS
+UHDR_SHARED_LIBS
+UHDR_SHARED_CFLAGS
 TIFF_DELEGATE_FALSE
 TIFF_DELEGATE_TRUE
 TIFF_LIBS
@@ -1373,8 +1377,10 @@ CAIRO_SVG_CFLAGS
 CAIRO_SVG_LIBS
 TIFF_CFLAGS
 TIFF_LIBS
-UHDR_CFLAGS
-UHDR_LIBS
+UHDR_SHARED_CFLAGS
+UHDR_SHARED_LIBS
+UHDR_STATIC_CFLAGS
+UHDR_STATIC_LIBS
 WEBP_CFLAGS
 WEBP_LIBS
 WEBPMUX_CFLAGS
@@ -2253,8 +2259,14 @@ Some influential environment variables:
               linker flags for CAIRO_SVG, overriding pkg-config
   TIFF_CFLAGS C compiler flags for TIFF, overriding pkg-config
   TIFF_LIBS   linker flags for TIFF, overriding pkg-config
-  UHDR_CFLAGS C compiler flags for UHDR, overriding pkg-config
-  UHDR_LIBS   linker flags for UHDR, overriding pkg-config
+  UHDR_SHARED_CFLAGS
+              C compiler flags for UHDR_SHARED, overriding pkg-config
+  UHDR_SHARED_LIBS
+              linker flags for UHDR_SHARED, overriding pkg-config
+  UHDR_STATIC_CFLAGS
+              C compiler flags for UHDR_STATIC, overriding pkg-config
+  UHDR_STATIC_LIBS
+              linker flags for UHDR_STATIC, overriding pkg-config
   WEBP_CFLAGS C compiler flags for WEBP, overriding pkg-config
   WEBP_LIBS   linker flags for WEBP, overriding pkg-config
   WEBPMUX_CFLAGS
@@ -35797,6 +35809,8 @@ if test "$with_uhdr" != 'yes'; then
 fi

 have_uhdr='no'
+have_uhdr_shared='no'
+have_uhdr_static='no'
 UHDR_CFLAGS=""
 UHDR_LIBS=""
 UHDR_PKG=""
@@ -35808,8 +35822,81 @@ pkg_failed=no
 { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for libuhdr >= 1.4.0" >&5
 printf %s "checking for libuhdr >= 1.4.0... " >&6; }

-if test -n "$UHDR_CFLAGS"; then
-    pkg_cv_UHDR_CFLAGS="$UHDR_CFLAGS"
+if test -n "$UHDR_SHARED_CFLAGS"; then
+    pkg_cv_UHDR_SHARED_CFLAGS="$UHDR_SHARED_CFLAGS"
+ elif test -n "$PKG_CONFIG"; then
+    if test -n "$PKG_CONFIG" && \
+    { { printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"libuhdr >= 1.4.0\""; } >&5
+  ($PKG_CONFIG --exists --print-errors "libuhdr >= 1.4.0") 2>&5
+  ac_status=$?
+  printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; then
+  pkg_cv_UHDR_SHARED_CFLAGS=`$PKG_CONFIG --cflags "libuhdr >= 1.4.0" 2>/dev/null`
+		      test "x$?" != "x0" && pkg_failed=yes
+else
+  pkg_failed=yes
+fi
+ else
+    pkg_failed=untried
+fi
+if test -n "$UHDR_SHARED_LIBS"; then
+    pkg_cv_UHDR_SHARED_LIBS="$UHDR_SHARED_LIBS"
+ elif test -n "$PKG_CONFIG"; then
+    if test -n "$PKG_CONFIG" && \
+    { { printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"libuhdr >= 1.4.0\""; } >&5
+  ($PKG_CONFIG --exists --print-errors "libuhdr >= 1.4.0") 2>&5
+  ac_status=$?
+  printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; then
+  pkg_cv_UHDR_SHARED_LIBS=`$PKG_CONFIG --libs "libuhdr >= 1.4.0" 2>/dev/null`
+		      test "x$?" != "x0" && pkg_failed=yes
+else
+  pkg_failed=yes
+fi
+ else
+    pkg_failed=untried
+fi
+
+
+
+if test $pkg_failed = yes; then
+        { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
+printf "%s\n" "no" >&6; }
+
+if $PKG_CONFIG --atleast-pkgconfig-version 0.20; then
+        _pkg_short_errors_supported=yes
+else
+        _pkg_short_errors_supported=no
+fi
+        if test $_pkg_short_errors_supported = yes; then
+	        UHDR_SHARED_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors --cflags --libs "libuhdr >= 1.4.0" 2>&1`
+        else
+	        UHDR_SHARED_PKG_ERRORS=`$PKG_CONFIG --print-errors --cflags --libs "libuhdr >= 1.4.0" 2>&1`
+        fi
+	# Put the nasty error message in config.log where it belongs
+	echo "$UHDR_SHARED_PKG_ERRORS" >&5
+
+	have_uhdr_shared=no
+elif test $pkg_failed = untried; then
+        { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
+printf "%s\n" "no" >&6; }
+	have_uhdr_shared=no
+else
+	UHDR_SHARED_CFLAGS=$pkg_cv_UHDR_SHARED_CFLAGS
+	UHDR_SHARED_LIBS=$pkg_cv_UHDR_SHARED_LIBS
+        { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+printf "%s\n" "yes" >&6; }
+	have_uhdr_shared=yes
+fi
+    _save_PKG_CONFIG=$PKG_CONFIG
+PKG_CONFIG="$PKG_CONFIG --static"
+
+pkg_failed=no
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for libuhdr >= 1.4.0" >&5
+printf %s "checking for libuhdr >= 1.4.0... " >&6; }
+
+if test -n "$UHDR_STATIC_CFLAGS"; then
+    pkg_cv_UHDR_STATIC_CFLAGS="$UHDR_STATIC_CFLAGS"
  elif test -n "$PKG_CONFIG"; then
     if test -n "$PKG_CONFIG" && \
     { { printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"libuhdr >= 1.4.0\""; } >&5
@@ -35817,7 +35904,7 @@ if test -n "$UHDR_CFLAGS"; then
   ac_status=$?
   printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
   test $ac_status = 0; }; then
-  pkg_cv_UHDR_CFLAGS=`$PKG_CONFIG --cflags "libuhdr >= 1.4.0" 2>/dev/null`
+  pkg_cv_UHDR_STATIC_CFLAGS=`$PKG_CONFIG --cflags "libuhdr >= 1.4.0" 2>/dev/null`
 		      test "x$?" != "x0" && pkg_failed=yes
 else
   pkg_failed=yes
@@ -35825,8 +35912,8 @@ fi
  else
     pkg_failed=untried
 fi
-if test -n "$UHDR_LIBS"; then
-    pkg_cv_UHDR_LIBS="$UHDR_LIBS"
+if test -n "$UHDR_STATIC_LIBS"; then
+    pkg_cv_UHDR_STATIC_LIBS="$UHDR_STATIC_LIBS"
  elif test -n "$PKG_CONFIG"; then
     if test -n "$PKG_CONFIG" && \
     { { printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"libuhdr >= 1.4.0\""; } >&5
@@ -35834,7 +35921,7 @@ if test -n "$UHDR_LIBS"; then
   ac_status=$?
   printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
   test $ac_status = 0; }; then
-  pkg_cv_UHDR_LIBS=`$PKG_CONFIG --libs "libuhdr >= 1.4.0" 2>/dev/null`
+  pkg_cv_UHDR_STATIC_LIBS=`$PKG_CONFIG --libs "libuhdr >= 1.4.0" 2>/dev/null`
 		      test "x$?" != "x0" && pkg_failed=yes
 else
   pkg_failed=yes
@@ -35855,29 +35942,86 @@ else
         _pkg_short_errors_supported=no
 fi
         if test $_pkg_short_errors_supported = yes; then
-	        UHDR_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors --cflags --libs "libuhdr >= 1.4.0" 2>&1`
+	        UHDR_STATIC_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors --cflags --libs "libuhdr >= 1.4.0" 2>&1`
         else
-	        UHDR_PKG_ERRORS=`$PKG_CONFIG --print-errors --cflags --libs "libuhdr >= 1.4.0" 2>&1`
+	        UHDR_STATIC_PKG_ERRORS=`$PKG_CONFIG --print-errors --cflags --libs "libuhdr >= 1.4.0" 2>&1`
         fi
 	# Put the nasty error message in config.log where it belongs
-	echo "$UHDR_PKG_ERRORS" >&5
+	echo "$UHDR_STATIC_PKG_ERRORS" >&5

-	have_uhdr=no
+	have_uhdr_static=no
 elif test $pkg_failed = untried; then
         { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
 printf "%s\n" "no" >&6; }
-	have_uhdr=no
+	have_uhdr_static=no
 else
-	UHDR_CFLAGS=$pkg_cv_UHDR_CFLAGS
-	UHDR_LIBS=$pkg_cv_UHDR_LIBS
+	UHDR_STATIC_CFLAGS=$pkg_cv_UHDR_STATIC_CFLAGS
+	UHDR_STATIC_LIBS=$pkg_cv_UHDR_STATIC_LIBS
         { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: yes" >&5
 printf "%s\n" "yes" >&6; }
-	have_uhdr=yes
+	have_uhdr_static=yes
 fi
+PKG_CONFIG=$_save_PKG_CONFIG
     { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: " >&5
 printf "%s\n" "" >&6; }
 fi

+if test "$have_uhdr_shared" = 'yes' || test "$have_uhdr_static" = 'yes'; then
+  { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking whether libuhdr links from C" >&5
+printf %s "checking whether libuhdr links from C... " >&6; }
+  uhdr_links_from_c='no'
+  saved_CPPFLAGS="$CPPFLAGS"
+  saved_LIBS="$LIBS"
+  for uhdr_link_mode in shared static; do
+    if test "$uhdr_link_mode" = 'shared'; then
+      if test "$have_uhdr_shared" != 'yes'; then
+        continue
+      fi
+      candidate_cflags="$UHDR_SHARED_CFLAGS"
+      candidate_libs="$UHDR_SHARED_LIBS"
+    else
+      if test "$have_uhdr_static" != 'yes'; then
+        continue
+      fi
+      candidate_cflags="$UHDR_STATIC_CFLAGS"
+      candidate_libs="$UHDR_STATIC_LIBS"
+    fi
+    CPPFLAGS="$candidate_cflags $saved_CPPFLAGS"
+    for cxx_runtime in '' '-lstdc++' '-lc++' '-lc++ -lc++abi'; do
+      LIBS="$candidate_libs $cxx_runtime $MATH_LIBS $saved_LIBS"
+      cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <ultrahdr_api.h>
+int
+main (void)
+{
+uhdr_codec_private_t *decoder=uhdr_create_decoder();
+            uhdr_codec_private_t *encoder=uhdr_create_encoder();
+            (void) uhdr_encode(encoder);
+            uhdr_release_encoder(encoder);
+            uhdr_release_decoder(decoder);
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"
+then :
+  UHDR_CFLAGS="$candidate_cflags"
+         UHDR_LIBS="$candidate_libs $cxx_runtime"
+         uhdr_links_from_c='yes'
+         have_uhdr='yes'
+         break 2
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.beam \
+    conftest$ac_exeext conftest.$ac_ext
+    done
+  done
+  CPPFLAGS="$saved_CPPFLAGS"
+  LIBS="$saved_LIBS"
+  { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $uhdr_links_from_c" >&5
+printf "%s\n" "$uhdr_links_from_c" >&6; }
+fi
+
 if test "$have_uhdr" = 'yes'; then

 printf "%s\n" "#define UHDR_DELEGATE 1" >>confdefs.h
@@ -44201,4 +44345,3 @@ Options used to compile and link:
   DELEGATES         = $MAGICK_DELEGATES
 ==============================================================================
 " >&6;}
-
diff --git a/configure.ac b/configure.ac
index 56f3ed9dd..5bafe3c08 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2896,15 +2896,61 @@ if test "$with_uhdr" != 'yes'; then
 fi

 have_uhdr='no'
+have_uhdr_shared='no'
+have_uhdr_static='no'
 UHDR_CFLAGS=""
 UHDR_LIBS=""
 UHDR_PKG=""
 if test "$with_uhdr" != 'no'; then
     AC_MSG_RESULT([-------------------------------------------------------------])
-    PKG_CHECK_MODULES([UHDR],[libuhdr >= 1.4.0],[have_uhdr=yes],[have_uhdr=no])
+    PKG_CHECK_MODULES([UHDR_SHARED],[libuhdr >= 1.4.0],
+      [have_uhdr_shared=yes],[have_uhdr_shared=no])
+    PKG_CHECK_MODULES_STATIC([UHDR_STATIC],[libuhdr >= 1.4.0],
+      [have_uhdr_static=yes],[have_uhdr_static=no])
     AC_MSG_RESULT([])
 fi

+if test "$have_uhdr_shared" = 'yes' || test "$have_uhdr_static" = 'yes'; then
+  AC_MSG_CHECKING([whether libuhdr links from C])
+  uhdr_links_from_c='no'
+  saved_CPPFLAGS="$CPPFLAGS"
+  saved_LIBS="$LIBS"
+  for uhdr_link_mode in shared static; do
+    if test "$uhdr_link_mode" = 'shared'; then
+      if test "$have_uhdr_shared" != 'yes'; then
+        continue
+      fi
+      candidate_cflags="$UHDR_SHARED_CFLAGS"
+      candidate_libs="$UHDR_SHARED_LIBS"
+    else
+      if test "$have_uhdr_static" != 'yes'; then
+        continue
+      fi
+      candidate_cflags="$UHDR_STATIC_CFLAGS"
+      candidate_libs="$UHDR_STATIC_LIBS"
+    fi
+    CPPFLAGS="$candidate_cflags $saved_CPPFLAGS"
+    for cxx_runtime in '' '-lstdc++' '-lc++' '-lc++ -lc++abi'; do
+      LIBS="$candidate_libs $cxx_runtime $MATH_LIBS $saved_LIBS"
+      AC_LINK_IFELSE(
+        [AC_LANG_PROGRAM([[#include <ultrahdr_api.h>]],
+          [[uhdr_codec_private_t *decoder=uhdr_create_decoder();
+            uhdr_codec_private_t *encoder=uhdr_create_encoder();
+            (void) uhdr_encode(encoder);
+            uhdr_release_encoder(encoder);
+            uhdr_release_decoder(decoder);]])],
+        [UHDR_CFLAGS="$candidate_cflags"
+         UHDR_LIBS="$candidate_libs $cxx_runtime"
+         uhdr_links_from_c='yes'
+         have_uhdr='yes'
+         break 2],[])
+    done
+  done
+  CPPFLAGS="$saved_CPPFLAGS"
+  LIBS="$saved_LIBS"
+  AC_MSG_RESULT([$uhdr_links_from_c])
+fi
+
 if test "$have_uhdr" = 'yes'; then
   AC_DEFINE([UHDR_DELEGATE],[1],[Define if you have google ultrahdr library])
   CFLAGS="$UHDR_CFLAGS $CFLAGS"