cmake_minimum_required(VERSION 3.16)

project(Qt6MigratedBenchmark LANGUAGES CXX)

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

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

# Coverage option - disabled by default
option(ENABLE_COVERAGE "Enable code coverage with gcov/lcov" OFF)

find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets Test)

qt_standard_project_setup(REQUIRES 6.5)

add_executable(Qt6MigratedBenchmark
    main.cpp
    MainWindow.h
    MainWindow.cpp
    MainWindow.ui
    resources.qrc
)

target_link_libraries(Qt6MigratedBenchmark PRIVATE
    Qt6::Core
    Qt6::Gui
    Qt6::Widgets
)

# Coverage configuration
if(ENABLE_COVERAGE)
    if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
        message(STATUS "Coverage enabled for ${CMAKE_CXX_COMPILER_ID}")
        
        # Add coverage flags
        target_compile_options(Qt6MigratedBenchmark PRIVATE
            -g
            -O0
            --coverage
            -fprofile-arcs
            -ftest-coverage
        )
        
        target_link_options(Qt6MigratedBenchmark PRIVATE
            --coverage
            -fprofile-arcs
            -ftest-coverage
        )
    else()
        message(WARNING "Coverage is only supported with GCC or Clang")
    endif()
endif()

# Enable testing
enable_testing()

# Add test executable
add_executable(Qt6MigratedBenchmarkTest
    tests/test_main.cpp
    tests/test_mainwindow.cpp
    MainWindow.h
    MainWindow.cpp
)

target_include_directories(Qt6MigratedBenchmarkTest PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}
)

target_link_libraries(Qt6MigratedBenchmarkTest PRIVATE
    Qt6::Core
    Qt6::Gui
    Qt6::Widgets
    Qt6::Test
)

# Apply same coverage flags to test target
if(ENABLE_COVERAGE)
    if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
        target_compile_options(Qt6MigratedBenchmarkTest PRIVATE
            -g
            -O0
            --coverage
            -fprofile-arcs
            -ftest-coverage
        )
        
        target_link_options(Qt6MigratedBenchmarkTest PRIVATE
            --coverage
            -fprofile-arcs
            -ftest-coverage
        )
    endif()
endif()

# Register tests with CTest
add_test(NAME Qt6MigratedBenchmarkTest COMMAND Qt6MigratedBenchmarkTest)