From: Boyuan Yang <byang@debian.org>
Date: Tue, 26 Apr 2022 22:16:44 -0400
Subject: Use updated libboost searching m4macros

Refresh m4 files under m4macros dir to fix libboost 1.75+ detection.

Also update configure.ac to use new AX_BOOST macros.
libboost macro update has been forwarded.

Forwarded: https://github.com/Aegisub/Aegisub/pull/235
---
 configure.ac                    |  13 +-
 m4macros/ac_agi.m4              |  14 +-
 m4macros/ax_boost_base.m4       | 433 +++++++++++++-------------
 m4macros/ax_boost_chrono.m4     | 118 ++++++++
 m4macros/ax_boost_filesystem.m4 | 118 ++++++++
 m4macros/ax_boost_locale.m4     | 119 ++++++++
 m4macros/ax_boost_regex.m4      | 111 +++++++
 m4macros/ax_boost_system.m4     | 121 ++++++++
 m4macros/ax_boost_thread.m4     | 187 ++++++++++++
 m4macros/ax_check_gl.m4         | 657 ++++++++++++++++++++++++++++++++--------
 m4macros/ax_pthread.m4          |  85 ++++--
 11 files changed, 1606 insertions(+), 370 deletions(-)
 create mode 100644 m4macros/ax_boost_chrono.m4
 create mode 100644 m4macros/ax_boost_filesystem.m4
 create mode 100644 m4macros/ax_boost_locale.m4
 create mode 100644 m4macros/ax_boost_regex.m4
 create mode 100644 m4macros/ax_boost_system.m4
 create mode 100644 m4macros/ax_boost_thread.m4

diff --git a/configure.ac b/configure.ac
index 62ee5a1..0c0d13a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -25,6 +25,7 @@ AC_INIT([Aegisub], [aegisub_version],, [aegisub])
 : ${CXXFLAGS=""}
 AC_CONFIG_SRCDIR([src/main.cpp])
 AC_CONFIG_HEADER([acconf.h])
+AC_CONFIG_MACRO_DIRS([m4macros])
 AC_GNU_SOURCE
 AC_CANONICAL_HOST
 
@@ -217,12 +218,12 @@ FONTCONFIG_CONF_DIR=$($PKG_CONFIG --variable=confdir fontconfig)
 AC_SUBST(FONTCONFIG_CONF_DIR)
 
 AX_BOOST_BASE([boost_required_version])
-AX_BOOST_LIB(chrono,     BOOST_CHRONO_LIB,     [chrono.hpp], [boost::chrono::system_clock::time_point time])
-AX_BOOST_LIB(filesystem, BOOST_FILESYSTEM_LIB, [filesystem/path.hpp], [boost::filesystem::path("data.txt")])
-AX_BOOST_LIB(locale,     BOOST_LOCALE_LIB,     [locale.hpp], [boost::locale::generator{}])
-AX_BOOST_LIB(regex,      BOOST_REGEX_LIB,      [regex.hpp], [boost::regex{}])
-AX_BOOST_LIB(system,     BOOST_SYSTEM_LIB,     [system/error_code.hpp], [boost::system::system_category])
-AX_BOOST_LIB(thread,     BOOST_THREAD_LIB,     [thread/thread.hpp], [boost::thread_group{}])
+AX_BOOST_CHRONO
+AX_BOOST_FILESYSTEM
+AX_BOOST_LOCALE
+AX_BOOST_REGEX
+AX_BOOST_SYSTEM
+AX_BOOST_THREAD
 
 cppflags_bak="$CPPFLAGS"
 CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
diff --git a/m4macros/ac_agi.m4 b/m4macros/ac_agi.m4
index c182ff4..e45a547 100644
--- a/m4macros/ac_agi.m4
+++ b/m4macros/ac_agi.m4
@@ -3,12 +3,14 @@ AC_DEFUN([AC_AGI_COMPILE],[
   aegisub_save_CPPFLAGS="$CPPFLAGS"
   CPPFLAGS="$CPPFLAGS $3"
   LIBS="$LIBS $4"
-      AC_CACHE_CHECK(
-        [whether $1 works], [agi_cv_with_$2],
-        [AC_RUN_IFELSE([AC_LANG_SOURCE([$5])],
-                       [eval agi_cv_with_$2="yes"],
-                       [eval agi_cv_with_$2="no"],
-                       [AS_IF([test $? -ne 0], [eval agi_cv_with_$2="no"], [eval agi_cv_with_$2="yes"])])])
+  AC_LANG_PUSH(C++)
+  AC_CACHE_CHECK(
+    [whether $1 works], [agi_cv_with_$2],
+    [AC_RUN_IFELSE([AC_LANG_SOURCE([$5])],
+                   [eval agi_cv_with_$2="yes"],
+                   [eval agi_cv_with_$2="no"],
+                   [AS_IF([test $? -ne 0], [eval agi_cv_with_$2="no"], [eval agi_cv_with_$2="yes"])])])
+  AC_LANG_POP(C++)
   CPPFLAGS="$aegisub_save_CPPFLAGS"
   LIBS="$aegisub_save_LIBS"
 ])
diff --git a/m4macros/ax_boost_base.m4 b/m4macros/ax_boost_base.m4
index 9d5617b..519f1c9 100644
--- a/m4macros/ax_boost_base.m4
+++ b/m4macros/ax_boost_base.m4
@@ -1,5 +1,5 @@
 # ===========================================================================
-#       http://www.gnu.org/software/autoconf-archive/ax_boost_base.html
+#      https://www.gnu.org/software/autoconf-archive/ax_boost_base.html
 # ===========================================================================
 #
 # SYNOPSIS
@@ -33,246 +33,271 @@
 #   and this notice are preserved. This file is offered as-is, without any
 #   warranty.
 
-#serial 20
+#serial 49
 
-AC_DEFUN([AX_BOOST_BASE], [
+# example boost program (need to pass version)
+m4_define([_AX_BOOST_BASE_PROGRAM],
+          [AC_LANG_PROGRAM([[
+#include <boost/version.hpp>
+]],[[
+(void) ((void)sizeof(char[1 - 2*!!((BOOST_VERSION) < ($1))]));
+]])])
+
+AC_DEFUN([AX_BOOST_BASE],
+[
 AC_ARG_WITH([boost],
-            [AS_HELP_STRING([--with-boost=PATH], [use Boost library from the specified location])],
-            [ac_boost_path="$withval"],
-            [ac_boost_path=""])
+  [AS_HELP_STRING([--with-boost@<:@=ARG@:>@],
+    [use Boost library from a standard location (ARG=yes),
+     from the specified location (ARG=<path>),
+     or disable it (ARG=no)
+     @<:@ARG=yes@:>@ ])],
+    [
+     AS_CASE([$withval],
+       [no],[want_boost="no";_AX_BOOST_BASE_boost_path=""],
+       [yes],[want_boost="yes";_AX_BOOST_BASE_boost_path=""],
+       [want_boost="yes";_AX_BOOST_BASE_boost_path="$withval"])
+    ],
+    [want_boost="yes"])
+
 
 AC_ARG_WITH([boost-libdir],
-            AS_HELP_STRING([--with-boost-libdir=LIB_DIR],
-                           [Force given directory for boost libraries.
-                            Note that this will override library path detection,
-                            so use this parameter only if default library
-                            detection fails and you know exactly where your
-                            boost libraries are located.]),
-    [ac_boost_lib_path="$withval"],
-    [ac_boost_lib_path=""])
+  [AS_HELP_STRING([--with-boost-libdir=LIB_DIR],
+    [Force given directory for boost libraries.
+     Note that this will override library path detection,
+     so use this parameter only if default library detection fails
+     and you know exactly where your boost libraries are located.])],
+  [
+   AS_IF([test -d "$withval"],
+         [_AX_BOOST_BASE_boost_lib_path="$withval"],
+    [AC_MSG_ERROR([--with-boost-libdir expected directory name])])
+  ],
+  [_AX_BOOST_BASE_boost_lib_path=""])
 
-boost_lib_version_req=ifelse([$1], ,1.20.0,$1)
-boost_lib_version_req_shorten=`expr $boost_lib_version_req : '\([[0-9]]*\.[[0-9]]*\)'`
-boost_lib_version_req_major=`expr $boost_lib_version_req : '\([[0-9]]*\)'`
-boost_lib_version_req_minor=`expr $boost_lib_version_req : '[[0-9]]*\.\([[0-9]]*\)'`
-boost_lib_version_req_sub_minor=`expr $boost_lib_version_req : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
-if test "x$boost_lib_version_req_sub_minor" = "x"; then
-    boost_lib_version_req_sub_minor="0"
-fi
-WANT_BOOST_VERSION=`expr $boost_lib_version_req_major \* 100000 \+  $boost_lib_version_req_minor \* 100 \+ $boost_lib_version_req_sub_minor`
-AC_MSG_CHECKING(for boostlib >= $boost_lib_version_req)
-succeeded=no
+BOOST_LDFLAGS=""
+BOOST_CPPFLAGS=""
+AS_IF([test "x$want_boost" = "xyes"],
+      [_AX_BOOST_BASE_RUNDETECT([$1],[$2],[$3])])
+AC_SUBST(BOOST_CPPFLAGS)
+AC_SUBST(BOOST_LDFLAGS)
+])
 
-dnl On 64-bit systems check for system libraries in both lib64 and lib.
-dnl The former is specified by FHS, but e.g. Debian does not adhere to
-dnl this (as it rises problems for generic multi-arch support).
-dnl The last entry in the list is chosen by default when no libraries
-dnl are found, e.g. when only header-only libraries are installed!
-libsubdirs="lib"
-ax_arch=`uname -m`
-if test $ax_arch = x86_64 -o $ax_arch = ppc64 -o $ax_arch = s390x -o $ax_arch = sparc64; then
-    libsubdirs="lib64 lib lib64"
-fi
 
-dnl first we check the system location for boost libraries
-dnl this location ist chosen if boost libraries are installed with the --layout=system option
-dnl or if you install boost with RPM
-if test "$ac_boost_path" != ""; then
-    BOOST_CPPFLAGS="-I$ac_boost_path/include"
-    for ac_boost_path_tmp in $libsubdirs; do
-            if test -d "$ac_boost_path"/"$ac_boost_path_tmp" ; then
-                    BOOST_LDFLAGS="-L$ac_boost_path/$ac_boost_path_tmp"
-                    break
-            fi
-    done
-elif test "$cross_compiling" != yes; then
-    for ac_boost_path_tmp in /usr /usr/local /opt /opt/local ; do
-        if test -d "$ac_boost_path_tmp/include/boost" && test -r "$ac_boost_path_tmp/include/boost"; then
-            for libsubdir in $libsubdirs ; do
-                if ls "$ac_boost_path_tmp/$libsubdir/libboost_"* >/dev/null 2>&1 ; then break; fi
-            done
-            BOOST_LDFLAGS="-L$ac_boost_path_tmp/$libsubdir"
-            BOOST_CPPFLAGS="-I$ac_boost_path_tmp/include"
-            break;
-        fi
-    done
-fi
+# convert a version string in $2 to numeric and affect to polymorphic var $1
+AC_DEFUN([_AX_BOOST_BASE_TONUMERICVERSION],[
+  AS_IF([test "x$2" = "x"],[_AX_BOOST_BASE_TONUMERICVERSION_req="1.20.0"],[_AX_BOOST_BASE_TONUMERICVERSION_req="$2"])
+  _AX_BOOST_BASE_TONUMERICVERSION_req_shorten=`expr $_AX_BOOST_BASE_TONUMERICVERSION_req : '\([[0-9]]*\.[[0-9]]*\)'`
+  _AX_BOOST_BASE_TONUMERICVERSION_req_major=`expr $_AX_BOOST_BASE_TONUMERICVERSION_req : '\([[0-9]]*\)'`
+  AS_IF([test "x$_AX_BOOST_BASE_TONUMERICVERSION_req_major" = "x"],
+        [AC_MSG_ERROR([You should at least specify libboost major version])])
+  _AX_BOOST_BASE_TONUMERICVERSION_req_minor=`expr $_AX_BOOST_BASE_TONUMERICVERSION_req : '[[0-9]]*\.\([[0-9]]*\)'`
+  AS_IF([test "x$_AX_BOOST_BASE_TONUMERICVERSION_req_minor" = "x"],
+        [_AX_BOOST_BASE_TONUMERICVERSION_req_minor="0"])
+  _AX_BOOST_BASE_TONUMERICVERSION_req_sub_minor=`expr $_AX_BOOST_BASE_TONUMERICVERSION_req : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
+  AS_IF([test "X$_AX_BOOST_BASE_TONUMERICVERSION_req_sub_minor" = "X"],
+        [_AX_BOOST_BASE_TONUMERICVERSION_req_sub_minor="0"])
+  _AX_BOOST_BASE_TONUMERICVERSION_RET=`expr $_AX_BOOST_BASE_TONUMERICVERSION_req_major \* 100000 \+  $_AX_BOOST_BASE_TONUMERICVERSION_req_minor \* 100 \+ $_AX_BOOST_BASE_TONUMERICVERSION_req_sub_minor`
+  AS_VAR_SET($1,$_AX_BOOST_BASE_TONUMERICVERSION_RET)
+])
 
-dnl overwrite ld flags if we have required special directory with
-dnl --with-boost-libdir parameter
-if test "$ac_boost_lib_path" != ""; then
-   BOOST_LDFLAGS="-L$ac_boost_lib_path"
-fi
+dnl Run the detection of boost should be run only if $want_boost
+AC_DEFUN([_AX_BOOST_BASE_RUNDETECT],[
+    _AX_BOOST_BASE_TONUMERICVERSION(WANT_BOOST_VERSION,[$1])
+    succeeded=no
 
-CPPFLAGS_SAVED="$CPPFLAGS"
-CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
-export CPPFLAGS
 
-LDFLAGS_SAVED="$LDFLAGS"
-LDFLAGS="$LDFLAGS $BOOST_LDFLAGS"
-export LDFLAGS
+    AC_REQUIRE([AC_CANONICAL_HOST])
+    dnl On 64-bit systems check for system libraries in both lib64 and lib.
+    dnl The former is specified by FHS, but e.g. Debian does not adhere to
+    dnl this (as it rises problems for generic multi-arch support).
+    dnl The last entry in the list is chosen by default when no libraries
+    dnl are found, e.g. when only header-only libraries are installed!
+    AS_CASE([${host_cpu}],
+      [x86_64],[libsubdirs="lib64 libx32 lib lib64"],
+      [mips*64*],[libsubdirs="lib64 lib32 lib lib64"],
+      [ppc64|powerpc64|s390x|sparc64|aarch64|ppc64le|powerpc64le|riscv64|e2k],[libsubdirs="lib64 lib lib64"],
+      [libsubdirs="lib"]
+    )
 
-AC_REQUIRE([AC_PROG_CXX])
-AC_LANG_PUSH(C++)
-    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
-@%:@include <boost/version.hpp>
-]], [[
-#if BOOST_VERSION >= $WANT_BOOST_VERSION
-// Everything is okay
-#else
-#  error Boost version is too old
-#endif
-]])],[
-    AC_MSG_RESULT(yes)
-succeeded=yes
-found_system=yes
-    ],[
-    ])
-AC_LANG_POP([C++])
+    dnl allow for real multi-arch paths e.g. /usr/lib/x86_64-linux-gnu. Give
+    dnl them priority over the other paths since, if libs are found there, they
+    dnl are almost assuredly the ones desired.
+    AS_CASE([${host_cpu}],
+      [i?86],[multiarch_libsubdir="lib/i386-${host_os}"],
+      [armv7l],[multiarch_libsubdir="lib/arm-${host_os}"],
+      [multiarch_libsubdir="lib/${host_cpu}-${host_os}"]
+    )
 
-dnl if we found no boost with system layout we search for boost libraries
-dnl built and installed without the --layout=system option or for a staged(not installed) version
-if test "x$succeeded" != "xyes"; then
-    _version=0
-    if test "$ac_boost_path" != ""; then
-        if test -d "$ac_boost_path" && test -r "$ac_boost_path"; then
-            for i in `ls -d $ac_boost_path/include/boost-* 2>/dev/null`; do
-                _version_tmp=`echo $i | sed "s#$ac_boost_path##" | sed 's/\/include\/boost-//' | sed 's/_/./'`
-                V_CHECK=`expr $_version_tmp \> $_version`
-                if test "$V_CHECK" = "1" ; then
-                    _version=$_version_tmp
-                fi
-                VERSION_UNDERSCORE=`echo $_version | sed 's/\./_/'`
-                BOOST_CPPFLAGS="-I$ac_boost_path/include/boost-$VERSION_UNDERSCORE"
-            done
+    dnl first we check the system location for boost libraries
+    dnl this location ist chosen if boost libraries are installed with the --layout=system option
+    dnl or if you install boost with RPM
+    AS_IF([test "x$_AX_BOOST_BASE_boost_path" != "x"],[
+        AC_MSG_CHECKING([for boostlib >= $1 ($WANT_BOOST_VERSION) includes in "$_AX_BOOST_BASE_boost_path/include"])
+         AS_IF([test -d "$_AX_BOOST_BASE_boost_path/include" && test -r "$_AX_BOOST_BASE_boost_path/include"],[
+           AC_MSG_RESULT([yes])
+           BOOST_CPPFLAGS="-I$_AX_BOOST_BASE_boost_path/include"
+           for _AX_BOOST_BASE_boost_path_tmp in $multiarch_libsubdir $libsubdirs; do
+                AC_MSG_CHECKING([for boostlib >= $1 ($WANT_BOOST_VERSION) lib path in "$_AX_BOOST_BASE_boost_path/$_AX_BOOST_BASE_boost_path_tmp"])
+                AS_IF([test -d "$_AX_BOOST_BASE_boost_path/$_AX_BOOST_BASE_boost_path_tmp" && test -r "$_AX_BOOST_BASE_boost_path/$_AX_BOOST_BASE_boost_path_tmp" ],[
+                        AC_MSG_RESULT([yes])
+                        BOOST_LDFLAGS="-L$_AX_BOOST_BASE_boost_path/$_AX_BOOST_BASE_boost_path_tmp";
+                        break;
+                ],
+      [AC_MSG_RESULT([no])])
+           done],[
+      AC_MSG_RESULT([no])])
+    ],[
+        if test X"$cross_compiling" = Xyes; then
+            search_libsubdirs=$multiarch_libsubdir
+        else
+            search_libsubdirs="$multiarch_libsubdir $libsubdirs"
         fi
-    else
-        if test "$cross_compiling" != yes; then
-            for ac_boost_path in /usr /usr/local /opt /opt/local ; do
-                if test -d "$ac_boost_path" && test -r "$ac_boost_path"; then
-                    for i in `ls -d $ac_boost_path/include/boost-* 2>/dev/null`; do
-                        _version_tmp=`echo $i | sed "s#$ac_boost_path##" | sed 's/\/include\/boost-//' | sed 's/_/./'`
-                        V_CHECK=`expr $_version_tmp \> $_version`
-                        if test "$V_CHECK" = "1" ; then
-                            _version=$_version_tmp
-                            best_path=$ac_boost_path
-                        fi
-                    done
-                fi
-            done
-
-            VERSION_UNDERSCORE=`echo $_version | sed 's/\./_/'`
-            BOOST_CPPFLAGS="-I$best_path/include/boost-$VERSION_UNDERSCORE"
-            if test "$ac_boost_lib_path" = ""; then
-                for libsubdir in $libsubdirs ; do
-                    if ls "$best_path/$libsubdir/libboost_"* >/dev/null 2>&1 ; then break; fi
+        for _AX_BOOST_BASE_boost_path_tmp in /usr /usr/local /opt /opt/local ; do
+            if test -d "$_AX_BOOST_BASE_boost_path_tmp/include/boost" && test -r "$_AX_BOOST_BASE_boost_path_tmp/include/boost" ; then
+                for libsubdir in $search_libsubdirs ; do
+                    if ls "$_AX_BOOST_BASE_boost_path_tmp/$libsubdir/libboost_"* >/dev/null 2>&1 ; then break; fi
                 done
-                BOOST_LDFLAGS="-L$best_path/$libsubdir"
+                BOOST_LDFLAGS="-L$_AX_BOOST_BASE_boost_path_tmp/$libsubdir"
+                BOOST_CPPFLAGS="-I$_AX_BOOST_BASE_boost_path_tmp/include"
+                break;
             fi
-        fi
+        done
+    ])
 
-        if test "x$BOOST_ROOT" != "x"; then
-            for libsubdir in $libsubdirs ; do
-                if ls "$BOOST_ROOT/stage/$libsubdir/libboost_"* >/dev/null 2>&1 ; then break; fi
-            done
-            if test -d "$BOOST_ROOT" && test -r "$BOOST_ROOT" && test -d "$BOOST_ROOT/stage/$libsubdir" && test -r "$BOOST_ROOT/stage/$libsubdir"; then
-                version_dir=`expr //$BOOST_ROOT : '.*/\(.*\)'`
-                stage_version=`echo $version_dir | sed 's/boost_//' | sed 's/_/./g'`
-                    stage_version_shorten=`expr $stage_version : '\([[0-9]]*\.[[0-9]]*\)'`
-                V_CHECK=`expr $stage_version_shorten \>\= $_version`
-                if test "$V_CHECK" = "1" -a "$ac_boost_lib_path" = "" ; then
-                    AC_MSG_NOTICE(We will use a staged boost library from $BOOST_ROOT)
-                    BOOST_CPPFLAGS="-I$BOOST_ROOT"
-                    BOOST_LDFLAGS="-L$BOOST_ROOT/stage/$libsubdir"
-                fi
-            fi
-        fi
-    fi
+    dnl overwrite ld flags if we have required special directory with
+    dnl --with-boost-libdir parameter
+    AS_IF([test "x$_AX_BOOST_BASE_boost_lib_path" != "x"],
+          [BOOST_LDFLAGS="-L$_AX_BOOST_BASE_boost_lib_path"])
 
+    AC_MSG_CHECKING([for boostlib >= $1 ($WANT_BOOST_VERSION)])
+    CPPFLAGS_SAVED="$CPPFLAGS"
     CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
     export CPPFLAGS
+
+    LDFLAGS_SAVED="$LDFLAGS"
     LDFLAGS="$LDFLAGS $BOOST_LDFLAGS"
     export LDFLAGS
 
+    AC_REQUIRE([AC_PROG_CXX])
     AC_LANG_PUSH(C++)
-        AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
-    @%:@include <boost/version.hpp>
-    ]], [[
-    #if BOOST_VERSION >= $WANT_BOOST_VERSION
-    // Everything is okay
-    #else
-    #  error Boost version is too old
-    #endif
-    ]])],[
+        AC_COMPILE_IFELSE([_AX_BOOST_BASE_PROGRAM($WANT_BOOST_VERSION)],[
         AC_MSG_RESULT(yes)
     succeeded=yes
     found_system=yes
         ],[
         ])
     AC_LANG_POP([C++])
-fi
 
-if test "$succeeded" != "yes" ; then
-    if test "$_version" = "0" ; then
-        AC_MSG_ERROR([[We could not detect the boost libraries (version $boost_lib_version_req_shorten or higher). If you have a staged boost library (still not installed) please specify \$BOOST_ROOT in your environment and do not give a PATH to --with-boost option.  If you are sure you have boost installed, then check your version number looking in <boost/version.hpp>. See http://randspringer.de/boost for more documentation.]])
-    else
-        AC_MSG_ERROR([Your boost libraries seem too old (version $_version).])
-    fi
-    # execute ACTION-IF-NOT-FOUND (if present):
-    ifelse([$3], , :, [$3])
-else
-    BOOSTLIBDIR=`echo $BOOST_LDFLAGS | sed -e 's/@<:@^\/@:>@*//'`
-    AC_SUBST(BOOST_CPPFLAGS)
-    AC_SUBST(BOOST_LDFLAGS)
-    # execute ACTION-IF-FOUND (if present):
-    ifelse([$2], , :, [$2])
-fi
 
-CPPFLAGS="$CPPFLAGS_SAVED"
-LDFLAGS="$LDFLAGS_SAVED"
-])
 
-AC_DEFUN([AX_BOOST_LIB], [
-AC_ARG_WITH([boost-$1],
-            AS_HELP_STRING([--with-boost-$1=LIBNAME],
-                           [Override the name of the library for boost.$1
-                           e.g. --with-boost-$1=boost_$1-gcc-mt]),
-            [ax_boost_lib="$withval"],
-            [ax_boost_lib="boost_$1"])
+    dnl if we found no boost with system layout we search for boost libraries
+    dnl built and installed without the --layout=system option or for a staged(not installed) version
+    if test "x$succeeded" != "xyes" ; then
+        CPPFLAGS="$CPPFLAGS_SAVED"
+        LDFLAGS="$LDFLAGS_SAVED"
+        BOOST_CPPFLAGS=
+        if test -z "$_AX_BOOST_BASE_boost_lib_path" ; then
+            BOOST_LDFLAGS=
+        fi
+        _version=0
+        if test -n "$_AX_BOOST_BASE_boost_path" ; then
+            if test -d "$_AX_BOOST_BASE_boost_path" && test -r "$_AX_BOOST_BASE_boost_path"; then
+                for i in `ls -d $_AX_BOOST_BASE_boost_path/include/boost-* 2>/dev/null`; do
+                    _version_tmp=`echo $i | sed "s#$_AX_BOOST_BASE_boost_path##" | sed 's/\/include\/boost-//' | sed 's/_/./'`
+                    V_CHECK=`expr $_version_tmp \> $_version`
+                    if test "x$V_CHECK" = "x1" ; then
+                        _version=$_version_tmp
+                    fi
+                    VERSION_UNDERSCORE=`echo $_version | sed 's/\./_/'`
+                    BOOST_CPPFLAGS="-I$_AX_BOOST_BASE_boost_path/include/boost-$VERSION_UNDERSCORE"
+                done
+                dnl if nothing found search for layout used in Windows distributions
+                if test -z "$BOOST_CPPFLAGS"; then
+                    if test -d "$_AX_BOOST_BASE_boost_path/boost" && test -r "$_AX_BOOST_BASE_boost_path/boost"; then
+                        BOOST_CPPFLAGS="-I$_AX_BOOST_BASE_boost_path"
+                    fi
+                fi
+                dnl if we found something and BOOST_LDFLAGS was unset before
+                dnl (because "$_AX_BOOST_BASE_boost_lib_path" = ""), set it here.
+                if test -n "$BOOST_CPPFLAGS" && test -z "$BOOST_LDFLAGS"; then
+                    for libsubdir in $libsubdirs ; do
+                        if ls "$_AX_BOOST_BASE_boost_path/$libsubdir/libboost_"* >/dev/null 2>&1 ; then break; fi
+                    done
+                    BOOST_LDFLAGS="-L$_AX_BOOST_BASE_boost_path/$libsubdir"
+                fi
+            fi
+        else
+            if test "x$cross_compiling" != "xyes" ; then
+                for _AX_BOOST_BASE_boost_path in /usr /usr/local /opt /opt/local ; do
+                    if test -d "$_AX_BOOST_BASE_boost_path" && test -r "$_AX_BOOST_BASE_boost_path" ; then
+                        for i in `ls -d $_AX_BOOST_BASE_boost_path/include/boost-* 2>/dev/null`; do
+                            _version_tmp=`echo $i | sed "s#$_AX_BOOST_BASE_boost_path##" | sed 's/\/include\/boost-//' | sed 's/_/./'`
+                            V_CHECK=`expr $_version_tmp \> $_version`
+                            if test "x$V_CHECK" = "x1" ; then
+                                _version=$_version_tmp
+                                best_path=$_AX_BOOST_BASE_boost_path
+                            fi
+                        done
+                    fi
+                done
 
-CPPFLAGS_SAVED="$CPPFLAGS"
-CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
-export CPPFLAGS
+                VERSION_UNDERSCORE=`echo $_version | sed 's/\./_/'`
+                BOOST_CPPFLAGS="-I$best_path/include/boost-$VERSION_UNDERSCORE"
+                if test -z "$_AX_BOOST_BASE_boost_lib_path" ; then
+                    for libsubdir in $libsubdirs ; do
+                        if ls "$best_path/$libsubdir/libboost_"* >/dev/null 2>&1 ; then break; fi
+                    done
+                    BOOST_LDFLAGS="-L$best_path/$libsubdir"
+                fi
+            fi
 
-LDFLAGS_SAVED="$LDFLAGS"
-LDFLAGS="$LDFLAGS $BOOST_LDFLAGS"
-export LDFLAGS
+            if test -n "$BOOST_ROOT" ; then
+                for libsubdir in $libsubdirs ; do
+                    if ls "$BOOST_ROOT/stage/$libsubdir/libboost_"* >/dev/null 2>&1 ; then break; fi
+                done
+                if test -d "$BOOST_ROOT" && test -r "$BOOST_ROOT" && test -d "$BOOST_ROOT/stage/$libsubdir" && test -r "$BOOST_ROOT/stage/$libsubdir"; then
+                    version_dir=`expr //$BOOST_ROOT : '.*/\(.*\)'`
+                    stage_version=`echo $version_dir | sed 's/boost_//' | sed 's/_/./g'`
+                        stage_version_shorten=`expr $stage_version : '\([[0-9]]*\.[[0-9]]*\)'`
+                    V_CHECK=`expr $stage_version_shorten \>\= $_version`
+                    if test "x$V_CHECK" = "x1" && test -z "$_AX_BOOST_BASE_boost_lib_path" ; then
+                        AC_MSG_NOTICE(We will use a staged boost library from $BOOST_ROOT)
+                        BOOST_CPPFLAGS="-I$BOOST_ROOT"
+                        BOOST_LDFLAGS="-L$BOOST_ROOT/stage/$libsubdir"
+                    fi
+                fi
+            fi
+        fi
 
-AS_IF([test x$enable_sanity_checks != xno],
-      [AC_CHECK_LIB($ax_boost_lib, exit, [ax_boost_actual_lib="-l$ax_boost_lib"])],
-      [ax_boost_actual_lib="-l$ax_boost_lib"])
+        CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
+        export CPPFLAGS
+        LDFLAGS="$LDFLAGS $BOOST_LDFLAGS"
+        export LDFLAGS
 
-if test "x$ax_boost_actual_lib" = "x"; then
-    for lib in `ls -r $BOOSTLIBDIR/libboost_$1* 2>/dev/null | sed 's,.*/lib,,' | sed 's,\..*,,'`; do
-        ax_boost_lib=${lib}
-        AC_CHECK_LIB($ax_boost_lib, exit, [ax_boost_actual_lib="-l$ax_boost_lib"; break])
-    done
-fi
+        AC_LANG_PUSH(C++)
+            AC_COMPILE_IFELSE([_AX_BOOST_BASE_PROGRAM($WANT_BOOST_VERSION)],[
+            AC_MSG_RESULT(yes)
+        succeeded=yes
+        found_system=yes
+            ],[
+            ])
+        AC_LANG_POP([C++])
+    fi
 
-if test "x$ax_boost_actual_lib" = "x"; then
-    for lib in `ls -r $BOOSTLIBDIR/boost_$1* 2>/dev/null | sed 's,.*/,,' | sed -e 's,\..*,,'` ; do
-        ax_boost_lib=${lib}
-        AC_CHECK_LIB($ax_boost_lib, exit, [ax_boost_actual_lib="-l$ax_boost_lib"; break])
-    done
-fi
+    if test "x$succeeded" != "xyes" ; then
+        if test "x$_version" = "x0" ; then
+            AC_MSG_NOTICE([[We could not detect the boost libraries (version $1 or higher). If you have a staged boost library (still not installed) please specify \$BOOST_ROOT in your environment and do not give a PATH to --with-boost option.  If you are sure you have boost installed, then check your version number looking in <boost/version.hpp>. See http://randspringer.de/boost for more documentation.]])
+        else
+            AC_MSG_NOTICE([Your boost libraries seems to old (version $_version).])
+        fi
+        # execute ACTION-IF-NOT-FOUND (if present):
+        ifelse([$3], , :, [$3])
+    else
+        AC_DEFINE(HAVE_BOOST,,[define if the Boost library is available])
+        # execute ACTION-IF-FOUND (if present):
+        ifelse([$2], , :, [$2])
+    fi
 
-if test "x$ax_boost_actual_lib" = "x"; then
-    AC_MSG_ERROR(Could not find a working version of the library!)
-fi
+    CPPFLAGS="$CPPFLAGS_SAVED"
+    LDFLAGS="$LDFLAGS_SAVED"
 
-eval $2="$ax_boost_actual_lib"
-AC_SUBST($2)
-CPPFLAGS="$CPPFLAGS_SAVED"
-LDFLAGS="$LDFLAGS_SAVED"
 ])
-
diff --git a/m4macros/ax_boost_chrono.m4 b/m4macros/ax_boost_chrono.m4
new file mode 100644
index 0000000..4cd3b86
--- /dev/null
+++ b/m4macros/ax_boost_chrono.m4
@@ -0,0 +1,118 @@
+# ===========================================================================
+#     https://www.gnu.org/software/autoconf-archive/ax_boost_chrono.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+#   AX_BOOST_CHRONO
+#
+# DESCRIPTION
+#
+#   Test for Chrono library from the Boost C++ libraries. The macro requires
+#   a preceding call to AX_BOOST_BASE. Further documentation is available at
+#   <http://randspringer.de/boost/index.html>.
+#
+#   This macro calls:
+#
+#     AC_SUBST(BOOST_CHRONO_LIB)
+#
+#   And sets:
+#
+#     HAVE_BOOST_CHRONO
+#
+# LICENSE
+#
+#   Copyright (c) 2012 Xiyue Deng <manphiz@gmail.com>
+#
+#   Copying and distribution of this file, with or without modification, are
+#   permitted in any medium without royalty provided the copyright notice
+#   and this notice are preserved. This file is offered as-is, without any
+#   warranty.
+
+#serial 5
+
+AC_DEFUN([AX_BOOST_CHRONO],
+[
+	AC_ARG_WITH([boost-chrono],
+	AS_HELP_STRING([--with-boost-chrono@<:@=special-lib@:>@],
+                   [use the Chrono library from boost - it is possible to specify a certain library for the linker
+                        e.g. --with-boost-chrono=boost_chrono-gcc-mt ]),
+        [
+        if test "$withval" = "no"; then
+			want_boost="no"
+        elif test "$withval" = "yes"; then
+            want_boost="yes"
+            ax_boost_user_chrono_lib=""
+        else
+		    want_boost="yes"
+		ax_boost_user_chrono_lib="$withval"
+		fi
+        ],
+        [want_boost="yes"]
+	)
+
+	if test "x$want_boost" = "xyes"; then
+        AC_REQUIRE([AC_PROG_CC])
+        AC_REQUIRE([AC_CANONICAL_BUILD])
+		CPPFLAGS_SAVED="$CPPFLAGS"
+		CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
+		export CPPFLAGS
+
+		LDFLAGS_SAVED="$LDFLAGS"
+		LDFLAGS="$LDFLAGS $BOOST_LDFLAGS"
+		export LDFLAGS
+
+        AC_CACHE_CHECK(whether the Boost::Chrono library is available,
+					   ax_cv_boost_chrono,
+        [AC_LANG_PUSH([C++])
+			 CXXFLAGS_SAVE=$CXXFLAGS
+
+			 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[@%:@include <boost/chrono.hpp>]],
+                                   [[boost::chrono::system_clock::time_point* time = new boost::chrono::system_clock::time_point; delete time;]])],
+                   ax_cv_boost_chrono=yes, ax_cv_boost_chrono=no)
+			 CXXFLAGS=$CXXFLAGS_SAVE
+             AC_LANG_POP([C++])
+		])
+		if test "x$ax_cv_boost_chrono" = "xyes"; then
+			AC_SUBST(BOOST_CPPFLAGS)
+
+			AC_DEFINE(HAVE_BOOST_CHRONO,,[define if the Boost::Chrono library is available])
+            BOOSTLIBDIR=`echo $BOOST_LDFLAGS | sed -e 's/@<:@^\/@:>@*//'`
+
+			LDFLAGS_SAVE=$LDFLAGS
+            if test "x$ax_boost_user_chrono_lib" = "x"; then
+                for libextension in `ls $BOOSTLIBDIR/libboost_chrono*.so* $BOOSTLIBDIR/libboost_chrono*.dylib* $BOOSTLIBDIR/libboost_chrono*.a* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^lib\(boost_chrono.*\)\.so.*$;\1;' -e 's;^lib\(boost_chrono.*\)\.dylib.*$;\1;' -e 's;^lib\(boost_chrono.*\)\.a.*$;\1;'` ; do
+                     ax_lib=${libextension}
+				    AC_CHECK_LIB($ax_lib, exit,
+                                 [BOOST_CHRONO_LIB="-l$ax_lib"; AC_SUBST(BOOST_CHRONO_LIB) link_chrono="yes"; break],
+                                 [link_chrono="no"])
+				done
+                if test "x$link_chrono" != "xyes"; then
+                for libextension in `ls $BOOSTLIBDIR/boost_chrono*.dll* $BOOSTLIBDIR/boost_chrono*.a* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^\(boost_chrono.*\)\.dll.*$;\1;' -e 's;^\(boost_chrono.*\)\.a.*$;\1;'` ; do
+                     ax_lib=${libextension}
+				    AC_CHECK_LIB($ax_lib, exit,
+                                 [BOOST_CHRONO_LIB="-l$ax_lib"; AC_SUBST(BOOST_CHRONO_LIB) link_chrono="yes"; break],
+                                 [link_chrono="no"])
+				done
+                fi
+
+            else
+               for ax_lib in $ax_boost_user_chrono_lib boost_chrono-$ax_boost_user_chrono_lib; do
+				      AC_CHECK_LIB($ax_lib, exit,
+                                   [BOOST_CHRONO_LIB="-l$ax_lib"; AC_SUBST(BOOST_CHRONO_LIB) link_chrono="yes"; break],
+                                   [link_chrono="no"])
+                  done
+
+            fi
+            if test "x$ax_lib" = "x"; then
+                AC_MSG_ERROR(Could not find a version of the Boost::Chrono library!)
+            fi
+			if test "x$link_chrono" = "xno"; then
+				AC_MSG_ERROR(Could not link against $ax_lib !)
+			fi
+		fi
+
+		CPPFLAGS="$CPPFLAGS_SAVED"
+	LDFLAGS="$LDFLAGS_SAVED"
+	fi
+])
diff --git a/m4macros/ax_boost_filesystem.m4 b/m4macros/ax_boost_filesystem.m4
new file mode 100644
index 0000000..12f7bc5
--- /dev/null
+++ b/m4macros/ax_boost_filesystem.m4
@@ -0,0 +1,118 @@
+# ===========================================================================
+#   https://www.gnu.org/software/autoconf-archive/ax_boost_filesystem.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+#   AX_BOOST_FILESYSTEM
+#
+# DESCRIPTION
+#
+#   Test for Filesystem library from the Boost C++ libraries. The macro
+#   requires a preceding call to AX_BOOST_BASE. Further documentation is
+#   available at <http://randspringer.de/boost/index.html>.
+#
+#   This macro calls:
+#
+#     AC_SUBST(BOOST_FILESYSTEM_LIB)
+#
+#   And sets:
+#
+#     HAVE_BOOST_FILESYSTEM
+#
+# LICENSE
+#
+#   Copyright (c) 2009 Thomas Porschberg <thomas@randspringer.de>
+#   Copyright (c) 2009 Michael Tindal
+#   Copyright (c) 2009 Roman Rybalko <libtorrent@romanr.info>
+#
+#   Copying and distribution of this file, with or without modification, are
+#   permitted in any medium without royalty provided the copyright notice
+#   and this notice are preserved. This file is offered as-is, without any
+#   warranty.
+
+#serial 28
+
+AC_DEFUN([AX_BOOST_FILESYSTEM],
+[
+	AC_ARG_WITH([boost-filesystem],
+	AS_HELP_STRING([--with-boost-filesystem@<:@=special-lib@:>@],
+                   [use the Filesystem library from boost - it is possible to specify a certain library for the linker
+                        e.g. --with-boost-filesystem=boost_filesystem-gcc-mt ]),
+        [
+        if test "$withval" = "no"; then
+			want_boost="no"
+        elif test "$withval" = "yes"; then
+            want_boost="yes"
+            ax_boost_user_filesystem_lib=""
+        else
+		    want_boost="yes"
+		ax_boost_user_filesystem_lib="$withval"
+		fi
+        ],
+        [want_boost="yes"]
+	)
+
+	if test "x$want_boost" = "xyes"; then
+        AC_REQUIRE([AC_PROG_CC])
+		CPPFLAGS_SAVED="$CPPFLAGS"
+		CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
+		export CPPFLAGS
+
+		LDFLAGS_SAVED="$LDFLAGS"
+		LDFLAGS="$LDFLAGS $BOOST_LDFLAGS"
+		export LDFLAGS
+
+		LIBS_SAVED=$LIBS
+		LIBS="$LIBS $BOOST_SYSTEM_LIB"
+		export LIBS
+
+        AC_CACHE_CHECK(whether the Boost::Filesystem library is available,
+					   ax_cv_boost_filesystem,
+        [AC_LANG_PUSH([C++])
+         AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[@%:@include <boost/filesystem/path.hpp>]],
+                                   [[using namespace boost::filesystem;
+                                   path my_path( "foo/bar/data.txt" );
+                                   return 0;]])],
+					       ax_cv_boost_filesystem=yes, ax_cv_boost_filesystem=no)
+         AC_LANG_POP([C++])
+		])
+		if test "x$ax_cv_boost_filesystem" = "xyes"; then
+			AC_DEFINE(HAVE_BOOST_FILESYSTEM,,[define if the Boost::Filesystem library is available])
+            BOOSTLIBDIR=`echo $BOOST_LDFLAGS | sed -e 's/@<:@^\/@:>@*//'`
+            if test "x$ax_boost_user_filesystem_lib" = "x"; then
+                for libextension in `ls -r $BOOSTLIBDIR/libboost_filesystem* 2>/dev/null | sed 's,.*/lib,,' | sed 's,\..*,,'` ; do
+                     ax_lib=${libextension}
+				    AC_CHECK_LIB($ax_lib, exit,
+                                 [BOOST_FILESYSTEM_LIB="-l$ax_lib"; AC_SUBST(BOOST_FILESYSTEM_LIB) link_filesystem="yes"; break],
+                                 [link_filesystem="no"])
+				done
+                if test "x$link_filesystem" != "xyes"; then
+                for libextension in `ls -r $BOOSTLIBDIR/boost_filesystem* 2>/dev/null | sed 's,.*/,,' | sed -e 's,\..*,,'` ; do
+                     ax_lib=${libextension}
+				    AC_CHECK_LIB($ax_lib, exit,
+                                 [BOOST_FILESYSTEM_LIB="-l$ax_lib"; AC_SUBST(BOOST_FILESYSTEM_LIB) link_filesystem="yes"; break],
+                                 [link_filesystem="no"])
+				done
+		    fi
+            else
+               for ax_lib in $ax_boost_user_filesystem_lib boost_filesystem-$ax_boost_user_filesystem_lib; do
+				      AC_CHECK_LIB($ax_lib, exit,
+                                   [BOOST_FILESYSTEM_LIB="-l$ax_lib"; AC_SUBST(BOOST_FILESYSTEM_LIB) link_filesystem="yes"; break],
+                                   [link_filesystem="no"])
+                  done
+
+            fi
+            if test "x$ax_lib" = "x"; then
+                AC_MSG_ERROR(Could not find a version of the Boost::Filesystem library!)
+            fi
+			if test "x$link_filesystem" != "xyes"; then
+				AC_MSG_ERROR(Could not link against $ax_lib !)
+			fi
+		fi
+
+		CPPFLAGS="$CPPFLAGS_SAVED"
+		LDFLAGS="$LDFLAGS_SAVED"
+		LIBS="$LIBS_SAVED"
+	fi
+])
diff --git a/m4macros/ax_boost_locale.m4 b/m4macros/ax_boost_locale.m4
new file mode 100644
index 0000000..0f7562e
--- /dev/null
+++ b/m4macros/ax_boost_locale.m4
@@ -0,0 +1,119 @@
+# ===========================================================================
+#     https://www.gnu.org/software/autoconf-archive/ax_boost_locale.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+#   AX_BOOST_LOCALE
+#
+# DESCRIPTION
+#
+#   Test for System library from the Boost C++ libraries. The macro requires
+#   a preceding call to AX_BOOST_BASE. Further documentation is available at
+#   <http://randspringer.de/boost/index.html>.
+#
+#   This macro calls:
+#
+#     AC_SUBST(BOOST_LOCALE_LIB)
+#
+#   And sets:
+#
+#     HAVE_BOOST_LOCALE
+#
+# LICENSE
+#
+#   Copyright (c) 2012 Xiyue Deng <manphiz@gmail.com>
+#
+#   Copying and distribution of this file, with or without modification, are
+#   permitted in any medium without royalty provided the copyright notice
+#   and this notice are preserved. This file is offered as-is, without any
+#   warranty.
+
+#serial 3
+
+AC_DEFUN([AX_BOOST_LOCALE],
+[
+	AC_ARG_WITH([boost-locale],
+	AS_HELP_STRING([--with-boost-locale@<:@=special-lib@:>@],
+                   [use the Locale library from boost - it is possible to specify a certain library for the linker
+                        e.g. --with-boost-locale=boost_locale-gcc-mt ]),
+        [
+        if test "$withval" = "no"; then
+			want_boost="no"
+        elif test "$withval" = "yes"; then
+            want_boost="yes"
+            ax_boost_user_locale_lib=""
+        else
+		    want_boost="yes"
+		ax_boost_user_locale_lib="$withval"
+		fi
+        ],
+        [want_boost="yes"]
+	)
+
+	if test "x$want_boost" = "xyes"; then
+        AC_REQUIRE([AC_PROG_CC])
+        AC_REQUIRE([AC_CANONICAL_BUILD])
+		CPPFLAGS_SAVED="$CPPFLAGS"
+		CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
+		export CPPFLAGS
+
+		LDFLAGS_SAVED="$LDFLAGS"
+		LDFLAGS="$LDFLAGS $BOOST_LDFLAGS"
+		export LDFLAGS
+
+        AC_CACHE_CHECK(whether the Boost::Locale library is available,
+					   ax_cv_boost_locale,
+        [AC_LANG_PUSH([C++])
+			 CXXFLAGS_SAVE=$CXXFLAGS
+
+			 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[@%:@include <boost/locale.hpp>]],
+                                   [[boost::locale::generator gen;
+                                   std::locale::global(gen(""));]])],
+                   ax_cv_boost_locale=yes, ax_cv_boost_locale=no)
+			 CXXFLAGS=$CXXFLAGS_SAVE
+             AC_LANG_POP([C++])
+		])
+		if test "x$ax_cv_boost_locale" = "xyes"; then
+			AC_SUBST(BOOST_CPPFLAGS)
+
+			AC_DEFINE(HAVE_BOOST_LOCALE,,[define if the Boost::Locale library is available])
+            BOOSTLIBDIR=`echo $BOOST_LDFLAGS | sed -e 's/@<:@^\/@:>@*//'`
+
+			LDFLAGS_SAVE=$LDFLAGS
+            if test "x$ax_boost_user_locale_lib" = "x"; then
+                for libextension in `ls $BOOSTLIBDIR/libboost_locale*.so* $BOOSTLIBDIR/libboost_locale*.dylib* $BOOSTLIBDIR/libboost_locale*.a* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^lib\(boost_locale.*\)\.so.*$;\1;' -e 's;^lib\(boost_locale.*\)\.dylib.*$;\1;' -e 's;^lib\(boost_locale.*\)\.a.*$;\1;'` ; do
+                     ax_lib=${libextension}
+				    AC_CHECK_LIB($ax_lib, exit,
+                                 [BOOST_LOCALE_LIB="-l$ax_lib"; AC_SUBST(BOOST_LOCALE_LIB) link_locale="yes"; break],
+                                 [link_locale="no"])
+				done
+                if test "x$link_locale" != "xyes"; then
+                for libextension in `ls $BOOSTLIBDIR/boost_locale*.dll* $BOOSTLIBDIR/boost_locale*.a* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^\(boost_locale.*\)\.dll.*$;\1;' -e 's;^\(boost_locale.*\)\.a.*$;\1;'` ; do
+                     ax_lib=${libextension}
+				    AC_CHECK_LIB($ax_lib, exit,
+                                 [BOOST_LOCALE_LIB="-l$ax_lib"; AC_SUBST(BOOST_LOCALE_LIB) link_locale="yes"; break],
+                                 [link_locale="no"])
+				done
+                fi
+
+            else
+               for ax_lib in $ax_boost_user_locale_lib boost_locale-$ax_boost_user_locale_lib; do
+				      AC_CHECK_LIB($ax_lib, exit,
+                                   [BOOST_LOCALE_LIB="-l$ax_lib"; AC_SUBST(BOOST_LOCALE_LIB) link_locale="yes"; break],
+                                   [link_locale="no"])
+                  done
+
+            fi
+            if test "x$ax_lib" = "x"; then
+                AC_MSG_ERROR(Could not find a version of the Boost::Locale library!)
+            fi
+			if test "x$link_locale" = "xno"; then
+				AC_MSG_ERROR(Could not link against $ax_lib !)
+			fi
+		fi
+
+		CPPFLAGS="$CPPFLAGS_SAVED"
+	LDFLAGS="$LDFLAGS_SAVED"
+	fi
+])
diff --git a/m4macros/ax_boost_regex.m4 b/m4macros/ax_boost_regex.m4
new file mode 100644
index 0000000..e2413c2
--- /dev/null
+++ b/m4macros/ax_boost_regex.m4
@@ -0,0 +1,111 @@
+# ===========================================================================
+#      https://www.gnu.org/software/autoconf-archive/ax_boost_regex.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+#   AX_BOOST_REGEX
+#
+# DESCRIPTION
+#
+#   Test for Regex library from the Boost C++ libraries. The macro requires
+#   a preceding call to AX_BOOST_BASE. Further documentation is available at
+#   <http://randspringer.de/boost/index.html>.
+#
+#   This macro calls:
+#
+#     AC_SUBST(BOOST_REGEX_LIB)
+#
+#   And sets:
+#
+#     HAVE_BOOST_REGEX
+#
+# LICENSE
+#
+#   Copyright (c) 2008 Thomas Porschberg <thomas@randspringer.de>
+#   Copyright (c) 2008 Michael Tindal
+#
+#   Copying and distribution of this file, with or without modification, are
+#   permitted in any medium without royalty provided the copyright notice
+#   and this notice are preserved. This file is offered as-is, without any
+#   warranty.
+
+#serial 23
+
+AC_DEFUN([AX_BOOST_REGEX],
+[
+	AC_ARG_WITH([boost-regex],
+	AS_HELP_STRING([--with-boost-regex@<:@=special-lib@:>@],
+                   [use the Regex library from boost - it is possible to specify a certain library for the linker
+                        e.g. --with-boost-regex=boost_regex-gcc-mt-d-1_33_1 ]),
+        [
+        if test "$withval" = "no"; then
+			want_boost="no"
+        elif test "$withval" = "yes"; then
+            want_boost="yes"
+            ax_boost_user_regex_lib=""
+        else
+		    want_boost="yes"
+		ax_boost_user_regex_lib="$withval"
+		fi
+        ],
+        [want_boost="yes"]
+	)
+
+	if test "x$want_boost" = "xyes"; then
+        AC_REQUIRE([AC_PROG_CC])
+		CPPFLAGS_SAVED="$CPPFLAGS"
+		CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
+		export CPPFLAGS
+
+		LDFLAGS_SAVED="$LDFLAGS"
+		LDFLAGS="$LDFLAGS $BOOST_LDFLAGS"
+		export LDFLAGS
+
+        AC_CACHE_CHECK(whether the Boost::Regex library is available,
+					   ax_cv_boost_regex,
+        [AC_LANG_PUSH([C++])
+			 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[@%:@include <boost/regex.hpp>
+												]],
+                                   [[boost::regex r(); return 0;]])],
+                   ax_cv_boost_regex=yes, ax_cv_boost_regex=no)
+         AC_LANG_POP([C++])
+		])
+		if test "x$ax_cv_boost_regex" = "xyes"; then
+			AC_DEFINE(HAVE_BOOST_REGEX,,[define if the Boost::Regex library is available])
+            BOOSTLIBDIR=`echo $BOOST_LDFLAGS | sed -e 's/@<:@^\/@:>@*//'`
+            if test "x$ax_boost_user_regex_lib" = "x"; then
+                for libextension in `ls $BOOSTLIBDIR/libboost_regex*.so* $BOOSTLIBDIR/libboost_regex*.dylib* $BOOSTLIBDIR/libboost_regex*.a* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^lib\(boost_regex.*\)\.so.*$;\1;' -e 's;^lib\(boost_regex.*\)\.dylib.*;\1;' -e 's;^lib\(boost_regex.*\)\.a.*$;\1;'` ; do
+                     ax_lib=${libextension}
+				    AC_CHECK_LIB($ax_lib, exit,
+                                 [BOOST_REGEX_LIB="-l$ax_lib"; AC_SUBST(BOOST_REGEX_LIB) link_regex="yes"; break],
+                                 [link_regex="no"])
+				done
+                if test "x$link_regex" != "xyes"; then
+                for libextension in `ls $BOOSTLIBDIR/boost_regex*.dll* $BOOSTLIBDIR/boost_regex*.a* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^\(boost_regex.*\)\.dll.*$;\1;' -e 's;^\(boost_regex.*\)\.a.*$;\1;'` ; do
+                     ax_lib=${libextension}
+				    AC_CHECK_LIB($ax_lib, exit,
+                                 [BOOST_REGEX_LIB="-l$ax_lib"; AC_SUBST(BOOST_REGEX_LIB) link_regex="yes"; break],
+                                 [link_regex="no"])
+				done
+                fi
+
+            else
+               for ax_lib in $ax_boost_user_regex_lib boost_regex-$ax_boost_user_regex_lib; do
+				      AC_CHECK_LIB($ax_lib, main,
+                                   [BOOST_REGEX_LIB="-l$ax_lib"; AC_SUBST(BOOST_REGEX_LIB) link_regex="yes"; break],
+                                   [link_regex="no"])
+               done
+            fi
+            if test "x$ax_lib" = "x"; then
+                AC_MSG_ERROR(Could not find a version of the Boost::Regex library!)
+            fi
+			if test "x$link_regex" != "xyes"; then
+				AC_MSG_ERROR(Could not link against $ax_lib !)
+			fi
+		fi
+
+		CPPFLAGS="$CPPFLAGS_SAVED"
+	LDFLAGS="$LDFLAGS_SAVED"
+	fi
+])
diff --git a/m4macros/ax_boost_system.m4 b/m4macros/ax_boost_system.m4
new file mode 100644
index 0000000..323e2a6
--- /dev/null
+++ b/m4macros/ax_boost_system.m4
@@ -0,0 +1,121 @@
+# ===========================================================================
+#     https://www.gnu.org/software/autoconf-archive/ax_boost_system.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+#   AX_BOOST_SYSTEM
+#
+# DESCRIPTION
+#
+#   Test for System library from the Boost C++ libraries. The macro requires
+#   a preceding call to AX_BOOST_BASE. Further documentation is available at
+#   <http://randspringer.de/boost/index.html>.
+#
+#   This macro calls:
+#
+#     AC_SUBST(BOOST_SYSTEM_LIB)
+#
+#   And sets:
+#
+#     HAVE_BOOST_SYSTEM
+#
+# LICENSE
+#
+#   Copyright (c) 2008 Thomas Porschberg <thomas@randspringer.de>
+#   Copyright (c) 2008 Michael Tindal
+#   Copyright (c) 2008 Daniel Casimiro <dan.casimiro@gmail.com>
+#
+#   Copying and distribution of this file, with or without modification, are
+#   permitted in any medium without royalty provided the copyright notice
+#   and this notice are preserved. This file is offered as-is, without any
+#   warranty.
+
+#serial 20
+
+AC_DEFUN([AX_BOOST_SYSTEM],
+[
+	AC_ARG_WITH([boost-system],
+	AS_HELP_STRING([--with-boost-system@<:@=special-lib@:>@],
+                   [use the System library from boost - it is possible to specify a certain library for the linker
+                        e.g. --with-boost-system=boost_system-gcc-mt ]),
+        [
+        if test "$withval" = "no"; then
+			want_boost="no"
+        elif test "$withval" = "yes"; then
+            want_boost="yes"
+            ax_boost_user_system_lib=""
+        else
+		    want_boost="yes"
+		ax_boost_user_system_lib="$withval"
+		fi
+        ],
+        [want_boost="yes"]
+	)
+
+	if test "x$want_boost" = "xyes"; then
+        AC_REQUIRE([AC_PROG_CC])
+        AC_REQUIRE([AC_CANONICAL_BUILD])
+		CPPFLAGS_SAVED="$CPPFLAGS"
+		CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
+		export CPPFLAGS
+
+		LDFLAGS_SAVED="$LDFLAGS"
+		LDFLAGS="$LDFLAGS $BOOST_LDFLAGS"
+		export LDFLAGS
+
+        AC_CACHE_CHECK(whether the Boost::System library is available,
+					   ax_cv_boost_system,
+        [AC_LANG_PUSH([C++])
+			 CXXFLAGS_SAVE=$CXXFLAGS
+			 CXXFLAGS=
+
+			 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[@%:@include <boost/system/error_code.hpp>]],
+				    [[boost::system::error_category *a = 0;]])],
+                   ax_cv_boost_system=yes, ax_cv_boost_system=no)
+			 CXXFLAGS=$CXXFLAGS_SAVE
+             AC_LANG_POP([C++])
+		])
+		if test "x$ax_cv_boost_system" = "xyes"; then
+			AC_SUBST(BOOST_CPPFLAGS)
+
+			AC_DEFINE(HAVE_BOOST_SYSTEM,,[define if the Boost::System library is available])
+            BOOSTLIBDIR=`echo $BOOST_LDFLAGS | sed -e 's/@<:@^\/@:>@*//'`
+
+			LDFLAGS_SAVE=$LDFLAGS
+            if test "x$ax_boost_user_system_lib" = "x"; then
+                for libextension in `ls -r $BOOSTLIBDIR/libboost_system* 2>/dev/null | sed 's,.*/lib,,' | sed 's,\..*,,'` ; do
+                     ax_lib=${libextension}
+				    AC_CHECK_LIB($ax_lib, exit,
+                                 [BOOST_SYSTEM_LIB="-l$ax_lib"; AC_SUBST(BOOST_SYSTEM_LIB) link_system="yes"; break],
+                                 [link_system="no"])
+				done
+                if test "x$link_system" != "xyes"; then
+                for libextension in `ls -r $BOOSTLIBDIR/boost_system* 2>/dev/null | sed 's,.*/,,' | sed -e 's,\..*,,'` ; do
+                     ax_lib=${libextension}
+				    AC_CHECK_LIB($ax_lib, exit,
+                                 [BOOST_SYSTEM_LIB="-l$ax_lib"; AC_SUBST(BOOST_SYSTEM_LIB) link_system="yes"; break],
+                                 [link_system="no"])
+				done
+                fi
+
+            else
+               for ax_lib in $ax_boost_user_system_lib boost_system-$ax_boost_user_system_lib; do
+				      AC_CHECK_LIB($ax_lib, exit,
+                                   [BOOST_SYSTEM_LIB="-l$ax_lib"; AC_SUBST(BOOST_SYSTEM_LIB) link_system="yes"; break],
+                                   [link_system="no"])
+                  done
+
+            fi
+            if test "x$ax_lib" = "x"; then
+                AC_MSG_ERROR(Could not find a version of the Boost::System library!)
+            fi
+			if test "x$link_system" = "xno"; then
+				AC_MSG_ERROR(Could not link against $ax_lib !)
+			fi
+		fi
+
+		CPPFLAGS="$CPPFLAGS_SAVED"
+	LDFLAGS="$LDFLAGS_SAVED"
+	fi
+])
diff --git a/m4macros/ax_boost_thread.m4 b/m4macros/ax_boost_thread.m4
new file mode 100644
index 0000000..75e80e6
--- /dev/null
+++ b/m4macros/ax_boost_thread.m4
@@ -0,0 +1,187 @@
+# ===========================================================================
+#     https://www.gnu.org/software/autoconf-archive/ax_boost_thread.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+#   AX_BOOST_THREAD
+#
+# DESCRIPTION
+#
+#   Test for Thread library from the Boost C++ libraries. The macro requires
+#   a preceding call to AX_BOOST_BASE. Further documentation is available at
+#   <http://randspringer.de/boost/index.html>.
+#
+#   This macro calls:
+#
+#     AC_SUBST(BOOST_THREAD_LIB)
+#
+#   And sets:
+#
+#     HAVE_BOOST_THREAD
+#
+# LICENSE
+#
+#   Copyright (c) 2009 Thomas Porschberg <thomas@randspringer.de>
+#   Copyright (c) 2009 Michael Tindal
+#
+#   Copying and distribution of this file, with or without modification, are
+#   permitted in any medium without royalty provided the copyright notice
+#   and this notice are preserved. This file is offered as-is, without any
+#   warranty.
+
+#serial 33
+
+AC_DEFUN([AX_BOOST_THREAD],
+[
+    AC_ARG_WITH([boost-thread],
+    AS_HELP_STRING([--with-boost-thread@<:@=special-lib@:>@],
+                   [use the Thread library from boost -
+                    it is possible to specify a certain library for the linker
+                    e.g. --with-boost-thread=boost_thread-gcc-mt ]),
+        [
+        if test "$withval" = "yes"; then
+            want_boost="yes"
+            ax_boost_user_thread_lib=""
+        else
+            want_boost="yes"
+            ax_boost_user_thread_lib="$withval"
+        fi
+        ],
+        [want_boost="yes"]
+    )
+
+    if test "x$want_boost" = "xyes"; then
+        AC_REQUIRE([AC_PROG_CC])
+        AC_REQUIRE([AC_CANONICAL_BUILD])
+        CPPFLAGS_SAVED="$CPPFLAGS"
+        CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
+        export CPPFLAGS
+
+        LDFLAGS_SAVED="$LDFLAGS"
+        LDFLAGS="$LDFLAGS $BOOST_LDFLAGS"
+        export LDFLAGS
+
+        AC_CACHE_CHECK(whether the Boost::Thread library is available,
+                       ax_cv_boost_thread,
+        [AC_LANG_PUSH([C++])
+             CXXFLAGS_SAVE=$CXXFLAGS
+
+             case "x$host_os" in
+                 xsolaris )
+                     CXXFLAGS="-pthreads $CXXFLAGS"
+                     break;
+                     ;;
+                 xmingw32 )
+                     CXXFLAGS="-mthreads $CXXFLAGS"
+                     break;
+                     ;;
+                 *android* )
+                     break;
+                     ;;
+                 * )
+                     CXXFLAGS="-pthread $CXXFLAGS"
+                     break;
+                     ;;
+             esac
+
+             AC_COMPILE_IFELSE([
+                 AC_LANG_PROGRAM(
+                     [[@%:@include <boost/thread/thread.hpp>]],
+                     [[boost::thread_group thrds;
+                       return 0;]])],
+                 ax_cv_boost_thread=yes, ax_cv_boost_thread=no)
+             CXXFLAGS=$CXXFLAGS_SAVE
+             AC_LANG_POP([C++])
+        ])
+        if test "x$ax_cv_boost_thread" = "xyes"; then
+            case "x$host_os" in
+                xsolaris )
+                    BOOST_CPPFLAGS="-pthreads $BOOST_CPPFLAGS"
+                    break;
+                    ;;
+                xmingw32 )
+                    BOOST_CPPFLAGS="-mthreads $BOOST_CPPFLAGS"
+                    break;
+                    ;;
+                *android* )
+                    break;
+                    ;;
+                * )
+                    BOOST_CPPFLAGS="-pthread $BOOST_CPPFLAGS"
+                    break;
+                    ;;
+            esac
+
+            AC_SUBST(BOOST_CPPFLAGS)
+
+            AC_DEFINE(HAVE_BOOST_THREAD,,
+                      [define if the Boost::Thread library is available])
+            BOOSTLIBDIR=`echo $BOOST_LDFLAGS | sed -e 's/@<:@^\/@:>@*//'`
+
+            LDFLAGS_SAVE=$LDFLAGS
+                        case "x$host_os" in
+                          *bsd* )
+                               LDFLAGS="-pthread $LDFLAGS"
+                          break;
+                          ;;
+                        esac
+            if test "x$ax_boost_user_thread_lib" = "x"; then
+                for libextension in `ls -r $BOOSTLIBDIR/libboost_thread* 2>/dev/null | sed 's,.*/lib,,' | sed 's,\..*,,'`; do
+                     ax_lib=${libextension}
+                    AC_CHECK_LIB($ax_lib, exit,
+                                 [link_thread="yes"; break],
+                                 [link_thread="no"])
+                done
+                if test "x$link_thread" != "xyes"; then
+                for libextension in `ls -r $BOOSTLIBDIR/boost_thread* 2>/dev/null | sed 's,.*/,,' | sed 's,\..*,,'`; do
+                     ax_lib=${libextension}
+                    AC_CHECK_LIB($ax_lib, exit,
+                                 [link_thread="yes"; break],
+                                 [link_thread="no"])
+                done
+                fi
+
+            else
+               for ax_lib in $ax_boost_user_thread_lib boost_thread-$ax_boost_user_thread_lib; do
+                      AC_CHECK_LIB($ax_lib, exit,
+                                   [link_thread="yes"; break],
+                                   [link_thread="no"])
+                  done
+
+            fi
+            if test "x$ax_lib" = "x"; then
+                AC_MSG_ERROR(Could not find a version of the Boost::Thread library!)
+            fi
+            if test "x$link_thread" = "xno"; then
+                AC_MSG_ERROR(Could not link against $ax_lib !)
+            else
+                BOOST_THREAD_LIB="-l$ax_lib"
+                case "x$host_os" in
+                    *bsd* )
+                        BOOST_LDFLAGS="-pthread $BOOST_LDFLAGS"
+                        break;
+                        ;;
+                    xsolaris )
+                        BOOST_THREAD_LIB="$BOOST_THREAD_LIB -lpthread"
+                        break;
+                        ;;
+                    xmingw32 )
+                        break;
+                        ;;
+                    *android* )
+                        break;
+                        ;;
+                    * )
+                        BOOST_THREAD_LIB="$BOOST_THREAD_LIB -lpthread"
+                        break;
+                        ;;
+                esac
+                AC_SUBST(BOOST_THREAD_LIB)
+            fi
+        fi
+
+        CPPFLAGS="$CPPFLAGS_SAVED"
+        LDFLAGS="$LDFLAGS_SAVED"
+    fi
+])
diff --git a/m4macros/ax_check_gl.m4 b/m4macros/ax_check_gl.m4
index 3530664..39423bf 100644
--- a/m4macros/ax_check_gl.m4
+++ b/m4macros/ax_check_gl.m4
@@ -1,66 +1,78 @@
-# -*- mode: autoconf -*-
+# ===========================================================================
+#        http://www.gnu.org/software/autoconf-archive/ax_check_gl.html
+# ===========================================================================
 #
-# AX_CHECK_GL
+# SYNOPSIS
 #
-# Check for an OpenGL implementation.  If GL is found, the required compiler
-# and linker flags are included in the output variables "GL_CFLAGS" and
-# "GL_LIBS", respectively.  If no usable GL implementation is found, "no_gl"
-# is set to "yes".
+#   AX_CHECK_GL
 #
-# If the header "GL/gl.h" is found, "HAVE_GL_GL_H" is defined.  If the header
-# "OpenGL/gl.h" is found, HAVE_OPENGL_GL_H is defined.  These preprocessor
-# definitions may not be mutually exclusive.
+# DESCRIPTION
 #
-# version: 2.7
-# author: Braden McDaniel <braden@endoframe.com>
+#   Check for an OpenGL implementation. If GL is found, the required
+#   compiler and linker flags are included in the output variables
+#   "GL_CFLAGS", "GL_LIBS", "GL_LDFLAGS", respectively. If no usable GL
+#   implementation is found, "no_gl" is set to "yes".
 #
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2, or (at your option)
-# any later version.
+#   You could disable OpenGL using --with-gl=no
 #
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
+#   You could choose a specific OpenGL libs using --with-gl=lib_name
 #
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-# 02110-1301, USA.
+#   Under darwin, cygwin and mingw target you could prefer the OpenGL
+#   implementation that link with X setting --with-gl=x or without X support
+#   with --with-gl=nox. Notes that this script try to guess the right
+#   implementation.
 #
-# As a special exception, the you may copy, distribute and modify the
-# configure scripts that are the output of Autoconf when processing
-# the Macro.  You need not follow the terms of the GNU General Public
-# License when using or distributing such scripts.
+#   If the header "GL/gl.h" is found, "HAVE_GL_GL_H" is defined. If the
+#   header "OpenGL/gl.h" is found, HAVE_OPENGL_GL_H is defined. These
+#   preprocessor definitions may not be mutually exclusive.
 #
-AC_DEFUN([AX_CHECK_GL],
-[AC_REQUIRE([AC_CANONICAL_HOST])dnl
-AC_REQUIRE([AC_PATH_X])dnl
-AC_REQUIRE([AC_PROG_SED])dnl
-
-AC_LANG_PUSH([C])
-AX_LANG_COMPILER_MS
-
+#   You should use something like this in your headers:
+#
+#     #if defined(HAVE_WINDOWS_H) && defined(_WIN32)
+#     # include <windows.h>
+#     #endif
+#     #ifdef HAVE_GL_GL_H
+#     # include <GL/gl.h>
+#     #elif defined(HAVE_OPENGL_GL_H)
+#     # include <OpenGL/gl.h>
+#     #else
+#     # error no gl.h
+#     #endif
+#
+# LICENSE
+#
+#   Copyright (c) 2009 Braden McDaniel <braden@endoframe.com>
+#   Copyright (c) 2012 Bastien Roucaries <roucaries.bastien+autoconf@gmail.com>
 #
-# Use x_includes and x_libraries if they have been set (presumably by
-# AC_PATH_X).
+#   This program is free software; you can redistribute it and/or modify it
+#   under the terms of the GNU General Public License as published by the
+#   Free Software Foundation; either version 2 of the License, or (at your
+#   option) any later version.
 #
-AS_IF([test X$no_x != Xyes -a -n "$x_includes"],
-      [GL_CFLAGS="-I$x_includes $GL_CFLAGS"])
+#   This program is distributed in the hope that it will be useful, but
+#   WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+#   Public License for more details.
+#
+#   You should have received a copy of the GNU General Public License along
+#   with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+#   As a special exception, the respective Autoconf Macro's copyright owner
+#   gives unlimited permission to copy, distribute and modify the configure
+#   scripts that are the output of Autoconf when processing the Macro. You
+#   need not follow the terms of the GNU General Public License when using
+#   or distributing such scripts, even though portions of the text of the
+#   Macro appear in them. The GNU General Public License (GPL) does govern
+#   all other use of the material that constitutes the Autoconf Macro.
+#
+#   This special exception to the GPL applies to versions of the Autoconf
+#   Macro released by the Autoconf Archive. When you make and distribute a
+#   modified version of the Autoconf Macro, you may extend this special
+#   exception to the GPL to apply to your modified version as well.
 
-AC_CHECK_HEADERS([windows.h])
+#serial 17
 
-ax_save_CPPFLAGS=$CPPFLAGS
-CPPFLAGS="$GL_CFLAGS $CPPFLAGS"
-AC_CHECK_HEADERS([GL/gl.h OpenGL/gl.h], , , [
-# if defined(HAVE_WINDOWS_H) && defined(_WIN32)
-#   include <windows.h>
-# endif
-])
-CPPFLAGS=$ax_save_CPPFLAGS
-
-m4_define([AX_CHECK_GL_PROGRAM],
+m4_define([_AX_CHECK_GL_PROGRAM],
           [AC_LANG_PROGRAM([[
 # if defined(HAVE_WINDOWS_H) && defined(_WIN32)
 #   include <windows.h>
@@ -71,78 +83,477 @@ m4_define([AX_CHECK_GL_PROGRAM],
 #   include <OpenGL/gl.h>
 # else
 #   error no gl.h
-# endif]],
-                           [[glBegin(0)]])])
-
-m4_define([AX_CHECK_GL_GLX_PROGRAM],
-          [AC_LANG_PROGRAM([[
-# if defined(HAVE_WINDOWS_H) && defined(_WIN32)
-#   include <windows.h>
 # endif
-# ifdef HAVE_GL_GL_H
-#   include <GL/gl.h>
-# elif defined(HAVE_OPENGL_GL_H)
-#   include <OpenGL/gl.h>
-# else
-#   error no gl.h
-# endif]],
-                           [[glXQueryVersion(0, 0, 0)]])])
-
-AC_CACHE_CHECK([for OpenGL library], [ax_cv_check_gl_libgl],
-[ax_cv_check_gl_libgl=no
-case $host_cpu in
-  x86_64) ax_check_gl_libdir=lib64 ;;
-  *)      ax_check_gl_libdir=lib ;;
-esac
-ax_save_CPPFLAGS=$CPPFLAGS
-CPPFLAGS="$CPPFLAGS $GL_CFLAGS"
-ax_save_LDFLAGS=$LDFLAGS
-AS_IF([test X$no_x != Xyes -a -n "$x_libraries"],
-      [LDFLAGS="$LDFLAGS -L$x_libraries"])
-ax_save_LIBS=$LIBS
-ax_check_libs="-lopengl32 -lGL"
-for ax_lib in $ax_check_libs; do
-  AS_IF([test X$ax_compiler_ms = Xyes],
-        [ax_try_lib=`echo $ax_lib | $SED -e 's/^-l//' -e 's/$/.lib/'`],
-        [ax_try_lib=$ax_lib])
-  LDFLAGS="$ax_save_LDFLAGS $GL_LIBS"
-  LIBS="$ax_try_lib $ax_save_LIBS"
-AC_LINK_IFELSE([AX_CHECK_GL_PROGRAM],
-               [ax_cv_check_gl_libgl=$ax_try_lib; break],
-               [ax_check_gl_dylib_flag='-dylib_file /System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib:/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib'
-                LDFLAGS="$ax_save_LDFLAGS $GL_LIBS $ax_check_gl_dylib_flag"
-                AC_LINK_IFELSE([AX_CHECK_GL_PROGRAM],
-                               [ax_cv_check_gl_libgl="$ax_check_gl_dylib_flag $ax_try_lib"; break])])
-done
-
-#
-# If no_x is "yes", we don't want to wind up using a libGL that is
-# linked with X11.  Test to see if the found libGL includes GLX
-# functions.  If it does and no_x is "yes", we want to reset
-# ax_cv_check_gl_libgl back to "no".
-#
-# Note that LDFLAGS and LIBS should still have whatever values they
-# had when we broke out of the test loop above; use that.
-#
-AS_IF([test "X$ax_cv_check_gl_libgl" != Xno],
-      [AC_LINK_IFELSE([AX_CHECK_GL_GLX_PROGRAM],
-                      [AS_IF([test X$no_x = Xyes],
-                             [ax_cv_check_gl_libgl=no])])])
-
-LIBS=$ax_save_LIBS
-AS_IF([test "X$ax_cv_check_gl_libgl" = Xno],
-      [LDFLAGS="$ax_save_LDFLAGS -framework OpenGL"
-      AC_LINK_IFELSE([AX_CHECK_GL_PROGRAM],
-                     [ax_cv_check_gl_libgl='-framework OpenGL'])])
-
-LDFLAGS=$ax_save_LDFLAGS
-CPPFLAGS=$ax_save_CPPFLAGS])
-
-AS_IF([test "X$ax_cv_check_gl_libgl" = Xno],
-      [no_gl=yes; GL_CFLAGS=""; GL_LIBS=""],
-      [GL_LIBS="$ax_cv_check_gl_libgl $GL_LIBS"])
-AC_LANG_POP([C])
-
-AC_SUBST([GL_CFLAGS])
-AC_SUBST([GL_LIBS])
+]],[[glBegin(0)]])])
+
+dnl Default include : add windows.h
+dnl see http://www.opengl.org/wiki/Platform_specifics:_Windows
+dnl (acceded 20120801)
+AC_DEFUN([_AX_CHECK_GL_INCLUDES_DEFAULT],dnl
+[
+  AC_INCLUDES_DEFAULT
+  [
+  # if defined(HAVE_WINDOWS_H) && defined(_WIN32)
+  #   include <windows.h>
+  # endif
+  ]
+])
+
+dnl local save flags
+AC_DEFUN([_AX_CHECK_GL_SAVE_FLAGS],
+[dnl
+ax_check_gl_saved_libs="${LIBS}"
+ax_check_gl_saved_cflags="${CFLAGS}"
+ax_check_gl_saved_cppflags="${CPPFLAGS}"
+ax_check_gl_saved_ldflags="${LDFLAGS}"
+])
+
+dnl local restore flags
+AC_DEFUN([_AX_CHECK_GL_RESTORE_FLAGS],
+[dnl
+LIBS="${ax_check_gl_saved_libs}"
+CFLAGS="${ax_check_gl_saved_cflags}"
+CPPFLAGS="${ax_check_gl_saved_cppflags}"
+LDFLAGS="${ax_check_gl_saved_ldflags}"
+])
+
+dnl default switch case failure
+AC_DEFUN([_AX_CHECK_MSG_FAILURE_ORDER],
+[dnl
+ AC_MSG_FAILURE([Order logic in ax_check_gl is buggy])
+])
+
+# set the varible ax_check_gl_need_x
+# this variable determine if we need opengl that link with X
+# value are default aka try the first library wether if it link or not with x
+# yes that means we need a opengl with x
+# no that means we do not need an opengl with x
+AC_DEFUN([_AX_CHECK_GL_NEED_X],
+[dnl
+ # do not check if empty : allow a subroutine to modify the choice
+ AS_IF([test "X$ax_check_gl_need_x" = "X"],
+       [ax_check_gl_need_x="default"
+        AS_IF([test "X$no_x" = "Xyes"],[ax_check_gl_need_x="no"])
+        AS_IF([test "X$ax_check_gl_want_gl" = "Xnox"],[ax_check_gl_need_x="no"])
+        AS_IF([test "X$ax_check_gl_want_gl" = "Xx"],[ax_check_gl_need_x="yes"])])
+])
+
+# compile the example program
+AC_DEFUN([_AX_CHECK_GL_COMPILE],
+[dnl
+ AC_LANG_PUSH([C])
+ _AX_CHECK_GL_SAVE_FLAGS()
+ CFLAGS="${GL_CFLAGS} ${CFLAGS}"
+ AC_COMPILE_IFELSE([_AX_CHECK_GL_PROGRAM],
+                   [ax_check_gl_compile_opengl="yes"],
+                   [ax_check_gl_compile_opengl="no"])
+ _AX_CHECK_GL_RESTORE_FLAGS()
+ AC_LANG_POP([C])
+])
+
+# compile the example program (cache)
+AC_DEFUN([_AX_CHECK_GL_COMPILE_CV],
+[dnl
+ AC_CACHE_CHECK([for compiling a minimal OpenGL program],[ax_cv_check_gl_compile_opengl],
+                [_AX_CHECK_GL_COMPILE()
+                 ax_cv_check_gl_compile_opengl="${ax_check_gl_compile_opengl}"])
+ ax_check_gl_compile_opengl="${ax_cv_check_gl_compile_opengl}"
+])
+
+# link the example program
+AC_DEFUN([_AX_CHECK_GL_LINK],
+[dnl
+ AC_LANG_PUSH([C])
+ _AX_CHECK_GL_SAVE_FLAGS()
+ CFLAGS="${GL_CFLAGS} ${CFLAGS}"
+ LIBS="${GL_LIBS} ${LIBS}"
+ LDFLAGS="${GL_LDFLAGS} ${LDFLAGS}"
+ AC_LINK_IFELSE([_AX_CHECK_GL_PROGRAM],
+                [ax_check_gl_link_opengl="yes"],
+                [ax_check_gl_link_opengl="no"])
+ _AX_CHECK_GL_RESTORE_FLAGS()
+ AC_LANG_POP([C])
+])
+
+# link the example program (cache)
+AC_DEFUN([_AX_CHECK_GL_LINK_CV],
+[dnl
+ AC_CACHE_CHECK([for linking a minimal OpenGL program],[ax_cv_check_gl_link_opengl],
+                [_AX_CHECK_GL_LINK()
+                 ax_cv_check_gl_link_opengl="${ax_check_gl_link_opengl}"])
+ ax_check_gl_link_opengl="${ax_cv_check_gl_link_opengl}"
+])
+
+dnl Check headers manually (default case)
+AC_DEFUN([_AX_CHECK_GL_MANUAL_HEADERS_DEFAULT],
+[AC_REQUIRE([AC_PATH_XTRA])
+ AC_LANG_PUSH([C])
+ _AX_CHECK_GL_SAVE_FLAGS()
+ CFLAGS="${GL_CFLAGS} ${CFLAGS}"
+ # see comment in _AX_CHECK_GL_INCLUDES_DEFAULT
+ AC_CHECK_HEADERS([windows.h],[],[],[AC_INCLUDES_DEFAULT])
+ # FIXME: use extra cflags
+ AC_CHECK_HEADERS([GL/gl.h],[ax_check_gl_have_headers="yes"],
+                            [ax_check_gl_have_headers_headers="no"],
+			    [_AX_CHECK_GL_INCLUDES_DEFAULT()])
+ # do not try darwin specific OpenGl/gl.h
+ _AX_CHECK_GL_RESTORE_FLAGS()
+ AC_LANG_POP([C])
+])
+
+# darwin headers without X
+AC_DEFUN([_AX_CHECK_GL_MANUAL_HEADERS_DARWIN_NOX],[
+ AC_LANG_PUSH([C])
+ _AX_CHECK_GL_SAVE_FLAGS()
+ # FIXME: use -framework opengl as an extra cflags
+ CFLAGS="-framework opengl ${GL_CFLAGS} ${CFLAGS}"
+ AC_CHECK_HEADERS([OpenGL/gl.h],[ax_check_gl_have_headers="yes"],
+                                [ax_check_gl_have_headers_headers="no"],
+			        [_AX_CHECK_GL_INCLUDES_DEFAULT()])
+ AS_IF([test "X$ax_check_gl_have_headers" = "yes"],
+       [GL_CFLAGS="-framework opengl ${GL_CFLAGS}"])
+ _AX_CHECK_GL_SAVE_FLAGS()
+ AC_LANG_POP([C])
+])
+
+# check header for darwin
+AC_DEFUN([_AX_CHECK_GL_MANUAL_HEADERS_DARWIN],
+[AC_REQUIRE([_AX_CHECK_GL_NEED_X])dnl
+ AS_CASE(["$ax_check_gl_order"],
+         # try to use framework
+         ["gl"],[_AX_CHECK_GL_MANUAL_HEADERS_DARWIN_NOX()],
+	 # try to use framework then mesa (X)
+	 ["gl mesagl"],[
+	   _AX_CHECK_GL_MANUAL_HEADERS_DARWIN_NOX()
+	   AS_IF([test "X$ax_check_gl_have_headers" = "yes"],
+	         [ax_check_gl_order="gl"
+		  ax_check_gl_need_x="yes"],
+		 [ax_check_gl_order="mesagl gl"
+		  ax_check_gl_need_x="no"]
+		  # retry with general test
+		  _AX_CHECK_GL_MANUAL_HEADERS_DEFAULT()])],
+         ["mesagl gl"],[
+	   _AX_CHECK_GL_MANUAL_HEADERS_DEFAULT()
+	   AS_IF([test "X$ax_check_gl_have_headers" = "yes"],
+	         [ax_check_gl_order="mesagl gl"
+		  ax_check_gl_need_x="no"],
+		 [ax_check_gl_order="gl"
+		  ax_check_gl_need_x="yes"
+		  # retry with framework
+		  _AX_CHECK_GL_MANUAL_HEADERS_DARWIN_NOX()])],
+        [_AX_CHECK_MSG_FAILURE_ORDER()])
+])
+
+dnl Check headers manually: subroutine must set ax_check_gl_have_headers={yes,no}
+AC_DEFUN([_AX_CHECK_GL_MANUAL_HEADERS],
+[AC_REQUIRE([AC_CANONICAL_HOST])
+ AS_CASE([${host}],
+         [*-darwin*],[_AX_CHECK_GL_MANUAL_HEADERS_DARWIN],
+	 [_AX_CHECK_GL_MANUAL_HEADERS_DEFAULT()])
+ AC_CACHE_CHECK([for OpenGL headers],[ax_cv_check_gl_have_headers],
+               	[ax_cv_check_gl_have_headers="${ax_check_gl_have_headers}"])
+])
+
+# dnl try to found library (generic case)
+# dnl $1 is set to the library to found
+AC_DEFUN([_AX_CHECK_GL_MANUAL_LIBS_GENERIC],
+[dnl
+ ax_check_gl_manual_libs_generic_extra_libs="$1"
+ AS_IF([test "X$ax_check_gl_manual_libs_generic_extra_libs" = "X"],
+       [AC_MSG_ERROR([AX_CHECK_GL_MANUAL_LIBS_GENERIC argument must no be empty])])
+
+ AC_LANG_PUSH([C])
+ _AX_CHECK_GL_SAVE_FLAGS()
+ CFLAGS="${GL_CFLAGS} ${CFLAGS}"
+ LIBS="${GL_LIBS} ${LIBS}"
+ AC_SEARCH_LIBS([glBegin],[$ax_check_gl_manual_libs_generic_extra_libs],
+                [ax_check_gl_lib_opengl="yes"],
+                [ax_check_gl_lib_opengl="no"])
+ AS_CASE([$ac_cv_search_glBegin],
+         ["none required"],[],
+ 	 [no],[],
+ 	 [GL_LIBS="${ac_cv_search_glBegin} ${GL_LIBS}"])
+  _AX_CHECK_GL_RESTORE_FLAGS()
+  AC_LANG_PUSH([C])
+])
+
+# dnl try to found lib under darwin
+# darwin opengl hack
+# see http://web.archive.org/web/20090410052741/http://developer.apple.com/qa/qa2007/qa1567.html
+# and http://web.eecs.umich.edu/~sugih/courses/eecs487/glut-howto/
+AC_DEFUN([_AX_CHECK_GL_MANUAL_LIBS_DARWIN],
+[# ldhack list
+ ldhack1 = "-Wl,-framework,OpenGL"
+ ldhack2 = "-Wl,-dylib_file,/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib:/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib"
+ ldhack3 = "$ldhack1,$ldhack2"
+
+ # select hack
+ AS_IF([test "X$ax_check_gl_need_x" = "Xyes"],
+       [# libs already set by -framework cflag
+        darwinlibs=""
+        ldhacks="$ldhack1 $ldhack2 $ldhack3"],
+       [# do not use framework ldflags in case of x version
+        darwinlibs="GL gl MesaGL"
+        ldhack="$ldhack2"])
+
+ ax_check_gl_link_opengl="no"
+ for extralibs in " " $darwinlibs; do
+   for extraldflags in " " ldhacks; do
+       AC_LANG_PUSH([C])
+        _AX_CHECK_GL_SAVE_FLAGS()
+       CFLAGS="${GL_CFLAGS} ${CFLAGS}"
+       LIBS="$extralibs ${GL_LIBS} ${LIBS}"
+       LDFLAGS="$extraldflags ${GL_LDFLAGS} ${LDFLAGS}"
+       AC_LINK_IFELSE([_AX_CHECK_GL_PROGRAM],
+                      [ax_check_gl_link_opengl="yes"],
+                      [ax_check_gl_link_opengl="no"])
+       _AX_CHECK_GL_RESTORE_FLAGS()
+       AC_LANG_POP([C])
+       AS_IF([test "X$ax_check_gl_link_opengl" = "Xyes"],[break])
+   done;
+   AS_IF([test "X$ax_check_gl_link_opengl" = "Xyes"],[break])
+ done;
+ GL_LIBS="$extralibs ${GL_LIBS}"
+ GL_LDFLAGS="$extraldflags ${GL_LDFLAGS}"
+])
+
+dnl Check library manually: subroutine must set
+dnl $ax_check_gl_lib_opengl={yes,no}
+AC_DEFUN([_AX_CHECK_GL_MANUAL_LIBS],
+[AC_REQUIRE([AC_CANONICAL_HOST])
+ AS_CASE([${host}],
+         [*-darwin*],[_AX_CHECK_GL_MANUAL_LIBS_DARWIN()],
+         # try first cygwin version
+         [*-cygwin*|*-mingw*],[
+	   AS_CASE(["$ax_check_gl_order"],
+	           ["gl"],[_AX_CHECK_GL_MANUAL_LIBS_GENERIC([opengl32])],
+		   ["gl mesagl"],[_AX_CHECK_GL_MANUAL_LIBS_GENERIC([opengl32 GL gl MesaGL])],
+		   ["mesagl gl"],[_AX_CHECK_GL_MANUAL_LIBS_GENERIC([GL gl MesaGL opengl32])],
+		   [_AX_CHECK_MSG_FAILURE_ORDER()])],
+	 [AS_CASE(["$ax_check_gl_order"],
+                  ["gl"],[_AX_CHECK_GL_MANUAL_LIBS_GENERIC([GL gl])],
+		  ["gl mesagl"],[_AX_CHECK_GL_MANUAL_LIBS_GENERIC([GL gl MesaGL])],
+		  ["mesagl gl"],[_AX_CHECK_GL_MANUAL_LIBS_GENERIC([MesaGL GL gl])],
+		  [_AX_CHECK_MSG_FAILURE_ORDER()])]
+		  )
+
+ AC_CACHE_CHECK([for OpenGL libraries],[ax_cv_check_gl_lib_opengl],
+               	[ax_cv_check_gl_lib_opengl="${ax_check_gl_lib_opengl}"])
+ ax_check_gl_lib_opengl="${ax_cv_check_gl_lib_opengl}"
+])
+
+# manually check aka old way
+AC_DEFUN([_AX_CHECK_GL_MANUAL],
+[AC_REQUIRE([AC_CANONICAL_HOST])dnl
+ AC_REQUIRE([AC_PATH_XTRA])dnl
+
+ no_gl="yes"
+
+ _AX_CHECK_GL_MANUAL_HEADERS()
+ AS_IF([test "X$ax_check_gl_have_headers" = "Xyes"],
+       [_AX_CHECK_GL_COMPILE_CV()],
+       [ax_check_gl_compile_opengl=no])
+
+ AS_IF([test "X$ax_check_gl_compile_opengl" = "Xyes"],
+       [_AX_CHECK_GL_MANUAL_LIBS],
+       [ax_check_gl_lib_opengl=no])
+
+ AS_IF([test "X$ax_check_gl_lib_opengl" = "Xyes"],
+       [_AX_CHECK_GL_LINK_CV()],
+       [ax_check_gl_link_opengl=no])
+
+ AS_IF([test "X$ax_check_gl_link_opengl" = "Xyes"],
+       [no_gl="no"],
+       [no_gl="yes"])
 ])dnl
+
+
+# try to test using pkgconfig: set ax_check_gl_pkg_config=no if not found
+AC_DEFUN([_AX_CHECK_GL_PKG_CONFIG],dnl
+[dnl
+ AC_REQUIRE([PKG_PROG_PKG_CONFIG])
+
+ dnl First try mesagl
+ AS_CASE(["$ax_check_gl_order"],
+         ["gl"],[PKG_CHECK_MODULES([GL],[mesagl],
+	                  [ax_check_gl_pkg_config=yes],
+			  [ax_check_gl_pkg_config=no])],
+	 ["gl mesagl"],[PKG_CHECK_MODULES([GL],[gl],
+	                  [ax_check_gl_pkg_config=yes],
+			  [PKG_CHECK_MODULES([GL],[mesagl],
+	                         [ax_check_gl_pkg_config=yes],
+				 [ax_check_gl_pkg_config=no])])],
+	 ["mesagl gl"],[PKG_CHECK_MODULES([GL],[mesagl],
+	                  [ax_check_gl_pkg_config=yes],
+			  [PKG_CHECK_MODULES([GL],[gl],
+	                         [ax_check_gl_pkg_config=yes],
+				 [ax_check_gl_pkg_config=no])])],
+	 [_AX_CHECK_MSG_FAILURE_ORDER])
+
+ AS_IF([test "X$ax_check_gl_pkg_config" = "Xyes"],[
+        # check headers
+        AC_LANG_PUSH([C])
+ 	_AX_CHECK_GL_SAVE_FLAGS()
+        CFLAGS="${GL_CFLAGS} ${CFLAGS}"
+        AC_CHECK_HEADERS([windows.h],[],[],[AC_INCLUDES_DEFAULT])
+        AC_CHECK_HEADERS([GL/gl.h OpenGL/gl.h],
+                         [ax_check_gl_have_headers="yes";break],
+                         [ax_check_gl_have_headers_headers="no"],
+			 [_AX_CHECK_GL_INCLUDES_DEFAULT()])
+        _AX_CHECK_GL_RESTORE_FLAGS()
+	AC_LANG_POP([C])
+	AC_CACHE_CHECK([for OpenGL headers],[ax_cv_check_gl_have_headers],
+               	       [ax_cv_check_gl_have_headers="${ax_check_gl_have_headers}"])
+
+        # pkgconfig library are suposed to work ...
+        AS_IF([test "X$ax_cv_check_gl_have_headers" = "Xno"],
+              [AC_MSG_ERROR("Pkgconfig detected OpenGL library has no headers!")])
+
+	_AX_CHECK_GL_COMPILE_CV()
+	AS_IF([test "X$ax_cv_check_gl_compile_opengl" = "Xno"],
+              [AC_MSG_ERROR("Pkgconfig detected opengl library could not be used for compiling minimal program!")])
+
+	_AX_CHECK_GL_LINK_CV()
+	AS_IF([test "X$ax_cv_check_gl_link_opengl" = "Xno"],
+              [AC_MSG_ERROR("Pkgconfig detected opengl library could not be used for linking minimal program!")])
+  ],[ax_check_gl_pkg_config=no])
+])
+
+# test if gl link with X
+AC_DEFUN([_AX_CHECK_GL_WITH_X],
+[
+ # try if opengl need X
+ AC_LANG_PUSH([C])
+ _AX_CHECK_GL_SAVE_FLAGS()
+ CFLAGS="${GL_CFLAGS} ${CFLAGS}"
+ LIBS="${GL_LIBS} ${LIBS}"
+ LDFLAGS="${GL_LDFLAGS} ${LDFLAGS}"
+ AC_LINK_IFELSE([AC_LANG_CALL([], [glXQueryVersion])],
+                [ax_check_gl_link_implicitly_with_x="yes"],
+   	        [ax_check_gl_link_implicitly_with_x="no"])
+ _AX_CHECK_GL_RESTORE_FLAGS()
+ AC_LANG_POP([C])
+])
+
+# internal routine: entry point if gl not disable
+AC_DEFUN([_AX_CHECK_GL],[dnl
+ AC_REQUIRE([PKG_PROG_PKG_CONFIG])
+ AC_REQUIRE([AC_PATH_X])dnl
+
+ # does we need X or not
+ _AX_CHECK_GL_NEED_X()
+
+ # try first pkgconfig
+ AC_MSG_CHECKING([for a working OpenGL implementation by pkg-config])
+ AS_IF([test "X${PKG_CONFIG}" = "X"],
+       [ AC_MSG_RESULT([no])
+         ax_check_gl_pkg_config=no],
+       [ AC_MSG_RESULT([yes])
+         _AX_CHECK_GL_PKG_CONFIG()])
+
+ # if no pkgconfig or pkgconfig fail try manual way
+ AS_IF([test "X$ax_check_gl_pkg_config" = "Xno"],
+       [_AX_CHECK_GL_MANUAL()],
+       [no_gl=no])
+
+ # test if need to test X compatibility
+ AS_IF([test $no_gl = no],
+       [# test X compatibility
+ 	AS_IF([test X$ax_check_gl_need_x != "Xdefault"],
+       	      [AC_CACHE_CHECK([wether OpenGL link implictly with X],[ax_cv_check_gl_link_with_x],
+                              [_AX_CHECK_GL_WITH_X()
+                               ax_cv_check_gl_link_with_x="${ax_check_gl_link_implicitly_with_x}"])
+                               AS_IF([test "X${ax_cv_check_gl_link_with_x}" = "X${ax_check_gl_need_x}"],
+                                     [no_gl="no"],
+                                     [no_gl=yes])])
+	     ])
+])
+
+# ax_check_gl entry point
+AC_DEFUN([AX_CHECK_GL],
+[AC_REQUIRE([AC_PATH_X])dnl
+ AC_REQUIRE([AC_CANONICAL_HOST])
+
+ AC_ARG_WITH([gl],
+  [AS_HELP_STRING([--with-gl@<:@=ARG@:>@],
+    [use opengl (ARG=yes),
+     using the specific lib (ARG=<lib>),
+     using the OpenGL lib that link with X (ARG=x),
+     using the OpenGL lib that link without X (ARG=nox),
+     or disable it (ARG=no)
+     @<:@ARG=yes@:>@ ])],
+    [
+    AS_CASE(["$withval"],
+            ["no"|"NO"],[ax_check_gl_want_gl="no"],
+	    ["yes"|"YES"],[ax_check_gl_want_gl="yes"],
+	    [ax_check_gl_want_gl="$withval"])
+    ],
+    [ax_check_gl_want_gl="yes"])
+
+ dnl compatibility with AX_HAVE_OPENGL
+ AC_ARG_WITH([Mesa],
+    [AS_HELP_STRING([--with-Mesa@<:@=ARG@:>@],
+    [Prefer the Mesa library over a vendors native OpenGL (ARG=yes except on mingw ARG=no),
+     @<:@ARG=yes@:>@ ])],
+    [
+    AS_CASE(["$withval"],
+            ["no"|"NO"],[ax_check_gl_want_mesa="no"],
+	    ["yes"|"YES"],[ax_check_gl_want_mesa="yes"],
+	    [AC_MSG_ERROR([--with-mesa flag is only yes no])])
+    ],
+    [ax_check_gl_want_mesa="default"])
+
+ # check consistency of parameters
+ AS_IF([test "X$have_x" = "Xdisabled"],
+       [AS_IF([test X$ax_check_gl_want_gl = "Xx"],
+              [AC_MSG_ERROR([You prefer OpenGL with X and asked for no X support])])])
+
+ AS_IF([test "X$have_x" = "Xdisabled"],
+       [AS_IF([test X$x_check_gl_want_mesa = "Xyes"],
+              [AC_MSG_WARN([You prefer mesa but you disable X. Disable mesa because mesa need X])
+	       ax_check_gl_want_mesa="no"])])
+
+ # mesa default means yes except on mingw
+ AC_MSG_CHECKING([wether we should prefer mesa for opengl implementation])
+ AS_IF([test X$ax_check_gl_want_mesa = "Xdefault"],
+       [AS_CASE([${host}],
+                [*-mingw*],[ax_check_gl_want_mesa=no],
+		[ax_check_gl_want_mesa=yes])])
+ AC_MSG_RESULT($ax_check_gl_want_mesa)
+
+ # set default guess order
+ AC_MSG_CHECKING([for a working OpenGL order detection])
+ AS_IF([test "X$no_x" = "Xyes"],
+       [ax_check_gl_order="gl"],
+       [AS_IF([test X$ax_check_gl_want_mesa = "Xyes"],
+              [ax_check_gl_order="mesagl gl"],
+	      [ax_check_gl_order="gl mesagl"])])
+ AC_MSG_RESULT($ax_check_gl_order)
+
+ # set flags
+ no_gl="yes"
+ have_GL="no"
+
+ # now do the real testing
+ AS_IF([test X$ax_check_gl_want_gl != "Xno"],
+       [_AX_CHECK_GL()])
+
+ AC_MSG_CHECKING([for a working OpenGL implementation])
+ AS_IF([test "X$no_gl" = "Xno"],
+       [have_GL="yes"
+        AC_MSG_RESULT([yes])
+        AC_MSG_CHECKING([for CFLAGS needed for OpenGL])
+        AC_MSG_RESULT(["${GL_CFLAGS}"])
+        AC_MSG_CHECKING([for LIBS needed for OpenGL])
+        AC_MSG_RESULT(["${GL_LIBS}"])
+        AC_MSG_CHECKING([for LDFLAGS needed for OpenGL])
+        AC_MSG_RESULT(["${GL_LDFLAGS}"])],
+       [AC_MSG_RESULT([no])
+        GL_CFLAGS=""
+        GL_LIBS=""
+        GL_LDFLAGS=""])
+
+ AC_SUBST([GL_CFLAGS])
+ AC_SUBST([GL_LIBS])
+ AC_SUBST([GL_LDFLAGS])
+])
diff --git a/m4macros/ax_pthread.m4 b/m4macros/ax_pthread.m4
index e20a388..d383ad5 100644
--- a/m4macros/ax_pthread.m4
+++ b/m4macros/ax_pthread.m4
@@ -82,7 +82,7 @@
 #   modified version of the Autoconf Macro, you may extend this special
 #   exception to the GPL to apply to your modified version as well.
 
-#serial 17
+#serial 21
 
 AU_ALIAS([ACX_PTHREAD], [AX_PTHREAD])
 AC_DEFUN([AX_PTHREAD], [
@@ -103,8 +103,8 @@ if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
         save_LIBS="$LIBS"
         LIBS="$PTHREAD_LIBS $LIBS"
         AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
-        AC_TRY_LINK_FUNC(pthread_join, ax_pthread_ok=yes)
-        AC_MSG_RESULT($ax_pthread_ok)
+        AC_TRY_LINK_FUNC([pthread_join], [ax_pthread_ok=yes])
+        AC_MSG_RESULT([$ax_pthread_ok])
         if test x"$ax_pthread_ok" = xno; then
                 PTHREAD_LIBS=""
                 PTHREAD_CFLAGS=""
@@ -145,8 +145,8 @@ ax_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mt
 # --thread-safe: KAI C++
 # pthread-config: use pthread-config program (for GNU Pth library)
 
-case "${host_cpu}-${host_os}" in
-        *solaris*)
+case ${host_os} in
+        solaris*)
 
         # On Solaris (at least, for some versions), libc contains stubbed
         # (non-functional) versions of the pthreads routines, so link-based
@@ -159,11 +159,25 @@ case "${host_cpu}-${host_os}" in
         ax_pthread_flags="-pthreads pthread -mt -pthread $ax_pthread_flags"
         ;;
 
-        *-darwin*)
+        darwin*)
         ax_pthread_flags="-pthread $ax_pthread_flags"
         ;;
 esac
 
+# Clang doesn't consider unrecognized options an error unless we specify
+# -Werror. We throw in some extra Clang-specific options to ensure that
+# this doesn't happen for GCC, which also accepts -Werror.
+
+AC_MSG_CHECKING([if compiler needs -Werror to reject unknown flags])
+save_CFLAGS="$CFLAGS"
+ax_pthread_extra_flags="-Werror"
+CFLAGS="$CFLAGS $ax_pthread_extra_flags -Wunknown-warning-option -Wsizeof-array-argument"
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([int foo(void);],[foo()])],
+                  [AC_MSG_RESULT([yes])],
+                  [ax_pthread_extra_flags=
+                   AC_MSG_RESULT([no])])
+CFLAGS="$save_CFLAGS"
+
 if test x"$ax_pthread_ok" = xno; then
 for flag in $ax_pthread_flags; do
 
@@ -178,7 +192,7 @@ for flag in $ax_pthread_flags; do
                 ;;
 
                 pthread-config)
-                AC_CHECK_PROG(ax_pthread_config, pthread-config, yes, no)
+                AC_CHECK_PROG([ax_pthread_config], [pthread-config], [yes], [no])
                 if test x"$ax_pthread_config" = xno; then continue; fi
                 PTHREAD_CFLAGS="`pthread-config --cflags`"
                 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
@@ -193,7 +207,7 @@ for flag in $ax_pthread_flags; do
         save_LIBS="$LIBS"
         save_CFLAGS="$CFLAGS"
         LIBS="$PTHREAD_LIBS $LIBS"
-        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
+        CFLAGS="$CFLAGS $PTHREAD_CFLAGS $ax_pthread_extra_flags"
 
         # Check for various functions.  We must include pthread.h,
         # since some functions may be macros.  (On the Sequent, we
@@ -219,7 +233,7 @@ for flag in $ax_pthread_flags; do
         LIBS="$save_LIBS"
         CFLAGS="$save_CFLAGS"
 
-        AC_MSG_RESULT($ax_pthread_ok)
+        AC_MSG_RESULT([$ax_pthread_ok])
         if test "x$ax_pthread_ok" = xyes; then
                 break;
         fi
@@ -245,61 +259,70 @@ if test "x$ax_pthread_ok" = xyes; then
                 [attr_name=$attr; break],
                 [])
         done
-        AC_MSG_RESULT($attr_name)
+        AC_MSG_RESULT([$attr_name])
         if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
-            AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
+            AC_DEFINE_UNQUOTED([PTHREAD_CREATE_JOINABLE], [$attr_name],
                                [Define to necessary symbol if this constant
                                 uses a non-standard name on your system.])
         fi
 
         AC_MSG_CHECKING([if more special flags are required for pthreads])
         flag=no
-        case "${host_cpu}-${host_os}" in
-            *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
-            *-osf* | *-hpux*) flag="-D_REENTRANT";;
-            *solaris*)
+        case ${host_os} in
+            aix* | freebsd* | darwin*) flag="-D_THREAD_SAFE";;
+            osf* | hpux*) flag="-D_REENTRANT";;
+            solaris*)
             if test "$GCC" = "yes"; then
                 flag="-D_REENTRANT"
             else
+                # TODO: What about Clang on Solaris?
                 flag="-mt -D_REENTRANT"
             fi
             ;;
         esac
-        AC_MSG_RESULT(${flag})
+        AC_MSG_RESULT([$flag])
         if test "x$flag" != xno; then
             PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
         fi
 
         AC_CACHE_CHECK([for PTHREAD_PRIO_INHERIT],
-            ax_cv_PTHREAD_PRIO_INHERIT, [
-                AC_LINK_IFELSE([
-                    AC_LANG_PROGRAM([[#include <pthread.h>]], [[int i = PTHREAD_PRIO_INHERIT;]])],
+            [ax_cv_PTHREAD_PRIO_INHERIT], [
+                AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <pthread.h>]],
+                                                [[int i = PTHREAD_PRIO_INHERIT;]])],
                     [ax_cv_PTHREAD_PRIO_INHERIT=yes],
                     [ax_cv_PTHREAD_PRIO_INHERIT=no])
             ])
         AS_IF([test "x$ax_cv_PTHREAD_PRIO_INHERIT" = "xyes"],
-            AC_DEFINE([HAVE_PTHREAD_PRIO_INHERIT], 1, [Have PTHREAD_PRIO_INHERIT.]))
+            [AC_DEFINE([HAVE_PTHREAD_PRIO_INHERIT], [1], [Have PTHREAD_PRIO_INHERIT.])])
 
         LIBS="$save_LIBS"
         CFLAGS="$save_CFLAGS"
 
-        # More AIX lossage: must compile with xlc_r or cc_r
-        if test x"$GCC" != xyes; then
-          AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC})
-        else
-          PTHREAD_CC=$CC
+        # More AIX lossage: compile with *_r variant
+        if test "x$GCC" != xyes; then
+            case $host_os in
+                aix*)
+                AS_CASE(["x/$CC"],
+                  [x*/c89|x*/c89_128|x*/c99|x*/c99_128|x*/cc|x*/cc128|x*/xlc|x*/xlc_v6|x*/xlc128|x*/xlc128_v6],
+                  [#handle absolute path differently from PATH based program lookup
+                   AS_CASE(["x$CC"],
+                     [x/*],
+                     [AS_IF([AS_EXECUTABLE_P([${CC}_r])],[PTHREAD_CC="${CC}_r"])],
+                     [AC_CHECK_PROGS([PTHREAD_CC],[${CC}_r],[$CC])])])
+                ;;
+            esac
         fi
-else
-        PTHREAD_CC="$CC"
 fi
 
-AC_SUBST(PTHREAD_LIBS)
-AC_SUBST(PTHREAD_CFLAGS)
-AC_SUBST(PTHREAD_CC)
+test -n "$PTHREAD_CC" || PTHREAD_CC="$CC"
+
+AC_SUBST([PTHREAD_LIBS])
+AC_SUBST([PTHREAD_CFLAGS])
+AC_SUBST([PTHREAD_CC])
 
 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
 if test x"$ax_pthread_ok" = xyes; then
-        ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
+        ifelse([$1],,[AC_DEFINE([HAVE_PTHREAD],[1],[Define if you have POSIX threads libraries and header files.])],[$1])
         :
 else
         ax_pthread_ok=no
