cmake_minimum_required(VERSION 3.18...3.26 FATAL_ERROR)

project(DataBase VERSION 5.22.0 LANGUAGES CXX)

if(NOT DEFINED CMAKE_CXX_STANDARD)
    set(CMAKE_CXX_STANDARD 14)
endif()

set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

include(configure/CMakeLists.txt)
include(test/CMakeLists.txt)

configure_file(cmake_config.h.in cmake_config.h @ONLY)

set(SOURCES DataBase.cpp
            DataBaseClass.cpp
            DatabaseConnectionHandle.cpp
            DatabaseConnectionPool.cpp
            DataBaseStateMachine.cpp
            ClassFactory.cpp
            main.cpp)

set(ADDITIONAL_SOURCES  DataBaseUtils.cpp
                        update_starter.cpp)

add_executable(Databaseds ${SOURCES} ${ADDITIONAL_SOURCES})
target_include_directories(Databaseds PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(Databaseds PUBLIC Tango::Tango MySQL::MySQL)

if (WIN32 AND (Tango_IS_STATIC OR Tango_FORCE_STATIC))
    set_target_properties(Databaseds PROPERTIES
        MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
    )
endif()

if (MSVC)
    target_compile_options(Databaseds PUBLIC /W3)
else()
    target_compile_options(Databaseds PUBLIC -Wall -Wextra -D_FORTIFY_SOURCE=2 -O1)
endif()


if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
    set_target_properties(Databaseds PROPERTIES LINK_FLAGS "-bind_at_load")
elseif(NOT MSVC)
    target_compile_options(Databaseds PUBLIC -fpie)
    set_target_properties(Databaseds PROPERTIES LINK_FLAGS "-Wl,-z,now -pie")
endif()


if(TDB_ENABLE_COVERAGE)
  target_link_options(Databaseds PUBLIC --coverage)
  target_compile_options(Databaseds PUBLIC --coverage)
endif()

install(TARGETS Databaseds
        RUNTIME DESTINATION "${CMAKE_INSTALL_FULL_BINDIR}")

option(TANGO_WARNINGS_AS_ERRORS "Treat compiler warnings as errors" OFF)

if(TANGO_WARNINGS_AS_ERRORS)
    if (MSVC)
        target_compile_options(Databaseds PUBLIC /WX)
    else()
        target_compile_options(Databaseds PUBLIC -Werror -Wformat -Werror=format-security -pedantic)
    endif()
endif()
