cmake_minimum_required(VERSION 3.10)

# Default install location. Must be set here, before setting the project.
if (NOT DEFINED CMAKE_INSTALL_PREFIX)
    set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/install CACHE PATH "" FORCE)
endif()

project(lomiri-action-api VERSION 1.2.1 LANGUAGES CXX C)

if(PROJECT_BINARY_DIR STREQUAL PROJECT_SOURCE_DIR)
   message(FATAL_ERROR "In-tree build attempt detected, aborting. Set your build dir outside your source dir, delete CMakeCache.txt from source root and try again.")
endif()

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)

option(basic_warnings "Basic compiler warnings." ON)
option(use_libhud2 "Use external libhud2 lib" ON)
option(ENABLE_QT6 "Enable Qt6" OFF)
if ("${CMAKE_BUILD_TYPE}" STREQUAL "release" OR "${CMAKE_BUILD_TYPE}" STREQUAL "relwithdebinfo")
    option(ENABLE_WERROR "Treat warnings as errors" ON)
else()
    option(ENABLE_WERROR "Treat warnings as errors" OFF)
endif()

include(FindPkgConfig)

if (use_libhud2)
    pkg_search_module(HUD REQUIRED hud-2)
else()
    pkg_check_modules(GLIB2 REQUIRED glib-2.0)
    include_directories(${GLIB2_INCLUDE_DIRS})

    pkg_check_modules(GOBJECT2 REQUIRED gobject-2.0)
    include_directories(${GOBJECT2_INCLUDE_DIRS})

    pkg_check_modules(GIO2 REQUIRED gio-2.0)
    include_directories(${GIO2_INCLUDE_DIRS})
endif()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++17 -fno-permissive -pedantic -Wall -Wextra")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")

if (ENABLE_WERROR)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
endif()

if(${basic_warnings})
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -pedantic")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic")
endif()

# Standard install paths
include(GNUInstallDirs)

set(LIBDIR "lib" CACHE PATH "Destination install dir for the library")

set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

# enable QML debugging
string(TOLOWER "${CMAKE_BUILD_TYPE}" cmake_build_type_lower) # Build types should always be lowercase but sometimes they are not.
if(cmake_build_type_lower MATCHES "debug")
    add_definitions(-DQT_QML_DEBUG)
endif()

# Make sure we have all the needed symbols
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,defs")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-z,defs")

# Static C++ checks
add_custom_target(cppcheck COMMAND cppcheck --enable=all -q --error-exitcode=2
                                       ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/tests)

if (ENABLE_QT6)
    find_package(QT NAMES Qt6 REQUIRED COMPONENTS Core Qml)
    set(QT_VERSION_IN_LIBNAME ${QT_VERSION_MAJOR})
else()
    find_package(QT NAMES Qt5 REQUIRED COMPONENTS Core Qml)
    set(QT_VERSION_IN_LIBNAME "")
endif()

find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Widgets Quick Qml)

include_directories(include)
add_subdirectory(src)
add_subdirectory(qml)
add_subdirectory(examples)

option(GENERATE_DOCUMENTATION "Generate documenation for C++ and QML" OFF)
if(${GENERATE_DOCUMENTATION})
  add_subdirectory(documentation)
endif()


find_program(qmltestrunner_exe qmltestrunner)

if(NOT qmltestrunner_exe)
  message(FATAL_ERROR "Could not locate qmltestrunner.")
endif()

macro(add_qml_test COMPONENT_NAME)
    set(options NO_ADD_TEST NO_TARGETS)

    cmake_parse_arguments(qmltest "${options}" "IMPORT_PATH" "TARGETS" ${ARGN})

    set(qmltest_TARGET test${COMPONENT_NAME})
    set(qmltest_FILE tst_${COMPONENT_NAME})

    if("${qmltest_IMPORT_PATH}" STREQUAL "")
        add_custom_target(${qmltest_TARGET}
            ${qmltestrunner_exe}  -input ${CMAKE_CURRENT_SOURCE_DIR}/${qmltest_FILE}.qml
                          -o ${CMAKE_BINARY_DIR}/${qmltest_TARGET}.xml,xunitxml
                          -o -,txt)
    else()
        add_custom_target(${qmltest_TARGET}
            ${qmltestrunner_exe} -input ${CMAKE_CURRENT_SOURCE_DIR}/${qmltest_FILE}.qml
                          -import ${qmltest_IMPORT_PATH}
                          -o ${CMAKE_BINARY_DIR}/${qmltest_TARGET}.xml,xunitxml
                          -o -,txt)
    endif()

    if(NOT "${qmltest_UNPARSED_ARGUMENTS}" STREQUAL "")
        set_target_properties(${qmltest_TARGET} ${qmltest_PROPERTIES})
    elseif(NOT "${qmltest_DEFAULT_PROPERTIES}" STREQUAL "")
        set_target_properties(${qmltest_TARGET} ${qmltest_DEFAULT_PROPERTIES})
    endif()

    if("${qmltest_NO_ADD_TEST}" STREQUAL FALSE AND NOT "${qmltest_DEFAULT_NO_ADD_TEST}" STREQUAL "TRUE")
        add_test(${qmltest_TARGET} make ${qmltest_TARGET})

        if(NOT "${qmltest_UNPARSED_ARGUMENTS}" STREQUAL "")
            set_tests_properties(${qmltest_TARGET} ${qmltest_PROPERTIES})
        elseif(NOT "${qmltest_DEFAULT_PROPERTIES}" STREQUAL "")
            set_tests_properties(${qmltest_TARGET} ${qmltest_DEFAULT_PROPERTIES})
        endif()
    endif("${qmltest_NO_ADD_TEST}" STREQUAL FALSE AND NOT "${qmltest_DEFAULT_NO_ADD_TEST}" STREQUAL "TRUE")

    if("${qmltest_NO_TARGETS}" STREQUAL "FALSE")
        if(NOT "${qmltest_TARGETS}" STREQUAL "")
            foreach(TARGET ${qmltest_TARGETS})
                add_dependencies(${TARGET} ${qmltest_TARGET})
            endforeach(TARGET)
        elseif(NOT "${qmltest_DEFAULT_TARGETS}" STREQUAL "")
            foreach(TARGET ${qmltest_DEFAULT_TARGETS})
                add_dependencies(${TARGET} ${qmltest_TARGET})
            endforeach(TARGET)
        endif()
    endif("${qmltest_NO_TARGETS}" STREQUAL "FALSE")
endmacro(add_qml_test)

option(ENABLE_TESTING "Enable testing." ON)
if(${ENABLE_TESTING})
  enable_testing()
  add_subdirectory(test)

  ADD_CUSTOM_TARGET(
    check
    ${CMAKE_CTEST_COMMAND} --force-new-ctest-process --output-on-failure
  )
endif()

include(EnableCoverageReport)
#####################################################################
# Enable code coverage calculation with gcov/gcovr/lcov
# Usage:
#  * Switch build type to coverage (use ccmake or cmake-gui)
#  * Invoke make, make test, make coverage (or ninja if you use that backend)
#  * Find html report in subdir coveragereport
#  * Find xml report feasible for jenkins in coverage.xml
#####################################################################
if(cmake_build_type_lower MATCHES coverage)
  ENABLE_COVERAGE_REPORT(TARGETS lomiri-action-qt lomiri-action-qml
                         FILTER /usr/include ${CMAKE_SOURCE_DIR}/test/* ${CMAKE_BINARY_DIR}/*)
endif()
