cmake_minimum_required(VERSION 3.15)
project(rnba-cpp-sim VERSION 0.1.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Default to an optimized build when the caller does not choose one.
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE Release)
endif()

# On Apple, ensure we use the correct SDK
if(APPLE)
    set(CMAKE_OSX_DEPLOYMENT_TARGET 10.15)
endif()

# Optional: Use Eigen for optimized sparse ops later
# find_package(Eigen3 REQUIRED)

# nlohmann/json — required for fast model loading (replaces regex-based parsing)
# Prefer a system install; fall back to FetchContent so isolated pip builds
# work on machines without the library preinstalled.
find_package(nlohmann_json 3.10 QUIET)
if(NOT nlohmann_json_FOUND)
    include(FetchContent)
    FetchContent_Declare(nlohmann_json
        URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz
        URL_HASH SHA256=d6c65aca6b1ed68e7a182f4757257b107ae403032760ed6ef121c9d55e81757d
    )
    FetchContent_MakeAvailable(nlohmann_json)
endif()

# Main library
add_library(rnba_sim STATIC
    src/model.cpp
    src/parser.cpp
    src/simulator.cpp
    src/output.cpp
    src/sparse_matrix.cpp
)

target_include_directories(rnba_sim PUBLIC include)
target_compile_options(rnba_sim PRIVATE
    $<$<CXX_COMPILER_ID:MSVC>:/W4>
    $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall -Wextra>
)
target_link_libraries(rnba_sim nlohmann_json::nlohmann_json)

# CLI executable
add_executable(rnba-sim src/main.cpp)
target_link_libraries(rnba-sim rnba_sim)

if(SKBUILD)
    # Building a Python wheel via scikit-build-core: ship the executable
    # inside the rnba package (located at runtime by rnba._sim).
    install(TARGETS rnba-sim DESTINATION rnba/_bin)
else()
    enable_testing()
    add_subdirectory(tests)

    install(TARGETS rnba-sim DESTINATION bin)
    install(DIRECTORY include/ DESTINATION include/rnba-sim)
endif()
