set(Renderive_Qt_dependencies)
if(RENDERIVE_BUILD_TESTS)
    list(APPEND Renderive_Qt_dependencies global::GTest)
endif()
rcl_add_dependency_action_targets(Renderive_Qt_env ${Renderive_Qt_dependencies})
set_target_properties(Renderive_Qt_env PROPERTIES FOLDER Renderive_Qt)
library_is_installed_with_rely(Renderive_Qt_dependencies_installed ${Renderive_Qt_dependencies})
if(NOT Renderive_Qt_dependencies_installed)
    rcl_log_append("[FATAL_ERROR] Renderive_Qt dependencies are not installed. Build Renderive_Qt_env first")
    return()
endif()
if(Renderive_Qt_dependencies)
    rcl_load_dependency_environment(${Renderive_Qt_dependencies})
endif()
find_package(Qt5 CONFIG REQUIRED COMPONENTS Core Gui Widgets)
if(RENDERIVE_BUILD_TESTS)
    find_package(GTest CONFIG REQUIRED)
endif()
if(NOT TARGET Renderive_render_2D)
    rcl_log_append("[FATAL_ERROR] Renderive_Qt requires Renderive_render_2D")
    return()
endif()
set(Renderive_Qt_source_dir "${CMAKE_CURRENT_LIST_DIR}")
append_glob_source(Renderive_Qt_sources "${Renderive_Qt_source_dir}")
list(FILTER Renderive_Qt_sources EXCLUDE REGEX "/tests/")
add_library(Renderive_Qt STATIC ${Renderive_Qt_sources})
set_target_properties(Renderive_Qt PROPERTIES AUTOMOC ON)
target_include_directories(Renderive_Qt PUBLIC
    "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/..>"
)
target_compile_features(Renderive_Qt PUBLIC cxx_std_20)
target_link_libraries(Renderive_Qt PUBLIC
    Renderive_render_2D
    Qt5::Core
    Qt5::Gui
    Qt5::Widgets
)
if(MSVC)
    target_compile_options(Renderive_Qt PRIVATE /utf-8)
endif()
if(RENDERIVE_BUILD_TESTS)
    get_filename_component(Renderive_Qt_runtime_root "${Qt5_DIR}/../../.." ABSOLUTE)
    set(Renderive_Qt_test_dir "${CMAKE_CURRENT_LIST_DIR}/tests")
    append_glob_source(Renderive_Qt_test_sources "${Renderive_Qt_test_dir}")
    foreach(Renderive_Qt_test_source IN LISTS Renderive_Qt_test_sources)
        if(NOT Renderive_Qt_test_source MATCHES "\\.(c|cc|cpp|cxx)$")
            continue()
        endif()
        file(RELATIVE_PATH Renderive_Qt_test_name "${Renderive_Qt_test_dir}" "${Renderive_Qt_test_source}")
        string(MD5 Renderive_Qt_test_hash "${Renderive_Qt_test_name}")
        string(SUBSTRING "${Renderive_Qt_test_hash}" 0 8 Renderive_Qt_test_hash)
        get_filename_component(Renderive_Qt_test_name "${Renderive_Qt_test_source}" NAME_WE)
        string(MAKE_C_IDENTIFIER "${Renderive_Qt_test_name}" Renderive_Qt_test_name)
        set(Renderive_Qt_test_target "Renderive_Qt_${Renderive_Qt_test_name}_${Renderive_Qt_test_hash}")
        add_executable("${Renderive_Qt_test_target}" "${Renderive_Qt_test_source}")
        target_link_libraries("${Renderive_Qt_test_target}" PRIVATE Renderive_Qt GTest::gtest)
        add_test(NAME "${Renderive_Qt_test_target}"
            COMMAND "${CMAKE_COMMAND}" -E env
            "PATH=${Renderive_Qt_runtime_root}/bin\;$ENV{PATH}"
            "QT_QPA_PLATFORM=offscreen"
            "QT_QPA_PLATFORM_PLUGIN_PATH=${Renderive_Qt_runtime_root}/plugins/platforms"
            "$<TARGET_FILE:${Renderive_Qt_test_target}>"
        )
    endforeach()
endif()
