Skip to content

cmake: include static backend dependencies in pkg-config files - #861

Open
BillyONeal wants to merge 2 commits into
libusb:masterfrom
BillyONeal:cmake-pkgconfig-static-deps
Open

BillyONeal wants to merge 2 commits into
libusb:masterfrom
BillyONeal:cmake-pkgconfig-static-deps

Conversation

@BillyONeal

@BillyONeal BillyONeal commented Nov 21, 2026 •

Copy link
Copy Markdown

This PR is due to pointing code review bots at the vcpkg submission: microsoft/vcpkg#55112 Against that submission, GPT 6.2 Sol notes:

Missing static link dependencies in new pkg-config modules. The new generic hidapi.pc selects a backend but omits its static link dependencies: libudev on Linux and libusb on Android (src/CMakeLists.txt). The new hidapi-darwin.pc likewise omits IOKit and CoreFoundation (mac/CMakeLists.txt).

[...]

The older hidraw, libusb, and generic macOS modules already omitted required libraries or frameworks; those omissions remain.

GPT 6.2 Sol wrote these changes to fix those problems. About these changes, it notes:

Keep the generic and backend-specific modules in sync so pkg-config --static reports the libraries used by the selected backend. Only declare a libusb module dependency when discovery used pkg-config; a parent-provided CMake target may not install a module.

Assisted-by: github-copilot:gpt-6.2-sol

Keep the generic and backend-specific modules in sync so pkg-config --static reports the libraries used by the selected backend. Only declare a libusb module dependency when discovery used pkg-config; a parent-provided CMake target may not install a module.

Assisted-by: github-copilot:gpt-6.2-sol
Keep the shared templates compatible with config.status so the Cflags line and existing FreeBSD private requirements survive. Substitute an empty value for the CMake-specific private linker field in Autotools.

Assisted-by: github-copilot:gpt-6.2-sol
@BillyONeal
BillyONeal marked this pull request as ready for review November 21, 2026 01:36
@mcuee mcuee added the build system/CI Anything related to building the project or running on CI label Nov 22, 2026
Comment thread libusb/CMakeLists.txt Resolved
Comment thread configure.ac

AC_SUBST([HIDAPI_LIB_NAME])
AC_SUBST([HIDAPI_GENERIC_REQUIRES_PRIVATE])
AC_SUBST([HIDAPI_PC_LIBS_PRIVATE_FIELD], [])

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.

I assume this is for build compatibility, but it is not really populated properly for autotools.
If so - that's fine, but just to confirm it verbally.

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.

Confirming, after running both build systems on Linux: this line only keeps the Autotools output valid, it does not populate anything.

Both .pc.in templates are shared between CMake and Autotools, so without this AC_SUBST the new @HIDAPI_PC_LIBS_PRIVATE_FIELD@ placeholder would be left verbatim in the Autotools-generated files. With it, the ./configure output of hidapi.pc, hidapi-hidraw.pc and hidapi-libusb.pc differs from master by one empty line and nothing else.

So an Autotools-built static library keeps the same gaps as before (no libudev / libusb / frameworks in its .pc files, apart from the existing FreeBSD Requires.private). That's fine given Autotools is deprecated.

A note in case someone wants to fix Autotools later: the placeholder is one name used by both templates and AC_SUBST is global, so hidraw and libusb cannot get different values through it. It would need the same GENERIC / BACKEND split that Requires.private has, plus one more sed argument in HIDAPI_CONFIGURE_BACKEND_PC. Not asking for that in this PR.

Drafted with Claude Code.

@Youw

Youw commented Nov 22, 2026

Copy link
Copy Markdown
Member

Should I assume with this chaneg no more patches would be required for vcpkg integration?

@Youw

Youw commented Nov 22, 2026

Copy link
Copy Markdown
Member

Should I assume with this change no more patches would be required for vcpkg integration?

I looked at the port to answer this myself: not quite. microsoft/vcpkg#55112 is merged, and ports/hidapi currently carries two patches:

  • pkgconfig-private-dependencies.patch, which is identical to this PR;
  • vcpkg-pkgconfig-iconv.patch, applied on top of it, which appends -liconv to the Libs.private of hidapi-libusb in the two branches of libusb/CMakeLists.txt that link a separate iconv library (Iconv::Iconv when it is not built-in, and the explicit -liconv fallback).

The second one is not vcpkg-specific. With this PR alone, a static hidapi-libusb on a platform without iconv in libc (e.g. OpenBSD) still gets an incomplete pkg-config --static --libs, while hidapi-config.cmake already covers that case through HIDAPI_NEED_EXPORT_ICONV. So the iconv part would have to land here as well (or in a follow-up) for the port to become patch-free.

Drafted with Claude Code.

@Youw

Youw commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

A few more findings:

Private dependencies are also written for shared builds. With the PR's shared build, pkg-config --cflags hidapi-hidraw fails with "Package libudev was not found" when libudev.pc is not installed

The recommendation is to gating the fields on NOT BUILD_SHARED_LIBS in hidapi_configure_pc, but that could potentially change current behavior. Also emitting them always is also an accepted pkg-config convention (as per claude analysys), so Im not sure if that is worth a change.

I withdraw my original recommendation due to: https://github.com/libusb/hidapi/blob/master/BUILD.cmake.md#both-shared-and-static-build - with the gating that scenario will be partially broken anyway, so let keep the current behavior.

The libusb backend's iconv dependency is missing from Libs.private. When libusb/CMakeLists.txt links Iconv::Iconv or the -liconv fallback, the .pc file does not list it. vcpkg already carries a second patch for exactly this (vcpkg-pkgconfig-iconv.patch)

Maybe we want to include that into this PR as well.

This branch has not been deployed

No deployments
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants