FindIconv.cmake 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Based on CMake's FindIconv.cmake
  2. # Modified to prefer non-built-in iconv over the built-in one
  3. # See https://gitlab.kitware.com/cmake/cmake/-/issues/24695 for details
  4. # This file can be deleted once that issue has been closed and the fix has
  5. # made it into a satisfactory number of cmake versions. FreeBSD is the only
  6. # system known to hit this so far, so "satisfactory" can probably be defined
  7. # as "enough that most FreeBSD users have a fixed cmake".
  8. find_path(Iconv_INCLUDE_DIR
  9. NAMES "iconv.h"
  10. DOC "iconv include directory")
  11. mark_as_advanced(Iconv_INCLUDE_DIR)
  12. find_library(Iconv_LIBRARY
  13. NAMES iconv libiconv
  14. NAMES_PER_DIR
  15. DOC "iconv library (if not in the C library)")
  16. mark_as_advanced(Iconv_LIBRARY)
  17. # iconv can only be provided in libc on a POSIX system.
  18. if(UNIX AND (NOT Iconv_INCLUDE_DIR OR NOT Iconv_LIBRARY))
  19. include(CMakePushCheckState)
  20. include(CheckCXXSourceCompiles)
  21. cmake_push_check_state(RESET)
  22. # We always suppress the message here: Otherwise on supported systems
  23. # not having iconv in their C library (e.g. those using libiconv)
  24. # would always display a confusing "Looking for iconv - not found" message
  25. set(CMAKE_FIND_QUIETLY TRUE)
  26. # The following code will not work, but it's sufficient to see if it compiles.
  27. # Note: libiconv will define the iconv functions as macros, so CheckSymbolExists
  28. # will not yield correct results.
  29. set(Iconv_IMPLICIT_TEST_CODE
  30. "
  31. #include <stddef.h>
  32. #include <iconv.h>
  33. int main() {
  34. char *a, *b;
  35. size_t i, j;
  36. iconv_t ic;
  37. ic = iconv_open(\"to\", \"from\");
  38. iconv(ic, &a, &i, &b, &j);
  39. iconv_close(ic);
  40. }
  41. "
  42. )
  43. check_cxx_source_compiles("${Iconv_IMPLICIT_TEST_CODE}" Iconv_IS_BUILT_IN)
  44. cmake_pop_check_state()
  45. if(Iconv_IS_BUILT_IN)
  46. unset(Iconv_INCLUDE_DIR)
  47. unset(Iconv_LIBRARY)
  48. endif()
  49. else()
  50. set(Iconv_IS_BUILT_IN FALSE)
  51. endif()
  52. set(_Iconv_REQUIRED_VARS)
  53. if(Iconv_IS_BUILT_IN)
  54. set(_Iconv_REQUIRED_VARS _Iconv_IS_BUILT_IN_MSG)
  55. set(_Iconv_IS_BUILT_IN_MSG "built in to C library")
  56. else()
  57. set(_Iconv_REQUIRED_VARS Iconv_LIBRARY Iconv_INCLUDE_DIR)
  58. endif()
  59. # NOTE: glibc's iconv.h does not define _LIBICONV_VERSION
  60. if(Iconv_INCLUDE_DIR AND EXISTS "${Iconv_INCLUDE_DIR}/iconv.h")
  61. file(STRINGS ${Iconv_INCLUDE_DIR}/iconv.h Iconv_VERSION_DEFINE REGEX "_LIBICONV_VERSION (.*)")
  62. if(Iconv_VERSION_DEFINE MATCHES "(0x[A-Fa-f0-9]+)")
  63. set(Iconv_VERSION_NUMBER "${CMAKE_MATCH_1}")
  64. # encoding -> version number: (major<<8) + minor
  65. math(EXPR Iconv_VERSION_MAJOR "${Iconv_VERSION_NUMBER} >> 8" OUTPUT_FORMAT HEXADECIMAL)
  66. math(EXPR Iconv_VERSION_MINOR "${Iconv_VERSION_NUMBER} - (${Iconv_VERSION_MAJOR} << 8)" OUTPUT_FORMAT HEXADECIMAL)
  67. math(EXPR Iconv_VERSION_MAJOR "${Iconv_VERSION_MAJOR}" OUTPUT_FORMAT DECIMAL)
  68. math(EXPR Iconv_VERSION_MINOR "${Iconv_VERSION_MINOR}" OUTPUT_FORMAT DECIMAL)
  69. set(Iconv_VERSION "${Iconv_VERSION_MAJOR}.${Iconv_VERSION_MINOR}")
  70. endif()
  71. unset(Iconv_VERSION_DEFINE)
  72. unset(Iconv_VERSION_NUMBER)
  73. endif()
  74. include(FindPackageHandleStandardArgs)
  75. find_package_handle_standard_args(Iconv
  76. REQUIRED_VARS ${_Iconv_REQUIRED_VARS}
  77. VERSION_VAR Iconv_VERSION)
  78. if(Iconv_FOUND)
  79. if(Iconv_IS_BUILT_IN)
  80. set(Iconv_INCLUDE_DIRS "")
  81. set(Iconv_LIBRARIES "")
  82. else()
  83. set(Iconv_INCLUDE_DIRS "${Iconv_INCLUDE_DIR}")
  84. set(Iconv_LIBRARIES "${Iconv_LIBRARY}")
  85. endif()
  86. if(NOT TARGET Iconv::Iconv)
  87. add_library(Iconv::Iconv INTERFACE IMPORTED)
  88. set_property(TARGET Iconv::Iconv PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${Iconv_INCLUDE_DIRS}")
  89. set_property(TARGET Iconv::Iconv PROPERTY INTERFACE_LINK_LIBRARIES "${Iconv_LIBRARIES}")
  90. endif()
  91. endif()