Use library paths from pkg-config #4

Merged
horizon merged 1 commits from argilo/rtl-sdr:libusb-pkgconfig into master 2023-12-07 17:03:28 +00:00
Contributor

When using pkg-config, it is not possible to link to libusb if it is in a non-standard location. This worked prior to 0847e93e08 because PkgConfig::LIBUSB provided the path to the library, but the LIBUSB_LIBRARIES variable set by pkg_check_modules only includes the library name and not the path. The LIBUSB_LINK_LIBRARIES variable set by pkg_check_modules includes the path, so we can resolve the problem (while retaining the existing LIBUSB_LIBRARIES variable for non-pkg-config installs) by copying LIBUSB_LINK_LIBRARIES into LIBUSB_LIBRARIES.

When using pkg-config, it is not possible to link to libusb if it is in a non-standard location. This worked prior to 0847e93e0869feab50fd27c7afeb85d78ca04631 because `PkgConfig::LIBUSB` provided the path to the library, but the `LIBUSB_LIBRARIES` variable set by `pkg_check_modules` only includes the library name and not the path. The `LIBUSB_LINK_LIBRARIES` variable set by `pkg_check_modules` includes the path, so we can resolve the problem (while retaining the existing `LIBUSB_LIBRARIES` variable for non-pkg-config installs) by copying `LIBUSB_LINK_LIBRARIES` into `LIBUSB_LIBRARIES`.
argilo added 1 commit 2023-12-07 02:56:34 +00:00
c945bd075c Use library paths from pkg-config
Signed-off-by: Clayton Smith <argilo@gmail.com>
ewild was assigned by laforge 2023-12-07 10:38:01 +00:00
steve-m was assigned by laforge 2023-12-07 10:38:01 +00:00
horizon was assigned by laforge 2023-12-07 10:38:01 +00:00
horizon merged commit b85f037d85 into master 2023-12-07 17:03:28 +00:00
Author
Contributor

I just noticed that this only works with CMake >= 3.12 because prior versions don't set <XXX>_LINK_LIBRARIES.

Unfortunately I can't reach gitea-ssh.osmocom.org to push a fix to my fork, but perhaps you could apply the following patch, which avoids overwriting LIBUSB_LIBRARIES if LIBUSB_LINK_LIBRARIES doesn't exist:

From 7ea314dc2580230487ffa51582e1d47be64342bd Mon Sep 17 00:00:00 2001
From: Clayton Smith <argilo@gmail.com>
Date: Sat, 23 Dec 2023 20:14:53 -0500
Subject: [PATCH] Only use LIBUSB_LINK_LIBRARIES if it exists

FindPkgConfig only sets <XXX>_LINK_LIBRARIES in CMake >= 3.12.

Signed-off-by: Clayton Smith <argilo@gmail.com>
---
 CMakeLists.txt | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1cb32a38d2..1ff6c9aa2c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -71,7 +71,9 @@ find_package(PkgConfig)
 
 if(PKG_CONFIG_FOUND)
     pkg_check_modules(LIBUSB libusb-1.0 IMPORTED_TARGET)
-    set(LIBUSB_LIBRARIES "${LIBUSB_LINK_LIBRARIES}")
+    if(LIBUSB_LINK_LIBRARIES)
+        set(LIBUSB_LIBRARIES "${LIBUSB_LINK_LIBRARIES}")
+    endif()
 else()
     set(LIBUSB_LIBRARIES "" CACHE STRING "manual libusb path")
     set(LIBUSB_INCLUDE_DIRS "" CACHE STRING "manual libusb includepath")
-- 
2.34.1
I just noticed that this only works with CMake >= 3.12 because prior versions don't set `<XXX>_LINK_LIBRARIES`. Unfortunately I can't reach `gitea-ssh.osmocom.org` to push a fix to my fork, but perhaps you could apply the following patch, which avoids overwriting `LIBUSB_LIBRARIES` if `LIBUSB_LINK_LIBRARIES` doesn't exist: ``` From 7ea314dc2580230487ffa51582e1d47be64342bd Mon Sep 17 00:00:00 2001 From: Clayton Smith <argilo@gmail.com> Date: Sat, 23 Dec 2023 20:14:53 -0500 Subject: [PATCH] Only use LIBUSB_LINK_LIBRARIES if it exists FindPkgConfig only sets <XXX>_LINK_LIBRARIES in CMake >= 3.12. Signed-off-by: Clayton Smith <argilo@gmail.com> --- CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1cb32a38d2..1ff6c9aa2c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,7 +71,9 @@ find_package(PkgConfig) if(PKG_CONFIG_FOUND) pkg_check_modules(LIBUSB libusb-1.0 IMPORTED_TARGET) - set(LIBUSB_LIBRARIES "${LIBUSB_LINK_LIBRARIES}") + if(LIBUSB_LINK_LIBRARIES) + set(LIBUSB_LIBRARIES "${LIBUSB_LINK_LIBRARIES}") + endif() else() set(LIBUSB_LIBRARIES "" CACHE STRING "manual libusb path") set(LIBUSB_INCLUDE_DIRS "" CACHE STRING "manual libusb includepath") -- 2.34.1 ```
Owner

Pushed, thanks!

Pushed, thanks!
Sign in to join this conversation.
No reviewers
No Label
No Milestone
No project
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sdr/rtl-sdr#4
No description provided.