e4f567ce4d1c99406f12bd8507c3b5951b55cfff
[debian/gnuradio] / config / usrp_libusb.m4
1 dnl Copyright 2003,2008,2009 Free Software Foundation, Inc.
2 dnl 
3 dnl This file is part of GNU Radio
4 dnl 
5 dnl GNU Radio is free software; you can redistribute it and/or modify
6 dnl it under the terms of the GNU General Public License as published by
7 dnl the Free Software Foundation; either version 3, or (at your option)
8 dnl any later version.
9 dnl 
10 dnl GNU Radio is distributed in the hope that it will be useful,
11 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
12 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 dnl GNU General Public License for more details.
14 dnl 
15 dnl You should have received a copy of the GNU General Public License
16 dnl along with GNU Radio; see the file COPYING.  If not, write to
17 dnl the Free Software Foundation, Inc., 51 Franklin Street,
18 dnl Boston, MA 02110-1301, USA.
19
20 # $1 is $req_libusb1:
21 #   yes : check libusb-1.0
22 #   no  : check libusb-0.12
23 #   ""  : check libusb-0.12
24
25
26 AC_DEFUN([USRP_LIBUSB], [
27   dnl Use PKGCONFIG to check for packages first, then check to
28   dnl make sure the USB_* variables work (whether from PKGCONFIG
29   dnl or overridden by the user)
30
31   dnl do not use LDFLAGS, since PKGCONFIG will provide everything
32   saved_LDFLAGS=${LDFLAGS}
33   LDFLAGS=
34   LIBUSB_PKG_CONFIG_NAME=
35
36   dnl loop over various possible 'libusb' PKGCONFIG names, and choose
37   dnl the first one that meets both the user's selection (via
38   dnl configure flags) as well as what is installed
39
40   dnl create the list of libusb PKGCONFIG modules to test
41   libusb_list=''
42   if test x$1 = xyes; then
43     dnl libusb-1.0 was requested; just test for it
44     libusb_list="libusb-1.0"
45   else
46     dnl test for legacy libusb only
47     libusb_list="libusb libusb-legacy"
48   fi
49   for libusb_name in ${libusb_list}; do
50     dnl clear internal variables
51     libusbok=no
52     have_libusb1=no
53     usb_header=''
54     usb_lib_func=''
55     usb_lib_name=''
56
57     dnl start checks
58     AC_MSG_NOTICE([Checking for LIBUSB version '${libusb_name}'])
59     if test ${libusb_name} = "libusb-1.0"; then
60       dnl see if the pkgconfig module is available
61       PKG_CHECK_MODULES(USB, ${libusb_name}, [
62         libusbok=yes
63         have_libusb1=yes
64         usb_header='libusb-1.0/libusb.h'
65         usb_lib_func='libusb_bulk_transfer'
66       ], [libusbok=no])
67     else
68       dnl see if the pkgconfig module is available
69       PKG_CHECK_MODULES(USB, ${libusb_name}, [
70         libusbok=yes
71         usb_header='usb.h'
72         usb_lib_func='usb_bulk_write'
73       ], [libusbok=no])
74     fi
75     if test $libusbok = yes; then
76       dnl PKGCONFIG found a version of LIBUSB.
77       dnl Check it to make sure it meets enough criteria:
78       dnl Verify that $usb_header is a valid header. If so, then
79       dnl verify that $usb_lib_func can be found in the library
80       dnl $usb_lib_name.  if so, verify that the symbol 'usb_debug' is
81       dnl found in the library.
82
83       dnl Check for the header.  Similar to AC_CHECK_HEADERS,
84       dnl but doesn't append to known \#defines.
85       dnl If PKGCONFIG found variable USB_INCLUDEDIR, and it is
86       dnl not empty, use it for checking for $usb_header.
87       dnl Otherwise, maybe the user's shell environment is already
88       dnl configured to find this header.
89       AC_LANG_PUSH(C)
90       save_CPPFLAGS="$CPPFLAGS"
91       if test x$USB_INCLUDEDIR != x; then
92         USB_INCLUDES="-I$USB_INCLUDEDIR"
93         CPPFLAGS="$USB_INCLUDES"
94       fi
95       AC_MSG_CHECKING([$libusb_name for header $usb_header])
96       AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
97         #include "$usb_header"
98         ]], [])],
99         [libusbok=yes],[libusbok=no])
100       AC_MSG_RESULT([$libusbok])
101       CPPFLAGS="$save_CPPFLAGS"
102       AC_LANG_POP(C)
103
104       if test $libusbok = yes; then
105         dnl found the header; now make sure the library is OK
106         dnl On Darwin, need to include the IOKit library.     
107
108         AC_LANG_PUSH(C)
109         save_LIBS="$LIBS"
110         case "$host_os" in
111           darwin*)
112             USB_LIBS="$USB_LIBS -lIOKit"
113             ;;
114           *) ;;
115         esac
116         LIBS="$USB_LIBS"
117
118         dnl find the library link name
119         usb_lib_name=`echo $USB_LIBS | sed -e "s@.*-l\(usb[[^ ]]*\).*@\1@"`
120
121         dnl Check for the function in the library.  Similar to
122         dnl AC_CHECK_LIB, but doesn't append to known \#defines.
123         AC_MSG_CHECKING([$libusb_name for function $usb_lib_func in library $usb_lib_name])
124         AC_LINK_IFELSE([AC_LANG_PROGRAM([[
125           #ifdef __cplusplus
126           extern "C"
127           #endif
128           char $usb_lib_func ();
129           ]], [[return $usb_lib_func ();]])],
130           [libusbok=yes],[libusbok=no])
131         AC_MSG_RESULT([$libusbok])
132         LIBS="$save_LIBS"
133         AC_LANG_POP(C)
134
135         if test $libusbok = yes; then
136           if test ${libusb_name} != "libusb-1.0"; then
137             dnl PKGCONFIG found a legacy version of libusb; make sure the
138             dnl variable _usb_debug is available in the found library.
139             dnl Do not test on Windows, since that symbol is not defined.
140             case "$host_os" in
141               cygwin* | mingw*)
142               ;;
143             *)
144               AC_LANG_PUSH(C)
145               save_CPPFLAGS="$CPPFLAGS"
146               CPPFLAGS="$USB_INCLUDES"
147               save_LIBS="$LIBS"
148               LIBS="$USB_LIBS"
149               AC_MSG_CHECKING([$libusb_name for symbol usb_debug in library $usb_lib_name])
150               AC_LINK_IFELSE([AC_LANG_PROGRAM([[
151                 extern int usb_debug;]],
152                 [[usb_debug = 0;]])],
153                 [libusbok=yes],[libusbok=no])
154               AC_MSG_RESULT([$libusbok])
155               LIBS="$save_LIBS"
156               CPPFLAGS="$save_CPPFLAGS"
157               AC_LANG_POP(C)
158               ;;
159             esac
160           fi
161         fi
162       fi
163     fi
164     dnl if everything checks out OK, finish up
165     if test $libusbok = yes; then
166       LIBUSB_PKG_CONFIG_NAME="${libusb_name}"
167       break
168     else
169       dnl something wasn't found in this LIBUSB version.
170       dnl HACK: clear cache variables for header and library
171       unset USB_INCLUDEDIR
172       unset USB_INCLUDES
173       unset USB_LIBS
174       unset $as_ac_Header
175       unset $as_ac_Lib
176     fi
177   done
178
179   if test $libusbok = yes; then
180     dnl final error checking, mostly to create #define's
181     AC_LANG_PUSH(C)
182     save_CPPFLAGS="$CPPFLAGS"
183     CPPFLAGS="$USB_INCLUDES"
184     dnl Check for the header.
185     AC_CHECK_HEADERS([$usb_header], [], [libusbok=no])
186     CPPFLAGS="$save_CPPFLAGS"
187     AC_LANG_POP(C)
188
189     if test $libusbok = no; then
190       AC_MSG_RESULT([USRP requires libusb header '$usb_header' which was not found or was not usable. See http://www.libusb.org])
191     else
192       dnl check for the library (again)
193       AC_LANG_PUSH(C)
194       save_CPPFLAGS="$CPPFLAGS"
195       CPPFLAGS="$USB_INCLUDES"
196       save_LIBS="$LIBS"
197       LIBS="$USB_LIBS"
198       AC_CHECK_LIB([$usb_lib_name], [$usb_lib_func], [], [
199         libusbok=no
200         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])])
201       case "$host_os" in
202         cygwin* | mingw*)
203           USB_LIBS="$LIBS"
204           ;;
205         *) ;;
206       esac
207       LIBS="$save_LIBS"
208       CPPFLAGS="$save_CPPFLAGS"
209       AC_LANG_POP(C)
210     fi
211   fi
212
213   if test $libusbok = yes; then
214     dnl success
215     AC_MSG_NOTICE([Using LIBUSB version '${libusb_name}'])
216     ifelse([$2], , :, [$2])
217   else
218     dnl not found; clear substitution variables
219     LIBUSB_PKG_CONFIG_NAME=
220     USB_INCLUDES=
221     USB_LIBS=
222     ifelse([$3], , :, [$3])
223   fi
224
225   dnl create substitution variables
226   AC_SUBST(USB_INCLUDES)
227   AC_SUBST(USB_LIBS)
228   AC_SUBST(LIBUSB_PKG_CONFIG_NAME)
229
230   dnl restore LDFLAGS
231   LDFLAGS=${saved_LDFLAGS}
232 ])