configure_file(AppConfig.h.in ${CMAKE_CURRENT_BINARY_DIR}/generated/AppConfig.h @ONLY)

# Everything except main() lives in a static library: the dashboard executables and
# the tests link against it.
qt_add_library(vrdashboard_core STATIC
    app/DashboardApplication.h app/DashboardApplication.cpp
    app/Surface.h app/Surface.cpp
    vr/Overlay.h vr/Overlay.cpp
    render/RenderBackend.h render/RenderBackend.cpp
    render/OpenGLRenderBackend.h render/OpenGLRenderBackend.cpp
    render/OffscreenQuickRenderer.h render/OffscreenQuickRenderer.cpp
    vr/VrRuntime.h vr/VrRuntime.cpp
    vr/DashboardOverlay.h vr/DashboardOverlay.cpp
    input/OverlayInputBridge.h input/OverlayInputBridge.cpp
    input/ControllerNavigation.h input/ControllerNavigation.cpp
)
target_include_directories(vrdashboard_core PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_BINARY_DIR}/generated)
target_link_libraries(vrdashboard_core PUBLIC
    Qt6::Core Qt6::Gui Qt6::OpenGL Qt6::Qml Qt6::Quick Qt6::QuickControls2
    OpenVR::OpenVR)
target_compile_options(vrdashboard_core PRIVATE -Wall -Wextra)

# Optional Vulkan backend: needs the Vulkan headers and a Qt built with Vulkan support.
if(VRDASH_VULKAN STREQUAL "AUTO")
    find_package(Vulkan QUIET)
    set(_vulkan_ok FALSE)
    if(TARGET Vulkan::Headers AND QT_FEATURE_vulkan)
        set(_vulkan_ok TRUE)
    endif()
elseif(VRDASH_VULKAN)
    find_package(Vulkan REQUIRED)
    if(NOT QT_FEATURE_vulkan)
        message(FATAL_ERROR "VRDASH_VULKAN=ON but Qt was built without Vulkan support")
    endif()
    set(_vulkan_ok TRUE)
else()
    set(_vulkan_ok FALSE)
endif()
if(_vulkan_ok)
    target_sources(vrdashboard_core PRIVATE
        render/VulkanRenderBackend.h render/VulkanRenderBackend.cpp)
    # Headers only: Qt loads libvulkan at runtime, nothing links against it.
    target_link_libraries(vrdashboard_core PUBLIC Vulkan::Headers)
    target_compile_definitions(vrdashboard_core PUBLIC VRDASH_HAS_VULKAN=1)
else()
    target_compile_definitions(vrdashboard_core PUBLIC VRDASH_HAS_VULKAN=0)
endif()
message(STATUS "Vulkan backend: ${_vulkan_ok}")

# The app's own logic (no SteamVR, no rendering): linked by the app and its tests.
qt_add_library(frameassistant_lib STATIC
    config/Config.h config/Config.cpp
    mqtt/MqttClient.h
    mqtt/MosquittoClient.h mqtt/MosquittoClient.cpp
)
target_include_directories(frameassistant_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(frameassistant_lib PUBLIC Qt6::Core PkgConfig::MOSQUITTO)
target_compile_options(frameassistant_lib PRIVATE -Wall -Wextra)

# The app: its UI is the QML module "FrameAssistant" (src/qml/).
vrdash_add_dashboard(frameassistant
    OUTPUT_NAME ${VRDASH_EXECUTABLE_NAME}
    QML_URI FrameAssistant
    SOURCES main.cpp Dashboard.h Dashboard.cpp
    QML_FILES
        qml/Main.qml
        qml/Card.qml)
target_link_libraries(frameassistant PRIVATE frameassistant_lib)

install(FILES ${OPENVR_LIBRARY} DESTINATION ${CMAKE_INSTALL_LIBDIR})

# Licenses: ours, the overview of third-party components, OpenVR's.
install(FILES ${PROJECT_SOURCE_DIR}/LICENSE ${PROJECT_SOURCE_DIR}/THIRD_PARTY_NOTICES.md
        DESTINATION ${CMAKE_INSTALL_DOCDIR})
if(EXISTS ${OPENVR_LICENSE})
    install(FILES ${OPENVR_LICENSE} DESTINATION ${CMAKE_INSTALL_DOCDIR}/third-party
            RENAME openvr.LICENSE)
endif()
