cmake_minimum_required(VERSION 3.22)

message(STATUS "Running with CMake version ${CMAKE_VERSION}")

project(metatensor-torch-test-cmake-project CXX)

option(USE_CMAKE_SUBDIRECTORY OFF)

if (USE_CMAKE_SUBDIRECTORY)
    message(STATUS "Using metatensor-torch with add_subdirectory")
    set(BUILD_METATENSOR_TORCH ON)
    # build metatensor_torch as part of this project
    add_subdirectory(../../../ metatensor)
    find_package(Torch)

    # load metatensor and metatensor-torch from the build path
    set(CMAKE_BUILD_RPATH "$<TARGET_FILE_DIR:torch>;$<TARGET_FILE_DIR:metatensor::shared>;$<TARGET_FILE_DIR:metatensor_torch>")
else()
    message(STATUS "Using metatensor-torch with find_package")
    # If building a dev version, we also need to update the REQUIRED_METATENSOR_VERSION
    # in the same way we update the metatensor-torch version
    include(../../cmake/dev-versions.cmake)
    set(REQUIRED_METATENSOR_TORCH_VERSION "0.8.3")
    create_development_version("${REQUIRED_METATENSOR_TORCH_VERSION}" METATENSOR_TORCH_FULL_VERSION "metatensor-torch-v")
    string(REGEX REPLACE "([0-9]*)\\.([0-9]*).*" "\\1.\\2" REQUIRED_METATENSOR_TORCH_VERSION ${METATENSOR_TORCH_FULL_VERSION})

    # find metatensor_torch with find_package
    find_package(metatensor_torch ${REQUIRED_METATENSOR_TORCH_VERSION} CONFIG REQUIRED)

    get_target_property(mts_torch_build_version metatensor_torch BUILD_VERSION)
    if (NOT ${mts_torch_build_version} STREQUAL ${METATENSOR_TORCH_FULL_VERSION})
        message(FATAL_ERROR "Invalid BUILD_VERSION for metatensor_torch, expected ${METATENSOR_TORCH_FULL_VERSION} but got ${mts_torch_build_version}")
    endif()
endif()

add_executable(torch-main src/main.cpp)
target_link_libraries(torch-main metatensor_torch)

enable_testing()
add_test(NAME torch-main COMMAND torch-main)

if(WIN32)
    # We need to set the path to allow access to metatensor.dll
    STRING(REPLACE ";" "\\;" PATH_STRING "$ENV{PATH}")
    set_tests_properties(torch-main PROPERTIES
        ENVIRONMENT "PATH=${PATH_STRING}\;$<TARGET_FILE_DIR:torch>\;$<TARGET_FILE_DIR:metatensor::shared>\;$<TARGET_FILE_DIR:metatensor_torch>"
    )
endif()
