CMake: Support using bundled libraries even if system libraries exist

This commit is contained in:
TellowKrinkle 2023-04-16 02:46:42 -05:00
parent 5b10a80401
commit ade40650f5
3 changed files with 102 additions and 179 deletions

View File

@ -1,29 +0,0 @@
# When packaging Dolphin for an OS distribution, distro vendors usually prefer
# to limit vendored ("Externals") dependencies as much as possible, in favor of
# using system provided libraries. This modules provides an option to allow
# only specific vendored dependencies and error-out at configuration time for
# non-approved ones.
#
# Usage:
# $ cmake -D APPROVED_VENDORED_DEPENDENCIES="a;b;c;..."
#
# Unless the option is explicitly used, vendored dependencies control is
# disabled.
#
# If you want to disallow all vendored dependencies, put "none" in the approved
# dependencies list.
set(APPROVED_VENDORED_DEPENDENCIES "" CACHE STRING "\
Semicolon separated list of approved vendored dependencies. See docstring in \
CMake/CheckVendoringApproved.cmake.")
function(check_vendoring_approved dep)
if(APPROVED_VENDORED_DEPENDENCIES)
if(NOT dep IN_LIST APPROVED_VENDORED_DEPENDENCIES)
message(SEND_ERROR "\
Library ${dep} was not found systemwide and was not approved for vendoring. \
Vendored dependencies control is enabled. Add \"${dep}\" to the \
APPROVED_VENDORED_DEPENDENCIES list to bypass this error.")
endif()
endif()
endfunction()

View File

@ -18,3 +18,78 @@ function(dolphin_make_imported_target_if_missing target lib)
add_library(${target} ALIAS _${lib})
endif()
endfunction()
function(dolphin_optional_system_library 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(RESOLVED_USE_SYSTEM_${upperlib} AUTO PARENT_SCOPE)
else()
set(RESOLVED_USE_SYSTEM_${upperlib} ON PARENT_SCOPE)
endif()
else()
set(RESOLVED_USE_SYSTEM_${upperlib} ${USE_SYSTEM_LIBS} PARENT_SCOPE)
endif()
else()
set(RESOLVED_USE_SYSTEM_${upperlib} ${USE_SYSTEM_${upperlib}} PARENT_SCOPE)
endif()
endfunction()
function(dolphin_add_bundled_library library bundled_path)
string(TOUPPER ${library} upperlib)
if (${RESOLVED_USE_SYSTEM_${upperlib}} 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)
endfunction()
function(dolphin_find_optional_system_library library bundled_path)
dolphin_optional_system_library(${library})
string(TOUPPER ${library} upperlib)
if(RESOLVED_USE_SYSTEM_${upperlib})
find_package(${library} ${ARGN})
# Yay for cmake packages being inconsistent
if(DEFINED ${library}_FOUND)
set(prefix ${library})
else()
set(prefix ${upperlib})
endif()
if((NOT ${found}) AND (NOT ${RESOLVED_USE_SYSTEM_${upperlib}} 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)
message(STATUS "Using system ${library}")
set(${prefix}_TYPE "System" PARENT_SCOPE)
else()
dolphin_add_bundled_library(${library} ${bundled_path})
set(${prefix}_TYPE "Bundled" PARENT_SCOPE)
endif()
endfunction()
function(dolphin_find_optional_system_library_pkgconfig library search alias bundled_path)
dolphin_optional_system_library(${library})
string(TOUPPER ${library} upperlib)
if(RESOLVED_USE_SYSTEM_${upperlib})
pkg_check_modules(${library} ${search} ${ARGN} IMPORTED_TARGET)
if((NOT ${library}_FOUND) AND (NOT ${RESOLVED_USE_SYSTEM_${upperlib}} 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)
message(STATUS "Using system ${library}")
dolphin_alias_library(${alias} PkgConfig::${library})
set(${library}_TYPE "System" PARENT_SCOPE)
else()
dolphin_add_bundled_library(${library} ${bundled_path})
set(${library}_TYPE "Bundled" PARENT_SCOPE)
endif()
endfunction()

View File

@ -85,6 +85,11 @@ if(NOT ANDROID)
option(ENABLE_CLI_TOOL "Enable dolphin-tool, a CLI-based utility for functions such as managing disc images" ON)
endif()
set(USE_SYSTEM_LIBS "AUTO" CACHE STRING "Use system libraries instead of bundled libraries. ON - Always use system and fail if unavailable, OFF - Always use bundled, AUTO - Use system if available, otherwise use bundled. Default is AUTO")
if(APPROVED_VENDORED_DEPENDENCIES)
message(WARNING "APPROVED_VENDORED_DEPENDENCIES is deprecated. Please migrate to setting USE_SYSTEM_LIBS to ON and setting USE_SYSTEM_<dependency> to either AUTO or OFF to allow bundled libs.")
endif()
option(USE_UPNP "Enables UPnP port mapping support" ON)
option(ENABLE_NOGUI "Enable NoGUI frontend" ON)
option(ENABLE_QT "Enable Qt (Default)" ON)
@ -157,7 +162,6 @@ list(APPEND CMAKE_MODULE_PATH
# Support functions
include(CheckAndAddFlag)
include(CheckCCompilerFlag)
include(CheckVendoringApproved)
include(DolphinCompileDefinitions)
include(DolphinDisableWarningsMSVC)
include(DolphinLibraryTools)
@ -638,14 +642,7 @@ if(UNIX)
endif()
if(ENABLE_SDL)
find_package(SDL2)
if(SDL2_FOUND)
message(STATUS "Using system SDL2")
else()
message(STATUS "Using static SDL2 from Externals")
add_subdirectory(Externals/SDL)
endif()
dolphin_find_optional_system_library(SDL2 Externals/SDL)
endif()
if(ENABLE_ANALYTICS)
@ -686,14 +683,8 @@ if (_M_X86)
endif()
add_subdirectory(Externals/cpp-optparse)
find_package(fmt 8)
if(fmt_FOUND)
message(STATUS "Using shared fmt ${fmt_VERSION}")
else()
check_vendoring_approved(fmt)
message(STATUS "Using static fmt from Externals")
add_subdirectory(Externals/fmt EXCLUDE_FROM_ALL)
endif()
dolphin_find_optional_system_library(fmt Externals/fmt 8)
add_subdirectory(Externals/imgui)
add_subdirectory(Externals/implot)
add_subdirectory(Externals/glslang)
@ -721,88 +712,30 @@ if(NOT WIN32 OR (NOT (CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")))
add_definitions(-DHAS_OPENGL)
endif()
find_package(pugixml)
if(NOT pugixml_FOUND)
check_vendoring_approved(pugixml)
message(STATUS "Using static pugixml from Externals")
add_subdirectory(Externals/pugixml)
endif()
dolphin_find_optional_system_library(pugixml Externals/pugixml)
pkg_check_modules(ENET libenet>=1.3.8 IMPORTED_TARGET)
if(ENET_FOUND)
message(STATUS "Using shared enet")
dolphin_alias_library(enet::enet PkgConfig::ENET)
else()
check_vendoring_approved(enet)
message(STATUS "Using static enet from Externals")
add_subdirectory(Externals/enet)
endif()
dolphin_find_optional_system_library_pkgconfig(ENET libenet>=1.3.8 enet::enet Externals/enet)
if(NOT XXHASH_FOUND)
message(STATUS "Using static xxhash from Externals")
add_subdirectory(Externals/xxhash)
endif()
find_package(BZip2)
if(BZIP2_FOUND)
message(STATUS "Using shared bzip2")
else()
check_vendoring_approved(bzip2)
message(STATUS "Shared bzip2 not found, falling back to the static library")
add_subdirectory(Externals/bzip2)
endif()
dolphin_find_optional_system_library(BZip2 Externals/bzip2)
# macOS ships with liblzma.dylib but no headers, so check for the headers too
find_package(LibLZMA)
if(LIBLZMA_FOUND)
dolphin_find_optional_system_library(LibLZMA Externals/liblzma)
# Imported target added in CMake 3.14
dolphin_make_imported_target_if_missing(LibLZMA::LibLZMA LIBLZMA)
message(STATUS "Using shared lzma")
else()
check_vendoring_approved(lzma)
message(STATUS "Shared lzma not found, falling back to the static library")
add_subdirectory(Externals/liblzma)
endif()
pkg_check_modules(ZSTD QUIET libzstd>=1.4.0 IMPORTED_TARGET)
if(ZSTD_FOUND)
message(STATUS "Using shared zstd version: " ${ZSTD_VERSION})
dolphin_alias_library(zstd::zstd PkgConfig::ZSTD)
else()
check_vendoring_approved(zstd)
message(STATUS "Shared zstd not found, falling back to the static library")
add_subdirectory(Externals/zstd)
endif()
dolphin_find_optional_system_library_pkgconfig(ZSTD libzstd>=1.4.0 zstd::zstd Externals/zstd)
add_subdirectory(Externals/zlib-ng)
pkg_check_modules(MINIZIP minizip>=3.0.0 IMPORTED_TARGET)
if(MINIZIP_FOUND)
message(STATUS "Using shared minizip")
dolphin_alias_library(minizip::minizip PkgConfig::MINIZIP)
else()
check_vendoring_approved(minizip)
message(STATUS "Shared minizip not found, falling back to the static library")
add_subdirectory(Externals/minizip)
endif()
dolphin_find_optional_system_library_pkgconfig(MINIZIP minizip>=3.0.0 minizip::minizip Externals/minizip)
find_package(LZO)
if(LZO_FOUND)
message(STATUS "Using shared lzo")
else()
check_vendoring_approved(lzo)
message(STATUS "Using static lzo from Externals")
add_subdirectory(Externals/LZO EXCLUDE_FROM_ALL)
endif()
dolphin_find_optional_system_library(LZO Externals/LZO)
pkg_check_modules(pc_spng IMPORTED_TARGET spng)
if (pc_spng_FOUND AND TARGET PkgConfig::pc_spng)
message(STATUS "Using the system libspng")
dolphin_alias_library(spng::spng PkgConfig::pc_spng)
else()
message(STATUS "Using static libspng from Externals")
add_subdirectory(Externals/libspng)
endif()
dolphin_find_optional_system_library_pkgconfig(SPNG spng spng::spng Externals/libspng)
# Using static FreeSurround from Externals
# There is no system FreeSurround library.
@ -820,85 +753,33 @@ endif()
add_subdirectory(Externals/soundtouch)
include_directories(Externals/soundtouch)
find_package(CUBEB)
if(CUBEB_FOUND)
message(STATUS "Using the system cubeb")
else()
check_vendoring_approved(cubeb)
message(STATUS "Using static cubeb from Externals")
add_subdirectory(Externals/cubeb EXCLUDE_FROM_ALL)
endif()
dolphin_find_optional_system_library(CUBEB Externals/cubeb)
if(NOT ANDROID)
dolphin_find_optional_system_library(LibUSB Externals/libusb)
add_definitions(-D__LIBUSB__)
find_package(LibUSB)
if(LIBUSB_FOUND)
message(STATUS "Using shared LibUSB")
else()
check_vendoring_approved(libusb)
message(STATUS "Using static LibUSB from Externals")
add_subdirectory(Externals/libusb EXCLUDE_FROM_ALL)
endif()
endif()
find_package(SFML 2.1 COMPONENTS network system)
if(SFML_FOUND)
message(STATUS "Using shared SFML")
else()
check_vendoring_approved(sfml)
message(STATUS "Using static SFML from Externals")
add_subdirectory(Externals/SFML)
endif()
dolphin_find_optional_system_library(SFML Externals/SFML 2.1 COMPONENTS network system)
if(USE_UPNP)
find_package(MINIUPNPC 1.6)
if(MINIUPNPC_FOUND)
message(STATUS "Using shared miniupnpc")
else()
check_vendoring_approved(miniupnpc)
message(STATUS "Using static miniupnpc from Externals")
add_subdirectory(Externals/miniupnpc)
endif()
dolphin_find_optional_system_library(MINIUPNPC Externals/miniupnpc 1.6)
add_definitions(-DUSE_UPNP)
endif()
find_package(MBEDTLS 2.28)
if(MBEDTLS_FOUND)
message(STATUS "Using shared mbed TLS")
else()
check_vendoring_approved(mbedtls)
message(STATUS "Using static mbed TLS from Externals")
add_subdirectory(Externals/mbedtls/ EXCLUDE_FROM_ALL)
endif()
dolphin_find_optional_system_library(MBEDTLS Externals/mbedtls 2.28)
find_package(CURL)
if(CURL_FOUND)
message(STATUS "Using shared libcurl")
else()
check_vendoring_approved(curl)
message(STATUS "Using static libcurl from Externals")
add_subdirectory(Externals/curl EXCLUDE_FROM_ALL)
endif()
dolphin_find_optional_system_library(CURL Externals/curl)
if(NOT ANDROID)
find_package(Iconv)
endif()
if(TARGET Iconv::Iconv)
message(STATUS "Using shared iconv")
dolphin_find_optional_system_library(Iconv Externals/libiconv-1.14)
else()
check_vendoring_approved(iconv)
message(STATUS "Using static iconv from Externals")
add_subdirectory(Externals/libiconv-1.14 EXCLUDE_FROM_ALL)
endif()
if(NOT ANDROID)
find_package(HIDAPI)
if(NOT HIDAPI_FOUND)
check_vendoring_approved(hidapi)
message(STATUS "Using static HIDAPI from Externals")
add_subdirectory(Externals/hidapi EXCLUDE_FROM_ALL)
endif()
dolphin_find_optional_system_library(HIDAPI Externals/hidapi)
endif()
if(USE_DISCORD_PRESENCE)
@ -911,11 +792,7 @@ if(NOT ENABLE_QT)
set(USE_MGBA 0)
endif()
if(USE_MGBA)
find_package(LIBMGBA)
if(NOT LIBMGBA_FOUND)
message(STATUS "Using static libmgba from Externals")
add_subdirectory(Externals/mGBA)
endif()
dolphin_find_optional_system_library(LIBMGBA Externals/mGBA)
endif()
find_package(SYSTEMD)