Skip to content

Commit

Permalink
CMake: fix detection of hidapi and #include paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Be-ing committed Aug 15, 2021
1 parent 05016f8 commit a6bb14e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2605,12 +2605,12 @@ 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)
target_include_directories(mixxx-hidapi SYSTEM PRIVATE lib/hidapi/hidapi)
target_include_directories(mixxx-lib PRIVATE lib/hidapi)
if(WIN32)
target_sources(mixxx-hidapi PRIVATE lib/hidapi/windows/hid.c)
find_library(Setupapi_LIBRARY Setupapi REQUIRED)
Expand Down
31 changes: 17 additions & 14 deletions cmake/modules/Findhidapi.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ if(PkgConfig_FOUND)
endif()

find_path(hidapi_INCLUDE_DIR
NAMES hidapi.h
NAMES hidapi/hidapi.h
PATHS ${PC_hidapi_INCLUDE_DIRS}
PATH_SUFFIXES hidapi
DOC "hidapi include directory")
mark_as_advanced(hidapi_INCLUDE_DIR)

Expand All @@ -62,28 +61,32 @@ find_library(hidapi_LIBRARY
)
mark_as_advanced(hidapi_LIBRARY)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
hidapi
DEFAULT_MSG
hidapi_LIBRARY
hidapi_INCLUDE_DIR
)

# Version detection
if (EXISTS "${hidapi_INCLUDE_DIR}/hidapi.h")
file(READ "${hidapi_INCLUDE_DIR}/hidapi.h" hidapi_H_CONTENTS)
if (EXISTS "${hidapi_INCLUDE_DIR}/hidapi/hidapi.h")
file(READ "${hidapi_INCLUDE_DIR}/hidapi/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)
# 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
hidapi_VERSION
)

if(hidapi_FOUND)
set(hidapi_LIBRARIES "${hidapi_LIBRARY}")
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/hid/hidcontroller.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <hidapi.h>
#include <hidapi/hidapi.h>

#include <QAtomicInt>

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/hid/hidenumerator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "controllers/hid/hidenumerator.h"

#include <hidapi.h>
#include <hidapi/hidapi.h>

#include "controllers/hid/hidcontroller.h"
#include "controllers/hid/hiddenylist.h"
Expand Down

0 comments on commit a6bb14e

Please sign in to comment.