cmake_minimum_required(VERSION 3.8.2)

#-----------------------------------------------------------------------------
# Plugin name and version

project(read-and-write-resource-manager-state VERSION 1.0)

#-----------------------------------------------------------------------------
# This plugin requires C++11

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_EXTENSIONS False)

#-----------------------------------------------------------------------------
# Plugin options

option(ENABLE_TESTING "Enable Testing" OFF)

#-----------------------------------------------------------------------------
# Find packages for the plugin
find_package(smtk REQUIRED)
find_package(ParaView REQUIRED)
find_package(Boost 1.64.0 REQUIRED COMPONENTS filesystem)
find_package(Qt5 REQUIRED COMPONENTS Core)

#-----------------------------------------------------------------------------
# SMTK settings

# We need the path to SMTK's include directory so EncodeStringFunctions.cmake
# finds things.
get_target_property(SMTK_INCLUDE_DIR smtkCore INTERFACE_INCLUDE_DIRECTORIES)

# smtk's included CMake macros
include(SMTKMacros)
include(SMTKOperationXML)

#-----------------------------------------------------------------------------
# ParaView settings

# ParaView's included CMake macros
include(${PARAVIEW_USE_FILE})
include(ParaViewPlugins)

#-----------------------------------------------------------------------------
# Boost settings

add_library(boost_cxx_flags INTERFACE)
# For MSVC:
#  (/EHsc) setup windows exception handling
#  (/wd4996) quiet warnings about printf being potentially unsafe
#  (/wd4503) quiet warnings about truncating decorated name
#  (-DBOOST_ALL_NO_LIB) remove autolinking
target_compile_options(boost_cxx_flags INTERFACE
  $<$<CXX_COMPILER_ID:MSVC>:/EHsc /wd4996 /wd4503 -DBOOST_ALL_NO_LIB>)

set_property(TARGET Boost::boost APPEND PROPERTY INTERFACE_LINK_LIBRARIES boost_cxx_flags)

#-----------------------------------------------------------------------------
# CMake build settings

# Setting this ensures that "make install" will leave rpaths to external
# libraries (not part of the build-tree e.g. Qt, ffmpeg, etc.) intact on
# "make install". This ensures that one can install a version of ParaView on the
# build machine without any issues. If this not desired, simply specify
# CMAKE_INSTALL_RPATH_USE_LINK_PATH when configuring and "make install" will
# strip all rpaths, which is default behavior.
if (NOT CMAKE_INSTALL_RPATH_USE_LINK_PATH)
  set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
endif ()

# Set the directory where the binaries will be stored
set(EXECUTABLE_OUTPUT_PATH         ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

# Add our Cmake directory to the module search path
list(APPEND CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/CMake)

#-----------------------------------------------------------------------------
# Plugin sources

# Build the library containing the state I/O operations
add_subdirectory(operators)

# Build the plugin containing the File menu items
add_subdirectory(plugin)

#-----------------------------------------------------------------------------
# Plugin tests

if (ENABLE_TESTING)
  enable_testing()
  include(TestingMacros)
  add_subdirectory(testing)
endif()

#-----------------------------------------------------------------------------
# Plugin configuration

include(CMakePackageConfigHelpers)

# Our requirements for a version file are basic, so we use CMake's basic version
# file generator
write_basic_package_version_file(
  "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}ConfigVersion.cmake"
  VERSION ${${PROJECT_NAME}_VERSION}
  COMPATIBILITY AnyNewerVersion
)

# Export the targets generated by the plugin
export(EXPORT ReadWriteSMTKResourceManagerState
  FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}Targets.cmake"
)

# As an SMTK plugin-generating project, consuming this project at configure-time
# should append this project's plugins to the global list of SMTK plugins
get_property(SMTK_PLUGINS GLOBAL PROPERTY SMTK_PLUGINS)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}Config.cmake"
  "include(\"\${CMAKE_CURRENT_LIST_DIR}/${PROJECT_NAME}Targets.cmake\")\n"
  "list(APPEND\ SMTK_PLUGINS\ ${SMTK_PLUGINS})"
  )

# Install the generated targets file
set(ConfigPackageLocation lib/cmake/${PROJECT_NAME})
install(EXPORT ReadWriteSMTKResourceManagerState
  FILE
    ${PROJECT_NAME}Targets.cmake
  DESTINATION
    ${ConfigPackageLocation}
)

# Install the generated configure files
install(
  FILES
    "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}Config.cmake"
    "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}ConfigVersion.cmake"
  DESTINATION
    ${ConfigPackageLocation}
  COMPONENT
    Devel
)
