cmake_minimum_required(VERSION 3.8)
if(POLICY CMP0104)
  # DTK's nvcc-compatible wrapper owns target selection; an empty CMake CUDA
  # architecture list is valid for this compatibility build.
  cmake_policy(SET CMP0104 OLD)
endif()
project(qcu LANGUAGES C CXX CUDA)
# DTK 23.10's nvcc-compatible wrapper rejects CMake's --options-file syntax.
set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_INCLUDES 0)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CUDA_STANDARD 11)
message(STATUS "PATH is: $ENV{PATH}")
message(STATUS "LD_LIBRARY_PATH is: $ENV{LD_LIBRARY_PATH}")

# ---------------------------------------------------------------------------
# MPI provenance.
#
# On AC9R a single shell carries TWO unrelated OpenMPI trees on
# LD_LIBRARY_PATH: the system hpcx 4.x build
# (/opt/hpc/software/mpi/hpcx/v2.11.0/gcc-7.3.1) and the conda-forge OpenMPI
# 5.x build that python's mpi4py and QUDA link against
# ($HOME/miniforge3/lib/libmpi.so.40).  `$HOME/miniforge3/bin/mpicc` is an
# opal_wrapper already configured for *hpcx*, so letting find_package(MPI)
# probe it silently binds libqcu.so to the wrong libmpi.so.40.  The two carry
# the same SONAME, so the loader picks whichever object is already mapped and
# the other one's symbols never exist; mpi4py then dies at import with
#
#   undefined symbol: ompi_mpi_short_float
#
# (hpcx's libmpi does not export the OpenMPI-5 MPI_SHORT_FLOAT datum).
#
# Setting QCU_MPI_ROOT (as a -D option or an environment variable) pins the
# include directory and the library to one tree and skips the probe
# entirely.  Leaving it unset keeps the historic find_package(MPI)
# behaviour, so the plain CUDA build is unchanged.
# ---------------------------------------------------------------------------
set(_QCU_MPI_ROOT "${QCU_MPI_ROOT}")
if("${_QCU_MPI_ROOT}" STREQUAL "" AND DEFINED ENV{QCU_MPI_ROOT})
  set(_QCU_MPI_ROOT "$ENV{QCU_MPI_ROOT}")
endif()
if(NOT "${_QCU_MPI_ROOT}" STREQUAL "")
  find_path(QCU_MPI_INCLUDE_DIR mpi.h
            HINTS "${_QCU_MPI_ROOT}" PATH_SUFFIXES include NO_DEFAULT_PATH)
  find_library(QCU_MPI_LIBRARY NAMES mpi
               HINTS "${_QCU_MPI_ROOT}" PATH_SUFFIXES lib lib64 NO_DEFAULT_PATH)
  if(NOT QCU_MPI_INCLUDE_DIR OR NOT QCU_MPI_LIBRARY)
    message(FATAL_ERROR
      "QCU_MPI_ROOT=${_QCU_MPI_ROOT} must contain include/mpi.h and "
      "lib/libmpi; found include='${QCU_MPI_INCLUDE_DIR}' "
      "library='${QCU_MPI_LIBRARY}'")
  endif()
  set(MPI_C_INCLUDE_DIRS "${QCU_MPI_INCLUDE_DIR}")
  set(MPI_C_LIBRARIES "${QCU_MPI_LIBRARY}")
  set(MPI_CXX_INCLUDE_DIRS "${QCU_MPI_INCLUDE_DIR}")
  set(MPI_CXX_LIBRARIES "${QCU_MPI_LIBRARY}")
  message(STATUS "QCU MPI pinned to ${_QCU_MPI_ROOT}")
  message(STATUS "QCU MPI include: ${QCU_MPI_INCLUDE_DIR}")
  message(STATUS "QCU MPI library: ${QCU_MPI_LIBRARY}")
else()
  find_package(MPI REQUIRED COMPONENTS C CXX)
endif()

include(GNUInstallDirs)
include_directories(./include)
aux_source_directory(./src SOURCE_FILE)
add_library(qcu SHARED ${SOURCE_FILE})
find_package(CUDA REQUIRED)
target_include_directories(qcu PRIVATE ${MPI_C_INCLUDE_DIRS})
target_link_libraries(qcu ${MPI_C_LIBRARIES})
target_link_libraries(qcu ${CUDA_LIBRARIES})
target_link_libraries(qcu curand)
target_link_libraries(qcu cublas)
