cmake_minimum_required(VERSION 3.22)

project(frameassistant VERSION 0.1.0 LANGUAGES CXX)

# ---------------------------------------------------------------------------
# App identity. The app key must be globally unique within SteamVR (reverse-DNS style).
# ---------------------------------------------------------------------------
set(VRDASH_APP_KEY       "net.matzman.frameassistant" CACHE STRING "SteamVR application/overlay key")
set(VRDASH_DISPLAY_NAME  "Frame Assistant"      CACHE STRING "Name shown in the SteamVR dashboard")
# Must not be the name of a SteamVR process (checked in cmake/Dashboard.cmake).
set(VRDASH_EXECUTABLE_NAME "frame-assistant"    CACHE STRING "File name of the executable")
set(VRDASH_TEXTURE_WIDTH  1600 CACHE STRING "Overlay texture width in pixels")
set(VRDASH_TEXTURE_HEIGHT 1000 CACHE STRING "Overlay texture height in pixels")

set(OPENVR_VERSION "2.15.6" CACHE STRING "OpenVR SDK version to download when OPENVR_ROOT is not set")

set(VRDASH_VULKAN "AUTO" CACHE STRING "Build the Vulkan render backend (AUTO, ON, OFF)")
set_property(CACHE VRDASH_VULKAN PROPERTY STRINGS AUTO ON OFF)
option(VRDASH_QML_BYTECODE_ONLY "Compile QML to bytecode only (no C++): keeps the binary off Qt's version-locked private API, so it runs on other Qt 6 patch releases (system Qt)" OFF)
option(VRDASH_DEPLOY_QT "Bundle Qt libraries, plugins and QML modules on install (self-contained build)" OFF)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Mirror the install layout in the build tree (bin/, lib/, share/<project>/) so the
# binary finds its resources and libopenvr_api.so the same way in both places.
include(GNUInstallDirs)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
set(VRDASH_SHARE_SUBDIR ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME})
set(CMAKE_BUILD_RPATH "\$ORIGIN/../${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_INSTALL_RPATH "\$ORIGIN/../${CMAKE_INSTALL_LIBDIR}")

# Arch/SteamOS build Qt with -mno-direct-extern-access (protected symbols), which forbids
# copy relocations in consumers. Qt's CMake only passes the flag on if Qt itself was built
# with it, so a binary built against e.g. Debian's Qt would fail to load against Arch's
# system Qt on x86-64. Always using indirect access works with both. (GCC has no such flag
# for aarch64; its PIE code never uses copy relocations there.)
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-mno-direct-extern-access VRDASH_HAS_NO_DIRECT_EXTERN_ACCESS)
if(VRDASH_HAS_NO_DIRECT_EXTERN_ACCESS)
    add_compile_options(-mno-direct-extern-access)
endif()

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include(OpenVR)

find_package(Qt6 6.5 REQUIRED COMPONENTS Core Gui OpenGL Qml Quick QuickControls2)
qt_standard_project_setup(REQUIRES 6.5)
if(QT_KNOWN_POLICY_QTP0004)
    qt_policy(SET QTP0004 NEW) # QML files live in src/qml/
endif()

include(Dashboard)
if(VRDASH_DEPLOY_QT)
    include(BundleQt)
endif()

add_subdirectory(src)
add_subdirectory(resources)

# After all dashboards (and their install rules) are defined.
if(VRDASH_DEPLOY_QT)
    vrdash_bundle_qt()
endif()

option(VRDASH_BUILD_TESTS "Build unit tests" ON)
if(VRDASH_BUILD_TESTS)
    enable_testing()
    add_subdirectory(tests)
endif()
