# =============================================================================
# mlipcpp library sources
# =============================================================================

# Core components
set(MLIPCPP_CORE_SOURCES
    core/system.cpp
    core/neighbor_list.cpp
    core/gguf_loader.cpp
    core/backend.cpp
    core/ggml_utils.cpp
    core/timer.cpp
)

# Model implementations
set(MLIPCPP_MODEL_SOURCES
    models/model.cpp
    models/pet/pet.cpp
    models/pet/pet_batch.cpp
    models/pet/pet_layers.cpp
)

# I/O and utilities
set(MLIPCPP_IO_SOURCES
    io/xyz.cpp
)

# Runtime (graph interpreter)
set(MLIPCPP_RUNTIME_SOURCES
    runtime/graph_ir.cpp
    runtime/graph_interpreter.cpp
    runtime/graph_model.cpp
)

# Utilities
set(MLIPCPP_UTIL_SOURCES
    ggml_attention.cpp
)

# API bindings
set(MLIPCPP_API_SOURCES
    api/c/mlipcpp_api.cpp
    api/cpp/mlipcpp_cpp.cpp
)

# Combine all sources (prepend src/ directory)
set(MLIPCPP_SOURCES)
foreach(src IN LISTS
    MLIPCPP_CORE_SOURCES
    MLIPCPP_MODEL_SOURCES
    MLIPCPP_IO_SOURCES
    MLIPCPP_RUNTIME_SOURCES
    MLIPCPP_UTIL_SOURCES
    MLIPCPP_API_SOURCES)
    list(APPEND MLIPCPP_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/${src})
endforeach()

# Create library target
if(MLIPCPP_BUILD_SHARED)
    set(MLIPCPP_LIBRARY_TYPE SHARED)
else()
    set(MLIPCPP_LIBRARY_TYPE STATIC)
endif()

add_library(mlipcpp ${MLIPCPP_LIBRARY_TYPE} ${MLIPCPP_SOURCES})
add_library(mlipcpp::mlipcpp ALIAS mlipcpp)

target_include_directories(mlipcpp
    PUBLIC
        $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
    PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}
)

# The public C++ header (mlipcpp.hpp) uses std::span, so consumers need C++20
# too. Propagating it as a compile feature (rather than relying on the
# directory-scoped CMAKE_CXX_STANDARD) keeps it correct when mlipcpp is pulled
# in as a subproject of a project with an older default (e.g. GULP).
target_compile_features(mlipcpp PUBLIC cxx_std_20)

target_link_libraries(mlipcpp
    PRIVATE
        ggml
        nlohmann_json::nlohmann_json
        $<$<NOT:$<BOOL:${EMSCRIPTEN}>>:fmt::fmt>
)

# (IntelLLVM's -fp-model=precise is set for the whole tree, including ggml,
# in the top-level CMakeLists.txt.)
if(MSVC OR CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
    # MSVC, clang-cl and icx-cl: -Wall there means -Weverything.
    target_compile_options(mlipcpp PRIVATE /W4)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|IntelLLVM")
    target_compile_options(mlipcpp PRIVATE -Wall -Wextra -Wpedantic)
endif()

set_target_properties(mlipcpp PROPERTIES
    VERSION ${PROJECT_VERSION}
    SOVERSION ${PROJECT_VERSION_MAJOR}
    EXPORT_NAME mlipcpp
    # The C/C++ API has no export macros; export everything from a Windows DLL.
    WINDOWS_EXPORT_ALL_SYMBOLS ON
)

# =============================================================================
# Fortran bindings (optional)
# =============================================================================
if(MLIPCPP_BUILD_FORTRAN)
    add_library(mlipcpp_fortran ${MLIPCPP_LIBRARY_TYPE}
        api/fortran/mlipcpp.f90
    )
    add_library(mlipcpp::fortran ALIAS mlipcpp_fortran)

    target_link_libraries(mlipcpp_fortran PUBLIC mlipcpp)

    # PROJECT_BINARY_DIR (not CMAKE_BINARY_DIR) so the module directory stays
    # inside mlipcpp's own build tree when it is used as a subproject.
    set_target_properties(mlipcpp_fortran PROPERTIES
        Fortran_MODULE_DIRECTORY ${PROJECT_BINARY_DIR}/fortran_modules
        VERSION ${PROJECT_VERSION}
        SOVERSION ${PROJECT_VERSION_MAJOR}
        EXPORT_NAME fortran
    )

    target_include_directories(mlipcpp_fortran
        PUBLIC
            $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/fortran_modules>
            $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/mlipcpp/fortran>
    )

    # The bindings pass logical(c_bool) values to C `bool`. Intel (ifx/ifort)
    # and NVHPC encode .true. as -1 by default, which is not a valid C bool;
    # switch them to the C convention (.true. == 1). PUBLIC because callers
    # build the option structs and logical arguments in their own code.
    target_compile_options(mlipcpp_fortran PUBLIC
        "$<$<AND:$<COMPILE_LANG_AND_ID:Fortran,Intel,IntelLLVM>,$<NOT:$<PLATFORM_ID:Windows>>>:SHELL:-fpscomp logicals>"
        "$<$<AND:$<COMPILE_LANG_AND_ID:Fortran,Intel,IntelLLVM>,$<PLATFORM_ID:Windows>>:/fpscomp:logicals>"
        "$<$<COMPILE_LANG_AND_ID:Fortran,NVHPC,PGI>:-Munixlogical>"
    )
endif()
