cmake_minimum_required(VERSION 3.25)

project(biometryd
    VERSION 0.4.0
    LANGUAGES CXX)

set(BIOMETRYD_VERSION_MAJOR 1)
set(BIOMETRYD_VERSION_MINOR 0)
set(BIOMETRYD_VERSION_PATCH 1)

option(ENABLE_WERROR "Treat all build warnings as errors" ON)
option(ENABLE_QT6 "Enable Qt6 build" OFF)
set(CMAKE_CXX_STANDARD 17 CACHE STRING "The C++ standard to use")
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -pedantic -Wextra -fvisibility=hidden")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fno-strict-aliasing -fvisibility=hidden -fvisibility-inlines-hidden -pedantic -Wextra")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined")

if(ENABLE_WERROR)
    add_compile_options("-Werror")
endif()

include(GNUInstallDirs)

if(ENABLE_QT6)
    find_package(Qt6 REQUIRED COMPONENTS Core Qml Quick QuickTest)
    set(QT_VERSION_MAJOR 6)
    # FIXME disabled due to issue building tests with GMock if CMAKE_AUTOMOC is
    # enabled
    # qt_standard_project_setup()
else()
    find_package(Qt5 REQUIRED COMPONENTS Core Qml Quick QuickTest)
    set(QT_VERSION_MAJOR 5)
endif()

set(
    BIOMETRYD_DEFAULT_PLUGIN_DIRECTORY "${CMAKE_INSTALL_FULL_LIBDIR}/biometryd/plugins"
    CACHE STRING "Default plugin installation directory")

set(
    BIOMETRYD_CUSTOM_PLUGIN_DIRECTORY "/custom/vendor/biometryd/plugins"
    CACHE STRING "Custom plugin installation directory")

enable_testing()

find_package(PkgConfig)
find_package(Boost COMPONENTS filesystem program_options REQUIRED)
find_package(nlohmann_json 3.11 REQUIRED)

pkg_check_modules(DBUS_CPP dbus-cpp REQUIRED)
pkg_check_modules(DBUS dbus-1 REQUIRED)
pkg_check_modules(LIBAPPARMOR libapparmor REQUIRED)
pkg_check_modules(PROCESS_CPP process-cpp REQUIRED)
pkg_check_modules(SQLITE3 sqlite3 REQUIRED)

# Opt-out of depending on systemd at build-time
option(USE_SYSTEMD "Install systemd service" ON)
if (USE_SYSTEMD)
    pkg_check_modules(SYSTEMD systemd)
    if (NOT SYSTEMD_FOUND)
        message(FATAL_ERROR "systemd pkg-config not found (required for figuring out service install location)")
    endif()
endif()

# Opt-in to enable hybris support
option(WITH_HYBRIS "Enable libhybris integration support" OFF)
if (WITH_HYBRIS)
    # Try to find hybris, and disable hybris from build if not found
    find_library(Hybris
        NAMES hybris-common REQUIRED
    )
endif()

include_directories(
    include
    src

    # Make sure that files generated during build are available for compilation.
    # Most notably, this refers to include/biometry/version.h
    ${CMAKE_BINARY_DIR}/include

    ${Boost_INCLUDE_DIRS}
    ${DBUS_CPP_INCLUDE_DIRS}
    ${DBUS_INCLUDE_DIRS}
    ${LIBAPPARMOR_INCLUDE_DIRS}
    ${PROCESS_CPP_INCLUDE_DIRS}
    ${SQLITE3_INCLUDE_DIRS})

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