| 1 |
# Based on CMake's FindIconv.cmake |
| 2 |
# Modified to prefer non-built-in iconv over the built-in one |
| 3 |
# See https://gitlab.kitware.com/cmake/cmake/-/issues/24695 for details |
| 4 |
# This file can be deleted once that issue has been closed and the fix has |
| 5 |
# made it into a satisfactory number of cmake versions. FreeBSD is the only |
| 6 |
# system known to hit this so far, so "satisfactory" can probably be defined |
| 7 |
# as "enough that most FreeBSD users have a fixed cmake". |
| 8 |
|
| 9 |
find_path(Iconv_INCLUDE_DIR |
| 10 |
NAMES "iconv.h" |
| 11 |
DOC "iconv include directory") |
| 12 |
mark_as_advanced(Iconv_INCLUDE_DIR) |
| 13 |
|
| 14 |
find_library(Iconv_LIBRARY |
| 15 |
NAMES iconv libiconv |
| 16 |
NAMES_PER_DIR |
| 17 |
DOC "iconv library (if not in the C library)") |
| 18 |
mark_as_advanced(Iconv_LIBRARY) |
| 19 |
|
| 20 |
# iconv can only be provided in libc on a POSIX system. |
| 21 |
if(UNIX AND (NOT Iconv_INCLUDE_DIR OR NOT Iconv_LIBRARY)) |
| 22 |
include(CMakePushCheckState) |
| 23 |
include(CheckCXXSourceCompiles) |
| 24 |
cmake_push_check_state(RESET) |
| 25 |
# We always suppress the message here: Otherwise on supported systems |
| 26 |
# not having iconv in their C library (e.g. those using libiconv) |
| 27 |
# would always display a confusing "Looking for iconv - not found" message |
| 28 |
set(CMAKE_FIND_QUIETLY TRUE) |
| 29 |
# The following code will not work, but it's sufficient to see if it compiles. |
| 30 |
# Note: libiconv will define the iconv functions as macros, so CheckSymbolExists |
| 31 |
# will not yield correct results. |
| 32 |
set(Iconv_IMPLICIT_TEST_CODE |
| 33 |
" |
| 34 |
#include <stddef.h> |
| 35 |
#include <iconv.h> |
| 36 |
int main() { |
| 37 |
char *a, *b; |
| 38 |
size_t i, j; |
| 39 |
iconv_t ic; |
| 40 |
ic = iconv_open(\"to\", \"from\"); |
| 41 |
iconv(ic, &a, &i, &b, &j); |
| 42 |
iconv_close(ic); |
| 43 |
} |
| 44 |
" |
| 45 |
) |
| 46 |
check_cxx_source_compiles("${Iconv_IMPLICIT_TEST_CODE}" Iconv_IS_BUILT_IN) |
| 47 |
cmake_pop_check_state() |
| 48 |
if(Iconv_IS_BUILT_IN) |
| 49 |
unset(Iconv_INCLUDE_DIR) |
| 50 |
unset(Iconv_LIBRARY) |
| 51 |
endif() |
| 52 |
else() |
| 53 |
set(Iconv_IS_BUILT_IN FALSE) |
| 54 |
endif() |
| 55 |
|
| 56 |
set(_Iconv_REQUIRED_VARS) |
| 57 |
if(Iconv_IS_BUILT_IN) |
| 58 |
set(_Iconv_REQUIRED_VARS _Iconv_IS_BUILT_IN_MSG) |
| 59 |
set(_Iconv_IS_BUILT_IN_MSG "built in to C library") |
| 60 |
else() |
| 61 |
set(_Iconv_REQUIRED_VARS Iconv_LIBRARY Iconv_INCLUDE_DIR) |
| 62 |
endif() |
| 63 |
|
| 64 |
# NOTE: glibc's iconv.h does not define _LIBICONV_VERSION |
| 65 |
if(Iconv_INCLUDE_DIR AND EXISTS "${Iconv_INCLUDE_DIR}/iconv.h") |
| 66 |
file(STRINGS ${Iconv_INCLUDE_DIR}/iconv.h Iconv_VERSION_DEFINE REGEX "_LIBICONV_VERSION (.*)") |
| 67 |
|
| 68 |
if(Iconv_VERSION_DEFINE MATCHES "(0x[A-Fa-f0-9]+)") |
| 69 |
set(Iconv_VERSION_NUMBER "${CMAKE_MATCH_1}") |
| 70 |
# encoding -> version number: (major<<8) + minor |
| 71 |
math(EXPR Iconv_VERSION_MAJOR "${Iconv_VERSION_NUMBER} >> 8" OUTPUT_FORMAT HEXADECIMAL) |
| 72 |
math(EXPR Iconv_VERSION_MINOR "${Iconv_VERSION_NUMBER} - (${Iconv_VERSION_MAJOR} << 8)" OUTPUT_FORMAT HEXADECIMAL) |
| 73 |
|
| 74 |
math(EXPR Iconv_VERSION_MAJOR "${Iconv_VERSION_MAJOR}" OUTPUT_FORMAT DECIMAL) |
| 75 |
math(EXPR Iconv_VERSION_MINOR "${Iconv_VERSION_MINOR}" OUTPUT_FORMAT DECIMAL) |
| 76 |
set(Iconv_VERSION "${Iconv_VERSION_MAJOR}.${Iconv_VERSION_MINOR}") |
| 77 |
endif() |
| 78 |
|
| 79 |
unset(Iconv_VERSION_DEFINE) |
| 80 |
unset(Iconv_VERSION_NUMBER) |
| 81 |
endif() |
| 82 |
|
| 83 |
include(FindPackageHandleStandardArgs) |
| 84 |
find_package_handle_standard_args(Iconv |
| 85 |
REQUIRED_VARS ${_Iconv_REQUIRED_VARS} |
| 86 |
VERSION_VAR Iconv_VERSION) |
| 87 |
|
| 88 |
if(Iconv_FOUND) |
| 89 |
if(Iconv_IS_BUILT_IN) |
| 90 |
set(Iconv_INCLUDE_DIRS "") |
| 91 |
set(Iconv_LIBRARIES "") |
| 92 |
else() |
| 93 |
set(Iconv_INCLUDE_DIRS "${Iconv_INCLUDE_DIR}") |
| 94 |
set(Iconv_LIBRARIES "${Iconv_LIBRARY}") |
| 95 |
endif() |
| 96 |
if(NOT TARGET Iconv::Iconv) |
| 97 |
add_library(Iconv::Iconv INTERFACE IMPORTED) |
| 98 |
set_property(TARGET Iconv::Iconv PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${Iconv_INCLUDE_DIRS}") |
| 99 |
set_property(TARGET Iconv::Iconv PROPERTY INTERFACE_LINK_LIBRARIES "${Iconv_LIBRARIES}") |
| 100 |
endif() |
| 101 |
endif() |