add_library(mace_libtorch_backend SHARED
    mace_interface.cpp
)

set_target_properties(mace_libtorch_backend PROPERTIES
    OUTPUT_NAME "mace_libtorch_backend"
    LIBRARY_OUTPUT_DIRECTORY "${MACE_LIBTORCH_BACKEND_OUTPUT_DIR}"
    POSITION_INDEPENDENT_CODE ON
)

target_include_directories(mace_libtorch_backend
    PRIVATE
      ${TORCH_INCLUDE_DIRS}
)

target_compile_options(mace_libtorch_backend
    PRIVATE
      ${TORCH_CXX_FLAGS}
)

target_link_libraries(mace_libtorch_backend
    PRIVATE
      ${TORCH_LIBRARIES}
)

if(LIBTORCH_ROOT)
  set_target_properties(mace_libtorch_backend PROPERTIES
      BUILD_RPATH   "${LIBTORCH_ROOT}/lib"
      INSTALL_RPATH "${LIBTORCH_ROOT}/lib")
endif()

add_library(mace_fortran
    mace_interface_mod.f90
    mace_wrapper_mod.f90
    mace_backend_loader.c
)

set_target_properties(mace_fortran PROPERTIES
    Fortran_MODULE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/mod"
    POSITION_INDEPENDENT_CODE ON
)

target_include_directories(mace_fortran
    PUBLIC
      "${CMAKE_CURRENT_BINARY_DIR}/mod"
)

target_link_libraries(mace_fortran
    PUBLIC
      ${CMAKE_DL_LIBS}
)

target_compile_definitions(mace_fortran
    PRIVATE
      MACE_LIBTORCH_BACKEND_PATH="${MACE_LIBTORCH_BACKEND_OUTPUT_DIR}/libmace_libtorch_backend.so"
)
