From ef014f7ddf8fd603a4238f5ed4878d7038ce162d Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sun, 10 Nov 2013 03:18:08 +0100 Subject: Properly detect iconv features to compile --- CMakeLists.txt | 5 +++- cmake/Modules/FindIconv.cmake | 57 +++++++++++++++++++++++++++++++++++++++++++ src/utils/encoding.cpp | 9 +++++-- 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 cmake/Modules/FindIconv.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ec9dd0..f651723 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,14 +13,16 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Og -fsanitize=address") # set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") find_package(Cryptopp REQUIRED) +find_package(Iconv REQUIRED) include(FindEXPAT) find_package(EXPAT REQUIRED) include_directories("src/") +include_directories(${EXPAT_INCLUDE_DIRS}) +include_directories(${ICONV_INCLUDE_DIR}) # the SYSTEM flag tells the compiler that we don't care about warnings # coming from these headers. include_directories(SYSTEM ${CRYPTO++_INCLUDE_DIR}) -include_directories(SYSTEM ${EXPAT_INCLUDE_DIRS}) # ## utils @@ -28,6 +30,7 @@ include_directories(SYSTEM ${EXPAT_INCLUDE_DIRS}) file(GLOB source_utils src/utils/*.[hc]pp) add_library(utils STATIC ${source_utils}) +target_link_libraries(utils ${ICONV_LIBRARIES}) # ## network diff --git a/cmake/Modules/FindIconv.cmake b/cmake/Modules/FindIconv.cmake new file mode 100644 index 0000000..c957af6 --- /dev/null +++ b/cmake/Modules/FindIconv.cmake @@ -0,0 +1,57 @@ +# - Try to find Iconv +# Once done this will define +# +# ICONV_FOUND - system has Iconv +# ICONV_INCLUDE_DIR - the Iconv include directory +# ICONV_LIBRARIES - Link these to use Iconv +# ICONV_SECOND_ARGUMENT_IS_CONST - the second argument for iconv() is const +# +include(CheckCXXSourceCompiles) + +IF (ICONV_INCLUDE_DIR AND ICONV_LIBRARIES) + # Already in cache, be silent + SET(ICONV_FIND_QUIETLY TRUE) +ENDIF (ICONV_INCLUDE_DIR AND ICONV_LIBRARIES) + +FIND_PATH(ICONV_INCLUDE_DIR iconv.h) + +FIND_LIBRARY(ICONV_LIBRARIES NAMES iconv libiconv libiconv-2 c) + +IF(ICONV_INCLUDE_DIR AND ICONV_LIBRARIES) + SET(ICONV_FOUND TRUE) +ENDIF(ICONV_INCLUDE_DIR AND ICONV_LIBRARIES) + +set(CMAKE_REQUIRED_INCLUDES ${ICONV_INCLUDE_DIR}) +set(CMAKE_REQUIRED_LIBRARIES ${ICONV_LIBRARIES}) +IF(ICONV_FOUND) + check_cxx_source_compiles(" + #include + int main(){ + iconv_t conv = 0; + const char* in = 0; + size_t ilen = 0; + char* out = 0; + size_t olen = 0; + iconv(conv, &in, &ilen, &out, &olen); + return 0; + } +" ICONV_SECOND_ARGUMENT_IS_CONST ) +ENDIF(ICONV_FOUND) +set(CMAKE_REQUIRED_INCLUDES) +set(CMAKE_REQUIRED_LIBRARIES) + +IF(ICONV_FOUND) + IF(NOT ICONV_FIND_QUIETLY) + MESSAGE(STATUS "Found Iconv: ${ICONV_LIBRARIES}") + ENDIF(NOT ICONV_FIND_QUIETLY) +ELSE(ICONV_FOUND) + IF(Iconv_FIND_REQUIRED) + MESSAGE(FATAL_ERROR "Could not find Iconv") + ENDIF(Iconv_FIND_REQUIRED) +ENDIF(ICONV_FOUND) + +MARK_AS_ADVANCED( + ICONV_INCLUDE_DIR + ICONV_LIBRARIES + ICONV_SECOND_ARGUMENT_IS_CONST +) \ No newline at end of file diff --git a/src/utils/encoding.cpp b/src/utils/encoding.cpp index a1bc01b..2a6aecb 100644 --- a/src/utils/encoding.cpp +++ b/src/utils/encoding.cpp @@ -75,10 +75,15 @@ namespace utils // Make sure cd is always closed when we leave this function ScopeGuard sg([&]{ iconv_close(cd); }); - // iconv will not attempt to modify this buffer, but it still requires - // a char**. size_t inbytesleft = str.size(); + + // iconv will not attempt to modify this buffer, but some plateform + // require a char** anyway +#ifdef ICONV_SECOND_ARGUMENT_IS_CONST + const char* inbuf_ptr = str.c_str(); +#else char* inbuf_ptr = const_cast(str.c_str()); +#endif size_t outbytesleft = str.size() * 4; char* outbuf = new char[outbytesleft]; -- cgit v1.2.3