|
| 1 | +# - Find libusb for portable USB support |
| 2 | +# This module will find libusb as published by |
| 3 | +# http://libusb.sf.net and |
| 4 | +# http://libusb-win32.sf.net |
| 5 | +# |
| 6 | +# It will use PkgConfig if present and supported, else search |
| 7 | +# it on its own. If the LibUSB_ROOT_DIR environment variable |
| 8 | +# is defined, it will be used as base path. |
| 9 | +# The following standard variables get defined: |
| 10 | +# LibUSB_FOUND: true if LibUSB was found |
| 11 | +# LibUSB_INCLUDE_DIRS: the directory that contains the include file |
| 12 | +# LibUSB_LIBRARIES: the library |
| 13 | + |
| 14 | +include ( CheckLibraryExists ) |
| 15 | +include ( CheckIncludeFile ) |
| 16 | + |
| 17 | +find_package ( PkgConfig ) |
| 18 | +if ( PKG_CONFIG_FOUND ) |
| 19 | + pkg_check_modules ( PKGCONFIG_LIBUSB libusb ) |
| 20 | +endif ( PKG_CONFIG_FOUND ) |
| 21 | + |
| 22 | +if ( PKGCONFIG_LIBUSB_FOUND ) |
| 23 | + set ( LibUSB_FOUND ${PKGCONFIG_LIBUSB_FOUND} ) |
| 24 | + set ( LibUSB_INCLUDE_DIRS ${PKGCONFIG_LIBUSB_INCLUDE_DIRS} ) |
| 25 | + foreach ( i ${PKGCONFIG_LIBUSB_LIBRARIES} ) |
| 26 | + find_library ( ${i}_LIBRARY |
| 27 | + NAMES ${i} |
| 28 | + PATHS ${PKGCONFIG_LIBUSB_LIBRARY_DIRS} |
| 29 | + ) |
| 30 | + if ( ${i}_LIBRARY ) |
| 31 | + list ( APPEND LibUSB_LIBRARIES ${${i}_LIBRARY} ) |
| 32 | + endif ( ${i}_LIBRARY ) |
| 33 | + mark_as_advanced ( ${i}_LIBRARY ) |
| 34 | + endforeach ( i ) |
| 35 | + |
| 36 | +else ( PKGCONFIG_LIBUSB_FOUND ) |
| 37 | + find_path ( LibUSB_INCLUDE_DIRS |
| 38 | + NAMES |
| 39 | + libusb.h |
| 40 | + PATHS |
| 41 | + $ENV{ProgramFiles}/LibUSB-Win32 |
| 42 | + $ENV{LibUSB_ROOT_DIR} |
| 43 | + PATH_SUFFIXES |
| 44 | + libusb |
| 45 | + ) |
| 46 | + mark_as_advanced ( LibUSB_INCLUDE_DIRS ) |
| 47 | +# message ( STATUS "LibUSB include dir: ${LibUSB_INCLUDE_DIRS}" ) |
| 48 | + |
| 49 | + if ( ${CMAKE_SYSTEM_NAME} STREQUAL "Windows" ) |
| 50 | + # LibUSB-Win32 binary distribution contains several libs. |
| 51 | + # Use the lib that got compiled with the same compiler. |
| 52 | + if ( MSVC ) |
| 53 | + if ( ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x32" ) |
| 54 | + set ( LibUSB_LIBRARY_PATH_SUFFIX lib/msvc ) |
| 55 | + else () |
| 56 | + message(x64) |
| 57 | + set ( LibUSB_LIBRARY_PATH_SUFFIX x64/Debug/dll ) |
| 58 | + endif () |
| 59 | + elseif ( BORLAND ) |
| 60 | + set ( LibUSB_LIBRARY_PATH_SUFFIX lib/bcc ) |
| 61 | + elseif ( CMAKE_COMPILER_IS_GNUCC ) |
| 62 | + set ( LibUSB_LIBRARY_PATH_SUFFIX lib/gcc ) |
| 63 | + endif ( MSVC ) |
| 64 | + endif ( ${CMAKE_SYSTEM_NAME} STREQUAL "Windows" ) |
| 65 | + |
| 66 | + find_library ( usb_LIBRARY |
| 67 | + NAMES |
| 68 | + libusb libusb-1.0 usb |
| 69 | + PATHS |
| 70 | + $ENV{ProgramFiles}/LibUSB-Win32 |
| 71 | + $ENV{LibUSB_ROOT_DIR} |
| 72 | + PATH_SUFFIXES |
| 73 | + ${LibUSB_LIBRARY_PATH_SUFFIX} |
| 74 | + ) |
| 75 | + mark_as_advanced ( usb_LIBRARY ) |
| 76 | + if ( usb_LIBRARY ) |
| 77 | + set ( LibUSB_LIBRARIES ${usb_LIBRARY} ) |
| 78 | + endif ( usb_LIBRARY ) |
| 79 | + |
| 80 | + if ( LibUSB_INCLUDE_DIRS AND LibUSB_LIBRARIES ) |
| 81 | + set ( LibUSB_FOUND true ) |
| 82 | + endif ( LibUSB_INCLUDE_DIRS AND LibUSB_LIBRARIES ) |
| 83 | +endif ( PKGCONFIG_LIBUSB_FOUND ) |
| 84 | + |
| 85 | +if ( LibUSB_FOUND ) |
| 86 | + set ( CMAKE_REQUIRED_INCLUDES "${LibUSB_INCLUDE_DIRS}" ) |
| 87 | + check_include_file ( usb.h LibUSB_FOUND ) |
| 88 | +# message ( STATUS "LibUSB: usb.h is usable: ${LibUSB_FOUND}" ) |
| 89 | +endif ( LibUSB_FOUND ) |
| 90 | +if ( LibUSB_FOUND ) |
| 91 | + check_library_exists ( "${LibUSB_LIBRARIES}" usb_open "" LibUSB_FOUND ) |
| 92 | +# message ( STATUS "LibUSB: library is usable: ${LibUSB_FOUND}" ) |
| 93 | +endif ( LibUSB_FOUND ) |
| 94 | + |
| 95 | +if ( NOT LibUSB_FOUND ) |
| 96 | + if ( NOT LibUSB_FIND_QUIETLY ) |
| 97 | + message ( STATUS "LibUSB not found, try setting LibUSB_ROOT_DIR environment variable." ) |
| 98 | + endif ( NOT LibUSB_FIND_QUIETLY ) |
| 99 | + if ( LibUSB_FIND_REQUIRED ) |
| 100 | + message ( FATAL_ERROR "" ) |
| 101 | + endif ( LibUSB_FIND_REQUIRED ) |
| 102 | +endif ( NOT LibUSB_FOUND ) |
| 103 | +#message ( STATUS "LibUSB: ${LibUSB_FOUND}" ) |
0 commit comments