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)

if (NOT DEFINED ENV{CONDA_BUILD})
    # Download automatically, you can also just copy the conan.cmake file
    if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
       message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
       file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/v0.13/conan.cmake"
                     "${CMAKE_BINARY_DIR}/conan.cmake")
    endif()

    include(${CMAKE_BINARY_DIR}/conan.cmake)

    # conan.cmake implicitly assumes that the cmake executable is accessible from
    # the command line. We therefore add its location to the local environment's
    # PATH when configuring the xms libraries.
    get_filename_component(cmake_exe_location ${CMAKE_COMMAND} DIRECTORY)
    set(envsep ":")
    if (WIN32)
      set(envsep ";")
    endif ()
    set(path $ENV{PATH}${envsep}${cmake_exe_location})

    conan_cmake_run(REQUIRES
      xmscore/[>=2.0.0,<3.0.0]@aquaveo/stable
      xmsinterp/[>=2.0.0,<3.0.0]@aquaveo/stable
      xmsgrid/[>=2.0.0,<3.0.0]@aquaveo/stable
      xmsmesh/[>=1.0.0,<99.99.99]@aquaveo/stable
      BASIC_SETUP
      KEEP_RPATHS
      BUILD missing
      ENV PATH=${path})

    conan_define_targets()
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)

include(XMSSMTKTestingMacros)

#Setup 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()

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

#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.
  if (NOT PYTHONINTERP_FOUND)
    find_package(PythonInterp 2.7 REQUIRED)
  endif ()

  if (NOT PYTHONLIBS_FOUND)
    find_package(PythonLibs REQUIRED)
  endif ()

  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()
