cmake: use readelf to extract soname from shared elf library

This commit is contained in:
Anonymous Maarten
2026-01-19 20:28:10 +01:00
committed by Anonymous Maarten
parent 8a62d6d406
commit c34b67250c

View File

@@ -208,9 +208,14 @@ function(target_get_dynamic_library DEST TARGET)
endif() endif()
endforeach() endforeach()
else() else()
if(UNIX AND NOT APPLE)
find_program(READELF_BIN NAMES readelf)
endif()
# 1. find the target library a file might be symbolic linking to # 1. find the target library a file might be symbolic linking to
# 2. find all other files in the same folder that symbolic link to it # 2. if readelf is available, parse the output of `readelf -d` and output the "(SONAME)" line
# 3. sort all these files, and select the 1st item on Linux, and last on macOS # 3. Else:
# 1. find all other files in the same folder that symbolic link to it
# 2. sort all these files, and select the 1st item on Linux, and last on macOS
set(location_properties IMPORTED_LOCATION) set(location_properties IMPORTED_LOCATION)
if(CMAKE_BUILD_TYPE) if(CMAKE_BUILD_TYPE)
list(APPEND location_properties IMPORTED_LOCATION_${CMAKE_BUILD_TYPE}) list(APPEND location_properties IMPORTED_LOCATION_${CMAKE_BUILD_TYPE})
@@ -228,6 +233,16 @@ function(target_get_dynamic_library DEST TARGET)
get_target_property(library_path "${TARGET}" ${location_property}) get_target_property(library_path "${TARGET}" ${location_property})
message(DEBUG "get_target_property(${TARGET} ${location_property}) -> ${library_path}") message(DEBUG "get_target_property(${TARGET} ${location_property}) -> ${library_path}")
if(EXISTS "${library_path}") if(EXISTS "${library_path}")
if(READELF_BIN)
execute_process(COMMAND "${READELF_BIN}" -d "${library_path}" RESULTS_VARIABLE readelf_result OUTPUT_VARIABLE readelf_output)
if(readelf_output)
string(REGEX MATCH "\\(SONAME\\).*\\[([0-9A-Za-z_.-]+)\\]" soname "${readelf_output}")
if(soname)
set(result "${CMAKE_MATCH_1}")
endif()
endif()
endif()
if(NOT result)
get_filename_component(library_path "${library_path}" ABSOLUTE) get_filename_component(library_path "${library_path}" ABSOLUTE)
while (IS_SYMLINK "${library_path}") while (IS_SYMLINK "${library_path}")
read_absolute_symlink(library_path "${library_path}") read_absolute_symlink(library_path "${library_path}")
@@ -257,6 +272,7 @@ function(target_get_dynamic_library DEST TARGET)
get_filename_component(result "${item}" NAME) get_filename_component(result "${item}" NAME)
endif() endif()
endif() endif()
endif()
endforeach() endforeach()
endif() endif()
if(result) if(result)