cmake_minimum_required(VERSION 2.8)
project(VITE CXX C)

### The current version number
set (VITE_VERSION_MAJOR 1)
set (VITE_VERSION_MINOR 2)
set (VITE_VERSION_PATCH 0)

if(COMMAND CMAKE_POLICY)
  # CMP0003: add the link paths to the link command as with cmake 2.4
  cmake_policy(SET CMP0003 NEW)
endif(COMMAND CMAKE_POLICY)

### Misc options
option(BUILD_SHARED_LIBS
  "Build shared libraries" OFF)
option(BUILD_64bits
  "Build 64 bits mode" ON)
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build, options are None, Debug, Release, RelWithDebInfo and MinSizeRel." FORCE)
endif(NOT CMAKE_BUILD_TYPE)

# cmake modules setup
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/")
include (CMakeDetermineSystem)
include (CheckCCompilerFlag)
include (CheckFunctionExists)
include (CheckSymbolExists)
include (CheckIncludeFiles)
include (CheckLibraryExists)
include (CMakeDependentOption)

### Required Packages
# Try to find Qt5 : http://forum.openscenegraph.org/viewtopic.php?t=11431
set(USE_QT5 OFF)
# QUIET option disables messages if the package cannot be found.
find_package(Qt5Widgets QUIET)
if(Qt5Widgets_FOUND)
# CMake 2.8.8 or greater required for qt5 use
 if("${CMAKE_VERSION}" VERSION_GREATER 2.8.7)
   set(USE_QT5 ON)
 endif()
endif()

if(USE_QT5)
  set(CMAKE_AUTOMOC ON)
  find_package(Qt5 COMPONENTS Core Xml Widgets OpenGL UiTools REQUIRED)
else(USE_QT5) # Try Qt4
  find_package(Qt4 COMPONENTS QtCore QtGui QtXml REQUIRED)
  set(QT_USE_QTUITOOLS ON)
  set(QT_USE_QTOPENGL ON)
  include(${QT_USE_FILE})
endif(USE_QT5)

#find_package(OpenGL)
find_package(GLU)

### Optimization options
option(VITE_ENABLE_SERIALIZATION
  "Enable the support of Boost Serialization." OFF)
option(VITE_ENABLE_VBO
  "Enable the support of VBO." OFF)
option(VITE_ENABLE_MT_PARSERS
  "Enable multi-threading in parsers." OFF)

### Debug options
option(VITE_DBG_MEMORY_USAGE
  "Enable statistic on memory usage." OFF)
cmake_dependent_option(VITE_DBG_MEMORY_TRACE
  "Enable trace generation of memory usage (requires MEMORY_USAGE)." OFF "VITE_DBG_MEMORY_USAGE" OFF)

### Trace format options
option(VITE_ENABLE_OTF
  "Enable the support of OTF file format." OFF)
option(VITE_ENABLE_TAU
  "Enable the support of TAU file format." OFF)

# timeval, timespec, realtime clocks, etc
include(CheckStructHasMember)
check_struct_has_member("struct timespec" tv_nsec time.h HAVE_TIMESPEC_TV_NSEC)
if( NOT HAVE_TIMESPEC_TV_NSEC )
  add_definitions(-D_GNU_SOURCE)
  check_struct_has_member("struct timespec" tv_nsec time.h HAVE_TIMESPEC_TV_NSEC)
endif( NOT HAVE_TIMESPEC_TV_NSEC )
check_library_exists(rt clock_gettime "" HAVE_CLOCK_GETTIME)
if( HAVE_CLOCK_GETTIME )
  set(EXTRA_LIBS "${EXTRA_LIBS};rt")
endif( HAVE_CLOCK_GETTIME )

# stdlib, stdio, string, getopt, etc
check_include_files(stdarg.h HAVE_STDARG_H)
check_function_exists(va_copy HAVE_VA_COPY)
if (NOT HAVE_VA_COPY)
  check_function_exists(__va_copy HAVE_UNDERSCORE_VA_COPY)
endif (NOT HAVE_VA_COPY)
check_function_exists(asprintf HAVE_ASPRINTF)
check_function_exists(vasprintf HAVE_VASPRINTF)
check_include_files(getopt.h HAVE_GETOPT_H)
check_include_files(unistd.h HAVE_UNISTD_H)
check_function_exists(getopt_long HAVE_GETOPT_LONG)
check_include_files(errno.h HAVE_ERRNO_H)
check_include_files(stddef.h HAVE_STDDEF_H)
check_function_exists(getrusage HAVE_GETRUSAGE)
check_include_files(limits.h HAVE_LIMITS_H)
check_include_files(string.h HAVE_STRING_H)

#
# Find optional packages
#

# Search for Boost
#    Components : filesystem, iostreams, programoptions, python, regex,
#                 serialization, signals, system, thread, wave
if(VITE_ENABLE_SERIALIZATION)
  find_package( Boost COMPONENTS serialization thread iostreams system)
endif(VITE_ENABLE_SERIALIZATION)

if(VITE_ENABLE_VBO)
  find_package(GLEW)
endif(VITE_ENABLE_VBO)

if(VITE_ENABLE_OTF)
  find_package(OTF)
endif(VITE_ENABLE_OTF)

if(VITE_ENABLE_TAU)
  find_package(TAU)
endif(VITE_ENABLE_TAU)

if(VITE_ENABLE_MT_PARSERS)
  add_definitions(-DMT_PARSING)
endif()

if(VITE_DBG_MEMORY_USAGE)
  add_definitions(-DMEMORY_USAGE)
endif()

if(VITE_DBG_MEMORY_TRACE)
  add_definitions(-DMEMORY_TRACE)
endif()

# Configuration header
configure_file (
  "${CMAKE_CURRENT_SOURCE_DIR}/src/common/ViteConfig.hpp.in"
  "${CMAKE_CURRENT_SOURCE_DIR}/src/common/ViteConfig.hpp")

add_subdirectory(src)
add_subdirectory(plugins)

include(CPackLists.txt)
