New way of checking for various LIBUSB names; for legacy version, make sure the symbo...
authorMichael Dickens <mdickens@nd.edu>
Wed, 8 Sep 2010 01:56:23 +0000 (21:56 -0400)
committerJohnathan Corgan <jcorgan@corganenterprises.com>
Wed, 6 Oct 2010 19:03:33 +0000 (12:03 -0700)
config/usrp_libusb.m4 [changed mode: 0755->0644]

old mode 100755 (executable)
new mode 100644 (file)
index 251f7df..a0d3425
@@ -28,64 +28,160 @@ AC_DEFUN([USRP_LIBUSB], [
   dnl make sure the USB_* variables work (whether from PKGCONFIG
   dnl or overridden by the user)
 
-  libusbok=no
-  have_libusb1=no
-  LIBUSB_PKG_CONFIG_NAME=''
+  dnl do not use LDFLAGS, since PKGCONFIG will provide everything
+  saved_LDFLAGS=${LDFLAGS}
+  LDFLAGS=
+  LIBUSB_PKG_CONFIG_NAME=
+
+  dnl loop over various possible 'libusb' PKGCONFIG names, and choose
+  dnl the first one that meets both the user's selection (via
+  dnl configure flags) as well as what is installed
+
+  dnl create the list of libusb PKGCONFIG modules to test
+  libusb_list=''
   if test x$1 = xyes; then
-    PKG_CHECK_MODULES(USB, libusb-1.0, [
-      libusbok=yes
-      have_libusb1=yes
-      usb_header='libusb-1.0/libusb.h'
-      usb_lib_func='libusb_bulk_transfer'
-      usb_lib_name='usb-1.0'
-      LIBUSB_PKG_CONFIG_NAME='libusb-1.0'
-    ])
+    dnl libusb-1.0 was requested; just test for it
+    libusb_list="libusb-1.0"
   else
-    dnl not using libusb1 (for now); see if legacy version is found.
-    dnl it might be installed under the name either 'libusb' or
-    dnl 'libusb-legacy', or just available via the
-    dnl user's shell environment
-
-    dnl see if the pkgconfig module 'libusb' is available
-    PKG_CHECK_MODULES(USB, libusb, [
-      libusbok=yes
-      LIBUSB_PKG_CONFIG_NAME='libusb'
+    dnl test for legacy libusb only
+    libusb_list="libusb libusb-legacy"
+  fi
+  for libusb_name in ${libusb_list}; do
+    dnl clear internal variables
+    libusbok=no
+    have_libusb1=no
+    usb_header=''
+    usb_lib_func=''
+    usb_lib_name=''
+
+    dnl start checks
+    AC_MSG_NOTICE([Checking for LIBUSB version '${libusb_name}'])
+    if test ${libusb_name} = "libusb-1.0"; then
+      dnl see if the pkgconfig module is available
+      PKG_CHECK_MODULES(USB, ${libusb_name}, [
+        libusbok=yes
+        have_libusb1=yes
+        usb_header='libusb-1.0/libusb.h'
+        usb_lib_func='libusb_bulk_transfer'
       ], [libusbok=no])
-    dnl PKG_CHECK_MODULES does not work correctly when embedded
-    if test $libusbok = no; then
-      dnl if not, see if the pkgconfig module 'libusb-legacy' is available
-      PKG_CHECK_MODULES(USB, [libusb-legacy], [
+    else
+      dnl see if the pkgconfig module is available
+      PKG_CHECK_MODULES(USB, ${libusb_name}, [
         libusbok=yes
-        LIBUSB_PKG_CONFIG_NAME='libusb-legacy'
-        ], [libusbok=no])
+        usb_header='usb.h'
+        usb_lib_func='usb_bulk_write'
+      ], [libusbok=no])
     fi
-    dnl set variables for further testing
-    usb_header='usb.h'
-    usb_lib_func='usb_bulk_write'
-    usb_lib_name='usb'
-  fi
-  AC_SUBST(LIBUSB_PKG_CONFIG_NAME)
-  if test x$1 != xyes || test $have_libusb1 = yes; then
-    dnl Either (1) libusb1 was specified and found; or
-    dnl (2) libusb1 was not specified. Restart checking.
-    libusbok=yes
+    if test $libusbok = yes; then
+      dnl PKGCONFIG found a version of LIBUSB.
+      dnl Check it to make sure it meets enough criteria:
+      dnl Verify that $usb_header is a valid header. If so, then
+      dnl verify that $usb_lib_func can be found in the library
+      dnl $usb_lib_name.  if so, verify that the symbol 'usb_debug' is
+      dnl found in the library.
+
+      dnl Check for the header.  Similar to AC_CHECK_HEADERS,
+      dnl but doesn't append to known \#defines.
+      dnl If PKGCONFIG found variable USB_INCLUDEDIR, and it is
+      dnl not empty, use it for checking for $usb_header.
+      dnl Otherwise, maybe the user's shell environment is already
+      dnl configured to find this header.
+      AC_LANG_PUSH(C)
+      save_CPPFLAGS="$CPPFLAGS"
+      if test x$USB_INCLUDEDIR != x; then
+        USB_INCLUDES="-I$USB_INCLUDEDIR"
+        CPPFLAGS="$USB_INCLUDES"
+      fi
+      AC_MSG_CHECKING([$libusb_name for header $usb_header])
+      AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
+        #include "$usb_header"
+        ]], [])],
+        [libusbok=yes],[libusbok=no])
+      AC_MSG_RESULT([$libusbok])
+      CPPFLAGS="$save_CPPFLAGS"
+      AC_LANG_POP(C)
+
+      if test $libusbok = yes; then
+        dnl found the header; now make sure the library is OK
+        dnl On Darwin, need to include the IOKit library.     
 
-    dnl Verify that $usb_header is a valid header, and if so,
-    dnl then verify that $usb_lib_func can be found in the
-    dnl library $usb_lib_name.
+        AC_LANG_PUSH(C)
+        save_LIBS="$LIBS"
+        LIBS=""
+        case "$host_os" in
+          darwin*)
+            USB_LIBS="$USB_LIBS -lIOKit"
+            LIBS="$USB_LIBS"
+            ;;
+          *) ;;
+        esac
 
-    dnl If PKGCONFIG found variable USB_INCLUDEDIR, and it is
-    dnl not empty, use it for checking for $usb_header.
-    dnl Otherwise, maybe the user's shell environment is already
-    dnl configured to find this header.
+       dnl find the library link name
+        usb_lib_name=`echo $USB_LIBS | sed -e "s@.*-l\(usb[[^ ]]*\).*@\1@"`
 
+        dnl Check for the function in the library.  Similar to
+        dnl AC_CHECK_LIB, but doesn't append to known \#defines.
+        AC_MSG_CHECKING([$libusb_name for function $usb_lib_func in library $usb_lib_name])
+        AC_LINK_IFELSE([AC_LANG_PROGRAM([[
+          #ifdef __cplusplus
+          extern "C"
+          #endif
+          char $usb_lib_func ();
+          ]], [[return $usb_lib_func ();]])],
+          [libusbok=yes],[libusbok=no])
+        AC_MSG_RESULT([$libusbok])
+        case "$host_os" in
+          cygwin* | mingw*)
+            USB_LIBS="$LIBS"
+            ;;
+          *) ;;
+        esac
+        LIBS="$save_LIBS"
+        AC_LANG_POP(C)
+
+        if test $libusbok = yes; then
+          if test ${libusb_name} != "libusb-1.0"; then
+            dnl PKGCONFIG found a legacy version of libusb; make sure the
+            dnl variable _usb_debug is available in the found library
+            AC_LANG_PUSH(C)
+            save_CPPFLAGS="$CPPFLAGS"
+            CPPFLAGS="$USB_INCLUDES"
+            save_LIBS="$LIBS"
+            LIBS="$USB_LIBS"
+            AC_MSG_CHECKING([$libusb_name for symbol usb_debug in library $usb_lib_name])
+            AC_LINK_IFELSE([AC_LANG_PROGRAM([[
+              extern int usb_debug;]],
+              [[usb_debug = 0;]])],
+              [libusbok=yes],[libusbok=no])
+            AC_MSG_RESULT([$libusbok])
+            LIBS="$save_LIBS"
+            CPPFLAGS="$save_CPPFLAGS"
+            AC_LANG_POP(C)
+          fi
+        fi
+      fi
+    fi
+    dnl if everything checks out OK, finish up
+    if test $libusbok = yes; then
+      LIBUSB_PKG_CONFIG_NAME="${libusb_name}"
+      break
+    else
+      dnl something wasn't found in this LIBUSB version.
+      dnl HACK: clear cache variables for header and library
+      unset USB_INCLUDEDIR
+      unset USB_INCLUDES
+      unset USB_LIBS
+      unset $as_ac_Header
+      unset $as_ac_Lib
+    fi
+  done
+
+  if test $libusbok = yes; then
+    dnl final error checking, mostly to create #define's
     AC_LANG_PUSH(C)
     save_CPPFLAGS="$CPPFLAGS"
-    if test x$USB_INCLUDEDIR != x; then
-      USB_INCLUDES="-I$USB_INCLUDEDIR"
-      CPPFLAGS="$CPPFLAGS $USB_INCLUDES"
-      AC_SUBST(USB_INCLUDES)
-    fi
+    CPPFLAGS="$USB_INCLUDES"
+    dnl Check for the header.
     AC_CHECK_HEADERS([$usb_header], [], [libusbok=no])
     CPPFLAGS="$save_CPPFLAGS"
     AC_LANG_POP(C)
@@ -93,40 +189,38 @@ AC_DEFUN([USRP_LIBUSB], [
     if test $libusbok = no; then
       AC_MSG_RESULT([USRP requires libusb header '$usb_header' which was not found or was not usable. See http://www.libusb.org])
     else
-
-      dnl found the header; now make sure the library is OK
-      dnl On Darwin, need to include the IOKit library.     
-
+      dnl check for the library (again)
       AC_LANG_PUSH(C)
+      save_CPPFLAGS="$CPPFLAGS"
+      CPPFLAGS="$USB_INCLUDES"
       save_LIBS="$LIBS"
-      LIBS=""
-      case "$host_os" in
-        darwin*)
-          USB_LIBS="$USB_LIBS -lIOKit"
-          LIBS="$USB_LIBS"
-          ;;
-        *) ;;
-      esac
+      LIBS="$USB_LIBS"
       AC_CHECK_LIB([$usb_lib_name], [$usb_lib_func], [], [
         libusbok=no
-        AC_MSG_RESULT([USRP requires library '$usb_lib_name' with function '$usb_lib_func', which was either not found or was not usable. See http://www.libusb.org])
-      ])
-      case "$host_os" in
-        cygwin* | mingw*)
-          USB_LIBS="$LIBS"
-          ;;
-        *) ;;
-      esac
+        AC_MSG_RESULT([USRP requires library '$usb_lib_name' with function '$usb_lib_func', which was either not found or was not usable. See http://www.libusb.org])])
       LIBS="$save_LIBS"
+      CPPFLAGS="$save_CPPFLAGS"
       AC_LANG_POP(C)
     fi
   fi
+
   if test $libusbok = yes; then
-    AC_SUBST(USB_LIBS)
+    dnl success
+    AC_MSG_NOTICE([Using LIBUSB version '${libusb_name}'])
     ifelse([$2], , :, [$2])
   else
+    dnl not found; clear substitution variables
+    LIBUSB_PKG_CONFIG_NAME=
     USB_INCLUDES=
     USB_LIBS=
     ifelse([$3], , :, [$3])
   fi
+
+  dnl create substitution variables
+  AC_SUBST(USB_INCLUDES)
+  AC_SUBST(USB_LIBS)
+  AC_SUBST(LIBUSB_PKG_CONFIG_NAME)
+
+  dnl restore LDFLAGS
+  LDFLAGS=${saved_LDFLAGS}
 ])