Skip to content

Commit

Permalink
cmake: fix finding shared libraries on Apple
Browse files Browse the repository at this point in the history
  • Loading branch information
madebr committed Sep 8, 2023
1 parent 5dbaffa commit 5f4a94a
Showing 1 changed file with 39 additions and 24 deletions.
63 changes: 39 additions & 24 deletions cmake/PrivateSdlFunctions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,24 @@ endmacro()

function(read_absolute_symlink DEST PATH)
file(READ_SYMLINK "${PATH}" p)
if (NOT IS_ABSOLUTE p)
if(NOT IS_ABSOLUTE "${p}")
get_filename_component(pdir "${PATH}" DIRECTORY)
set(p "${pdir}/${p}")
endif()
get_filename_component(p "${p}" ABSOLUTE)
set("${DEST}" "${p}" PARENT_SCOPE)
endfunction()

function(win32_implib_identify_dll DEST IMPLIB)
cmake_parse_arguments(ARGS "NOTFATAL" "" "" ${ARGN})
if (CMAKE_DLLTOOL)
if(CMAKE_DLLTOOL)
execute_process(
COMMAND "${CMAKE_DLLTOOL}" --identify "${IMPLIB}"
RESULT_VARIABLE retcode
OUTPUT_VARIABLE stdout
ERROR_VARIABLE stderr)
if (NOT retcode EQUAL 0)
if (NOT ARGS_NOTFATAL)
if(NOT retcode EQUAL 0)
if(NOT ARGS_NOTFATAL)
message(FATAL_ERROR "${CMAKE_DLLTOOL} failed.")
else()
set("${DEST}" "${DEST}-NOTFOUND" PARENT_SCOPE)
Expand All @@ -53,26 +54,26 @@ function(win32_implib_identify_dll DEST IMPLIB)
endif()
string(STRIP "${stdout}" result)
set(${DEST} "${result}" PARENT_SCOPE)
elseif (MSVC)
elseif(MSVC)
get_filename_component(CMAKE_C_COMPILER_DIRECTORY "${CMAKE_C_COMPILER}" DIRECTORY CACHE)
find_program(CMAKE_DUMPBIN NAMES dumpbin PATHS "${CMAKE_C_COMPILER_DIRECTORY}")
if (CMAKE_DUMPBIN)
if(CMAKE_DUMPBIN)
execute_process(
COMMAND "${CMAKE_DUMPBIN}" "-headers" "${IMPLIB}"
RESULT_VARIABLE retcode
OUTPUT_VARIABLE stdout
ERROR_VARIABLE stderr)
if (NOT retcode EQUAL 0)
if (NOT ARGS_NOTFATAL)
if(NOT retcode EQUAL 0)
if(NOT ARGS_NOTFATAL)
message(FATAL_ERROR "dumpbin failed.")
else()
set(${DEST} "${DEST}-NOTFOUND" PARENT_SCOPE)
return()
endif()
endif()
string(REGEX MATCH "DLL name[ ]+:[ ]+([^\n]+)\n" match "${stdout}")
if (NOT match)
if (NOT ARGS_NOTFATAL)
if(NOT match)
if(NOT ARGS_NOTFATAL)
message(FATAL_ERROR "dumpbin did not find any associated dll for ${IMPLIB}.")
else()
set(${DEST} "${DEST}-NOTFOUND" PARENT_SCOPE)
Expand All @@ -85,7 +86,7 @@ function(win32_implib_identify_dll DEST IMPLIB)
message(FATAL_ERROR "Cannot find dumpbin, please set CMAKE_DUMPBIN cmake variable")
endif()
else()
if (NOT ARGS_NOTFATAL)
if(NOT ARGS_NOTFATAL)
message(FATAL_ERROR "Don't know how to identify dll from import library. Set CMAKE_DLLTOOL (for mingw) or CMAKE_DUMPBIN (for MSVC)")
else()
set(${DEST} "${DEST}-NOTFOUND")
Expand All @@ -96,25 +97,27 @@ endfunction()
function(get_actual_target)
set(dst "${ARGV0}")
set(target "${${dst}}")
set(input "${target}")
get_target_property(alias "${target}" ALIASED_TARGET)
while(alias)
set(target "${alias}")
get_target_property(alias "${target}" ALIASED_TARGET)
endwhile()
message(DEBUG "get_actual_target(\"${input}\") -> \"${target}\"")
set("${dst}" "${target}" PARENT_SCOPE)
endfunction()

function(target_get_dynamic_library DEST TARGET)
set(result)
get_actual_target(TARGET)
if (WIN32)
if(WIN32)
# Use the target dll of the import library
set(props_to_check IMPORTED_IMPLIB)
if (CMAKE_BUILD_TYPE)
if(CMAKE_BUILD_TYPE)
list(APPEND props_to_check IMPORTED_IMPLIB_${CMAKE_BUILD_TYPE})
endif()
list(APPEND props_to_check IMPORTED_LOCATION)
if (CMAKE_BUILD_TYPE)
if(CMAKE_BUILD_TYPE)
list(APPEND props_to_check IMPORTED_LOCATION_${CMAKE_BUILD_TYPE})
endif()
foreach (config_type ${CMAKE_CONFIGURATION_TYPES} RELEASE DEBUG RELWITHDEBINFO MINSIZEREL)
Expand All @@ -123,47 +126,59 @@ function(target_get_dynamic_library DEST TARGET)
endforeach()

foreach(prop_to_check ${props_to_check})
if (NOT result)
if(NOT result)
get_target_property(propvalue "${TARGET}" ${prop_to_check})
if (propvalue AND EXISTS "${propvalue}")
if(propvalue AND EXISTS "${propvalue}")
win32_implib_identify_dll(result "${propvalue}" NOTFATAL)
endif()
endif()
endforeach()
else()
# 1. find the target library a file might be symbolic linking to
# 2. find all other files in the same folder that symolic link to it
# 3. sort all these files, and select the 2nd item
# 3. sort all these files, and select the 1st item on Linux, and last on Macos
set(location_properties IMPORTED_LOCATION)
if (CMAKE_BUILD_TYPE)
if(CMAKE_BUILD_TYPE)
list(APPEND location_properties IMPORTED_LOCATION_${CMAKE_BUILD_TYPE})
endif()
foreach (config_type ${CMAKE_CONFIGURATION_TYPES} RELEASE DEBUG RELWITHDEBINFO MINSIZEREL)
list(APPEND location_properties IMPORTED_LOCATION_${config_type})
endforeach()
if(APPLE)
set(valid_shared_library_regex "\\.[0-9]+\\.dylib$")
else()
set(valid_shared_library_regex "\\.so\\.([0-9.]+)?[0-9]")
endif()
foreach(location_property ${location_properties})
if (NOT result)
if(NOT result)
get_target_property(library_path "${TARGET}" ${location_property})
if (EXISTS "${library_path}")
message(DEBUG "get_target_property(${TARGET} ${location_propert}) -> ${library_path}")
if(EXISTS "${library_path}")
get_filename_component(library_path "${library_path}" ABSOLUTE)
while (IS_SYMLINK "${library_path}")
read_absolute_symlink(library_path "${library_path}")
endwhile()
message(DEBUG "${TARGET} -> ${library_path}")
get_filename_component(libdir "${library_path}" DIRECTORY)
file(GLOB subfiles "${libdir}/*")
set(similar_files "${library_path}")
foreach(subfile ${subfiles})
if (IS_SYMLINK "${subfile}")
if(IS_SYMLINK "${subfile}")
read_absolute_symlink(subfile_target "${subfile}")
while (IS_SYMLINK "${subfile_target}")
while(IS_SYMLINK "${subfile_target}")
read_absolute_symlink(subfile_target "${subfile_target}")
endwhile()
if (subfile_target STREQUAL library_path AND NOT "${subfile}" MATCHES ".*(dylib|so)$")
get_filename_component(subfile_target "${subfile_target}" ABSOLUTE)
if(subfile_target STREQUAL library_path AND subfile MATCHES "${valid_shared_library_regex}")
list(APPEND similar_files "${subfile}")
endif()
endif()
endforeach()
list(SORT similar_files)
list(LENGTH similar_files eq_length)
message(DEBUG "files that are similar to \"${library_path}\"=${similar_files}")
if(APPLE)
list(REVERSE similar_files)
endif()
list(GET similar_files 0 item)
get_filename_component(result "${item}" NAME)
endif()
Expand Down

0 comments on commit 5f4a94a

Please sign in to comment.