cmake_minimum_required(VERSION 3.18...3.26 FATAL_ERROR)
project(TangoAccessControl VERSION 2.19 LANGUAGES CXX)

get_property(IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)

if(NOT CMAKE_BUILD_TYPE AND NOT IS_MULTI_CONFIG)
    message(STATUS "No build type specified - default is DEBUG")
    set(CMAKE_BUILD_TYPE DEBUG)
endif()

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
find_package(MySQL REQUIRED)
find_package(Tango REQUIRED)

set(SOURCES ClassFactory.cpp
            DbUtils.cpp
            TangoAccessControlStateMachine.cpp
            TangoAccessControl.cpp
            TangoAccessControlClass.cpp
            main.cpp)

set(ADDITIONAL_SOURCES ../AbstractClasses/AccessControl.cpp
                       ../AbstractClasses/AccessControlClass.cpp
                       ../AbstractClasses/AccessControlStateMachine.cpp)

set(HEADERS Logging.h
            TangoAccessControl.h
            TangoAccessControlClass.h)

set(ADDITIONAL_HEADERS ../AbstractClasses/AccessControl.h
                       ../AbstractClasses/AccessControlClass.h)

add_executable(TangoAccessControl ${SOURCES} ${ADDITIONAL_SOURCES} ${HEADERS} ${ADDITIONAL_HEADERS})
target_link_libraries(TangoAccessControl PUBLIC Tango::Tango MySQL::MySQL)
target_include_directories(TangoAccessControl PUBLIC ../AbstractClasses)

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


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

    if(NOT (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
        target_compile_options(TangoAccessControl PUBLIC -fpie)
        set_target_properties(TangoAccessControl PROPERTIES LINK_FLAGS "-Wl,-z,now -pie")
    else()
        set_target_properties(TangoAccessControl PROPERTIES LINK_FLAGS "-bind_at_load")
    endif()
endif()

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