File: CMakeLists.txt

package info (click to toggle)
opencv 4.6.0%2Bdfsg-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 276,172 kB
  • sloc: cpp: 1,079,020; xml: 682,526; python: 43,885; lisp: 30,943; java: 25,642; ansic: 7,968; javascript: 5,956; objc: 2,039; sh: 1,017; cs: 601; perl: 494; makefile: 179
file content (220 lines) | stat: -rw-r--r-- 8,520 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
if(WINRT)
  ocv_module_disable(dnn)
endif()

if(NOT HAVE_PROTOBUF)
  ocv_module_disable(opencv_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)
ocv_add_dispatched_file_force_all("int8layers/layers_common" AVX2 AVX512_SKX)

ocv_add_module(dnn opencv_core opencv_imgproc WRAP python java objc js)

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

if(OPENCV_DNN_OPENCL AND HAVE_OPENCL)
  add_definitions(-DCV_OCL4DNN=1)
endif()

if(WITH_WEBNN AND HAVE_WEBNN)
  add_definitions(-DHAVE_WEBNN=1)
endif()

if(HAVE_TIMVX)
  add_definitions(-DHAVE_TIMVX=1)
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)
    add_definitions(-DCV_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(HAVE_TENGINE)
  add_definitions(-DHAVE_TENGINE=1)
endif()

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)
  add_definitions(-DOPENCV_DNN_EXTERNAL_PROTOBUF=1)
endif()

add_definitions(-DHAVE_PROTOBUF=1)

#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 "")

if(PROTOBUF_UPDATE_FILES)
  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")
  set(PROTOBUF_GENERATE_CPP_APPEND_PATH ON) # required for tensorflow
  protobuf_generate_cpp(fw_srcs fw_hdrs ${proto_files})
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()

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)
else()
  set(sources_options ${sources_options} EXCLUDE_CUDA)
endif()

if(HAVE_TENGINE)
	list(APPEND include_dirs ${TENGINE_INCLUDE_DIRS})
	list(APPEND libs -Wl,--whole-archive ${TENGINE_LIBRARIES} -Wl,--no-whole-archive)
endif()

if(HAVE_TIMVX)
    list(APPEND include_dirs ${TIMVX_INCLUDE_DIR})
    list(APPEND libs -Wl,--whole-archive ${TIMVX_LIBRARY} -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()

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 "")

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()
  list(APPEND dnn_runtime_libs ocv.3rdparty.openvino)
endif()

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

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}
)

ocv_option(OPENCV_DNN_PERF_CAFFE "Add performance tests of Caffe framework" OFF)
ocv_option(OPENCV_DNN_PERF_CLCAFFE "Add performance tests of clCaffe framework" OFF)
if(BUILD_PERF_TESTS)
  if (OPENCV_DNN_PERF_CAFFE
      OR ${the_module}_PERF_CAFFE  # compatibility for deprecated option
  )
    find_package(Caffe QUIET)
    if (Caffe_FOUND)
      add_definitions(-DHAVE_CAFFE=1)
      ocv_target_link_libraries(opencv_perf_dnn caffe)
    endif()
  elseif(OPENCV_DNN_PERF_CLCAFFE
         OR ${the_module}_PERF_CAFFE  # compatibility for deprecated option
  )
    find_package(Caffe QUIET)
    if (Caffe_FOUND)
      add_definitions(-DHAVE_CLCAFFE=1)
      ocv_target_link_libraries(opencv_perf_dnn caffe)
    endif()
  endif()
endif()