Skip to content

Commit

Permalink
cmake: sync cmake/PrivateSdlFunctions.cmake with SDL2_image
Browse files Browse the repository at this point in the history
  • Loading branch information
madebr committed Sep 9, 2023
1 parent 5bba532 commit a9cace3
Showing 1 changed file with 58 additions and 47 deletions.
105 changes: 58 additions & 47 deletions cmake/PrivateSdlFunctions.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file is shared amongst SDL_image/SDL_mixer/SDL_net/SDL_ttf
# This file is shared amongst SDL_image/SDL_mixer/SDL_ttf

macro(sdl_calculate_derived_version_variables)
if (NOT DEFINED MAJOR_VERSION OR NOT DEFINED MINOR_VERSION OR NOT DEFINED MICRO_VERSION)
if(NOT DEFINED MAJOR_VERSION OR NOT DEFINED MINOR_VERSION OR NOT DEFINED MICRO_VERSION)
message(FATAL_ERROR "MAJOR_VERSION, MINOR_VERSION and MICRO_VERSION need to be defined")
endif()

Expand All @@ -10,7 +10,7 @@ macro(sdl_calculate_derived_version_variables)
# Calculate a libtool-like version number
math(EXPR BINARY_AGE "${MINOR_VERSION} * 100 + ${MICRO_VERSION}")
math(EXPR IS_DEVELOPMENT "${MINOR_VERSION} % 2")
if (IS_DEVELOPMENT)
if(IS_DEVELOPMENT)
# Development branch, 2.5.1 -> libSDL2_XXXXX-2.0.so.0.501.0
set(INTERFACE_AGE 0)
else()
Expand Down Expand Up @@ -58,7 +58,7 @@ macro(sdl_find_sdl2 TARGET VERSION)

# Use Private FindSDL2.cmake module to find SDL2 for installations where no SDL2Config.cmake is available,
# or for those installations where no target is generated.
if (NOT TARGET ${TARGET})
if(NOT TARGET ${TARGET})
message(STATUS "Using private SDL2 find module")
find_package(PrivateSDL2 ${VERSION} REQUIRED)
add_library(${TARGET} INTERFACE IMPORTED)
Expand All @@ -70,23 +70,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 @@ -95,26 +96,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 @@ -127,7 +128,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 @@ -149,14 +150,14 @@ 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 @@ -165,49 +166,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
set(props_to_check IMPORTED_LOCATION)
if (CMAKE_BUILD_TYPE)
list(APPEND props_to_check IMPORTED_LOCATION_${CMAKE_BUILD_TYPE})
# 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)
list(APPEND location_properties IMPORTED_LOCATION_${CMAKE_BUILD_TYPE})
endif()
foreach (config_type ${CMAKE_CONFIGURATION_TYPES} RELEASE DEBUG RELWITHDEBINFO MINSIZEREL)
list(APPEND props_to_check IMPORTED_LOCATION_${config_type})
list(APPEND location_properties IMPORTED_LOCATION_${config_type})
endforeach()
foreach(prop_to_check ${props_to_check})
if (NOT result)
get_target_property(propvalue "${TARGET}" ${prop_to_check})
if (EXISTS "${propvalue}")
while (IS_SYMLINK "${propvalue}")
read_absolute_symlink(propvalue "${propvalue}")
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)
get_target_property(library_path "${TARGET}" ${location_property})
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()
get_filename_component(libdir "${propvalue}" DIRECTORY)
get_filename_component(libdir "${library_path}" DIRECTORY)
file(GLOB subfiles "${libdir}/*")
set(similar_files "${propvalue}")
set(similar_files "${library_path}")
foreach(subfile ${subfiles})
if (IS_SYMLINK "${subfile}")
if(IS_SYMLINK "${subfile}")
read_absolute_symlink(subfile_target "${subfile}")
if (subfile_target STREQUAL propvalue)
while (IS_SYMLINK "${subfile_target}")
read_absolute_symlink(subfile_target "${subfile_target}")
endwhile()
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)
if (eq_length GREATER 1)
list(GET similar_files 1 item)
else()
list(GET similar_files 0 item)
set(index 0)
if(APPLE)
list(LENGTH similar_files len)
math(EXPR index "${len}-1")
endif()
list(GET similar_files ${index} item)
get_filename_component(result "${item}" NAME)
endif()
endif()
Expand Down Expand Up @@ -237,14 +248,14 @@ function(target_get_dynamic_library DEST TARGET)
else()
message(WARNING "Unable to extract dynamic library from target=${TARGET}, type=${target_type}.")
endif()
set (result "$<TARGET_FILE_NAME:${TARGET}>")
endif()
# TARGET_SONAME_FILE is not allowed for DLL target platforms.
if(WIN32)
set(result "$<TARGET_FILE_NAME:${TARGET}>")
else()
set(result "$<TARGET_SONAME_FILE_NAME:${TARGET}>")
# TARGET_SONAME_FILE is not allowed for DLL target platforms.
if(WIN32)
set(result "$<TARGET_FILE_NAME:${TARGET}>")
else()
set(result "$<TARGET_SONAME_FILE_NAME:${TARGET}>")
endif()
endif()
set(${DEST} ${result} PARENT_SCOPE)
endfunction()

macro(sdl_check_project_in_subfolder relative_subfolder name vendored_option)
Expand Down

0 comments on commit a9cace3

Please sign in to comment.