cmake_minimum_required(VERSION 3.13.4)

project(SlicerSMTK)

#-----------------------------------------------------------------------------
# Extension meta-information
set(EXTENSION_HOMEPAGE "https://smtk.readthedocs.io/en/latest/")
set(EXTENSION_CATEGORY "SlicerSMTK")
set(EXTENSION_CONTRIBUTORS "Shreeraj Jadhav, Sam Horvath, Jean-Christophe Fillion-Robin, Andinet Enquobahrie (Kitware Inc)")
set(EXTENSION_DESCRIPTION "Modules useful for handling SMTK data in 3D Slicer.")
set(EXTENSION_ICONURL "https://www.example.com/Slicer/Extensions/SlicerSMTK.png")
set(EXTENSION_SCREENSHOTURLS "https://www.example.com/Slicer/Extensions/SlicerSMTK/Screenshots/1.png")
set(EXTENSION_DEPENDS "NA") # Specified as a list or "NA" if no dependencies
set(EXTENSION_BUILD_SUBDIRECTORY inner-build)

set(SUPERBUILD_TOPLEVEL_PROJECT inner)

#-----------------------------------------------------------------------------
# Extension dependencies
find_package(Slicer REQUIRED)
include(${Slicer_USE_FILE})
mark_as_superbuild(Slicer_DIR)

find_package(Git REQUIRED)
mark_as_superbuild(GIT_EXECUTABLE)

#-----------------------------------------------------------------------------
# SuperBuild setup
option(${EXTENSION_NAME}_SUPERBUILD "Build ${EXTENSION_NAME} and the projects it depends on." ON)
mark_as_advanced(${EXTENSION_NAME}_SUPERBUILD)
if(${EXTENSION_NAME}_SUPERBUILD)
  include("${CMAKE_CURRENT_SOURCE_DIR}/SuperBuildPrerequisites.cmake")
  include("${CMAKE_CURRENT_SOURCE_DIR}/SuperBuild.cmake")
  return()
endif()

#-----------------------------------------------------------------------------
# Extension dependencies
find_package(smtk REQUIRED)
find_package(Netgen REQUIRED)
find_package(aeva-session REQUIRED)

#-----------------------------------------------------------------------------
# Extension modules

add_subdirectory(SMTKFileReader)
add_subdirectory(SMTKFileWriter)

#-----------------------------------------------------------------------------
# Install Boost libraries

# Variable SMTK_BOOST_REQUIRED_COMPONENTS is assumed to be set in External_Boost.cmake
if(NOT SMTK_BOOST_REQUIRED_COMPONENTS)
  message(FATAL_ERROR "Variable SMTK_BOOST_REQUIRED_COMPONENTS is not set")
endif()

foreach(boost_lib IN LISTS SMTK_BOOST_REQUIRED_COMPONENTS)

  # Lookup target
  set(_boost_target "Boost::${boost_lib}")
  if(NOT TARGET ${_boost_target})
    message(FATAL_ERROR "Failed to lookup target ${_boost_target}")
  endif()

  # Extract location
  if(WIN32)
    get_target_property(_imported_library ${_boost_target} IMPORTED_IMPLIB_RELEASE)
  else()
    get_target_property(_imported_library ${_boost_target} IMPORTED_LOCATION_RELEASE)
  endif()

  if(NOT _imported_library)
    continue()
  endif()

  if(APPLE)
    # Skip installation: Library is expected to be copied by the fixup script
    # TODO: Ensure Boost_LIBRARY_DIRS is set and used in SlicerCPackBundleFixup.cmake
    continue()

  elseif(UNIX)
    set(_library ${_imported_library})

    slicerInstallLibrary(
      FILE ${_library}
      DESTINATION ${Slicer_THIRDPARTY_LIB_DIR}
      COMPONENT RuntimeLibraries
      PERMISSIONS OWNER_WRITE OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ
      )
  else(WIN32)
    get_filename_component(_imported_library_dir ${_imported_library} DIRECTORY)
    get_filename_component(_library_without_extension ${_imported_library} NAME_WE)
    set(_library ${_imported_library_dir}/${_library_without_extension}.dll)

    install(FILES "${_library}"
      DESTINATION ${Slicer_THIRDPARTY_LIB_DIR}
      COMPONENT RuntimeLibraries
      )
  endif()

endforeach()

#-----------------------------------------------------------------------------
set(EXTENSION_CPACK_INSTALL_CMAKE_PROJECTS)
list(APPEND EXTENSION_CPACK_INSTALL_CMAKE_PROJECTS "${CMAKE_BINARY_DIR}/../Netgen-build;Netgen;netgen;/")
list(APPEND EXTENSION_CPACK_INSTALL_CMAKE_PROJECTS "${CMAKE_BINARY_DIR}/../MOAB-build;MOAB;ALL;/")
list(APPEND EXTENSION_CPACK_INSTALL_CMAKE_PROJECTS "${smtk_DIR};smtk;ALL;/")
list(APPEND EXTENSION_CPACK_INSTALL_CMAKE_PROJECTS "${CMAKE_BINARY_DIR}/../aeva-session-build;aeva-session;ALL;/")

set(${EXTENSION_NAME}_CPACK_INSTALL_CMAKE_PROJECTS "${EXTENSION_CPACK_INSTALL_CMAKE_PROJECTS}" CACHE STRING "List of external projects to install" FORCE)

#-----------------------------------------------------------------------------
list(APPEND CPACK_INSTALL_CMAKE_PROJECTS "${CMAKE_BINARY_DIR};${EXTENSION_NAME};ALL;/")
list(APPEND CPACK_INSTALL_CMAKE_PROJECTS "${${EXTENSION_NAME}_CPACK_INSTALL_CMAKE_PROJECTS}")
include(${Slicer_EXTENSION_GENERATE_CONFIG})
include(${Slicer_EXTENSION_CPACK})

