Add libpfm to pkg-config Libs.private when libpfm is enabled (#2194)
The private link library derivation skips CMake targets, so the PFM::libpfm imported target was never translated into linker flags. Static consumers using pkg-config then fail to resolve pfm_* symbols. Resolve imported targets through their IMPORTED_LOCATION and emit the corresponding -L/-l flags alongside the other private libraries.
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 4696594..0e81cdb 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -113,6 +113,18 @@
foreach(LIB IN LISTS LINK_LIBS)
if(NOT TARGET "${LIB}" AND LIB MATCHES "^[a-zA-Z0-9_.-]+$")
list(APPEND BENCHMARK_PRIVATE_LINK_LIBRARIES "-l${LIB}")
+ elseif(TARGET "${LIB}")
+ get_target_property(_target_type "${LIB}" TYPE)
+ if(_target_type MATCHES "^(UNKNOWN|STATIC|SHARED|MODULE)_LIBRARY$")
+ get_target_property(_imported_loc "${LIB}" IMPORTED_LOCATION)
+ if(_imported_loc)
+ get_filename_component(_imported_dir "${_imported_loc}" DIRECTORY)
+ get_filename_component(_imported_name "${_imported_loc}" NAME)
+ string(REGEX REPLACE "^lib" "" _imported_name "${_imported_name}")
+ string(REGEX REPLACE "\\.(so|a|dylib)([0-9.]*)$" "" _imported_name "${_imported_name}")
+ list(APPEND BENCHMARK_PRIVATE_LINK_LIBRARIES "-L${_imported_dir}" "-l${_imported_name}")
+ endif()
+ endif()
endif()
endforeach()
string(JOIN " " BENCHMARK_PRIVATE_LINK_LIBRARIES ${BENCHMARK_PRIVATE_LINK_LIBRARIES})