SDL_mixer: autotools: simplified libmikmod detection

From b9b329ea334bbb2f69eeb3eef65f17b97fd21902 Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Sun, 22 May 2022 20:55:04 +0300
Subject: [PATCH] autotools: simplified libmikmod detection

---
 configure    | 114 +++++++++++++++++++++++++--------------------------
 configure.ac |  53 ++++++++++--------------
 2 files changed, 77 insertions(+), 90 deletions(-)

diff --git a/configure b/configure
index 2c0f05cc..8219bda2 100755
--- a/configure
+++ b/configure
@@ -795,6 +795,8 @@ FLUIDSYNTH_LIBS
 FLUIDSYNTH_CFLAGS
 MODPLUG_LIBS
 MODPLUG_CFLAGS
+LIBMIKMOD_LIBS
+LIBMIKMOD_CFLAGS
 LIBMIKMOD_CONFIG
 SDL_CONFIG
 SDL_LIBS
@@ -4080,13 +4082,13 @@ if ${lt_cv_nm_interface+:} false; then :
 else
   lt_cv_nm_interface="BSD nm"
   echo "int some_variable = 0;" > conftest.$ac_ext
-  (eval echo "\"\$as_me:4083: $ac_compile\"" >&5)
+  (eval echo "\"\$as_me:4085: $ac_compile\"" >&5)
   (eval "$ac_compile" 2>conftest.err)
   cat conftest.err >&5
-  (eval echo "\"\$as_me:4086: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
+  (eval echo "\"\$as_me:4088: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
   (eval "$NM \"conftest.$ac_objext\"" 2>conftest.err > conftest.out)
   cat conftest.err >&5
-  (eval echo "\"\$as_me:4089: output\"" >&5)
+  (eval echo "\"\$as_me:4091: output\"" >&5)
   cat conftest.out >&5
   if $GREP 'External.*some_variable' conftest.out > /dev/null; then
     lt_cv_nm_interface="MS dumpbin"
@@ -5297,7 +5299,7 @@ ia64-*-hpux*)
   ;;
 *-*-irix6*)
   # Find out which ABI we are using.
-  echo '#line 5300 "configure"' > conftest.$ac_ext
+  echo '#line 5302 "configure"' > conftest.$ac_ext
   if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_compile\""; } >&5
   (eval $ac_compile) 2>&5
   ac_status=$?
@@ -7127,11 +7129,11 @@ else
    -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:7130: $lt_compile\"" >&5)
+   (eval echo "\"\$as_me:7132: $lt_compile\"" >&5)
    (eval "$lt_compile" 2>conftest.err)
    ac_status=$?
    cat conftest.err >&5
-   echo "$as_me:7134: \$? = $ac_status" >&5
+   echo "$as_me:7136: \$? = $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.
@@ -7466,11 +7468,11 @@ else
    -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:7469: $lt_compile\"" >&5)
+   (eval echo "\"\$as_me:7471: $lt_compile\"" >&5)
    (eval "$lt_compile" 2>conftest.err)
    ac_status=$?
    cat conftest.err >&5
-   echo "$as_me:7473: \$? = $ac_status" >&5
+   echo "$as_me:7475: \$? = $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.
@@ -7571,11 +7573,11 @@ else
    -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:7574: $lt_compile\"" >&5)
+   (eval echo "\"\$as_me:7576: $lt_compile\"" >&5)
    (eval "$lt_compile" 2>out/conftest.err)
    ac_status=$?
    cat out/conftest.err >&5
-   echo "$as_me:7578: \$? = $ac_status" >&5
+   echo "$as_me:7580: \$? = $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
@@ -7626,11 +7628,11 @@ else
    -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:7629: $lt_compile\"" >&5)
+   (eval echo "\"\$as_me:7631: $lt_compile\"" >&5)
    (eval "$lt_compile" 2>out/conftest.err)
    ac_status=$?
    cat out/conftest.err >&5
-   echo "$as_me:7633: \$? = $ac_status" >&5
+   echo "$as_me:7635: \$? = $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
@@ -9988,7 +9990,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 9991 "configure"
+#line 9993 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -10084,7 +10086,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 10087 "configure"
+#line 10089 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -13268,11 +13270,11 @@ else
    -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:13271: $lt_compile\"" >&5)
+   (eval echo "\"\$as_me:13273: $lt_compile\"" >&5)
    (eval "$lt_compile" 2>conftest.err)
    ac_status=$?
    cat conftest.err >&5
-   echo "$as_me:13275: \$? = $ac_status" >&5
+   echo "$as_me:13277: \$? = $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.
@@ -13367,11 +13369,11 @@ else
    -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:13370: $lt_compile\"" >&5)
+   (eval echo "\"\$as_me:13372: $lt_compile\"" >&5)
    (eval "$lt_compile" 2>out/conftest.err)
    ac_status=$?
    cat out/conftest.err >&5
-   echo "$as_me:13374: \$? = $ac_status" >&5
+   echo "$as_me:13376: \$? = $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
@@ -13419,11 +13421,11 @@ else
    -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:13422: $lt_compile\"" >&5)
+   (eval echo "\"\$as_me:13424: $lt_compile\"" >&5)
    (eval "$lt_compile" 2>out/conftest.err)
    ac_status=$?
    cat out/conftest.err >&5
-   echo "$as_me:13426: \$? = $ac_status" >&5
+   echo "$as_me:13428: \$? = $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
@@ -15519,13 +15521,6 @@ else
 fi
 
 if test x$enable_music_mod = xyes; then
-    have_libmikmod=no
-    libmikmod_maj=3
-    libmikmod_min=1
-    libmikmod_rev=10
-    libmikmod_ver="$libmikmod_maj.$libmikmod_min.$libmikmod_rev"
-    CFLAGS_SAVED="$CFLAGS"
-    LIBS_SAVED="$LIBS"
     # Extract the first word of "libmikmod-config", so it can be a program name with args.
 set dummy libmikmod-config; ac_word=$2
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
@@ -15567,43 +15562,46 @@ $as_echo "no" >&6; }
 fi
 
 
+
+
     if test "$LIBMIKMOD_CONFIG" != "no" ; then
-        CFLAGS="$CFLAGS `$LIBMIKMOD_CONFIG --cflags`"
-        LIBS="$LIBS `$LIBMIKMOD_CONFIG --libs`"
-        have_libmikmod=yes
-        { $as_echo "$as_me:${as_lineno-$LINENO}: checking for libmikmod - version >= $libmikmod_ver" >&5
-$as_echo_n "checking for libmikmod - version >= $libmikmod_ver... " >&6; }
-        if test "$cross_compiling" = yes; then :
-  echo $ac_n "cross compiling; assumed OK... $ac_c"
-else
-  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+       test -z "$LIBMIKMOD_CFLAGS" && LIBMIKMOD_CFLAGS=`$LIBMIKMOD_CONFIG --cflags`
+       test -z "$LIBMIKMOD_LIBS" && LIBMIKMOD_LIBS=`$LIBMIKMOD_CONFIG --libs`
+    fi
+    CFLAGS_SAVED="$CFLAGS"
+    LIBS_SAVED="$LIBS"
+    CFLAGS="$CFLAGS $LIBMIKMOD_CFLAGS"
+    LIBS="$LIBS $LIBMIKMOD_LIBS"
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking for libmikmod >= 3.1.10" >&5
+$as_echo_n "checking for libmikmod >= 3.1.10... " >&6; }
+    cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 /* end confdefs.h.  */
+#include <mikmod.h>
+int
+main ()
+{
 
-#include "mikmod.h"
-#include "stdio.h"
+#if (LIBMIKMOD_VERSION < 0x03010a)
+choke me
+#endif
+Player_LoadGeneric(NULL,0,0);
 
-int main(int argc, char **argv)
-{
-  long maj=$libmikmod_maj,min=$libmikmod_min,rev=$libmikmod_rev,ver=MikMod_GetVersion();
-  if (ver>=((maj<<16)|(min<<8)|(rev))) {
-      printf("yes\n");
-      return 0;
-  }
-  printf("no\n*** libmikmod is older than %d.%d.%d, not using.\n",maj,min,rev);
-  return 1;
+  ;
+  return 0;
 }
-
 _ACEOF
-if ac_fn_c_try_run "$LINENO"; then :
-
+if ac_fn_c_try_link "$LINENO"; then :
+  have_libmikmod=yes
 else
   have_libmikmod=no
 fi
-rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \
-  conftest.$ac_objext conftest.beam conftest.$ac_ext
-fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
 
-    fi
+    CFLAGS="$CFLAGS_SAVED"
+    LIBS="$LIBS_SAVED"
+    { $as_echo "$as_me:${as_lineno-$LINENO}: result: $have_libmikmod" >&5
+$as_echo "$have_libmikmod" >&6; }
 
     if test x$have_libmikmod = xyes; then
         case "$host" in
@@ -15621,20 +15619,18 @@ fi
                 ;;
         esac
         SOURCES="$SOURCES $srcdir/*_mod.c"
-        EXTRA_CFLAGS="$EXTRA_CFLAGS -DMOD_MUSIC `$LIBMIKMOD_CONFIG --cflags`"
+        EXTRA_CFLAGS="$EXTRA_CFLAGS -DMOD_MUSIC $LIBMIKMOD_CFLAGS"
         if test x$enable_music_mod_shared = xyes && test x$mikmod_lib != x; then
             echo "-- dynamic libmikmod -> $mikmod_lib"
             EXTRA_CFLAGS="$EXTRA_CFLAGS -DMOD_DYNAMIC=\\\"$mikmod_lib\\\""
         else
-            EXTRA_LDFLAGS="$EXTRA_LDFLAGS `$LIBMIKMOD_CONFIG --libs`"
-            PC_LIBS="$PC_LIBS `$LIBMIKMOD_CONFIG --libs`"
+            EXTRA_LDFLAGS="$EXTRA_LDFLAGS $LIBMIKMOD_LIBS"
+            PC_LIBS="$PC_LIBS $LIBMIKMOD_LIBS"
         fi
     else
         { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: *** Unable to find MikMod library (http://mikmod.sourceforge.net/)" >&5
 $as_echo "$as_me: WARNING: *** Unable to find MikMod library (http://mikmod.sourceforge.net/)" >&2;}
     fi
-    LIBS="$LIBS_SAVED"
-    CFLAGS="$CFLAGS_SAVED"
 fi
 
 # Check whether --enable-music-mod-modplug was given.
diff --git a/configure.ac b/configure.ac
index 3e74b5d3..a2128e49 100644
--- a/configure.ac
+++ b/configure.ac
@@ -280,35 +280,28 @@ AC_ARG_ENABLE([music-mod-shared],
 [AS_HELP_STRING([--enable-music-mod-shared], [dynamically load MOD support [default=yes]])],
               [], [enable_music_mod_shared=yes])
 if test x$enable_music_mod = xyes; then
-    have_libmikmod=no
-    libmikmod_maj=3
-    libmikmod_min=1
-    libmikmod_rev=10
-    libmikmod_ver="$libmikmod_maj.$libmikmod_min.$libmikmod_rev"
-    CFLAGS_SAVED="$CFLAGS"
-    LIBS_SAVED="$LIBS"
     AC_PATH_PROG(LIBMIKMOD_CONFIG, libmikmod-config, no, [$PATH])
+    AC_SUBST(LIBMIKMOD_CFLAGS)
+    AC_SUBST(LIBMIKMOD_LIBS)
     if test "$LIBMIKMOD_CONFIG" != "no" ; then
-        CFLAGS="$CFLAGS `$LIBMIKMOD_CONFIG --cflags`"
-        LIBS="$LIBS `$LIBMIKMOD_CONFIG --libs`"
-        have_libmikmod=yes
-        AC_MSG_CHECKING([for libmikmod - version >= $libmikmod_ver])
-        AC_TRY_RUN([
-#include "mikmod.h"
-#include "stdio.h"
-
-int main(int argc, char **argv)
-{
-  long maj=$libmikmod_maj,min=$libmikmod_min,rev=$libmikmod_rev,ver=MikMod_GetVersion();
-  if (ver>=((maj<<16)|(min<<8)|(rev))) {
-      printf("yes\n");
-      return 0;
-  }
-  printf("no\n*** libmikmod is older than %d.%d.%d, not using.\n",maj,min,rev);
-  return 1;
-}
-], [],[have_libmikmod=no],[echo $ac_n "cross compiling; assumed OK... $ac_c"])
+       test -z "$LIBMIKMOD_CFLAGS" && LIBMIKMOD_CFLAGS=`$LIBMIKMOD_CONFIG --cflags`
+       test -z "$LIBMIKMOD_LIBS" && LIBMIKMOD_LIBS=`$LIBMIKMOD_CONFIG --libs`
     fi
+    CFLAGS_SAVED="$CFLAGS"
+    LIBS_SAVED="$LIBS"
+    CFLAGS="$CFLAGS $LIBMIKMOD_CFLAGS"
+    LIBS="$LIBS $LIBMIKMOD_LIBS"
+    AC_MSG_CHECKING([for libmikmod >= 3.1.10])
+    AC_TRY_LINK([#include <mikmod.h>], [
+#if (LIBMIKMOD_VERSION < 0x03010a)
+choke me
+#endif
+Player_LoadGeneric(NULL,0,0);
+], [have_libmikmod=yes],[have_libmikmod=no])
+
+    CFLAGS="$CFLAGS_SAVED"
+    LIBS="$LIBS_SAVED"
+    AC_MSG_RESULT($have_libmikmod)
 
     if test x$have_libmikmod = xyes; then
         case "$host" in
@@ -326,19 +319,17 @@ int main(int argc, char **argv)
                 ;;
         esac
         SOURCES="$SOURCES $srcdir/*_mod.c"
-        EXTRA_CFLAGS="$EXTRA_CFLAGS -DMOD_MUSIC `$LIBMIKMOD_CONFIG --cflags`"
+        EXTRA_CFLAGS="$EXTRA_CFLAGS -DMOD_MUSIC $LIBMIKMOD_CFLAGS"
         if test x$enable_music_mod_shared = xyes && test x$mikmod_lib != x; then
             echo "-- dynamic libmikmod -> $mikmod_lib"
             EXTRA_CFLAGS="$EXTRA_CFLAGS -DMOD_DYNAMIC=\\\"$mikmod_lib\\\""
         else
-            EXTRA_LDFLAGS="$EXTRA_LDFLAGS `$LIBMIKMOD_CONFIG --libs`"
-            PC_LIBS="$PC_LIBS `$LIBMIKMOD_CONFIG --libs`"
+            EXTRA_LDFLAGS="$EXTRA_LDFLAGS $LIBMIKMOD_LIBS"
+            PC_LIBS="$PC_LIBS $LIBMIKMOD_LIBS"
         fi
     else
         AC_MSG_WARN([*** Unable to find MikMod library (http://mikmod.sourceforge.net/)])
     fi
-    LIBS="$LIBS_SAVED"
-    CFLAGS="$CFLAGS_SAVED"
 fi
 
 AC_ARG_ENABLE([music-mod-modplug],