mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2026-12-06 12:43:35 +01:00
243 lines
8.9 KiB
CMake
243 lines
8.9 KiB
CMake
include(CheckCXXSourceCompiles)
|
|
include(CheckCXXSymbolExists)
|
|
include(FetchContent)
|
|
|
|
# like add_library(new ALIAS old) but avoids add_library cannot create ALIAS target "new" because target "old" is imported but not globally visible. on older cmake
|
|
# This can be replaced with a direct alias call once our minimum is cmake 3.18
|
|
function(dolphin_alias_library new old)
|
|
string(REPLACE "::" "" library_no_namespace ${old})
|
|
if (NOT TARGET _alias_${library_no_namespace})
|
|
add_library(_alias_${library_no_namespace} INTERFACE)
|
|
target_link_libraries(_alias_${library_no_namespace} INTERFACE ${old})
|
|
endif()
|
|
add_library(${new} ALIAS _alias_${library_no_namespace})
|
|
endfunction()
|
|
|
|
# Makes an imported target if it doesn't exist. Useful for when find scripts from older versions of cmake don't make the targets you need
|
|
function(dolphin_make_imported_target_if_missing target lib)
|
|
if(${lib}_FOUND AND NOT TARGET ${target})
|
|
add_library(_${lib} INTERFACE)
|
|
target_link_libraries(_${lib} INTERFACE "${${lib}_LIBRARIES}")
|
|
target_include_directories(_${lib} INTERFACE "${${lib}_INCLUDE_DIRS}")
|
|
add_library(${target} ALIAS _${lib})
|
|
endif()
|
|
endfunction()
|
|
|
|
function(dolphin_optional_system_library out_use_system library)
|
|
string(TOUPPER ${library} upperlib)
|
|
set(USE_SYSTEM_${upperlib} "" CACHE STRING "Use system ${library} instead of bundled. ON - Always use system and fail if unavailable, OFF - Always use bundled, AUTO - Use system if available, otherwise use bundled, blank - Delegate to USE_SYSTEM_LIBS. Default is blank.")
|
|
if("${USE_SYSTEM_${upperlib}}" STREQUAL "")
|
|
if(APPROVED_VENDORED_DEPENDENCIES)
|
|
string(TOLOWER ${library} lowerlib)
|
|
if(lowerlib IN_LIST APPROVED_VENDORED_DEPENDENCIES)
|
|
set(${out_use_system} AUTO PARENT_SCOPE)
|
|
else()
|
|
set(${out_use_system} ON PARENT_SCOPE)
|
|
endif()
|
|
else()
|
|
set(${out_use_system} ${USE_SYSTEM_LIBS} PARENT_SCOPE)
|
|
endif()
|
|
else()
|
|
set(${out_use_system} ${USE_SYSTEM_${upperlib}} PARENT_SCOPE)
|
|
endif()
|
|
endfunction()
|
|
|
|
function(dolphin_find_directory_targets dir result)
|
|
get_directory_property(targets DIRECTORY ${dir} BUILDSYSTEM_TARGETS)
|
|
|
|
get_property(subdirs DIRECTORY ${dir} PROPERTY SUBDIRECTORIES)
|
|
foreach(subdir IN LISTS subdirs)
|
|
dolphin_find_directory_targets(${subdir} sub_targets)
|
|
list(APPEND targets ${sub_targets})
|
|
endforeach()
|
|
|
|
set(${result} "${targets}" PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
function(dolphin_add_bundled_library library bundled_path)
|
|
set(use_system "${ARGV2}")
|
|
if (use_system STREQUAL "AUTO")
|
|
message(STATUS "No system ${library} was found. Using static ${library} from Externals.")
|
|
else()
|
|
message(STATUS "Using static ${library} from Externals")
|
|
endif()
|
|
if (NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${bundled_path}/CMakeLists.txt")
|
|
message(FATAL_ERROR "No bundled ${library} was found. Did you forget to checkout submodules?")
|
|
endif()
|
|
add_subdirectory(${bundled_path} EXCLUDE_FROM_ALL)
|
|
dolphin_find_directory_targets(${bundled_path} bundled_targets)
|
|
foreach(target IN LISTS bundled_targets)
|
|
get_target_property(type ${target} TYPE)
|
|
get_target_property(imported ${target} IMPORTED)
|
|
if(imported OR type STREQUAL "INTERFACE_LIBRARY" OR type STREQUAL "UTILITY")
|
|
continue()
|
|
endif()
|
|
dolphin_disable_warnings(${target})
|
|
endforeach()
|
|
endfunction()
|
|
|
|
function(dolphin_set_library_type library type)
|
|
if(DEFINED ${library}_TYPE AND NOT ${${library}_TYPE} STREQUAL "${type}")
|
|
message(FATAL_ERROR
|
|
"The selection for ${library} changed from '${${library}_TYPE}' to '${type}' after configuration.\n"
|
|
"Please delete the build directory (or at least CMakeCache.txt) and reconfigure."
|
|
)
|
|
endif()
|
|
|
|
set(${library}_TYPE "${type}" CACHE INTERNAL "")
|
|
endfunction()
|
|
|
|
function(dolphin_find_optional_system_library library bundled_path)
|
|
dolphin_optional_system_library(use_system ${library})
|
|
string(TOUPPER ${library} upperlib)
|
|
if(use_system)
|
|
# Try a list of versions
|
|
# Will be obsoleted if cmake ever fixes https://gitlab.kitware.com/cmake/cmake/-/issues/26477
|
|
if("DOLPHIN_TRY_VERSIONS" IN_LIST ARGN)
|
|
list(FIND ARGN DOLPHIN_TRY_VERSIONS versions_idx)
|
|
list(REMOVE_AT ARGN ${versions_idx})
|
|
# Extract list of version-like elements from ARGN into version_list
|
|
set(version_list)
|
|
while(TRUE)
|
|
list(LENGTH ARGN len)
|
|
if(len EQUAL versions_idx)
|
|
break()
|
|
endif()
|
|
LIST(GET ARGN ${versions_idx} ver)
|
|
if(ver MATCHES "^[0-9]+(\\.[0-9]+)*$")
|
|
list(APPEND version_list "${ver}")
|
|
list(REMOVE_AT ARGN ${versions_idx})
|
|
else()
|
|
break()
|
|
endif()
|
|
endwhile()
|
|
# Quietly check all the listed versions, from newest to oldest.
|
|
list(SORT version_list COMPARE NATURAL ORDER DESCENDING)
|
|
foreach(version IN LISTS version_list)
|
|
find_package(${library} ${version} ${ARGN} QUIET)
|
|
if(${library}_FOUND)
|
|
break()
|
|
endif()
|
|
endforeach()
|
|
# If none were found, find_package one version loudly so the user sees
|
|
if(NOT ${library}_FOUND)
|
|
list(GET version_list 0 first_version)
|
|
find_package(${library} ${first_version} ${ARGN})
|
|
endif()
|
|
else()
|
|
find_package(${library} ${ARGN})
|
|
endif()
|
|
|
|
# Yay for cmake packages being inconsistent
|
|
if(DEFINED ${library}_FOUND)
|
|
set(prefix ${library})
|
|
else()
|
|
set(prefix ${upperlib})
|
|
endif()
|
|
if((NOT ${prefix}_FOUND) AND (NOT ${use_system} STREQUAL "AUTO"))
|
|
message(FATAL_ERROR "No system ${library} was found. Please install it or set USE_SYSTEM_${upperlib} to AUTO or OFF.")
|
|
endif()
|
|
endif()
|
|
if(${prefix}_FOUND)
|
|
dolphin_set_library_type(${library} "System")
|
|
message(STATUS "Using system ${library}")
|
|
else()
|
|
dolphin_set_library_type(${library} "Bundled")
|
|
dolphin_add_bundled_library(${library} ${bundled_path} ${use_system})
|
|
endif()
|
|
endfunction()
|
|
|
|
function(dolphin_find_optional_system_library_pkgconfig library search alias bundled_path)
|
|
dolphin_optional_system_library(use_system ${library})
|
|
string(TOUPPER ${library} upperlib)
|
|
if(use_system)
|
|
pkg_search_module(${library} ${search} ${ARGN} IMPORTED_TARGET)
|
|
if((NOT ${library}_FOUND) AND (NOT ${use_system} STREQUAL "AUTO"))
|
|
message(FATAL_ERROR "No system ${library} was found. Please install it or set USE_SYSTEM_${upperlib} to AUTO or OFF.")
|
|
endif()
|
|
endif()
|
|
if(${library}_FOUND)
|
|
dolphin_set_library_type(${library} "System")
|
|
message(STATUS "Using system ${library}")
|
|
dolphin_alias_library(${alias} PkgConfig::${library})
|
|
else()
|
|
dolphin_set_library_type(${library} "Bundled")
|
|
dolphin_add_bundled_library(${library} ${bundled_path} ${use_system})
|
|
endif()
|
|
endfunction()
|
|
|
|
function(dolphin_check_toolset_version LABEL VERSION_VAR MIN_VERSION)
|
|
if(NOT DEFINED ${VERSION_VAR})
|
|
return()
|
|
endif()
|
|
message(STATUS "Using ${LABEL} ${${VERSION_VAR}}")
|
|
if(${VERSION_VAR} VERSION_LESS ${MIN_VERSION})
|
|
message(FATAL_ERROR "Requires ${LABEL} ${MIN_VERSION} or higher")
|
|
endif()
|
|
endfunction()
|
|
|
|
|
|
function(dolphin_check_std_version LABEL VERSION_MACRO MIN_VERSION)
|
|
check_cxx_symbol_exists(${VERSION_MACRO} version IS_${LABEL})
|
|
if(NOT IS_${LABEL})
|
|
return()
|
|
endif()
|
|
check_cxx_source_compiles([[
|
|
#include <version>
|
|
#if ${VERSION_MACRO} < ${MIN_VERSION}
|
|
#error
|
|
#endif
|
|
int main(){}
|
|
]] HAS_MINIMUM_${LABEL})
|
|
if(NOT HAS_MINIMUM_${LABEL})
|
|
message(FATAL_ERROR "Requires ${LABEL} ${MIN_VERSION} or higher")
|
|
endif()
|
|
endfunction()
|
|
|
|
function(dolphin_fetchcontent_declare NAME)
|
|
set(source_dir "${CMAKE_SOURCE_DIR}/.FetchContent/${NAME}")
|
|
FetchContent_Declare(${NAME} SOURCE_DIR "${source_dir}" ${ARGN})
|
|
|
|
string(TOUPPER "${NAME}" upper_name)
|
|
# Forget what we auto-set on a previous configure so a changed commit is re-evaluated
|
|
if("${FETCHCONTENT_SOURCE_DIR_${upper_name}}" STREQUAL "" OR
|
|
FETCHCONTENT_SOURCE_DIR_${upper_name} STREQUAL "${source_dir}")
|
|
unset(FETCHCONTENT_SOURCE_DIR_${upper_name} CACHE)
|
|
endif()
|
|
|
|
set(git_tag "")
|
|
list(FIND ARGN GIT_TAG i_tag)
|
|
list(LENGTH ARGN argc)
|
|
math(EXPR i_value "${i_tag} + 1")
|
|
if(NOT i_tag EQUAL -1 AND i_value LESS argc)
|
|
list(GET ARGN ${i_value} git_tag)
|
|
else()
|
|
return()
|
|
endif()
|
|
|
|
find_package(Git QUIET)
|
|
if(NOT GIT_FOUND)
|
|
return()
|
|
endif()
|
|
|
|
execute_process(
|
|
COMMAND "${GIT_EXECUTABLE}" -C "${source_dir}" rev-parse --verify --quiet "${git_tag}^{commit}"
|
|
OUTPUT_VARIABLE wanted_sha
|
|
OUTPUT_STRIP_TRAILING_WHITESPACE
|
|
RESULT_VARIABLE wanted_result
|
|
ERROR_QUIET
|
|
)
|
|
execute_process(
|
|
COMMAND "${GIT_EXECUTABLE}" -C "${source_dir}" rev-parse --verify --quiet HEAD
|
|
OUTPUT_VARIABLE head_sha
|
|
OUTPUT_STRIP_TRAILING_WHITESPACE
|
|
RESULT_VARIABLE head_result
|
|
ERROR_QUIET
|
|
)
|
|
|
|
# If a local clone exists with the correct commit, use it instead of downloading.
|
|
if(wanted_result EQUAL 0 AND head_result EQUAL 0 AND head_sha STREQUAL wanted_sha)
|
|
set(FETCHCONTENT_SOURCE_DIR_${upper_name} "${source_dir}" CACHE PATH "")
|
|
endif()
|
|
endfunction()
|