#=============================================================================
# Copyright (c) 2021, NVIDIA CORPORATION.
#
# 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.
#=============================================================================

#[=======================================================================[

Provide targets for the RAFT: Reusable Accelerated Functions and Tools

RAFT contains fundamental widely-used algorithms and primitives
for data science and machine learning.

Optional Components:
  - nn
  - distance
  - distributed

Imported Targets:
  - raft::raft
  - raft::nn brought in by the `nn` optional component
  - raft::distance brought in by the `distance` optional component
  - raft::distributed brought in by the `distributed` optional component



Result Variables
^^^^^^^^^^^^^^^^

This module will set the following variables::

  RAFT_FOUND
  RAFT_VERSION
  RAFT_VERSION_MAJOR
  RAFT_VERSION_MINOR

#]=======================================================================]


####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() #######
####### Any changes to this file will be overwritten by the next CMake run ####
####### The input file was config.cmake.in                            ########

get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)

macro(set_and_check _var _file)
  set(${_var} "${_file}")
  if(NOT EXISTS "${_file}")
    message(FATAL_ERROR "File or directory ${_file} referenced by variable ${_var} does not exist !")
  endif()
endmacro()

macro(check_required_components _NAME)
  foreach(comp ${${_NAME}_FIND_COMPONENTS})
    if(NOT ${_NAME}_${comp}_FOUND)
      if(${_NAME}_FIND_REQUIRED_${comp})
        set(${_NAME}_FOUND FALSE)
      endif()
    endif()
  endforeach()
endmacro()

####################################################################################

cmake_minimum_required(VERSION 3.23.1)

set(rapids_global_languages )
foreach(lang IN LISTS rapids_global_languages)
  include("${CMAKE_CURRENT_LIST_DIR}/raft-${lang}-language.cmake")
endforeach()
unset(rapids_global_languages)

set(rapids_base_components nn;distance;distributed)
set(rapids_allowed_components ${rapids_base_components})

if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/raft-dependencies.cmake")
  include("${CMAKE_CURRENT_LIST_DIR}/raft-dependencies.cmake")
endif()
foreach(comp IN LISTS rapids_allowed_components)
  # find dependencies before creating targets that use them
  # this way if a dependency can't be found we fail
  if(${comp} IN_LIST raft_FIND_COMPONENTS)
    include("${CMAKE_CURRENT_LIST_DIR}/raft-${comp}-dependencies.cmake" OPTIONAL)
    include("${CMAKE_CURRENT_LIST_DIR}/raft-${comp}-lib-dependencies.cmake" OPTIONAL)
  endif()
endforeach()

include("${CMAKE_CURRENT_LIST_DIR}/raft-targets.cmake" OPTIONAL)

foreach(comp IN LISTS rapids_allowed_components)
  if(${comp} IN_LIST raft_FIND_COMPONENTS)
    include("${CMAKE_CURRENT_LIST_DIR}/raft-${comp}-targets.cmake" OPTIONAL)
    include("${CMAKE_CURRENT_LIST_DIR}/raft-${comp}-lib-targets.cmake" OPTIONAL)
    set(raft_${comp}_FOUND TRUE)
  endif()
endforeach()

unset(rapids_allowed_components)

if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/raft-config-version.cmake")
  include("${CMAKE_CURRENT_LIST_DIR}/raft-config-version.cmake")
endif()

# Set our version variables
set(RAFT_VERSION_MAJOR 23)
set(RAFT_VERSION_MINOR 02)
set(RAFT_VERSION_PATCH 00)
set(RAFT_VERSION 23.02.00)


set(rapids_global_targets raft;nn;distance;distributed)
set(rapids_namespaced_global_targets raft;nn;distance;distributed)
if(rapids_namespaced_global_targets)
  list(TRANSFORM rapids_namespaced_global_targets PREPEND raft:: )
endif()

foreach(target IN LISTS rapids_namespaced_global_targets)
  if(TARGET ${target})
    get_target_property(_is_imported ${target} IMPORTED)
    get_target_property(_already_global ${target} IMPORTED_GLOBAL)
    if(_is_imported AND NOT _already_global)
      set_target_properties(${target} PROPERTIES IMPORTED_GLOBAL TRUE)
    endif()
  endif()
endforeach()

# For backwards compat
if("rapids_config_install" STREQUAL "rapids_config_build")
  foreach(target IN LISTS rapids_global_targets)
    if(TARGET ${target})
      get_target_property(_is_imported ${target} IMPORTED)
      get_target_property(_already_global ${target} IMPORTED_GLOBAL)
      if(_is_imported AND NOT _already_global)
        set_target_properties(${target} PROPERTIES IMPORTED_GLOBAL TRUE)
      endif()
      if(NOT TARGET raft::${target})
        add_library(raft::${target} ALIAS ${target})
      endif()
    endif()
  endforeach()
endif()

unset(rapids_global_targets)
unset(rapids_namespaced_global_targets)

check_required_components(raft)

set(${CMAKE_FIND_PACKAGE_NAME}_CONFIG "${CMAKE_CURRENT_LIST_FILE}")

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(${CMAKE_FIND_PACKAGE_NAME} CONFIG_MODE HANDLE_COMPONENTS)

option(RAFT_NVTX "Enable nvtx markers" OFF)
target_link_libraries(raft::raft INTERFACE $<$<BOOL:${RAFT_NVTX}>:CUDA::nvToolsExt>)
target_compile_definitions(raft::raft INTERFACE $<$<BOOL:${RAFT_NVTX}>:NVTX_ENABLED>)

    if(NOT TARGET raft::Thrust)
    thrust_create_target(raft::Thrust FROM_OPTIONS)
  endif()
  if(distance IN_LIST raft_FIND_COMPONENTS)
  enable_language(CUDA)
endif()

if(nn IN_LIST raft_FIND_COMPONENTS)
  enable_language(CUDA)

  if(TARGET faiss AND (NOT TARGET faiss::faiss))
      add_library(faiss::faiss ALIAS faiss)
  elseif(TARGET faiss::faiss AND (NOT TARGET faiss))
      add_library(faiss ALIAS faiss::faiss)
  endif()
endif()

