| 1 |
# 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 |
| 2 |
# This can be replaced with a direct alias call once our minimum is cmake 3.18 |
| 3 |
function(dolphin_alias_library new old) |
| 4 |
string(REPLACE "::" "" library_no_namespace ${old}) |
| 5 |
if (NOT TARGET _alias_${library_no_namespace}) |
| 6 |
add_library(_alias_${library_no_namespace} INTERFACE) |
| 7 |
target_link_libraries(_alias_${library_no_namespace} INTERFACE ${old}) |
| 8 |
endif() |
| 9 |
add_library(${new} ALIAS _alias_${library_no_namespace}) |
| 10 |
endfunction() |
| 11 |
|
| 12 |
# 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 |
| 13 |
function(dolphin_make_imported_target_if_missing target lib) |
| 14 |
if(${lib}_FOUND AND NOT TARGET ${target}) |
| 15 |
add_library(_${lib} INTERFACE) |
| 16 |
target_link_libraries(_${lib} INTERFACE "${${lib}_LIBRARIES}") |
| 17 |
target_include_directories(_${lib} INTERFACE "${${lib}_INCLUDE_DIRS}") |
| 18 |
add_library(${target} ALIAS _${lib}) |
| 19 |
endif() |
| 20 |
endfunction() |
| 21 |
|
| 22 |
function(dolphin_optional_system_library out_use_system library) |
| 23 |
string(TOUPPER ${library} upperlib) |
| 24 |
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.") |
| 25 |
if("${USE_SYSTEM_${upperlib}}" STREQUAL "") |
| 26 |
if(APPROVED_VENDORED_DEPENDENCIES) |
| 27 |
string(TOLOWER ${library} lowerlib) |
| 28 |
if(lowerlib IN_LIST APPROVED_VENDORED_DEPENDENCIES) |
| 29 |
set(${out_use_system} AUTO PARENT_SCOPE) |
| 30 |
else() |
| 31 |
set(${out_use_system} ON PARENT_SCOPE) |
| 32 |
endif() |
| 33 |
else() |
| 34 |
set(${out_use_system} ${USE_SYSTEM_LIBS} PARENT_SCOPE) |
| 35 |
endif() |
| 36 |
else() |
| 37 |
set(${out_use_system} ${USE_SYSTEM_${upperlib}} PARENT_SCOPE) |
| 38 |
endif() |
| 39 |
endfunction() |
| 40 |
|
| 41 |
function(dolphin_add_bundled_library library use_system bundled_path) |
| 42 |
if (${use_system} STREQUAL "AUTO") |
| 43 |
message(STATUS "No system ${library} was found. Using static ${library} from Externals.") |
| 44 |
else() |
| 45 |
message(STATUS "Using static ${library} from Externals") |
| 46 |
endif() |
| 47 |
if (NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${bundled_path}/CMakeLists.txt") |
| 48 |
message(FATAL_ERROR "No bundled ${library} was found. Did you forget to checkout submodules?") |
| 49 |
endif() |
| 50 |
add_subdirectory(${bundled_path} EXCLUDE_FROM_ALL) |
| 51 |
endfunction() |
| 52 |
|
| 53 |
function(dolphin_set_library_type library type) |
| 54 |
if(DEFINED ${library}_TYPE AND NOT ${${library}_TYPE} STREQUAL "${type}") |
| 55 |
message(FATAL_ERROR |
| 56 |
"The selection for ${library} changed from '${${library}_TYPE}' to '${type}' after configuration.\n" |
| 57 |
"Please delete the build directory (or at least CMakeCache.txt) and reconfigure." |
| 58 |
) |
| 59 |
endif() |
| 60 |
|
| 61 |
set(${library}_TYPE "${type}" CACHE INTERNAL "") |
| 62 |
endfunction() |
| 63 |
|
| 64 |
function(dolphin_find_optional_system_library library bundled_path) |
| 65 |
dolphin_optional_system_library(use_system ${library}) |
| 66 |
string(TOUPPER ${library} upperlib) |
| 67 |
if(use_system) |
| 68 |
# Try a list of versions |
| 69 |
# Will be obsoleted if cmake ever fixes https://gitlab.kitware.com/cmake/cmake/-/issues/26477 |
| 70 |
if("DOLPHIN_TRY_VERSIONS" IN_LIST ARGN) |
| 71 |
list(FIND ARGN DOLPHIN_TRY_VERSIONS versions_idx) |
| 72 |
list(REMOVE_AT ARGN ${versions_idx}) |
| 73 |
# Extract list of version-like elements from ARGN into version_list |
| 74 |
set(version_list) |
| 75 |
while(TRUE) |
| 76 |
list(LENGTH ARGN len) |
| 77 |
if(len EQUAL versions_idx) |
| 78 |
break() |
| 79 |
endif() |
| 80 |
LIST(GET ARGN ${versions_idx} ver) |
| 81 |
if(ver MATCHES "^[0-9]+(\\.[0-9]+)*$") |
| 82 |
list(APPEND version_list "${ver}") |
| 83 |
list(REMOVE_AT ARGN ${versions_idx}) |
| 84 |
else() |
| 85 |
break() |
| 86 |
endif() |
| 87 |
endwhile() |
| 88 |
# Quietly check all the listed versions |
| 89 |
foreach(version IN LISTS version_list) |
| 90 |
find_package(${library} ${version} ${ARGN} QUIET) |
| 91 |
if(${library}_FOUND) |
| 92 |
break() |
| 93 |
endif() |
| 94 |
endforeach() |
| 95 |
# If none were found, find_package one version loudly so the user sees |
| 96 |
if(NOT ${library}_FOUND) |
| 97 |
list(GET version_list 0 first_version) |
| 98 |
find_package(${library} ${first_version} ${ARGN}) |
| 99 |
endif() |
| 100 |
else() |
| 101 |
find_package(${library} ${ARGN}) |
| 102 |
endif() |
| 103 |
|
| 104 |
# Yay for cmake packages being inconsistent |
| 105 |
if(DEFINED ${library}_FOUND) |
| 106 |
set(prefix ${library}) |
| 107 |
else() |
| 108 |
set(prefix ${upperlib}) |
| 109 |
endif() |
| 110 |
if((NOT ${prefix}_FOUND) AND (NOT ${use_system} STREQUAL "AUTO")) |
| 111 |
message(FATAL_ERROR "No system ${library} was found. Please install it or set USE_SYSTEM_${upperlib} to AUTO or OFF.") |
| 112 |
endif() |
| 113 |
endif() |
| 114 |
if(${prefix}_FOUND) |
| 115 |
dolphin_set_library_type(${library} "System") |
| 116 |
message(STATUS "Using system ${library}") |
| 117 |
else() |
| 118 |
dolphin_set_library_type(${library} "Bundled") |
| 119 |
dolphin_add_bundled_library(${library} ${use_system} ${bundled_path}) |
| 120 |
endif() |
| 121 |
endfunction() |
| 122 |
|
| 123 |
function(dolphin_find_optional_system_library_pkgconfig library search alias bundled_path) |
| 124 |
dolphin_optional_system_library(use_system ${library}) |
| 125 |
string(TOUPPER ${library} upperlib) |
| 126 |
if(use_system) |
| 127 |
pkg_search_module(${library} ${search} ${ARGN} IMPORTED_TARGET) |
| 128 |
if((NOT ${library}_FOUND) AND (NOT ${use_system} STREQUAL "AUTO")) |
| 129 |
message(FATAL_ERROR "No system ${library} was found. Please install it or set USE_SYSTEM_${upperlib} to AUTO or OFF.") |
| 130 |
endif() |
| 131 |
endif() |
| 132 |
if(${library}_FOUND) |
| 133 |
dolphin_set_library_type(${library} "System") |
| 134 |
message(STATUS "Using system ${library}") |
| 135 |
dolphin_alias_library(${alias} PkgConfig::${library}) |
| 136 |
else() |
| 137 |
dolphin_set_library_type(${library} "Bundled") |
| 138 |
dolphin_add_bundled_library(${library} ${use_system} ${bundled_path}) |
| 139 |
endif() |
| 140 |
endfunction() |