cmake_minimum_required(VERSION 3.13)

project(AdaptiveBrightness CXX)

option(FORCE_COLORED_OUTPUT "Always produce ANSI-colored output (GNU/Clang only)." ON)
option(LOG_SOURCE_FILE "Log the location in the source file where the log originates" OFF)
option(LOG_FUNCTION "Log the name of the function where the log originates" OFF)

set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})

include(compile_flags)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Release" CACHE STRING "CMake Build Type" FORCE)
endif()

set(CMAKE_CONFIGURATION_TYPES Debug Release CACHE STRING "CMake Configuration Types" FORCE)

if(${FORCE_COLORED_OUTPUT})
    if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
        add_compile_options(-fdiagnostics-color=always)
    elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang|AppleClang")
        add_compile_options(-fcolor-diagnostics)
    endif()
endif()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

find_package(Threads REQUIRED)
find_package(Qt5 COMPONENTS
             Core
             Gui
             Widgets
             SerialPort
             REQUIRED
)

file(GLOB_RECURSE adaptive_brightness_srcs
     AdaptiveBrightness/*.cpp
     AdaptiveBrightness/*.hpp

     AdaptiveBrightness/*.ui
     AdaptiveBrightness/*.qrc
)

if(MSVC)
    file(GLOB_RECURSE icon_resource
        AdaptiveBrightness/*.rc
    )
    list(APPEND adaptive_brightness_srcs ${icon_resource})

    add_executable(AdaptiveBrightness WIN32 ${adaptive_brightness_srcs})
else()
    add_executable(AdaptiveBrightness ${adaptive_brightness_srcs})
endif()

target_include_directories(AdaptiveBrightness PUBLIC ./AdaptiveBrightness)
target_link_libraries(AdaptiveBrightness
                      Threads::Threads
                      Qt5::Core
                      Qt5::Gui
                      Qt5::Widgets
                      Qt5::SerialPort
)

if(MSVC)
target_link_libraries(AdaptiveBrightness
                      Dxva2.lib
)
endif()

if(MSVC)
    get_target_property(QT5_QMAKE_EXECUTABLE Qt5::qmake IMPORTED_LOCATION)
    get_filename_component(QT5_WINDEPLOYQT_EXECUTABLE ${QT5_QMAKE_EXECUTABLE} PATH)
    set(QT5_WINDEPLOYQT_EXECUTABLE "${QT5_WINDEPLOYQT_EXECUTABLE}/windeployqt.exe")

    add_custom_command(TARGET AdaptiveBrightness POST_BUILD
                       COMMAND ${QT5_WINDEPLOYQT_EXECUTABLE}
                       --no-system-d3d-compiler
                       --no-compiler-runtime
                       --no-opengl-sw
                       --qmldir "${CMAKE_SOURCE_DIR}/AdaptiveBrightness"
                       $<TARGET_FILE_DIR:AdaptiveBrightness>
    )
endif()

adaptive_brightness_add_compile_flags(AdaptiveBrightness)
adaptive_brightness_enable_warnings(AdaptiveBrightness)

if(${LOG_SOURCE_FILE})
    target_compile_definitions(${target} PUBLIC LOG_SOURCE_LOCATION)
endif()

if(${LOG_FUNCTION})
    target_compile_definitions(${target} PUBLIC LOG_FUNCTION_LOCATION)
endif()

set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT AdaptiveBrightness)
