# Toolchain file for Embarcadero bcc64x (clang-15 + MinGW + ld.lld + libc++)
# Usage: cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE=<path>/bcc64x-toolchain.cmake
#              -DROOTDIR="C:/Program Files (x86)/Embarcadero/Studio/37.0" ..

set(CMAKE_SYSTEM_NAME Windows)
# Setting CMAKE_SYSTEM_NAME marks this a cross-compile, so CMake no longer
# auto-detects the processor. bcc64x targets x86_64; set it explicitly or
# dependencies that test CMAKE_SYSTEM_PROCESSOR (e.g. Poco) hit empty-string
# if() parse errors.
set(CMAKE_SYSTEM_PROCESSOR AMD64)

if(NOT DEFINED ROOTDIR)
  set(ROOTDIR "C:/Program Files (x86)/Embarcadero/Studio/37.0"
      CACHE PATH "Embarcadero Studio installation root")
endif()

# bcc64x lives in bin64 (NOT bin). Set the compiler before including the
# platform module so its CMAKE_BASE_NAME-based variant detection works.
set(CMAKE_C_COMPILER   "${ROOTDIR}/bin64/bcc64x.exe" CACHE FILEPATH "C compiler")
set(CMAKE_CXX_COMPILER "${ROOTDIR}/bin64/bcc64x.exe" CACHE FILEPATH "C++ compiler")

# Pre-load the custom platform module. It sets the __WINDOWS_EMBARCADERO
# include blocker so CMake's built-in (Borland ilink64/-Od) module does not
# run afterwards. CMAKE_BASE_NAME is not yet set at toolchain time, so provide
# it explicitly for the module's compiler-variant detection.
set(CMAKE_BASE_NAME "bcc64x")
include("${CMAKE_CURRENT_LIST_DIR}/Windows-Embarcadero.cmake")

# Runs after Platform/Windows.cmake to force CMAKE_LINK_LIBRARY_SUFFIX="" (lld
# needs -lfoo, not -lfoo.lib). The platform module's setting is otherwise
# overwritten by the generic Windows.cmake.
set(CMAKE_USER_MAKE_RULES_OVERRIDE "${CMAKE_CURRENT_LIST_DIR}/bcc64x-rules-override.cmake"
    CACHE FILEPATH "bcc64x post-platform link-rule corrections" FORCE)

# Ninja executable — Embarcadero ships one in its cmake directory.
if(NOT CMAKE_MAKE_PROGRAM)
  find_program(CMAKE_MAKE_PROGRAM
    NAMES ninja ninja.exe
    PATHS "${ROOTDIR}/cmake" "${ROOTDIR}/bin"
    NO_DEFAULT_PATH
  )
endif()
