# Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
#
# SPDX-License-Identifier: MIT

set(TEST_SRC
    hipMemGetAllocationGranularity.cc
    hipMemRetainAllocationHandle.cc)

if(HIP_PLATFORM MATCHES "nvidia")
  set(TEST_SRC
    ${TEST_SRC}
    hipMemMapArrayAsync.cc)
endif()

if(UNIX)
  if(HIP_PLATFORM MATCHES "amd")
    set(TEST_SRC
      ${TEST_SRC}
      hipMemGetHandleForAddressRange.cc
      hipMemCreateHostNuma.cc)
  endif()
  set(TEST_SRC
      ${TEST_SRC}
      hipMemExportToShareableHandle.cc
      hipMemImportFromShareableHandle.cc)
endif()

if(HIP_PLATFORM MATCHES "amd")
  set(TEST_SRC
      ${TEST_SRC}
      hipGetProcAddressVmmApis.cc)
endif()

set(TEST_SRC
    ${TEST_SRC}
    hipMemAddressFree.cc
    hipMemAddressReserve.cc
    hipMemCreate.cc
    hipMemSetGetAccess.cc
    hipMemGetAllocationPropertiesFromHandle.cc
    hipMemMap.cc
    hipMemRelease.cc
    hipMemUnmap.cc)

hip_add_exe_to_target(NAME VirtualMemoryManagementTest
  TEST_SRC ${TEST_SRC}
  TEST_TARGET_NAME build_tests COMMON_SHARED_SRC ${COMMON_SHARED_SRC})

if(UNIX AND HIP_PLATFORM MATCHES "amd")
  # hipMemCreateHostNuma.cc uses libnuma for NUMA node discovery and placement
  # verification (numa_available(), numa_max_node(), move_pages(), etc.).
  list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
  find_package(NUMA QUIET)
  if(NUMA_FOUND)
    set(NUMA "${NUMA_LIBRARIES}")
  else()
    find_library(NUMA NAMES numa REQUIRED)
    find_path(NUMA_INCLUDE_DIR NAMES numa.h)
  endif()
  if(NUMA_INCLUDE_DIR)
    target_include_directories(VirtualMemoryManagementTest PRIVATE ${NUMA_INCLUDE_DIR})
  endif()
  target_link_libraries(VirtualMemoryManagementTest ${NUMA})
endif()
