changeset 47:28d7d7598fdb

sync + no more need of "-DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES" to dynamically link against system-wide orthanc framework
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 12 Jun 2021 09:25:13 +0200
parents 914b8113fd46
children f412a8993806
files CMakeLists.txt Resources/Orthanc/CMake/Compiler.cmake Resources/Orthanc/CMake/DownloadOrthancFramework.cmake Resources/Orthanc/Plugins/OrthancPluginCppWrapper.cpp Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h
diffstat 5 files changed, 159 insertions(+), 141 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Wed Jan 06 18:01:12 2021 +0100
+++ b/CMakeLists.txt	Sat Jun 12 09:25:13 2021 +0200
@@ -25,7 +25,7 @@
   set(ORTHANC_FRAMEWORK_VERSION "mainline")
   set(ORTHANC_FRAMEWORK_DEFAULT_SOURCE "hg")
 else()
-  set(ORTHANC_FRAMEWORK_VERSION "1.8.1")
+  set(ORTHANC_FRAMEWORK_VERSION "1.9.3")
   set(ORTHANC_FRAMEWORK_DEFAULT_SOURCE "web")
 endif()
 
@@ -46,6 +46,17 @@
 include_directories(${ORTHANC_FRAMEWORK_ROOT})
 
 if (ORTHANC_FRAMEWORK_SOURCE STREQUAL "system")
+  if (ORTHANC_FRAMEWORK_USE_SHARED)
+    include(FindBoost)
+    find_package(Boost COMPONENTS regex system)
+    
+    if (NOT Boost_FOUND)
+      message(FATAL_ERROR "Unable to locate Boost on this system")
+    endif()
+    
+    link_libraries(${Boost_LIBRARIES} jsoncpp)
+  endif()
+  
   link_libraries(${ORTHANC_FRAMEWORK_LIBRARIES})
 
 else()
--- a/Resources/Orthanc/CMake/Compiler.cmake	Wed Jan 06 18:01:12 2021 +0100
+++ b/Resources/Orthanc/CMake/Compiler.cmake	Sat Jun 12 09:25:13 2021 +0200
@@ -188,7 +188,7 @@
   add_definitions(
     -D_CRT_SECURE_NO_WARNINGS=1
     )
-  link_libraries(rpcrt4 ws2_32)
+  link_libraries(rpcrt4 ws2_32 iphlpapi)  # "iphlpapi" is for "SystemToolbox::GetMacAddresses()"
 
   if (CMAKE_COMPILER_IS_GNUCXX)
     # Some additional C/C++ compiler flags for MinGW
--- a/Resources/Orthanc/CMake/DownloadOrthancFramework.cmake	Wed Jan 06 18:01:12 2021 +0100
+++ b/Resources/Orthanc/CMake/DownloadOrthancFramework.cmake	Sat Jun 12 09:25:13 2021 +0200
@@ -122,6 +122,14 @@
         set(ORTHANC_FRAMEWORK_MD5 "db094f96399cbe8b9bbdbce34884c220")
       elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.8.2")
         set(ORTHANC_FRAMEWORK_MD5 "8bfa10e66c9931e74111be0bfb1f4548")
+      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.9.0")
+        set(ORTHANC_FRAMEWORK_MD5 "cea0b02ce184671eaf1bd668beefbf28")
+      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.9.1")
+        set(ORTHANC_FRAMEWORK_MD5 "08eebc66ef93c3b40115c38501db5fbd")
+      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.9.2")
+        set(ORTHANC_FRAMEWORK_MD5 "3ea66c09f64aca990016683b6375734e")
+      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.9.3")
+        set(ORTHANC_FRAMEWORK_MD5 "9b86e6f00e03278293cd15643cc0233f")
 
       # Below this point are development snapshots that were used to
       # release some plugin, before an official release of the Orthanc
@@ -426,6 +434,8 @@
 
 if (ORTHANC_FRAMEWORK_SOURCE STREQUAL "system")
   set(ORTHANC_FRAMEWORK_LIBDIR "" CACHE PATH "")
+  set(ORTHANC_FRAMEWORK_USE_SHARED ON CACHE BOOL "Whether to use the shared library or the static library")
+  set(ORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES "" CACHE STRING "Additional libraries to link against, separated by whitespaces, typically needed if using the static library (a common minimal value is \"boost_filesystem boost_iostreams boost_locale boost_regex boost_thread jsoncpp pugixml uuid\")")
 
   if (CMAKE_SYSTEM_NAME STREQUAL "Windows" AND
       CMAKE_COMPILER_IS_GNUCXX) # MinGW
@@ -442,113 +452,75 @@
   include(${CMAKE_CURRENT_LIST_DIR}/AutoGeneratedCode.cmake)
   set(EMBED_RESOURCES_PYTHON ${CMAKE_CURRENT_LIST_DIR}/EmbedResources.py)
 
-  if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows" OR
-      ORTHANC_FRAMEWORK_STATIC)
-    include_directories(${ORTHANC_FRAMEWORK_ROOT}/..)
+  if (ORTHANC_FRAMEWORK_USE_SHARED)
+    list(GET CMAKE_FIND_LIBRARY_PREFIXES 0 Prefix)
+    list(GET CMAKE_FIND_LIBRARY_SUFFIXES 0 Suffix)
   else()
-    # Look for mandatory dependency JsonCpp (cf. JsonCppConfiguration.cmake)
+    list(GET CMAKE_FIND_LIBRARY_PREFIXES 0 Prefix)
+    list(GET CMAKE_FIND_LIBRARY_SUFFIXES 1 Suffix)
+  endif()
+
+  # The "OrthancFramework" library must be the first one to be included
+  if ("${ORTHANC_FRAMEWORK_LIBDIR}" STREQUAL "")
+    set(ORTHANC_FRAMEWORK_LIBRARIES ${Prefix}OrthancFramework${Suffix})
+  else ()
+    set(ORTHANC_FRAMEWORK_LIBRARIES ${ORTHANC_FRAMEWORK_LIBDIR}/${Prefix}OrthancFramework${Suffix})
+  endif()
+
+  if (NOT ORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES STREQUAL "")
+    # https://stackoverflow.com/a/5272993/881731
+    string(REPLACE " " ";" tmp ${ORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES})
+    list(APPEND ORTHANC_FRAMEWORK_LIBRARIES ${tmp})
+  endif()
+
+  # Look for the version of the mandatory dependency JsonCpp (cf. JsonCppConfiguration.cmake)
+  if (CMAKE_CROSSCOMPILING)
+    set(JSONCPP_INCLUDE_DIR ${ORTHANC_FRAMEWORK_ROOT}/..)
+  else()
     find_path(JSONCPP_INCLUDE_DIR json/reader.h
+      ${ORTHANC_FRAMEWORK_ROOT}/..
       /usr/include/jsoncpp
       /usr/local/include/jsoncpp
       )
+  endif()
 
-    message("JsonCpp include dir: ${JSONCPP_INCLUDE_DIR}")
-    include_directories(${JSONCPP_INCLUDE_DIR})
-    link_libraries(jsoncpp)
+  message("JsonCpp include dir: ${JSONCPP_INCLUDE_DIR}")
+  include_directories(${JSONCPP_INCLUDE_DIR})
 
-    CHECK_INCLUDE_FILE_CXX(${JSONCPP_INCLUDE_DIR}/json/reader.h HAVE_JSONCPP_H)
-    if (NOT HAVE_JSONCPP_H)
-      message(FATAL_ERROR "Please install the libjsoncpp-dev package")
-    endif()
-
-    # Switch to the C++11 standard if the version of JsonCpp is 1.y.z
-    # (same as variable JSONCPP_CXX11 in the source code of Orthanc)
-    if (EXISTS ${JSONCPP_INCLUDE_DIR}/json/version.h)
-      file(STRINGS
-        "${JSONCPP_INCLUDE_DIR}/json/version.h" 
-        JSONCPP_VERSION_MAJOR1 REGEX
-        ".*define JSONCPP_VERSION_MAJOR.*")
+  CHECK_INCLUDE_FILE_CXX(${JSONCPP_INCLUDE_DIR}/json/reader.h HAVE_JSONCPP_H)
+  if (NOT HAVE_JSONCPP_H)
+    message(FATAL_ERROR "Please install the libjsoncpp-dev package")
+  endif()
 
-      if (NOT JSONCPP_VERSION_MAJOR1)
-        message(FATAL_ERROR "Unable to extract the major version of JsonCpp")
-      endif()
-      
-      string(REGEX REPLACE
-        ".*JSONCPP_VERSION_MAJOR.*([0-9]+)$" "\\1" 
-        JSONCPP_VERSION_MAJOR ${JSONCPP_VERSION_MAJOR1})
-      message("JsonCpp major version: ${JSONCPP_VERSION_MAJOR}")
+  # Switch to the C++11 standard if the version of JsonCpp is 1.y.z
+  # (same as variable JSONCPP_CXX11 in the source code of Orthanc)
+  if (EXISTS ${JSONCPP_INCLUDE_DIR}/json/version.h)
+    file(STRINGS
+      "${JSONCPP_INCLUDE_DIR}/json/version.h" 
+      JSONCPP_VERSION_MAJOR1 REGEX
+      ".*define JSONCPP_VERSION_MAJOR.*")
 
-      if (JSONCPP_VERSION_MAJOR GREATER 0)
-        message("Switching to C++11 standard, as version of JsonCpp is >= 1.0.0")
-        if (CMAKE_COMPILER_IS_GNUCXX)
-          set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
-        elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
-          set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
-        endif()
-      endif()
-    else()
-      message("Unable to detect the major version of JsonCpp, assuming < 1.0.0")
-    endif()
-
-    # Look for mandatory dependency Boost (cf. BoostConfiguration.cmake)
-    include(FindBoost)
-    find_package(Boost COMPONENTS filesystem thread system date_time regex ${ORTHANC_BOOST_COMPONENTS})
-
-    if (NOT Boost_FOUND)
-      message(FATAL_ERROR "Unable to locate Boost on this system")
+    if (NOT JSONCPP_VERSION_MAJOR1)
+      message(FATAL_ERROR "Unable to extract the major version of JsonCpp")
     endif()
     
-    include_directories(${Boost_INCLUDE_DIRS})
-    link_libraries(${Boost_LIBRARIES})
-
-    # Optional component - Lua
-    if (ENABLE_LUA)
-      include(FindLua)
-
-      if (NOT LUA_FOUND)
-        message(FATAL_ERROR "Please install the liblua-dev package")
-      endif()
-
-      include_directories(${LUA_INCLUDE_DIR})
-      link_libraries(${LUA_LIBRARIES})
-    endif()
-
-    # Optional component - SQLite
-    if (ENABLE_SQLITE)    
-      CHECK_INCLUDE_FILE(sqlite3.h HAVE_SQLITE_H)
-      if (NOT HAVE_SQLITE_H)
-        message(FATAL_ERROR "Please install the libsqlite3-dev package")
-      endif()
-      link_libraries(sqlite3)
-    endif()
+    string(REGEX REPLACE
+      ".*JSONCPP_VERSION_MAJOR.*([0-9]+)$" "\\1" 
+      JSONCPP_VERSION_MAJOR ${JSONCPP_VERSION_MAJOR1})
+    message("JsonCpp major version: ${JSONCPP_VERSION_MAJOR}")
 
-    # Optional component - Pugixml
-    if (ENABLE_PUGIXML)
-      CHECK_INCLUDE_FILE_CXX(pugixml.hpp HAVE_PUGIXML_H)
-      if (NOT HAVE_PUGIXML_H)
-        message(FATAL_ERROR "Please install the libpugixml-dev package")
-      endif()      
-      link_libraries(pugixml)
+    if (JSONCPP_VERSION_MAJOR GREATER 0)
+      message("Switching to C++11 standard, as version of JsonCpp is >= 1.0.0")
+      if (CMAKE_COMPILER_IS_GNUCXX)
+        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
+      elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
+        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
+      endif()
     endif()
-
-    # Optional component - DCMTK
-    if (ENABLE_DCMTK)
-      include(FindDCMTK)
-      include_directories(${DCMTK_INCLUDE_DIRS})
-      link_libraries(${DCMTK_LIBRARIES})
-    endif()
-
-    # Optional component - OpenSSL
-    if (ENABLE_SSL)
-      include(FindOpenSSL)
-      if (NOT ${OPENSSL_FOUND})
-        message(FATAL_ERROR "Unable to find OpenSSL")
-      endif()
-      include_directories(${OPENSSL_INCLUDE_DIR})
-      link_libraries(${OPENSSL_LIBRARIES})
-    endif()
+  else()
+    message("Unable to detect the major version of JsonCpp, assuming < 1.0.0")
   endif()
-
+  
   # Look for Orthanc framework shared library
   include(CheckCXXSymbolExists)
 
@@ -568,28 +540,17 @@
   
   message("Orthanc framework include dir: ${ORTHANC_FRAMEWORK_INCLUDE_DIR}")
   include_directories(${ORTHANC_FRAMEWORK_INCLUDE_DIR})
-  
-  if ("${ORTHANC_FRAMEWORK_LIBDIR}" STREQUAL "")
-    set(ORTHANC_FRAMEWORK_LIBRARIES OrthancFramework)
-  else()
-    if (MSVC)
-      set(Suffix ".lib")
-      set(Prefix "")
-    else()
-      list(GET CMAKE_FIND_LIBRARY_PREFIXES 0 Prefix)
-      list(GET CMAKE_FIND_LIBRARY_SUFFIXES 0 Suffix)
+
+  if (ORTHANC_FRAMEWORK_USE_SHARED)
+    set(CMAKE_REQUIRED_INCLUDES "${ORTHANC_FRAMEWORK_INCLUDE_DIR}")
+    set(CMAKE_REQUIRED_LIBRARIES "${ORTHANC_FRAMEWORK_LIBRARIES}")
+    
+    check_cxx_symbol_exists("Orthanc::InitializeFramework" "OrthancFramework.h" HAVE_ORTHANC_FRAMEWORK)
+    if (NOT HAVE_ORTHANC_FRAMEWORK)
+      message(FATAL_ERROR "Cannot find the Orthanc framework")
     endif()
-    set(ORTHANC_FRAMEWORK_LIBRARIES ${ORTHANC_FRAMEWORK_LIBDIR}/${Prefix}OrthancFramework${Suffix})
+    
+    unset(CMAKE_REQUIRED_INCLUDES)
+    unset(CMAKE_REQUIRED_LIBRARIES)
   endif()
-
-  set(CMAKE_REQUIRED_INCLUDES "${ORTHANC_FRAMEWORK_INCLUDE_DIR}")
-  set(CMAKE_REQUIRED_LIBRARIES "${ORTHANC_FRAMEWORK_LIBRARIES}")
-  
-  check_cxx_symbol_exists("Orthanc::InitializeFramework" "OrthancFramework.h" HAVE_ORTHANC_FRAMEWORK)
-  if (NOT HAVE_ORTHANC_FRAMEWORK)
-    message(FATAL_ERROR "Cannot find the Orthanc framework")
-  endif()
-
-  unset(CMAKE_REQUIRED_INCLUDES)
-  unset(CMAKE_REQUIRED_LIBRARIES)
 endif()
--- a/Resources/Orthanc/Plugins/OrthancPluginCppWrapper.cpp	Wed Jan 06 18:01:12 2021 +0100
+++ b/Resources/Orthanc/Plugins/OrthancPluginCppWrapper.cpp	Sat Jun 12 09:25:13 2021 +0200
@@ -313,6 +313,37 @@
   }
 
 
+  static bool ReadJsonInternal(Json::Value& target,
+                               const void* buffer,
+                               size_t size,
+                               bool collectComments)
+  {
+#if JSONCPP_USE_DEPRECATED == 1
+    Json::Reader reader;
+    return reader.parse(reinterpret_cast<const char*>(buffer),
+                        reinterpret_cast<const char*>(buffer) + size, target, collectComments);
+#else
+    Json::CharReaderBuilder builder;
+    builder.settings_["collectComments"] = collectComments;
+    
+    const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
+    assert(reader.get() != NULL);
+    
+    JSONCPP_STRING err;
+    if (reader->parse(reinterpret_cast<const char*>(buffer),
+                      reinterpret_cast<const char*>(buffer) + size, &target, &err))
+    {
+      return true;
+    }
+    else
+    {
+      LogError("Cannot parse JSON: " + std::string(err));
+      return false;
+    }
+#endif
+  }
+
+
   bool ReadJson(Json::Value& target,
                 const std::string& source)
   {
@@ -324,29 +355,25 @@
                 const void* buffer,
                 size_t size)
   {
-#if JSONCPP_USE_DEPRECATED == 1
-    Json::Reader reader;
-    return reader.parse(reinterpret_cast<const char*>(buffer),
-                        reinterpret_cast<const char*>(buffer) + size, target);
-#else
-    Json::CharReaderBuilder builder;
-    const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
-    assert(reader.get() != NULL);
-    JSONCPP_STRING err;
-    if (reader->parse(reinterpret_cast<const char*>(buffer),
-                      reinterpret_cast<const char*>(buffer) + size, &target, &err))
-    {
-      return true;
-    }
-    else
-    {
-      LogError("Cannot parse JSON: " + err);
-      return false;
-    }
-#endif
+    return ReadJsonInternal(target, buffer, size, true);
   }
   
 
+  bool ReadJsonWithoutComments(Json::Value& target,
+                               const std::string& source)
+  {
+    return ReadJsonWithoutComments(target, source.empty() ? NULL : source.c_str(), source.size());
+  }
+  
+
+  bool ReadJsonWithoutComments(Json::Value& target,
+                               const void* buffer,
+                               size_t size)
+  {
+    return ReadJsonInternal(target, buffer, size, false);
+  }
+
+
   void WriteFastJson(std::string& target,
                      const Json::Value& source)
   {
@@ -2699,10 +2726,18 @@
           delete *it;
         }
 
+        size_ = 0;
         content_.clear();
       }
 
-      void Flatten(std::string& target) const
+      /**
+       * Since Orthanc 1.9.3, this function also clears the content of
+       * the ChunkedBuffer in order to mimic the behavior of the
+       * original class "Orthanc::ChunkedBuffer". This prevents the
+       * forgetting of calling "Clear()" in order to reduce memory
+       * consumption.
+       **/
+      void Flatten(std::string& target)
       {
         target.resize(size_);
 
@@ -2718,10 +2753,14 @@
             memcpy(&target[pos], (*it)->c_str(), s);
             pos += s;
           }
+
+          delete *it;
         }
 
-        assert(size_ == 0 ||
-               pos == target.size());
+        assert(pos == target.size());
+
+        size_ = 0;
+        content_.clear();
       }
 
       void AddChunk(const void* data,
@@ -2752,7 +2791,7 @@
         return headers_;
       }
 
-      const ChunkedBuffer& GetBody() const
+      ChunkedBuffer& GetBody()
       {
         return body_;
       }
--- a/Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h	Wed Jan 06 18:01:12 2021 +0100
+++ b/Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h	Sat Jun 12 09:25:13 2021 +0200
@@ -483,6 +483,13 @@
                 const void* buffer,
                 size_t size);
 
+  bool ReadJsonWithoutComments(Json::Value& target,
+                               const std::string& source);  
+
+  bool ReadJsonWithoutComments(Json::Value& target,
+                               const void* buffer,
+                               size_t size);
+
   void WriteFastJson(std::string& target,
                      const Json::Value& source);