# The wheel's half of fond: the compiled extensions in this directory and the generated version.py.
# scikit-build-core runs this one (pyproject's cmake.source-dir); fond's dev build is the repo root's
# CMakeLists, so a wheel build never configures the tests.
cmake_minimum_required(VERSION 3.25)

if(NOT DEFINED SKBUILD_PROJECT_VERSION)
    message(FATAL_ERROR "src/fond/libs is built through pyproject.toml: run `uv build` (or `uv sync` for an "
                        "editable install). fond's own CMake build is the repo root's CMakeLists.txt.")
endif()

project(fond VERSION ${SKBUILD_PROJECT_VERSION} LANGUAGES CXX)

# MSVC has no C++26 switch yet; c++latest is the newest it has.
if(MSVC)
    add_compile_options(/std:c++latest)
else()
    set(CMAKE_CXX_STANDARD 26)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    set(CMAKE_CXX_EXTENSIONS OFF)
endif()

find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
find_package(pybind11 CONFIG REQUIRED)
# fond::fond brings the headers and, on MSVC, the /Zc:preprocessor the FOR_EACH macros need.
include("${CMAKE_CURRENT_SOURCE_DIR}/../cmake/FondTarget.cmake")

# `uv sync` is an editable install, and pytest imports fond straight from src/ (pythonpath = src), so
# editable builds also drop their outputs into the source tree, where they were before. A wheel
# build never writes into src/.
set(_fond_editable OFF)
if(SKBUILD_STATE STREQUAL "editable")
    set(_fond_editable ON)
endif()

cmake_host_system_information(RESULT _fond_host QUERY HOSTNAME)
string(TOLOWER "${_fond_host}" _fond_host)

# The extensions: each name is src/fond/libs/<name>.cpp, imported as fond.libs.<name>.
set(FOND_EXTENSIONS
    toml_bindings
    attrs
    maths
    utils
    files
    msgpak_bindings
)

foreach(name IN LISTS FOND_EXTENSIONS)
    pybind11_add_module(${name} MODULE "${name}.cpp")
    target_link_libraries(${name} PRIVATE fond::fond)
    if(APPLE)
        target_link_options(${name} PRIVATE "LINKER:-w")
        # Pluto's wheels are tuned for Pluto.
        if(_fond_host MATCHES "^pluto")
            target_compile_options(${name} PRIVATE -march=native)
        endif()
    elseif(UNIX)
        target_link_options(${name} PRIVATE "LINKER:--as-needed" "LINKER:-w")
    endif()
    if(_fond_editable)
        # $<1:...> keeps multi-config generators (Visual Studio) from adding a Release/ subdirectory.
        set_target_properties(${name} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "$<1:${CMAKE_CURRENT_SOURCE_DIR}>")
    endif()
    install(TARGETS ${name} LIBRARY DESTINATION fond/libs RUNTIME DESTINATION fond/libs)
endforeach()

# pyproject.toml's version is the only source. CMake splits it, so version_tuple stays real ints.
set(FOND_VERSION "${SKBUILD_PROJECT_VERSION_FULL}")
set(FOND_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}")
set(FOND_VERSION_MINOR "${PROJECT_VERSION_MINOR}")
set(FOND_VERSION_PATCH "${PROJECT_VERSION_PATCH}")
if(FOND_VERSION_PATCH STREQUAL "")
    set(FOND_VERSION_PATCH 0)
endif()
set(_fond_version_in "${CMAKE_CURRENT_SOURCE_DIR}/../_internal/version.py.in")
configure_file("${_fond_version_in}" "${CMAKE_CURRENT_BINARY_DIR}/version.py" @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/version.py" DESTINATION fond/_internal)
if(_fond_editable)
    configure_file("${_fond_version_in}" "${CMAKE_CURRENT_SOURCE_DIR}/../_internal/version.py" @ONLY)
endif()
