##
## This file is part of haec_sim (https://tu-dresden.de/zih/haec_sim)
## haec_sim - A trace-based parallel discrete event simulation framework
##
## Copyright (c) 2013-2015 Technische Universitaet Dresden, Germany
##
## haec_sim is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## haec_sim is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with haec_sim.  If not, see <http://www.gnu.org/licenses/>.
##

project(haec_sim)

cmake_minimum_required(VERSION 2.8)

# build HAEC_SIM_VERSION
file(STRINGS VERSION HAEC_SIM_VERSION)
string(REPLACE "." ";" VERSION_LIST ${HAEC_SIM_VERSION})
list(GET VERSION_LIST 0 HAEC_SIM_VERSION_MAJOR)
list(GET VERSION_LIST 1 HAEC_SIM_VERSION_MINOR)
list(GET VERSION_LIST 2 HAEC_SIM_VERSION_PATCH)
add_definitions("-DHAEC_SIM_VERSION=\"${HAEC_SIM_VERSION}\"")


# CTest for running tests
include(CTest)

# Set a default build type if none was specified
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    message(STATUS "Setting build type to 'Debug' as none was specified.")
    set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build." FORCE)

    # Set the possible values of build type for cmake-gui
    set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
        "MinSizeRel" "RelWithDebInfo")
endif()

# setup Boost
find_package(Boost 1.56.0 COMPONENTS mpi filesystem system serialization program_options REQUIRED)
include_directories(SYSTEM ${Boost_INCLUDE_DIRS})

# override for taurus install
OPTION(BOOST_WORKAROUND "Enable build workaround for particular installations (e.g., taurus)" OFF)
if(BOOST_WORKAROUND)
    list(INSERT Boost_LIBRARIES 0 "-L${Boost_LIBRARY_DIR}")
endif()

# setup MPI
find_package(MPI REQUIRED)
include_directories(SYSTEM ${MPI_INCLUDE_PATH})

# find libOTF2
include(cmake/FindOTF2.cmake)
if(NOT ${OTF2_FOUND})
    message(SEND_ERROR "OTF2 library is required but could not be found.")
endif()

include_directories(SYSTEM ${OTF2_INCLUDE_DIRS})

# include path
include_directories("${CMAKE_SOURCE_DIR}/include")

# handle RPATH correctly
# 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}/lib")
# 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}/lib" isSystemDir)
IF("${isSystemDir}" STREQUAL "-1")
   SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
ENDIF("${isSystemDir}" STREQUAL "-1")

# enable c++11
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -pedantic")

# set additional flags for debug build
SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")

# load common cmake
include(cmake/common.cmake)

# adding resource managers
include_directories(resource_managers)
add_subdirectory(resource_managers)

# adding modules
add_subdirectory(modules)

# adding libraries
add_subdirectory(lib)

# adding executables
add_subdirectory(main)

# adding tests
add_subdirectory(tests)

# adding tests
add_subdirectory(scripts)

# adding utils
add_subdirectory(utils)

# hack for XCode
IF(CMAKE_GENERATOR MATCHES "Xcode")
    add_library(Sources EXCLUDE_FROM_ALL ./include ./main ./etc ./share ./modules ./tests ./lib)
endif()

# add a target to generate API documentation with Doxygen
find_package(Doxygen)
if(DOXYGEN_FOUND)

    find_program(QTHELPGENERATOR_EXECUTABLE qhelpgenerator PATHS ENV PATH)

    if(QTHELPGENERATOR_EXECUTABLE)
        set(DOXYGEN_GENERATE_QCH "TRUE")

        message(STATUS "Doxygen generate .qch file")
    else()
        set(DOXYGEN_GENERATE_QCH "FALSE")
        set(QTHELPGENERATOR_EXECUTABLE "")
    endif()

    configure_file(${CMAKE_CURRENT_SOURCE_DIR}/share/doc/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)

    option(BUILD_DOC "Always build documention with normal build" OFF)

    if(BUILD_DOC)
        message(STATUS "Doxygen found. Will generate the documention.")
        add_custom_target(doc
            ALL
            ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
            COMMENT "Generating API documentation with Doxygen" VERBATIM
        )
    else()
        message(STATUS "Doxygen found. To generate the documention set BUILD_DOC to ON or use 'make doc'")

        add_custom_target(doc
            ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
            COMMENT "Generating API documentation with Doxygen" VERBATIM
        )
    endif()
else()
    message(STATUS "Doxygen NOT found. You will not be able to generate the documentation.")
endif(DOXYGEN_FOUND)

INCLUDE(InstallRequiredSystemLibraries)

# package configuration
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "HAEC-SIM")
SET(CPACK_PACKAGE_VENDOR "TU Dresden, Center for Information Services and High Performance Computing (ZIH)")
SET(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README")
SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
SET(CPACK_PACKAGE_VERSION_MAJOR ${HAEC_SIM_VERSION_MAJOR})
SET(CPACK_PACKAGE_VERSION_MINOR ${HAEC_SIM_VERSION_MINOR})
SET(CPACK_PACKAGE_VERSION_PATCH ${HAEC_SIM_VERSION_PATCH})
SET(CPACK_PACKAGE_INSTALL_DIRECTORY "haec_sim ${HAEC_SIM_VERSION_MAJOR}.${HAEC_SIM_VERSION_MINOR}")
SET(CPACK_STRIP_FILES "bin/haec_sim:scripts/haecsimcc.sh:scripts/haecsimrun.sh")
SET(CPACK_SOURCE_STRIP_FILES "")
SET(CPACK_PACKAGE_EXECUTABLES "haec_sim:haecsimcc.sh:haecsimrun.sh")

SET(CPACK_SOURCE_PACKAGE_FILE_NAME "haec_sim-${HAEC_SIM_VERSION_MAJOR}.${HAEC_SIM_VERSION_MINOR}.${HAEC_SIM_VERSION_PATCH}")
SET(CPACK_SOURCE_GENERATOR "TBZ2;ZIP")
set(CPACK_SOURCE_IGNORE_FILES
"~$"
"\\\\.gitignore$"
"\\\\.clang-format$"
"\\\\bootstrap.sh$"
"\\\\KEDB$"
"\\\\TODO"
".*\\\\/\\\\..*|^\\\\..*"
"^${PROJECT_SOURCE_DIR}/build*"
"^${PROJECT_SOURCE_DIR}/BTS_scripts/"
"^${PROJECT_SOURCE_DIR}/jenkins/"
"^${PROJECT_SOURCE_DIR}/examples/"
)
# Use cpack from cmake
INCLUDE(CPack)

# New rule: "make dist"
add_custom_target(dist COMMAND ${CMAKE_MAKE_PROGRAM} package_source)
