cmake_minimum_required(VERSION 3.3)

# Set project name.
project(XMSMeshOperation)

cmake_minimum_required(VERSION 3.8.2)
if (POLICY CMP0042)
  cmake_policy(SET CMP0042 NEW)
endif()
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_EXTENSIONS False)
set(CMAKE_MODULE_PATH
  "${SMTK_DIR}"
  ${CMAKE_MODULE_PATH}
)

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

#Set up XMS and required dependencies
#########################################################################
find_package(Boost 1.66.0
             COMPONENTS thread filesystem system date_time log log_setup timer serialization REQUIRED)

find_library(XMS_CORE_LIB NAMES xmscore)
set(XMS_CORE_INCLUDE "" CACHE FILEPATH "Path to XMS Core includes")
find_library(XMS_MESH_LIB NAMES xmsmesh)
set(XMS_MESH_INCLUDE "" CACHE FILEPATH "Path to XMS Mesh includes")
find_library(XMS_INTERP_LIB NAMES xmsinterp)
set(XMS_INTERP_INCLUDE "" CACHE FILEPATH "Path to XMS Interp includes")

# #Set up XMS and required dependencies using conan
# include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
# conan_basic_setup(TARGETS)

include(XMSSMTKTestingMacros)

#Set up SMTK
#########################################################################
find_package(smtk REQUIRED)

# this is probably not the right way to do this, but 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)

include(SMTKMacros)
include(SMTKOperationXML)

if (TARGET smtkPluginSupport)
  find_package(ParaView REQUIRED)
  include(${PARAVIEW_USE_FILE})
endif()

#Setup up the option to enable the testing directory
option(ENABLE_TESTING "Enable Testing" OFF)

#Setup up the option to enable python wrapping using pybind11
option(ENABLE_PYTHON_WRAPPING "Build Python Wrappings" OFF)

include(CMakeDependentOption)

cmake_dependent_option(
  INSTALL_PYTHON_TO_SITE_PACKAGES
  "Install Python modules to the interpreter's site-packages directory or into CMAKE_INSTALL_PREFIX?"
  OFF
  ENABLE_PYTHON_WRAPPING OFF)

if(ENABLE_PYTHON_WRAPPING)
  # by including the correct python cmake scripts before adding pybind11, we
  # work around pybind11's custom python cmake scripts without having to patch
  # pybind11.
  find_package(PythonInterp REQUIRED)
  find_package(PythonLibs REQUIRED)
  if(MSVC)
    set(PYTHON_MODULE_EXTENSION ".pyd")
  else()
    set(PYTHON_MODULE_EXTENSION ".so")
  endif()
  set(PYTHON_MODULE_PREFIX "")

  find_package(pybind11 REQUIRED)

  set(PYBIND11_FLAGS " ")
  if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR
      CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR
      CMAKE_CXX_COMPILER_ID MATCHES "Intel")
    set(PYBIND11_FLAGS "${PYBIND11_FLAGS} -Wno-shadow")
  endif()

  # Initialize PYTHON_MODULEDIR.
  # This stores the location where we'll install our Python modules.
  # Note that PYTHON_MODULEDIR may be provided if the project is being
  # built as a submodule or as part of a superbuild.
  if (NOT DEFINED PYTHON_MODULEDIR)
    if (INSTALL_PYTHON_TO_SITE_PACKAGES)
      execute_process(
        COMMAND
        ${PYTHON_EXECUTABLE}
        -c "import site; print(site.getsitepackages())[-1]"
        RESULT_VARIABLE PYTHON_MODULEDIR
        )
    elseif(WIN32)
      set(PYTHON_MODULEDIR
        "bin/Lib/site-packages")
    else()
      set(PYTHON_MODULEDIR
        "lib/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}/site-packages")
    endif()
  endif()
endif()

if(ENABLE_TESTING)
  enable_testing()

  set(CMAKE_INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/lib/")
endif()

###  Header locations
#
# Locations of dependent package headers
include_directories(
  "${CMAKE_CURRENT_SOURCE_DIR}"
  "${CMAKE_CURRENT_BINARY_DIR}"
)

add_subdirectory(smtk)

export(
  TARGETS smtkXMSMesh
  FILE    "${CMAKE_BINARY_DIR}/XMSSMTK-targets.cmake")

if(WIN32)
  #on windows you want to tell boost to only link to the libs it needs
  target_compile_definitions(smtkXMSMesh PRIVATE BOOST_ALL_NO_LIB)
endif()
