Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix version detection in Findhidapi.cmake #4215

Merged
merged 5 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2605,9 +2605,8 @@ find_package(LibUSB)
# USB HID controller support
option(HID "USB HID controller support" ON)
if(HID)
find_package(hidapi)
# hidapi_VERSION is only available starting with 0.10.0
if(NOT hidapi_FOUND OR NOT hidapi_VERSION OR hidapi_VERSION VERSION_LESS 0.10.1)
find_package(hidapi 0.10.1)
if(NOT hidapi_FOUND)
message(STATUS "Linking internal libhidapi statically")
add_library(mixxx-hidapi STATIC EXCLUDE_FROM_ALL)
target_include_directories(mixxx-hidapi SYSTEM PUBLIC lib/hidapi/hidapi)
Expand All @@ -2630,10 +2629,6 @@ if(HID)
endif()
target_link_libraries(mixxx-lib PRIVATE mixxx-hidapi)
else()
message(STATUS "Linking libhidapi dynamically")
if(NOT HIDAPI_FOUND)
message(FATAL_ERROR "USB HID controller support requires libhidapi-libusb and its development headers.")
endif()
target_link_libraries(mixxx-lib PRIVATE hidapi::hidapi)
endif()
target_sources(mixxx-lib PRIVATE
Expand Down
43 changes: 25 additions & 18 deletions cmake/modules/Findhidapi.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,36 @@ find_library(hidapi_LIBRARY
)
mark_as_advanced(hidapi_LIBRARY)


# Version detection
if(DEFINED PC_hidapi_VERSION)
set(hidapi_VERSION "${PC_hidapi_VERSION}")
else()
if (EXISTS "${hidapi_INCLUDE_DIR}/hidapi.h")
file(READ "${hidapi_INCLUDE_DIR}/hidapi.h" hidapi_H_CONTENTS)
string(REGEX MATCH "#define HID_API_VERSION_MAJOR ([0-9]+)" _dummy "${hidapi_H_CONTENTS}")
set(hidapi_VERSION_MAJOR "${CMAKE_MATCH_1}")
string(REGEX MATCH "#define HID_API_VERSION_MINOR ([0-9]+)" _dummy "${hidapi_H_CONTENTS}")
set(hidapi_VERSION_MINOR "${CMAKE_MATCH_1}")
string(REGEX MATCH "#define HID_API_VERSION_PATCH ([0-9]+)" _dummy "${hidapi_H_CONTENTS}")
set(hidapi_VERSION_PATCH "${CMAKE_MATCH_1}")
# hidapi_VERSION is only available starting with 0.10.0
# Simply using if(NOT) does not work because 0 is a valid value, so compare to empty string.
if (NOT hidapi_VERSION_MAJOR STREQUAL "" AND
NOT hidapi_VERSION_MINOR STREQUAL "" AND
NOT hidapi_VERSION_PATCH STREQUAL "")
set(hidapi_VERSION "${hidapi_VERSION_MAJOR}.${hidapi_VERSION_MINOR}.${hidapi_VERSION_PATCH}")
endif()
endif()
endif ()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
hidapi
DEFAULT_MSG
hidapi_LIBRARY
hidapi_INCLUDE_DIR
REQUIRED_VARS hidapi_LIBRARY hidapi_INCLUDE_DIR
VERSION_VAR hidapi_VERSION
)

# Version detection
if (EXISTS "${hidapi_INCLUDE_DIR}/hidapi.h")
file(READ "${hidapi_INCLUDE_DIR}/hidapi.h" hidapi_H_CONTENTS)
string(REGEX MATCH "#define HID_API_VERSION_MAJOR ([0-9]+)" _dummy "${hidapi_H_CONTENTS}")
set(hidapi_VERSION_MAJOR "${CMAKE_MATCH_1}")
string(REGEX MATCH "#define HID_API_VERSION_MINOR ([0-9]+)" _dummy "${hidapi_H_CONTENTS}")
set(hidapi_VERSION_MINOR "${CMAKE_MATCH_1}")
string(REGEX MATCH "#define HID_API_VERSION_PATCH ([0-9]+)" _dummy "${hidapi_H_CONTENTS}")
set(hidapi_VERSION_PATCH "${CMAKE_MATCH_1}")
# hidapi_VERSION is only available starting with 0.10.0
if (hidapi_VERSION_MAJOR AND hidapi_VERSION_MINOR AND hidapi_VERSION_PATCH)
set(hidapi_VERSION "${hidapi_VERSION_MAJOR}.${hidapi_VERSION_MINOR}.${hidapi_VERSION_PATCH}")
endif()
endif ()

if(hidapi_FOUND)
set(hidapi_LIBRARIES "${hidapi_LIBRARY}")
set(hidapi_INCLUDE_DIRS "${hidapi_INCLUDE_DIR}")
Expand Down