The open source OpenXR runtime
1#.rst:
2# FindHIDAPI
3# ----------
4#
5# Try to find HIDAPI library, from http://www.signal11.us/oss/hidapi/
6#
7# Cache Variables: (probably not for direct use in your scripts)
8# HIDAPI_INCLUDE_DIR
9# HIDAPI_LIBRARY
10#
11# Non-cache variables you might use in your CMakeLists.txt:
12# HIDAPI_FOUND
13# HIDAPI_INCLUDE_DIRS
14# HIDAPI_LIBRARIES
15#
16# COMPONENTS
17# ^^^^^^^^^^
18#
19# This module respects several COMPONENTS specifying the backend you prefer:
20# ``any`` (the default), ``libusb``, and ``hidraw``.
21# The availablility of the latter two depends on your platform.
22#
23#
24# IMPORTED Targets
25# ^^^^^^^^^^^^^^^^
26#
27# This module defines :prop_tgt:`IMPORTED` target ``HIDAPI::hidapi`` (in all cases or
28# if no components specified), ``HIDAPI::hidapi-libusb`` (if you requested the libusb component),
29# and ``HIDAPI::hidapi-hidraw`` (if you requested the hidraw component),
30#
31# Result Variables
32# ^^^^^^^^^^^^^^^^
33#
34# ``HIDAPI_FOUND``
35# True if HIDAPI or the requested components (if any) were found.
36#
37# We recommend using the imported targets instead of the following.
38#
39# ``HIDAPI_INCLUDE_DIRS``
40# ``HIDAPI_LIBRARIES``
41#
42# Original Author:
43# 2009-2010, 2019 Ryan Pavlik <ryan.pavlik@collabora.com> <abiryan@ryand.net>
44# http://academic.cleardefinition.com
45#
46# Copyright Iowa State University 2009-2010.
47# Copyright Collabora, Ltd. 2019.
48# SPDX-License-Identifier: BSL-1.0
49# Distributed under the Boost Software License, Version 1.0.
50# (See accompanying file LICENSE_1_0.txt or copy at
51# http://www.boost.org/LICENSE_1_0.txt)
52
53set(HIDAPI_ROOT_DIR
54 "${HIDAPI_ROOT_DIR}"
55 CACHE PATH "Root to search for HIDAPI")
56
57# Clean up components
58if(HIDAPI_FIND_COMPONENTS)
59 if(WIN32 OR APPLE)
60 # This makes no sense on Windows or Mac, which have native APIs
61 list(REMOVE HIDAPI_FIND_COMPONENTS libusb)
62 endif()
63
64 if(NOT ${CMAKE_SYSTEM} MATCHES "Linux")
65 # hidraw is only on linux
66 list(REMOVE HIDAPI_FIND_COMPONENTS hidraw)
67 endif()
68endif()
69if(NOT HIDAPI_FIND_COMPONENTS)
70 # Default to any
71 set(HIDAPI_FIND_COMPONENTS any)
72endif()
73
74# Ask pkg-config for hints
75find_package(PkgConfig QUIET)
76if(PKG_CONFIG_FOUND)
77 set(_old_prefix_path "${CMAKE_PREFIX_PATH}")
78 # So pkg-config uses HIDAPI_ROOT_DIR too.
79 if(HIDAPI_ROOT_DIR)
80 list(APPEND CMAKE_PREFIX_PATH ${HIDAPI_ROOT_DIR})
81 endif()
82 pkg_check_modules(PC_HIDAPI_LIBUSB QUIET hidapi-libusb)
83 pkg_check_modules(PC_HIDAPI_HIDRAW QUIET hidapi-hidraw)
84 # Restore
85 set(CMAKE_PREFIX_PATH "${_old_prefix_path}")
86endif()
87
88# Actually search
89find_library(
90 HIDAPI_UNDECORATED_LIBRARY
91 NAMES hidapi
92 PATHS "${HIDAPI_ROOT_DIR}"
93 PATH_SUFFIXES lib)
94
95find_library(
96 HIDAPI_LIBUSB_LIBRARY
97 NAMES hidapi hidapi-libusb
98 PATHS "${HIDAPI_ROOT_DIR}"
99 PATH_SUFFIXES lib
100 HINTS ${PC_HIDAPI_LIBUSB_LIBRARY_DIRS})
101
102if(CMAKE_SYSTEM MATCHES "Linux")
103 find_library(
104 HIDAPI_HIDRAW_LIBRARY
105 NAMES hidapi-hidraw
106 HINTS ${PC_HIDAPI_HIDRAW_LIBRARY_DIRS})
107endif()
108
109find_path(
110 HIDAPI_INCLUDE_DIR
111 NAMES hidapi.h
112 PATHS "${HIDAPI_ROOT_DIR}"
113 PATH_SUFFIXES hidapi include include/hidapi
114 HINTS ${PC_HIDAPI_HIDRAW_INCLUDE_DIRS} ${PC_HIDAPI_LIBUSB_INCLUDE_DIRS})
115
116find_package(Threads QUIET)
117
118###
119# Compute the "I don't care which backend" library
120###
121set(HIDAPI_LIBRARY)
122
123# First, try to use a preferred backend if supplied
124if("${HIDAPI_FIND_COMPONENTS}" MATCHES "libusb"
125 AND HIDAPI_LIBUSB_LIBRARY
126 AND NOT HIDAPI_LIBRARY)
127 set(HIDAPI_LIBRARY ${HIDAPI_LIBUSB_LIBRARY})
128endif()
129if("${HIDAPI_FIND_COMPONENTS}" MATCHES "hidraw"
130 AND HIDAPI_HIDRAW_LIBRARY
131 AND NOT HIDAPI_LIBRARY)
132 set(HIDAPI_LIBRARY ${HIDAPI_HIDRAW_LIBRARY})
133endif()
134
135# Then, if we don't have a preferred one, settle for anything.
136if(NOT HIDAPI_LIBRARY)
137 if(HIDAPI_LIBUSB_LIBRARY)
138 set(HIDAPI_LIBRARY ${HIDAPI_LIBUSB_LIBRARY})
139 elseif(HIDAPI_HIDRAW_LIBRARY)
140 set(HIDAPI_LIBRARY ${HIDAPI_HIDRAW_LIBRARY})
141 elseif(HIDAPI_UNDECORATED_LIBRARY)
142 set(HIDAPI_LIBRARY ${HIDAPI_UNDECORATED_LIBRARY})
143 endif()
144endif()
145
146###
147# Determine if the various requested components are found.
148###
149set(_hidapi_component_required_vars)
150
151foreach(_comp IN LISTS HIDAPI_FIND_COMPONENTS)
152 if("${_comp}" STREQUAL "any")
153 list(APPEND _hidapi_component_required_vars HIDAPI_INCLUDE_DIR
154 HIDAPI_LIBRARY)
155 if(HIDAPI_INCLUDE_DIR AND EXISTS "${HIDAPI_LIBRARY}")
156 set(HIDAPI_any_FOUND TRUE)
157 mark_as_advanced(HIDAPI_INCLUDE_DIR)
158 else()
159 set(HIDAPI_any_FOUND FALSE)
160 endif()
161
162 elseif("${_comp}" STREQUAL "libusb")
163 list(APPEND _hidapi_component_required_vars HIDAPI_INCLUDE_DIR
164 HIDAPI_LIBUSB_LIBRARY)
165 if(HIDAPI_INCLUDE_DIR AND EXISTS "${HIDAPI_LIBUSB_LIBRARY}")
166 set(HIDAPI_libusb_FOUND TRUE)
167 mark_as_advanced(HIDAPI_INCLUDE_DIR HIDAPI_LIBUSB_LIBRARY)
168 else()
169 set(HIDAPI_libusb_FOUND FALSE)
170 endif()
171
172 elseif("${_comp}" STREQUAL "hidraw")
173 list(APPEND _hidapi_component_required_vars HIDAPI_INCLUDE_DIR
174 HIDAPI_HIDRAW_LIBRARY)
175 if(HIDAPI_INCLUDE_DIR AND EXISTS "${HIDAPI_HIDRAW_LIBRARY}")
176 set(HIDAPI_hidraw_FOUND TRUE)
177 mark_as_advanced(HIDAPI_INCLUDE_DIR HIDAPI_HIDRAW_LIBRARY)
178 else()
179 set(HIDAPI_hidraw_FOUND FALSE)
180 endif()
181
182 else()
183 message(WARNING "${_comp} is not a recognized HIDAPI component")
184 set(HIDAPI_${_comp}_FOUND FALSE)
185 endif()
186endforeach()
187unset(_comp)
188
189###
190# FPHSA call
191###
192include(FindPackageHandleStandardArgs)
193find_package_handle_standard_args(
194 HIDAPI
195 REQUIRED_VARS ${_hidapi_component_required_vars} THREADS_FOUND
196 HANDLE_COMPONENTS)
197if(HIDAPI_FOUND)
198 set(HIDAPI_LIBRARIES "${HIDAPI_LIBRARY}")
199 set(HIDAPI_INCLUDE_DIRS "${HIDAPI_INCLUDE_DIR}")
200 if(NOT TARGET HIDAPI::hidapi)
201 add_library(HIDAPI::hidapi UNKNOWN IMPORTED)
202 set_target_properties(
203 HIDAPI::hidapi
204 PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C"
205 IMPORTED_LOCATION "${HIDAPI_LIBRARY}"
206 INTERFACE_INCLUDE_DIRECTORIES "${HIDAPI_INCLUDE_DIR}"
207 IMPORTED_LINK_INTERFACE_LIBRARIES Threads::Threads)
208 endif()
209endif()
210
211if(HIDAPI_libusb_FOUND AND NOT TARGET HIDAPI::hidapi-libusb)
212 add_library(HIDAPI::hidapi-libusb UNKNOWN IMPORTED)
213 set_target_properties(
214 HIDAPI::hidapi-libusb
215 PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C"
216 IMPORTED_LOCATION "${HIDAPI_LIBUSB_LIBRARY}"
217 INTERFACE_INCLUDE_DIRECTORIES "${HIDAPI_INCLUDE_DIR}"
218 IMPORTED_LINK_INTERFACE_LIBRARIES Threads::Threads)
219endif()
220
221if(HIDAPI_hidraw_FOUND AND NOT TARGET HIDAPI::hidapi-hidraw)
222 add_library(HIDAPI::hidapi-hidraw UNKNOWN IMPORTED)
223 set_target_properties(
224 HIDAPI::hidapi-hidraw
225 PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C"
226 IMPORTED_LOCATION "${HIDAPI_HIDRAW_LIBRARY}"
227 INTERFACE_INCLUDE_DIRECTORIES "${HIDAPI_INCLUDE_DIR}"
228 IMPORTED_LINK_INTERFACE_LIBRARIES Threads::Threads)
229endif()