if(ACCELNET_PROVIDER STREQUAL "EXTERNAL")
  # Import only the C ABI. In particular, never import the native Fortran
  # targets/modules or NVHPC flags into a GNU/Intel-compiled PIMD.
  find_package(AccelNetC 1.1.1 CONFIG REQUIRED)
  set_property(TARGET AccelNet::TargetC PROPERTY IMPORTED_GLOBAL TRUE)
  include(CheckCSourceCompiles)
  set(CMAKE_REQUIRED_LIBRARIES AccelNet::TargetC)
  unset(PIMD_ACCELNET_C_API_FOUND CACHE)
  check_c_source_compiles("
    #include <accelnet_target.h>
    int main(void) {
      void *h = 0; double rc; int n; char error[512], symbol[17];
      accelnet_target_create_versioned(0, 0, -1, 0, 0, 0, &rc, &h, error);
      accelnet_target_create_n2p2(\"\", -1, 0, &n, &rc, &h, error);
      accelnet_target_get_species(h, 1, 17, symbol, error);
      accelnet_target_compute(h, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, error);
      accelnet_target_destroy(h);
      return 0;
    }" PIMD_ACCELNET_C_API_FOUND)
  if(NOT PIMD_ACCELNET_C_API_FOUND)
    message(FATAL_ERROR "AccelNetC lacks the required C API or its shared-library dependencies. Use AccelNet 1.1.1 or later with the TargetC installation; see lib/accelnet/README.md.")
  endif()
  return()
elseif(NOT ACCELNET_PROVIDER STREQUAL "FETCH")
  message(FATAL_ERROR "ACCELNET_PROVIDER must be FETCH or EXTERNAL")
endif()

include(FetchContent)

# Track upstream main at configure time. FetchContent keeps the checkout in
# the build tree and supports FETCHCONTENT_SOURCE_DIR_ACCELNET for local use.
FetchContent_Declare(accelnet
  GIT_REPOSITORY https://github.com/cometscome/AccelNet.git
  GIT_TAG origin/main
  GIT_SHALLOW TRUE
  SOURCE_SUBDIR AccelNetPredictor
)

# Embed only the inference libraries, without changing PIMD's test settings
# or the output directories through AccelNet's top-level project.
set(ACCELNET_PREDICTOR_BUILD_TESTING OFF)
set(ACCELNET_DESCRIPTORS_BUILD_TESTING OFF)
set(ACCELNET_DESCRIPTORS_BUILD_REFERENCE OFF)
# Directory-local settings: build the serial C ABI with PIMD's compiler.
# GPU builds use EXTERNAL so their compiler and runtime remain independent.
set(BUILD_SHARED_LIBS ON)
set(ACCELNET_BUILD_OPENMP_TARGET ON)
set(ACCELNET_TARGET_SERIAL ON)
FetchContent_MakeAvailable(accelnet)
if(NOT TARGET AccelNet::TargetC)
  message(FATAL_ERROR "This AccelNet checkout lacks AccelNet::TargetC. Use AccelNet 1.1.1 or later via FETCHCONTENT_SOURCE_DIR_ACCELNET or an EXTERNAL AccelNetC installation; see lib/accelnet/README.md.")
endif()

# CMake 3.20 has no FetchContent EXCLUDE_FROM_ALL argument. Set the directory
# property instead: linked libraries build, standalone tools/benchmarks do not.
set_property(DIRECTORY "${accelnet_SOURCE_DIR}/AccelNetPredictor"
  PROPERTY EXCLUDE_FROM_ALL TRUE)

find_package(Git REQUIRED)
execute_process(
  COMMAND "${GIT_EXECUTABLE}" rev-parse HEAD
  WORKING_DIRECTORY "${accelnet_SOURCE_DIR}"
  OUTPUT_VARIABLE _accelnet_revision
  OUTPUT_STRIP_TRAILING_WHITESPACE
  ERROR_QUIET
  RESULT_VARIABLE _accelnet_revision_status
)
if(_accelnet_revision_status EQUAL 0)
  message(STATUS "AccelNet source: ${accelnet_SOURCE_DIR} (${_accelnet_revision})")
else()
  message(STATUS "AccelNet source: ${accelnet_SOURCE_DIR}")
endif()
