#
# author: Nicolas Van der Noot
#
# Visualization project for Robotran, with OpenGL
#

# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                       PROJECT MAIN CONFIGURATIONS
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


# CMake minimum version
cmake_minimum_required(VERSION 2.8.12)

# project name
project (CtrlTool)

# project configuration
set( CMAKE_C_FLAGS_RELEASE   "-O3" )
set( CMAKE_CXX_FLAGS_RELEASE "-O3" )

# Variable for storing the path to Robotran common files (should be adapted depending on the location of those source)
if(NOT DEFINED ROBOTRAN_SOURCE_DIR)
    set(TRIAL_PATHS_MBSYSC
        /home/nico/Documents/Robotran/Projects/robotran/mbsysc/MBsysC    # Nico ThinkPad Linux
        /data/nvander/Documents/Robotran/Projects/robotran/mbsysc/MBsysC # Nico BioRob Linux
        C:/Users/Nico/Documents/Robotran/Projects/mbsysc/MBsysC          # Nico ThinkPad Windows
        /Users/MarieSophie/Documents/Robotran/Projects/mbsysc/MBsysC     # Mary Mac OS
        ${PROJECT_SOURCE_DIR}/../mbsysCopy # copy folder
        D:/Documents/Github/mbsysc/MBsysC      # phil asus
        /home/pgreiner/Documents/mbsysc/MBsysC # phil biorob linux
    )
    find_path(ROBOTRAN_SOURCE_DIR mbs_common ${TRIAL_PATHS_MBSYSC} DOC "Path to the Robotran-MBsysC common files")
endif ( )


# message to display the project name
message(STATUS "Processing ${PROJECT_NAME}")
message(STATUS "MBsysC path: ${ROBOTRAN_SOURCE_DIR}")

# for Unix: display all the warnings, except the ones related to /* -- */
if (UNIX)
  set(CMAKE_C_FLAGS "-g -Wall -Wno-comment")
endif (UNIX)

# copy dll files used for Windows
# these dll files are copied in the Executable directory (Debug or Release)
if (WIN32)

    # avoid warnings
    if(COMMAND cmake_policy)
        cmake_policy(SET CMP0054 NEW)
    endif()

    # Visual studio version
    if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
        if (${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 18.0)
            message("Visual Studio version set to VS2012 (${CMAKE_CXX_COMPILER_VERSION})")
            set(MSVC_VERSION lib-vc2012)
        elseif (${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 19.0)
            message("Visual Studio version set to VS2013 (${CMAKE_CXX_COMPILER_VERSION})")
            set(MSVC_VERSION lib-vc2013)
        else ( )
            message("Visual Studio version set to VS2015 (${CMAKE_CXX_COMPILER_VERSION})")
            set(MSVC_VERSION lib-vc2015)
        endif ( )
    else ( )
        message(FATAL_ERROR "CMake error: only Visual Studio projects can currently be used on Windows !")
    endif ( )

    ## --- WIN32 or WIN64 DETECTION --- ##
    if( CMAKE_SIZEOF_VOID_P EQUAL 8 )   # 64bit Windows
        SET(WIN_LIB_DIRECTORY win64_include_lib)
    else( )  # 32bit Windows
        SET(WIN_LIB_DIRECTORY win32_include_lib)
    endif( )

    ## ---- WINDOWS DLL FILES ---- ##

    # copy all the dll (except 'jvm.dll') used for Windows
    # these dll files are copied in the Executable directory (Debug or Release)
    
    # common dll
    file(COPY ${ROBOTRAN_SOURCE_DIR}/${WIN_LIB_DIRECTORY}/dll/ DESTINATION ${CMAKE_BINARY_DIR}/Debug)
    file(COPY ${ROBOTRAN_SOURCE_DIR}/${WIN_LIB_DIRECTORY}/dll/ DESTINATION ${CMAKE_BINARY_DIR}/Release)

    # GLFW dll
    file(COPY ${ROBOTRAN_SOURCE_DIR}/${WIN_LIB_DIRECTORY}/dll/GLFW/${MSVC_VERSION}/ DESTINATION ${CMAKE_BINARY_DIR}/Debug)
    file(COPY ${ROBOTRAN_SOURCE_DIR}/${WIN_LIB_DIRECTORY}/dll/GLFW/${MSVC_VERSION}/ DESTINATION ${CMAKE_BINARY_DIR}/Release)
endif ( )

# additional CMake functions
set(CMAKE_AUX ${ROBOTRAN_SOURCE_DIR}/cmake_aux)
set(CMAKE_AUX_BIN ${PROJECT_BINARY_DIR}/cmake_aux)

add_subdirectory ( ${CMAKE_AUX}/listing/   ${CMAKE_AUX_BIN}/listing/   )
add_subdirectory ( ${CMAKE_AUX}/libraries/ ${CMAKE_AUX_BIN}/libraries/ )

# Windows M_PI definitions
if (WIN32)
    add_definitions(-D _USE_MATH_DEFINES)
endif (WIN32)

# UNIX system
if (UNIX)
    add_definitions(-D UNIX)
endif (UNIX)

# project own definition
add_definitions(-D CTRL_TOOL)



# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                           USER ABSOLUTE PATHS
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

configure_file (
  "${ROBOTRAN_SOURCE_DIR}/conf/cmake_config.h.in"
  "${PROJECT_BINARY_DIR}/conf/cmake_config.h"
)

set(OPEN_GL_FILES_PATH ${ROBOTRAN_SOURCE_DIR}/mbs_common/mbs_realtime/open_gl/)

configure_file (
    "${ROBOTRAN_SOURCE_DIR}/mbs_common/mbs_realtime/open_gl/conf/cmake_open_gl_config.h.in"
    "${PROJECT_BINARY_DIR}/conf/cmake_open_gl_config.h"
)

# 'cmake_config.h.in' and 'cmake_open_gl_config.h.in' is in the 'conf' folder
include_directories (${PROJECT_BINARY_DIR}/conf)



# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                           EXTERNAL LIBRARIES
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

set(CMAKE_MODULE_PATH ${ROBOTRAN_SOURCE_DIR}/conf)

# OpenGL
open_gl_lib(main)


# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                           LIST FILES
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

 
# list source files to compile
init_src()

increment_src("${PROJECT_SOURCE_DIR}/src")


# list include directories (to find headers)
init_include()

increment_include("${PROJECT_SOURCE_DIR}/src")
increment_include("${PROJECT_SOURCE_DIR}/../parallel_simu/src")


# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                         CONFIGURE EXECUTABLE
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

# include these directories
include_directories (${INCLUDE_DIR})

# name of the executable
set (Executable Exec)

# generate the executable
add_executable (${Executable} ${SOURCE_FILES})

# link library: OpenGL
target_link_libraries ( ${Executable} ${OPEN_GL_LIBS})

# math external library (for Unix): linkage
if (UNIX)
    target_link_libraries (${Executable} m)
endif (UNIX)
