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