changeset 4496:9ea70ccf0c21

Possibility to generate a static library containing the Orthanc Framework
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 08 Feb 2021 18:34:40 +0100
parents fa2311f94d9f
children 10357b2f7150
files NEWS OrthancFramework/Resources/CMake/DownloadOrthancFramework.cmake OrthancFramework/SharedLibrary/CMakeLists.txt OrthancFramework/UnitTestsSources/CMakeLists.txt
diffstat 4 files changed, 100 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Thu Feb 04 18:01:07 2021 +0100
+++ b/NEWS	Mon Feb 08 18:34:40 2021 +0100
@@ -3,6 +3,7 @@
 
 * New metadata automatically computed at the instance level: "PixelDataOffset"
 * Fix build on big-endian architectures
+* Possibility to generate a static library containing the Orthanc Framework
 
 
 Version 1.9.0 (2021-01-29)
--- a/OrthancFramework/Resources/CMake/DownloadOrthancFramework.cmake	Thu Feb 04 18:01:07 2021 +0100
+++ b/OrthancFramework/Resources/CMake/DownloadOrthancFramework.cmake	Mon Feb 08 18:34:40 2021 +0100
@@ -428,6 +428,7 @@
 
 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")
 
   if (CMAKE_SYSTEM_NAME STREQUAL "Windows" AND
       CMAKE_COMPILER_IS_GNUCXX) # MinGW
@@ -444,6 +445,21 @@
   include(${CMAKE_CURRENT_LIST_DIR}/AutoGeneratedCode.cmake)
   set(EMBED_RESOURCES_PYTHON ${CMAKE_CURRENT_LIST_DIR}/EmbedResources.py)
 
+  # The "OrthancFramework" library must be the first one to be included
+  if ("${ORTHANC_FRAMEWORK_LIBDIR}" STREQUAL "")
+    set(ORTHANC_FRAMEWORK_LIBRARIES OrthancFramework)
+  else()
+    if(ORTHANC_FRAMEWORK_USE_SHARED)
+      list(GET CMAKE_FIND_LIBRARY_PREFIXES 0 Prefix)
+      list(GET CMAKE_FIND_LIBRARY_SUFFIXES 0 Suffix)
+    else()
+      list(GET CMAKE_FIND_LIBRARY_PREFIXES 0 Prefix)
+      list(GET CMAKE_FIND_LIBRARY_SUFFIXES 1 Suffix)
+    endif()
+    set(ORTHANC_FRAMEWORK_LIBRARIES
+      ${ORTHANC_FRAMEWORK_LIBDIR}/${Prefix}OrthancFramework${Suffix})
+  endif()
+
   if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows" OR
       ORTHANC_FRAMEWORK_STATIC)
     include_directories(${ORTHANC_FRAMEWORK_ROOT}/..)
@@ -456,7 +472,7 @@
 
     message("JsonCpp include dir: ${JSONCPP_INCLUDE_DIR}")
     include_directories(${JSONCPP_INCLUDE_DIR})
-    link_libraries(jsoncpp)
+    list(APPEND ORTHANC_FRAMEWORK_LIBRARIES jsoncpp)
 
     CHECK_INCLUDE_FILE_CXX(${JSONCPP_INCLUDE_DIR}/json/reader.h HAVE_JSONCPP_H)
     if (NOT HAVE_JSONCPP_H)
@@ -493,15 +509,17 @@
     endif()
 
     # Look for mandatory dependency Boost (cf. BoostConfiguration.cmake)
+    # NB: "locale" and "iostreams" are not required if using the shared library
     include(FindBoost)
-    find_package(Boost COMPONENTS filesystem thread system date_time regex ${ORTHANC_BOOST_COMPONENTS})
+    find_package(Boost COMPONENTS filesystem thread system date_time
+      iostreams locale regex ${ORTHANC_BOOST_COMPONENTS})
 
     if (NOT Boost_FOUND)
       message(FATAL_ERROR "Unable to locate Boost on this system")
     endif()
     
     include_directories(${Boost_INCLUDE_DIRS})
-    link_libraries(${Boost_LIBRARIES})
+    list(APPEND ORTHANC_FRAMEWORK_LIBRARIES ${Boost_LIBRARIES})
 
     # Optional component - Lua
     if (ENABLE_LUA)
@@ -512,7 +530,7 @@
       endif()
 
       include_directories(${LUA_INCLUDE_DIR})
-      link_libraries(${LUA_LIBRARIES})
+      list(APPEND ORTHANC_FRAMEWORK_LIBRARIES ${LUA_LIBRARIES})
     endif()
 
     # Optional component - SQLite
@@ -521,7 +539,7 @@
       if (NOT HAVE_SQLITE_H)
         message(FATAL_ERROR "Please install the libsqlite3-dev package")
       endif()
-      link_libraries(sqlite3)
+      list(APPEND ORTHANC_FRAMEWORK_LIBRARIES sqlite3)
     endif()
 
     # Optional component - Pugixml
@@ -530,14 +548,14 @@
       if (NOT HAVE_PUGIXML_H)
         message(FATAL_ERROR "Please install the libpugixml-dev package")
       endif()      
-      link_libraries(pugixml)
+      list(APPEND ORTHANC_FRAMEWORK_LIBRARIES pugixml)
     endif()
 
     # Optional component - DCMTK
     if (ENABLE_DCMTK)
-      include(FindDCMTK)
+      include(FindDCMTK NO_MODULE)
+      list(APPEND ORTHANC_FRAMEWORK_LIBRARIES ${DCMTK_LIBRARIES})
       include_directories(${DCMTK_INCLUDE_DIRS})
-      link_libraries(${DCMTK_LIBRARIES})
     endif()
 
     # Optional component - OpenSSL
@@ -547,7 +565,38 @@
         message(FATAL_ERROR "Unable to find OpenSSL")
       endif()
       include_directories(${OPENSSL_INCLUDE_DIR})
-      link_libraries(${OPENSSL_LIBRARIES})
+      list(APPEND ORTHANC_FRAMEWORK_LIBRARIES ${OPENSSL_LIBRARIES})
+    endif()
+  endif()
+  
+  if (NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows" AND
+      NOT ORTHANC_FRAMEWORK_USE_SHARED)
+    # Static library has more dependencies
+
+    # Mandatory dependency: libuuid
+    CHECK_INCLUDE_FILE(uuid/uuid.h HAVE_UUID_H)
+    if (NOT HAVE_UUID_H)
+      message(FATAL_ERROR "Please install uuid-dev, e2fsprogs (OpenBSD) or e2fsprogs-libuuid (FreeBSD)")
+    endif()
+
+    find_library(LIBUUID uuid
+      PATHS
+      /usr/lib
+      /usr/local/lib
+      )
+
+    check_library_exists(${LIBUUID} uuid_generate_random "" HAVE_LIBUUID)
+    if (NOT HAVE_LIBUUID)
+      message(FATAL_ERROR "Unable to find the uuid library")
+    endif()
+
+    list(APPEND ORTHANC_FRAMEWORK_LIBRARIES ${LIBUUID})
+
+    # Optional component - libcurl
+    if (ENABLE_WEB_CLIENT)
+      include(FindCURL)
+      include_directories(${CURL_INCLUDE_DIRS})
+      list(APPEND ORTHANC_FRAMEWORK_LIBRARIES ${CURL_LIBRARIES})
     endif()
   endif()
 
@@ -570,19 +619,6 @@
   
   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)
-    endif()
-    set(ORTHANC_FRAMEWORK_LIBRARIES ${ORTHANC_FRAMEWORK_LIBDIR}/${Prefix}OrthancFramework${Suffix})
-  endif()
 
   set(CMAKE_REQUIRED_INCLUDES "${ORTHANC_FRAMEWORK_INCLUDE_DIR}")
   set(CMAKE_REQUIRED_LIBRARIES "${ORTHANC_FRAMEWORK_LIBRARIES}")
--- a/OrthancFramework/SharedLibrary/CMakeLists.txt	Thu Feb 04 18:01:07 2021 +0100
+++ b/OrthancFramework/SharedLibrary/CMakeLists.txt	Mon Feb 08 18:34:40 2021 +0100
@@ -44,6 +44,7 @@
 # issues if re-building the shared library after install!
 set(ORTHANC_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}" CACHE PATH "")
 SET(UNIT_TESTS_WITH_HTTP_CONNEXIONS ON CACHE BOOL "Allow unit tests to make HTTP requests")
+set(BUILD_SHARED_LIBRARY ON CACHE BOOL "Whether to build a shared library instead of a static library")
 
 
 
@@ -262,37 +263,42 @@
     COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/OrthancFramework.js
     )
 else()
-  add_library(OrthancFramework SHARED
-    ${AUTOGENERATED_SOURCES}
-    ${ORTHANC_CORE_SOURCES}
-    ${ORTHANC_DICOM_SOURCES}
-    DllMain.cpp
-    )
-endif()
+  if (BUILD_SHARED_LIBRARY)
+    add_library(OrthancFramework SHARED
+      ${AUTOGENERATED_SOURCES}
+      ${ORTHANC_CORE_SOURCES}
+      ${ORTHANC_DICOM_SOURCES}
+      DllMain.cpp
+      )
 
-
-if (NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
-  # By default, hide all the symbols
-  set_target_properties(OrthancFramework PROPERTIES C_VISIBILITY_PRESET hidden)
-  set_target_properties(OrthancFramework PROPERTIES CXX_VISIBILITY_PRESET hidden)
+    # By default, hide all the symbols
+    set_target_properties(OrthancFramework PROPERTIES C_VISIBILITY_PRESET hidden)
+    set_target_properties(OrthancFramework PROPERTIES CXX_VISIBILITY_PRESET hidden)
 
-  # Configure the version of the shared library
-  set_target_properties(
-    OrthancFramework PROPERTIES 
-    VERSION ${ORTHANC_VERSION} 
-    SOVERSION ${ORTHANC_FRAMEWORK_SOVERSION}
-    )
-endif()
+    # Configure the version of the shared library
+    set_target_properties(
+      OrthancFramework PROPERTIES 
+      VERSION ${ORTHANC_VERSION} 
+      SOVERSION ${ORTHANC_FRAMEWORK_SOVERSION}
+      )    
 
+    target_link_libraries(OrthancFramework ${DCMTK_LIBRARIES})
 
-target_link_libraries(OrthancFramework ${DCMTK_LIBRARIES})
+    if (LIBICU_LIBRARIES)
+      target_link_libraries(OrthancFramework ${LIBICU_LIBRARIES})
+    endif()
 
-if (LIBICU_LIBRARIES)
-  target_link_libraries(OrthancFramework ${LIBICU_LIBRARIES})
-endif()
-
-if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
-  target_link_libraries(OrthancFramework winpthread)
+    if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
+      target_link_libraries(OrthancFramework winpthread)
+    endif()
+  else()
+    # Building a static library
+    add_library(OrthancFramework STATIC
+      ${AUTOGENERATED_SOURCES}
+      ${ORTHANC_CORE_SOURCES}
+      ${ORTHANC_DICOM_SOURCES}
+      )
+  endif()
 endif()
 
 
@@ -462,6 +468,7 @@
   TARGETS OrthancFramework
   RUNTIME DESTINATION ${ORTHANC_INSTALL_PREFIX}/lib    # Destination for Windows
   LIBRARY DESTINATION ${ORTHANC_INSTALL_PREFIX}/lib    # Destination for Linux
+  ARCHIVE DESTINATION ${ORTHANC_INSTALL_PREFIX}/lib    # Destination for static library
   )
 
 if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
@@ -517,6 +524,7 @@
     -DUSE_GOOGLE_TEST_DEBIAN_PACKAGE:BOOL=${USE_GOOGLE_TEST_DEBIAN_PACKAGE}
     -DUSE_SYSTEM_BOOST:BOOL=${USE_SYSTEM_BOOST}
     -DUSE_SYSTEM_GOOGLE_TEST:BOOL=${USE_SYSTEM_GOOGLE_TEST}
+    -DORTHANC_FRAMEWORK_USE_SHARED:BOOL=${BUILD_SHARED_LIBRARY}
 
     ${Flags}
     )
--- a/OrthancFramework/UnitTestsSources/CMakeLists.txt	Thu Feb 04 18:01:07 2021 +0100
+++ b/OrthancFramework/UnitTestsSources/CMakeLists.txt	Mon Feb 08 18:34:40 2021 +0100
@@ -42,18 +42,19 @@
   add_definitions(-DUNIT_TESTS_WITH_HTTP_CONNEXIONS=0)
 endif()
 
+set(ENABLE_DCMTK ON)
+set(ENABLE_GOOGLE_TEST ON)
+set(ENABLE_LUA ON)
+set(ENABLE_PUGIXML ON)
+set(ENABLE_SQLITE ON)
+set(ENABLE_WEB_CLIENT ON)
+
 if (NOT ORTHANC_FRAMEWORK_STATIC)
-  set(ENABLE_DCMTK ON)
-  set(ENABLE_LUA ON)
-  set(ENABLE_PUGIXML ON)
-  set(ENABLE_SQLITE ON)
-
   set(USE_SYSTEM_GOOGLE_TEST ON CACHE BOOL "Use the system version of Google Test")
 else()
   set(USE_SYSTEM_GOOGLE_TEST OFF CACHE INTERNAL "")
 endif()
   
-set(ENABLE_GOOGLE_TEST ON)
 include(${CMAKE_SOURCE_DIR}/../Resources/CMake/DownloadOrthancFramework.cmake)
 include(${CMAKE_SOURCE_DIR}/../Resources/CMake/BoostConfiguration.cmake)
 include(${CMAKE_SOURCE_DIR}/../Resources/CMake/GoogleTestConfiguration.cmake)