cmake_minimum_required(VERSION 3.9.0)

#if(POLICY CMP0025)
#  #setup policy rules for CMake 3.0 while we have a minimum required of 2.8.X
#  #both 42 and 25 were introduced in the same cmake version
#  cmake_policy(SET CMP0025 NEW)  # Clang is now Clang and Apple Clang
#  cmake_policy(SET CMP0042 OLD)  # Disable @rpath on Apple
#endif()

#If the user/superbuild hasn't explicitly stated what c++ standard to use
#require C++11
if(NOT DEFINED CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 11)
  set(CMAKE_CXX_STANDARD_REQUIRED True)
  set(CMAKE_CXX_EXTENSIONS FALSE)
endif()

project(cmb)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

option(cmb_enable_testing "Build CMB Testing" ON)
if (cmb_enable_testing)
  enable_testing()
  include(CTest)

  set(cmb_test_dir ${cmb_BINARY_DIR}/testing/temporary)
  make_directory(${cmb_test_dir})

  unset(cmb_data_dir CACHE)
  file(READ "${CMAKE_CURRENT_SOURCE_DIR}/data/cmb-data" cdata)
  if (NOT cdata STREQUAL "\n")
    message(WARNING
      "Testing is enabled, but CMB's data is not available. Use git-lfs in order "
      "to obtain the testing data.")
    set(cmb_data_dir)
  else ()
    set(cmb_data_dir "${CMAKE_CURRENT_SOURCE_DIR}/data")
  endif ()

  unset(smtk_data_dir CACHE)
  file(READ "${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/smtk/data/smtk-data" sdata)
  if (NOT sdata STREQUAL "\n")
    message(WARNING
      "Testing is enabled, but SMTK's data is not available. Use git-lfs in order "
      "to obtain the testing data.")
    set(smtk_data_dir)
  else ()
    set(smtk_data_dir "${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/smtk/data")
  endif ()

  #add the first test which is for checking the copyright
  add_test(NAME cmb-copyright-check
    COMMAND ${CMAKE_COMMAND}
      -D "cmb_SOURCE_DIR=${cmb_SOURCE_DIR}"
      -P "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CheckCopyright.cmake"
  )
  set_tests_properties(cmb-copyright-check
    PROPERTIES LABELS "cmb;swprocess"
  )
endif()

# Fetch CMB version from version.txt and set up install/output directories:
include(CMBInstallRules)

# Add options for performing code coverage tests:
include(CMBCoverage)

# Do not report some warnings from generated code to the dashboard:
configure_file(
  "${CMAKE_CURRENT_SOURCE_DIR}/CTestCustom.cmake.in"
  "${CMAKE_CURRENT_BINARY_DIR}/CTestCustom.cmake")

find_package(ParaView COMPONENTS vtkIOGDAL REQUIRED)
find_package(Qt5 REQUIRED COMPONENTS Core Widgets)

include(${PARAVIEW_USE_FILE})

set(cmb_plugin_paths
  "${cmb_plugin_paths}"
  "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}"
  CACHE STRING "Semi-colon separated paths for testing plugins."
)
mark_as_advanced(cmb_plugin_paths)

add_subdirectory(thirdparty)
add_subdirectory(plugins) # Plugins must be built before the app so .plugins file will be correct.
add_subdirectory(modelbuilder)

option(cmb_enable_documentation "Include targets Sphinx-generated documentation" OFF)
if (cmb_enable_documentation)
  find_package(Sphinx)
  add_subdirectory(doc)
endif()
