# SPDX-FileCopyrightText: 2026 Martin Heriban
# SPDX-License-Identifier: AGPL-3.0-only

cmake_minimum_required(VERSION 3.25)
project(q4t_winbt LANGUAGES C CXX)

if(NOT WIN32 OR NOT MINGW)
    message(FATAL_ERROR "q4t-winbt qualification requires a MinGW Windows target")
endif()

if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    message(FATAL_ERROR "q4t-winbt canonical candidate toolchain is LLVM-MinGW/Clang")
endif()

if(NOT DEFINED Q4T_CPPWINRT_INCLUDE OR Q4T_CPPWINRT_INCLUDE STREQUAL "")
    message(FATAL_ERROR "Q4T_CPPWINRT_INCLUDE must point to the directory containing winrt/base.h")
endif()

if(NOT EXISTS "${Q4T_CPPWINRT_INCLUDE}/winrt/base.h")
    message(FATAL_ERROR "C++/WinRT projection missing: ${Q4T_CPPWINRT_INCLUDE}/winrt/base.h")
endif()

add_library(q4t-winbt SHARED
    src/q4t_winbt.cpp
)

target_include_directories(q4t-winbt PRIVATE
    "${CMAKE_CURRENT_SOURCE_DIR}/include"
)
target_include_directories(q4t-winbt SYSTEM PRIVATE
    "${Q4T_CPPWINRT_INCLUDE}"
)

target_compile_features(q4t-winbt PRIVATE cxx_std_20)
set_target_properties(q4t-winbt PROPERTIES
    CXX_EXTENSIONS OFF
    OUTPUT_NAME "q4t-winbt"
    PREFIX ""
)

# The selected Bluetooth API floor is Windows 10 1607 / build 14393 because
# BluetoothDevice::GetRfcommServicesForIdAsync was introduced there. WINVER is
# kept at the Windows 10 family value; runtime/product support records the
# stricter build floor.
target_compile_definitions(q4t-winbt PRIVATE
    WINVER=0x0A00
    _WIN32_WINNT=0x0A00
    NOMINMAX
    WIN32_LEAN_AND_MEAN
)

target_compile_options(q4t-winbt PRIVATE
    -Wall
    -Wextra
    -Wpedantic
    -Werror
)

# C++/WinRT consumes the WinRT umbrella import library. Current LLVM-MinGW/
# mingw-w64 provides libwindowsapp.a; no MSVC or Windows SDK import library is
# part of this candidate build path.
target_link_libraries(q4t-winbt PRIVATE windowsapp)

# Keep the q4t Windows bundle self-contained. Windows system DLLs remain
# dynamic, but the LLVM-MinGW C++ runtime must not become a side-by-side
# deployment requirement for this small native bridge.
target_link_options(q4t-winbt PRIVATE -static)
