## Version: $Id$
##
######################################################################
##
### Commentary:
##
######################################################################
##
### Change Log:
##
######################################################################
##
### Code:

cmake_minimum_required(VERSION 3.2.0)

######################################################################

project(dtk)

## ###################################################################
## Version setup
## ###################################################################

set(dtk_VERSION_MAJOR 1)
set(dtk_VERSION_MINOR 7)
set(dtk_VERSION_PATCH 1)
set(dtk_VERSION
  ${dtk_VERSION_MAJOR}.${dtk_VERSION_MINOR}.${dtk_VERSION_PATCH})

## ###################################################################
## Path setup
## ###################################################################

include(GNUInstallDirs)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)

set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  message(STATUS "Setting build type to 'RelWithDebInfo' as none was specified.")
  set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build." FORCE)
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")

## #################################################################
## Generate compilation database
## #################################################################

set(CMAKE_EXPORT_COMPILE_COMMANDS "ON")

## ###################################################################
## Use ccache on every platform
## ###################################################################

find_program(CCACHE_FOUND ccache)

if(CCACHE_FOUND)
  set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
  set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
endif(CCACHE_FOUND)

## #################################################################
## Install prefix
## #################################################################

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/install" CACHE PATH "${PROJECT_NAME} install prefix" FORCE)
endif(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)

## ###################################################################
## Build setup
## ###################################################################

set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

include(GenerateExportHeader)

if(UNIX)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unknown-pragmas")
endif(UNIX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(CMAKE_SIZEOF_VOID_P MATCHES 8)
  set(DTK_BUILD_32 0)
  set(DTK_BUILD_64 1)
else(CMAKE_SIZEOF_VOID_P MATCHES 8)
  set(DTK_BUILD_32 1)
  set(DTK_BUILD_64 0)
endif(CMAKE_SIZEOF_VOID_P MATCHES 8)

if(MSVC)
  add_definitions("/wd4068 /wd4244 /wd4996 /wd4005 /wd4305")
endif(MSVC)

## #################################################################
## Options
## #################################################################

option(DTK_ENABLE_COVERAGE           "enable coverage report"          false)
option(DTK_COVERAGE_USE_COBERTURA    "use cobertura coverage format"    true)
option(DTK_ENABLE_MEMCHECK           "enable valgrind memcheck report" false)

option(DTK_BUILD_COMPOSER            "Build composer layer"    true)
option(DTK_BUILD_DISTRIBUTED         "Build distributed layer" true)
option(DTK_BUILD_SCRIPT              "Build script layer"     false)
option(DTK_BUILD_WRAPPERS            "Build wrappers"         false)
option(DTK_BUILD_WIDGETS             "Build widgets"           true)
option(DTK_BUILD_SUPPORT_CORE        "Build support layer"    false)
option(DTK_BUILD_SUPPORT_CONTAINER   "Build support layer"    false)
option(DTK_BUILD_SUPPORT_COMPOSER    "Build support layer"    false)
option(DTK_BUILD_SUPPORT_DISTRIBUTED "Build support layer"    false)
option(DTK_BUILD_SUPPORT_GUI         "Build support layer"    false)
option(DTK_BUILD_SUPPORT_MATH        "Build support layer"    false)
option(DTK_BUILD_SUPPORT_PLOT        "Build support layer"    false)
option(DTK_BUILD_SUPPORT_VR          "Build support layer"    false)

## ###################################################################
## Test setup
## ###################################################################

if(DTK_ENABLE_MEMCHECK)

  find_program(CTEST_MEMORYCHECK_COMMAND valgrind)

  set(CTEST_MEMORYCHECK_COMMAND_OPTIONS "--trace-children=yes --leak-check=full --xml=yes --suppressions=${CMAKE_SOURCE_DIR}/tst/gtk.suppressions  --xml-file=valgrind-%p.xml")

  function(add_test name binary)
    set(memcheck_command "${CTEST_MEMORYCHECK_COMMAND} ${CTEST_MEMORYCHECK_COMMAND_OPTIONS}")
    separate_arguments(memcheck_command)
    _add_test(${name}_memcheck ${memcheck_command} ${binary} ${ARGN})
  endfunction(add_test)

  function(set_memcheck_test_properties name)
    set_tests_properties(${name}_memcheck ${ARGN})
  endfunction(set_memcheck_test_properties)

endif(DTK_ENABLE_MEMCHECK)

enable_testing()

if(DTK_ENABLE_COVERAGE)

  if(UNIX AND CMAKE_COMPILER_IS_GNUCXX)
    set(CMAKE_CXX_FLAGS        "${CMAKE_CXX_FLAGS}        -fprofile-arcs -ftest-coverage")
    set(CMAKE_C_FLAGS          "${CMAKE_CXX_FLAGS}        -fprofile-arcs -ftest-coverage")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  endif(UNIX AND CMAKE_COMPILER_IS_GNUCXX)

  include(dtkCodeCoverage)
  include(FindPythonInterp)

  if(DTK_COVERAGE_USE_COBERTURA)
    SETUP_TARGET_FOR_COVERAGE_COBERTURA(cov ctest coverage)
  else(DTK_COVERAGE_USE_COBERTURA)
    SETUP_TARGET_FOR_COVERAGE(cov ctest coverage)
  endif(DTK_COVERAGE_USE_COBERTURA)

endif(DTK_ENABLE_COVERAGE)

## #################################################################
## Dependencies
## #################################################################

find_package(Qt5 REQUIRED COMPONENTS
  Core
  Concurrent
  Quick
  Network
  Gui
  Test
  Svg
  Widgets
  Xml)

## ###################################################################
##
## ###################################################################

include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/dtkWrap.cmake)

## ###################################################################
## Doc setup
## ###################################################################

set(dtk_INSTALL_DOCS ${CMAKE_BINARY_DIR}/html)
set(dtk_CONFIG_DOCS  ${CMAKE_BINARY_DIR}/doc/cfg)

get_target_property(QT5_QMAKE_EXECUTABLE Qt5::qmake LOCATION)

get_filename_component(QT5_BINARY_DIR ${QT5_QMAKE_EXECUTABLE} PATH)

## #################################################################
## Dependencies - Optional                           [TO BE REMOVED]
## #################################################################

set(DTK_HAVE_MPI "NO")

if(DTK_BUILD_SUPPORT_DISTRIBUTED)

find_package(MPI QUIET)

if(MPI_FOUND)
  include_directories(${MPI_INCLUDE_PATH})
  set(COMPILE_FLAGS ${COMPILE_FLAGS} ${MPI_COMPILE_FLAGS})
  set(DTK_HAVE_MPI "YES")
endif(MPI_FOUND)

endif(DTK_BUILD_SUPPORT_DISTRIBUTED)

set(DTK_HAVE_ZLIB "NO")

find_package(ZLIB QUIET)
if (ZLIB_FOUND)
  include_directories( ${ZLIB_INCLUDE_DIRS} )
  set(DTK_HAVE_ZLIB "YES")
endif (ZLIB_FOUND)

set(DTK_HAVE_VRPN "NO")

if(DTK_BUILD_SUPPORT_VR)

## #################################################################
## OpenNI / Nite
## #################################################################

find_path(OPENNI_INCLUDES XnOpenNI.h /usr/include/ni)
find_path(  NITE_INCLUDES XnVNite.h  /usr/include/nite)

if(OPENNI_INCLUDES AND NITE_INCLUDES)
  include_directories(/usr/include/ni)
  include_directories(/usr/include/nite)
endif(OPENNI_INCLUDES AND NITE_INCLUDES)

find_library(OPENNI_LIBRARY NAMES OpenNI  PATHS /usr/lib)
find_library(  NITE_LIBRARY NAMES XnVNite PATHS /usr/lib)

if(OPENNI_LIBRARY AND NITE_LIBRARY)
  link_directories(/usr/lib)
endif(OPENNI_LIBRARY AND NITE_LIBRARY)

if(OPENNI_INCLUDES AND NITE_INCLUDES AND NOT DEFINED DTK_HAVE_NITE)
  set(DTK_HAVE_NITE "YES")
else(OPENNI_INCLUDES AND NITE_INCLUDES AND NOT DEFINED DTK_HAVE_NITE)
  set(DTK_HAVE_NITE "NO")
endif(OPENNI_INCLUDES AND NITE_INCLUDES AND NOT DEFINED DTK_HAVE_NITE)

mark_as_advanced(OPENNI_INCLUDES)
mark_as_advanced(NITE_INCLUDES)

mark_as_advanced(OPENNI_LIBRARY)
mark_as_advanced(NITE_LIBRARY)

## #################################################################
## Vrpn
## #################################################################

find_path(QUAT_INCLUDES           quat.h /usr/include /usr/local/include)
find_path(VRPN_INCLUDES vrpn_Configure.h /usr/include /usr/local/include)

if(QUAT_INCLUDES AND VRPN_INCLUDES)
  include_directories(${QUAT_INCLUDES})
  include_directories(${VRPN_INCLUDES})
endif(QUAT_INCLUDES AND VRPN_INCLUDES)

mark_as_advanced(VRPN_INCLUDES)
mark_as_advanced(QUAT_INCLUDES)

find_library(QUAT_LIBRARY NAMES quat PATHS /usr/lib /usr/local/lib)
find_library(VRPN_LIBRARY NAMES vrpn PATHS /usr/lib /usr/local/lib)

if(QUAT_LIBRARY AND VRPN_LIBRARY)
  add_definitions(-DHAVE_VRPN)
  set(DTK_HAVE_VRPN "YES")
endif(QUAT_LIBRARY AND VRPN_LIBRARY)

mark_as_advanced(QUAT_LIBRARY)
mark_as_advanced(VRPN_LIBRARY)

endif(DTK_BUILD_SUPPORT_VR)

## #################################################################
## Dependencies - internal
## #################################################################

include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/dtk)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/dtkComposer)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/dtkCore)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/dtkDistributed)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/dtkDistributedTest)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/dtkLog)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/dtkMath)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/dtkMeta)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/dtkScript)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/dtkTest)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/dtkWidgets)

if(DTK_BUILD_SUPPORT_CORE)
  include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/dtkCoreSupport)
endif()

if(DTK_BUILD_SUPPORT_CONTAINER)
    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/dtkContainerSupport)
endif()

if(DTK_BUILD_SUPPORT_COMPOSER)
  include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/dtkComposerSupport)
endif()

if(DTK_BUILD_SUPPORT_DISTRIBUTED)
  include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/dtkDistributedSupport)
endif()

if(DTK_BUILD_SUPPORT_GUI)
  include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/dtkGuiSupport)
endif()

if(DTK_BUILD_SUPPORT_MATH)
  include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/dtkMathSupport)
endif()

if(DTK_BUILD_SUPPORT_PLOT)
  include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/dtkPlotSupport)
endif()

if(DTK_BUILD_SUPPORT_VR)
  include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/dtkVrSupport)
endif()

## ###################################################################
## Input
## ###################################################################

add_subdirectory(src)
add_subdirectory(tst)
add_subdirectory(app)
add_subdirectory(doc)
add_subdirectory(wrp)

## ###################################################################
## Exporting
## ###################################################################

export(PACKAGE dtk)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/dtkConfig.cmake.in
  "${CMAKE_CURRENT_BINARY_DIR}/dtkConfig.cmake" @ONLY)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/dtkConfig.install.cmake.in
  "${PROJECT_BINARY_DIR}/install/dtkConfig.cmake" @ONLY)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/dtkConfigVersion.cmake.in
  "${CMAKE_CURRENT_BINARY_DIR}/dtkConfigVersion.cmake" @ONLY)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/dtkSetTestEnvironment.cmake.in
  "${CMAKE_CURRENT_BINARY_DIR}/dtkSetTestEnvironment.cmake" @ONLY)

set_directory_properties(PROPERTIES TEST_INCLUDE_FILE "${CMAKE_CURRENT_BINARY_DIR}/dtkSetTestEnvironment.cmake")

install(FILES
  "${CMAKE_CURRENT_BINARY_DIR}/install/dtkConfig.cmake"
  "${CMAKE_CURRENT_BINARY_DIR}/dtkConfigVersion.cmake"
  "${CMAKE_CURRENT_BINARY_DIR}/dtkSetTestEnvironment.cmake"
  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/dtkCodeCoverage.cmake"
  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/dtkWrap.cmake"
  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindSIP.cmake"
  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindSIP.py"
  DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/dtk)

install(EXPORT dtkDepends DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/dtk)

## ###################################################################
## Beautifying
## ###################################################################

mark_as_advanced(dtk_VERSION_MAJOR)
mark_as_advanced(dtk_VERSION_MINOR)
mark_as_advanced(dtk_VERSION_BUILD)

mark_as_advanced(Qt5_DIR)
mark_as_advanced(Qt5Concurrent_DIR)
mark_as_advanced(Qt5Core_DIR)
mark_as_advanced(Qt5Gui_DIR)
mark_as_advanced(Qt5Network_DIR)
mark_as_advanced(Qt5Qml_DIR)
mark_as_advanced(Qt5Quick_DIR)
mark_as_advanced(Qt5QuickTest_DIR)
mark_as_advanced(Qt5Script_DIR)
mark_as_advanced(Qt5Svg_DIR)
mark_as_advanced(Qt5Test_DIR)
mark_as_advanced(Qt5Widgets_DIR)
mark_as_advanced(Qt5Xml_DIR)

mark_as_advanced(CMAKE_AR)
mark_as_advanced(CMAKE_BUILD_TYPE)
mark_as_advanced(CMAKE_INSTALL_PREFIX)
mark_as_advanced(CMAKE_OSX_ARCHITECTURES)
mark_as_advanced(CMAKE_OSX_DEPLOYMENT_TARGET)
mark_as_advanced(CMAKE_OSX_SYSROOT)
mark_as_advanced(CTEST_MEMORYCHECK_COMMAND)

mark_as_advanced(DTK_ENABLE_COVERAGE)
mark_as_advanced(DTK_ENABLE_MEMCHECK)
mark_as_advanced(DTK_COVERAGE_USE_COBERTURA)
mark_as_advanced(DTK_PYTHON_VERSION)

mark_as_advanced(CCACHE_FOUND)

######################################################################
### CMakeLists.txt ends here
