changeset 2366:26f3a346226f

more consistent handling of libiconv in cmake
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 21 Aug 2017 21:21:48 +0200
parents 9e0eae8a5ea4
children 2aff870c2c58
files CMakeLists.txt Resources/CMake/BoostConfiguration.cmake Resources/CMake/LibIconvConfiguration.cmake UnitTestsSources/VersionsTests.cpp
diffstat 4 files changed, 116 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Mon Aug 21 17:15:51 2017 +0200
+++ b/CMakeLists.txt	Mon Aug 21 21:21:48 2017 +0200
@@ -43,6 +43,7 @@
 SET(USE_SYSTEM_LUA ON CACHE BOOL "Use the system version of Lua")
 SET(USE_SYSTEM_DCMTK ON CACHE BOOL "Use the system version of DCMTK")
 SET(USE_SYSTEM_BOOST ON CACHE BOOL "Use the system version of Boost")
+SET(USE_SYSTEM_LIBICONV ON CACHE BOOL "Use the system version of libiconv")
 SET(USE_SYSTEM_LIBPNG ON CACHE BOOL "Use the system version of libpng")
 SET(USE_SYSTEM_LIBJPEG ON CACHE BOOL "Use the system version of libjpeg")
 SET(USE_SYSTEM_CURL ON CACHE BOOL "Use the system version of LibCurl")
@@ -74,6 +75,7 @@
 # Some basic inclusions
 include(CheckIncludeFiles)
 include(CheckIncludeFileCXX)
+include(CheckFunctionExists)
 include(CheckLibraryExists)
 include(FindPythonInterp)
 include(${CMAKE_SOURCE_DIR}/Resources/CMake/AutoGeneratedCode.cmake)
@@ -303,6 +305,7 @@
 include(${CMAKE_SOURCE_DIR}/Resources/CMake/SQLiteConfiguration.cmake)
 include(${CMAKE_SOURCE_DIR}/Resources/CMake/ZlibConfiguration.cmake)
 include(${CMAKE_SOURCE_DIR}/Resources/CMake/GoogleTestConfiguration.cmake)
+include(${CMAKE_SOURCE_DIR}/Resources/CMake/LibIconvConfiguration.cmake)
 
 # These are the two most heavyweight dependencies. We put them as the
 # last includes to quickly spot problems when configuring static
@@ -466,14 +469,15 @@
   ${BOOST_SOURCES}
   ${CURL_SOURCES}
   ${JSONCPP_SOURCES}
+  ${LIBICONV_SOURCES}
+  ${LIBJPEG_SOURCES}
+  ${LIBP11_SOURCES}
   ${LIBPNG_SOURCES}
-  ${LIBJPEG_SOURCES}
   ${LUA_SOURCES}
   ${MONGOOSE_SOURCES}
   ${PUGIXML_SOURCES}
   ${SQLITE_SOURCES}
   ${ZLIB_SOURCES}
-  ${LIBP11_SOURCES}
 
   ${CMAKE_SOURCE_DIR}/Resources/ThirdParty/md5/md5.c
   ${CMAKE_SOURCE_DIR}/Resources/ThirdParty/base64/base64.cpp
@@ -557,6 +561,7 @@
   add_library(ServeFolders SHARED 
     ${BOOST_SOURCES}
     ${JSONCPP_SOURCES}
+    ${LIBICONV_SOURCES}
     Plugins/Samples/ServeFolders/Plugin.cpp
     Plugins/Samples/Common/OrthancPluginCppWrapper.cpp
     ${SERVE_FOLDERS_RESOURCES}
@@ -601,6 +606,7 @@
   add_library(ModalityWorklists SHARED 
     ${BOOST_SOURCES}
     ${JSONCPP_SOURCES}
+    ${LIBICONV_SOURCES}
     Plugins/Samples/Common/OrthancPluginCppWrapper.cpp
     Plugins/Samples/ModalityWorklists/Plugin.cpp
     ${MODALITY_WORKLISTS_RESOURCES}
--- a/Resources/CMake/BoostConfiguration.cmake	Mon Aug 21 17:15:51 2017 +0200
+++ b/Resources/CMake/BoostConfiguration.cmake	Mon Aug 21 21:21:48 2017 +0200
@@ -65,7 +65,6 @@
       ${BOOST_SOURCES_DIR}/libs/thread/src/pthread/thread.cpp
       )
     add_definitions(
-      -DBOOST_LOCALE_WITH_ICONV=1
       -DBOOST_LOCALE_NO_WINAPI_BACKEND=1
       -DBOOST_LOCALE_NO_STD_BACKEND=1
       )
@@ -77,8 +76,6 @@
       add_definitions(-DBOOST_HAS_SCHED_YIELD=1)
     endif()
 
-    link_libraries(iconv)
-
   elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
     list(APPEND BOOST_SOURCES
       ${BOOST_SOURCES_DIR}/libs/thread/src/win32/tss_dll.cpp
@@ -87,17 +84,6 @@
       ${BOOST_FILESYSTEM_SOURCES_DIR}/windows_file_codecvt.cpp
       )
 
-    # Starting with release 0.8.2, Orthanc statically links against
-    # libiconv, even on Windows. Indeed, the "WCONV" library of
-    # Windows XP seems not to support properly several codepages
-    # (notably "Latin3", "Hebrew", and "Arabic").
-
-    if (USE_BOOST_ICONV)
-      include(${ORTHANC_ROOT}/Resources/CMake/LibIconvConfiguration.cmake)
-    else()
-      add_definitions(-DBOOST_LOCALE_WITH_WCONV=1)
-    endif()
-
     add_definitions(
       -DBOOST_LOCALE_NO_POSIX_BACKEND=1
       -DBOOST_LOCALE_NO_STD_BACKEND=1
--- a/Resources/CMake/LibIconvConfiguration.cmake	Mon Aug 21 17:15:51 2017 +0200
+++ b/Resources/CMake/LibIconvConfiguration.cmake	Mon Aug 21 21:21:48 2017 +0200
@@ -1,52 +1,108 @@
-set(LIBICONV_SOURCES_DIR ${CMAKE_BINARY_DIR}/libiconv-1.15)
-set(LIBICONV_URL "http://www.orthanc-server.com/downloads/third-party/libiconv-1.15.tar.gz")
-set(LIBICONV_MD5 "ace8b5f2db42f7b3b3057585e80d9808")
+if (NOT USE_BOOST_ICONV)
+  message("Not using libiconv")
 
-DownloadPackage(${LIBICONV_MD5} ${LIBICONV_URL} "${LIBICONV_SOURCES_DIR}")
+  if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
+    # Starting with release 0.8.2, Orthanc statically links against
+    # libiconv, even on Windows. Indeed, the "WCONV" library of
+    # Windows XP seems not to support properly several codepages
+    # (notably "Latin3", "Hebrew", and "Arabic"). The flag
+    # "USE_BOOST_ICONV" allows to force the use of "WCONV".
+    add_definitions(-DBOOST_LOCALE_WITH_WCONV=1)
+  else()
+    message(FATAL_ERROR "Support your platform here")
+  endif()
+
+else()
+  message("Using libiconv")
+
+  add_definitions(
+    -DBOOST_LOCALE_WITH_ICONV=1
+    )
 
-# https://groups.google.com/d/msg/android-ndk/AS1nkxnk6m4/EQm09hD1tigJ
-add_definitions(
-  -DBOOST_LOCALE_WITH_ICONV=1
-  -DBUILDING_LIBICONV=1
-  -DIN_LIBRARY=1
-  -DLIBDIR=""
-  -DICONV_CONST=
-  )
+  if (STATIC_BUILD OR NOT USE_SYSTEM_LIBICONV)
+    set(LIBICONV_SOURCES_DIR ${CMAKE_BINARY_DIR}/libiconv-1.15)
+    set(LIBICONV_URL "http://www.orthanc-server.com/downloads/third-party/libiconv-1.15.tar.gz")
+    set(LIBICONV_MD5 "ace8b5f2db42f7b3b3057585e80d9808")
+
+    DownloadPackage(${LIBICONV_MD5} ${LIBICONV_URL} "${LIBICONV_SOURCES_DIR}")
+
+    # Disable the support of libiconv that is shipped by default with
+    # the C standard library on Linux. Setting this macro redirects
+    # calls from "iconv*()" to "libiconv*()" in the source code of
+    # "libiconv-1.15".
+    add_definitions(-DLIBICONV_PLUG=1)
 
-configure_file(
-  ${LIBICONV_SOURCES_DIR}/srclib/localcharset.h
-  ${LIBICONV_SOURCES_DIR}/include
-  COPYONLY)
+    # https://groups.google.com/d/msg/android-ndk/AS1nkxnk6m4/EQm09hD1tigJ
+    add_definitions(
+      -DBUILDING_LIBICONV=1
+      -DIN_LIBRARY=1
+      -DLIBDIR=""
+      -DICONV_CONST=
+      )
+
+    configure_file(
+      ${LIBICONV_SOURCES_DIR}/srclib/localcharset.h
+      ${LIBICONV_SOURCES_DIR}/include
+      COPYONLY)
 
-set(HAVE_VISIBILITY 0)
-set(ICONV_CONST ${ICONV_CONST})
-set(USE_MBSTATE_T 1)
-set(BROKEN_WCHAR_H 0)
-set(EILSEQ)
-set(HAVE_WCHAR_T 1)   
-configure_file(
-  ${LIBICONV_SOURCES_DIR}/include/iconv.h.build.in
-  ${LIBICONV_SOURCES_DIR}/include/iconv.h
-  )
-unset(HAVE_VISIBILITY)
-unset(ICONV_CONST)
-unset(USE_MBSTATE_T)
-unset(BROKEN_WCHAR_H)
-unset(EILSEQ)
-unset(HAVE_WCHAR_T)   
+    set(HAVE_VISIBILITY 0)
+    set(ICONV_CONST ${ICONV_CONST})
+    set(USE_MBSTATE_T 1)
+    set(BROKEN_WCHAR_H 0)
+    set(EILSEQ)
+    set(HAVE_WCHAR_T 1)
+    configure_file(
+      ${LIBICONV_SOURCES_DIR}/include/iconv.h.build.in
+      ${LIBICONV_SOURCES_DIR}/include/iconv.h
+      )
+    unset(HAVE_VISIBILITY)
+    unset(ICONV_CONST)
+    unset(USE_MBSTATE_T)
+    unset(BROKEN_WCHAR_H)
+    unset(EILSEQ)
+    unset(HAVE_WCHAR_T)
+
+    # Create an empty "config.h" for libiconv
+    file(WRITE ${LIBICONV_SOURCES_DIR}/include/config.h "")
+
+    include_directories(
+      ${LIBICONV_SOURCES_DIR}/include
+      )
+
+    set(LIBICONV_SOURCES
+      ${LIBICONV_SOURCES_DIR}/lib/iconv.c  
+      ${LIBICONV_SOURCES_DIR}/lib/relocatable.c
+      ${LIBICONV_SOURCES_DIR}/libcharset/lib/localcharset.c  
+      ${LIBICONV_SOURCES_DIR}/libcharset/lib/relocatable.c
+      )
 
-# Create an empty "config.h" for libiconv
-file(WRITE ${LIBICONV_SOURCES_DIR}/include/config.h "")
+    source_group(ThirdParty\\libiconv REGULAR_EXPRESSION ${LIBICONV_SOURCES_DIR}/.*)
 
-include_directories(
-  ${LIBICONV_SOURCES_DIR}/include
-  )
+    if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
+      add_definitions(-DHAVE_WORKING_O_NOFOLLOW=0)
+    else()
+      add_definitions(-DHAVE_WORKING_O_NOFOLLOW=1)
+    endif()
+
+  else() 
+    CHECK_INCLUDE_FILE_CXX(iconv.h HAVE_ICONV_H)
+    if (NOT HAVE_ICONV_H)
+      message(FATAL_ERROR "Please install the libiconv-dev package")
+    endif()
 
-list(APPEND BOOST_SOURCES
-  ${LIBICONV_SOURCES_DIR}/lib/iconv.c  
-  ${LIBICONV_SOURCES_DIR}/lib/relocatable.c
-  ${LIBICONV_SOURCES_DIR}/libcharset/lib/localcharset.c  
-  ${LIBICONV_SOURCES_DIR}/libcharset/lib/relocatable.c
-  )
+    # Check whether the support for libiconv is bundled within the
+    # standard library
+    CHECK_FUNCTION_EXISTS(iconv_open HAVE_ICONV_LIB)
+    if (NOT HAVE_ICONV_LIB)
+      # No builtin support for libiconv, try and find an external library.
+      # Open question: Does this make sense on any platform?
+      CHECK_LIBRARY_EXISTS(iconv iconv_open "" HAVE_ICONV_LIB_2)
+      if (NOT HAVE_ICONV_LIB_2)
+        message(FATAL_ERROR "Please install the libiconv-dev package")
+      else()
+        link_libraries(iconv)
+      endif()
+    endif()
 
-source_group(ThirdParty\\libiconv REGULAR_EXPRESSION ${LIBICONV_SOURCES_DIR}/.*)
+  endif()
+endif()
--- a/UnitTestsSources/VersionsTests.cpp	Mon Aug 21 17:15:51 2017 +0200
+++ b/UnitTestsSources/VersionsTests.cpp	Mon Aug 21 21:21:48 2017 +0200
@@ -44,6 +44,7 @@
 #include <sqlite3.h>
 #include <lua.h>
 #include <jpeglib.h>
+#include <iconv.h>
 
 #if ORTHANC_ENABLE_SSL == 1
 #include <openssl/opensslv.h>
@@ -143,6 +144,13 @@
   ASSERT_STREQ("Lua 5.1.5", LUA_RELEASE);
 }
 
+TEST(Version, LibIconvStatic)
+{
+  static const int major = 1;
+  static const int minor = 15;  
+  ASSERT_EQ((major << 8) + minor, _LIBICONV_VERSION);
+}
+
 
 #if ORTHANC_ENABLE_SSL == 1
 TEST(Version, OpenSslStatic)