# CMake glue for anneal-core.
#
# Mirrors the meson script: drives the Rust crate build through Corrosion
# (so cargo handles every transitive crate dep), regenerates the public
# C header via cbindgen, and exposes an `anneal-core::anneal-core` target
# that downstream `find_package`/`add_subdirectory` consumers can link
# against. The cargo-c-installed pkg-config file (`anneal-core.pc`) gives
# system-wide consumers an alternate, install-time entry point.
cmake_minimum_required(VERSION 3.22)

# The project version is [workspace.package].version in Cargo.toml.
# The first column-0 `version = "` line is that field. Member crates
# inherit it, so LIMIT_COUNT 1 stays on that one line.
set_property(
    DIRECTORY
    APPEND
    PROPERTY CMAKE_CONFIGURE_DEPENDS
    "${CMAKE_CURRENT_SOURCE_DIR}/Cargo.toml")
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/Cargo.toml" _anneal_version_line
     REGEX "^version = \""
     LIMIT_COUNT 1)
if(NOT _anneal_version_line)
    message(FATAL_ERROR "Cargo.toml has no package version")
endif()
string(REGEX REPLACE "^version = \"([^\"]+)\".*" "\\1" ANNEAL_VERSION "${_anneal_version_line}")
project(anneal-core VERSION ${ANNEAL_VERSION} LANGUAGES C CXX)

# Corrosion builds the Rust crate from Cargo.toml
include(FetchContent)
FetchContent_Declare(
    Corrosion
    GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
    GIT_TAG v0.5
)
FetchContent_MakeAvailable(Corrosion)

# The C ABI is the `capi` feature. Without it the imported library has
# no anneal_* exports and a C consumer cannot link.
corrosion_import_crate(MANIFEST_PATH ${CMAKE_CURRENT_SOURCE_DIR}/Cargo.toml
                       CRATES anneal-core
                       FEATURES capi)
# Corrosion >=0.5 mirrors cargo's post-1.79 dash-to-underscore rule for
# library target names, so the imported target is `anneal_core` even though
# the package name is `anneal-core`.
corrosion_set_env_vars(anneal_core "CARGO_CFG_TARGET_OS=$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")

# Generate the C header via cbindgen
find_program(CBINDGEN cbindgen REQUIRED)
set(ANNEAL_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/include/anneal-core.h)
add_custom_command(
    OUTPUT ${ANNEAL_HEADER}
    COMMAND ${CBINDGEN}
        -q
        --config ${CMAKE_CURRENT_SOURCE_DIR}/cbindgen.toml
        --crate anneal
        --output ${ANNEAL_HEADER}
        -- ${CMAKE_CURRENT_SOURCE_DIR}/src/lib.rs
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/cbindgen.toml
            ${CMAKE_CURRENT_SOURCE_DIR}/src/lib.rs
    COMMENT "Generating anneal-core.h via cbindgen"
)
add_custom_target(anneal-core-header DEPENDS ${ANNEAL_HEADER})
add_dependencies(anneal_core anneal-core-header)

# Exported target for consumers: anneal-core::anneal-core
add_library(anneal-core::anneal-core ALIAS anneal_core)

target_include_directories(anneal_core INTERFACE
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>
)

# Install header alongside the library. anneal-core.h includes
# dlpack/dlpack.h, so that header has to land on the same include prefix.
install(FILES ${ANNEAL_HEADER} DESTINATION include)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/anneal-version.h DESTINATION include)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/dlpack/dlpack.h DESTINATION include/dlpack)
