first shot at Windows-compatible LIBUSB check
[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 do not use LDFLAGS, except on Windows
28   saved_LDFLAGS=${LDFLAGS}
29   case "$host_os" in
30     cygwin* | mingw*)
31     ;;
32     *)
33       LDFLAGS=
34     ;;
35   esac
36
37   dnl this variable is set in usrp/usrp.pc.in as a requirement
38   dnl for libusrp; it is OK to be empty.
39   LIBUSB_PKG_CONFIG_NAME=
40
41   dnl for Windows (cygin, mingw), do not use PKGCONFIG since LIBUSB
42   dnl does not install a .pc file.  For all other OSs, use
43   dnl PKGCONFIG to check for various package names first.
44   libusb_list=''
45   case "$host_os" in
46     cygwin* | mingw*)
47       libusb_list='libusb'
48     ;;
49     *)
50       dnl create the list of libusb PKGCONFIG modules to test
51       if test x$1 = xyes; then
52         dnl libusb-1.0 was requested; just test for it
53         libusb_list="libusb-1.0"
54       else
55         dnl test for legacy libusb only
56         libusb_list="libusb libusb-legacy"
57       fi
58     ;;
59   esac
60
61   dnl loop over various possible 'libusb' names, and
62   dnl choose the first one that meets both the user's selection
63   dnl (via configure flags) as well as what is installed
64   for libusb_name in ${libusb_list}; do
65     dnl clear internal variables
66     libusbok=no
67     have_libusb1=no
68     usb_header=''
69     usb_lib_func=''
70     usb_lib_name=''
71
72     case "$host_os" in
73       cygwin* | mingw*)
74         USB_INCLUDEDIR=
75         USB_INCLUDES=
76         USB_LIBS=-lusb
77         usb_header='usb.h'
78         usb_lib_func='usb_bulk_write'
79         libusbok=yes
80       ;;
81       *)
82         dnl start checks
83         AC_MSG_NOTICE([Checking for LIBUSB version '${libusb_name}'])
84         if test ${libusb_name} = "libusb-1.0"; then
85           dnl see if the pkgconfig module is available
86           PKG_CHECK_MODULES(USB, ${libusb_name}, [
87             libusbok=yes
88             have_libusb1=yes
89             usb_header='libusb-1.0/libusb.h'
90             usb_lib_func='libusb_bulk_transfer'
91           ], [libusbok=no])
92         else
93           dnl see if the pkgconfig module is available
94           PKG_CHECK_MODULES(USB, ${libusb_name}, [
95             libusbok=yes
96             usb_header='usb.h'
97             usb_lib_func='usb_bulk_write'
98           ], [libusbok=no])
99         fi
100       ;;
101     esac
102     if test $libusbok = yes; then
103       dnl PKGCONFIG found a version of LIBUSB, or the info was
104       dnl provided by the user, or the OS is Windows.
105
106       dnl Check it to make sure it meets enough criteria:
107       dnl Verify that $usb_header is a valid header. If so, then
108       dnl verify that $usb_lib_func can be found in the library
109       dnl $usb_lib_name.  if so, verify that the symbol 'usb_debug' is
110       dnl found in the library if not using Windows.
111
112       dnl Check for the header.  Similar to AC_CHECK_HEADERS,
113       dnl but doesn't append to known \#defines.
114       dnl If PKGCONFIG found variable USB_INCLUDEDIR, and it is
115       dnl not empty, use it for checking for $usb_header.
116       dnl Otherwise, maybe the user's shell environment is already
117       dnl configured to find this header.
118       AC_LANG_PUSH(C)
119       save_CPPFLAGS="$CPPFLAGS"
120       if test x$USB_INCLUDEDIR != x; then
121         USB_INCLUDES="-I$USB_INCLUDEDIR"
122         CPPFLAGS="$USB_INCLUDES"
123       fi
124       AC_MSG_CHECKING([$libusb_name for header $usb_header])
125       AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
126         #include "$usb_header"
127         ]], [])],
128         [libusbok=yes],[libusbok=no])
129       AC_MSG_RESULT([$libusbok])
130       CPPFLAGS="$save_CPPFLAGS"
131       AC_LANG_POP(C)
132
133       if test $libusbok = yes; then
134         dnl found the header; now make sure the library is OK
135         dnl On Darwin, need to include the IOKit library.     
136
137         AC_LANG_PUSH(C)
138         save_LIBS="$LIBS"
139         case "$host_os" in
140           darwin*)
141             USB_LIBS="$USB_LIBS -lIOKit"
142             ;;
143           *) ;;
144         esac
145         LIBS="$USB_LIBS"
146
147         dnl find the library link name
148         usb_lib_name=`echo $USB_LIBS | sed -e "s@.*-l\(usb[[^ ]]*\).*@\1@"`
149
150         dnl Check for the function in the library.  Similar to
151         dnl AC_CHECK_LIB, but doesn't append to known \#defines.
152         AC_MSG_CHECKING([$libusb_name for function $usb_lib_func in library $usb_lib_name])
153         AC_LINK_IFELSE([AC_LANG_PROGRAM([[
154           #ifdef __cplusplus
155           extern "C"
156           #endif
157           char $usb_lib_func ();
158           ]], [[return $usb_lib_func ();]])],
159           [libusbok=yes],[libusbok=no])
160         AC_MSG_RESULT([$libusbok])
161         LIBS="$save_LIBS"
162         AC_LANG_POP(C)
163
164         if test $libusbok = yes; then
165           if test ${libusb_name} != "libusb-1.0"; then
166             dnl PKGCONFIG found a legacy version of libusb; make sure the
167             dnl variable _usb_debug is available in the found library.
168             dnl Do not test on Windows, since that symbol is not defined.
169             case "$host_os" in
170               cygwin* | mingw*)
171               ;;
172             *)
173               AC_LANG_PUSH(C)
174               save_CPPFLAGS="$CPPFLAGS"
175               if test x$USB_INCLUDEDIR != x; then
176                 CPPFLAGS="$USB_INCLUDES"
177               fi
178               save_LIBS="$LIBS"
179               LIBS="$USB_LIBS"
180               AC_MSG_CHECKING([$libusb_name for symbol usb_debug in library $usb_lib_name])
181               AC_LINK_IFELSE([AC_LANG_PROGRAM([[
182                 extern int usb_debug;]],
183                 [[usb_debug = 0;]])],
184                 [libusbok=yes],[libusbok=no])
185               AC_MSG_RESULT([$libusbok])
186               LIBS="$save_LIBS"
187               CPPFLAGS="$save_CPPFLAGS"
188               AC_LANG_POP(C)
189               ;;
190             esac
191           fi
192         fi
193       fi
194     fi
195     dnl if everything checks out OK, finish up
196     if test $libusbok = yes; then
197       case "$host_os" in
198         cygwin* | mingw*)
199         ;;
200         *)
201           LIBUSB_PKG_CONFIG_NAME="${libusb_name}"
202         ;;
203       esac
204       break
205     else
206       dnl something wasn't found in this LIBUSB version.
207       dnl HACK: clear cache variables for header and library
208       unset USB_INCLUDEDIR
209       unset USB_INCLUDES
210       unset USB_LIBS
211       unset $as_ac_Header
212       unset $as_ac_Lib
213     fi
214   done
215
216   if test $libusbok = yes; then
217     dnl final error checking, mostly to create #define's
218     AC_LANG_PUSH(C)
219     save_CPPFLAGS="$CPPFLAGS"
220     if test x$USB_INCLUDEDIR != x; then
221       CPPFLAGS="$USB_INCLUDES"
222     fi
223     dnl Check for the header.
224     AC_CHECK_HEADERS([$usb_header], [], [libusbok=no])
225     CPPFLAGS="$save_CPPFLAGS"
226     AC_LANG_POP(C)
227
228     if test $libusbok = no; then
229       AC_MSG_RESULT([USRP requires libusb header '$usb_header' which was not found or was not usable. See http://www.libusb.org])
230     else
231       dnl check for the library (again)
232       AC_LANG_PUSH(C)
233       save_CPPFLAGS="$CPPFLAGS"
234       if test x$USB_INCLUDEDIR != x; then
235         CPPFLAGS="$USB_INCLUDES"
236       fi
237       save_LIBS="$LIBS"
238       LIBS="$USB_LIBS"
239       AC_CHECK_LIB([$usb_lib_name], [$usb_lib_func], [], [
240         libusbok=no
241         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])])
242 #      case "$host_os" in
243 #        cygwin* | mingw*)
244 #          USB_LIBS="$LIBS"
245 #          ;;
246 #        *) ;;
247 #      esac
248       LIBS="$save_LIBS"
249       CPPFLAGS="$save_CPPFLAGS"
250       AC_LANG_POP(C)
251     fi
252   fi
253
254   if test $libusbok = yes; then
255     dnl success
256     AC_MSG_NOTICE([Using LIBUSB version '${libusb_name}'])
257     ifelse([$2], , :, [$2])
258   else
259     dnl not found; clear substitution variables
260     LIBUSB_PKG_CONFIG_NAME=
261     USB_INCLUDES=
262     USB_LIBS=
263     ifelse([$3], , :, [$3])
264   fi
265
266   dnl create substitution variables
267   AC_SUBST(USB_INCLUDES)
268   AC_SUBST(USB_LIBS)
269   AC_SUBST(LIBUSB_PKG_CONFIG_NAME)
270
271   dnl restore LDFLAGS
272   LDFLAGS=${saved_LDFLAGS}
273 ])