aboutsummaryrefslogtreecommitdiffstats
path: root/recipes-support/opencv/opencv/fix-build-with-protobuf-v22.patch
blob: 3538f24b32eb63a29d79fbb793085354b398fb57 (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
From 5e4150826fea6f37276f348c65d94ce4847d1211 Mon Sep 17 00:00:00 2001
From: Kumataro <Kumataro@users.noreply.github.com>
Date: Sat, 7 Oct 2023 10:11:25 +0900
Subject: [PATCH] 3rdparty: supporting protobuf v22 and later

Upstream-Status: Submitted [https://github.com/opencv/opencv/pull/24372]
---
 cmake/OpenCVFindProtobuf.cmake | 37 ++++++++++++++++++++++++++++++++++
 modules/dnn/CMakeLists.txt     |  9 +++++++++
 2 files changed, 46 insertions(+)

diff --git a/cmake/OpenCVFindProtobuf.cmake b/cmake/OpenCVFindProtobuf.cmake
index 8835347d1d..9bd5c28db8 100644
--- a/cmake/OpenCVFindProtobuf.cmake
+++ b/cmake/OpenCVFindProtobuf.cmake
@@ -67,6 +67,38 @@ else()
   endif()
 endif()
 
+# See https://github.com/opencv/opencv/issues/24369
+# In Protocol Buffers v22.0 and later drops C++11 support and depends abseil-cpp.
+#   Details: https://protobuf.dev/news/2022-08-03/
+# And if std::text_view is in abseil-cpp requests C++17 and later.
+if(HAVE_PROTOBUF)
+    if("${Protobuf_VERSION}" MATCHES [[[0-9]+.([0-9]+).[0-9]+]])
+        string(COMPARE GREATER_EQUAL "${CMAKE_MATCH_1}" "22" REQUEST_ABSL)
+
+        if(REQUEST_ABSL)
+            string(COMPARE GREATER_EQUAL "${CMAKE_CXX_STANDARD}" "17" USED_AFTER_CXX17)
+            if(NOT USED_AFTER_CXX17)
+                message("CMAKE_CXX_STANDARD : ${CMAKE_CXX_STANDARD}")
+                message("protobuf           : ${Protobuf_VERSION}")
+                message(FATAL_ERROR "protobuf(v22 and later) and abseil-cpp request CMAKE_CXX_STANDARD=17 and later.")
+            endif()
+
+            ocv_check_modules(ABSL_STRINGS absl_strings)
+            if(NOT ABSL_STRINGS_FOUND)
+                message(FATAL_ERROR "protobuf(v22 and later) requests abseil-cpp(strings), but missing.")
+            endif()
+
+            ocv_check_modules(ABSL_LOG absl_log)
+            if(NOT ABSL_LOG_FOUND)
+                message(FATAL_ERROR "protobuf(v22 and later) requests abseil-cpp(log), but missing.")
+            endif()
+
+        endif()
+    else()
+        message(FATAL_ERROR "Protobuf version(${Protobuf_VERSION}) is unexpected to split.")
+    endif()
+endif()
+
 if(HAVE_PROTOBUF AND PROTOBUF_UPDATE_FILES AND NOT COMMAND PROTOBUF_GENERATE_CPP)
   message(FATAL_ERROR "Can't configure protobuf dependency (BUILD_PROTOBUF=${BUILD_PROTOBUF} PROTOBUF_UPDATE_FILES=${PROTOBUF_UPDATE_FILES})")
 endif()
@@ -89,3 +121,8 @@ if(HAVE_PROTOBUF)
     BUILD_PROTOBUF THEN "build (${Protobuf_VERSION})"
     ELSE "${__location} (${Protobuf_VERSION})")
 endif()
+
+if(HAVE_ABSL_STRINGS AND HAVE_ABSL_LOG)
+  list(APPEND CUSTOM_STATUS absl)
+  list(APPEND CUSTOM_STATUS_absl "      abseil-cpp:" "YES (${ABSL_STRINGS_VERSION})" )
+endif()
diff --git a/modules/dnn/CMakeLists.txt b/modules/dnn/CMakeLists.txt
index 804b78ead2..d32007b37e 100644
--- a/modules/dnn/CMakeLists.txt
+++ b/modules/dnn/CMakeLists.txt
@@ -149,6 +149,15 @@ if(NOT BUILD_PROTOBUF)
   list(APPEND include_dirs ${Protobuf_INCLUDE_DIRS})
 endif()
 
+if(HAVE_ABSL_STRINGS)
+  list(APPEND libs ${ABSL_STRINGS_LIBRARIES})
+  list(APPEND include_dirs ${ABSL_STRTRINGS_INCLUDE_DIRS})
+endif()
+if(HAVE_ABSL_LOG)
+  list(APPEND libs ${ABSL_LOG_LIBRARIES})
+  list(APPEND include_dirs ${ABSL_LOG_INCLUDE_DIRS})
+endif()
+
 set(sources_options "")
 
 list(APPEND libs ${LAPACK_LIBRARIES})
From 06a7669521d205f647d3e718322ccd153cdbbb77 Mon Sep 17 00:00:00 2001
From: Kumataro <Kumataro@users.noreply.github.com>
Date: Sun, 8 Oct 2023 09:39:35 +0900
Subject: [PATCH] dnn: disable some tests for external protobuf

---
 modules/dnn/CMakeLists.txt         |  5 +++++
 modules/dnn/src/caffe/caffe_io.cpp | 24 ++++++++++++++++++++++--
 modules/dnn/test/test_layers.cpp   | 16 ++++++++++++++++
 3 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/modules/dnn/CMakeLists.txt b/modules/dnn/CMakeLists.txt
index d32007b37e..face38465f 100644
--- a/modules/dnn/CMakeLists.txt
+++ b/modules/dnn/CMakeLists.txt
@@ -254,6 +254,12 @@ 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()
+
 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")

diff --git a/modules/dnn/src/caffe/caffe_io.cpp b/modules/dnn/src/caffe/caffe_io.cpp
index ebecf95eea..ebceca84cf 100644
--- a/modules/dnn/src/caffe/caffe_io.cpp
+++ b/modules/dnn/src/caffe/caffe_io.cpp
@@ -1130,7 +1130,17 @@ bool ReadProtoFromTextFile(const char* filename, Message* proto) {
     parser.AllowUnknownField(true);
     parser.SetRecursionLimit(1000);
 #endif
-    return parser.Parse(&input, proto);
+    const bool ret = parser.Parse(&input, proto);
+
+#ifdef OPENCV_DNN_EXTERNAL_PROTOBUF
+    if(!ret)
+    {
+        LOG(ERROR) << "Some data requires patched protobuf (available in OpenCV source tree only).";
+        CV_Error_(Error::StsError,("Some data requires patched protobuf (available in OpenCV source tree only)."));
+    }
+#endif
+
+    return ret;
 }
 
 bool ReadProtoFromBinaryFile(const char* filename, Message* proto) {
@@ -1148,7 +1158,17 @@ bool ReadProtoFromTextBuffer(const char* data, size_t len, Message* proto) {
     parser.AllowUnknownField(true);
     parser.SetRecursionLimit(1000);
 #endif
-    return parser.Parse(&input, proto);
+    const bool ret = parser.Parse(&input, proto);
+
+#ifdef OPENCV_DNN_EXTERNAL_PROTOBUF
+    if(!ret)
+    {
+        LOG(ERROR) << "Some data requires patched protobuf (available in OpenCV source tree only).";
+        CV_Error_(Error::StsError,("Some data requires patched protobuf (available in OpenCV source tree only)."));
+    }
+#endif
+
+    return ret;
 }
 
 
diff --git a/modules/dnn/test/test_layers.cpp b/modules/dnn/test/test_layers.cpp
index 763d94b99c..a07d442d76 100644
--- a/modules/dnn/test/test_layers.cpp
+++ b/modules/dnn/test/test_layers.cpp
@@ -754,7 +754,11 @@ TEST_F(Layer_RNN_Test, get_set_test)
     EXPECT_EQ(shape(outputs[1]), shape(nT, nS, nH));
 }
 
+#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF
 TEST_P(Test_Caffe_layers, Accum)
+#else
+TEST_P(Test_Caffe_layers, DISABLED_Accum)  // requires patched protobuf (available in OpenCV source tree only)
+#endif
 {
     if (backend == DNN_BACKEND_OPENCV && target != DNN_TARGET_CPU)
         applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL, CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
@@ -778,7 +782,11 @@ TEST_P(Test_Caffe_layers, ChannelNorm)
     testLayerUsingCaffeModels("channel_norm", false, false);
 }
 
+#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF
 TEST_P(Test_Caffe_layers, DataAugmentation)
+#else
+TEST_P(Test_Caffe_layers, DISABLED_DataAugmentation)  // requires patched protobuf (available in OpenCV source tree only)
+#endif
 {
     if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
         applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
@@ -787,7 +795,11 @@ TEST_P(Test_Caffe_layers, DataAugmentation)
     testLayerUsingCaffeModels("data_augmentation_8x6", true, false);
 }
 
+#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF
 TEST_P(Test_Caffe_layers, Resample)
+#else
+TEST_P(Test_Caffe_layers, DISABLED_Resample)  // requires patched protobuf (available in OpenCV source tree only)
+#endif
 {
     if (backend != DNN_BACKEND_OPENCV)
         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
@@ -795,7 +807,11 @@ TEST_P(Test_Caffe_layers, Resample)
     testLayerUsingCaffeModels("nearest", false, false);
 }
 
+#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF
 TEST_P(Test_Caffe_layers, Correlation)
+#else
+TEST_P(Test_Caffe_layers, DISABLED_Correlation)  // requires patched protobuf (available in OpenCV source tree only)
+#endif
 {
     if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER,
From 2c33798f41942aefc203183d673ce4846f32dba4 Mon Sep 17 00:00:00 2001
From: Kumataro <Kumataro@users.noreply.github.com>
Date: Sun, 8 Oct 2023 13:28:40 +0900
Subject: [PATCH] use GREATER instead of GREATER_EQUAL and remove new blank
 line at EOF

---
 cmake/OpenCVFindProtobuf.cmake | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/cmake/OpenCVFindProtobuf.cmake b/cmake/OpenCVFindProtobuf.cmake
index 9bd5c28db8..ba147526d6 100644
--- a/cmake/OpenCVFindProtobuf.cmake
+++ b/cmake/OpenCVFindProtobuf.cmake
@@ -71,12 +71,14 @@ endif()
 # In Protocol Buffers v22.0 and later drops C++11 support and depends abseil-cpp.
 #   Details: https://protobuf.dev/news/2022-08-03/
 # And if std::text_view is in abseil-cpp requests C++17 and later.
+
 if(HAVE_PROTOBUF)
     if("${Protobuf_VERSION}" MATCHES [[[0-9]+.([0-9]+).[0-9]+]])
-        string(COMPARE GREATER_EQUAL "${CMAKE_MATCH_1}" "22" REQUEST_ABSL)
+        string(COMPARE GREATER "${CMAKE_MATCH_1}" "21" REQUEST_ABSL)  # >=22
 
         if(REQUEST_ABSL)
-            string(COMPARE GREATER_EQUAL "${CMAKE_CXX_STANDARD}" "17" USED_AFTER_CXX17)
+            string(COMPARE GREATER "${CMAKE_CXX_STANDARD}" "16" USED_AFTER_CXX17)  # >=17
+
             if(NOT USED_AFTER_CXX17)
                 message("CMAKE_CXX_STANDARD : ${CMAKE_CXX_STANDARD}")
                 message("protobuf           : ${Protobuf_VERSION}")
From cd709eefbeedd116bf3495b42280323b932791ff Mon Sep 17 00:00:00 2001
From: Kumataro <Kumataro@users.noreply.github.com>
Date: Mon, 9 Oct 2023 21:51:04 +0900
Subject: [PATCH] fix for review

---
 cmake/OpenCVFindProtobuf.cmake     |  5 ----
 modules/dnn/src/caffe/caffe_io.cpp | 24 ++----------------
 modules/dnn/test/test_layers.cpp   | 40 +++++++++++++++---------------
 3 files changed, 22 insertions(+), 47 deletions(-)

diff --git a/cmake/OpenCVFindProtobuf.cmake b/cmake/OpenCVFindProtobuf.cmake
index ba147526d6..2faf1010bf 100644
--- a/cmake/OpenCVFindProtobuf.cmake
+++ b/cmake/OpenCVFindProtobuf.cmake
@@ -123,8 +123,3 @@ if(HAVE_PROTOBUF)
     BUILD_PROTOBUF THEN "build (${Protobuf_VERSION})"
     ELSE "${__location} (${Protobuf_VERSION})")
 endif()
-
-if(HAVE_ABSL_STRINGS AND HAVE_ABSL_LOG)
-  list(APPEND CUSTOM_STATUS absl)
-  list(APPEND CUSTOM_STATUS_absl "      abseil-cpp:" "YES (${ABSL_STRINGS_VERSION})" )
-endif()
diff --git a/modules/dnn/src/caffe/caffe_io.cpp b/modules/dnn/src/caffe/caffe_io.cpp
index ebceca84cf..ebecf95eea 100644
--- a/modules/dnn/src/caffe/caffe_io.cpp
+++ b/modules/dnn/src/caffe/caffe_io.cpp
@@ -1130,17 +1130,7 @@ bool ReadProtoFromTextFile(const char* filename, Message* proto) {
     parser.AllowUnknownField(true);
     parser.SetRecursionLimit(1000);
 #endif
-    const bool ret = parser.Parse(&input, proto);
-
-#ifdef OPENCV_DNN_EXTERNAL_PROTOBUF
-    if(!ret)
-    {
-        LOG(ERROR) << "Some data requires patched protobuf (available in OpenCV source tree only).";
-        CV_Error_(Error::StsError,("Some data requires patched protobuf (available in OpenCV source tree only)."));
-    }
-#endif
-
-    return ret;
+    return parser.Parse(&input, proto);
 }
 
 bool ReadProtoFromBinaryFile(const char* filename, Message* proto) {
@@ -1158,17 +1148,7 @@ bool ReadProtoFromTextBuffer(const char* data, size_t len, Message* proto) {
     parser.AllowUnknownField(true);
     parser.SetRecursionLimit(1000);
 #endif
-    const bool ret = parser.Parse(&input, proto);
-
-#ifdef OPENCV_DNN_EXTERNAL_PROTOBUF
-    if(!ret)
-    {
-        LOG(ERROR) << "Some data requires patched protobuf (available in OpenCV source tree only).";
-        CV_Error_(Error::StsError,("Some data requires patched protobuf (available in OpenCV source tree only)."));
-    }
-#endif
-
-    return ret;
+    return parser.Parse(&input, proto);
 }
 
 
diff --git a/modules/dnn/test/test_layers.cpp b/modules/dnn/test/test_layers.cpp
index a07d442d76..5c6fc541d7 100644
--- a/modules/dnn/test/test_layers.cpp
+++ b/modules/dnn/test/test_layers.cpp
@@ -754,17 +754,17 @@ TEST_F(Layer_RNN_Test, get_set_test)
     EXPECT_EQ(shape(outputs[1]), shape(nT, nS, nH));
 }
 
-#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF
 TEST_P(Test_Caffe_layers, Accum)
-#else
-TEST_P(Test_Caffe_layers, DISABLED_Accum)  // requires patched protobuf (available in OpenCV source tree only)
-#endif
 {
+#ifdef OPENCV_DNN_EXTERNAL_PROTOBUF
+    throw SkipTestException("Requires patched protobuf");
+#else
     if (backend == DNN_BACKEND_OPENCV && target != DNN_TARGET_CPU)
         applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL, CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
 
     testLayerUsingCaffeModels("accum", false, false, 0.0, 0.0, 2);
     testLayerUsingCaffeModels("accum_ref", false, false, 0.0, 0.0, 2);
+#endif
 }
 
 TEST_P(Test_Caffe_layers, FlowWarp)
@@ -782,42 +782,42 @@ TEST_P(Test_Caffe_layers, ChannelNorm)
     testLayerUsingCaffeModels("channel_norm", false, false);
 }
 
-#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF
 TEST_P(Test_Caffe_layers, DataAugmentation)
-#else
-TEST_P(Test_Caffe_layers, DISABLED_DataAugmentation)  // requires patched protobuf (available in OpenCV source tree only)
-#endif
 {
+#ifdef OPENCV_DNN_EXTERNAL_PROTOBUF
+    throw SkipTestException("Requires patched protobuf");
+#else
     if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
         applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
     testLayerUsingCaffeModels("data_augmentation", true, false);
     testLayerUsingCaffeModels("data_augmentation_2x1", true, false);
     testLayerUsingCaffeModels("data_augmentation_8x6", true, false);
+#endif
 }
 
-#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF
 TEST_P(Test_Caffe_layers, Resample)
-#else
-TEST_P(Test_Caffe_layers, DISABLED_Resample)  // requires patched protobuf (available in OpenCV source tree only)
-#endif
 {
+#ifdef OPENCV_DNN_EXTERNAL_PROTOBUF
+    throw SkipTestException("Requires patched protobuf");
+#else
     if (backend != DNN_BACKEND_OPENCV)
         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
     testLayerUsingCaffeModels("nearest_2inps", false, false, 0.0, 0.0, 2);
     testLayerUsingCaffeModels("nearest", false, false);
+#endif
 }
 
-#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF
 TEST_P(Test_Caffe_layers, Correlation)
-#else
-TEST_P(Test_Caffe_layers, DISABLED_Correlation)  // requires patched protobuf (available in OpenCV source tree only)
-#endif
 {
+#ifdef OPENCV_DNN_EXTERNAL_PROTOBUF
+    throw SkipTestException("Requires patched protobuf");
+#else
     if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER,
                      CV_TEST_TAG_DNN_SKIP_OPENCL, CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
     testLayerUsingCaffeModels("correlation", false, false, 0.0, 0.0, 2);
 }
+#endif
 
 TEST_P(Test_Caffe_layers, Convolution2Inputs)
 {
@@ -1644,12 +1644,11 @@ private:
     int outWidth, outHeight, zoomFactor;
 };
 
-#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF
 TEST_P(Test_Caffe_layers, Interp)
-#else
-TEST_P(Test_Caffe_layers, DISABLED_Interp)  // requires patched protobuf (available in OpenCV source tree only)
-#endif
 {
+#ifdef OPENCV_DNN_EXTERNAL_PROTOBUF
+    throw SkipTestException("Requires patched protobuf");
+#else
 #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2022010000)
     // Cannot get memory!
     if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_CPU)
@@ -1686,6 +1685,7 @@ TEST_P(Test_Caffe_layers, DISABLED_Interp)  // requires patched protobuf (availa
 
     // Test an implemented layer.
     testLayerUsingCaffeModels("layer_interp", false, false);
+#endif
 }
 
 INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_Caffe_layers, dnnBackendsAndTargets());