Skip to content

Navigation Menu

Sign in
Sign up

FYI, it looks like your cmake script does not allow for easy building of hidapi with libusb. #682

Unanswered
Youw asked this question in Q&A
Discussion options

Originally posted by @madebr in #681 (comment) :

For this, SDL uses a FindLibUSB.cmake module and a GitHub prebuilt release to provide binaries

You must be logged in to vote

Replies: 4 comments 5 replies

Comment options

Youw
Jul 11, 2024
Maintainer Author

FYI, it looks like your cmake script does not allow for easy building of hidapi with libusb.

Define what do you expect to be easy.
I'm using libusb-cmake and because of this (need add_subdirectory(libusb) before add_subdirectory(hidapi) ) - it is quite trivial.

You must be logged in to vote
0 replies
Comment options

Youw
Jul 11, 2024
Maintainer Author

Originally posted by @madebr in #681 (comment)

Define what do you expect to be easy.

By easy, I mean the cmake script does need no modifications and can build with libusb support (using a system library) without needing to building libusb yourself.

hidapi/CMakeLists.txt

Lines 40 to 48 in 4578ea2

elseif(NOT WIN32)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
option(HIDAPI_WITH_HIDRAW "Build HIDRAW-based implementation of HIDAPI" ON)
option(HIDAPI_WITH_LIBUSB "Build LIBUSB-based implementation of HIDAPI" ON)
endif()
if(CMAKE_SYSTEM_NAME MATCHES "NetBSD")
option(HIDAPI_WITH_NETBSD "Build NetBSD/UHID implementation of HIDAPI" ON)
endif()
endif()

hidapi/src/CMakeLists.txt

Lines 110 to 130 in 4578ea2

if(WIN32)
target_include_directories(hidapi_include INTERFACE
"$<BUILD_INTERFACE:${PROJECT_ROOT}/windows>"
)
add_subdirectory("${PROJECT_ROOT}/windows" windows)
set(EXPORT_ALIAS winapi)
list(APPEND EXPORT_COMPONENTS winapi)
elseif(APPLE)
target_include_directories(hidapi_include INTERFACE
"$<BUILD_INTERFACE:${PROJECT_ROOT}/mac>"
)
add_subdirectory("${PROJECT_ROOT}/mac" mac)
set(EXPORT_ALIAS darwin)
list(APPEND EXPORT_COMPONENTS darwin)
if(NOT BUILD_SHARED_LIBS)
set(HIDAPI_NEED_EXPORT_THREADS TRUE)
endif()
else()
if(NOT DEFINED HIDAPI_WITH_LIBUSB)
set(HIDAPI_WITH_LIBUSB ON)
endif()

HIDAPI_WITH_LIBUSB is not used on Windows

I'm using libusb-cmake and because of this (need add_subdirectory(libusb) before add_subdirectory(hidapi) ) - it is quite trivial.

It works, but you're effectively using a fork and do not use upstream.
For SDL's use case, we don't want this hard dependency: users should bring their own libusb.
This is also valid for every (mingw) distribution (e.g. msys2)

One more thing - do you thing we could update HIDAPI CI script to catch this/similar issues in the future?

Looking at SDL's cmake script, we've added a SDL_WERROR cmake option to build with -Werror//WX (default off, enabled on ci).
Recent CMake versions have a cmake property for this: configure with -DCMAKE_COMPILER_WARNING_AS_ERROR=1.
We don't pass a special MSVC warning flag.

You must be logged in to vote
0 replies
Comment options

Youw
Jul 11, 2024
Maintainer Author

By easy, I mean the cmake script does need no modifications and can build with libusb support (using a system library) without needing to building libusb yourself.

HIDAPI_WITH_LIBUSB is not used on Windows

This is also valid for every (mingw) distribution (e.g. msys2)

libusb backend for HIDAPI officially supported only on a number of platforms but Windows is not one of them. If you happen to use libusb backend of HIDAPI on Windows - you must be using modified or your own build script for HIDAPI, which is not official distribution as of yet.

On those supported platforms, libusb officially distributed in a way that pkg-config should be used to find it, and this part of HIDAPI CMake script handles it, at least according to my testing. Are you saying it doesn't work in your environemnt?

Looking at SDL's cmake script, we've added a SDL_WERROR cmake option to build with -Werror//WX (default off, enabled on ci).

We have /WX on our CI too, so I wounder why it wasn't caught...

You must be logged in to vote
5 replies
Comment options

On those supported platforms, libusb officially distributed in a way that pkg-config should be used to find it, and this part of HIDAPI CMake script handles it, at least according to my testing. Are you saying it doesn't work in your environemnt?

pkg-config is a unix-ism, and is not commonly installed on MSVC development systems.
The "CMake way" is to use modules.
(SDL's FindLibUSB.cmake uses pkg-config as a hint)
I know there's pkgconf, which works well on Windows, but building with MSVC in autoconf in a bash shell is an adventure on itself.
But again, the "official" libusb MSVC binaries don't contain pc files so pkgconf won't find anything.

Comment options

Youw Jul 11, 2024
Maintainer Author

Again, libusb backend is not supported by HIDAPI on Windows, specially with MSVC. What is your use-case?

Comment options

Oh, SDL3 allows me to configure and build in this configuration.
I assumed this was a working combination.

Comment options

Youw Jul 11, 2024
Maintainer Author

Maybe it is working in some environments, but it is not officially supported by HIDAPI build system or sources (yet).

Comment options

I think SDL heavily patched hidapi. That's why there lives a fork inside SDL.

Comment options

I caught this on a local MSVC instance, which apparently warned about this.
I don't know how to reproduce the warning in a fresh project (apart from adding /W3).
On gcc, you can warn with -Wconversion and -Wfloat-conversion.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /