cmake_minimum_required(VERSION 3.0.2)
project(messaging-framework VERSION "0.1" LANGUAGES C CXX)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

# Select a pedantic set of compiler flags and enable C++11 support.
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-deprecated-register -Wno-extra-semi -Wno-gnu-zero-variadic-macro-arguments -fvisibility=hidden")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -fno-strict-aliasing -fvisibility=hidden -fvisibility-inlines-hidden -Wextra -Wno-deprecated-register -Wno-extra-semi -Wno-gnu-zero-variadic-macro-arguments")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Include the entire CTest functionality, specifically for MakeNightlyMemCheck.
include(CTest)
include(GNUInstallDirs)

find_package(Boost COMPONENTS filesystem program_options system REQUIRED)
find_package(GLog)
find_package(PkgConfig)
find_package(Qt5Core REQUIRED)
find_package(Qt5DBus REQUIRED)
find_package(Qt5Test REQUIRED)
find_package(Qt5Network REQUIRED)
find_package(Qt5Xml REQUIRED)
find_package(Threads)
find_package(LibPhoneNumber REQUIRED)
pkg_check_modules(DBUS_CPP dbus-cpp REQUIRED)
pkg_check_modules(PROCESS_CPP process-cpp REQUIRED)
pkg_check_modules(TP_QT5 REQUIRED TelepathyQt5)

find_package(Gtest REQUIRED)
include_directories(${GMOCK_INCLUDE_DIR} ${GTEST_INCLUDE_DIR})

# Enable handy automoc functionality.
add_definitions(-DQT_NO_KEYWORDS)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)

set(API_VERSION "1")

execute_process(
  COMMAND /bin/sh ${CMAKE_CURRENT_SOURCE_DIR}/tools/get_soversion.sh
  OUTPUT_VARIABLE SO_VERSION
  OUTPUT_STRIP_TRAILING_WHITESPACE
  RESULT_VARIABLE result)
if(NOT result EQUAL 0)
  message(FATAL_ERROR "Error running get-soversion.sh script")
endif()

set(LIB_VERSION "${SO_VERSION}.${PROJECT_VERSION}")

option(CLICK_MODE "Installs to a contained location" on)

#
# Code style fixer. We put the code through astyle first because it makes some fixes that
# clang-format won't do (such as "char *p" -> "char* p"). But astyle messes up other things
# (particularly lambdas and assembly-style comments), which clang-format does right.
# So, we run clang-format after running astyle, which undoes the damage done by astyle
# without reverting desirable astyle fixes.
#

find_program(ASTYLE_COMMAND NAMES astyle)
if (NOT ASTYLE_COMMAND)
    message(WARNING "Cannot find astyle: formatcode target will not be available")
else()
    # astyle 2.03 creates DOS line endings, so we need to fix its output
    find_program(DOS2UNIX_COMMAND NAMES dos2unix)
    if (NOT DOS2UNIX_COMMAND)
        message(WARNING "Cannot find dos2unix: formatcode target will not be available")
    else()
        find_program(CLANG_FORMAT_COMMAND NAMES clang-format-3.6 clang-format-3.5)
        if (NOT CLANG_FORMAT_COMMAND)
            message(WARNING "Cannot find clang-format >= clang-format-3.5: formatcode target will not be available")
        endif()
    endif()
endif()

if (ASTYLE_COMMAND AND DOS2UNIX_COMMAND AND CLANG_FORMAT_COMMAND)
add_subdirectory(scripts)
add_custom_target(
  format

  COMMAND ${CMAKE_BINARY_DIR}/scripts/formatcode ${CMAKE_SOURCE_DIR}/include ${ASTYLE_COMMAND} ${CLANG_FORMAT_COMMAND}
  COMMAND ${CMAKE_BINARY_DIR}/scripts/formatcode ${CMAKE_SOURCE_DIR}/src ${ASTYLE_COMMAND} ${CLANG_FORMAT_COMMAND}
  COMMAND ${CMAKE_BINARY_DIR}/scripts/formatcode ${CMAKE_SOURCE_DIR}/tests ${ASTYLE_COMMAND} ${CLANG_FORMAT_COMMAND}
  )
endif()

include_directories(
  ${CMAKE_SOURCE_DIR}
  ${CMAKE_SOURCE_DIR}/include
  ${Boost_INCLUDE_DIRS}
  ${GLog_INCLUDE_DIR}
  ${TP_QT5_INCLUDE_DIRS}
)

add_subdirectory(data)
add_subdirectory(tools)
add_subdirectory(doc)
add_subdirectory(include)
add_subdirectory(src)
add_subdirectory(tests)
