CMakeLists.txt 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. if(WINRT)
  2. ocv_module_disable(dnn)
  3. endif()
  4. if(NOT HAVE_PROTOBUF)
  5. ocv_module_disable(opencv_dnn)
  6. endif()
  7. set(the_description "Deep neural network module. It allows to load models from different frameworks and to make forward pass")
  8. ocv_add_dispatched_file_force_all("layers/layers_common" AVX AVX2 AVX512_SKX RVV)
  9. ocv_add_dispatched_file_force_all("int8layers/layers_common" AVX2 AVX512_SKX)
  10. ocv_add_module(dnn opencv_core opencv_imgproc WRAP python java objc js)
  11. ocv_option(OPENCV_DNN_OPENCL "Build with OpenCL support" HAVE_OPENCL AND NOT APPLE)
  12. if(OPENCV_DNN_OPENCL AND HAVE_OPENCL)
  13. add_definitions(-DCV_OCL4DNN=1)
  14. endif()
  15. if(WITH_WEBNN AND HAVE_WEBNN)
  16. add_definitions(-DHAVE_WEBNN=1)
  17. endif()
  18. ocv_option(OPENCV_DNN_CUDA "Build with CUDA support"
  19. HAVE_CUDA
  20. AND HAVE_CUBLAS
  21. AND HAVE_CUDNN
  22. )
  23. if(OPENCV_DNN_CUDA)
  24. if(HAVE_CUDA AND HAVE_CUBLAS AND HAVE_CUDNN)
  25. add_definitions(-DCV_CUDA4DNN=1)
  26. else()
  27. if(NOT HAVE_CUDA)
  28. message(SEND_ERROR "DNN: CUDA backend requires CUDA Toolkit. Please resolve dependency or disable OPENCV_DNN_CUDA=OFF")
  29. elseif(NOT HAVE_CUBLAS)
  30. message(SEND_ERROR "DNN: CUDA backend requires cuBLAS. Please resolve dependency or disable OPENCV_DNN_CUDA=OFF")
  31. elseif(NOT HAVE_CUDNN)
  32. message(SEND_ERROR "DNN: CUDA backend requires cuDNN. Please resolve dependency or disable OPENCV_DNN_CUDA=OFF")
  33. endif()
  34. endif()
  35. endif()
  36. ocv_cmake_hook_append(INIT_MODULE_SOURCES_opencv_dnn "${CMAKE_CURRENT_LIST_DIR}/cmake/hooks/INIT_MODULE_SOURCES_opencv_dnn.cmake")
  37. if(HAVE_TENGINE)
  38. add_definitions(-DHAVE_TENGINE=1)
  39. endif()
  40. if(MSVC)
  41. add_definitions( -D_CRT_SECURE_NO_WARNINGS=1 )
  42. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4244 /wd4267 /wd4018 /wd4355 /wd4800 /wd4251 /wd4996 /wd4146
  43. /wd4305 /wd4127 /wd4100 /wd4512 /wd4125 /wd4389 /wd4510 /wd4610
  44. /wd4702 /wd4456 /wd4457 /wd4065 /wd4310 /wd4661 /wd4506
  45. )
  46. if(MSVC_VERSION LESS 1920) # MSVS 2015/2017, .pb.cc generated files
  47. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4309) # 'static_cast': truncation of constant value
  48. endif()
  49. if(MSVC_VERSION LESS 1920) # <MSVS2019, .pb.cc generated files
  50. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4189) # local variable is initialized but not referenced
  51. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4592) # symbol will be dynamically initialized (implementation limitation)
  52. endif()
  53. else()
  54. ocv_warnings_disable(CMAKE_CXX_FLAGS -Wno-deprecated -Wmissing-prototypes -Wmissing-declarations -Wshadow
  55. -Wunused-parameter -Wsign-compare
  56. )
  57. endif()
  58. if(HAVE_CUDA)
  59. ocv_warnings_disable(CMAKE_CXX_FLAGS -Wundef)
  60. endif()
  61. if(NOT HAVE_CXX11)
  62. ocv_warnings_disable(CMAKE_CXX_FLAGS -Wno-undef) # LANG_CXX11 from protobuf files
  63. endif()
  64. if(APPLE_FRAMEWORK)
  65. ocv_warnings_disable(CMAKE_CXX_FLAGS -Wshorten-64-to-32)
  66. endif()
  67. if(ANDROID)
  68. add_definitions(-DDISABLE_POSIX_MEMALIGN -DTH_DISABLE_HEAP_TRACKING)
  69. endif()
  70. if(NOT BUILD_PROTOBUF)
  71. add_definitions(-DOPENCV_DNN_EXTERNAL_PROTOBUF=1)
  72. endif()
  73. add_definitions(-DHAVE_PROTOBUF=1)
  74. #suppress warnings in autogenerated caffe.pb.* files
  75. ocv_warnings_disable(CMAKE_CXX_FLAGS
  76. /wd4125 /wd4267 /wd4127 /wd4244 /wd4512 /wd4702
  77. /wd4456 /wd4510 /wd4610 /wd4800
  78. /wd4701 /wd4703 # potentially uninitialized local/pointer variable 'value' used
  79. /wd4505 # unreferenced local function has been removed
  80. /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
  81. -wd858 -wd2196
  82. -Winvalid-offsetof # Apple Clang (attr_value.pb.cc)
  83. )
  84. set(include_dirs "")
  85. set(libs "")
  86. if(PROTOBUF_UPDATE_FILES)
  87. file(GLOB proto_files "${CMAKE_CURRENT_LIST_DIR}/src/tensorflow/*.proto" "${CMAKE_CURRENT_LIST_DIR}/src/caffe/opencv-caffe.proto" "${CMAKE_CURRENT_LIST_DIR}/src/onnx/opencv-onnx.proto")
  88. set(PROTOBUF_GENERATE_CPP_APPEND_PATH ON) # required for tensorflow
  89. protobuf_generate_cpp(fw_srcs fw_hdrs ${proto_files})
  90. else()
  91. 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")
  92. 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")
  93. set(fw_inc "${CMAKE_CURRENT_LIST_DIR}/misc/caffe" "${CMAKE_CURRENT_LIST_DIR}/misc/tensorflow" "${CMAKE_CURRENT_LIST_DIR}/misc/onnx")
  94. endif()
  95. list(APPEND include_dirs ${fw_inc})
  96. list(APPEND libs ${Protobuf_LIBRARIES})
  97. if(NOT BUILD_PROTOBUF)
  98. list(APPEND include_dirs ${Protobuf_INCLUDE_DIRS})
  99. endif()
  100. set(sources_options "")
  101. list(APPEND libs ${LAPACK_LIBRARIES})
  102. if(OPENCV_DNN_OPENCL AND HAVE_OPENCL)
  103. list(APPEND include_dirs ${OPENCL_INCLUDE_DIRS})
  104. else()
  105. set(sources_options EXCLUDE_OPENCL)
  106. endif()
  107. if(OPENCV_DNN_CUDA AND HAVE_CUDA AND HAVE_CUBLAS AND HAVE_CUDNN)
  108. list(APPEND include_dirs ${CUDA_TOOLKIT_INCLUDE} ${CUDNN_INCLUDE_DIRS})
  109. set(CC_LIST ${CUDA_ARCH_BIN})
  110. separate_arguments(CC_LIST)
  111. foreach(cc ${CC_LIST})
  112. if(cc VERSION_LESS 3.0)
  113. 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.")
  114. endif()
  115. endforeach()
  116. unset(CC_LIST)
  117. else()
  118. set(sources_options ${sources_options} EXCLUDE_CUDA)
  119. endif()
  120. if(HAVE_TENGINE)
  121. list(APPEND include_dirs ${TENGINE_INCLUDE_DIRS})
  122. list(APPEND libs -Wl,--whole-archive ${TENGINE_LIBRARIES} -Wl,--no-whole-archive)
  123. endif()
  124. set(webnn_srcs "")
  125. if(NOT EMSCRIPTEN)
  126. if(HAVE_WEBNN)
  127. list(APPEND include_dirs ${WEBNN_HEADER_DIRS})
  128. list(APPEND include_dirs ${WEBNN_INCLUDE_DIRS})
  129. list(APPEND libs -Wl,--whole-archive ${WEBNN_LIBRARIES} -Wl,--no-whole-archive)
  130. list(APPEND webnn_srcs $ENV{WEBNN_NATIVE_DIR}/gen/src/webnn/webnn_cpp.cpp)
  131. endif()
  132. endif()
  133. ocv_module_include_directories(${include_dirs})
  134. if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  135. ocv_append_source_files_cxx_compiler_options(fw_srcs "-Wno-suggest-override") # GCC
  136. ocv_append_source_files_cxx_compiler_options(fw_srcs "-Wno-array-bounds") # GCC 9.3.0 (Ubuntu 20.04)
  137. elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  138. ocv_append_source_files_cxx_compiler_options(fw_srcs "-Wno-inconsistent-missing-override") # Clang
  139. endif()
  140. set(dnn_runtime_libs "")
  141. if(TARGET ocv.3rdparty.openvino)
  142. list(APPEND dnn_runtime_libs ocv.3rdparty.openvino)
  143. endif()
  144. ocv_glob_module_sources(${sources_options} SOURCES ${fw_srcs} ${webnn_srcs})
  145. ocv_create_module(${libs} ${dnn_runtime_libs})
  146. ocv_add_samples()
  147. ocv_add_accuracy_tests(${dnn_runtime_libs})
  148. set(perf_path "${CMAKE_CURRENT_LIST_DIR}/perf")
  149. file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
  150. file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
  151. ocv_add_perf_tests(${dnn_runtime_libs}
  152. FILES test_common "${CMAKE_CURRENT_LIST_DIR}/test/test_common.hpp" "${CMAKE_CURRENT_LIST_DIR}/test/test_common.impl.hpp"
  153. FILES Src ${perf_srcs}
  154. FILES Include ${perf_hdrs}
  155. )
  156. ocv_option(OPENCV_DNN_PERF_CAFFE "Add performance tests of Caffe framework" OFF)
  157. ocv_option(OPENCV_DNN_PERF_CLCAFFE "Add performance tests of clCaffe framework" OFF)
  158. if(BUILD_PERF_TESTS)
  159. if (OPENCV_DNN_PERF_CAFFE
  160. OR ${the_module}_PERF_CAFFE # compatibility for deprecated option
  161. )
  162. find_package(Caffe QUIET)
  163. if (Caffe_FOUND)
  164. add_definitions(-DHAVE_CAFFE=1)
  165. ocv_target_link_libraries(opencv_perf_dnn caffe)
  166. endif()
  167. elseif(OPENCV_DNN_PERF_CLCAFFE
  168. OR ${the_module}_PERF_CAFFE # compatibility for deprecated option
  169. )
  170. find_package(Caffe QUIET)
  171. if (Caffe_FOUND)
  172. add_definitions(-DHAVE_CLCAFFE=1)
  173. ocv_target_link_libraries(opencv_perf_dnn caffe)
  174. endif()
  175. endif()
  176. endif()