view Resources/CMake/LibVtkConfiguration.cmake @ 10:549ba7ada071

make the viewer use the /instances/.../stl route
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 20 Jul 2023 12:34:32 +0200
parents 0f03a8a0bd6f
children ed7047aa4d3b
line wrap: on
line source

# SPDX-FileCopyrightText: 2023 Sebastien Jodogne, UCLouvain, Belgium
# SPDX-License-Identifier: GPL-3.0-or-later


# STL plugin for Orthanc
# Copyright (C) 2023 Sebastien Jodogne, 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 (NOT STATIC_BUILD AND USE_SYSTEM_VTK)
  find_package(VTK REQUIRED COMPONENTS
    vtkCommonDataModel
    vtkFiltersCore
    vtkImagingCore
    )

  include_directories(${VTK_INCLUDE_DIRS})

  # Add a void target
  add_custom_target(VTK)

else()
  set(VTK_MAJOR 7)
  set(VTK_MINOR 1)
  set(VTK_REVISION 1)
  set(VTK_MD5 "daee43460f4e95547f0635240ffbc9cb")

  set(VTK_SOURCES_DIR "${CMAKE_BINARY_DIR}/VTK-${VTK_MAJOR}.${VTK_MINOR}.${VTK_REVISION}")

  DownloadPackage(
    "${VTK_MD5}"
    "https://orthanc.uclouvain.be/third-party-downloads/VTK-${VTK_MAJOR}.${VTK_MINOR}.${VTK_REVISION}.tar.gz"
    "${VTK_SOURCES_DIR}")

  if (CMAKE_TOOLCHAIN_FILE)
    # Take absolute path to the toolchain
    get_filename_component(TMP ${CMAKE_TOOLCHAIN_FILE} REALPATH BASE ${CMAKE_SOURCE_DIR}/..)
    list(APPEND VTKCMakeFlags
      -DCMAKE_TOOLCHAIN_FILE=${TMP}
      -DLSB_CC=${LSB_CC}
      -DLSB_CXX=${LSB_CXX}
      )
  endif()

  cmake_host_system_information(RESULT NumberOfPhysicalCores QUERY NUMBER_OF_PHYSICAL_CORES)

  include(ExternalProject)

  if (CMAKE_CROSSCOMPILING)
    # First, generate the "VTKCompileToolsConfig.cmake" file on the host
    # computer. This is necessary before cross-compiling. Explanations:
    # https://cmake.org/cmake/help/book/mastering-cmake/chapter/Cross%20Compiling%20With%20CMake.html#cross-compiling-a-complex-project-vtk
    externalproject_add(VTKCompileTools
      SOURCE_DIR "${VTK_SOURCES_DIR}"

      CMAKE_ARGS
      -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
      -DCMAKE_CXX_COMPILER=g++
      -DCMAKE_C_COMPILER=gcc

      BUILD_COMMAND
      ${CMAKE_COMMAND} --build <BINARY_DIR> --config <CONFIG> --parallel ${NumberOfPhysicalCores} -t
      VTKCompileToolsConfig.cmake

      INSTALL_COMMAND ""  # Skip the install step
      )

    ExternalProject_Get_Property(VTKCompileTools binary_dir)
    list(APPEND VTKCMakeFlags
      -DVTKCompileTools_DIR=${binary_dir}
      )

    list(APPEND VTKCMakeFlags
      -DCMAKE_REQUIRE_LARGE_FILE_SUPPORT=ON
      -DCMAKE_REQUIRE_LARGE_FILE_SUPPORT__TRYRUN_OUTPUT=""
      -DKWSYS_LFS_WORKS=ON
      -DKWSYS_LFS_WORKS__TRYRUN_OUTPUT=""
      -DFILE_OFFSET_BITS=64

      # This simply disables HDF5, which is not used in Orthanc
      -DVTK_USE_SYSTEM_HDF5=ON
      )
  else()
    add_custom_target(VTKCompileTools)  # Empty target
  endif()

  if (CMAKE_COMPILER_IS_GNUCXX OR
      CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    # The following flag is necessary to statically link VTK within
    # the shared library containing the plugin
    set(Flags "-fPIC")
  else()
    set(Flags "")
  endif()

  externalproject_add(VTK
    SOURCE_DIR "${VTK_SOURCES_DIR}"

    CMAKE_ARGS
    ${VTKCMakeFlags}
    -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
    -DBUILD_SHARED_LIBS=OFF
    -DVTK_Group_Rendering=OFF

    -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
    "-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} ${Flags}"
    -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
    "-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS} ${Flags}"
    -DCMAKE_OSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET}
    -DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}

    BUILD_COMMAND
    ${CMAKE_COMMAND} --build <BINARY_DIR> --config <CONFIG> --parallel ${NumberOfPhysicalCores} -t
    vtkCommonCore
    vtkCommonDataModel
    vtkCommonMath
    vtkCommonMisc
    vtkCommonSystem
    vtkCommonTransforms
    vtkFiltersCore
    vtkFiltersPoints
    vtkImagingCore

    EXCLUDE_FROM_ALL TRUE

    INSTALL_COMMAND ""  # Skip the install step

    DEPENDS VTKCompileTools
    )

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

  foreach(module IN ITEMS
      # WARNING: The order of the modules below *is* important!
      ImagingCore
      FiltersCore
      CommonExecutionModel
      CommonDataModel
      CommonTransforms
      CommonMath
      CommonMisc
      CommonSystem
      CommonCore
      sys
      )
    list(APPEND VTK_LIBRARIES
      ${Prefix}vtk${module}-${VTK_MAJOR}.${VTK_MINOR}${Suffix}
      )
  endforeach()

  ExternalProject_Get_Property(VTK binary_dir)
  ExternalProject_Get_Property(VTK source_dir)

  link_directories(${binary_dir}/lib)

  foreach(dir IN ITEMS
      Common/Core
      Common/DataModel
      Common/ExecutionModel
      Common/Misc
      Filters/Core
      IO/Image
      Imaging/Core
      Utilities/KWIML
      )
    include_directories(
      ${source_dir}/${dir}
      ${binary_dir}/${dir}
      )
  endforeach()

endif()