# ============================================================================ #
# Copyright (c) 2022 - 2026 NVIDIA Corporation & Affiliates.                   #
# All rights reserved.                                                         #
#                                                                              #
# This source code and the accompanying materials are made available under     #
# the terms of the Apache License 2.0 which accompanies this distribution.     #
# ============================================================================ #

set(LIBRARY_NAME cudaq)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-ctad-maybe-unsupported")

# Create the CUDA-Q Library
add_library(${LIBRARY_NAME}
  SHARED
    cudaq.cpp
    CompiledModuleExecution.cpp
    realtime.cpp
    target_control.cpp
    algorithms/draw.cpp
    algorithms/encoding.cpp
    algorithms/evolve.cpp
    algorithms/run.cpp
    algorithms/schedule.cpp
    analysis/Dem.cpp
    platform/common/QuantumExecutionQueue.cpp
    platform/qpu.cpp
    platform/qpu_utils.cpp
    platform/qpu_state.cpp
    platform/quantum_platform.cpp
    platform/platform_iface.cpp
    qis/execution_manager_c_api.cpp
    qis/execution_manager.cpp
    qis/state.cpp
    utils/cudaq_utils.cpp
    distributed/mpi_plugin.cpp
    ptsbe/PTSBEExecutionData.cpp
    ptsbe/PTSBESample.cpp
    ptsbe/PTSBESampleResult.cpp
    ptsbe/PTSBESampler.cpp
    ptsbe/ShotAllocationStrategy.cpp
    ptsbe/TrajectoryDeduplication.cpp
    ptsbe/strategies/ProbabilisticSamplingStrategy.cpp
    ptsbe/strategies/ExhaustiveSamplingStrategy.cpp
    ptsbe/strategies/OrderedSamplingStrategy.cpp
    ptsbe/strategies/ConditionalSamplingStrategy.cpp
    ptsbe/NoiseExtractor.cpp
)

set_property(GLOBAL APPEND PROPERTY CUDAQ_RUNTIME_LIBS ${LIBRARY_NAME})

if (CUDA_FOUND)
  set(_cudaq_cuda_build_includes "")
  foreach(_cuda_inc IN LISTS CUDAToolkit_INCLUDE_DIRS)
    list(APPEND _cudaq_cuda_build_includes "$<BUILD_INTERFACE:${_cuda_inc}>")
  endforeach()
  target_include_directories(${LIBRARY_NAME}
    PUBLIC $<INSTALL_INTERFACE:include>
            $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/runtime/include>
            $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/runtime>
            $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/tpls/eigen>
            ${_cudaq_cuda_build_includes}
    PRIVATE . ptsbe ptsbe/strategies)

  target_link_libraries(${LIBRARY_NAME}
    PUBLIC
      dl
      cudaq-operator
      cudaq-common
      $<$<BOOL:${CUDAQ_ENABLE_NLOPT}>:cudaq::cudaq-nlopt>
      $<$<BOOL:${CUDAQ_ENABLE_ENSMALLEN}>:cudaq::cudaq-ensmallen>
      cudaq-logger
      CUDAQTargetConfig
    PRIVATE
      cudaqMLIR
      nvqir
      fmt::fmt-header-only
      CUDA::cudart_static
      CUDAQTargetConfigYaml
  )

  target_compile_definitions(${LIBRARY_NAME} PRIVATE CUDAQ_HAS_CUDA)

else()
  target_include_directories(${LIBRARY_NAME}
    PUBLIC $<INSTALL_INTERFACE:include>
            $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/runtime/include>
            $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/runtime>
            $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/tpls/eigen>
    PRIVATE . ptsbe ptsbe/strategies)

  target_link_libraries(${LIBRARY_NAME}
    PUBLIC
      dl
      cudaq-operator
      cudaq-common
      $<$<BOOL:${CUDAQ_ENABLE_NLOPT}>:cudaq::cudaq-nlopt>
      $<$<BOOL:${CUDAQ_ENABLE_ENSMALLEN}>:cudaq::cudaq-ensmallen>
      cudaq-logger
      CUDAQTargetConfig
    PRIVATE
      cudaqMLIR
      nvqir
      fmt::fmt-header-only
      CUDAQTargetConfigYaml
  )
endif()

if (CUDAQ_ENABLE_REALTIME)
  target_compile_definitions(${LIBRARY_NAME} PRIVATE CUDAQ_ENABLE_REALTIME)
  target_include_directories(${LIBRARY_NAME} PRIVATE
    ${CMAKE_SOURCE_DIR}/runtime/internal/device_call/include)
endif()

add_subdirectory(qis/managers)
add_subdirectory(algorithms)
add_subdirectory(platform)
add_subdirectory(builder)
add_subdirectory(domains)
add_subdirectory(operators)
add_subdirectory(ptsbe)

install(TARGETS ${LIBRARY_NAME}
        EXPORT cudaq-targets
        DESTINATION lib
        COMPONENT Runtime)

install(EXPORT cudaq-targets
        FILE CUDAQTargets.cmake
        NAMESPACE cudaq::
        DESTINATION lib/cmake/cudaq
        COMPONENT Development)

# Install CUDAQ MPI interface header, reference implementation, and build script
# for manual activation on a local system if needed.
install(
  FILES
      distributed/distributed_capi.h
      distributed/builtin/mpi_comm_impl.cpp
      distributed/builtin/activate_custom_mpi.sh
  DESTINATION distributed_interfaces
  COMPONENT Runtime
  PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ
)
