# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the \"License\");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an \"AS IS\" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 3.20)
# Note: CUDA is required to build matx_basic.cu
project(matx_basic CUDA CXX)

if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
  message(STATUS "CMAKE_CUDA_ARCHITECTURES not defined, setting it to `native`")
  set(CMAKE_CUDA_ARCHITECTURES "native")
elseif(CMAKE_CUDA_ARCHITECTURES MATCHES "^[0-9]+$" AND CMAKE_CUDA_ARCHITECTURES LESS 70)
  message(STATUS "MatX requires CUDA compute capability 7.0 or newer. Setting to 70")
  # Note: atomicCas() used in matx/transforms/reduce.h requires compute capability 7.0 or newer
  set(CMAKE_CUDA_ARCHITECTURES "70")
endif()

find_package(CUDAToolkit QUIET)
if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 13.0)
  set(MATX_CCCL_MIN_VERSION 3.0)
else()
  set(MATX_CCCL_MIN_VERSION 2.8)
endif()

# Holoscan SDK core features depend on CCCL >= 2.5 while the optional holoscan::matx feature
# depends on CCCL >= 2.8. Skip this example if no compatible CCCL is found.
if(MATX_CCCL_MIN_VERSION VERSION_GREATER_EQUAL 3.0)
  find_package(CCCL ${MATX_CCCL_MIN_VERSION} PATHS /usr/local/cuda/lib64/cmake/cccl)
else()
  # Allow compatibility across CCCL major versions
  find_package(CCCL 3.0 PATHS /usr/local/cuda/lib64/cmake/cccl QUIET)
  if(NOT CCCL_FOUND)
    find_package(CCCL ${MATX_CCCL_MIN_VERSION} PATHS /usr/local/cuda/lib64/cmake/cccl QUIET)
  endif()
endif()

if(NOT CCCL_FOUND)
  message(STATUS "CCCL not found or version too old. Need at least CCCL ${MATX_CCCL_MIN_VERSION} for MatX.
Please install a compatible CCCL version from https://github.com/NVIDIA/cccl.git
and re-configure the project with -DCCCL_ROOT=path/to/cccl to resolve the issue.
Skipping the matx_basic example...")
  return()
endif()

# Finds the package holoscan
find_package(holoscan REQUIRED CONFIG
             PATHS "/opt/nvidia/holoscan" "/workspace/holoscan-sdk/install")

add_executable(matx_basic
  matx_basic.cu
)

target_link_libraries(matx_basic
  PRIVATE
  holoscan::core
  holoscan::matx
)

# Testing
if(BUILD_TESTING)
  add_test(NAME EXAMPLE_CPP_MATX_BASIC
           COMMAND matx_basic
           WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
          )
  set_tests_properties(EXAMPLE_CPP_MATX_BASIC PROPERTIES
                       PASS_REGULAR_EXPRESSION "000009:  2\\.1000e\\+01"
                       FAIL_REGULAR_EXPRESSION "error"
                      )
endif()
