1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
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.
12 The following variables are provided to indicate iconv support:
14 .. variable:: Iconv_FOUND
16 Variable indicating if the iconv support was found.
18 .. variable:: Iconv_INCLUDE_DIRS
20 The directories containing the iconv headers.
22 .. variable:: Iconv_LIBRARIES
24 The iconv libraries to be linked.
26 .. variable:: Iconv_IS_BUILT_IN
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.
32 Additionally, the following :prop_tgt:`IMPORTED` target is being provided:
34 .. variable:: Iconv::Iconv
36 Imported target for using iconv.
38 The following cache variables may also be set:
40 .. variable:: Iconv_INCLUDE_DIR
42 The directory containing the iconv headers.
44 .. variable:: Iconv_LIBRARY
46 The iconv library (if not implicitly given in the C library).
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.
52 #]=======================================================================]
54 include(CMakePushCheckState)
55 if(CMAKE_C_COMPILER_LOADED)
56 include(CheckCSourceCompiles)
57 elseif(CMAKE_CXX_COMPILER_LOADED)
58 include(CheckCXXSourceCompiles)
60 # If neither C nor CXX are loaded, implicit iconv makes no sense.
61 set(Iconv_IS_BUILT_IN NO)
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
84 ic = iconv_open(\"to\", \"from\");
85 iconv(ic, &a, &i, &b, &j);
90 if(CMAKE_C_COMPILER_LOADED)
91 check_c_source_compiles("${Iconv_IMPLICIT_TEST_CODE}" Iconv_IS_BUILT_IN)
93 check_cxx_source_compiles("${Iconv_IMPLICIT_TEST_CODE}" Iconv_IS_BUILT_IN)
95 cmake_pop_check_state()
97 set(Iconv_IS_BUILT_IN NO)
101 if(NOT Iconv_IS_BUILT_IN)
102 find_path(Iconv_INCLUDE_DIR
104 DOC "iconv include directory")
105 set(Iconv_LIBRARY_NAMES "iconv" "libiconv")
107 set(Iconv_INCLUDE_DIR "" CACHE FILEPATH "iconv include directory")
108 set(Iconv_LIBRARY_NAMES "c")
111 find_library(Iconv_LIBRARY
112 NAMES ${Iconv_LIBRARY_NAMES}
113 DOC "iconv library (potentially the C library)")
115 mark_as_advanced(Iconv_INCLUDE_DIR)
116 mark_as_advanced(Iconv_LIBRARY)
118 include(FindPackageHandleStandardArgs)
119 if(NOT Iconv_IS_BUILT_IN)
120 find_package_handle_standard_args(Iconv REQUIRED_VARS Iconv_LIBRARY Iconv_INCLUDE_DIR)
122 find_package_handle_standard_args(Iconv REQUIRED_VARS Iconv_LIBRARY)
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)
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}")