FindIconv.cmake 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindIconv
  5. ---------
  6. This module finds the ``iconv()`` POSIX.1 functions on the system.
  7. These functions might be provided in the regular C library or externally
  8. in the form of an additional library.
  9. The following variables are provided to indicate iconv support:
  10. .. variable:: Iconv_FOUND
  11. Variable indicating if the iconv support was found.
  12. .. variable:: Iconv_INCLUDE_DIRS
  13. The directories containing the iconv headers.
  14. .. variable:: Iconv_LIBRARIES
  15. The iconv libraries to be linked.
  16. .. variable:: Iconv_IS_BUILT_IN
  17. A variable indicating whether iconv support is stemming from the
  18. C library or not. Even if the C library provides `iconv()`, the presence of
  19. an external `libiconv` implementation might lead to this being false.
  20. Additionally, the following :prop_tgt:`IMPORTED` target is being provided:
  21. .. variable:: Iconv::Iconv
  22. Imported target for using iconv.
  23. The following cache variables may also be set:
  24. .. variable:: Iconv_INCLUDE_DIR
  25. The directory containing the iconv headers.
  26. .. variable:: Iconv_LIBRARY
  27. The iconv library (if not implicitly given in the C library).
  28. .. note::
  29. On POSIX platforms, iconv might be part of the C library and the cache
  30. variables ``Iconv_INCLUDE_DIR`` and ``Iconv_LIBRARY`` might be empty.
  31. #]=======================================================================]
  32. include(${CMAKE_CURRENT_LIST_DIR}/CMakePushCheckState.cmake)
  33. if(CMAKE_C_COMPILER_LOADED)
  34. include(${CMAKE_CURRENT_LIST_DIR}/CheckCSourceCompiles.cmake)
  35. elseif(CMAKE_CXX_COMPILER_LOADED)
  36. include(${CMAKE_CURRENT_LIST_DIR}/CheckCXXSourceCompiles.cmake)
  37. else()
  38. # If neither C nor CXX are loaded, implicit iconv makes no sense.
  39. set(Iconv_IS_BUILT_IN FALSE)
  40. endif()
  41. # iconv can only be provided in libc on a POSIX system.
  42. # If any cache variable is already set, we'll skip this test.
  43. if(NOT DEFINED Iconv_IS_BUILT_IN)
  44. if(UNIX AND NOT DEFINED Iconv_INCLUDE_DIR AND NOT DEFINED Iconv_LIBRARY)
  45. cmake_push_check_state(RESET)
  46. # We always suppress the message here: Otherwise on supported systems
  47. # not having iconv in their C library (e.g. those using libiconv)
  48. # would always display a confusing "Looking for iconv - not found" message
  49. set(CMAKE_FIND_QUIETLY TRUE)
  50. # The following code will not work, but it's sufficient to see if it compiles.
  51. # Note: libiconv will define the iconv functions as macros, so CheckSymbolExists
  52. # will not yield correct results.
  53. set(Iconv_IMPLICIT_TEST_CODE
  54. "
  55. #include <stddef.h>
  56. #include <iconv.h>
  57. int main() {
  58. char *a, *b;
  59. size_t i, j;
  60. iconv_t ic;
  61. ic = iconv_open(\"to\", \"from\");
  62. iconv(ic, &a, &i, &b, &j);
  63. iconv_close(ic);
  64. }
  65. "
  66. )
  67. if(CMAKE_C_COMPILER_LOADED)
  68. check_c_source_compiles("${Iconv_IMPLICIT_TEST_CODE}" Iconv_IS_BUILT_IN)
  69. else()
  70. check_cxx_source_compiles("${Iconv_IMPLICIT_TEST_CODE}" Iconv_IS_BUILT_IN)
  71. endif()
  72. cmake_pop_check_state()
  73. else()
  74. set(Iconv_IS_BUILT_IN FALSE)
  75. endif()
  76. endif()
  77. if(NOT Iconv_IS_BUILT_IN)
  78. find_path(Iconv_INCLUDE_DIR
  79. NAMES "iconv.h"
  80. DOC "iconv include directory")
  81. set(Iconv_LIBRARY_NAMES "iconv" "libiconv")
  82. else()
  83. set(Iconv_INCLUDE_DIR "" CACHE FILEPATH "iconv include directory")
  84. set(Iconv_LIBRARY_NAMES "c")
  85. endif()
  86. find_library(Iconv_LIBRARY
  87. NAMES ${Iconv_LIBRARY_NAMES}
  88. DOC "iconv library (potentially the C library)")
  89. mark_as_advanced(Iconv_INCLUDE_DIR)
  90. mark_as_advanced(Iconv_LIBRARY)
  91. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  92. if(NOT Iconv_IS_BUILT_IN)
  93. find_package_handle_standard_args(Iconv REQUIRED_VARS Iconv_LIBRARY Iconv_INCLUDE_DIR)
  94. else()
  95. find_package_handle_standard_args(Iconv REQUIRED_VARS Iconv_LIBRARY)
  96. endif()
  97. if(Iconv_FOUND)
  98. set(Iconv_INCLUDE_DIRS "${Iconv_INCLUDE_DIR}")
  99. set(Iconv_LIBRARIES "${Iconv_LIBRARY}")
  100. if(NOT TARGET Iconv::Iconv)
  101. add_library(Iconv::Iconv INTERFACE IMPORTED)
  102. endif()
  103. set_property(TARGET Iconv::Iconv PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${Iconv_INCLUDE_DIRS}")
  104. set_property(TARGET Iconv::Iconv PROPERTY INTERFACE_LINK_LIBRARIES "${Iconv_LIBRARIES}")
  105. endif()