cmake_minimum_required(VERSION 3.24)
project(gk3hd_native_openxr_session LANGUAGES CXX)
find_path(OPENXR_INCLUDE_DIR openxr/openxr.h REQUIRED)
add_library(gk3hd_native_openxr_session STATIC packed_session.cpp controller_actions.cpp)
target_include_directories(gk3hd_native_openxr_session PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}" "${OPENXR_INCLUDE_DIR}")
target_compile_features(gk3hd_native_openxr_session PUBLIC cxx_std_17)
set_property(TARGET gk3hd_native_openxr_session PROPERTY POSITION_INDEPENDENT_CODE ON)
if(MSVC)
    set_property(TARGET gk3hd_native_openxr_session PROPERTY MSVC_RUNTIME_LIBRARY MultiThreaded)
    target_compile_options(gk3hd_native_openxr_session PRIVATE /W4 /WX /EHsc)
else()
    target_compile_options(gk3hd_native_openxr_session PRIVATE -Wall -Wextra -Werror)
endif()
option(GK3HD_OPENXR_ANDROID "Build the opt-in Android GLES/JNI host" OFF)
if(GK3HD_OPENXR_ANDROID)
    if(NOT ANDROID OR NOT ANDROID_ABI STREQUAL "arm64-v8a")
        message(FATAL_ERROR "The Android OpenXR host currently targets ARM64")
    endif()
    if(NOT EXISTS "${OPENXR_LOADER_LIBRARY}")
        message(FATAL_ERROR "Supply the verified Android OpenXR loader library")
    endif()
    add_library(openxr_loader SHARED IMPORTED)
    set_property(TARGET openxr_loader PROPERTY IMPORTED_LOCATION "${OPENXR_LOADER_LIBRARY}")
    add_library(gk3hd_openxr SHARED android_host.cpp)
    target_compile_features(gk3hd_openxr PRIVATE cxx_std_17)
    target_compile_options(gk3hd_openxr PRIVATE -Wall -Wextra -Werror)
    target_link_options(gk3hd_openxr PRIVATE "-Wl,-z,max-page-size=16384")
    target_link_libraries(gk3hd_openxr PRIVATE gk3hd_native_openxr_session openxr_loader EGL GLESv3 android log)
endif()
option(GK3HD_OPENXR_SESSION_TESTS "Build isolated dispatch/ownership tests, never a game or headset source" OFF)
if(GK3HD_OPENXR_SESSION_TESTS)
    enable_testing()
    find_package(Threads REQUIRED)
    function(native_test name source)
        add_executable(gk3hd-native-${name}-tests ${source})
        target_link_libraries(gk3hd-native-${name}-tests PRIVATE gk3hd_native_openxr_session Threads::Threads)
        if(MSVC)
            set_property(TARGET gk3hd-native-${name}-tests PROPERTY MSVC_RUNTIME_LIBRARY MultiThreaded)
            target_compile_options(gk3hd-native-${name}-tests PRIVATE /W4 /WX /EHsc)
        else()
            target_compile_options(gk3hd-native-${name}-tests PRIVATE -Wall -Wextra -Werror)
        endif()
        add_test(NAME native-openxr-${name} COMMAND gk3hd-native-${name}-tests)
        set_tests_properties(native-openxr-${name} PROPERTIES TIMEOUT 15)
    endfunction()
    native_test(session tests.cpp)
    native_test(controllers controller_tests.cpp)
    native_test(discovery discovery_tests.cpp)
    native_test(display-rates display_rate_tests.cpp)
    native_test(performance performance_tests.cpp)
endif()
