cmake_minimum_required(VERSION 3.8.2)

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

project(truchas-extensions 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(BUILD_SHARED_LIBS "Build Truchas Extensions using shared libraries" ON)
option(ENABLE_TESTING "Enable Testing" OFF)
option(BUILD_EXAMPLES "Build example applications" ON)

include(CMakeDependentOption)

#-----------------------------------------------------------------------------
# Find packages for the plugin
find_package(smtk REQUIRED)
find_package(ParaView QUIET)
find_package(Qt5 COMPONENTS Core Gui Widgets)

#-----------------------------------------------------------------------------
# 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)
get_target_property(SMTK_LIB_DIR smtkCore LOCATION)
get_filename_component(SMTK_LIB_DIR ${SMTK_LIB_DIR} DIRECTORY)

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

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

# ParaView's included CMake macros
if (ParaView_FOUND AND TARGET smtkPluginSupport)
  include(${PARAVIEW_USE_FILE})
  include(ParaViewPlugins)
endif ()

#-----------------------------------------------------------------------------
# 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 tests

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

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

# Include the plugin's source directory
add_subdirectory(smtk)

#-----------------------------------------------------------------------------
# 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 TruchasExtensions
  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 TruchasExtensions
  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
)
