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

CMake: fix detection of hidapi #4200

Closed
wants to merge 1 commit into from
Closed

Conversation

Be-ing
Copy link
Contributor

@Be-ing Be-ing commented Aug 14, 2021

Mixxx was building its vendored copy of hidapi even when the system had hidapi 0.10.1.

@github-actions github-actions bot added the build label Aug 14, 2021
CMakeLists.txt Show resolved Hide resolved
CMakeLists.txt Outdated Show resolved Hide resolved
@uklotzde
Copy link
Contributor

I suppose we are not able to resolve this issue in 2.3 without touching the macOS and Windows environments. Our include paths are wrong.

@Be-ing Be-ing force-pushed the hidapi_system branch 4 times, most recently from ea9ebb7 to beda3b3 Compare August 15, 2021 05:01
Copy link
Contributor

@uklotzde uklotzde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This went smoother than expected.

Copy link
Member

@daschuer daschuer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the version detection in findhidapi.cmake requires an update to fix the issue.

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was wrong with this expression above?
Find_package(hidapi 0.10.1) succeeds on Ubuntu Bionic, because it is no version number set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oof, why does CMake let this succeed...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed this by adding hidapi_VERSION to the find_package_handle_standard_args call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the error makes it clear what is happening:

-- Could NOT find hidapi (missing: hidapi_VERSION) (Required is at least version "0.10.1")
-- Linking internal libhidapi statically

@Be-ing Be-ing requested a review from daschuer August 15, 2021 15:16
@daschuer
Copy link
Member

Now it prints:

-- Could NOT find hidapi (missing: hidapi_VERSION) 

which is misleading. There is a Version: 0.9.0 entry in the pc file. Cant we read that?

@Be-ing
Copy link
Contributor Author

Be-ing commented Aug 15, 2021

I think it is good enough how it is. It prints the required version already.

# 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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not sure if the header is in the /hidapi sub-directory.

I am sure the original code was working when it has been written by @uklotzde here:
#3730

So for a reliable version detection we need to use the
Cflags: -I${includedir}/hidapi
Info.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not sure if the header is in the /hidapi sub-directory.

Yes we are. This is easily verifiable by looking up the list of files installed by packages. Here is the libhidapi-dev package from Ubuntu 18.04:
https://packages.ubuntu.com/bionic/amd64/libhidapi-dev/filelist

It installs /usr/include/hidapi/hidapi.h.

I am not sure this code ever worked before.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm that for Ubuntu Groovy. Maybe @uklotzde can explain why the code was written like that. Is it different with fedora?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is the same on Fedora:

❯ rpm -ql hidapi-devel
/usr/include/hidapi
/usr/include/hidapi/hidapi.h
/usr/lib64/libhidapi-hidraw.so
/usr/lib64/libhidapi-libusb.so
/usr/lib64/pkgconfig/hidapi-hidraw.pc
/usr/lib64/pkgconfig/hidapi-libusb.pc

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same on Arch:

$ yay -Ql hidapi | grep -F hidapi.h
hidapi /usr/include/hidapi/hidapi.h

@daschuer
Copy link
Member

Why don't we just use PC_hidapi_VERSION?

@daschuer
Copy link
Member

I think it is good enough how it is. It prints the required version already.

No, sorry. "Could NOT find hidapi" is wrong and misleading.

@Be-ing
Copy link
Contributor Author

Be-ing commented Aug 15, 2021

I don't really care about perfect error messages for outdated operating systems. It still works and it gives a hint what is wrong:

Required is at least version "0.10.1"

@uklotzde
Copy link
Contributor

@daschuer If you need better error messages for an issue that will disappear in the future and that probably affects only a minority of developers please take care of this later.

@daschuer
Copy link
Member

What is the reason not to use "PC_hidapi_VERSION" that the error message would be reasonable by default.

@Be-ing
Copy link
Contributor Author

Be-ing commented Aug 15, 2021

That requires rewriting more code to handle the case where there is no pkgconfig.

@daschuer
Copy link
Member

We have it on all out targets. At least use the "PC_hidapi_VERSION" and fall back to reading the header file, with the code guessing the content of the pc file.

@Be-ing
Copy link
Contributor Author

Be-ing commented Aug 16, 2021

@daschuer you can improve the error message for outdated operating systems if you want after merging this but I don't think there's any reason not to merge this now. I have a million other things to do.

@daschuer
Copy link
Member

This can instantly merged, if you revert the changes in the CMakeList.txt, and making hidapi_VERSION not a requirement in findhidapi.cmake.
These changes are unnecessary and are leading to the noted issues.

The required bugfix is just to look into the hidapi sub folder, that's all and sufficient for the fix.

@Be-ing Be-ing closed this Aug 16, 2021
@Be-ing
Copy link
Contributor Author

Be-ing commented Aug 16, 2021

I don't have energy for this bikeshedding.

@uklotzde
Copy link
Contributor

@daschuer Instead of starting a discussion it would have been more helpful to simply provide the changes you deem necessary. Then we could test if it still works for us.

@daschuer
Copy link
Member

@uklotzde It is no problem for me to issue an alternative solution that makes use of the PC_hidapi_VERSION or just a simply fix of the header path. But for my understanding it is the normal way in a review to discuss things with the author. In this case l have discovered that he original PR was not working at all. Than I have clearly pointed out the later fix introduced an unecessary regression and suggested alternatives. It feels bad to being blamed for bikeshedding because of this.

Since @Be-ing is stepped back from this let's discuss what would be a merge-able. Should I make use of PC_hidapi_VERSION or is it preferred to just fix the include path based on your original solution?

@uklotzde
Copy link
Contributor

uklotzde commented Aug 16, 2021

@daschuer From what I understood the outcome is expected, i.e. the bundled/vendored version is used if the system version is too old and detection fails. The exact error message is a minor detail if it works.

@uklotzde uklotzde reopened this Aug 16, 2021
@uklotzde
Copy link
Contributor

Why do we need to discuss this if the CI builds demonstrate that there are no actual regressions? This PR fixes the include paths and dynamic linking for all Linux distributions except Ubuntu LTS.

What did I miss?

@daschuer
Copy link
Member

The exact error message is a minor detail if it works.

Yes, but we have a solution in the 2.3 branch that has not the wrong error message. This solution was removed here along with the actual fix. I am happy to issue an alternative PR without this regression. I just need a conses before I start to work, because I have no interest to put work into a third solution.

@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is unrelated to the fix and can be reverted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not unrelated. find_package(hidapi 0.10.1) creates the error message hinting the version is too old.

DEFAULT_MSG
hidapi_LIBRARY
hidapi_INCLUDE_DIR
hidapi_VERSION
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the hidapi_VERSION version required to "find" the package which is an issue because the code above does not work for versions < 0.10.0. We can make it work by using the version from the PC file or just stick with the original solution.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please give a hint what you prefer and I will prepare a PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intentional. We require 0.10.1, so if there is no hidapi_VERSION it is too old anyway.

@uklotzde
Copy link
Contributor

Before: The code was silently broken
After: The code is fixed with slight inconveniences for some special cases

Why do we need to pile up more work on top of it? Changing 1 or 2 lines would be ok, if it is as easy as claimed. But anything else could and should be done later.

@daschuer
Copy link
Member

daschuer commented Aug 17, 2021

I was only asking for reverting a few lines, that's all.

Let's move one and consider a solution ..

After looking deeper into it I think the Findhidapi.cmake file is still broken.
It aims to work with and without a PC file available, but now the path without PC file will return the wrong include directory
just "/usr/include" instead" of "/usr/include/hidapi". It still woks in our case, because we use the cflags from the PC file, which comes with the correct include derective.

Do we know why using PATH_SUFFIXES was broken in the first place? It works on Linux.

@daschuer
Copy link
Member

It was not broken, #4215 fixes all issues.

@Be-ing
Copy link
Contributor Author

Be-ing commented Aug 17, 2021

just "/usr/include" instead" of "/usr/include/hidapi"

/usr/include is correct. The file is at <hidapi/hidapi.h> relative to the installation prefix.

@Holzhaus
Copy link
Member

just "/usr/include" instead" of "/usr/include/hidapi"

/usr/include is correct. The file is at <hidapi/hidapi.h> relative to the installation prefix.

Nope:

$ grep -HrnF hidapi.h src
src/controllers/hid/hidcontroller.cpp:3:#include <hidapi.h>
src/controllers/hid/hidenumerator.cpp:3:#include <hidapi.h>
src/controllers/hid/hiddevice.h:3:#include <hidapi.h>

@Be-ing
Copy link
Contributor Author

Be-ing commented Aug 17, 2021

@Holzhaus yes that is what I mean. The #include paths in our C++ code were wrong. I fixed that in this PR.

@Holzhaus
Copy link
Member

This is not what the hidapi README suggests: https://github.com/libusb/hidapi#what-does-the-api-look-like

@Be-ing
Copy link
Contributor Author

Be-ing commented Aug 17, 2021

Well then the documentation is misleading. hidapi's build system has always installed the header to <hidapi/hidapi.h> relative to the installation prefix.

@Be-ing
Copy link
Contributor Author

Be-ing commented Aug 17, 2021

Fix for the hidapi documentation: libusb/hidapi#323

@Be-ing
Copy link
Contributor Author

Be-ing commented Aug 17, 2021

I have already implemented a fully working solution. It does not create a welcoming environment to have every unimportant detail nitpicked to shreds then reimplemented in a worse way that does not solve all the problems, and makes the minuscule imperfections that were raised worse.

@Be-ing
Copy link
Contributor Author

Be-ing commented Aug 17, 2021

The pkgconfig file says the #include path is /usr/include:

 ❯ cat /usr/lib64/pkgconfig/hidapi-libusb.pc
prefix=/usr
exec_prefix=/usr
libdir=/usr/lib64
includedir=/usr/include

Name: hidapi-libusb
Description: C Library for USB HID device access from Linux, Mac OS X, FreeBSD, and Windows. This is the libusb implementation.
Version: 0.10.1
Libs: -L${libdir} -lhidapi-libusb
Cflags: -I${includedir}/hidapi

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

@Be-ing
Copy link
Contributor Author

Be-ing commented Aug 17, 2021

Oh, wait the pkgconfig file is doing weird stuff:

includedir=/usr/include

That is normal.

Cflags: -I${includedir}/hidapi

That I did not expect.

@uklotzde
Copy link
Contributor

uklotzde commented Aug 17, 2021

The example in test.c doesn't even show how hidapi.h is supposed to be included from a system path:

#include "hidapi.h"

This is NOT how a consumer should include an external library.

@Holzhaus
Copy link
Member

Holzhaus commented Aug 17, 2021

I think #include <hidapi.h> is the standard. Looking at some other prominent projects, they use that, too:

@@ -1,6 +1,6 @@
#pragma once

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not correct
as per HIDAPI design, you should only include <hidapi.h>

@Be-ing
Copy link
Contributor Author

Be-ing commented Aug 17, 2021

I reverted the change to the #include path.

@Be-ing Be-ing closed this Aug 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants