# Policy max is the newest CMake this tree is tested with (3.31.8).
# That makes CMP0110 NEW on the directory stack, so CTest keeps a spaced
# name such as "CuH2 Utils Test" as one test. CMake 3.16 through 3.18
# still configure. They have no CMP0110, so a spaced name stays unquoted.
cmake_minimum_required(VERSION 3.16...3.31.8)

if(POLICY CMP0110)
  cmake_policy(SET CMP0110 NEW)
endif()

project(
  rgpot
  # [[[cog import rgpot_version as v; cog.outl(f"  VERSION {v.VERSION}")]]]
  VERSION 3.8.0
  # [[[end]]]
  DESCRIPTION "Cpp core for potential energy surface functionality"
  HOMEPAGE_URL "https://github.com/OmniPotentRPC/rgpot"
  LANGUAGES C CXX)

# --- Top-level detection (CMake < 3.21 compat) ---
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
  set(RGPOT_IS_TOP_LEVEL TRUE)
else()
  set(RGPOT_IS_TOP_LEVEL FALSE)
endif()

# --- Options ---
option(RGPOT_RPC_CLIENT_ONLY "Build ONLY the RPC client bridge (lightweight)"
       OFF)

if(RGPOT_RPC_CLIENT_ONLY)
  set(CMAKE_CXX_STANDARD 17) # C++17 is sufficient for the RPC bridge
  option(RGPOT_WITH_RPC "Build with Cap'n Proto RPC support" ON)

  # Force disable heavy dependencies
  set(RGPOT_WITH_XTENSOR OFF)
  set(RGPOT_WITH_EIGEN OFF)
  set(RGPOT_WITH_CACHE OFF)
  set(RGPOT_PURE_LIB ON)
  set(RGPOT_BUILD_EXAMPLES OFF)
else()
  set(CMAKE_CXX_STANDARD 20)
  option(RGPOT_WITH_XTENSOR "Build with xtensor support" OFF)
  option(RGPOT_WITH_EIGEN "Build with Eigen3 support" OFF)
  option(RGPOT_PURE_LIB "Build as a pure library (no fmt dependency)" ON)
  option(RGPOT_WITH_CACHE "Build with RocksDB caching support" ON)
  option(RGPOT_WITH_RPC "Build with Cap'n Proto RPC support" OFF)
  option(RGPOT_BUILD_EXAMPLES "Build examples" ${RGPOT_IS_TOP_LEVEL})
  option(RGPOT_WITH_HIGHWAY "Google Highway runtime ISA dispatch" ON)
  if(WIN32)
    set(_rgpot_ipi_default OFF)
  else()
    set(_rgpot_ipi_default ON)
  endif()
  option(RGPOT_WITH_IPI "i-PI socket server and driver client (POSIX sockets)"
         ${_rgpot_ipi_default})
  if(RGPOT_WITH_IPI AND WIN32)
    message(
      FATAL_ERROR
        "RGPOT_WITH_IPI requires POSIX sockets and cannot be enabled on Windows"
    )
  endif()
endif()

# rgpot/rgpot_api.h exports the C ABI from a DLL when RGPOT_BUILDING is
# set. Every target here defines it, as the meson build does for the
# library archives and the tests.
if(WIN32)
  add_compile_definitions(RGPOT_BUILDING)
endif()

option(RGPOT_WITH_EAM_TABLE
       "EAM and Finnis-Sinclair potentials read from LAMMPS tables" OFF)
option(RGPOT_WITH_ORCA
       "ORCAPot: ORCA energy and gradient through the orca executable run as a child process (POSIX)"
       OFF)
if(RGPOT_WITH_ORCA AND WIN32)
  message(
    FATAL_ERROR
      "RGPOT_WITH_ORCA runs ORCA through fork and exec and cannot be enabled on Windows"
  )
endif()
option(RGPOT_WITH_CBE "CH5 2008 surface of the linked potlibc library" OFF)
option(RGPOT_WITH_MDI
       "Optional MDI engine frontend over rgpot_potential_t (builds rgpot-core with cargo; uses the official MDI Library at runtime)"
       OFF)
option(RGPOT_BUILD_TESTS "Build tests" ${RGPOT_IS_TOP_LEVEL})
option(RGPOT_WITH_CP2K
       "CP2KPot, the potserv CP2K backend and libcp2kc_rgpot against libcp2k (pkg-config libcp2k)" OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)

# librgpot's ELF soversion is kCxxSoversion in CxxLayout.hpp, the same
# constant Meson reads. The C ABI macros in rgpot.h are a different stamp
# and did not change. They have no shared library of their own.
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/CppCore/rgpot/abi/CxxLayout.hpp"
     _rgpot_cxx_layout_hpp)
string(REGEX MATCH "kCxxSoversion = ([0-9]+)" _rgpot_cxx_so_match
             "${_rgpot_cxx_layout_hpp}")
if(NOT CMAKE_MATCH_1)
  message(FATAL_ERROR "kCxxSoversion missing from CxxLayout.hpp")
endif()
set(RGPOT_CXX_SOVERSION "${CMAKE_MATCH_1}")

# --- Dependencies ---
include(FetchContent)

# 1. RPC Dependency
if(RGPOT_WITH_RPC)
  find_package(CapnProto REQUIRED)
endif()

# 1. Server/Physics Dependencies
if(NOT RGPOT_RPC_CLIENT_ONLY)
  include(CheckCXXSourceCompiles)
  check_cxx_source_compiles(
    "#include <atomic>\n#include <cstdint>\nint main() { std::uint64_t w = 0; std::atomic_ref<std::uint64_t>(w).fetch_or(1u, std::memory_order_relaxed); return 0; }"
    RGPOT_HAS_STD_ATOMIC_REF)
  if(NOT RGPOT_HAS_STD_ATOMIC_REF)
    check_cxx_source_compiles(
      "#include <cstdint>\n#if defined(_MSC_VER)\n#include <intrin.h>\n#endif\nint main() {\n  std::uint64_t w = 0;\n#if defined(__GNUC__) || defined(__clang__)\n  __atomic_fetch_or(&w, 1u, __ATOMIC_RELAXED);\n#elif defined(_MSC_VER)\n  _InterlockedOr64(reinterpret_cast<volatile long long *>(&w), 1LL);\n#else\n#error \"no atomic_fetch_or fallback\"\n#endif\n  return 0;\n}"
      RGPOT_HAS_ATOMIC_FETCH_OR_FALLBACK)
    if(NOT RGPOT_HAS_ATOMIC_FETCH_OR_FALLBACK)
      message(
        FATAL_ERROR
          "std::atomic_ref requires libc++ 19 or newer (or a compiler with __atomic_fetch_or)"
      )
    endif()
    message(
      STATUS
        "std::atomic_ref unavailable, using compiler atomic fetch_or fallback")
  endif()
  # Core builds provide every public Fortran potential. The RPC-only
  # bridge has no numerical kernels and does not need a Fortran compiler.
  enable_language(Fortran)
  if(RGPOT_WITH_CACHE)
    # Conda macOS runners sometimes fail FindThreads before RocksDB; set the
    # pthread hint explicitly so RocksDBConfig's find_dependency(Threads) works.
    set(CMAKE_THREAD_LIBS_INIT "-lpthread")
    set(CMAKE_HAVE_THREADS_LIBRARY 1)
    set(CMAKE_USE_PTHREADS_INIT 1)
    set(THREADS_PREFER_PTHREAD_FLAG ON)
    find_package(Threads REQUIRED)
    find_package(RocksDB REQUIRED)
    list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
    find_package(xxHash QUIET)
    list(POP_BACK CMAKE_MODULE_PATH)
    if(NOT xxHash_FOUND)
      message(
        STATUS "xxHash not found via find_package, fetching from source...")
      FetchContent_Declare(
        xxHash
        GIT_REPOSITORY https://github.com/Cyan4973/xxHash.git
        GIT_TAG v0.8.3)
      FetchContent_MakeAvailable(xxHash)
      if(NOT TARGET xxHash::xxhash)
        # The pinned release keeps its CMake project below the source root.
        set(XXHASH_BUILD_XXHSUM OFF)
        set(XXHASH_BUNDLED_MODE OFF)
        add_subdirectory("${xxhash_SOURCE_DIR}/cmake_unofficial"
                         "${xxhash_BINARY_DIR}")
      endif()
      set_target_properties(xxhash PROPERTIES POSITION_INDEPENDENT_CODE ON)
    endif()
  endif()

  if(NOT RGPOT_PURE_LIB)
    find_package(fmt REQUIRED)
  endif()

  if(RGPOT_WITH_XTENSOR)
    find_package(xtensor REQUIRED)
    find_package(xtensor-blas REQUIRED)
  endif()

  if(RGPOT_WITH_EIGEN)
    find_package(Eigen3 3.4.0 REQUIRED)
  endif()
endif()

# --- Build Targets ---

# The compiled schema is the vendored rgconf potential.capnp and the
# common.capnp it imports, as they are. The neighbour tests read it without
# the RPC stack.
if(RGPOT_WITH_RPC OR RGPOT_BUILD_TESTS)
  find_package(Python3 COMPONENTS Interpreter REQUIRED)
  set(RGPOT_SCHEMA_SRC_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}")
  set(RGPOT_POTENTIALS_SCHEMA
      "${CMAKE_CURRENT_SOURCE_DIR}/CppCore/rgpot/rpc/Potentials.capnp")
  set(RGPOT_COMMON_SCHEMA
      "${CMAKE_CURRENT_SOURCE_DIR}/CppCore/rgpot/rpc/common.capnp")
endif()

if(RGPOT_WITH_RPC)
  set(CAPNP_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/CppCore")
  file(MAKE_DIRECTORY "${CAPNP_OUTPUT_DIR}/rgpot/rpc")

  set(CAPNPC_SRC_PREFIX "${RGPOT_SCHEMA_SRC_PREFIX}")
  set(CAPNPC_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}")
  capnp_generate_cpp(CAPNP_SOURCES CAPNP_HEADERS "${RGPOT_POTENTIALS_SCHEMA}"
                     "${RGPOT_COMMON_SCHEMA}")

  # On Apple, static intermediate RPC libs avoid install_name / mixed
  # toolchain dylib issues when system clang links conda Cap'n Proto; the
  # full RPC/cache feature set is still exercised in executables/tests.
  if(APPLE)
    add_library(ptlrpc STATIC ${CAPNP_SOURCES})
  else()
    add_library(ptlrpc SHARED ${CAPNP_SOURCES})
  endif()
  target_link_libraries(ptlrpc PUBLIC CapnProto::capnp-rpc)
  # Always drive C++ links with the C++ linker.
  set_target_properties(ptlrpc PROPERTIES LINKER_LANGUAGE CXX)

  # Ensure the generated headers are in the include path
  target_include_directories(
    ptlrpc
    PUBLIC $<BUILD_INTERFACE:${CAPNP_OUTPUT_DIR}/rgpot/rpc>
           $<BUILD_INTERFACE:${CAPNP_OUTPUT_DIR}>
           $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
           $<INSTALL_INTERFACE:include/rgpot/rpc>
           $<INSTALL_INTERFACE:include>)

  # Client Bridge
  if(APPLE)
    add_library(rgpot_client_bridge STATIC CppCore/rgpot/rpc/pot_bridge.cc)
  else()
    add_library(rgpot_client_bridge SHARED CppCore/rgpot/rpc/pot_bridge.cc)
  endif()
  target_link_libraries(rgpot_client_bridge PUBLIC ptlrpc CapnProto::capnp-rpc)
  set_target_properties(rgpot_client_bridge PROPERTIES LINKER_LANGUAGE CXX)

  target_include_directories(
    rgpot_client_bridge
    PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/CppCore>
           $<INSTALL_INTERFACE:include>)
endif()

if(NOT RGPOT_RPC_CLIENT_ONLY)
  # Basic sources
  # The classical pots potserv can serve. Keep this in step with the arms in
  # rgpot/rpc/server.cpp: the server names them unconditionally, so a missing
  # source here surfaces as an undefined constructor at link time rather than
  # as a smaller build.
  set(RGPOT_SOURCES
      CppCore/rgpot/CalculatorGroup.cc
      CppCore/rgpot/PotHelpers.cc
      CppCore/rgpot/units.cc
      CppCore/rgpot/SumPot/SumPot.cc
      CppCore/rgpot/D2Pot/D2CpmdPot.cc
      CppCore/rgpot/D2Pot/D2Pot.cc
      CppCore/rgpot/abi/CxxLayout.cc
      CppCore/rgpot/plugin/PluginRegistry.cc
      CppCore/rgpot/plugin/PluginBridge.cc
      CppCore/rgpot/plugin/builtins/lj_plugin.cpp
      CppCore/rgpot/LennardJones/LJPot.cc
      CppCore/rgpot/LennardJones/LJClusterPot.cc
      CppCore/rgpot/LennardJones/LJClusterKernel.cc
      CppCore/rgpot/Morse/MorsePot.cc
      CppCore/rgpot/Models/ModelPots.cc
      CppCore/rgpot/Models/ModelCapi.cc
      CppCore/rgpot/ZBL/ZBLPot.cc
      CppCore/rgpot/simd/dispatch.cc
      CppCore/rgpot/simd/scalar_kernels.cc
      CppCore/rgpot/CuH2/cuh2Utils.cc
      CppCore/rgpot/fortran/FortranPots.cc
      CppCore/rgpot/SuttonChen/SuttonChenPot.cc
      CppCore/rgpot/ReducedPair/ReducedPair.cc
      CppCore/rgpot/SuttonChen/SuttonChenScales.cc
      CppCore/rgpot/nlist/HalfShellWalk.cc
      CppCore/rgpot/nlist/HalfShell.cc
      CppCore/rgpot/StillingerWeber/SWSi.cc
      CppCore/rgpot/Tersoff/TersoffSiC.cc
      CppCore/rgpot/MEAM/MEAMModel.cc
      CppCore/rgpot/MEAM/MEAMReader.cc
      CppCore/rgpot/MEAM/MEAMPot.cc)
  # The rest of Meson's librgpot: pots potserv does not serve, and the
  # MOPAC, XTB and external-engine frontends, which load their engines at
  # run time.
  set(_rgpot_emt_sources
      CppCore/rgpot/EMT/Asap/NeighborList.cpp
      CppCore/rgpot/EMT/Asap/GhostAtoms.cpp
      CppCore/rgpot/EMT/Asap/CellList.cpp
      CppCore/rgpot/EMT/Asap/Vec.cpp
      CppCore/rgpot/EMT/Asap/Atoms.cpp
      CppCore/rgpot/EMT/Asap/Exception.cpp
      CppCore/rgpot/EMT/Asap/EMTRasmussenParameterProvider.cpp
      CppCore/rgpot/EMT/Asap/EMTDefaultParameterProvider.cpp
      CppCore/rgpot/EMT/Asap/EMT.cpp
      CppCore/rgpot/EMT/Asap/stacktrace.c
      CppCore/rgpot/EMT/Asap/SuperCell.cpp
      CppCore/rgpot/EMT/EMTPot.cc)
  list(APPEND RGPOT_SOURCES
       CppCore/rgpot/Morse/cambridge_morse.c
       CppCore/rgpot/Gupta/gupta.c
       CppCore/rgpot/EAM/EAM.cpp
       CppCore/rgpot/EAM/EAMPot.cc
       CppCore/rgpot/MOPACPot/MOPACPot.cc
       CppCore/rgpot/ORCACPot/ORCACPot.cc
       CppCore/rgpot/XTBPot/XTBDlopen.cc
       CppCore/rgpot/ExternalPot/ExternalPot.cc
       CppCore/rgpot/ExternalPot/detail/LoadedIdentity.cc
       ${_rgpot_emt_sources})
  # Asap's short includes (Atoms.h, EMT.h) resolve in its own directory.
  set_source_files_properties(${_rgpot_emt_sources} PROPERTIES
    INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/CppCore/rgpot/EMT/Asap")
  set_source_files_properties(CppCore/rgpot/EAM/EAM.cpp CppCore/rgpot/EAM/EAMPot.cc
    PROPERTIES COMPILE_DEFINITIONS EAM_STANDALONE)

  if(RGPOT_WITH_HIGHWAY)
    list(APPEND RGPOT_SOURCES CppCore/rgpot/simd/hwy_kernels.cc)
  endif()

  # NWChemPot / CPMDPot are pure dlopen consumers of the split nwchemc / cpmdc
  # engines (parity with meson). Cap'n Proto params live in the generated schema,
  # so the frontends are only compiled when RGPOT_WITH_RPC enables ptlrpc.
  # makePotential (PotentialFactory.cc) reads PotentialConfig and builds
  # engine-library arms through EnginePot, so both come with the RPC stack too.
  if(RGPOT_WITH_RPC)
    list(APPEND RGPOT_SOURCES CppCore/rgpot/NWChemPot/NWChemPot.cc
         CppCore/rgpot/CPMDPot/CPMDPot.cc CppCore/rgpot/EnginePot/EnginePot.cc
         CppCore/rgpot/PotentialFactory.cc)
  endif()
  # CP2KPot dlopens the libcp2k shim. It is built only with the shim, so a
  # build without libcp2k compiles no CP2K code.
  if(RGPOT_WITH_CP2K)
    if(NOT RGPOT_WITH_RPC)
      message(FATAL_ERROR "RGPOT_WITH_CP2K requires RGPOT_WITH_RPC")
    endif()
    find_package(PkgConfig REQUIRED)
    pkg_check_modules(LIBCP2K REQUIRED IMPORTED_TARGET libcp2k)
    list(APPEND RGPOT_SOURCES CppCore/rgpot/CP2KPot/CP2KPot.cc
         CppCore/rgpot/CP2KPot/CP2KDeck.cc)
  endif()

  if(RGPOT_WITH_HIGHWAY)
    find_package(hwy 1.2 QUIET CONFIG)
    if(NOT TARGET hwy)
      set(HWY_ENABLE_TESTS OFF CACHE BOOL "" FORCE)
      set(HWY_ENABLE_EXAMPLES OFF CACHE BOOL "" FORCE)
      set(HWY_ENABLE_CONTRIB OFF CACHE BOOL "" FORCE)
      set(BUILD_TESTING OFF CACHE BOOL "" FORCE)
      FetchContent_Declare(
        highway
        URL https://github.com/google/highway/archive/refs/tags/1.2.0.tar.gz
        URL_HASH SHA256=7e0be78b8318e8bdbf6fa545d2ecb4c90f947df03f7aadc42c1967f019e63343)
      FetchContent_MakeAvailable(highway)
      if(TARGET hwy)
        set_target_properties(hwy PROPERTIES CXX_VISIBILITY_PRESET hidden)
        target_compile_definitions(
          hwy PRIVATE HWY_DISABLED_TARGETS=0xC0F800024041090)
      endif()
    endif()
  endif()

  if(RGPOT_WITH_IPI)
    list(APPEND RGPOT_SOURCES CppCore/rgpot/IPIPot/IPIWire.cc
         CppCore/rgpot/IPIPot/IPIPot.cc CppCore/rgpot/IPIPot/IPIDriver.cc)
  endif()

  if(RGPOT_WITH_EAM_TABLE)
    list(APPEND RGPOT_SOURCES CppCore/rgpot/EAMTable/EAMTablePot.cc
         CppCore/rgpot/EAMTable/EAMTableImages.cc)
  endif()

  if(RGPOT_WITH_ORCA)
    list(APPEND RGPOT_SOURCES CppCore/rgpot/ORCAPot/ORCAFiles.cc
         CppCore/rgpot/ORCAPot/ORCAPot.cc)
  endif()

  if(RGPOT_BUILD_TESTS)
    enable_testing()
  endif()
  find_package(Threads REQUIRED)
  add_library(rgpot_nlist STATIC
      CppCore/rgpot/Slices.cc
      CppCore/rgpot/nlist/NeighbourList.cc
      CppCore/rgpot/nlist/Neighbours.cc
      CppCore/rgpot/nlist/MinimumImage.cpp
      CppCore/rgpot/simd/exact_fma_hw.cc
      CppCore/rgpot/simd/exp.cc)
  target_include_directories(rgpot_nlist PUBLIC
      $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/CppCore>)
  target_link_libraries(rgpot_nlist PUBLIC Threads::Threads)
  find_package(OpenMP)
  if(OpenMP_CXX_FOUND)
    target_link_libraries(rgpot_nlist PUBLIC OpenMP::OpenMP_CXX)
  endif()
  set_target_properties(rgpot_nlist PROPERTIES POSITION_INDEPENDENT_CODE ON)

  file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/third_party/vesin/VERSION"
       _rgpot_vesin_versions REGEX "^[0-9]+\\.[0-9]+\\.[0-9]+$")
  list(LENGTH _rgpot_vesin_versions _rgpot_vesin_version_count)
  if(NOT _rgpot_vesin_version_count EQUAL 1)
    message(
      FATAL_ERROR
        "third_party/vesin/VERSION must contain exactly one version")
  endif()
  list(GET _rgpot_vesin_versions 0 RGPOT_VESIN_VERSION)
  file(STRINGS
       "${CMAKE_CURRENT_SOURCE_DIR}/third_party/vesin/vesin-single-build.cpp"
       _rgpot_vesin_source_version REGEX "^// vesin version: ")
  if(NOT _rgpot_vesin_source_version STREQUAL
     "// vesin version: ${RGPOT_VESIN_VERSION}")
    message(
      FATAL_ERROR
        "vendored vesin source does not match VERSION ${RGPOT_VESIN_VERSION}")
  endif()

  add_library(rgpot_vesin STATIC third_party/vesin/vesin-single-build.cpp)
  add_library(rgpot::vesin ALIAS rgpot_vesin)
  set_target_properties(rgpot_vesin PROPERTIES
    EXPORT_NAME vesin
    POSITION_INDEPENDENT_CODE ON
    CXX_VISIBILITY_PRESET hidden)
  target_include_directories(rgpot_vesin
    PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/third_party/vesin/include>
           $<INSTALL_INTERFACE:include/rgpot/vesin>)
  target_link_libraries(rgpot_vesin PRIVATE Threads::Threads)
  # The sources meson compiles without contraction: rgpot_core,
  # rgpot_minimage, rgpot_exact_fma, and the lennard_jones, morse, zbl,
  # gupta, sutton_chen, tersoff_sic, reduced_pair and eam_cell libraries.
  # Tests compare their bits with unfused references, and arm64 contracts
  # a*b+c by default, so a source missing here fails there and nowhere else.
  # tools/check_test_parity.sh checks this list against meson.
  set(_rgpot_unfused_sources
      CppCore/rgpot/CalculatorGroup.cc
      CppCore/rgpot/PotHelpers.cc
      CppCore/rgpot/PotentialCache.cc
      CppCore/rgpot/Slices.cc
      CppCore/rgpot/units.cc
      CppCore/rgpot/SumPot/SumPot.cc
      CppCore/rgpot/D2Pot/D2CpmdPot.cc
      CppCore/rgpot/D2Pot/D2Pot.cc
      CppCore/rgpot/abi/CxxLayout.cc
      CppCore/rgpot/plugin/PluginRegistry.cc
      CppCore/rgpot/plugin/PluginBridge.cc
      CppCore/rgpot/plugin/builtins/lj_plugin.cpp
      CppCore/rgpot/nlist/MinimumImage.cpp
      CppCore/rgpot/simd/exact_fma_hw.cc
      CppCore/rgpot/simd/exp.cc
      CppCore/rgpot/simd/dispatch.cc
      CppCore/rgpot/simd/scalar_kernels.cc
      CppCore/rgpot/simd/hwy_kernels.cc
      CppCore/rgpot/LennardJones/LJPot.cc
      CppCore/rgpot/LennardJones/LJClusterPot.cc
      CppCore/rgpot/LennardJones/LJClusterKernel.cc
      CppCore/rgpot/Morse/MorsePot.cc
      CppCore/rgpot/Morse/cambridge_morse.c
      CppCore/rgpot/ZBL/ZBLPot.cc
      CppCore/rgpot/Gupta/gupta.c
      CppCore/rgpot/SuttonChen/SuttonChenPot.cc
      CppCore/rgpot/SuttonChen/SuttonChenScales.cc
      CppCore/rgpot/Tersoff/TersoffSiC.cc
      CppCore/rgpot/MEAM/MEAMModel.cc
      CppCore/rgpot/MEAM/MEAMReader.cc
      CppCore/rgpot/MEAM/MEAMPot.cc
      CppCore/rgpot/ReducedPair/ReducedPair.cc
      CppCore/rgpot/EAM/EAM.cpp
      CppCore/rgpot/EAM/EAMPot.cc)
  if(MSVC)
    set_source_files_properties(${_rgpot_unfused_sources}
      PROPERTIES COMPILE_OPTIONS "/fp:precise")
  else()
    set_source_files_properties(${_rgpot_unfused_sources}
      PROPERTIES COMPILE_OPTIONS "-ffp-contract=off")
    set_property(SOURCE CppCore/rgpot/ReducedPair/ReducedPair.cc
      APPEND PROPERTY COMPILE_OPTIONS -fno-fast-math)
    # Value-safe: lets the exp clamps if-convert and sqrt use sqrtpd, so
    # the exp and Morse row loops vectorise, also at -O2. Each flag is kept
    # only where the compiler accepts it (clang has no -fvect-cost-model),
    # as the Meson build does with get_supported_arguments.
    include(CheckCXXCompilerFlag)
    set(RGPOT_VECTOR_LOOP_FLAGS)
    foreach(flag -fno-trapping-math -fno-math-errno -ftree-vectorize
                 -fvect-cost-model=dynamic)
      string(MAKE_C_IDENTIFIER "RGPOT_HAS${flag}" flag_var)
      check_cxx_compiler_flag(${flag} ${flag_var})
      if(${flag_var})
        list(APPEND RGPOT_VECTOR_LOOP_FLAGS ${flag})
      endif()
    endforeach()
    set_property(SOURCE CppCore/rgpot/simd/exp.cc CppCore/rgpot/Morse/MorsePot.cc
      APPEND PROPERTY COMPILE_OPTIONS ${RGPOT_VECTOR_LOOP_FLAGS})
  endif()
  add_subdirectory(CppCore/rgpot/fortran)
  if(RGPOT_WITH_CBE)
    find_package(PkgConfig REQUIRED)
    pkg_check_modules(POTLIBC REQUIRED potlibc)
    find_path(POTLIBC_HEADER_DIR NAMES potlibc.h HINTS ${POTLIBC_INCLUDE_DIRS}
              NO_DEFAULT_PATH REQUIRED)
    find_library(POTLIBC_LIBRARY NAMES potlibc HINTS ${POTLIBC_LIBRARY_DIRS}
                 NO_DEFAULT_PATH REQUIRED)
    list(APPEND RGPOT_SOURCES CppCore/rgpot/CBEPot/CBEPot.cc)
  endif()
  add_library(rgpot ${RGPOT_SOURCES})
  # A static rgpot passes rgpot_nlist's OpenMP::OpenMP_CXX to its consumers
  # as a link-only dependency, so the installed package has to define it.
  get_target_property(_rgpot_type rgpot TYPE)
  set(RGPOT_CONFIG_FIND_OPENMP OFF)
  if(OpenMP_CXX_FOUND AND _rgpot_type STREQUAL "STATIC_LIBRARY")
    set(RGPOT_CONFIG_FIND_OPENMP ON)
  endif()
  target_compile_definitions(rgpot PRIVATE RGPOT_MODEL_BUILD)
  if(WIN32 AND _rgpot_type STREQUAL "STATIC_LIBRARY")
    target_compile_definitions(rgpot INTERFACE
      $<BUILD_INTERFACE:RGPOT_STATIC>
      $<INSTALL_INTERFACE:RGPOT_STATIC>)
  endif()
  if(RGPOT_WITH_HIGHWAY)
    target_link_libraries(rgpot PRIVATE hwy)
    target_compile_definitions(rgpot PRIVATE RGPOT_WITH_HIGHWAY)
  endif()
  set_target_properties(rgpot PROPERTIES LINKER_LANGUAGE CXX
                                        POSITION_INDEPENDENT_CODE ON
                                        VERSION ${PROJECT_VERSION}
                                        SOVERSION ${RGPOT_CXX_SOVERSION})
  target_link_libraries(rgpot PRIVATE rgpot_fortran_kernels rgpot_nlist Threads::Threads)
  if(TARGET rgpot_cuh2)
    # A static umbrella would otherwise pull rgpot_nlist into this plugin
    # and define a second Neighbours generation counter. Link the built
    # file: a shared umbrella records a dependency on librgpot, and a
    # static umbrella leaves the neighbour symbols undefined so they bind
    # to the process that dlopens the plugin.
    add_dependencies(rgpot_cuh2 rgpot)
    target_link_libraries(rgpot_cuh2 PRIVATE "$<TARGET_FILE:rgpot>")
  endif()
  # A static librgpot does not absorb rgpot_nlist, so the ABI check reads
  # that archive too. On Linux a shared librgpot names every neighbour
  # export as undefined, so a link that drops unreferenced sections still
  # keeps that C ABI. The session calls rgpot_nlist_build, which pulls the
  # object that defines those exports.
  get_target_property(_rgpot_library_type rgpot TYPE)
  if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND _rgpot_library_type STREQUAL "SHARED_LIBRARY")
    foreach(_rgpot_nlist_sym IN ITEMS
            rgpot_nlist_abi_version rgpot_nlist_build rgpot_nlist_builder_close
            rgpot_nlist_builder_open rgpot_nlist_builder_update
            rgpot_nlist_build_systems rgpot_nlist_fortran_build
            rgpot_nlist_fortran_close rgpot_nlist_fortran_open
            rgpot_nlist_request_init rgpot_nlist_strerror rgpot_nlist_walk_close
            rgpot_nlist_walk_open rgpot_nlist_walk_pull)
      target_link_options(rgpot PRIVATE "LINKER:--undefined=${_rgpot_nlist_sym}")
    endforeach()
  endif()
  if(RGPOT_WITH_CBE)
    target_include_directories(rgpot PRIVATE ${POTLIBC_HEADER_DIR})
    target_link_libraries(rgpot PRIVATE ${POTLIBC_LIBRARY})
    target_compile_definitions(rgpot PUBLIC RGPOT_HAS_CBE)
  endif()
  if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    target_link_options(
      rgpot PRIVATE
      "LINKER:--exclude-libs,$<TARGET_FILE_NAME:rgpot_fortran_kernels>")
    get_target_property(_rgpot_library_type rgpot TYPE)
    if(_rgpot_library_type STREQUAL "STATIC_LIBRARY")
      # Static consumers perform the final link and must hide the kernel
      # archive's internal Fortran entries in their own shared objects.
      target_link_options(
        rgpot INTERFACE
        "$<BUILD_INTERFACE:LINKER:--exclude-libs,$<TARGET_FILE_NAME:rgpot_fortran_kernels>>"
        "$<INSTALL_INTERFACE:LINKER:--exclude-libs,$<TARGET_FILE_NAME:rgpot::rgpot_fortran_kernels>>")
    endif()
  endif()

  # librgpot never links MPI; it loads librgpot_mpi with dlopen when a host
  # asks for calculator groups (CppCore/rgpot/calculator_mpi_abi.h).
  target_link_libraries(rgpot PRIVATE ${CMAKE_DL_LIBS})
  find_package(MPI QUIET COMPONENTS CXX)
  set(RGPOT_HAVE_MPI OFF)
  if(MPI_FOUND)
    # A system MPI wrapper can be found without headers the active
    # compiler can include; only a configuration that compiles counts.
    include(CheckCXXSourceCompiles)
    set(CMAKE_REQUIRED_LIBRARIES MPI::MPI_CXX)
    check_cxx_source_compiles(
      "#include <mpi.h>
       int main(int argc, char **argv) { MPI_Init(&argc, &argv); return MPI_Finalize(); }"
      RGPOT_MPI_COMPILES)
    unset(CMAKE_REQUIRED_LIBRARIES)
    set(RGPOT_HAVE_MPI ${RGPOT_MPI_COMPILES})
  endif()
  if(RGPOT_HAVE_MPI)
    add_library(rgpot_mpi SHARED CppCore/rgpot/CalculatorGroupMpi.cc)
    target_include_directories(rgpot_mpi
                               PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/CppCore)
    target_link_libraries(rgpot_mpi PRIVATE MPI::MPI_CXX)
    # soversion 3 is librgpot_mpi's own SONAME. RGPOT_MPI_ABI_VERSION is
    # still 1, so this name does not move with librgpot's kCxxSoversion.
    set_target_properties(rgpot_mpi PROPERTIES CXX_VISIBILITY_PRESET hidden
                                               SOVERSION 3)
  endif()

  if(RGPOT_WITH_CACHE)
    target_sources(rgpot PRIVATE CppCore/rgpot/PotentialCache.cc)
    # A static RocksDB can bring jemalloc onto each executable's own link
    # line, where its malloc wins over libc's, and its free then recurses
    # through __tls_get_addr once a library with TLS (libmpi) is dlopen'd.
    # The shared library, which meson's pkg-config lookup links, keeps
    # jemalloc behind libc.
    if(TARGET RocksDB::rocksdb-shared)
      target_link_libraries(rgpot PUBLIC RocksDB::rocksdb-shared xxHash::xxhash)
    else()
      target_link_libraries(rgpot PUBLIC RocksDB::rocksdb xxHash::xxhash)
    endif()
    target_compile_definitions(rgpot PUBLIC RGPOT_HAS_CACHE)
  endif()

  if(RGPOT_WITH_IPI)
    target_compile_definitions(rgpot PUBLIC RGPOT_HAS_IPI)
  endif()

  if(RGPOT_WITH_EAM_TABLE)
    target_compile_definitions(rgpot PUBLIC RGPOT_HAS_EAM_TABLE)
  endif()

  if(RGPOT_WITH_ORCA)
    target_compile_definitions(rgpot PUBLIC RGPOT_HAS_ORCA)
  endif()

  if(RGPOT_WITH_RPC)
    target_link_libraries(rgpot PUBLIC ptlrpc)
    # ExternalPot.cc includes the generated Potentials.capnp.h. Listing the
    # generated headers as sources orders them before its compile in a
    # parallel build, as ptlrpc_hdr_dep does for Meson.
    target_sources(rgpot PRIVATE ${CAPNP_HEADERS})
    target_compile_definitions(rgpot PUBLIC RGPOT_HAS_NWCHEM_FRONTEND
                                            RGPOT_HAS_CPMD_FRONTEND)
    # DynLib / dlopen for libnwchemc.so / libcpmdc.so at runtime
    if(UNIX AND NOT APPLE)
      target_link_libraries(rgpot PUBLIC ${CMAKE_DL_LIBS})
    elseif(UNIX)
      target_link_libraries(rgpot PUBLIC dl)
    endif()
  endif()

  if(RGPOT_WITH_RPC)
    set(_rgpot_lammpc_profile 1)
  else()
    set(_rgpot_lammpc_profile 0)
  endif()
  set_source_files_properties(CppCore/rgpot/ExternalPot/ExternalPot.cc
    PROPERTIES COMPILE_DEFINITIONS
    RGPOT_HAS_LAMMPC_PROFILE=${_rgpot_lammpc_profile})

  add_library(rgpot::rgpot ALIAS rgpot)

  # The libcp2k shim is the only target linking libcp2k and MPI; CP2KPot
  # dlopens it.
  if(RGPOT_WITH_CP2K)
    target_compile_definitions(rgpot PUBLIC RGPOT_HAS_CP2K)
    find_package(MPI REQUIRED COMPONENTS C)
    add_library(cp2kc_rgpot SHARED CppCore/rgpot/CP2KPot/cp2kc_shim.c)
    target_include_directories(cp2kc_rgpot
                               PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/CppCore/rgpot/CP2KPot)
    target_link_libraries(cp2kc_rgpot PRIVATE PkgConfig::LIBCP2K MPI::MPI_C)
    set_target_properties(cp2kc_rgpot PROPERTIES BUILD_RPATH "${LIBCP2K_LIBDIR}"
                                                 INSTALL_RPATH "${LIBCP2K_LIBDIR}")
    install(TARGETS cp2kc_rgpot LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
  endif()

  target_include_directories(
    rgpot PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/CppCore>
                 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
                 $<INSTALL_INTERFACE:include>)

  if(UNIX AND NOT APPLE)
    target_link_libraries(rgpot PUBLIC m)
  endif()

  if(NOT RGPOT_PURE_LIB)
    target_link_libraries(rgpot PUBLIC fmt)
    target_compile_definitions(rgpot PUBLIC NOT_PURE_LIB)
  endif()

  if(RGPOT_WITH_XTENSOR)
    target_link_libraries(rgpot PUBLIC xtensor xtensor-blas)
    target_compile_definitions(rgpot PUBLIC WITH_XTENSOR)
  endif()

  if(RGPOT_WITH_EIGEN)
    target_link_libraries(rgpot PUBLIC Eigen3::Eigen)
  endif()

  if(MSVC)
    target_compile_options(rgpot PRIVATE /W4)
  else()
    target_compile_options(rgpot PRIVATE -Wall -Wextra -Wpedantic)
  endif()
endif()

# The MPI hosts are explicit executables; the serial server and core library
# do not acquire an MPI load dependency.
if(RGPOT_WITH_RPC AND NOT RGPOT_RPC_CLIENT_ONLY)
  add_executable(potserv CppCore/rgpot/rpc/server.cpp)
  target_link_libraries(potserv PRIVATE rgpot::rgpot ptlrpc CapnProto::capnp-rpc)
  if(RGPOT_HAVE_MPI)
    add_executable(potserv_mpi CppCore/rgpot/rpc/server.cpp)
    target_compile_definitions(potserv_mpi PRIVATE RGPOT_POTSERV_MPI)
    target_link_libraries(potserv_mpi PRIVATE rgpot::rgpot ptlrpc
                                            CapnProto::capnp-rpc MPI::MPI_CXX)
    add_executable(nwchem_mpi_force_host CppCore/tools/nwchem_mpi_force_host.cc)
    target_link_libraries(nwchem_mpi_force_host PRIVATE rgpot::rgpot MPI::MPI_CXX)
  endif()
endif()

# --- MDI frontend ---
# rgpot-core is built with cargo only for this option, with the Cargo features
# meson passes for its with_rust_core and with_mdi options. rgpot-mdi and
# rgpot-eval follow meson's engines/: they are not built on Windows or in the
# client-only build.
if(RGPOT_WITH_MDI)
  find_program(RGPOT_CARGO cargo)
  if(NOT RGPOT_CARGO)
    message(FATAL_ERROR "RGPOT_WITH_MDI needs cargo to build rgpot-core")
  endif()
  find_package(Python3 COMPONENTS Interpreter REQUIRED)
  find_package(Threads REQUIRED)

  set(_rgpot_cargo_features mdi)
  if(RGPOT_WITH_RPC)
    list(APPEND _rgpot_cargo_features rpc)
  endif()
  if(RGPOT_WITH_CACHE)
    list(APPEND _rgpot_cargo_features cache)
  endif()
  list(JOIN _rgpot_cargo_features "," _rgpot_cargo_feature_list)
  set(_rgpot_cargo_profile debug)
  if(CMAKE_BUILD_TYPE STREQUAL "Release")
    set(_rgpot_cargo_profile release)
  endif()
  if(MSVC)
    set(_rgpot_rust_archive rgpot_core.lib)
  else()
    set(_rgpot_rust_archive librgpot_core.a)
  endif()
  set(_rgpot_rust_dir "${CMAKE_CURRENT_BINARY_DIR}/rust-core")
  set(_rgpot_rust_built "${_rgpot_rust_dir}/${_rgpot_rust_archive}")
  file(MAKE_DIRECTORY "${_rgpot_rust_dir}")
  add_custom_target(
    rgpot_rust_core_build
    COMMAND
      "${Python3_EXECUTABLE}"
      "${CMAKE_CURRENT_SOURCE_DIR}/scripts/build_rust_core.py" --cargo
      "${RGPOT_CARGO}" --manifest
      "${CMAKE_CURRENT_SOURCE_DIR}/rgpot-core/Cargo.toml" --target-dir
      "${_rgpot_rust_dir}/cargo-target" --profile ${_rgpot_cargo_profile}
      --artifact ${_rgpot_rust_archive} --features
      "${_rgpot_cargo_feature_list}" "${_rgpot_rust_built}"
      "${_rgpot_rust_dir}/rgpot_rust_built.h"
    BYPRODUCTS "${_rgpot_rust_built}" "${_rgpot_rust_dir}/rgpot_rust_built.h"
    COMMENT "Building rgpot-core with cargo (features: ${_rgpot_cargo_feature_list})"
    USES_TERMINAL)
  add_library(rgpot_rust_core STATIC IMPORTED GLOBAL)
  set_target_properties(
    rgpot_rust_core
    PROPERTIES IMPORTED_LOCATION "${_rgpot_rust_built}"
               INTERFACE_INCLUDE_DIRECTORIES
               "${CMAKE_CURRENT_SOURCE_DIR}/rgpot-core/include"
               INTERFACE_LINK_LIBRARIES "Threads::Threads;${CMAKE_DL_LIBS}")
  add_dependencies(rgpot_rust_core rgpot_rust_core_build)

  if(RGPOT_RPC_CLIENT_ONLY OR WIN32)
    message(
      STATUS
        "rgpot-mdi is not built: engines/ is skipped on Windows and in the client-only build")
  else()
    set(_rgpot_engines_dir "${CMAKE_CURRENT_SOURCE_DIR}/engines")
    add_library(rgpot_pot_spec STATIC "${_rgpot_engines_dir}/pot_spec.cpp")
    target_include_directories(
      rgpot_pot_spec PUBLIC "${_rgpot_engines_dir}"
                            "${CMAKE_CURRENT_SOURCE_DIR}/rgpot-core/include")
    target_compile_definitions(rgpot_pot_spec PUBLIC RGPOT_HAS_FORTRAN_POTS)
    target_link_libraries(rgpot_pot_spec PUBLIC rgpot::rgpot)
    set_target_properties(rgpot_pot_spec PROPERTIES POSITION_INDEPENDENT_CODE ON)

    add_executable(rgpot-mdi "${_rgpot_engines_dir}/rgpot_mdi.cpp")
    target_link_libraries(rgpot-mdi PRIVATE rgpot_pot_spec rgpot_rust_core
                                            rgpot_vesin ${CMAKE_DL_LIBS})
    add_executable(rgpot-eval "${_rgpot_engines_dir}/rgpot_eval.cpp")
    target_link_libraries(rgpot-eval PRIVATE rgpot_pot_spec rgpot_vesin)
    foreach(_rgpot_engine rgpot-mdi rgpot-eval)
      set_target_properties(${_rgpot_engine} PROPERTIES LINKER_LANGUAGE CXX)
      if(OpenMP_CXX_FOUND)
        target_link_libraries(${_rgpot_engine} PRIVATE OpenMP::OpenMP_CXX)
      endif()
      if(MSVC)
        target_compile_options(${_rgpot_engine} PRIVATE /W4)
      else()
        target_compile_options(${_rgpot_engine} PRIVATE -Wall -Wextra -Wpedantic)
      endif()
    endforeach()
  endif()
endif()

# --- Installation ---
include(GNUInstallDirs)

if(RGPOT_IS_TOP_LEVEL)
  if(TARGET potserv)
    install(TARGETS potserv RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
  endif()
  if(TARGET potserv_mpi)
    install(TARGETS potserv_mpi nwchem_mpi_force_host
            RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
  endif()
  if(TARGET rgpot-mdi)
    install(TARGETS rgpot-mdi RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
  endif()
  if(RGPOT_WITH_RPC)
    install(
      TARGETS ptlrpc rgpot_client_bridge
      EXPORT rgpotTargets
      LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
      ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
      RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
    # Install the bridge header in the correct directory structure
    install(FILES CppCore/rgpot/rpc/pot_bridge.h
            DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/rgpot/rpc)
    install(FILES ${CAPNP_HEADERS} "${RGPOT_POTENTIALS_SCHEMA}"
                  "${RGPOT_COMMON_SCHEMA}"
            DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/rgpot/rpc)
  endif()

  if(NOT RGPOT_RPC_CLIENT_ONLY)
    install(
      TARGETS rgpot rgpot_fortran_kernels rgpot_nlist rgpot_vesin
      EXPORT rgpotTargets
      LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
      ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
      RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
      INCLUDES
      DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
    install(FILES third_party/vesin/vesin.h
            DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/rgpot/vesin)
    install(TARGETS rgpot_cuh2
            LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
            RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
    if(TARGET rgpot_mpi)
      install(TARGETS rgpot_mpi
              LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
              RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
    endif()
    install(FILES CppCore/rgpot/calculator_mpi_abi.h include/rgpot/plugin.h
            DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/rgpot)
    install(
      DIRECTORY CppCore/rgpot
      DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
      FILES_MATCHING
      PATTERN "ExternalPot/detail" EXCLUDE
      PATTERN "Morse/detail" EXCLUDE
      PATTERN "SuttonChen/detail" EXCLUDE
      PATTERN "*.hpp"
      PATTERN "*.h")
  endif()

  install(
    EXPORT rgpotTargets
    FILE rgpotTargets.cmake
    NAMESPACE rgpot::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/rgpot)

  include(CMakePackageConfigHelpers)
  configure_package_config_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/cmake/rgpotConfig.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/rgpotConfig.cmake"
    INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/rgpot)
  write_basic_package_version_file(
    "${CMAKE_CURRENT_BINARY_DIR}/rgpotConfigVersion.cmake"
    COMPATIBILITY SameMajorVersion)
  install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindxxHash.cmake"
                "${CMAKE_CURRENT_BINARY_DIR}/rgpotConfig.cmake"
                "${CMAKE_CURRENT_BINARY_DIR}/rgpotConfigVersion.cmake"
          DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/rgpot)
endif()

# --- Tests ---
# CppCore/tests/registry.txt lists them; tests.cmake registers them.
if(RGPOT_BUILD_TESTS)
  include("${CMAKE_CURRENT_SOURCE_DIR}/CppCore/tests/tests.cmake")
endif()
