2019-04-08 16:24:48 +00:00
|
|
|
#.rst:
|
|
|
|
# FindHIDAPI
|
|
|
|
# ----------
|
2020-08-05 19:13:00 +00:00
|
|
|
#
|
2019-04-08 16:24:48 +00:00
|
|
|
# Try to find HIDAPI library, from http://www.signal11.us/oss/hidapi/
|
2019-03-18 05:52:32 +00:00
|
|
|
#
|
|
|
|
# Cache Variables: (probably not for direct use in your scripts)
|
|
|
|
# HIDAPI_INCLUDE_DIR
|
|
|
|
# HIDAPI_LIBRARY
|
|
|
|
#
|
|
|
|
# Non-cache variables you might use in your CMakeLists.txt:
|
|
|
|
# HIDAPI_FOUND
|
|
|
|
# HIDAPI_INCLUDE_DIRS
|
|
|
|
# HIDAPI_LIBRARIES
|
|
|
|
#
|
2019-04-08 16:24:48 +00:00
|
|
|
# COMPONENTS
|
|
|
|
# ^^^^^^^^^^
|
|
|
|
#
|
|
|
|
# This module respects several COMPONENTS specifying the backend you prefer:
|
|
|
|
# ``any`` (the default), ``libusb``, and ``hidraw``.
|
|
|
|
# The availablility of the latter two depends on your platform.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# IMPORTED Targets
|
|
|
|
# ^^^^^^^^^^^^^^^^
|
2020-08-05 19:13:00 +00:00
|
|
|
#
|
2019-04-08 16:24:48 +00:00
|
|
|
# This module defines :prop_tgt:`IMPORTED` target ``HIDAPI::hidapi`` (in all cases or
|
|
|
|
# if no components specified), ``HIDAPI::hidapi-libusb`` (if you requested the libusb component),
|
|
|
|
# and ``HIDAPI::hidapi-hidraw`` (if you requested the hidraw component),
|
|
|
|
#
|
|
|
|
# Result Variables
|
|
|
|
# ^^^^^^^^^^^^^^^^
|
|
|
|
#
|
|
|
|
# ``HIDAPI_FOUND``
|
|
|
|
# True if HIDAPI or the requested components (if any) were found.
|
|
|
|
#
|
|
|
|
# We recommend using the imported targets instead of the following.
|
|
|
|
#
|
|
|
|
# ``HIDAPI_INCLUDE_DIRS``
|
|
|
|
# ``HIDAPI_LIBRARIES``
|
2019-03-18 05:52:32 +00:00
|
|
|
#
|
|
|
|
# Original Author:
|
2023-11-15 17:16:49 +00:00
|
|
|
# 2009-2021 Ryan Pavlik <ryan.pavlik@collabora.com> <abiryan@ryand.net>
|
2019-03-18 05:52:32 +00:00
|
|
|
# http://academic.cleardefinition.com
|
|
|
|
#
|
2023-11-15 17:16:49 +00:00
|
|
|
# Copyright 2009-2010, Iowa State University
|
|
|
|
# Copyright 2019-2021, Collabora, Ltd.
|
|
|
|
#
|
2020-03-18 16:42:27 +00:00
|
|
|
# SPDX-License-Identifier: BSL-1.0
|
2023-11-15 17:16:49 +00:00
|
|
|
#
|
2019-03-18 05:52:32 +00:00
|
|
|
# Distributed under the Boost Software License, Version 1.0.
|
|
|
|
# (See accompanying file LICENSE_1_0.txt or copy at
|
|
|
|
# http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
2020-08-05 19:13:00 +00:00
|
|
|
set(HIDAPI_ROOT_DIR
|
|
|
|
"${HIDAPI_ROOT_DIR}"
|
|
|
|
CACHE PATH "Root to search for HIDAPI")
|
2019-04-08 16:24:48 +00:00
|
|
|
|
|
|
|
# Clean up components
|
2020-03-03 20:56:22 +00:00
|
|
|
if(HIDAPI_FIND_COMPONENTS)
|
2020-08-05 19:13:00 +00:00
|
|
|
if(WIN32 OR APPLE)
|
|
|
|
# This makes no sense on Windows or Mac, which have native APIs
|
|
|
|
list(REMOVE HIDAPI_FIND_COMPONENTS libusb)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if(NOT ${CMAKE_SYSTEM} MATCHES "Linux")
|
|
|
|
# hidraw is only on linux
|
|
|
|
list(REMOVE HIDAPI_FIND_COMPONENTS hidraw)
|
|
|
|
endif()
|
2019-04-08 16:24:48 +00:00
|
|
|
endif()
|
2020-03-03 20:56:22 +00:00
|
|
|
if(NOT HIDAPI_FIND_COMPONENTS)
|
2020-08-05 19:13:00 +00:00
|
|
|
# Default to any
|
|
|
|
set(HIDAPI_FIND_COMPONENTS any)
|
2019-04-08 16:24:48 +00:00
|
|
|
endif()
|
|
|
|
|
|
|
|
# Ask pkg-config for hints
|
2021-05-06 14:57:16 +00:00
|
|
|
if(NOT ANDROID)
|
|
|
|
find_package(PkgConfig QUIET)
|
|
|
|
if(PKG_CONFIG_FOUND)
|
|
|
|
set(_old_prefix_path "${CMAKE_PREFIX_PATH}")
|
|
|
|
# So pkg-config uses HIDAPI_ROOT_DIR too.
|
|
|
|
if(HIDAPI_ROOT_DIR)
|
|
|
|
list(APPEND CMAKE_PREFIX_PATH ${HIDAPI_ROOT_DIR})
|
|
|
|
endif()
|
|
|
|
pkg_check_modules(PC_HIDAPI_LIBUSB QUIET hidapi-libusb)
|
|
|
|
pkg_check_modules(PC_HIDAPI_HIDRAW QUIET hidapi-hidraw)
|
|
|
|
# Restore
|
|
|
|
set(CMAKE_PREFIX_PATH "${_old_prefix_path}")
|
2020-08-05 19:13:00 +00:00
|
|
|
endif()
|
2019-04-08 16:24:48 +00:00
|
|
|
endif()
|
|
|
|
|
|
|
|
# Actually search
|
2020-08-05 19:13:00 +00:00
|
|
|
find_library(
|
|
|
|
HIDAPI_UNDECORATED_LIBRARY
|
|
|
|
NAMES hidapi
|
|
|
|
PATHS "${HIDAPI_ROOT_DIR}"
|
|
|
|
PATH_SUFFIXES lib)
|
|
|
|
|
|
|
|
find_library(
|
|
|
|
HIDAPI_LIBUSB_LIBRARY
|
|
|
|
NAMES hidapi hidapi-libusb
|
|
|
|
PATHS "${HIDAPI_ROOT_DIR}"
|
|
|
|
PATH_SUFFIXES lib
|
|
|
|
HINTS ${PC_HIDAPI_LIBUSB_LIBRARY_DIRS})
|
2019-04-08 16:24:48 +00:00
|
|
|
|
|
|
|
if(CMAKE_SYSTEM MATCHES "Linux")
|
2020-08-05 19:13:00 +00:00
|
|
|
find_library(
|
|
|
|
HIDAPI_HIDRAW_LIBRARY
|
|
|
|
NAMES hidapi-hidraw
|
|
|
|
HINTS ${PC_HIDAPI_HIDRAW_LIBRARY_DIRS})
|
2019-04-08 16:24:48 +00:00
|
|
|
endif()
|
2019-03-18 05:52:32 +00:00
|
|
|
|
2020-08-05 19:13:00 +00:00
|
|
|
find_path(
|
|
|
|
HIDAPI_INCLUDE_DIR
|
|
|
|
NAMES hidapi.h
|
|
|
|
PATHS "${HIDAPI_ROOT_DIR}"
|
|
|
|
PATH_SUFFIXES hidapi include include/hidapi
|
|
|
|
HINTS ${PC_HIDAPI_HIDRAW_INCLUDE_DIRS} ${PC_HIDAPI_LIBUSB_INCLUDE_DIRS})
|
2019-04-08 16:24:48 +00:00
|
|
|
|
|
|
|
find_package(Threads QUIET)
|
|
|
|
|
|
|
|
###
|
|
|
|
# Compute the "I don't care which backend" library
|
|
|
|
###
|
|
|
|
set(HIDAPI_LIBRARY)
|
|
|
|
|
|
|
|
# First, try to use a preferred backend if supplied
|
2020-08-05 19:13:00 +00:00
|
|
|
if("${HIDAPI_FIND_COMPONENTS}" MATCHES "libusb"
|
|
|
|
AND HIDAPI_LIBUSB_LIBRARY
|
|
|
|
AND NOT HIDAPI_LIBRARY)
|
|
|
|
set(HIDAPI_LIBRARY ${HIDAPI_LIBUSB_LIBRARY})
|
2019-04-08 16:24:48 +00:00
|
|
|
endif()
|
2020-08-05 19:13:00 +00:00
|
|
|
if("${HIDAPI_FIND_COMPONENTS}" MATCHES "hidraw"
|
|
|
|
AND HIDAPI_HIDRAW_LIBRARY
|
|
|
|
AND NOT HIDAPI_LIBRARY)
|
|
|
|
set(HIDAPI_LIBRARY ${HIDAPI_HIDRAW_LIBRARY})
|
2019-04-08 16:24:48 +00:00
|
|
|
endif()
|
|
|
|
|
|
|
|
# Then, if we don't have a preferred one, settle for anything.
|
|
|
|
if(NOT HIDAPI_LIBRARY)
|
2020-08-05 19:13:00 +00:00
|
|
|
if(HIDAPI_LIBUSB_LIBRARY)
|
|
|
|
set(HIDAPI_LIBRARY ${HIDAPI_LIBUSB_LIBRARY})
|
|
|
|
elseif(HIDAPI_HIDRAW_LIBRARY)
|
|
|
|
set(HIDAPI_LIBRARY ${HIDAPI_HIDRAW_LIBRARY})
|
|
|
|
elseif(HIDAPI_UNDECORATED_LIBRARY)
|
|
|
|
set(HIDAPI_LIBRARY ${HIDAPI_UNDECORATED_LIBRARY})
|
|
|
|
endif()
|
2019-04-08 16:24:48 +00:00
|
|
|
endif()
|
2019-03-18 05:52:32 +00:00
|
|
|
|
2019-04-08 16:24:48 +00:00
|
|
|
###
|
|
|
|
# Determine if the various requested components are found.
|
|
|
|
###
|
|
|
|
set(_hidapi_component_required_vars)
|
2020-08-05 19:13:00 +00:00
|
|
|
|
2019-04-08 16:24:48 +00:00
|
|
|
foreach(_comp IN LISTS HIDAPI_FIND_COMPONENTS)
|
2020-08-05 19:13:00 +00:00
|
|
|
if("${_comp}" STREQUAL "any")
|
|
|
|
list(APPEND _hidapi_component_required_vars HIDAPI_INCLUDE_DIR
|
|
|
|
HIDAPI_LIBRARY)
|
|
|
|
if(HIDAPI_INCLUDE_DIR AND EXISTS "${HIDAPI_LIBRARY}")
|
|
|
|
set(HIDAPI_any_FOUND TRUE)
|
|
|
|
mark_as_advanced(HIDAPI_INCLUDE_DIR)
|
|
|
|
else()
|
|
|
|
set(HIDAPI_any_FOUND FALSE)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
elseif("${_comp}" STREQUAL "libusb")
|
|
|
|
list(APPEND _hidapi_component_required_vars HIDAPI_INCLUDE_DIR
|
|
|
|
HIDAPI_LIBUSB_LIBRARY)
|
|
|
|
if(HIDAPI_INCLUDE_DIR AND EXISTS "${HIDAPI_LIBUSB_LIBRARY}")
|
|
|
|
set(HIDAPI_libusb_FOUND TRUE)
|
|
|
|
mark_as_advanced(HIDAPI_INCLUDE_DIR HIDAPI_LIBUSB_LIBRARY)
|
|
|
|
else()
|
|
|
|
set(HIDAPI_libusb_FOUND FALSE)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
elseif("${_comp}" STREQUAL "hidraw")
|
|
|
|
list(APPEND _hidapi_component_required_vars HIDAPI_INCLUDE_DIR
|
|
|
|
HIDAPI_HIDRAW_LIBRARY)
|
|
|
|
if(HIDAPI_INCLUDE_DIR AND EXISTS "${HIDAPI_HIDRAW_LIBRARY}")
|
|
|
|
set(HIDAPI_hidraw_FOUND TRUE)
|
|
|
|
mark_as_advanced(HIDAPI_INCLUDE_DIR HIDAPI_HIDRAW_LIBRARY)
|
|
|
|
else()
|
|
|
|
set(HIDAPI_hidraw_FOUND FALSE)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
else()
|
|
|
|
message(WARNING "${_comp} is not a recognized HIDAPI component")
|
|
|
|
set(HIDAPI_${_comp}_FOUND FALSE)
|
|
|
|
endif()
|
2019-04-08 16:24:48 +00:00
|
|
|
endforeach()
|
|
|
|
unset(_comp)
|
|
|
|
|
|
|
|
###
|
|
|
|
# FPHSA call
|
|
|
|
###
|
2019-03-18 05:52:32 +00:00
|
|
|
include(FindPackageHandleStandardArgs)
|
2020-08-05 19:13:00 +00:00
|
|
|
find_package_handle_standard_args(
|
|
|
|
HIDAPI
|
|
|
|
REQUIRED_VARS ${_hidapi_component_required_vars} THREADS_FOUND
|
|
|
|
HANDLE_COMPONENTS)
|
2019-03-18 05:52:32 +00:00
|
|
|
if(HIDAPI_FOUND)
|
2020-08-05 19:13:00 +00:00
|
|
|
set(HIDAPI_LIBRARIES "${HIDAPI_LIBRARY}")
|
|
|
|
set(HIDAPI_INCLUDE_DIRS "${HIDAPI_INCLUDE_DIR}")
|
|
|
|
if(NOT TARGET HIDAPI::hidapi)
|
|
|
|
add_library(HIDAPI::hidapi UNKNOWN IMPORTED)
|
|
|
|
set_target_properties(
|
|
|
|
HIDAPI::hidapi
|
|
|
|
PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C"
|
|
|
|
IMPORTED_LOCATION "${HIDAPI_LIBRARY}"
|
|
|
|
INTERFACE_INCLUDE_DIRECTORIES "${HIDAPI_INCLUDE_DIR}"
|
|
|
|
IMPORTED_LINK_INTERFACE_LIBRARIES Threads::Threads)
|
|
|
|
endif()
|
2019-04-08 16:24:48 +00:00
|
|
|
endif()
|
|
|
|
|
|
|
|
if(HIDAPI_libusb_FOUND AND NOT TARGET HIDAPI::hidapi-libusb)
|
2020-08-05 19:13:00 +00:00
|
|
|
add_library(HIDAPI::hidapi-libusb UNKNOWN IMPORTED)
|
|
|
|
set_target_properties(
|
|
|
|
HIDAPI::hidapi-libusb
|
|
|
|
PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C"
|
|
|
|
IMPORTED_LOCATION "${HIDAPI_LIBUSB_LIBRARY}"
|
|
|
|
INTERFACE_INCLUDE_DIRECTORIES "${HIDAPI_INCLUDE_DIR}"
|
|
|
|
IMPORTED_LINK_INTERFACE_LIBRARIES Threads::Threads)
|
2019-03-18 05:52:32 +00:00
|
|
|
endif()
|
|
|
|
|
2019-04-08 16:24:48 +00:00
|
|
|
if(HIDAPI_hidraw_FOUND AND NOT TARGET HIDAPI::hidapi-hidraw)
|
2020-08-05 19:13:00 +00:00
|
|
|
add_library(HIDAPI::hidapi-hidraw UNKNOWN IMPORTED)
|
|
|
|
set_target_properties(
|
|
|
|
HIDAPI::hidapi-hidraw
|
|
|
|
PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C"
|
|
|
|
IMPORTED_LOCATION "${HIDAPI_HIDRAW_LIBRARY}"
|
|
|
|
INTERFACE_INCLUDE_DIRECTORIES "${HIDAPI_INCLUDE_DIR}"
|
|
|
|
IMPORTED_LINK_INTERFACE_LIBRARIES Threads::Threads)
|
2019-04-08 16:24:48 +00:00
|
|
|
endif()
|