From: Christophe CURIS <[email protected]>

Check is faster when using cached results;
User can specify his lib args for the compiler;
The library check is a bit closer to autoconf coding recommendation
(both on portability and code style recommendation).

Signed-off-by: Christophe CURIS <[email protected]>
---
 m4/wm_libmath.m4 |   74 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 68 insertions(+), 6 deletions(-)

diff --git a/m4/wm_libmath.m4 b/m4/wm_libmath.m4
index 8d056a2..49bd677 100644
--- a/m4/wm_libmath.m4
+++ b/m4/wm_libmath.m4
@@ -24,11 +24,73 @@
 AC_DEFUN_ONCE([WM_CHECK_LIBM],
 [AC_CHECK_HEADER([math.h], [],
                  [AC_MSG_ERROR([header for math library not found])])
-AC_CHECK_FUNC(atan,
-    [LIBM=],
-    [AC_CHECK_LIB(m, [atan],
-        [LIBM=-lm],
-        [AC_MSG_WARN(Could not find Math library, you may experience problems)
-         LIBM=] )] ) dnl
+_WM_SEARCH_MATHLIB([atan], [],
+    [AC_MSG_FAILURE([could not link with math library])] )
 AC_SUBST(LIBM) dnl
 ])
+
+# _WM_SHELLFN_MATHLINK
+# --------------------
+# (internal shell function only!)
+#
+# Create a shell function to check if we can link with 'function'
+# using 'lib-args' parameters to the linker
+#
+# The function must take exactly 1 parameter or the compilation will always 
fail
+AC_DEFUN([_WM_SHELLFN_MATHLINK],
+[@%:@ wm_fn_c_try_link_mathfunc FUNCTION LIBS-ARGS
+@%:@ -----------------------------------
+@%:@ Try linking with FUNCTION using LIBS-ARGS options for the linker
+wm_fn_c_try_link_mathfunc ()
+{
+  save_LIBS="$LIBS"
+  LIBS="$LIBS $[]2"
+  AC_LINK_IFELSE(
+      [AC_LANG_PROGRAM(
+          [[@%:@include <math.h>]],
+          [double vin = 0.1, vout;
+           vout = $[]1(vin);
+           return vout;])],
+      [wm_retval=0],
+      [wm_retval=1])
+  LIBS="$save_LIBS"
+  AS_SET_STATUS([$wm_retval])
+}
+])
+
+# _WM_SEARCH_MATHLIB(function, [action-if-ok], [action-if-failed])
+# ----------------------------
+# (internal macro only!)
+#
+# Use the specified function to try to find how to properly link against Math
+# library
+# Uses value provided by user with -with-math-libs if it was set, or defines it
+# with link args found to work
+# Stores the result in the shell variable LIBM
+AC_DEFUN([_WM_SEARCH_MATHLIB],
+[AC_REQUIRE([_WM_SHELLFN_MATHLINK])
+AC_CACHE_CHECK([for math library using $1], [wm_cv_mathlib_lflags],
+    [wm_found=0
+     AC_ARG_WITH([math-libs],
+        [AS_HELP_STRING([--with-math-libs=ARG],
+            [specify link arguments to math library @<:@default=-lm@:>@])],
+        [AS_IF([wm_fn_c_try_link_mathfunc $1 "$withval"],
+            [wm_cv_mathlib_lflags="$withval"
+             wm_found=1])],
+        [AS_IF([wm_fn_c_try_link_mathfunc $1 ""],
+            [wm_cv_mathlib_lflags="no-need"
+             wm_found=1],
+            [wm_fn_c_try_link_mathfunc $1 "-lm"],
+            [wm_cv_mathlib_lflags="-lm"
+             wm_found=1])])
+     AS_IF([test "x$wm_found" = "x1"],
+        [AS_UNSET([wm_found])],
+        [wm_cv_mathlib_lflags=error]) dnl
+    ])
+    AS_CASE(["x$wm_cv_mathlib_lflags"],
+        [xerror], [$3],
+        [xno-need], [LIBM=""
+                     $2],
+        [LIBM="$wm_cv_mathlib_lflags"
+         $2]) dnl
+])
-- 
1.7.10.4


-- 
To unsubscribe, send mail to [email protected].

Reply via email to