#
# Copyright (c) 2023-2024, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#  * Neither the name of NVIDIA CORPORATION nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

# Need atleast 3.17 version due to the use of FindCUDAToolkit
cmake_minimum_required(VERSION 3.17)

project(vpi_python CXX)

if(POLICY CMP0074)
    # find_package() uses `PackageName>_ROOT variables.
    cmake_policy(SET CMP0074 NEW)
endif()

set(CMAKE_CXX_STANDARD 17)

# Use statically-linked C runtime
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

set(PYBIND11_PYTHON_VERSION ${PYTHON_VERSION})
add_subdirectory(pybind11)

find_package(vpi 4.1 REQUIRED)

pybind11_add_module(vpi_python MODULE
  Main.cpp
  Util.cpp
  CudaBuffer.cpp
  Object.cpp
  Cache.cpp
  Object.cpp
  Resource.cpp
  Container.cpp
  Context.cpp
  Image.cpp
  ImageFormat.cpp
  Pyramid.cpp
  Array.cpp
  Stream.cpp
  Types.cpp
  BackendSet.cpp
  HostFunction.cpp
  WarpGrid.cpp
  WarpMap.cpp
  LensModelFisheye.cpp
  LensModelPolynomial.cpp
  Erode.cpp
  Dilate.cpp
  Convolution.cpp
  SeparableConvolution.cpp
  BoxFilter.cpp
  GaussianFilter.cpp
  RecursiveGaussianFilter.cpp
  ConvertImageFormat.cpp
  BilateralFilter.cpp
  Rescale.cpp
  FFT.cpp
  InvFFT.cpp
  PerspectiveWarp.cpp
  EqualizeHist.cpp
  StereoDisparity.cpp
  OpticalFlowDense.cpp
  GaussianPyramid.cpp
  LaplacianPyramid.cpp
  TemporalNoiseReduction.cpp
  Histogram.cpp
  HarrisCornersDetector.cpp
  FASTCornerDetector.cpp
  DynamicRemap.cpp
  Remap.cpp
  OpticalFlowPyrLK.cpp
  BackgroundSubtractor.cpp
  KLTFeatureTracker.cpp
  MinMaxLoc.cpp
  ImageFlip.cpp
  MedianFilter.cpp
  MixChannels.cpp
  TemplateMatching.cpp
  ImageStats.cpp
  CannyEdgeDetector.cpp
  ORB.cpp
  BruteForceMatcher.cpp
  TransformEstimator.cpp
)

set_target_properties(vpi_python PROPERTIES OUTPUT_NAME vpi DEBUG_POSTFIX ""
    LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")

find_package(CUDAToolkit REQUIRED)

target_link_libraries(vpi_python PRIVATE vpi CUDA::cudart_static)

if(VPI_HACK_NVBUG_3736272)
    add_definitions(-DVPI_HACK_NVBUG_3736272=1)
endif()

if(NOT MSVC)
    target_link_libraries(vpi_python PRIVATE vpi
        -static-libstdc++
        -static-libgcc
        # use exports file to expose only the symbol dl-loaded by python,
        # and nothing else.
        -Wl,--version-script ${CMAKE_CURRENT_SOURCE_DIR}/exports.ldscript)
    target_compile_options(vpi_python PRIVATE -Wno-deprecated-declarations -Wsuggest-override)
endif()

if(CMAKE_CROSSCOMPILING AND CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
    get_target_property(module_suffix vpi_python SUFFIX)
    string(REGEX REPLACE "^([^-]+)-([^-]+)[^.]+\(\.[^-]+)$" "\\1-\\2-aarch64-linux-gnu\\3" module_suffix ${module_suffix})
    set_target_properties(vpi_python PROPERTIES SUFFIX ${module_suffix})
endif()

include(GNUInstallDirs)

install(TARGETS vpi_python LIBRARY DESTINATION lib COMPONENT python)
