|
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_HEADER_FILE: the location of the C header file |
12 |
| -# LibUSB_INCLUDE_DIRS: the directory that contains the include file |
13 |
| -# LibUSB_LIBRARIES: the library |
14 |
| -# source: https://github.com/IntelRealSense/librealsense |
15 |
| - |
16 |
| -cmake_policy(SET CMP0045 NEW) |
17 |
| -cmake_policy(SET CMP0053 NEW) |
18 |
| -cmake_policy(SET CMP0054 NEW) |
19 |
| - |
20 |
| -include ( CheckLibraryExists ) |
21 |
| -include ( CheckIncludeFile ) |
22 |
| - |
23 |
| -find_package ( PkgConfig ) |
24 |
| -if ( PKG_CONFIG_FOUND ) |
25 |
| - pkg_check_modules ( PKGCONFIG_LIBUSB libusb-1.0 ) |
26 |
| - if ( NOT PKGCONFIG_LIBUSB_FOUND ) |
27 |
| - pkg_check_modules ( PKGCONFIG_LIBUSB libusb ) |
28 |
| - endif ( NOT PKGCONFIG_LIBUSB_FOUND ) |
29 |
| -endif ( PKG_CONFIG_FOUND ) |
30 |
| - |
31 |
| -if ( PKGCONFIG_LIBUSB_FOUND ) |
32 |
| - set ( LibUSB_INCLUDE_DIRS ${PKGCONFIG_LIBUSB_INCLUDE_DIRS} ) |
33 |
| - foreach ( i ${PKGCONFIG_LIBUSB_LIBRARIES} ) |
34 |
| - string ( REGEX MATCH "[^-]*" ibase "${i}" ) |
35 |
| - find_library ( ${ibase}_LIBRARY |
36 |
| - NAMES ${i} |
37 |
| - PATHS ${PKGCONFIG_LIBUSB_LIBRARY_DIRS} |
38 |
| - ) |
39 |
| - if ( ${ibase}_LIBRARY ) |
40 |
| - list ( APPEND LibUSB_LIBRARIES ${${ibase}_LIBRARY} ) |
41 |
| - endif ( ${ibase}_LIBRARY ) |
42 |
| - mark_as_advanced ( ${ibase}_LIBRARY ) |
43 |
| - endforeach ( i ) |
| 1 | +# https://github.com/ryanbinns/ttwatch/blob/f449ce60818454415a4a667b39654f0e5446a6b3/cmake_modules/FindLibUSB.cmake#L62 |
44 | 2 |
|
45 |
| -else ( PKGCONFIG_LIBUSB_FOUND ) |
46 |
| - find_file ( LibUSB_HEADER_FILE |
47 |
| - NAMES |
48 |
| - libusb.h usb.h |
49 |
| - PATHS |
50 |
| - $ENV{ProgramFiles}/LibUSB-Win32 |
51 |
| - $ENV{LibUSB_ROOT_DIR} |
52 |
| - PATH_SUFFIXES |
53 |
| - include |
54 |
| - libusb-1.0 |
55 |
| - include/libusb-1.0 |
56 |
| - ) |
57 |
| - mark_as_advanced ( LibUSB_HEADER_FILE ) |
58 |
| - get_filename_component ( LibUSB_INCLUDE_DIRS "${LibUSB_HEADER_FILE}" PATH ) |
59 |
| - |
60 |
| - if ( ${CMAKE_SYSTEM_NAME} STREQUAL "Windows" ) |
61 |
| - # LibUSB-Win32 binary distribution contains several libs. |
62 |
| - # Use the lib that got compiled with the same compiler. |
63 |
| - if ( MSVC ) |
64 |
| - if ( WIN32 ) |
65 |
| - set ( LibUSB_LIBRARY_PATH_SUFFIX lib/msvc ) |
66 |
| - else ( WIN32 ) |
67 |
| - set ( LibUSB_LIBRARY_PATH_SUFFIX lib/msvc_x64 ) |
68 |
| - endif ( WIN32 ) |
69 |
| - elseif ( BORLAND ) |
70 |
| - set ( LibUSB_LIBRARY_PATH_SUFFIX lib/bcc ) |
71 |
| - elseif ( CMAKE_COMPILER_IS_GNUCC ) |
72 |
| - set ( LibUSB_LIBRARY_PATH_SUFFIX lib/gcc ) |
73 |
| - endif ( MSVC ) |
74 |
| - endif ( ${CMAKE_SYSTEM_NAME} STREQUAL "Windows" ) |
75 |
| - |
76 |
| - find_library ( usb_LIBRARY |
77 |
| - NAMES |
78 |
| - usb-1.0 libusb usb |
79 |
| - PATHS |
80 |
| - $ENV{ProgramFiles}/LibUSB-Win32 |
81 |
| - $ENV{LibUSB_ROOT_DIR} |
82 |
| - PATH_SUFFIXES |
83 |
| - ${LibUSB_LIBRARY_PATH_SUFFIX} |
84 |
| - ) |
85 |
| - mark_as_advanced ( usb_LIBRARY ) |
86 |
| - if ( usb_LIBRARY ) |
87 |
| - set ( LibUSB_LIBRARIES ${usb_LIBRARY} ) |
88 |
| - endif ( usb_LIBRARY ) |
| 3 | +# - Try to find libusb-1.0 |
| 4 | +# Once done this will define |
| 5 | +# |
| 6 | +# LIBUSB_1_FOUND - system has libusb |
| 7 | +# LIBUSB_1_INCLUDE_DIRS - the libusb include directory |
| 8 | +# LIBUSB_1_LIBRARIES - Link these to use libusb |
| 9 | +# LIBUSB_1_DEFINITIONS - Compiler switches required for using libusb |
| 10 | +# |
| 11 | +# Adapted from cmake-modules Google Code project |
| 12 | +# |
| 13 | +# Copyright (c) 2006 Andreas Schneider <[email protected]> |
| 14 | +# |
| 15 | +# (Changes for libusb) Copyright (c) 2008 Kyle Machulis <[email protected]> |
| 16 | +# |
| 17 | +# Redistribution and use is allowed according to the terms of the New BSD license. |
| 18 | +# |
| 19 | +# CMake-Modules Project New BSD License |
| 20 | +# |
| 21 | +# Redistribution and use in source and binary forms, with or without |
| 22 | +# modification, are permitted provided that the following conditions are met: |
| 23 | +# |
| 24 | +# * Redistributions of source code must retain the above copyright notice, this |
| 25 | +# list of conditions and the following disclaimer. |
| 26 | +# |
| 27 | +# * Redistributions in binary form must reproduce the above copyright notice, |
| 28 | +# this list of conditions and the following disclaimer in the |
| 29 | +# documentation and/or other materials provided with the distribution. |
| 30 | +# |
| 31 | +# * Neither the name of the CMake-Modules Project nor the names of its |
| 32 | +# contributors may be used to endorse or promote products derived from this |
| 33 | +# software without specific prior written permission. |
| 34 | +# |
| 35 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 36 | +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 37 | +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 38 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 39 | +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 40 | +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 41 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 42 | +# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 43 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 44 | +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 45 | +# |
89 | 46 |
|
90 |
| -endif ( PKGCONFIG_LIBUSB_FOUND ) |
91 | 47 |
|
92 |
| -if ( LibUSB_INCLUDE_DIRS AND LibUSB_LIBRARIES ) |
93 |
| - set ( LibUSB_FOUND true ) |
94 |
| -endif ( LibUSB_INCLUDE_DIRS AND LibUSB_LIBRARIES ) |
| 48 | +if (LIBUSB_1_LIBRARIES AND LIBUSB_1_INCLUDE_DIRS) |
| 49 | + # in cache already |
| 50 | + set(LIBUSB_FOUND TRUE) |
| 51 | +else (LIBUSB_1_LIBRARIES AND LIBUSB_1_INCLUDE_DIRS) |
| 52 | + find_path(LIBUSB_1_INCLUDE_DIR |
| 53 | + NAMES |
| 54 | + libusb.h |
| 55 | + PATHS |
| 56 | + /usr/include |
| 57 | + /usr/local/include |
| 58 | + /opt/local/include |
| 59 | + /sw/include |
| 60 | + PATH_SUFFIXES |
| 61 | + libusb-1.0 |
| 62 | + ) |
95 | 63 |
|
96 |
| -if ( LibUSB_FOUND ) |
97 |
| - set ( CMAKE_REQUIRED_INCLUDES "${LibUSB_INCLUDE_DIRS}" ) |
98 |
| - check_include_file ( "${LibUSB_HEADER_FILE}" LibUSB_FOUND ) |
99 |
| -endif ( LibUSB_FOUND ) |
| 64 | + find_library(LIBUSB_1_LIBRARY |
| 65 | + NAMES |
| 66 | + usb-1.0 usb |
| 67 | + PATHS |
| 68 | + /usr/lib |
| 69 | + /usr/local/lib |
| 70 | + /opt/local/lib |
| 71 | + /sw/lib |
| 72 | + ) |
100 | 73 |
|
101 |
| -if ( LibUSB_FOUND ) |
102 |
| - check_library_exists ( "${LibUSB_LIBRARIES}" usb_open "" LibUSB_FOUND ) |
103 |
| - check_library_exists ( "${LibUSB_LIBRARIES}" libusb_get_device_list "" LibUSB_VERSION_1.0 ) |
104 |
| - check_library_exists ( "${LibUSB_LIBRARIES}" libusb_get_port_numbers "" LibUSB_VERSION_1.0.16 ) |
| 74 | + set(LIBUSB_1_INCLUDE_DIRS |
| 75 | + ${LIBUSB_1_INCLUDE_DIR} |
| 76 | + ) |
| 77 | + set(LIBUSB_1_LIBRARIES |
| 78 | + ${LIBUSB_1_LIBRARY} |
| 79 | +) |
105 | 80 |
|
106 |
| - if((STATIC AND UNIX AND NOT APPLE) OR (DEPENDS AND CMAKE_SYSTEM_NAME STREQUAL "Linux")) |
107 |
| - find_library(LIBUDEV_LIBRARY udev) |
108 |
| - if(LIBUDEV_LIBRARY) |
109 |
| - set(LibUSB_LIBRARIES "${LibUSB_LIBRARIES};${LIBUDEV_LIBRARY}") |
110 |
| - else() |
111 |
| - message(WARNING "libudev library not found, binaries may fail to link.") |
112 |
| - endif() |
113 |
| - endif() |
| 81 | + if (LIBUSB_1_INCLUDE_DIRS AND LIBUSB_1_LIBRARIES) |
| 82 | + set(LIBUSB_1_FOUND TRUE) |
| 83 | + endif (LIBUSB_1_INCLUDE_DIRS AND LIBUSB_1_LIBRARIES) |
114 | 84 |
|
115 |
| - # Library 1.0.16+ compilation test. |
116 |
| - # The check_library_exists does not work well on Apple with shared libs. |
117 |
| - if (APPLE OR LibUSB_VERSION_1.0.16 OR STATIC) |
118 |
| - if (APPLE) |
119 |
| - if(DEPENDS) |
120 |
| - list(APPEND TEST_COMPILE_EXTRA_LIBRARIES "-framework Foundation -framework IOKit") |
121 |
| - else() |
122 |
| - find_library(COREFOUNDATION CoreFoundation) |
123 |
| - find_library(IOKIT IOKit) |
124 |
| - list(APPEND TEST_COMPILE_EXTRA_LIBRARIES ${IOKIT}) |
125 |
| - list(APPEND TEST_COMPILE_EXTRA_LIBRARIES ${COREFOUNDATION}) |
126 |
| - endif() |
127 |
| - endif() |
128 |
| - if (WIN32) |
129 |
| - list(APPEND TEST_COMPILE_EXTRA_LIBRARIES setupapi) |
130 |
| - endif() |
131 |
| - list(APPEND TEST_COMPILE_EXTRA_LIBRARIES ${LibUSB_LIBRARIES}) |
| 85 | + if (LIBUSB_1_FOUND) |
| 86 | + if (NOT libusb_1_FIND_QUIETLY) |
| 87 | + message(STATUS "Found libusb-1.0:") |
| 88 | + message(STATUS " - Includes: ${LIBUSB_1_INCLUDE_DIRS}") |
| 89 | + message(STATUS " - Libraries: ${LIBUSB_1_LIBRARIES}") |
| 90 | + endif (NOT libusb_1_FIND_QUIETLY) |
| 91 | + else (LIBUSB_1_FOUND) |
| 92 | + if (libusb_1_FIND_REQUIRED) |
| 93 | + message(FATAL_ERROR "Could not find libusb") |
| 94 | + endif (libusb_1_FIND_REQUIRED) |
| 95 | + endif (LIBUSB_1_FOUND) |
132 | 96 |
|
133 |
| - try_compile(LibUSB_COMPILE_TEST_PASSED |
134 |
| - ${CMAKE_BINARY_DIR} |
135 |
| - "${PROJECT_SOURCE_DIR}/cmake/test-libusb-version.cpp" |
136 |
| - CMAKE_FLAGS |
137 |
| - "-DINCLUDE_DIRECTORIES=${LibUSB_INCLUDE_DIRS}" |
138 |
| - "-DLINK_DIRECTORIES=${LibUSB_LIBRARIES}" |
139 |
| - LINK_LIBRARIES ${TEST_COMPILE_EXTRA_LIBRARIES} |
140 |
| - OUTPUT_VARIABLE OUTPUT) |
141 |
| - unset(TEST_COMPILE_EXTRA_LIBRARIES) |
142 |
| - message(STATUS "LibUSB Compilation test: ${LibUSB_COMPILE_TEST_PASSED}") |
143 |
| - endif() |
144 |
| -endif ( LibUSB_FOUND ) |
| 97 | + # show the LIBUSB_1_INCLUDE_DIRS and LIBUSB_1_LIBRARIES variables only in the advanced view |
| 98 | + mark_as_advanced(LIBUSB_1_INCLUDE_DIRS LIBUSB_1_LIBRARIES) |
145 | 99 |
|
146 |
| -if ( NOT LibUSB_FOUND ) |
147 |
| - if ( NOT LibUSB_FIND_QUIETLY ) |
148 |
| - message ( STATUS "LibUSB not found, try setting LibUSB_ROOT_DIR environment variable." ) |
149 |
| - endif ( NOT LibUSB_FIND_QUIETLY ) |
150 |
| - if ( LibUSB_FIND_REQUIRED ) |
151 |
| - message ( FATAL_ERROR "" ) |
152 |
| - endif ( LibUSB_FIND_REQUIRED ) |
153 |
| -endif ( NOT LibUSB_FOUND ) |
| 100 | +endif (LIBUSB_1_LIBRARIES AND LIBUSB_1_INCLUDE_DIRS) |
0 commit comments