~singpolyma/biboumi

ef014f7ddf8fd603a4238f5ed4878d7038ce162d — Florent Le Coz 10 years ago 6a43d35
Properly detect iconv features to compile
3 files changed, 68 insertions(+), 3 deletions(-)

M CMakeLists.txt
A cmake/Modules/FindIconv.cmake
M src/utils/encoding.cpp
M CMakeLists.txt => CMakeLists.txt +4 -1
@@ 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

A cmake/Modules/FindIconv.cmake => cmake/Modules/FindIconv.cmake +57 -0
@@ 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 <iconv.h>
 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

M src/utils/encoding.cpp => src/utils/encoding.cpp +7 -2
@@ 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<char*>(str.c_str());
#endif

    size_t outbytesleft = str.size() * 4;
    char* outbuf = new char[outbytesleft];