Debian Codegraph @

cmake/FindLibUSB.cmake

raw
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 include ( CheckLibraryExists )
17 include ( CheckIncludeFile )
18
19 find_package ( PkgConfig )
20 if ( PKG_CONFIG_FOUND )
21 pkg_check_modules ( PKGCONFIG_LIBUSB libusb-1.0 )
22 if ( NOT PKGCONFIG_LIBUSB_FOUND )
23 pkg_check_modules ( PKGCONFIG_LIBUSB libusb )
24 endif ( NOT PKGCONFIG_LIBUSB_FOUND )
25 endif ( PKG_CONFIG_FOUND )
26
27 if ( PKGCONFIG_LIBUSB_FOUND )
28 set ( LibUSB_INCLUDE_DIRS ${PKGCONFIG_LIBUSB_INCLUDE_DIRS} )
29 foreach ( i ${PKGCONFIG_LIBUSB_LIBRARIES} )
30 string ( REGEX MATCH "[^-]*" ibase "${i}" )
31 find_library ( ${ibase}_LIBRARY
32 NAMES ${i}
33 PATHS ${PKGCONFIG_LIBUSB_LIBRARY_DIRS}
34 )
35 if ( ${ibase}_LIBRARY )
36 list ( APPEND LibUSB_LIBRARIES ${${ibase}_LIBRARY} )
37 endif ( ${ibase}_LIBRARY )
38 mark_as_advanced ( ${ibase}_LIBRARY )
39 endforeach ( i )
40
41 else ( PKGCONFIG_LIBUSB_FOUND )
42 find_file ( LibUSB_HEADER_FILE
43 NAMES
44 libusb.h usb.h
45 PATHS
46 $ENV{ProgramFiles}/LibUSB-Win32
47 $ENV{LibUSB_ROOT_DIR}
48 PATH_SUFFIXES
49 include
50 libusb-1.0
51 include/libusb-1.0
52 )
53 mark_as_advanced ( LibUSB_HEADER_FILE )
54 get_filename_component ( LibUSB_INCLUDE_DIRS "${LibUSB_HEADER_FILE}" PATH )
55
56 if ( ${CMAKE_SYSTEM_NAME} STREQUAL "Windows" )
57 # LibUSB-Win32 binary distribution contains several libs.
58 # Use the lib that got compiled with the same compiler.
59 if ( MSVC )
60 if ( WIN32 )
61 set ( LibUSB_LIBRARY_PATH_SUFFIX lib/msvc )
62 else ( WIN32 )
63 set ( LibUSB_LIBRARY_PATH_SUFFIX lib/msvc_x64 )
64 endif ( WIN32 )
65 elseif ( BORLAND )
66 set ( LibUSB_LIBRARY_PATH_SUFFIX lib/bcc )
67 elseif ( CMAKE_COMPILER_IS_GNUCC )
68 set ( LibUSB_LIBRARY_PATH_SUFFIX lib/gcc )
69 endif ( MSVC )
70 endif ( ${CMAKE_SYSTEM_NAME} STREQUAL "Windows" )
71
72 find_library ( usb_LIBRARY
73 NAMES
74 usb-1.0 libusb usb
75 PATHS
76 $ENV{ProgramFiles}/LibUSB-Win32
77 $ENV{LibUSB_ROOT_DIR}
78 PATH_SUFFIXES
79 ${LibUSB_LIBRARY_PATH_SUFFIX}
80 )
81 mark_as_advanced ( usb_LIBRARY )
82 if ( usb_LIBRARY )
83 set ( LibUSB_LIBRARIES ${usb_LIBRARY} )
84 endif ( usb_LIBRARY )
85
86 endif ( PKGCONFIG_LIBUSB_FOUND )
87
88 if ( LibUSB_INCLUDE_DIRS AND LibUSB_LIBRARIES )
89 set ( LibUSB_FOUND true )
90 endif ( LibUSB_INCLUDE_DIRS AND LibUSB_LIBRARIES )
91
92 if ( LibUSB_FOUND )
93 set ( CMAKE_REQUIRED_INCLUDES "${LibUSB_INCLUDE_DIRS}" )
94 check_include_file ( "${LibUSB_HEADER_FILE}" LibUSB_FOUND )
95 endif ( LibUSB_FOUND )
96
97 if ( LibUSB_FOUND )
98 check_library_exists ( "${LibUSB_LIBRARIES}" usb_open "" LibUSB_FOUND )
99 check_library_exists ( "${LibUSB_LIBRARIES}" libusb_get_device_list "" LibUSB_VERSION_1.0 )
100 check_library_exists ( "${LibUSB_LIBRARIES}" libusb_get_port_numbers "" LibUSB_VERSION_1.0.16 )
101
102 if((STATIC AND UNIX AND NOT APPLE) OR (DEPENDS AND CMAKE_SYSTEM_NAME STREQUAL "Linux") OR ANDROID)
103 find_library(LIBUDEV_LIBRARY udev)
104 if(LIBUDEV_LIBRARY)
105 set(LibUSB_LIBRARIES "${LibUSB_LIBRARIES};${LIBUDEV_LIBRARY}")
106 else()
107 message(WARNING "libudev library not found, binaries may fail to link.")
108 endif()
109 endif()
110
111 # Library 1.0.16+ compilation test.
112 # The check_library_exists does not work well on Apple with shared libs.
113 if (APPLE OR LibUSB_VERSION_1.0.16 OR STATIC)
114 if (APPLE)
115 if(DEPENDS)
116 list(APPEND TEST_COMPILE_EXTRA_LIBRARIES "-framework Foundation -framework IOKit -framework Security")
117 else()
118 find_library(COREFOUNDATION CoreFoundation)
119 find_library(IOKIT IOKit)
120 list(APPEND TEST_COMPILE_EXTRA_LIBRARIES ${IOKIT})
121 list(APPEND TEST_COMPILE_EXTRA_LIBRARIES ${COREFOUNDATION})
122
123 if(STATIC)
124 find_library(OBJC objc.a)
125 set(LIBUSB_DEP_LINKER ${OBJC})
126 list(APPEND TEST_COMPILE_EXTRA_LIBRARIES ${LIBUSB_DEP_LINKER})
127 endif()
128 endif()
129 endif()
130 if (WIN32)
131 list(APPEND TEST_COMPILE_EXTRA_LIBRARIES setupapi)
132 endif()
133 list(APPEND TEST_COMPILE_EXTRA_LIBRARIES ${LibUSB_LIBRARIES})
134
135 try_compile(LibUSB_COMPILE_TEST_PASSED
136 ${CMAKE_BINARY_DIR}
137 "${CMAKE_CURRENT_LIST_DIR}/test-libusb-version.c"
138 CMAKE_FLAGS
139 "-DINCLUDE_DIRECTORIES=${LibUSB_INCLUDE_DIRS}"
140 "-DLINK_DIRECTORIES=${LibUSB_LIBRARIES}"
141 LINK_LIBRARIES ${TEST_COMPILE_EXTRA_LIBRARIES}
142 OUTPUT_VARIABLE OUTPUT)
143 unset(TEST_COMPILE_EXTRA_LIBRARIES)
144 message(STATUS "LibUSB Compilation test: ${LibUSB_COMPILE_TEST_PASSED}")
145 endif()
146 endif ( LibUSB_FOUND )
147
148 if ( NOT LibUSB_FOUND )
149 if ( NOT LibUSB_FIND_QUIETLY )
150 message ( STATUS "LibUSB not found, try setting LibUSB_ROOT_DIR environment variable." )
151 endif ( NOT LibUSB_FIND_QUIETLY )
152 if ( LibUSB_FIND_REQUIRED )
153 message ( FATAL_ERROR "" )
154 endif ( LibUSB_FIND_REQUIRED )
155 endif ( NOT LibUSB_FOUND )