Hey osmocom-sdr,

I've got a choose your own adventure patch set here for the adventurous
reviewer.

On OpenBSD (but not specific to OpenBSD) libusb-1.0 lives in
/usr/local/lib. The default linker (lld 13.0.0 on 7.2) doesn't include
/usr/local/lib in its out of the box search path, which triggers a build
failure in rtl-sdr because the -L argument(s) aren't passed through to ld.

The root cause here is that we pass ${LIBUSB_LIBRARIES} to
target_link_libraries, which is only the -lfoo arguments, but not the
-L/foo/bar arguments. This tends to work on most systems, since libusb is
usually in a place ld will search anyway.

There's two ways I could see to do this. Here comes the choose your own
adventure part:

1) The first is to use the PkgConfig::LIBUSB helper, which can be passed
directly to `target_link_libraries`, which allows all the arguments to be
passed through. This means the existing hooks to pass in a manual path
would be dropped and building without pkg-config against libusb won't be
supported anymore. This is the attached git format-patch entitled
"0001-Use-the-PkgConfig-imported-target-rather-than-_LIBRA.patch".

2) Punch the -L arguments through next to the LIBUSB_LIBRARIES argument to
`target_link_libraries`, add a new knob to set the values ("manual libusb
librarypath"), and when using pkg-config, generate the arguments with a
FOREACH. This is the attached git format-patch
"0001-Pass-additional-library-directory-arguments-to-ld.patch".

Both patch flavors build on my Debian sid box and an OpenBSD 7.2 box.

   paultag

-- 
:wq
From a9168d998d72b3d24afd050aedcc38bfb9839cd5 Mon Sep 17 00:00:00 2001
From: "Paul R. Tagliamonte" <paul...@gmail.com>
Date: Fri, 16 Dec 2022 18:56:42 -0500
Subject: [PATCH] Pass additional library directory arguments to ld

On OpenBSD, the linker (LLD 13.0.0) doesn't search /usr/local/lib,
which is where the libusb-1.0 port lives by default. ${*_LIBRARIES}
includes the, well, libraries such as -lfoo, but not the linker
arguments such as -L/bar/foo - which results in a failure to find the
right libraries to link against.
---
 CMakeLists.txt     |  9 +++++++++
 src/CMakeLists.txt | 20 ++++++++++++++++++--
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index e2946ba..b302091 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -71,9 +71,18 @@ find_package(PkgConfig)
 
 if(PKG_CONFIG_FOUND)
     pkg_check_modules(LIBUSB libusb-1.0 IMPORTED_TARGET)
+
+    # construct LIBUSB_LIBRARY_DIRS_ARGS to pass to ld when linking to find
+    # the right libraries in the system if they're not in the default system
+    # linker path.
+    FOREACH(lib ${LIBUSB_LIBRARY_DIRS})
+        LIST(APPEND LIBUSB_LIBRARY_DIRS_ARGS "-L${lib}")
+    ENDFOREACH(lib)
+    STRING(REPLACE ";" " " LIBUSB_LIBRARY_DIRS_ARGS "${LIBUSB_LIBRARY_DIRS_ARGS}")
 else()
     set(LIBUSB_LIBRARIES "" CACHE STRING "manual libusb path")
     set(LIBUSB_INCLUDE_DIRS "" CACHE STRING "manual libusb includepath")
+    set(LIBUSB_LIBRARY_DIRS "" CACHE STRING "manual libusb librarypath")
 endif()
 
 if(MSVC)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7b47309..b9af9c5 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -20,7 +20,11 @@
 ########################################################################
 add_library(rtlsdr SHARED librtlsdr.c
   tuner_e4k.c tuner_fc0012.c tuner_fc0013.c tuner_fc2580.c tuner_r82xx.c)
-target_link_libraries(rtlsdr ${LIBUSB_LIBRARIES} ${THREADS_PTHREADS_LIBRARY})
+target_link_libraries(rtlsdr
+    ${LIBUSB_LIBRARIES}
+    ${LIBUSB_LIBRARY_DIRS_ARGS}
+    ${THREADS_PTHREADS_LIBRARY}
+)
 target_include_directories(rtlsdr PUBLIC
   $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
   $<INSTALL_INTERFACE:include>  # <prefix>/include
@@ -38,7 +42,11 @@ generate_export_header(rtlsdr)
 ########################################################################
 add_library(rtlsdr_static STATIC librtlsdr.c
   tuner_e4k.c tuner_fc0012.c tuner_fc0013.c tuner_fc2580.c tuner_r82xx.c)
-target_link_libraries(rtlsdr ${LIBUSB_LIBRARIES} ${THREADS_PTHREADS_LIBRARY})
+target_link_libraries(rtlsdr
+    ${LIBUSB_LIBRARIES}
+    ${LIBUSB_LIBRARY_DIRS_ARGS}
+    ${THREADS_PTHREADS_LIBRARY}
+)
 target_include_directories(rtlsdr_static PUBLIC
   $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
   $<INSTALL_INTERFACE:include>  # <prefix>/include
@@ -98,34 +106,42 @@ set(INSTALL_TARGETS rtlsdr rtlsdr_static rtl_sdr rtl_tcp rtl_test rtl_fm rtl_eep
 
 target_link_libraries(rtl_sdr rtlsdr convenience_static
     ${LIBUSB_LIBRARIES}
+    ${LIBUSB_LIBRARY_DIRS_ARGS}
     ${CMAKE_THREAD_LIBS_INIT}
 )
 target_link_libraries(rtl_tcp rtlsdr convenience_static
     ${LIBUSB_LIBRARIES}
+    ${LIBUSB_LIBRARY_DIRS_ARGS}
     ${CMAKE_THREAD_LIBS_INIT}
 )
 target_link_libraries(rtl_test rtlsdr convenience_static
     ${LIBUSB_LIBRARIES}
+    ${LIBUSB_LIBRARY_DIRS_ARGS}
     ${CMAKE_THREAD_LIBS_INIT}
 )
 target_link_libraries(rtl_fm rtlsdr convenience_static
     ${LIBUSB_LIBRARIES}
+    ${LIBUSB_LIBRARY_DIRS_ARGS}
     ${CMAKE_THREAD_LIBS_INIT}
 )
 target_link_libraries(rtl_eeprom rtlsdr convenience_static
     ${LIBUSB_LIBRARIES}
+    ${LIBUSB_LIBRARY_DIRS_ARGS}
     ${CMAKE_THREAD_LIBS_INIT}
 )
 target_link_libraries(rtl_adsb rtlsdr convenience_static
     ${LIBUSB_LIBRARIES}
+    ${LIBUSB_LIBRARY_DIRS_ARGS}
     ${CMAKE_THREAD_LIBS_INIT}
 )
 target_link_libraries(rtl_power rtlsdr convenience_static
     ${LIBUSB_LIBRARIES}
+    ${LIBUSB_LIBRARY_DIRS_ARGS}
     ${CMAKE_THREAD_LIBS_INIT}
 )
 target_link_libraries(rtl_biast rtlsdr convenience_static
     ${LIBUSB_LIBRARIES}
+    ${LIBUSB_LIBRARY_DIRS_ARGS}
     ${CMAKE_THREAD_LIBS_INIT}
 )
 if(UNIX)
-- 
2.37.3

From 303bc181354e241ac37183dcd5192365cfff4be9 Mon Sep 17 00:00:00 2001
From: "Paul R. Tagliamonte" <paul...@gmail.com>
Date: Fri, 16 Dec 2022 18:56:42 -0500
Subject: [PATCH] Use the PkgConfig:: imported target rather than
 ${*_LIBRARIES}

On OpenBSD, the linker (LLD 13.0.0) doesn't search /usr/local/lib,
which is where the libusb-1.0 port lives by default. ${*_LIBRARIES}
includes the, well, libraries such as -lfoo, but not the linker
arguments such as -L/bar/foo - which results in a failure to find the
right libraries to link against.

This approach, however, means that non-pkg-config usage of libusb
won't build anymore.
---
 CMakeLists.txt     |  7 +------
 src/CMakeLists.txt | 24 ++++++++++++++----------
 2 files changed, 15 insertions(+), 16 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index e2946ba..619b185 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -69,12 +69,7 @@ endif()
 find_package(Threads)
 find_package(PkgConfig)
 
-if(PKG_CONFIG_FOUND)
-    pkg_check_modules(LIBUSB libusb-1.0 IMPORTED_TARGET)
-else()
-    set(LIBUSB_LIBRARIES "" CACHE STRING "manual libusb path")
-    set(LIBUSB_INCLUDE_DIRS "" CACHE STRING "manual libusb includepath")
-endif()
+pkg_check_modules(LIBUSB libusb-1.0 IMPORTED_TARGET REQUIRED)
 
 if(MSVC)
     set(THREADS_PTHREADS_LIBRARY "" CACHE STRING "manual pthread-win32 path")
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7b47309..d95f4d4 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -20,7 +20,11 @@
 ########################################################################
 add_library(rtlsdr SHARED librtlsdr.c
   tuner_e4k.c tuner_fc0012.c tuner_fc0013.c tuner_fc2580.c tuner_r82xx.c)
-target_link_libraries(rtlsdr ${LIBUSB_LIBRARIES} ${THREADS_PTHREADS_LIBRARY})
+target_link_libraries(
+  rtlsdr
+  PkgConfig::LIBUSB
+  ${THREADS_PTHREADS_LIBRARIES}
+  )
 target_include_directories(rtlsdr PUBLIC
   $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
   $<INSTALL_INTERFACE:include>  # <prefix>/include
@@ -38,7 +42,7 @@ generate_export_header(rtlsdr)
 ########################################################################
 add_library(rtlsdr_static STATIC librtlsdr.c
   tuner_e4k.c tuner_fc0012.c tuner_fc0013.c tuner_fc2580.c tuner_r82xx.c)
-target_link_libraries(rtlsdr ${LIBUSB_LIBRARIES} ${THREADS_PTHREADS_LIBRARY})
+target_link_libraries(rtlsdr PkgConfig::LIBUSB ${THREADS_PTHREADS_LIBRARY})
 target_include_directories(rtlsdr_static PUBLIC
   $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
   $<INSTALL_INTERFACE:include>  # <prefix>/include
@@ -97,35 +101,35 @@ add_executable(rtl_biast rtl_biast.c)
 set(INSTALL_TARGETS rtlsdr rtlsdr_static rtl_sdr rtl_tcp rtl_test rtl_fm rtl_eeprom rtl_adsb rtl_power rtl_biast)
 
 target_link_libraries(rtl_sdr rtlsdr convenience_static
-    ${LIBUSB_LIBRARIES}
+    PkgConfig::LIBUSB
     ${CMAKE_THREAD_LIBS_INIT}
 )
 target_link_libraries(rtl_tcp rtlsdr convenience_static
-    ${LIBUSB_LIBRARIES}
+    PkgConfig::LIBUSB
     ${CMAKE_THREAD_LIBS_INIT}
 )
 target_link_libraries(rtl_test rtlsdr convenience_static
-    ${LIBUSB_LIBRARIES}
+    PkgConfig::LIBUSB
     ${CMAKE_THREAD_LIBS_INIT}
 )
 target_link_libraries(rtl_fm rtlsdr convenience_static
-    ${LIBUSB_LIBRARIES}
+    PkgConfig::LIBUSB
     ${CMAKE_THREAD_LIBS_INIT}
 )
 target_link_libraries(rtl_eeprom rtlsdr convenience_static
-    ${LIBUSB_LIBRARIES}
+    PkgConfig::LIBUSB
     ${CMAKE_THREAD_LIBS_INIT}
 )
 target_link_libraries(rtl_adsb rtlsdr convenience_static
-    ${LIBUSB_LIBRARIES}
+    PkgConfig::LIBUSB
     ${CMAKE_THREAD_LIBS_INIT}
 )
 target_link_libraries(rtl_power rtlsdr convenience_static
-    ${LIBUSB_LIBRARIES}
+    PkgConfig::LIBUSB
     ${CMAKE_THREAD_LIBS_INIT}
 )
 target_link_libraries(rtl_biast rtlsdr convenience_static
-    ${LIBUSB_LIBRARIES}
+    PkgConfig::LIBUSB
     ${CMAKE_THREAD_LIBS_INIT}
 )
 if(UNIX)
-- 
2.37.3

Reply via email to