|
| 1 | +# From https://github.com/Kitware/CMake/blob/master/Modules/FindIconv.cmake |
| 2 | +# |
| 3 | +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
| 4 | +# file Copyright.txt or https://cmake.org/licensing for details. |
| 5 | + |
| 6 | +# Mods by Micah Snyder to support systems with both libc's iconv + libconv |
| 7 | + |
| 8 | +#[=======================================================================[.rst: |
| 9 | +FindICONV |
| 10 | +--------- |
| 11 | +
|
| 12 | +.. versionadded:: 3.11 |
| 13 | +
|
| 14 | +This module finds the ``iconv()`` POSIX.1 functions on the system. |
| 15 | +These functions might be provided in the regular C library or externally |
| 16 | +in the form of an additional library. |
| 17 | +
|
| 18 | +The following variables are provided to indicate iconv support: |
| 19 | +
|
| 20 | +.. variable:: ICONV_FOUND |
| 21 | +
|
| 22 | + Variable indicating if the iconv support was found. |
| 23 | +
|
| 24 | +.. variable:: ICONV_CONST |
| 25 | +
|
| 26 | + Variable to use when declaring "in" char* variable for the iconv() function |
| 27 | + so that it is const if the iconv implementation expects it to be const. |
| 28 | +
|
| 29 | +.. variable:: ICONV_INCLUDE_DIRS |
| 30 | +
|
| 31 | + The directories containing the iconv headers. |
| 32 | +
|
| 33 | +.. variable:: ICONV_LIBRARIES |
| 34 | +
|
| 35 | + The iconv libraries to be linked. |
| 36 | +
|
| 37 | +.. variable:: ICONV_IS_BUILT_IN |
| 38 | +
|
| 39 | + A variable indicating whether iconv support is stemming from the |
| 40 | + C library or not. Even if the C library provides `iconv()`, the presence of |
| 41 | + an external `libiconv` implementation might lead to this being false. |
| 42 | +
|
| 43 | +Additionally, the following :prop_tgt:`IMPORTED` target is being provided: |
| 44 | +
|
| 45 | +.. variable:: ICONV::Iconv |
| 46 | +
|
| 47 | + Imported target for using iconv. |
| 48 | +
|
| 49 | +The following cache variables may also be set: |
| 50 | +
|
| 51 | +.. variable:: ICONV_INCLUDE_DIR |
| 52 | +
|
| 53 | + The directory containing the iconv headers. |
| 54 | +
|
| 55 | +.. variable:: ICONV_LIBRARY |
| 56 | +
|
| 57 | + The iconv library (if not implicitly given in the C library). |
| 58 | +
|
| 59 | +.. note:: |
| 60 | + On POSIX platforms, iconv might be part of the C library and the cache |
| 61 | + variables ``ICONV_INCLUDE_DIR`` and ``ICONV_LIBRARY`` might be empty. |
| 62 | +
|
| 63 | +#]=======================================================================] |
| 64 | + |
| 65 | +include(CMakePushCheckState) |
| 66 | +include(CheckCSourceCompiles) |
| 67 | +include(CheckCXXSourceCompiles) |
| 68 | + |
| 69 | +# iconv can only be provided in libc on a POSIX system. |
| 70 | +# If any cache variable is already set, we'll skip this test. |
| 71 | +if(NOT DEFINED ICONV_IS_BUILT_IN) |
| 72 | + # Check for iconv.h first. |
| 73 | + # If it's not the built-in one, then ICONV_INCLUDE_DIR will |
| 74 | + find_path(ICONV_INCLUDE_DIR |
| 75 | + NAMES "iconv.h" |
| 76 | + DOC "iconv include directory") |
| 77 | + set(ICONV_LIBRARY_NAMES "iconv" "libiconv") |
| 78 | + |
| 79 | + if(UNIX AND ICONV_INCLUDE_DIR AND NOT DEFINED ICONV_LIBRARY) |
| 80 | + cmake_push_check_state(RESET) |
| 81 | + # We always suppress the message here: Otherwise on supported systems |
| 82 | + # not having iconv in their C library (e.g. those using libiconv) |
| 83 | + # would always display a confusing "Looking for iconv - not found" message |
| 84 | + set(CMAKE_FIND_QUIETLY TRUE) |
| 85 | + # The following code will not work, but it's sufficient to see if it compiles. |
| 86 | + # Note: libiconv will define the iconv functions as macros, so CheckSymbolExists |
| 87 | + # will not yield correct results. |
| 88 | + set(ICONV_IMPLICIT_TEST_CODE |
| 89 | + " |
| 90 | + #include <stddef.h> |
| 91 | + #include <iconv.h> |
| 92 | + int main() { |
| 93 | + char *a, *b; |
| 94 | + size_t i, j; |
| 95 | + iconv_t ic; |
| 96 | + ic = iconv_open(\"to\", \"from\"); |
| 97 | + iconv(ic, &a, &i, &b, &j); |
| 98 | + iconv_close(ic); |
| 99 | + } |
| 100 | + " |
| 101 | + ) |
| 102 | + |
| 103 | + # Make sure we're using the iconv.h we found above. This way we don't |
| 104 | + # accidentally compile against libiconv's header later but link with only |
| 105 | + # libc on systems that have both (eg FreeBSD with libiconv pkg installed). |
| 106 | + set(CMAKE_REQUIRED_INCLUDES ${ICONV_INCLUDE_DIR}) |
| 107 | + |
| 108 | + if(CMAKE_C_COMPILER_LOADED) |
| 109 | + check_c_source_compiles("${ICONV_IMPLICIT_TEST_CODE}" ICONV_IS_BUILT_IN) |
| 110 | + else() |
| 111 | + check_cxx_source_compiles("${ICONV_IMPLICIT_TEST_CODE}" ICONV_IS_BUILT_IN) |
| 112 | + endif() |
| 113 | + cmake_pop_check_state() |
| 114 | + else() |
| 115 | + set(ICONV_IS_BUILT_IN FALSE) |
| 116 | + endif() |
| 117 | +endif() |
| 118 | + |
| 119 | +if(ICONV_IS_BUILT_IN) |
| 120 | + set(ICONV_INCLUDE_DIR "" CACHE FILEPATH "iconv include directory") |
| 121 | + set(ICONV_LIBRARY_NAMES "c") |
| 122 | +endif() |
| 123 | + |
| 124 | +find_library(ICONV_LIBRARY |
| 125 | + NAMES ${ICONV_LIBRARY_NAMES} |
| 126 | + NAMES_PER_DIR |
| 127 | + DOC "iconv library (potentially the C library)") |
| 128 | + |
| 129 | +mark_as_advanced(ICONV_INCLUDE_DIR) |
| 130 | +mark_as_advanced(ICONV_LIBRARY) |
| 131 | + |
| 132 | +include(FindPackageHandleStandardArgs) |
| 133 | +if(NOT ICONV_IS_BUILT_IN) |
| 134 | + find_package_handle_standard_args(ICONV REQUIRED_VARS ICONV_LIBRARY ICONV_INCLUDE_DIR) |
| 135 | +else() |
| 136 | + find_package_handle_standard_args(ICONV REQUIRED_VARS ICONV_LIBRARY) |
| 137 | +endif() |
| 138 | + |
| 139 | +if(ICONV_FOUND) |
| 140 | + # Check if the second argument for iconv() needs to be const |
| 141 | + set(OLD_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") |
| 142 | + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror") |
| 143 | + check_c_source_compiles(" |
| 144 | + #include <iconv.h> |
| 145 | + int main() { |
| 146 | + iconv_t conv = 0; |
| 147 | + const char* in = 0; |
| 148 | + size_t ilen = 0; |
| 149 | + char* out = 0; |
| 150 | + size_t olen = 0; |
| 151 | + iconv(conv, &in, &ilen, &out, &olen); |
| 152 | + return 0; |
| 153 | + } |
| 154 | +" ICONV_SECOND_ARGUMENT_IS_CONST ) |
| 155 | + set(CMAKE_REQUIRED_FLAGS "${OLD_CMAKE_REQUIRED_FLAGS}") |
| 156 | + |
| 157 | + if(ICONV_SECOND_ARGUMENT_IS_CONST) |
| 158 | + set(ICONV_CONST "const") |
| 159 | + endif(ICONV_SECOND_ARGUMENT_IS_CONST) |
| 160 | + |
| 161 | + set(ICONV_INCLUDE_DIRS "${ICONV_INCLUDE_DIR}") |
| 162 | + set(ICONV_LIBRARIES "${ICONV_LIBRARY}") |
| 163 | + if(NOT TARGET ICONV::Iconv) |
| 164 | + add_library(ICONV::Iconv INTERFACE IMPORTED) |
| 165 | + endif() |
| 166 | + set_property(TARGET ICONV::Iconv PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${ICONV_INCLUDE_DIRS}") |
| 167 | + set_property(TARGET ICONV::Iconv PROPERTY INTERFACE_LINK_LIBRARIES "${ICONV_LIBRARIES}") |
| 168 | +endif() |
0 commit comments