if(WINRT)
  ocv_module_disable(dnn)
endif()

set(the_description "Deep neural network module. It allows to load models from different frameworks and to make forward pass")

ocv_add_dispatched_file_force_all("layers/layers_common" AVX AVX2 AVX512_SKX RVV LASX NEON SVE)
ocv_add_dispatched_file_force_all("int8layers/layers_common" AVX2 AVX512_SKX RVV LASX NEON)
ocv_add_dispatched_file_force_all("layers/cpu_kernels/conv_block" AVX AVX2 NEON NEON_FP16)
ocv_add_dispatched_file_force_all("layers/cpu_kernels/conv_depthwise" AVX AVX2 RVV LASX)
ocv_add_dispatched_file("layers/cpu_kernels/conv_winograd_f63" AVX AVX2 NEON NEON_FP16)
ocv_add_dispatched_file_force_all("layers/cpu_kernels/fast_gemm_kernels" AVX AVX2 NEON LASX)
ocv_add_dispatched_file("layers/cpu_kernels/conv2_depthwise" AVX AVX2 NEON NEON_FP16)
ocv_add_dispatched_file("layers/cpu_kernels/conv2_kernels" AVX AVX2 NEON NEON_FP16)
ocv_add_dispatched_file_force_all("int8layers/conv2_int8_kernels" AVX2)
ocv_add_dispatched_file("layers/cpu_kernels/activation_kernels" AVX AVX2 NEON NEON_FP16)
ocv_add_dispatched_file_force_all("layers/cpu_kernels/reduce2_kernels" AVX AVX2 NEON RVV LASX)
ocv_add_dispatched_file_force_all("layers/cpu_kernels/transpose_kernels" AVX AVX2 NEON RVV LASX)
ocv_add_dispatched_file_force_all("layers/cpu_kernels/gridsample_kernels" AVX AVX2 NEON RVV LASX)
ocv_add_dispatched_file_force_all("layers/cpu_kernels/nary_eltwise_kernels" AVX AVX2 NEON RVV LASX)

ocv_add_module(dnn opencv_core opencv_imgproc opencv_geometry WRAP python java objc js)

include(${CMAKE_CURRENT_LIST_DIR}/cmake/plugin.cmake)

ocv_option(OPENCV_DNN_OPENCL "Build with OpenCL support" HAVE_OPENCL AND NOT APPLE)

if(OPENCV_DNN_OPENCL AND HAVE_OPENCL)
  ocv_target_compile_definitions(${the_module} PRIVATE "CV_OCL4DNN=1")
endif()

if(WITH_WEBNN AND HAVE_WEBNN)
  ocv_target_compile_definitions(${the_module} PRIVATE "HAVE_WEBNN=1")
endif()

if(HAVE_TIMVX)
  ocv_target_compile_definitions(${the_module} PRIVATE "HAVE_TIMVX=1")
endif()

if(HAVE_CANN)
  ocv_target_compile_definitions(${the_module} PRIVATE "HAVE_CANN=1")
endif()

if(MSVC)
  ocv_target_compile_options(${the_module} PRIVATE "/fp:fast")
elseif(CV_GCC OR CV_CLANG)
  ocv_target_compile_options(${the_module} PRIVATE -ffast-math)
  ocv_target_compile_options(${the_module} PRIVATE -fno-finite-math-only)
endif()

ocv_option(OPENCV_DNN_CUDA "Build with CUDA support"
    HAVE_CUDA
    AND HAVE_CUBLAS
    AND HAVE_CUDNN
)

if(OPENCV_DNN_CUDA)
  if(HAVE_CUDA AND HAVE_CUBLAS AND HAVE_CUDNN)
    ocv_target_compile_definitions(${the_module} PRIVATE "CV_CUDA4DNN=1")
  else()
    if(NOT HAVE_CUDA)
      message(SEND_ERROR "DNN: CUDA backend requires CUDA Toolkit. Please resolve dependency or disable OPENCV_DNN_CUDA=OFF")
    elseif(NOT HAVE_CUBLAS)
      message(SEND_ERROR "DNN: CUDA backend requires cuBLAS. Please resolve dependency or disable OPENCV_DNN_CUDA=OFF")
    elseif(NOT HAVE_CUDNN)
      message(SEND_ERROR "DNN: CUDA backend requires cuDNN. Please resolve dependency or disable OPENCV_DNN_CUDA=OFF")
    endif()
  endif()
endif()


ocv_cmake_hook_append(INIT_MODULE_SOURCES_opencv_dnn "${CMAKE_CURRENT_LIST_DIR}/cmake/hooks/INIT_MODULE_SOURCES_opencv_dnn.cmake")


if(MSVC)
  add_definitions( -D_CRT_SECURE_NO_WARNINGS=1 )
  ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4244 /wd4267 /wd4018 /wd4355 /wd4800 /wd4251 /wd4996 /wd4146
                                       /wd4305 /wd4127 /wd4100 /wd4512 /wd4125 /wd4389 /wd4510 /wd4610
                                       /wd4702 /wd4456 /wd4457 /wd4065 /wd4310 /wd4661 /wd4506
  )
  if(MSVC_VERSION LESS 1920)  # MSVS 2015/2017, .pb.cc generated files
    ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4309)  # 'static_cast': truncation of constant value
  endif()
  if(MSVC_VERSION LESS 1920)  # <MSVS2019, .pb.cc generated files
    ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4189)  # local variable is initialized but not referenced
    ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4592)  # symbol will be dynamically initialized (implementation limitation)
  endif()
else()
  ocv_warnings_disable(CMAKE_CXX_FLAGS -Wno-deprecated -Wmissing-prototypes -Wmissing-declarations -Wshadow
                                       -Wunused-parameter -Wsign-compare
  )
endif()
if(HAVE_CUDA)
  ocv_warnings_disable(CMAKE_CXX_FLAGS -Wundef)
endif()
if(NOT HAVE_CXX11)
  ocv_warnings_disable(CMAKE_CXX_FLAGS -Wno-undef)  # LANG_CXX11 from protobuf files
endif()

if(APPLE_FRAMEWORK)
  ocv_warnings_disable(CMAKE_CXX_FLAGS -Wshorten-64-to-32)
endif()

if(ANDROID)
  add_definitions(-DDISABLE_POSIX_MEMALIGN -DTH_DISABLE_HEAP_TRACKING)
endif()

if(NOT BUILD_PROTOBUF)
  ocv_target_compile_definitions(${the_module} PRIVATE "OPENCV_DNN_EXTERNAL_PROTOBUF=1")
endif()

#suppress warnings in autogenerated caffe.pb.* files
ocv_warnings_disable(CMAKE_CXX_FLAGS
    /wd4125 /wd4267 /wd4127 /wd4244 /wd4512 /wd4702
    /wd4456 /wd4510 /wd4610 /wd4800
    /wd4701 /wd4703                    # potentially uninitialized local/pointer variable 'value' used
    /wd4505                            # unreferenced local function has been removed
    /wd4458                            # declaration of 'x' hides class member. GCC still works, MSVC bug is here: https://developercommunity.visualstudio.com/content/problem/219311/c-c4458-declaration-hides-class-member-warning-iss.html
    -wd858 -wd2196
    -Winvalid-offsetof                 # Apple Clang (attr_value.pb.cc)
)

set(include_dirs "")
set(libs "")

# ONNX Runtime
ocv_option(WITH_ONNXRUNTIME "Build with ONNX Runtime support" OFF)
ocv_option(DOWNLOAD_ONNXRUNTIME "Download ONNX Runtime prebuilt binaries" OFF IF WITH_ONNXRUNTIME)
ocv_option(DOWNLOAD_ONNXRUNTIME_GPU "Download GPU-enabled ONNX Runtime prebuilt binaries when available" OFF IF WITH_ONNXRUNTIME)

set(ONNXRUNTIME_VERSION "1.25.1" CACHE STRING "ONNX Runtime version to download (prebuilt binaries)")

if(WITH_ONNXRUNTIME)
  include("${OpenCV_SOURCE_DIR}/cmake/FindONNX.cmake")

  set(_ort_download_requested OFF)
  set(_ort_download_forced OFF)
  if(DOWNLOAD_ONNXRUNTIME OR DOWNLOAD_ONNXRUNTIME_GPU)
    set(_ort_download_requested ON)
    set(_ort_download_forced ON)
  elseif(NOT HAVE_ONNXRUNTIME)
    set(_ort_download_requested ON)
    message(STATUS "DNN: ONNX Runtime was not found in system paths, attempting to download prebuilt package")
  endif()

  if(_ort_download_requested)
    set(_ort_filename "")
    set(_ort_package_kind "CPU")
    if(DOWNLOAD_ONNXRUNTIME_GPU)
      set(_ort_package_kind "GPU")
    endif()
    string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" _ort_processor)
    set(_ort_is_x64 FALSE)
    set(_ort_is_arm64 FALSE)
    set(_ort_is_x86 FALSE)
    if(X86_64 OR _ort_processor MATCHES "^(x86_64|amd64)$")
      set(_ort_is_x64 TRUE)
    endif()
    if(ARM64 OR AARCH64 OR _ort_processor MATCHES "^(aarch64|arm64)$")
      set(_ort_is_arm64 TRUE)
    endif()
    if(X86 OR _ort_processor MATCHES "^(x86|i[3-6]86)$")
      set(_ort_is_x86 TRUE)
    endif()

    if(WIN32)
      if(_ort_is_arm64)
        if(DOWNLOAD_ONNXRUNTIME_GPU)
          message(FATAL_ERROR "DNN: ONNX Runtime GPU package is not available for Windows ARM64. "
            "Disable DOWNLOAD_ONNXRUNTIME_GPU or use a supported platform (Windows x64 or Linux x64).")
        endif()
        set(_ort_filename "onnxruntime-win-arm64-${ONNXRUNTIME_VERSION}.zip")
      elseif(_ort_is_x64)
        if(DOWNLOAD_ONNXRUNTIME_GPU)
          set(_ort_filename "onnxruntime-win-x64-gpu-${ONNXRUNTIME_VERSION}.zip")
        else()
          set(_ort_filename "onnxruntime-win-x64-${ONNXRUNTIME_VERSION}.zip")
        endif()
      elseif(_ort_is_x86)
        message(FATAL_ERROR
          "DNN: No official ONNX Runtime prebuilt package detected for 32-bit Windows and "
          "ONNXRUNTIME_VERSION='${ONNXRUNTIME_VERSION}'. "
          "Disable DOWNLOAD_ONNXRUNTIME and provide ONNXRT_ROOT_DIR manually."
        )
      endif()
    elseif(APPLE)
      if(DOWNLOAD_ONNXRUNTIME_GPU)
        message(FATAL_ERROR "DNN: ONNX Runtime GPU package is not available for macOS. "
          "Disable DOWNLOAD_ONNXRUNTIME_GPU or use a supported platform (Windows x64 or Linux x64).")
      endif()
      if(_ort_is_arm64)
        set(_ort_filename "onnxruntime-osx-arm64-${ONNXRUNTIME_VERSION}.tgz")
      elseif(_ort_is_x64)
        set(_ort_filename "onnxruntime-osx-x86_64-${ONNXRUNTIME_VERSION}.tgz")
      else()
        set(_ort_filename "onnxruntime-osx-universal2-${ONNXRUNTIME_VERSION}.tgz")
      endif()
    elseif(UNIX)
      if(_ort_is_x64)
        if(DOWNLOAD_ONNXRUNTIME_GPU)
          set(_ort_filename "onnxruntime-linux-x64-gpu-${ONNXRUNTIME_VERSION}.tgz")
        else()
          set(_ort_filename "onnxruntime-linux-x64-${ONNXRUNTIME_VERSION}.tgz")
        endif()
      elseif(_ort_is_arm64)
        if(DOWNLOAD_ONNXRUNTIME_GPU)
          message(FATAL_ERROR "DNN: ONNX Runtime GPU package is not available for Linux AArch64. "
            "Disable DOWNLOAD_ONNXRUNTIME_GPU or use a supported platform (Windows x64 or Linux x64).")
        endif()
        set(_ort_filename "onnxruntime-linux-aarch64-${ONNXRUNTIME_VERSION}.tgz")
      endif()
    endif()

    if(NOT _ort_filename)
      if(_ort_download_forced)
        message(FATAL_ERROR
          "DNN: DOWNLOAD_ONNXRUNTIME=ON, but there is no official ONNX Runtime prebuilt package for "
          "CMAKE_SYSTEM_NAME='${CMAKE_SYSTEM_NAME}', CMAKE_SYSTEM_PROCESSOR='${CMAKE_SYSTEM_PROCESSOR}'. "
          "Disable DOWNLOAD_ONNXRUNTIME and provide an installed ORT via ONNXRT_ROOT_DIR, or use a supported platform."
        )
      endif()
    else()
      set(_ort_url "https://github.com/microsoft/onnxruntime/releases/download/v${ONNXRUNTIME_VERSION}/${_ort_filename}")
      string(REGEX REPLACE "\\.(tgz|zip)$" "" _ort_unpack_dirname "${_ort_filename}")

      set(_ort_download_dir "${OpenCV_BINARY_DIR}/3rdparty/onnxruntime")
      set(_ort_cache_dir "${OPENCV_DOWNLOAD_PATH}/onnxruntime")
      set(_ort_cache_archive "${_ort_cache_dir}/${_ort_filename}")
      set(_ort_extracted_dir "${_ort_download_dir}/${_ort_unpack_dirname}")

      message(STATUS "DNN: ONNX Runtime download mode: ${_ort_package_kind}")
      message(STATUS "DNN: ONNX Runtime package: ${_ort_filename}")

      # Download to persistent cache if not already present
      if(NOT EXISTS "${_ort_cache_archive}")
        file(MAKE_DIRECTORY "${_ort_cache_dir}")
        message(STATUS "DNN: Downloading ONNX Runtime package from ${_ort_url}")
        file(DOWNLOAD "${_ort_url}" "${_ort_cache_archive}"
             SHOW_PROGRESS
             STATUS _ort_download_status
             LOG _ort_download_log)
        list(GET _ort_download_status 0 _ort_download_status_code)
        if(NOT _ort_download_status_code EQUAL 0)
          file(REMOVE "${_ort_cache_archive}")
          if(_ort_download_forced)
            message(FATAL_ERROR "DNN: ONNX Runtime download failed. URL='${_ort_url}'. Log: ${_ort_download_log}")
          else()
            message(STATUS "DNN: ONNX Runtime download failed, skipping. Log: ${_ort_download_log}")
          endif()
        endif()
      else()
        message(STATUS "DNN: ONNX Runtime package found in cache: ${_ort_cache_archive}")
      endif()

      # Extract to build dir if not already extracted
      if(EXISTS "${_ort_cache_archive}" AND NOT EXISTS "${_ort_extracted_dir}")
        file(MAKE_DIRECTORY "${_ort_download_dir}")
        message(STATUS "DNN: Extracting ONNX Runtime package to ${_ort_download_dir}")
        if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.18")
          file(ARCHIVE_EXTRACT INPUT "${_ort_cache_archive}"
               DESTINATION "${_ort_download_dir}")
        else()
          if(_ort_filename MATCHES "\\.zip$")
            set(_ort_tar_flags xvf)
          else()
            set(_ort_tar_flags xzf)
          endif()
          execute_process(
            COMMAND "${CMAKE_COMMAND}" -E tar ${_ort_tar_flags} "${_ort_cache_archive}"
            WORKING_DIRECTORY "${_ort_download_dir}"
            RESULT_VARIABLE _ort_extract_status
          )
          if(NOT _ort_extract_status EQUAL 0)
            message(FATAL_ERROR "DNN: ONNX Runtime extraction failed for '${_ort_cache_archive}'.")
          endif()
        endif()
      endif()

      set(ONNXRT_ROOT_DIR "${_ort_extracted_dir}"
          CACHE PATH "ONNX Runtime install directory" FORCE)

      if(NOT APPLE AND NOT WIN32)
        set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-rpath,'$ORIGIN/../3rdparty/onnxruntime/${_ort_unpack_dirname}/lib'")
        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath,'$ORIGIN/../3rdparty/onnxruntime/${_ort_unpack_dirname}/lib'")
      endif()

      include("${OpenCV_SOURCE_DIR}/cmake/FindONNX.cmake")
    endif()
  endif()  # _ort_download_requested

  if(HAVE_ONNX)
    set(HAVE_ONNXRUNTIME 1 CACHE INTERNAL "ONNX Runtime availability")
    # Suppress warnings from ONNX Runtime headers
    ocv_warnings_disable(CMAKE_CXX_FLAGS -Wsuggest-override -Wsign-promo)

    if(ONNXRUNTIME_PREFER_STATIC)
      set(_ort_static_lib "")
      if(ONNXRT_ROOT_DIR)
        find_file(_ort_static_lib
          NAMES libonnxruntime.a
          HINTS "${ONNXRT_ROOT_DIR}"
          PATH_SUFFIXES lib lib64
          NO_DEFAULT_PATH
        )
      endif()
      if(NOT _ort_static_lib AND ONNX_LIBRARIES)
        foreach(_ort_lib ${ONNX_LIBRARIES})
          get_filename_component(_ort_lib_dir "${_ort_lib}" DIRECTORY)
          find_file(_ort_static_candidate
            NAMES libonnxruntime.a
            HINTS "${_ort_lib_dir}"
            NO_DEFAULT_PATH
          )
          if(_ort_static_candidate AND _ort_static_candidate MATCHES "\\.a$")
            set(_ort_static_lib "${_ort_static_candidate}")
            break()
          endif()
        endforeach()
      endif()
      if(_ort_static_lib AND _ort_static_lib MATCHES "\\.a$")
        set(ONNX_LIBRARIES "${_ort_static_lib}" CACHE STRING "ONNX Runtime libraries" FORCE)
        message(STATUS "DNN: ONNX Runtime static library selected: ${_ort_static_lib}")
      endif()
      unset(_ort_static_candidate)
      unset(_ort_static_lib)
    endif()

    if(ONNX_INCLUDE_DIR)
      list(APPEND include_dirs "${ONNX_INCLUDE_DIR}")
    endif()
    if(ONNX_LIBRARIES)
      list(APPEND libs "${ONNX_LIBRARIES}")
    endif()

    add_definitions(-DHAVE_ONNXRUNTIME=1)
    message(STATUS "DNN: ONNX Runtime enabled")

    # Ensure runtime ORT binaries are deployed into OpenCV install tree
    set(_ort_runtime_libs "")
    if(WIN32)
      if(ONNXRT_ROOT_DIR)
        file(GLOB _ort_runtime_libs "${ONNXRT_ROOT_DIR}/bin/onnxruntime*.dll")
      endif()
      if(_ort_runtime_libs)
        install(FILES ${_ort_runtime_libs} DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs)
      endif()
    elseif(APPLE)
      if(ONNXRT_ROOT_DIR)
        file(GLOB _ort_runtime_libs "${ONNXRT_ROOT_DIR}/lib/libonnxruntime*.dylib")
      endif()
      if(NOT _ort_runtime_libs AND ONNX_LIBRARIES)
        foreach(_ort_lib ${ONNX_LIBRARIES})
          get_filename_component(_ort_lib_dir "${_ort_lib}" DIRECTORY)
          file(GLOB _ort_runtime_libs "${_ort_lib_dir}/libonnxruntime*.dylib")
          if(_ort_runtime_libs)
            break()
          endif()
        endforeach()
      endif()
      if(_ort_runtime_libs)
        install(FILES ${_ort_runtime_libs} DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs)
      endif()
    else()
      if(ONNXRT_ROOT_DIR)
        file(GLOB _ort_runtime_libs "${ONNXRT_ROOT_DIR}/lib/libonnxruntime.so*")
      endif()
      if(NOT _ort_runtime_libs AND ONNX_LIBRARIES)
        foreach(_ort_lib ${ONNX_LIBRARIES})
          get_filename_component(_ort_lib_dir "${_ort_lib}" DIRECTORY)
          file(GLOB _ort_runtime_libs "${_ort_lib_dir}/libonnxruntime.so*")
          if(_ort_runtime_libs)
            break()
          endif()
        endforeach()
      endif()
      if(_ort_runtime_libs)
        install(FILES ${_ort_runtime_libs} DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs)
      endif()
    endif()
    unset(_ort_runtime_libs)
  else()
    message(SEND_ERROR
      "DNN: ONNX Runtime support was requested (WITH_ONNXRUNTIME=ON), but it was not found. "
      "Set ONNXRT_ROOT_DIR to an existing installation or enable DOWNLOAD_ONNXRUNTIME=ON."
    )
  endif()

  unset(_ort_download_requested)
  unset(_ort_download_forced)
endif()

if(HAVE_PROTOBUF)
  ocv_target_compile_definitions(${the_module} PRIVATE "HAVE_PROTOBUF=1")

  if(PROTOBUF_UPDATE_FILES)
    file(GLOB proto_files "${CMAKE_CURRENT_LIST_DIR}/src/tensorflow/*.proto" "${CMAKE_CURRENT_LIST_DIR}/src/onnx/opencv-onnx.proto")
    if(CMAKE_VERSION VERSION_LESS "3.13.0")
      set(PROTOBUF_GENERATE_CPP_APPEND_PATH ON) # required for tensorflow
      protobuf_generate_cpp(fw_srcs fw_hdrs ${proto_files})
    else()
      protobuf_generate(
        APPEND_PATH # required for tensorflow
        LANGUAGE cpp
        IMPORT_DIRS ${Protobuf_IMPORT_DIRS}
        OUT_VAR fw_srcs
        PROTOC_EXE ${Protobuf_PROTOC_EXECUTABLE}
        PROTOS ${proto_files})
      set(fw_hdrs "${fw_srcs}")
      # separate the header files and source files
      list(FILTER fw_srcs EXCLUDE REGEX ".+\.h$")
      list(FILTER fw_hdrs INCLUDE REGEX ".+\.h$")
    endif()
  else()
    file(GLOB fw_srcs "${CMAKE_CURRENT_LIST_DIR}/misc/tensorflow/*.cc" "${CMAKE_CURRENT_LIST_DIR}/misc/caffe/opencv-caffe.pb.cc" "${CMAKE_CURRENT_LIST_DIR}/misc/onnx/opencv-onnx.pb.cc")
    file(GLOB fw_hdrs "${CMAKE_CURRENT_LIST_DIR}/misc/tensorflow/*.h" "${CMAKE_CURRENT_LIST_DIR}/misc/caffe/opencv-caffe.pb.h" "${CMAKE_CURRENT_LIST_DIR}/misc/onnx/opencv-onnx.pb.h")
    set(fw_inc "${CMAKE_CURRENT_LIST_DIR}/misc/caffe" "${CMAKE_CURRENT_LIST_DIR}/misc/tensorflow" "${CMAKE_CURRENT_LIST_DIR}/misc/onnx")
  endif()
endif()

ocv_option(OPENCV_DNN_TFLITE "Build with TFLite support" (TARGET ocv.3rdparty.flatbuffers))
if(TARGET ocv.3rdparty.flatbuffers AND OPENCV_DNN_TFLITE)
  if(NOT HAVE_FLATBUFFERS)
    message(FATAL_ERROR "DNN: TFLite is not supported without enabled 'flatbuffers'. Check build configuration.")
  endif()
  list(APPEND libs ocv.3rdparty.flatbuffers)
  list(APPEND fw_hdrs "${CMAKE_CURRENT_LIST_DIR}/misc/tflite/schema_generated.h")
  list(APPEND fw_inc "${CMAKE_CURRENT_LIST_DIR}/misc/tflite")

  # Schema is generated by this command:
  #add_custom_command(
  #      OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/schema_generated.h"
  #      COMMAND flatbuffers::flatc --cpp -o "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_LIST_DIR}/src/tflite/schema.fbs")
endif()

list(APPEND include_dirs ${fw_inc})
list(APPEND libs ${Protobuf_LIBRARIES})
if(NOT BUILD_PROTOBUF)
  list(APPEND include_dirs ${Protobuf_INCLUDE_DIRS})
endif()

set(sources_options "")

list(APPEND libs ${LAPACK_LIBRARIES})
if(OPENCV_DNN_OPENCL AND HAVE_OPENCL)
  list(APPEND include_dirs ${OPENCL_INCLUDE_DIRS})
else()
  set(sources_options EXCLUDE_OPENCL)
endif()

if(OPENCV_DNN_CUDA AND HAVE_CUDA AND HAVE_CUBLAS AND HAVE_CUDNN)
  list(APPEND include_dirs ${CUDA_TOOLKIT_INCLUDE} ${CUDNN_INCLUDE_DIRS})
  set(CC_LIST ${CUDA_ARCH_BIN})
  separate_arguments(CC_LIST)
  foreach(cc ${CC_LIST})
    if(cc VERSION_LESS 3.0)
      message(FATAL_ERROR "CUDA backend for DNN module requires CC 3.0 or higher. Please remove unsupported architectures from CUDA_ARCH_BIN option or disable OPENCV_DNN_CUDA=OFF.")
    endif()
  endforeach()
  unset(CC_LIST)
  if(ENABLE_CUDA_FIRST_CLASS_LANGUAGE)
    list(APPEND libs CUDA::cudart${CUDA_LIB_EXT} ${CUDNN_LIBRARIES} CUDA::cublas${CUDA_LIB_EXT})
    if(NOT CUDA_VERSION VERSION_LESS 10.1)
      list(APPEND libs CUDA::cublasLt${CUDA_LIB_EXT})
    endif()
  endif()
else()
  set(sources_options ${sources_options} EXCLUDE_CUDA)
endif()

if(HAVE_TIMVX)
    list(APPEND include_dirs ${TIMVX_INCLUDE_DIR})
    list(APPEND libs -Wl,--whole-archive ${TIMVX_LIBRARY} -Wl,--no-whole-archive)
endif()

if(HAVE_CANN)
  list(APPEND include_dirs ${CANN_INCLUDE_DIRS})
  list(APPEND libs -Wl,--whole-archive ${CANN_LIBRARIES} -Wl,--no-whole-archive)
endif()

set(webnn_srcs "")
if(NOT EMSCRIPTEN)
  if(HAVE_WEBNN)
    list(APPEND include_dirs ${WEBNN_HEADER_DIRS})
    list(APPEND include_dirs ${WEBNN_INCLUDE_DIRS})
    list(APPEND libs -Wl,--whole-archive ${WEBNN_LIBRARIES} -Wl,--no-whole-archive)
    list(APPEND webnn_srcs $ENV{WEBNN_NATIVE_DIR}/gen/src/webnn/webnn_cpp.cpp)
  endif()
endif()

# Vendored MLAS (Microsoft Linear Algebra Subprograms) from ONNX Runtime.
# Sources live in 3rdparty/mlas/. Builds to an OBJECT library whose objects
# link directly into opencv_dnn. Skipped under Emscripten: MLAS is a native
# CPU SGEMM accelerator (asm/intrinsic kernels) and the wasm scalar fallback
# offers no benefit for the JS bindings build that produces opencv.js.
set(HAVE_MLAS 0)
# Reset status flags + arch booleans
foreach(_v OPENCV_DNN_MLAS_ENABLED OPENCV_DNN_MLAS_SKIP_REASON
           MLAS_X86_64 MLAS_X86 MLAS_ARM MLAS_ARM64 MLAS_POWER
           MLAS_LOONGARCH64 MLAS_S390X MLAS_RISCV64 MLAS_WASM
           MLAS_HAS_ASM MLAS_HAS_POWER10 MLAS_HAS_RISCV64_RVV)
  unset(${_v} CACHE)
endforeach()
if(NOT EMSCRIPTEN)
  add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/mlas" "${CMAKE_BINARY_DIR}/3rdparty/mlas")
endif()
if(HAVE_MLAS)
  add_definitions(-DHAVE_MLAS=1)
  list(APPEND include_dirs ${MLAS_INCLUDE_DIRS})
  message(STATUS "DNN: MLAS (vendored) enabled.")
else()
  message(STATUS "DNN: MLAS (vendored) disabled — host arch/OS not wired up.")
endif()

ocv_module_include_directories(${include_dirs})
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  ocv_append_source_files_cxx_compiler_options(fw_srcs "-Wno-suggest-override")  # GCC
  ocv_append_source_files_cxx_compiler_options(fw_srcs "-Wno-array-bounds")  # GCC 9.3.0 (Ubuntu 20.04)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  ocv_append_source_files_cxx_compiler_options(fw_srcs "-Wno-inconsistent-missing-override")  # Clang
endif()

set(dnn_runtime_libs "")

file(GLOB_RECURSE dnn_srcs
     "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp"
)
file(GLOB_RECURSE dnn_int_hdrs
     "${CMAKE_CURRENT_LIST_DIR}/src/*.hpp"
     "${CMAKE_CURRENT_LIST_DIR}/src/*.h"
)
set(dnn_plugin_srcs ${dnn_srcs} ${dnn_int_hdrs})
ocv_list_filterout_ex(dnn_plugin_srcs
    "/src/dnn.cpp$|/src/dnn_utils.cpp$|/src/dnn_read.cpp$|/src/registry.cpp$|/src/backend.cpp$"
    # importers
    "/src/(caffe|onnx|tensorflow)/"
    # executors
    "/src/(cuda|cuda4dnn|ocl4dnn|vkcom|webnn)/"
)

ocv_option(OPENCV_DNN_OPENVINO "Build with OpenVINO support (2021.4+)" (TARGET ocv.3rdparty.openvino))
if(TARGET ocv.3rdparty.openvino AND OPENCV_DNN_OPENVINO)
  if(NOT HAVE_OPENVINO AND NOT HAVE_NGRAPH)
    message(FATAL_ERROR "DNN: Inference Engine is not supported without enabled 'nGraph'. Check build configuration.")
  endif()
  if("openvino" IN_LIST DNN_PLUGIN_LIST OR DNN_PLUGIN_LIST STREQUAL "all")
    # plugin doesn't support PCH, separate directory scope is necessary
    # opencv_world requires absolute path
    add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/misc/plugin/openvino" "${CMAKE_CURRENT_BINARY_DIR}/dnn_plugin_openvino")
  elseif(NOT OPENCV_DNN_BUILTIN_BACKEND)
    list(APPEND dnn_runtime_libs ocv.3rdparty.openvino)
  endif()
endif()

set(OPENCV_DNN_BACKEND_DEFAULT "" CACHE STRING "Default backend used by the DNN module (DNN_BACKEND_OPENCV if empty)")
if(OPENCV_DNN_BACKEND_DEFAULT)
  ocv_append_source_file_compile_definitions("${CMAKE_CURRENT_LIST_DIR}/src/dnn_params.cpp" "OPENCV_DNN_BACKEND_DEFAULT=${OPENCV_DNN_BACKEND_DEFAULT}")
endif()

ocv_install_used_external_targets(${libs} ${dnn_runtime_libs})

ocv_glob_module_sources(${sources_options} SOURCES ${fw_srcs} ${webnn_srcs} ${MLAS_OBJECTS})
ocv_create_module(${libs} ${dnn_runtime_libs})
ocv_add_samples()
ocv_add_accuracy_tests(${dnn_runtime_libs})

if(NOT BUILD_PROTOBUF)
  if(TARGET opencv_test_dnn)
    ocv_target_compile_definitions(opencv_test_dnn PRIVATE "OPENCV_DNN_EXTERNAL_PROTOBUF=1")
  endif()
endif()

# Include Tokenizer
file(GLOB extra_tokenizer_src
     "${CMAKE_CURRENT_LIST_DIR}/src/tokenizer/*.cpp"
)
list(APPEND include_dirs
     "${CMAKE_CURRENT_LIST_DIR}/src/tokenizer"
)
ocv_glob_module_sources(${sources_options} SOURCES ${fw_srcs} ${webnn_srcs} ${extra_tokenizer_src})

set(perf_path "${CMAKE_CURRENT_LIST_DIR}/perf")
file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
ocv_add_perf_tests(${dnn_runtime_libs}
    FILES test_common "${CMAKE_CURRENT_LIST_DIR}/test/test_common.hpp" "${CMAKE_CURRENT_LIST_DIR}/test/test_common.impl.hpp"
    FILES Src ${perf_srcs}
    FILES Include ${perf_hdrs}
)

if(DNN_ENABLE_PLUGINS)
  ocv_target_compile_definitions(${the_module} PRIVATE ENABLE_PLUGINS)
  if(TARGET opencv_test_dnn)
    ocv_target_compile_definitions(opencv_test_dnn PRIVATE ENABLE_PLUGINS)
  endif()
  if(OPENCV_DEBUG_POSTFIX)
    ocv_append_source_file_compile_definitions("${CMAKE_CURRENT_LIST_DIR}/src/backend.cpp" "DEBUG_POSTFIX=${OPENCV_DEBUG_POSTFIX}")
  endif()
endif()

ocv_option(OPENCV_TEST_DNN_OPENVINO "Build test with OpenVINO code" (TARGET ocv.3rdparty.openvino))
if(TARGET ocv.3rdparty.openvino AND OPENCV_TEST_DNN_OPENVINO)
  if(TARGET opencv_test_dnn)
    ocv_target_link_libraries(opencv_test_dnn ocv.3rdparty.openvino)
  endif()
endif()

ocv_option(OPENCV_TEST_DNN_CANN "Build test with CANN" (TARGET ocv.3rdparty.cann))
if(TARGET ocv.3rdparty.cann AND OPENCV_TEST_DNN_CANN)
  if(TARGET opencv_test_dnn)
    ocv_target_link_libraries(opencv_test_dnn ocv.3rdparty.cann)
  endif()
endif()

ocv_option(OPENCV_TEST_DNN_TIMVX "Build test with TIM-VX" (HAVE_TIMVX))
if(OPENCV_TEST_DNN_TIMVX)
  if(TARGET opencv_test_dnn)
    ocv_target_compile_definitions(opencv_test_dnn PRIVATE "HAVE_TIMVX=1")
  endif()
endif()

ocv_option(OPENCV_TEST_DNN_TFLITE "Build test with TFLite" (OPENCV_DNN_TFLITE))
if(OPENCV_TEST_DNN_TFLITE)
  if(TARGET opencv_test_dnn)
    ocv_target_compile_definitions(opencv_test_dnn PRIVATE "OPENCV_TEST_DNN_TFLITE=1")
  endif()
  if(TARGET opencv_perf_dnn)
    ocv_target_compile_definitions(opencv_perf_dnn PRIVATE "OPENCV_TEST_DNN_TFLITE=1")
  endif()
endif()
