Skip to content

Dynamically load libudev in linux-hidraw backend#788

Open
falkTX wants to merge 1 commit intolibusb:masterfrom
falkTX:linux-dynamic-libudev
Open

Dynamically load libudev in linux-hidraw backend#788
falkTX wants to merge 1 commit intolibusb:masterfrom
falkTX:linux-dynamic-libudev

Conversation

@falkTX
Copy link
Copy Markdown

@falkTX falkTX commented Apr 24, 2026

This PR allows applications using hidapi with linux-hidraw backend to not have any direct dependencies, making binaries more portable and easier to build.

For the loader file I used the same license as libudev (LGPL-2.1-or-later), and copied their way of doing the license header too.
The file is included in-place, just to keep the diff as small as possible.

@mcuee mcuee added the hidraw Related to Linux/hidraw backend label Apr 24, 2026
@mcuee
Copy link
Copy Markdown
Member

mcuee commented Apr 24, 2026

The build failed under Ubuntu and Arch Linux.

@falkTX falkTX force-pushed the linux-dynamic-libudev branch 2 times, most recently from b86c642 to de4b582 Compare April 24, 2026 10:46
@falkTX
Copy link
Copy Markdown
Author

falkTX commented Apr 24, 2026

The build failed under Ubuntu and Arch Linux.

Related to more strict warnings, likely fixed now.

Signed-off-by: falkTX <falktx@falktx.com>
@mcuee
Copy link
Copy Markdown
Member

mcuee commented Apr 24, 2026

The build failed under Ubuntu and Arch Linux.

Related to more strict warnings, likely fixed now.

Not yet...

@falkTX falkTX force-pushed the linux-dynamic-libudev branch from de4b582 to 8fc87a7 Compare April 24, 2026 12:25
@falkTX
Copy link
Copy Markdown
Author

falkTX commented Apr 24, 2026

The build failed under Ubuntu and Arch Linux.

Related to more strict warnings, likely fixed now.

Not yet...

I built locally with the same pedantic flags and pushed another try.

Copy link
Copy Markdown
Member

@Youw Youw left a comment

Choose a reason for hiding this comment

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

I don't mind the idea of having libudev loaded dynamically in runtime, but I can't agree that the current aproach is best for that. A few points I'd ask to change if I where to accept it this way:

  • licence shouldn't derive from libudev license - we don't copy-paste their code, and HIDAPI uses a bit different licencing sceme, and the new source that becomes a part of HIDAPI would potentially restrict its users;
  • including the .c file for this purpose looks a bit ugly, and the name libudev.c suggests that it actually is (i.e. implements) libudev, but in fact it is only a wrapper; what is best instead - have a hidapi_libudev_wrap.h, and all functions inside should be either static or inline;
  • the C() macro... I checked - it is a valid C, but the fact that I had to go and double-check suggests that it looks like something that might break; HIDAPI_UDEV_RESOLVE wouldn't raise any naming suspicious
  • RTLD_GLOBAL - that is a code smell to use it here, where passing the _lib (or rather udev_lib_handle to dlsym) explicitly is what I'd expect
  • no dlclose which is technically a resource leak if HIDAPI itself is ever to be dynamically loaded

At the end of the day - I'd be much happier to see a more common way of dynamically loading a library during the initialisation, saving and using function pointers explicitly and unloading a library during the hid_exit, e.g. like we did it for windows backend.

Yes that would cause a lot more changes in linux/hid.c - but that's absolutely fine.

Comment thread linux/CMakeLists.txt
find_package(Threads REQUIRED)

include(FindPkgConfig)
pkg_check_modules(libudev REQUIRED IMPORTED_TARGET libudev)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

the documentation and CI scripts also needs to be updated - now the libudev-dev no longer required and only the libudev1 is needed

@falkTX
Copy link
Copy Markdown
Author

falkTX commented Apr 24, 2026

That is all very fair, I mostly did this change as a way to get my cross-compilation builds working and decided to push it upstream because why not.

You have good points for something that would be integrated officially in hidapi, but sorry to say my needs are covered already as-is, the steps you mention make sense but sound like it would be quite a bit more work than I am willing to spend just for a small feature.

If you think the feature is worthwhile, take all the code here to adapt freely under any license, I give written consent.
This is as far as I go though, perhaps someone else can take over now.
Or otherwise feel free to close the PR too, I can understand that too.

@Youw Youw added build system/CI Anything related to building the project or running on CI Contributions Welcome Any external help is welcome don't_merge Don't merge this PR as is labels Apr 25, 2026
@Youw
Copy link
Copy Markdown
Member

Youw commented Apr 25, 2026

Thanks for the contribution.

I'll leave it be for now, in case anyone wants to catch up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build system/CI Anything related to building the project or running on CI Contributions Welcome Any external help is welcome don't_merge Don't merge this PR as is hidraw Related to Linux/hidraw backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants