view Resources/CMake/GdcmConfiguration.cmake @ 109:c1576f773901

trying with cmake generators
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 02 Sep 2025 12:22:58 +0200
parents 7db64d260ff3
children b1ae80761fdc
line wrap: on
line source

# Orthanc - A Lightweight, RESTful DICOM Store
# Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
# Department, University Hospital of Liege, Belgium
# Copyright (C) 2017-2023 Osimis S.A., Belgium
# Copyright (C) 2024-2025 Orthanc Team SRL, Belgium
# Copyright (C) 2021-2025 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


if (STATIC_BUILD OR NOT USE_SYSTEM_GDCM)
  set(GDCM_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/gdcm-static-build)

  if (USE_LEGACY_GDCM)
    set(GDCM_URL "https://orthanc.uclouvain.be/downloads/third-party-downloads/gdcm-2.8.9.tar.gz")
    set(GDCM_MD5 "aeb00e0cb5375d454010a72e2e0f6154")
  else()
    set(GDCM_URL "https://orthanc.uclouvain.be/downloads/third-party-downloads/gdcm-3.0.24.tar.gz")
    set(GDCM_MD5 "b51e20f7a7eaeafecae76b7d4da40481")
  endif()
  
  if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux" OR
      ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD" OR
      ${CMAKE_SYSTEM_NAME} STREQUAL "kFreeBSD")
    # If using gcc, build GDCM with the "-fPIC" argument to allow its
    # embedding into the shared library containing the Orthanc plugin
    set(AdditionalCFlags "-fPIC")
    set(AdditionalCxxFlags ${AdditionalCFlags})
  elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Windows" AND
      CMAKE_COMPILER_IS_GNUCXX)
    # Prevents error: "jump to label ‘err’ crosses initialization" of some variable
    # within "Source/Common/gdcmCAPICryptographicMessageSyntax.cxx" if using MinGW
    set(AdditionalCxxFlags "-fpermissive")
  elseif (${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD")
    # This definition is necessary to compile
    # "Source/MediaStorageAndFileFormat/gdcmFileStreamer.cxx"
    set(AdditionalCFlags "-Doff64_t=off_t") 
    set(AdditionalCxxFlags ${AdditionalCFlags})
  endif()
  
  set(Flags
    "-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS} ${AdditionalCFlags}"
    "-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} ${AdditionalCxxFlags}"
    -DCMAKE_C_FLAGS_DEBUG=${CMAKE_C_FLAGS_DEBUG}
    -DCMAKE_CXX_FLAGS_DEBUG=${CMAKE_CXX_FLAGS_DEBUG}
    -DCMAKE_C_FLAGS_RELEASE=${CMAKE_C_FLAGS_RELEASE}
    -DCMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}
    -DCMAKE_C_FLAGS_MINSIZEREL=${CMAKE_C_FLAGS_MINSIZEREL}
    -DCMAKE_CXX_FLAGS_MINSIZEREL=${CMAKE_CXX_FLAGS_MINSIZEREL} 
    -DCMAKE_C_FLAGS_RELWITHDEBINFO=${CMAKE_C_FLAGS_RELWITHDEBINFO} 
    -DCMAKE_CXX_FLAGS_RELWITHDEBINFO=${CMAKE_CXX_FLAGS_RELWITHDEBINFO}
    -DCMAKE_OSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET}
    -DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}
    )

  if (CMAKE_TOOLCHAIN_FILE)
    # Take absolute path to the toolchain
    get_filename_component(TMP ${CMAKE_TOOLCHAIN_FILE} REALPATH BASE ${CMAKE_SOURCE_DIR})
    list(APPEND Flags -DCMAKE_TOOLCHAIN_FILE=${TMP})
  endif()

  # Don't build manpages (since gdcm 2.8.4)
  list(APPEND Flags -DGDCM_BUILD_DOCBOOK_MANPAGES=OFF)

  if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux" OR
      "${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Darwin")
    # Patch to disable the compilation of socket++ by gdcm, which is
    # incompatible with LSB, but fortunately only required for DICOM
    # Networking. We don't do this on Windows, as the "patch.exe"
    # command-line tool will certainly be unavailable. (*)
    if (USE_LEGACY_GDCM)
      set(GDCM_PATCH "${CMAKE_SOURCE_DIR}/Resources/CMake/gdcm-2.8.9-no-networking.patch")
    else()
      set(GDCM_PATCH "${CMAKE_SOURCE_DIR}/Resources/CMake/gdcm-3.0.24-no-networking.patch")
    endif()

    find_program(PATCH_EXECUTABLE patch)
    if (${PATCH_EXECUTABLE} MATCHES "PATCH_EXECUTABLE-NOTFOUND")
      message(FATAL_ERROR "Please install the 'patch' standard command-line tool")
    endif()

    set(PATCH_COMMAND PATCH_COMMAND
      ${PATCH_EXECUTABLE} -p1 -N -i ${GDCM_PATCH})
  endif()

  include(ExternalProject)
  externalproject_add(GDCM
    URL "${GDCM_URL}"
    URL_MD5 "${GDCM_MD5}"
    TIMEOUT 60
    ${PATCH_COMMAND}   # Apply patch to remove networking support (*)
    INSTALL_DIR ${GDCM_INSTALL_DIR}

    # A full example about "externalproject_add" with CMake generators and
    # multi-configuration projects (for Visual Studio and XCode) is available at:
    # https://github.com/neundorf/CMakeExternalProjectExample
    CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>/$<CONFIG> -DCMAKE_BUILD_TYPE=$<CONFIG> ${Flags}
    )

  if(MSVC)
    set(Suffix ".lib")
    set(Prefix "")
  else()
    set(Suffix ".a")
    list(GET CMAKE_FIND_LIBRARY_PREFIXES 0 Prefix)
  endif()

  set(GDCM_LIBRARIES
    # WARNING: The order of the libraries below *is* important!
    ${Prefix}gdcmMSFF${Suffix}
    ${Prefix}gdcmcharls${Suffix}
    ${Prefix}gdcmDICT${Suffix}
    ${Prefix}gdcmDSED${Suffix}
    ${Prefix}gdcmIOD${Suffix}
    ${Prefix}gdcmjpeg8${Suffix}
    ${Prefix}gdcmjpeg12${Suffix}
    ${Prefix}gdcmjpeg16${Suffix}
    ${Prefix}gdcmopenjp2${Suffix}
    ${Prefix}gdcmzlib${Suffix}
    ${Prefix}gdcmCommon${Suffix}
    ${Prefix}gdcmexpat${Suffix}

    #${Prefix}socketxx${Suffix}
    #${Prefix}gdcmMEXD${Suffix}  # DICOM Networking, unneeded by Orthanc plugins
    #${Prefix}gdcmgetopt${Suffix}
    )

  if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
    list(APPEND GDCM_LIBRARIES
      rpcrt4   # For UUID stuff
      )
  else()
    list(APPEND GDCM_LIBRARIES
      ${Prefix}gdcmuuid${Suffix}
      )
  endif()

  link_directories(${GDCM_INSTALL_DIR}/$<CONFIG>/lib)

  if (USE_LEGACY_GDCM)
    include_directories(${GDCM_INSTALL_DIR}/$<CONFIG>/include/gdcm-2.8)
  else()
    include_directories(${GDCM_INSTALL_DIR}/$<CONFIG>/include/gdcm-3.0)
  endif()

else()
  find_package(GDCM REQUIRED)
  if (GDCM_FOUND)
    include(${GDCM_USE_FILE})
    set(GDCM_LIBRARIES gdcmCommon gdcmMSFF)
  else(GDCM_FOUND)
    message(FATAL_ERROR "Cannot find GDCM, did you set GDCM_DIR?")
  endif(GDCM_FOUND)
endif()