cmake_minimum_required(VERSION 3.21)

project(%{ProjectName} VERSION 0.1 LANGUAGES CXX)

enable_testing()

@if !%{HasQSPSetup}
set(CMAKE_AUTOMOC ON)
@endif
set(CMAKE_CXX_STANDARD_REQUIRED ON)

@if "%{TestFrameWork}" == "QtTest"
@if "%{RequireGUI}" == "true"
find_package(Qt6 REQUIRED COMPONENTS Gui Test)
@else
find_package(Qt6 REQUIRED COMPONENTS Test)
@endif
@endif
@if "%{TestFrameWork}" == "QtQuickTest"
@if "%{UseSetupCode}" == "true"
find_package(Qt6 REQUIRED COMPONENTS QuickTest Qml)
@else
find_package(Qt6 REQUIRED COMPONENTS QuickTest)
@endif
@endif

@if "%{TestFrameWork}" == "QtTest"
qt_standard_project_setup(REQUIRES 6.5)
@endif

@if "%{TestFrameWork}" == "QtQuickTest"
@if %{HasQSPSetup}
@if %{UsesAutoResourcePrefix}
qt_standard_project_setup(REQUIRES %{MinimumSupportedQtVersion})
@else
qt_standard_project_setup()
@endif
@endif
@endif

qt_add_executable(%{TestCaseName}
@if "%{TestFrameWork}" == "QtTest"
    %{TestCaseFileWithCppSuffix}
@endif
@if "%{TestFrameWork}" == "QtQuickTest"
    %{MainCppName}
@if "%{UseSetupCode}" == "true"
    setup.cpp setup.h
@endif
@endif
)
add_test(NAME %{TestCaseName} COMMAND %{TestCaseName})

@if "%{TestFrameWork}" == "QtQuickTest"
# no need to copy around qml test files for shadow builds - just set the respective define
add_definitions(-DQUICK_TEST_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}")

qt_add_qml_module(%{TestCaseName}
    URI Tests.%{TestCaseName}.QuickTest
    QML_FILES %{TestCaseFileWithQmlSuffix}
@if !%{UsesAutoResourcePrefix}
    RESOURCE_PREFIX "/qt/qml"
@endif
)

@endif

set_target_properties(%{TestCaseName} PROPERTIES
    MACOSX_BUNDLE FALSE
    WIN32_EXECUTABLE FALSE
)

target_link_libraries(%{TestCaseName}
@if "%{TestFrameWork}" == "QtTest"
@if "%{RequireGUI}" == "true"
    PRIVATE Qt6::Gui
@endif
    PRIVATE Qt6::Test
@endif
@if "%{TestFrameWork}" == "QtQuickTest"
@if "%{UseSetupCode}"
    PRIVATE Qt6::Qml
@endif
    PRIVATE Qt6::QuickTest
@endif
)

install(TARGETS %{TestCaseName}
    BUNDLE  DESTINATION .
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

qt_generate_deploy_app_script(
    TARGET %{TestCaseName}
    OUTPUT_SCRIPT deploy_script
    NO_UNSUPPORTED_PLATFORM_ERROR
)
install(SCRIPT ${deploy_script})
