Dynamically load libudev in linux-hidraw backend #788
Labels
No labels
3rdparty
Android
API
Author does not respond
bsd
bug
build system/CI
ci-virtual-device
CMake
Contributions Welcome
Core
dependencies
device-specific
documentation
don't_merge
duplicate
enhancement
Example
Haiku
hidraw
hotplug
info required
invalid
libusb
macOS
Misc
Not an issue
question
Release
Test
Test GUI
Windows
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-libusb/hidapi!788
Loading…
Reference in a new issue
No description provided.
Delete branch "falkTX/linux-dynamic-libudev"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This PR removes the direct build and link dependency on libudev from the Linux hidraw backend. The backend now loads
libudev.so.1at runtime, so applications that link hidapi do not inherit a direct libudev dependency. Systems using the hidraw backend still need the libudev runtime library.Implementation
RTLD_NOW | RTLD_LOCALhandle.hid_error().hid_exit().libudev1on Ubuntu andeudev-libson Alpine).dlopen/dlclosecleanup.Validation
Assisted-by: codex:gpt-5.6-sol
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.
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:
.cfile for this purpose looks a bit ugly, and the namelibudev.csuggests that it actually is (i.e. implements) libudev, but in fact it is only a wrapper; what is best instead - have ahidapi_libudev_wrap.h, and all functions inside should be either static or inline;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_RESOLVEwouldn't raise any naming suspiciousRTLD_GLOBAL- that is a code smell to use it here, where passing the_lib(or ratherudev_lib_handletodlsym) explicitly is what I'd expectdlclosewhich is technically a resource leak if HIDAPI itself is ever to be dynamically loadedAt 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.
@ -13,3 +12,1 @@pkg_check_modules(libudev REQUIRED IMPORTED_TARGET libudev)target_link_libraries(hidapi_hidraw PRIVATE PkgConfig::libudev Threads::Threads)target_link_libraries(hidapi_hidraw PRIVATE Threads::Threads ${CMAKE_DL_LIBS})the documentation and CI scripts also needs to be updated - now the
libudev-devno longer required and only thelibudev1is neededThat 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.
Thanks for the contribution.
I'll leave it be for now, in case anyone wants to catch up.
Anyone care to try this out?
Just wondering how to properly test this PR. Thanks.
libudev-devpackage is installed (solibudev.his not available)I see. Let me carry out some simple tests over the weekend or next week.
Looks okay to me. I rename libudev.h to libudev_org.h.
Tested with both auto-tools and CMake.
CMake runlog.
Tested under Ubuntu 26.04 with both CMake and auto-tools build.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.