#############################################################################
#
# This file is part of the ViSP software.
# Copyright (C) 2005 - 2015 by Inria. All rights reserved.
#
# This software is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# ("GPL") version 2 as published by the Free Software Foundation.
# See the file LICENSE.txt at the root directory of this source
# distribution for additional information about the GNU GPL.
#
# For using ViSP with software that can not be combined with the GNU
# GPL, please contact Inria about acquiring a ViSP Professional
# Edition License.
#
# See http://visp.inria.fr for more information.
#
# This software was developed at:
# Inria Rennes - Bretagne Atlantique
# Campus Universitaire de Beaulieu
# 35042 Rennes Cedex
# France
#
# If you have questions regarding the use of this file, please contact
# Inria at visp@inria.fr
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# Description:
# ViSP overall configuration file. Detect third party libraries (X11, GTK, ...)
#
# Authors:
# Fabien Spindler
#
#############################################################################

# Need to be befor project(VISP) to work
if(WIN32)
  set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "Installation Directory")
endif()

project(VISP C CXX)

cmake_minimum_required(VERSION 2.8.3)

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")

include(cmake/VISPUtils.cmake)

vp_clear_vars(VISPModules_TARGETS)

#-----------------------------------------------------------------------------
# VISP version number.  An even minor number corresponds to releases.
set(VISP_VERSION_MAJOR "3")
set(VISP_VERSION_MINOR "0")
set(VISP_VERSION_PATCH "0")
set(VISP_VERSION "${VISP_VERSION_MAJOR}.${VISP_VERSION_MINOR}.${VISP_VERSION_PATCH}")
# Package revision number
set(VISP_REVISION "1")

find_file(GNU_INSTALL_DIRS_FROM_CMAKE NAMES GNUInstallDirs.cmake PATHS ${CMAKE_ROOT}/Modules)
mark_as_advanced(GNU_INSTALL_DIRS_FROM_CMAKE)
if(GNU_INSTALL_DIRS_FROM_CMAKE)
  include(${CMAKE_ROOT}/Modules/GNUInstallDirs.cmake)
else()
  include(cmake/GNUInstallDirs.cmake)
endif()

# the include directory we depend on
set(VISP_INCLUDE_DIR ${VISP_BINARY_DIR}/${CMAKE_INSTALL_INCLUDEDIR})
set(VISP_DOC_DIR "${VISP_BINARY_DIR}/doc")

# The location in which to install VISP libraries.
set(LIBRARY_OUTPUT_PATH ${VISP_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
# The location in which to install some VISP binaries.
set(BINARY_OUTPUT_PATH ${VISP_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})

include_directories(${VISP_BINARY_DIR}/include)

if(WIN32)
  # Postfix of .lib and .dll
  set(VISP_DEBUG_POSTFIX "d")
  set(VISP_DLLVERSION "${VISP_VERSION_MAJOR}${VISP_VERSION_MINOR}${VISP_VERSION_PATCH}")
else()
  set(VISP_DEBUG_POSTFIX "")
  set(VISP_DLLVERSION "")
endif()

# ----------------------------------------------------------------------------
# Handle always full RPATH
# http://www.cmake.org/Wiki/CMake_RPATH_handling
# ----------------------------------------------------------------------------

# avoid CMP0042 warning
if(APPLE)
  set(CMAKE_MACOSX_RPATH ON)
endif()

# use, i.e. don't skip the full RPATH for the build tree
set(CMAKE_SKIP_BUILD_RPATH  FALSE)

# when building, don't use the install RPATH already
# (but later on when installing)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)

set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")

# add the automatically determined parts of the RPATH
# which point to directories outside the build tree to the install RPATH
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

# the RPATH to be used when installing, but only if it's not a system directory
list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}" isSystemDir)
if("${isSystemDir}" STREQUAL "-1")
  set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
endif("${isSystemDir}" STREQUAL "-1")

# ----------------------------------------------------------------------------
#  Path for additional contrib modules
# ----------------------------------------------------------------------------
set(VISP_CONTRIB_MODULES_PATH "" CACHE PATH "Where to look for additional contrib ViSP modules")

# Get the OS
set(OS ${CMAKE_SYSTEM_NAME})

set(OGRE_HOME $ENV{OGRE_HOME})
if(OGRE_HOME)
  # replace \ with / especially for windows
  STRING(REGEX REPLACE "\\\\" "/" OGRE_HOME ${OGRE_HOME}) 
endif()

# add the path to detect Ogre3D
if(WIN32)
  list(APPEND CMAKE_MODULE_PATH "${OGRE_HOME}/CMake")
endif(WIN32)
 
if(UNIX)
  list(APPEND CMAKE_MODULE_PATH "${OGRE_HOME}/cmake")
  list(APPEND CMAKE_MODULE_PATH "${OGRE_HOME}/CMake")
  list(APPEND CMAKE_MODULE_PATH "/usr/local/lib/OGRE/cmake")
  list(APPEND CMAKE_MODULE_PATH "/usr/lib/OGRE/cmake")
  list(APPEND CMAKE_MODULE_PATH "/usr/local/lib64/OGRE/cmake")
  list(APPEND CMAKE_MODULE_PATH "/usr/lib64/OGRE/cmake")
  list(APPEND CMAKE_MODULE_PATH "/usr/share/OGRE/cmake/modules")
endif(UNIX)

#--------------------------------------------------------------------
# Option management
#--------------------------------------------------------------------

# Choose static or shared libraries.
VP_OPTION(BUILD_SHARED_LIBS  "" "" "Build ViSP shared libraries (.dll/.so) instead of static ones (.lib/.a)" "" ON)
# Build examples as an option.
VP_OPTION(BUILD_EXAMPLES  "" "" "Build ViSP examples" "" ON)
# Build examples as an option.
VP_OPTION(BUILD_TESTS  "" "" "Build ViSP tests" "" ON)
VP_OPTION(BUILD_COVERAGE "" "" "Enables test coverage" "" OFF IF (BUILD_TESTS AND CMAKE_COMPILER_IS_GNUCXX AND NOT BUILD_SHARED_LIBS AND CMAKE_BUILD_TYPE MATCHES "Debug"))

# Build demos as an option.
VP_OPTION(BUILD_DEMOS  "" "" "Build ViSP demos" "" ON)
# Build demos as an option.
VP_OPTION(BUILD_TUTORIALS  "" "" "Build ViSP tutorials" "" ON)
# Build deprecated functions as an option.
VP_OPTION(BUILD_DEPRECATED_FUNCTIONS  "" "" "Build deprecated functionalities" "" ON)
# Debug and trace cflags
VP_OPTION(ACTIVATE_DEBUG_TRACE  "" "" "Enable debug and trace printings" "" ON)

VP_OPTION(BUILD_WITH_STATIC_CRT  "" "" "Enables use of staticaly linked CRT for staticaly linked ViSP" "" ON IF MSVC)

# Note that it is better to set MOMENTS_COMBINE_MATRICES to OFF
VP_OPTION(MOMENTS_COMBINE_MATRICES  "" "" "Use linear combination of matrices instead of linear combination of moments to compute interaction matrices." "MOMENTS_COMBINE_MATRICES" OFF)
VP_OPTION(ENABLE_TEST_WITHOUT_DISPLAY  "" "" "Don't use display features when testing" "ENABLE_TEST_WITHOUT_DISPLAY" ON)

if(ENABLE_TEST_WITHOUT_DISPLAY)
  set(OPTION_TO_DESACTIVE_DISPLAY "-d")
endif()

# Check for Inria's robot drivers
VP_CHECK_PACKAGE(RAW1394)
VP_CHECK_PACKAGE(RT)
VP_CHECK_PACKAGE(CALINUX)
VP_CHECK_PACKAGE(IRISA)
# Try to determine if Inria's NAS server hosting /udd/ is available
VP_CHECK_PACKAGE(NAS)
# check for linux/parport.h
VP_CHECK_PACKAGE(PARPORT)
# Find IsNaN
VP_CHECK_PACKAGE(IsNaN)
# Find IsInf
VP_CHECK_PACKAGE(IsInf)
# Find Round
VP_CHECK_PACKAGE(Round)
# OpenGL
VP_CHECK_PACKAGE(OpenGL)
# for pioneer
VP_CHECK_PACKAGE(RT)
VP_CHECK_PACKAGE(DL)

VP_OPTION(USE_AFMA4    "" "" "Include Afma4 robot support"      "" ON IF (RAW1394_FOUND AND RT_FOUND AND CALINUX_FOUND AND IRISA_FOUND))
VP_OPTION(USE_AFMA6    "" "" "Include Afma6 robot support"      "" ON IF (RAW1394_FOUND AND RT_FOUND AND CALINUX_FOUND AND IRISA_FOUND))
VP_OPTION(USE_VIPER650 "" "" "Include Viper s650 robot support" "" ON IF (RAW1394_FOUND AND RT_FOUND AND CALINUX_FOUND AND IRISA_FOUND))
VP_OPTION(USE_VIPER850 "" "" "Include Viper s850 robot support" "" ON IF (RAW1394_FOUND AND RT_FOUND AND CALINUX_FOUND AND IRISA_FOUND))
VP_OPTION(USE_DC1394      DC1394      "" "Include dc1394 support"          "" ON IF UNIX)
VP_OPTION(USE_V4L2        V4L2        "" "Include v4l2 support"            "" ON IF UNIX)
VP_OPTION(USE_BICLOPS     BICLOPS     "" "Include biclops support"         "" ON IF UNIX)
VP_OPTION(USE_PTU46       PTU46       "" "Include ptu-46 support"          "" ON IF UNIX)
VP_OPTION(USE_CMU1394     CMU1394     "" "Include cmu1494 support"         "" ON IF WIN32)
VP_OPTION(USE_GDI         GDI         "" "Include gdi support"             "" ON IF WIN32)
VP_OPTION(USE_DIRECT3D    DIRECT3D    "" "Include d3d support"             "" ON IF WIN32)
VP_OPTION(USE_DIRECTSHOW  DIRECTSHOW  "" "Include dshow support"           "" ON IF WIN32)
VP_OPTION(USE_OPENMP      OpenMP      "" "Include openmp support"          "" ON)
VP_OPTION(USE_CPP11       CPP11       "" "Include c++11 support"           "" OFF)
# Since the FindLAPACK.cmake provided with CMake is for Fortran language,
# in CMakeModules we have added FindLAPACK_C.cmake for C language
VP_OPTION(USE_LAPACK      LAPACK_C    "" "Include lapack support"          "" ON)
VP_OPTION(USE_GSL         GSL         "" "Include gsl support"             "" ON)
VP_OPTION(USE_COIN3D      "Coin3D;MyCoin3D" "" "Include coin3d support"          "" ON IF OPENGL_FOUND)
VP_OPTION(USE_YARP        YARP        QUIET "Include yarp support"         "YARP_DIR" ON)
VP_OPTION(USE_OGRE        OGRE        QUIET "Include Ogre support"         "OGRE_DIR" ON)
VP_OPTION(USE_OIS         OIS         QUIET "Include Ogre/ois support"     "OIS_DIR"  ON IF USE_OGRE)
VP_OPTION(USE_LIBFREENECT LIBFREENECT ""    "Include libfreenect support"  "" ON)
VP_OPTION(USE_LIBUSB_1    LIBUSB_1    ""    "Include libusb-1 support"     "" ON)
VP_OPTION(USE_SOWIN       SOWIN       ""    "Include Coin/SoWin support"   "" OFF IF (WIN32 AND USE_COIN3D))
VP_OPTION(USE_SOQT        SOQT        ""    "Include Coin/SoQt support"    "" OFF IF USE_COIN3D)
VP_OPTION(USE_QT          Qt          ""    "Include Coin/SoQt/Qt support" "" ON  IF USE_SOQT)
VP_OPTION(USE_SOXT        SOXT        ""    "Include Coin/SoXt support"    "" OFF IF USE_COIN3D)
VP_OPTION(USE_PTHREAD     PTHREAD     ""    "Include pthread support"      "" ON)
VP_OPTION(USE_XML2        XML2        ""    "Include xml support"          "" ON)
VP_OPTION(USE_OPENCV      OpenCV      QUIET "Include OpenCV support" "OpenCV_DIR;OpenCV_FOUND;OPENCV_FOUND" ON)
VP_OPTION(USE_ZLIB        "ZLIB;MyZLIB" ""  "Include zlib support"         "" ON)
VP_OPTION(USE_X11         X11         ""    "Include X11 support"          "" ON)
# The native FindGTK2.cmake doesn't consider libgobject-2.0 that is 
# requested by ViSP. That's why we use our FindMyGTK2.cmake 
VP_OPTION(USE_GTK2        MyGTK2       ""    "Include gtk2 support"         "" OFF)
VP_OPTION(USE_JPEG       "JPEG;MyJPEG" ""    "Include jpeg support"         "" ON)
VP_OPTION(USE_PNG        "PNG;MyPNG"   ""    "Include png support"          "" ON)
VP_OPTION(USE_FFMPEG      FFMPEG       ""    "Include ffmpeg support"       "" OFF)
# To control Pioneer mobile robots, under UNIX we need Aria, pthread, rt and dl 3rd party libraries
VP_OPTION(USE_ARIA        ARIA         ""    "Include aria support"         "" ON)
#VP_OPTION(USE_RT          RT           ""    "Include rt support"           "" ON)
#VP_OPTION(USE_DL          DL           ""    "Include dl support"           "" ON)
# bar codes
VP_OPTION(USE_ZBAR        ZBAR         ""    "Include zbar support"         "" ON)
VP_OPTION(USE_DMTX        DMTX         ""    "Include dmtx support"         "" ON)

#----------------------------------------------------------------------
# For Dart server and tests
# We use CDash set through CTestConfig.cmake file
# Dashboards are sent to http://cdash.irisa.fr/CDash/index.php?project=ViSP
#----------------------------------------------------------------------
if(BUILD_TESTS)
  enable_testing()
  mark_as_advanced(DART_ROOT)
  mark_as_advanced(BUILD_TESTING)
endif()

#----------------------------------------------------------------------
# Try to find doxygen for documentation generation
# Use "make visp_doc" target to generate the documentation
#----------------------------------------------------------------------
find_package(Doxygen)
if(DOXYGEN_FOUND)
  set(VISP_HAVE_DOXYGEN "yes")        # for header vpConfig.h
  set(VISP_HAVE_DOXYGEN_FOUND "yes")  # for ViSP-third-party.txt
  ## we need latex for doxygen because of the formulas
  find_package(LATEX)
  if(NOT LATEX_COMPILER)
    message(STATUS "latex command LATEX_COMPILER not found but usually required. You will probably get warnings and user interaction on doxy run.")
  endif()
  if(NOT MAKEINDEX_COMPILER)
    message(STATUS "makeindex command MAKEINDEX_COMPILER not found but usually required.")
  endif()
  if(NOT DVIPS_CONVERTER)
    message(STATUS "dvips command DVIPS_CONVERTER not found but usually required.")
  endif()

  configure_file(${VISP_SOURCE_DIR}/doc/config-doxygen.in
    ${VISP_DOC_DIR}/config-doxygen
    @ONLY )

  configure_file(${VISP_SOURCE_DIR}/doc/mainpage.doc.in
    ${VISP_DOC_DIR}/mainpage.doc
    @ONLY )
else()
  set(VISP_HAVE_DOXYGEN "no")        # for header vpConfig.h
  set(VISP_HAVE_DOXYGEN_FOUND "no")  # for ViSP-third-party.txt
endif()

# ----------------------------------------------------------------------------
# Solution folders:
# ----------------------------------------------------------------------------
if(MSVC_IDE OR CMAKE_GENERATOR MATCHES Xcode)
  option(ENABLE_SOLUTION_FOLDERS "Solution folder in Visual Studio or in other IDEs" ON)
endif()

if(ENABLE_SOLUTION_FOLDERS)
  set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "CMakeTargets")
endif()

# Extra ViSP targets: uninstall, etc.
include(cmake/VISPExtraTargets.cmake)

include(CheckLibraryExists)

# ----------------------------------------------------------------------------
# Check for system libs
# ----------------------------------------------------------------------------
if(UNIX)
  # try to found -lm requested on some platforms to link with X11
  find_library(M_LIBRARY NAMES m)
  mark_as_advanced(M_LIBRARY)
  if(M_LIBRARY)
    list(APPEND VISP_LINKER_LIBS ${M_LIBRARY})
  endif()
  # try to found -lsocket -lnsl requested for vpNetwork and vpSickLDMRS
  find_library(SOCKET_LIBRARY NAMES socket)
  find_library(NSL_LIBRARY NAMES nsl)
  mark_as_advanced(SOCKET_LIBRARY NSL_LIBRARY)
  if (SOCKET_LIBRARY)
    list(APPEND VISP_LINKER_LIBS ${SOCKET_LIBRARY})
  endif()
  if (NSL_LIBRARY)
    list(APPEND VISP_LINKER_LIBS ${NSL_LIBRARY})
  endif()
endif()

#----------------------------------------------------------------------
# Add definitions
#----------------------------------------------------------------------
# With Visual Studio 2005, Microsoft deprecates the standard C library, for
# example fopen() and sprintf(), to non-portable functions fopen_s() and
# sprintf_s(). These functions are considered by Microsoft more secure. This is
# a worthwhile exercise ! The use of these deprecated functions causes a lot of
# warnings. To suppress it, we add the _CRT_SECURE_NO_DEPRECATE preprocessor
# definition
if(WIN32 AND MSVC)
  add_definitions("-D_CRT_SECURE_NO_DEPRECATE")
endif()

#----------------------------------------------------------------------
# Use statically or dynamically linked CRT?
# Default: dynamic
#----------------------------------------------------------------------
if(MSVC)
   include(cmake/VISPCRTLinkage.cmake)
endif(MSVC)

#----------------------------------------------------------------------
# Platform specific
#----------------------------------------------------------------------
include(cmake/VISPDetectPlatform.cmake)

# Set the path where to install the lib
if(WIN32)
  if(DEFINED VISP_RUNTIME AND DEFINED VISP_ARCH)
    set(VISP_INSTALL_BINARIES_PREFIX "${VISP_ARCH}/${VISP_RUNTIME}/")
  else()
    message(STATUS "Can't detect runtime and/or arch")
    set(VISP_INSTALL_BINARIES_PREFIX "")
  endif()
else()
  set(VISP_INSTALL_BINARIES_PREFIX "")
endif()

# where to install the library
if(WIN32)
  if(VISP_STATIC)
    set(VISP_LIB_INSTALL_PATH   "${VISP_INSTALL_BINARIES_PREFIX}static${CMAKE_INSTALL_LIBDIR}")
  else()
    set(VISP_LIB_INSTALL_PATH   "${VISP_INSTALL_BINARIES_PREFIX}${CMAKE_INSTALL_LIBDIR}")
  endif()
  set(VISP_BIN_INSTALL_PATH   "${VISP_INSTALL_BINARIES_PREFIX}${CMAKE_INSTALL_BINDIR}")
else()
  set(VISP_LIB_INSTALL_PATH     ${CMAKE_INSTALL_LIBDIR})
  set(VISP_BIN_INSTALL_PATH     ${CMAKE_INSTALL_BINDIR})
endif()


#----------------------------------------------------------------------
# Configure the files that depend on the build <binary dir> or
# installation <install dir> usage. This is the case of:
# vpConfig.h
#----------------------------------------------------------------------

# case 1: when ViSP is build with make; files are used in <binary dir>
#--------------
set(VISP_ROOT_DIR_DATA_CONFIGCMAKE "${VISP_BINARY_DIR}")

set(VISP_SCENES_DIR ${VISP_ROOT_DIR_DATA_CONFIGCMAKE}/data/wireframe-simulator)
set(VISP_ROBOT_ARMS_DIR ${VISP_ROOT_DIR_DATA_CONFIGCMAKE}/data/robot-simulator)
    
# case 2: when ViSP is build with make install; files are used in <install dir>
#--------------
IF(UNIX)
  set(VISP_ROOT_DIR_DATA_CONFIGCMAKE "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATAROOTDIR}/visp-${VISP_VERSION}")
ELSE()
  set(VISP_ROOT_DIR_DATA_CONFIGCMAKE "${CMAKE_INSTALL_PREFIX}")
ENDIF()

list(APPEND VISP_SCENES_DIR ${VISP_ROOT_DIR_DATA_CONFIGCMAKE}/data/wireframe-simulator)
list(APPEND VISP_ROBOT_ARMS_DIR ${VISP_ROOT_DIR_DATA_CONFIGCMAKE}/data/robot-simulator)
# Only if resources.cfg created by hand, we change the path to resources.cfg in install/vpConfig.h
if(VISP_INSTALL_DIR_OGRE_RESOURCES)
  list(APPEND VISP_HAVE_OGRE_RESOURCES_PATH "${VISP_ROOT_DIR_DATA_CONFIGCMAKE}/data/ogre-simulator")
endif()
# append to VISP_HAVE_OGRE_PLUGINS_PATH the path of the installed plugins.cfg file
# to be able to use ViSP from build tree or install tree
if(VISP_INSTALL_DIR_OGRE_RESOURCES)
  list(APPEND VISP_HAVE_OGRE_PLUGINS_PATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/visp/data/ogre-simulator")
endif()

#----------------------------------------------------------------------
# Create and install visp-config.1.gz man page
#----------------------------------------------------------------------
if(UNIX)
  FIND_PROGRAM(GZIP gzip)
  file(MAKE_DIRECTORY ${VISP_BINARY_DIR}/doc/man/man1)
  ADD_CUSTOM_COMMAND(
    OUTPUT ${VISP_BINARY_DIR}/doc/man/man1/visp-config.1.gz
    COMMAND ${GZIP} --best -c ${CMAKE_CURRENT_SOURCE_DIR}/doc/man/man1/visp-config.1 > ${VISP_BINARY_DIR}/doc/man/man1/visp-config.1.gz
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/doc/man/man1/visp-config.1
  )
  ADD_CUSTOM_TARGET(man ALL
    DEPENDS ${VISP_BINARY_DIR}/doc/man/man1/visp-config.1.gz
  )
  INSTALL(FILES
    ${VISP_BINARY_DIR}/doc/man/man1/visp-config.1.gz
    DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/man/man1
    PERMISSIONS OWNER_READ GROUP_READ WORLD_READ OWNER_WRITE
    COMPONENT dev
  )
  MARK_AS_ADVANCED(GZIP)
endif()

#----------------------------------------------------------------------
# Modules
#----------------------------------------------------------------------
include(cmake/VISPModule.cmake)

# process subdirectories
add_subdirectory(modules)

#-----------------------------------------------------------------------------
# Add extra compilation flags under UNIX
#-----------------------------------------------------------------------------
include(cmake/AddExtraCompilationFlags.cmake) # should be called after FindOpenMP
vp_add_extra_compilation_flags()

#-------------------------------------------------------------------------------
# specific things that need to be updated in vpConfig.h and ViSP-third-party.txt
#-------------------------------------------------------------------------------
VP_SET(VISP_HAVE_OPENMP      TRUE IF USE_OPENMP)
VP_SET(VISP_HAVE_OPENCV      TRUE IF (BUILD_MODULE_visp_core AND USE_OPENCV))
VP_SET(VISP_HAVE_X11         TRUE IF (BUILD_MODULE_visp_core AND USE_X11))
VP_SET(VISP_HAVE_GTK         TRUE IF (BUILD_MODULE_visp_core AND USE_GTK2))
VP_SET(VISP_HAVE_GDI         TRUE IF (BUILD_MODULE_visp_core AND USE_GDI))
VP_SET(VISP_HAVE_D3D9        TRUE IF (BUILD_MODULE_visp_core AND USE_DIRECT3D))
VP_SET(VISP_HAVE_JPEG        TRUE IF (BUILD_MODULE_visp_core AND USE_JPEG))
VP_SET(VISP_HAVE_PNG         TRUE IF (BUILD_MODULE_visp_core AND USE_PNG))
VP_SET(VISP_HAVE_YARP        TRUE IF (BUILD_MODULE_visp_core AND USE_YARP))
VP_SET(VISP_HAVE_GSL         TRUE IF (BUILD_MODULE_visp_core AND USE_GSL))
VP_SET(VISP_HAVE_LAPACK_C    TRUE IF (BUILD_MODULE_visp_core AND USE_LAPACK))
VP_SET(VISP_HAVE_PTHREAD     TRUE IF (BUILD_MODULE_visp_core AND USE_PTHREAD))
VP_SET(VISP_HAVE_XML2        TRUE IF (BUILD_MODULE_visp_core AND USE_XML2))
VP_SET(VISP_HAVE_FFMPEG      TRUE IF (BUILD_MODULE_visp_core AND USE_FFMPEG))

VP_SET(VISP_HAVE_OGRE        TRUE IF (BUILD_MODULE_visp_ar AND USE_OGRE))
VP_SET(VISP_HAVE_OIS         TRUE IF (BUILD_MODULE_visp_ar AND USE_OIS))
VP_SET(VISP_HAVE_COIN3D      TRUE IF (BUILD_MODULE_visp_ar AND USE_COIN3D))
VP_SET(VISP_HAVE_SOWIN       TRUE IF (BUILD_MODULE_visp_ar AND USE_SOWIN))
VP_SET(VISP_HAVE_SOXT        TRUE IF (BUILD_MODULE_visp_ar AND USE_SOXT))
VP_SET(VISP_HAVE_SOQT        TRUE IF (BUILD_MODULE_visp_ar AND USE_SOQT))
VP_SET(VISP_HAVE_QT          TRUE IF (BUILD_MODULE_visp_ar AND USE_QT))

VP_SET(VISP_HAVE_ZBAR        TRUE IF (BUILD_MODULE_visp_detection AND USE_ZBAR))
VP_SET(VISP_HAVE_DMTX        TRUE IF (BUILD_MODULE_visp_detection AND USE_DMTX))

VP_SET(VISP_HAVE_AFMA4       TRUE IF (BUILD_MODULE_visp_robot AND USE_AFMA4))
VP_SET(VISP_HAVE_AFMA6       TRUE IF (BUILD_MODULE_visp_robot AND USE_AFMA6))
VP_SET(VISP_HAVE_VIPER650    TRUE IF (BUILD_MODULE_visp_robot AND USE_VIPER650))
VP_SET(VISP_HAVE_VIPER850    TRUE IF (BUILD_MODULE_visp_robot AND USE_VIPER850))
VP_SET(VISP_HAVE_BICLOPS     TRUE IF (BUILD_MODULE_visp_robot AND USE_BICLOPS))
VP_SET(VISP_HAVE_PTU46       TRUE IF (BUILD_MODULE_visp_robot AND USE_PTU46))
#VP_SET(VISP_HAVE_PIONEER     TRUE IF (BUILD_MODULE_visp_robot AND USE_ARIA))
if(BUILD_MODULE_visp_robot AND USE_ARIA)
  if(UNIX AND USE_PTHREAD AND RT_FOUND AND DL_FOUND)
    set(VISP_HAVE_PIONEER TRUE)
  elseif(NOT UNIX)
    set(VISP_HAVE_PIONEER TRUE)
  endif()
endif()
VP_SET(VISP_HAVE_COIN3D      TRUE IF (BUILD_MODULE_visp_robot AND USE_COIN3D))

VP_SET(VISP_HAVE_V4L2        TRUE IF (BUILD_MODULE_visp_sensor AND USE_V4L2))
VP_SET(VISP_HAVE_DC1394      TRUE IF (BUILD_MODULE_visp_sensor AND USE_DC1394))
VP_SET(VISP_HAVE_CMU1394     TRUE IF (BUILD_MODULE_visp_sensor AND USE_CMU1394))
VP_SET(VISP_HAVE_DIRECTSHOW  TRUE IF (BUILD_MODULE_visp_sensor AND USE_DIRECTSHOW))
VP_SET(VISP_HAVE_LIBFREENECT TRUE IF (BUILD_MODULE_visp_sensor AND USE_LIBFREENECT))
VP_SET(VISP_HAVE_LIBUSB_1    TRUE IF (BUILD_MODULE_visp_sensor AND USE_LIBUSB_1))

#VP_SET(VISP_HAVE_COIN3D      TRUE IF (BUILD_MODULE_visp_mbt AND USE_COIN3D))


VP_SET(VISP_BUILD_SHARED_LIBS TRUE IF BUILD_SHARED_LIBS) # for header vpConfig.h
VP_SET(VISP_HAVE_DC1394_CAMERA_ENUMERATE TRUE IF (USE_DC1394 AND DC1394_CAMERA_ENUMERATE_FOUND)) # for header vpConfig.h
VP_SET(VISP_HAVE_DC1394_FIND_CAMERAS     TRUE IF (USE_DC1394 AND DC1394_FIND_CAMERAS_FOUND)) # for header vpConfig.h
VP_SET(VISP_HAVE_D3D9     TRUE IF USE_DIRECT3D) # for header vpConfig.h
VP_SET(VISP_HAVE_GTK     TRUE IF USE_GTK2) # for header vpConfig.h

# Check if libfreenect dependencies (ie libusb-1.0 and libpthread) are available
if(USE_LIBFREENECT AND USE_LIBUSB_1 AND USE_PTHREAD)
  if(LIBFREENECT_FOUND AND LIBUSB_1_FOUND AND PTHREAD_FOUND)
    set(VISP_HAVE_LIBFREENECT_AND_DEPENDENCIES TRUE)
    set(VISP_HAVE_LIBFREENECT_AND_DEPENDENCIES_FOUND "yes")  # for ViSP-third-party.txt

    # The material is found. Check if libfreenect is an old version
    include(CheckCXXSourceCompiles)
    set(CMAKE_REQUIRED_LIBRARIES ${LIBFREENECT_LIBRARIES} ${PTHREAD_LIBRARIES} ${LIBUSB_1_LIBRARIES})
    set(CMAKE_REQUIRED_INCLUDES ${LIBFREENECT_INCLUDE_DIRS} ${PTHREAD_INCLUDE_DIRS} ${LIBUSB_1_INCLUDE_DIRS})
    CHECK_CXX_SOURCE_COMPILES("
      #include <libfreenect.hpp>

      class vpMyKinect : public Freenect::FreenectDevice
      {
      };

      int main()
      {
        Freenect::Freenect<vpMyKinect> freenect;
      }
      " LIBFREENECT_IS_OLD_VERSION)
    #MESSAGE("LIBFREENECT_IS_OLD_VERSION: ${LIBFREENECT_IS_OLD_VERSION}")
    if(LIBFREENECT_IS_OLD_VERSION)
      set(VISP_HAVE_LIBFREENECT_OLD TRUE)  # for header vpConfig.h
    else()
      set(VISP_HAVE_LIBFREENECT_OLD FALSE) # for header vpConfig.h
    endif()
  endif()
endif()

# check OpenCV nonfree modules and version
if(USE_OPENCV)
  set(VISP_HAVE_OPENCV_VERSION "(${OpenCV_VERSION_MAJOR}*65536 + ${OpenCV_VERSION_MINOR}*256 + ${OpenCV_VERSION_PATCH})") # for vpConfig.h
  if(OpenCV_VERSION)
    if(OpenCV_VERSION VERSION_LESS "2.4.0")
      message(STATUS "opencv nonfree module found")
       set(VISP_HAVE_OPENCV_NONFREE TRUE)  # for header vpConfig.h
    elseif(OPENCV_NONFREE_FOUND) # OpenCV < 3.0.0
      message(STATUS "opencv xfeatures2d module found")
      set(VISP_HAVE_OPENCV_NONFREE TRUE)  # for header vpConfig.h
    elseif(OPENCV_XFEATURES2D_FOUND) # OpenCV >= 3.0.0
      set(VISP_HAVE_OPENCV_XFEATURES2D TRUE)  # for header vpConfig.h
    else()
      message(STATUS "opencv nonfree or xfeature2d module not found")
    endif()
  else()
    message(STATUS "opencv nonfree not found")
    set(VISP_HAVE_OPENCV_VERSION "(0)") # for vpConfig.h
  endif()
endif()

# coin and gui
set(VISP_HAVE_COIN3D_AND_GUI_FOUND "no")     # for ViSP-third-party.txt
set(VISP_HAVE_QT3_FOUND "no")              # for ViSP-third-party.txt
set(VISP_HAVE_QT4_FOUND "no")              # for ViSP-third-party.txt
if(USE_SOWIN)
  set(VISP_HAVE_COIN3D_AND_GUI TRUE)         # for header vpConfig.h
  set(VISP_HAVE_COIN3D_AND_GUI_FOUND "yes")  # for ViSP-third-party.txt
elseif(USE_SOXT)
  set(VISP_HAVE_COIN3D_AND_GUI TRUE)         # for header vpConfig.h
  set(VISP_HAVE_COIN3D_AND_GUI_FOUND "yes")  # for ViSP-third-party.txt
elseif(USE_SOQT AND USE_QT)
  set(VISP_HAVE_COIN3D_AND_GUI TRUE)         # for header vpConfig.h
  set(VISP_HAVE_COIN3D_AND_GUI_FOUND "yes")  # for ViSP-third-party.txt
  if(DESIRED_QT_VERSION MATCHES 3)
    set(VISP_HAVE_QT3_FOUND "yes")         # for ViSP-third-party.txt
  elseif(DESIRED_QT_VERSION MATCHES 4)
    set(VISP_HAVE_QT4_FOUND "yes")         # for ViSP-third-party.txt
  endif()
endif()

# Ogre plugins and resources
if(USE_OGRE)
  include(cmake/OgreTools.cmake)
  vp_set_ogre_media()
endif()

# Find isnan macro (C-style)
VP_SET(VISP_HAVE_FUNC_ISNAN TRUE IF HAVE_FUNC_ISNAN) # for header vpConfig.h
# Find std::isnan function (cmath)
VP_SET(VISP_HAVE_FUNC_STD_ISNAN TRUE IF HAVE_FUNC_STD_ISNAN) # for header vpConfig.h
# Find _isnan function for MSVC
VP_SET(VISP_HAVE_FUNC__ISNAN TRUE IF HAVE_FUNC__ISNAN) # for header vpConfig.h
# Find isinf macro (C-style)
VP_SET(VISP_HAVE_FUNC_ISINF TRUE IF HAVE_FUNC_ISINF) # for header vpConfig.h
# Find std::isinf function (cmath)
VP_SET(VISP_HAVE_FUNC_STD_ISINF TRUE IF HAVE_FUNC_STD_ISINF) # for header vpConfig.h
# Find _finite function for MSVC
VP_SET(VISP_HAVE_FUNC__FINITE TRUE IF HAVE_FUNC__FINITE) # for header vpConfig.h
# Find round function (math.h)
VP_SET(VISP_HAVE_FUNC_ROUND TRUE IF HAVE_FUNC_ROUND) # for header vpConfig.h
# Find std::round function (cmath)
VP_SET(VISP_HAVE_FUNC_STD_ROUND TRUE IF HAVE_FUNC_STD_ROUND) # for header vpConfig.h

VP_SET(VISP_HAVE_ACCESS_TO_NAS TRUE IF NAS_FOUND) # for header vpConfig.h
VP_SET(VISP_BUILD_DEPRECATED_FUNCTIONS TRUE IF BUILD_DEPRECATED_FUNCTIONS) # for header vpConfig.h
VP_SET(MOMENTS_COMBINE_MATRICES TRUE IF VISP_MOMENTS_COMBINE_MATRICES) # for header vpConfig.h
VP_SET(VISP_USE_MSVC TRUE IF MSVC) # for header vpConfig.h
VP_SET(VISP_HAVE_CPP11_COMPATIBILITY TRUE IF USE_CPP11) # for header vpConfig.h
VP_SET(VISP_HAVE_BICLOPS_AND_GET_HOMED_STATE_FUNCTION TRUE IF (USE_BICLOPS AND BICLOPS_HAVE_GET_HOMED_STATE_FUNCTION)) # for header vpConfig.h

if(USE_CPP11)
  set(VISP_HAVE_CPP11_COMPATIBILITY_FOUND "yes")  # for ViSP-third-party.txt
else()
  set(VISP_HAVE_CPP11_COMPATIBILITY_FOUND "no")  # for ViSP-third-party.txt
endif()

# libraries for Pioneer mobile robots
if(USE_ARIA AND UNIX)
  if(ARIA_FOUND AND USE_PTHREAD AND RT_FOUND AND DL_FOUND)
    set(VISP_HAVE_PIONEER TRUE) # for header vpConfig.h
    set(VISP_HAVE_PIONEER_FOUND "yes")  # for ViSP-third-party.txt
  endif()
elseif(USE_ARIA AND NOT UNIX)
  set(VISP_HAVE_PIONEER TRUE) # for header vpConfig.h
  set(VISP_HAVE_PIONEER_FOUND "yes")  # for ViSP-third-party.txt
else()
  set(VISP_HAVE_PIONEER_FOUND "no")  # for ViSP-third-party.txt
endif()

if(USE_AFMA4)
  set(VISP_HAVE_AFMA4_FOUND "yes")  # for ViSP-third-party.txt
else()
  set(VISP_HAVE_AFMA4_FOUND "no")  # for ViSP-third-party.txt
endif()
if(USE_AFMA6)
  set(VISP_HAVE_AFMA6_FOUND "yes")  # for ViSP-third-party.txt
else()
  set(VISP_HAVE_AFMA6_FOUND "no")  # for ViSP-third-party.txt
endif()
if(USE_VIPER650)
  set(VISP_HAVE_VIPER650_FOUND "yes")  # for ViSP-third-party.txt
else()
  set(VISP_HAVE_VIPER650_FOUND "no")  # for ViSP-third-party.txt
endif()
if(USE_VIPER850)
  set(VISP_HAVE_VIPER850_FOUND "yes")  # for ViSP-third-party.txt
else()
  set(VISP_HAVE_VIPER850_FOUND "no")  # for ViSP-third-party.txt
endif()

# ----------------------------------------------------------------------------
# Finalization: generate configuration-based files
# ----------------------------------------------------------------------------

# Generate platform-dependent and configuration-dependent headers
include(cmake/VISPGenerateHeaders.cmake)

# Configure the file describing how to use ViSP. VISPConfig.cmake
# is the main file configuring a CMake package.
# . Exports build settings and dependencies for projects using ViSP as a
#   third party project.
# . Create and install files for simple use of find_package(VISP)
#   by other cmakified "user" projects and libraries depending on ViSP.
#   (see "Mastering CMake", pp.72)
# . To use ViSP in a third party project based on CMake:
#   find_package(VISP REQUIRED)
#   include_directories(${VISP_INCLUDE_DIRS})
#   target_link_libraries(<target> ${VISP_LIBRARIES})
include(cmake/VISPGenerateConfig.cmake)

#----------------------------------------------------------------------
# For Dart server and tests
# We use CDash set through CTestConfig.cmake file
# Dashboards are sent to http://cdash.irisa.fr/CDash/index.php?project=ViSP
#----------------------------------------------------------------------
if(BUILD_TESTS)
  include(CTest)
endif()

#----------------------------------------------------------------------
# For CPack packaging tool
#----------------------------------------------------------------------

option(BUILD_PACKAGE "Configure ViSP packaging" OFF)
if(BUILD_PACKAGE)
  if(UNIX AND NOT APPLE AND NOT WIN32) # =linux
    option(BUILD_PACKAGE_DEBIAN "Build debian package" ON)
    option(BUILD_PACKAGE_RPM "Build rpm package" ON)
  endif()

  include(cmake/CPackConfig.cmake)
endif(BUILD_PACKAGE)


#----------------------------------------------------------------------
# Generate the package dependent visp-config shell script for projects which
# are not using CMake:
# Usage:
#    visp-config --cflags ...
#----------------------------------------------------------------------
include(cmake/VISPGeneratePkgConfigScript.cmake)

#----------------------------------------------------------------------
# Propagation in sub dirs to build demo, example, test, tutorial
#----------------------------------------------------------------------

set(VISP_DIR ${PROJECT_BINARY_DIR})
mark_as_advanced(VISP_DIR)
mark_as_advanced(VISP_INCLUDE_DIRS)

if(BUILD_DEMOS)
  add_subdirectory(demo)
endif()
if(BUILD_EXAMPLES)
  add_subdirectory(example)
endif()
if(BUILD_TUTORIALS)
  add_subdirectory(tutorial)
endif()
