diff --git a/CMake/FindIconv.cmake b/CMake/FindIconv.cmake new file mode 100644 index 000000000000..d0b86fc7cbd2 --- /dev/null +++ b/CMake/FindIconv.cmake @@ -0,0 +1,101 @@ +# Based on CMake's FindIconv.cmake +# Modified to prefer non-built-in iconv over the built-in one +# See https://gitlab.kitware.com/cmake/cmake/-/issues/24695 for details +# This file can be deleted once that issue has been closed and the fix has +# made it into a satisfactory number of cmake versions. FreeBSD is the only +# system known to hit this so far, so "satisfactory" can probably be defined +# as "enough that most FreeBSD users have a fixed cmake". + +find_path(Iconv_INCLUDE_DIR + NAMES "iconv.h" + DOC "iconv include directory") +mark_as_advanced(Iconv_INCLUDE_DIR) + +find_library(Iconv_LIBRARY + NAMES iconv libiconv + NAMES_PER_DIR + DOC "iconv library (if not in the C library)") +mark_as_advanced(Iconv_LIBRARY) + +# iconv can only be provided in libc on a POSIX system. +if(UNIX AND (NOT Iconv_INCLUDE_DIR OR NOT Iconv_LIBRARY)) + include(CMakePushCheckState) + include(CheckCXXSourceCompiles) + cmake_push_check_state(RESET) + # We always suppress the message here: Otherwise on supported systems + # not having iconv in their C library (e.g. those using libiconv) + # would always display a confusing "Looking for iconv - not found" message + set(CMAKE_FIND_QUIETLY TRUE) + # The following code will not work, but it's sufficient to see if it compiles. + # Note: libiconv will define the iconv functions as macros, so CheckSymbolExists + # will not yield correct results. + set(Iconv_IMPLICIT_TEST_CODE + " + #include + #include + int main() { + char *a, *b; + size_t i, j; + iconv_t ic; + ic = iconv_open(\"to\", \"from\"); + iconv(ic, &a, &i, &b, &j); + iconv_close(ic); + } + " + ) + check_cxx_source_compiles("${Iconv_IMPLICIT_TEST_CODE}" Iconv_IS_BUILT_IN) + cmake_pop_check_state() + if(Iconv_IS_BUILT_IN) + unset(Iconv_INCLUDE_DIR) + unset(Iconv_LIBRARY) + endif() +else() + set(Iconv_IS_BUILT_IN FALSE) +endif() + +set(_Iconv_REQUIRED_VARS) +if(Iconv_IS_BUILT_IN) + set(_Iconv_REQUIRED_VARS _Iconv_IS_BUILT_IN_MSG) + set(_Iconv_IS_BUILT_IN_MSG "built in to C library") +else() + set(_Iconv_REQUIRED_VARS Iconv_LIBRARY Iconv_INCLUDE_DIR) +endif() + +# NOTE: glibc's iconv.h does not define _LIBICONV_VERSION +if(Iconv_INCLUDE_DIR AND EXISTS "${Iconv_INCLUDE_DIR}/iconv.h") + file(STRINGS ${Iconv_INCLUDE_DIR}/iconv.h Iconv_VERSION_DEFINE REGEX "_LIBICONV_VERSION (.*)") + + if(Iconv_VERSION_DEFINE MATCHES "(0x[A-Fa-f0-9]+)") + set(Iconv_VERSION_NUMBER "${CMAKE_MATCH_1}") + # encoding -> version number: (major<<8) + minor + math(EXPR Iconv_VERSION_MAJOR "${Iconv_VERSION_NUMBER} >> 8" OUTPUT_FORMAT HEXADECIMAL) + math(EXPR Iconv_VERSION_MINOR "${Iconv_VERSION_NUMBER} - (${Iconv_VERSION_MAJOR} << 8)" OUTPUT_FORMAT HEXADECIMAL) + + math(EXPR Iconv_VERSION_MAJOR "${Iconv_VERSION_MAJOR}" OUTPUT_FORMAT DECIMAL) + math(EXPR Iconv_VERSION_MINOR "${Iconv_VERSION_MINOR}" OUTPUT_FORMAT DECIMAL) + set(Iconv_VERSION "${Iconv_VERSION_MAJOR}.${Iconv_VERSION_MINOR}") + endif() + + unset(Iconv_VERSION_DEFINE) + unset(Iconv_VERSION_NUMBER) +endif() + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(Iconv + REQUIRED_VARS ${_Iconv_REQUIRED_VARS} + VERSION_VAR Iconv_VERSION) + +if(Iconv_FOUND) + if(Iconv_IS_BUILT_IN) + set(Iconv_INCLUDE_DIRS "") + set(Iconv_LIBRARIES "") + else() + set(Iconv_INCLUDE_DIRS "${Iconv_INCLUDE_DIR}") + set(Iconv_LIBRARIES "${Iconv_LIBRARY}") + endif() + if(NOT TARGET Iconv::Iconv) + add_library(Iconv::Iconv INTERFACE IMPORTED) + set_property(TARGET Iconv::Iconv PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${Iconv_INCLUDE_DIRS}") + set_property(TARGET Iconv::Iconv PROPERTY INTERFACE_LINK_LIBRARIES "${Iconv_LIBRARIES}") + endif() +endif() diff --git a/CMakeLists.txt b/CMakeLists.txt index 06ddaf127f79..dcb7f1b94a9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -937,19 +937,16 @@ else() include_directories(BEFORE Externals/curl/include) endif() -if (NOT ANDROID) - find_library(ICONV_LIBRARIES NAMES iconv libiconv libiconv-2 c) - find_path(ICONV_INCLUDE_DIR NAMES iconv.h) +if(NOT ANDROID) + find_package(Iconv) endif() -if (NOT ANDROID AND ICONV_LIBRARIES AND ICONV_INCLUDE_DIR) - mark_as_advanced(ICONV_INCLUDE_DIR ICONV_LIBRARIES) +if(TARGET Iconv::Iconv) + message(STATUS "Using shared iconv") else() check_vendoring_approved(iconv) message(STATUS "Using static iconv from Externals") - include_directories(Externals/libiconv-1.14/include) add_subdirectory(Externals/libiconv-1.14) - set(ICONV_LIBRARIES iconv) endif() if(NOT ANDROID) diff --git a/Externals/libiconv-1.14/CMakeLists.txt b/Externals/libiconv-1.14/CMakeLists.txt index 571a6f791c3b..9f04f671ff7a 100644 --- a/Externals/libiconv-1.14/CMakeLists.txt +++ b/Externals/libiconv-1.14/CMakeLists.txt @@ -1,10 +1,13 @@ -include_directories(include) -include_directories(libcharset/include) - -set(SRCS lib/iconv.c - lib/relocatable.c - libcharset/lib/localcharset.c +add_library(iconv STATIC + lib/iconv.c + lib/relocatable.c + libcharset/lib/localcharset.c +) +target_include_directories(iconv + PUBLIC + include + PRIVATE + libcharset/include ) - -add_library(iconv STATIC ${SRCS}) dolphin_disable_warnings_msvc(iconv) +add_library(Iconv::Iconv ALIAS iconv) diff --git a/Source/Core/Common/CMakeLists.txt b/Source/Core/Common/CMakeLists.txt index 9c642e7ec5fe..dfee159a7cb9 100644 --- a/Source/Core/Common/CMakeLists.txt +++ b/Source/Core/Common/CMakeLists.txt @@ -153,7 +153,7 @@ PUBLIC PRIVATE ${CURL_LIBRARIES} FatFs - ${ICONV_LIBRARIES} + Iconv::Iconv ${spng_target} ${VTUNE_LIBRARIES} )