set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)

#set(CMAKE_VERBOSE_MAKEFILE ON)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

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

find_package(glog REQUIRED)
find_package(Boost REQUIRED COMPONENTS iostreams serialization)
find_package(OpenCV REQUIRED)
find_package(nlohmann_json REQUIRED)

include(EnableWarnings)
include(GitVersion)

if (NOT WITHOUT_TESTING)
    message("Enable testing")
    find_package(GTest REQUIRED)
    include(GTest)
endif ()


macro (set_component_default_state TARGET STATE)
    if (NOT DEFINED WITH_${TARGET})
        set(WITH_${TARGET} ${STATE})
    endif ()
endmacro ()

function (add_component)
    if (${ARGC} GREATER_EQUAL 1)
        set(TARGET ${ARGV0})
        list(FIND TMK_DCL_COMPONENTS ${ARGV0} TARGET_INDEX)
        if (${ARGC} GREATER 1)
            set(ARGUMENTS "${ARGN}")
            if(NOT ${ARGV1} STREQUAL EXCLUDE_FROM_ALL)
                list(REMOVE_AT ARGUMENTS 0)
                set(TARGET_SOURCE ${ARGV1})
            else()
                set(TARGET_SOURCE ${ARGV0})
            endif()
        else ()
            set(ARGUMENTS ${ARGV0})
            set(TARGET_SOURCE ${ARGV0})
        endif ()
        if (WITH_${TARGET} OR NOT DEFINED WITH_${TARGET})
            message("Add component ${TARGET} from ${TARGET_SOURCE}")
            if (NOT TARGET_INDEX EQUAL -1)
                message(SEND_ERROR "Duplicate component ${TARGET} include")
                return()
            endif ()
            set_component_default_state(${TARGET} ON)
            add_subdirectory(${ARGUMENTS})
            list(APPEND TMK_DCL_COMPONENTS ${TARGET})
            set(TMK_DCL_COMPONENTS ${TMK_DCL_COMPONENTS} PARENT_SCOPE)
        else ()
            message("Component ${TARGET} marked to exclude from config")
        endif ()
    else ()
        message(SEND_ERROR "Incorrect component specification")
    endif ()
endfunction()

set(TMK_DCL_COMPONENTS "")
