#################################################
#   ___________    ____ ______  __ __  _____    #
#   \____ \__  \ _/ ___\\____ \|  |  \/  ___/   #
#   |  |_> > __ \\  \___|  |_> >  |  /\___ \    #
#   |   __(____  /\___  >   __/|____//____  >   #
#   |__|       \/     \/|__|              \/    #
#                                               #
#################################################

project(Pacpus)
cmake_minimum_required(VERSION 2.8)

if(POLICY CMP0020)
    cmake_policy(SET CMP0020 NEW) # for QT5.1 on windows / not work on linux
endif()
if(POLICY CMP0026)
    # allow using LOCATION_DEBUG in cmake/PacpusUtilities.cmake
    cmake_policy(SET CMP0026 OLD)
endif()

################################################################################
# Pacpus version
set(${PROJECT_NAME}_MAJOR_VERSION 0)
set(${PROJECT_NAME}_MINOR_VERSION 2)
set(${PROJECT_NAME}_PATCH_VERSION 3)
set(${PROJECT_NAME}_VERSION
    "${${PROJECT_NAME}_MAJOR_VERSION}.${${PROJECT_NAME}_MINOR_VERSION}.${${PROJECT_NAME}_PATCH_VERSION}")

################################################################################
# CMake variables
set(PACPUS_ROOT_DIR         ${CMAKE_SOURCE_DIR})
set(PACPUS_3RD_PARTY_DIR    ${PACPUS_ROOT_DIR}/3rd)
set(PACPUS_CMAKE_DIR        ${PACPUS_ROOT_DIR}/cmake)
set(PACPUS_DOC_DIR          ${PACPUS_ROOT_DIR}/doc)
set(PACPUS_EXAMPLES_DIR     ${PACPUS_ROOT_DIR}/examples)
set(PACPUS_INCLUDE_DIR      ${PACPUS_ROOT_DIR}/include)
set(PACPUS_SCRIPTS_DIR      ${PACPUS_ROOT_DIR}/scripts)
set(PACPUS_SOURCE_DIR       ${PACPUS_ROOT_DIR}/src)
set(PACPUS_TEST_DIR       ${PACPUS_ROOT_DIR}/test)
set(IS_BUILDING_PACPUS      TRUE)

set(PACPUS_DEFINITIONS "")

################################################################################
set(PACPUS_BUILD_TESTS TRUE CACHE BOOL "Whether build the tests")

################################################################################
# Configure CMake
list(APPEND CMAKE_MODULE_PATH ${PACPUS_CMAKE_DIR})

################################################################################
# Import some functions
include(${PACPUS_CMAKE_DIR}/PacpusUtilities.cmake)

################################################################################
# Configure Pacpus
include(${PACPUS_CMAKE_DIR}/PacpusConfiguration.cmake)

################################################################################
# Find dependencies
include(${PACPUS_CMAKE_DIR}/PacpusDependencies.cmake)

################################################################################
# Handle platform-specific behaviour
include(${PACPUS_CMAKE_DIR}/PacpusPlatforms.cmake)

################################################################################
# Compiler flags coming from PacpusDependencies and PacpusPlatforms
message(STATUS "\${PACPUS_DEFINITIONS} = ${PACPUS_DEFINITIONS}")
message(STATUS "\${PACPUS_DEPENDENCIES_INC} = ${PACPUS_DEPENDENCIES_INC}")
message(STATUS "\${PACPUS_DEPENDENCIES_LIB} = ${PACPUS_DEPENDENCIES_LIB}")
add_definitions(${PACPUS_DEFINITIONS})

################################################################################
# Includes directories
include_directories(
    ${PACPUS_DEPENDENCIES_INC}
    ${PACPUS_INCLUDE_DIR}
)

################################################################################
# Build the Pacpus' modules
add_subdirectory(${PACPUS_SOURCE_DIR})

################################################################################
# DOCUMENTATION
if(PACPUS_BUILD_DOC)
    add_subdirectory(${PACPUS_DOC_DIR})
endif()

################################################################################
# Pacpus Installation
include(${PACPUS_CMAKE_DIR}/PacpusInstall.cmake)
set(ENV{PACPUS_ROOT} ${PACPUS_INSTALL_DIR})

################################################################################
# EXAMPLES
if(PACPUS_BUILD_EXAMPLES)
    add_subdirectory(${PACPUS_EXAMPLES_DIR})
endif()

################################################################################
# TESTS
if(${PACPUS_BUILD_TESTS})
    enable_testing()
    #add_subdirectory(${PACPUS_TEST_DIR})
endif()

################################################################################
# SUMMARY
pacpus_info("")
pacpus_info("=====================================================================")
pacpus_info("Configuration for Pacpus ${${PROJECT_NAME}_VERSION}")
pacpus_info("=====================================================================")
pacpus_info("")
pacpus_info("  Platform:")
pacpus_info("    Host:" ${CMAKE_HOST_SYSTEM_NAME} ${CMAKE_HOST_SYSTEM_VERSION} ${CMAKE_HOST_SYSTEM_PROCESSOR})
if(CMAKE_CROSS_COMPILING)
    pacpus_info("    Target:" ${CMAKE_SYSTEM_NAME} ${CMAKE_SYSTEM_VERSION} ${CMAKE_SYSTEM_PROCESSOR})
endif()
pacpus_info("    CMake:" ${CMAKE_VERSION})
pacpus_info("    CMake generator:" ${CMAKE_GENERATOR})
pacpus_info("    CMake build tool:" ${CMAKE_BUILD_TOOL})
if(MSVC)
    pacpus_info("    MSVC:" ${MSVC_VERSION})
else()
    pacpus_info("    Configuration:" ${CMAKE_BUILD_TYPE})
endif()
pacpus_info("")
pacpus_info("  Compiler:")
pacpus_info("    C++ Compiler:" ${CMAKE_CXX_COMPILER})
pacpus_info("    C++ flags (release):" ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE})
pacpus_info("    C++ flags (debug):" ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG})
pacpus_info("")
pacpus_info("  Result:")
pacpus_info("    Install directory:" ${PACPUS_INSTALL_DIR})
pacpus_info("    Install with version number:" ${PACPUS_INSTALL_WITH_VERSION_NUMBER})
pacpus_info("")
pacpus_info("  Options:")
pacpus_info("    Logging enabled:" ${PACPUS_USE_LOG})
pacpus_info("    Colored logging enabled:" ${PACPUS_LOG_COLORED_OUTPUT})
pacpus_info("    Installation of 3rd party:" ${PACPUS_INSTALL_3RD})
pacpus_info("    Documentation enabled:" ${PACPUS_BUILD_DOC})
pacpus_info("")
if("${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
    message(WARNING "The source directory is the same as build directory. \"make clean\" may damage the source tree")
endif()
