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-2021 Rylie Pavlik <rylie.pavlik@collabora.com> <rylie@ryliepavlik.com>
44# https://ryliepavlik.com/
45#
46# Copyright 2009-2010, Iowa State University
47# Copyright 2019-2021, Collabora, Ltd.
48#
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#
53# SPDX-License-Identifier: BSL-1.0
54
55set(HIDAPI_ROOT_DIR
56 "${HIDAPI_ROOT_DIR}"
57 CACHE PATH "Root to search for HIDAPI")
58
59# Clean up components
60if(HIDAPI_FIND_COMPONENTS)
61 if(WIN32 OR APPLE)
62 # This makes no sense on Windows or Mac, which have native APIs
63 list(REMOVE HIDAPI_FIND_COMPONENTS libusb)
64 endif()
65
66 if(NOT ${CMAKE_SYSTEM} MATCHES "Linux")
67 # hidraw is only on linux
68 list(REMOVE HIDAPI_FIND_COMPONENTS hidraw)
69 endif()
70endif()
71if(NOT HIDAPI_FIND_COMPONENTS)
72 # Default to any
73 set(HIDAPI_FIND_COMPONENTS any)
74endif()
75
76# Ask pkg-config for hints
77if(NOT ANDROID)
78 find_package(PkgConfig QUIET)
79 if(PKG_CONFIG_FOUND)
80 set(_old_prefix_path "${CMAKE_PREFIX_PATH}")
81 # So pkg-config uses HIDAPI_ROOT_DIR too.
82 if(HIDAPI_ROOT_DIR)
83 list(APPEND CMAKE_PREFIX_PATH ${HIDAPI_ROOT_DIR})
84 endif()
85 pkg_check_modules(PC_HIDAPI_LIBUSB QUIET hidapi-libusb)
86 pkg_check_modules(PC_HIDAPI_HIDRAW QUIET hidapi-hidraw)
87 # Restore
88 set(CMAKE_PREFIX_PATH "${_old_prefix_path}")
89 endif()
90endif()
91
92# Actually search
93find_library(
94 HIDAPI_UNDECORATED_LIBRARY
95 NAMES hidapi
96 PATHS "${HIDAPI_ROOT_DIR}"
97 PATH_SUFFIXES lib)
98
99find_library(
100 HIDAPI_LIBUSB_LIBRARY
101 NAMES hidapi hidapi-libusb
102 PATHS "${HIDAPI_ROOT_DIR}"
103 PATH_SUFFIXES lib
104 HINTS ${PC_HIDAPI_LIBUSB_LIBRARY_DIRS})
105
106if(CMAKE_SYSTEM MATCHES "Linux")
107 find_library(
108 HIDAPI_HIDRAW_LIBRARY
109 NAMES hidapi-hidraw
110 HINTS ${PC_HIDAPI_HIDRAW_LIBRARY_DIRS})
111endif()
112
113find_path(
114 HIDAPI_INCLUDE_DIR
115 NAMES hidapi.h
116 PATHS "${HIDAPI_ROOT_DIR}"
117 PATH_SUFFIXES hidapi include include/hidapi
118 HINTS ${PC_HIDAPI_HIDRAW_INCLUDE_DIRS} ${PC_HIDAPI_LIBUSB_INCLUDE_DIRS})
119
120find_package(Threads QUIET)
121
122###
123# Compute the "I don't care which backend" library
124###
125set(HIDAPI_LIBRARY)
126
127# First, try to use a preferred backend if supplied
128if("${HIDAPI_FIND_COMPONENTS}" MATCHES "libusb"
129 AND HIDAPI_LIBUSB_LIBRARY
130 AND NOT HIDAPI_LIBRARY)
131 set(HIDAPI_LIBRARY ${HIDAPI_LIBUSB_LIBRARY})
132endif()
133if("${HIDAPI_FIND_COMPONENTS}" MATCHES "hidraw"
134 AND HIDAPI_HIDRAW_LIBRARY
135 AND NOT HIDAPI_LIBRARY)
136 set(HIDAPI_LIBRARY ${HIDAPI_HIDRAW_LIBRARY})
137endif()
138
139# Then, if we don't have a preferred one, settle for anything.
140if(NOT HIDAPI_LIBRARY)
141 if(HIDAPI_LIBUSB_LIBRARY)
142 set(HIDAPI_LIBRARY ${HIDAPI_LIBUSB_LIBRARY})
143 elseif(HIDAPI_HIDRAW_LIBRARY)
144 set(HIDAPI_LIBRARY ${HIDAPI_HIDRAW_LIBRARY})
145 elseif(HIDAPI_UNDECORATED_LIBRARY)
146 set(HIDAPI_LIBRARY ${HIDAPI_UNDECORATED_LIBRARY})
147 endif()
148endif()
149
150###
151# Determine if the various requested components are found.
152###
153set(_hidapi_component_required_vars)
154
155foreach(_comp IN LISTS HIDAPI_FIND_COMPONENTS)
156 if("${_comp}" STREQUAL "any")
157 list(APPEND _hidapi_component_required_vars HIDAPI_INCLUDE_DIR
158 HIDAPI_LIBRARY)
159 if(HIDAPI_INCLUDE_DIR AND EXISTS "${HIDAPI_LIBRARY}")
160 set(HIDAPI_any_FOUND TRUE)
161 mark_as_advanced(HIDAPI_INCLUDE_DIR)
162 else()
163 set(HIDAPI_any_FOUND FALSE)
164 endif()
165
166 elseif("${_comp}" STREQUAL "libusb")
167 list(APPEND _hidapi_component_required_vars HIDAPI_INCLUDE_DIR
168 HIDAPI_LIBUSB_LIBRARY)
169 if(HIDAPI_INCLUDE_DIR AND EXISTS "${HIDAPI_LIBUSB_LIBRARY}")
170 set(HIDAPI_libusb_FOUND TRUE)
171 mark_as_advanced(HIDAPI_INCLUDE_DIR HIDAPI_LIBUSB_LIBRARY)
172 else()
173 set(HIDAPI_libusb_FOUND FALSE)
174 endif()
175
176 elseif("${_comp}" STREQUAL "hidraw")
177 list(APPEND _hidapi_component_required_vars HIDAPI_INCLUDE_DIR
178 HIDAPI_HIDRAW_LIBRARY)
179 if(HIDAPI_INCLUDE_DIR AND EXISTS "${HIDAPI_HIDRAW_LIBRARY}")
180 set(HIDAPI_hidraw_FOUND TRUE)
181 mark_as_advanced(HIDAPI_INCLUDE_DIR HIDAPI_HIDRAW_LIBRARY)
182 else()
183 set(HIDAPI_hidraw_FOUND FALSE)
184 endif()
185
186 else()
187 message(WARNING "${_comp} is not a recognized HIDAPI component")
188 set(HIDAPI_${_comp}_FOUND FALSE)
189 endif()
190endforeach()
191unset(_comp)
192
193###
194# FPHSA call
195###
196include(FindPackageHandleStandardArgs)
197find_package_handle_standard_args(
198 HIDAPI
199 REQUIRED_VARS ${_hidapi_component_required_vars} THREADS_FOUND
200 HANDLE_COMPONENTS)
201if(HIDAPI_FOUND)
202 set(HIDAPI_LIBRARIES "${HIDAPI_LIBRARY}")
203 set(HIDAPI_INCLUDE_DIRS "${HIDAPI_INCLUDE_DIR}")
204 if(NOT TARGET HIDAPI::hidapi)
205 add_library(HIDAPI::hidapi UNKNOWN IMPORTED)
206 set_target_properties(
207 HIDAPI::hidapi
208 PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C"
209 IMPORTED_LOCATION "${HIDAPI_LIBRARY}"
210 INTERFACE_INCLUDE_DIRECTORIES "${HIDAPI_INCLUDE_DIR}"
211 IMPORTED_LINK_INTERFACE_LIBRARIES Threads::Threads)
212 endif()
213endif()
214
215if(HIDAPI_libusb_FOUND AND NOT TARGET HIDAPI::hidapi-libusb)
216 add_library(HIDAPI::hidapi-libusb UNKNOWN IMPORTED)
217 set_target_properties(
218 HIDAPI::hidapi-libusb
219 PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C"
220 IMPORTED_LOCATION "${HIDAPI_LIBUSB_LIBRARY}"
221 INTERFACE_INCLUDE_DIRECTORIES "${HIDAPI_INCLUDE_DIR}"
222 IMPORTED_LINK_INTERFACE_LIBRARIES Threads::Threads)
223endif()
224
225if(HIDAPI_hidraw_FOUND AND NOT TARGET HIDAPI::hidapi-hidraw)
226 add_library(HIDAPI::hidapi-hidraw UNKNOWN IMPORTED)
227 set_target_properties(
228 HIDAPI::hidapi-hidraw
229 PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C"
230 IMPORTED_LOCATION "${HIDAPI_HIDRAW_LIBRARY}"
231 INTERFACE_INCLUDE_DIRECTORIES "${HIDAPI_INCLUDE_DIR}"
232 IMPORTED_LINK_INTERFACE_LIBRARIES Threads::Threads)
233endif()