1# Find the headers and library required for iconv functions.
2# Libc-based iconv test lifted from the upstream CMake FindIconv.cmake module.
3
4# First, try to find the iconv header, looking in the hinted directory first.
5find_path(ICONV_INCLUDE_DIR
6    NAMES iconv.h
7    HINTS ${ICONV_INCLUDE_DIR_HINT}
8)
9
10if (ICONV_INCLUDE_DIR)
11    # Test to see if iconv is available from libc and matches the header we found.
12    # Assume that an explicit include dir or library dir hint means we're not going
13    # to be using a libc implementation.
14    if (UNIX AND NOT ICONV_INCLUDE_DIR_HINT AND NOT ICONV_LIBRARIES_DIR_HINT)
15        include(CMakePushCheckState)
16        cmake_push_check_state(RESET)
17        # Make sure we're using the iconv.h we found above
18        set(CMAKE_REQUIRED_INCLUDES ${ICONV_INCLUDE_DIR})
19        # We always suppress the message here: Otherwise on supported systems
20        # not having iconv in their C library (e.g. those using libiconv)
21        # would always display a confusing "Looking for iconv - not found" message
22        set(CMAKE_FIND_QUIETLY TRUE)
23        # The following code will not work, but it's sufficient to see if it compiles.
24        # Note: libiconv will define the iconv functions as macros, so CheckSymbolExists
25        # will not yield correct results.
26        set(ICONV_IMPLICIT_TEST_CODE
27            "
28            #include <stddef.h>
29            #include <iconv.h>
30            int main() {
31                char *a, *b;
32                size_t i, j;
33                iconv_t ic;
34                ic = iconv_open(\"to\", \"from\");
35                iconv(ic, &a, &i, &b, &j);
36                iconv_close(ic);
37            }
38            "
39        )
40        if (CMAKE_C_COMPILER_LOADED)
41            include(CheckCSourceCompiles)
42            check_c_source_compiles("${ICONV_IMPLICIT_TEST_CODE}" ICONV_IS_BUILT_IN)
43        elseif (CMAKE_CXX_COMPILER_LOADED)
44            include(CheckCXXSourceCompiles)
45            check_cxx_source_compiles("${ICONV_IMPLICIT_TEST_CODE}" ICONV_IS_BUILT_IN)
46        endif()
47        cmake_pop_check_state()
48    endif()
49
50    if (NOT ICONV_IS_BUILT_IN)
51        find_library(ICONV_LIBRARY
52            NAMES iconv libiconv
53            HINTS ${ICONV_LIBRARIES_DIR_HINT}
54        )
55    endif()
56else()
57    unset(ICONV_INCLUDE_DIR)
58endif()
59
60include(FindPackageHandleStandardArgs)
61if (NOT ICONV_IS_BUILT_IN)
62  find_package_handle_standard_args(ICONV REQUIRED_VARS ICONV_LIBRARY ICONV_INCLUDE_DIR)
63else()
64  find_package_handle_standard_args(ICONV REQUIRED_VARS ICONV_INCLUDE_DIR)
65endif()
66
67mark_as_advanced(ICONV_INCLUDE_DIR)
68mark_as_advanced(ICONV_LIBRARY)
69
70