Skip to content

supporting protobuf v22 and later(with abseil-cpp/C++17)#24372

Merged
asmorkalov merged 8 commits intoopencv:4.xfrom
Kumataro:fix24369
Oct 19, 2023
Merged

supporting protobuf v22 and later(with abseil-cpp/C++17)#24372
asmorkalov merged 8 commits intoopencv:4.xfrom
Kumataro:fix24369

Conversation

@Kumataro
Copy link
Copy Markdown
Contributor

@Kumataro Kumataro commented Oct 8, 2023

fix #24369
related #23791

  1. This patch supports external protobuf v22 and later, it required abseil-cpp and c++17.
    Even if the built-in protobuf is upgraded to v22 or later,
    the dependency on abseil-cpp and the requirement for C++17 will continue.
  2. Some test for caffe required patched protobuf, so this patch disable them.

This patch is tested by following libraries.

  • Protobuf: /usr/local/lib/libprotobuf.so (4.24.4)
  • abseil-cpp: YES (20230125)

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@Kumataro
Copy link
Copy Markdown
Contributor Author

Kumataro commented Oct 9, 2023

I was relieved that the test passed completely.

And this patch requires special setting to install protobuf v24+.

How to install external protobuf v24 and abseil-cpp at Ubuntu 23.04.

git clone https://github.com/protocolbuffers/protobuf.git
cd protobuf
  git checkout v4.24.4
  git reset --hard
  git submodule update --init --recursive
cd ..

cmake -S protobuf/third_party/abseil-cpp/ -B build_absl -GNinja -DCMAKE_CXX_STANDARD=17
cmake --build build_absl
sudo cmake --install build_absl
sudo ldconfig

cmake -S protobuf/ -B build_protobuf -GNinja -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_SHARED_LIBS=ON -DCMAKE_CXX_STANDARD=17
cmake --build build_protobuf
sudo cmake --install build_protobuf
sudo ldconfig

After that, we can detect protobuf and absl-* at pkg-config --list-all.

To test building opencv4 with options.

cmake -S opencv -B build4-main_cxx17 -DCMAKE_CXX_STANDARD=17 -GNinja -DBUILD_PROTOBUF=OFF -DPROTOBUF_UPDATE_FILES=ON
cmake --build main_cxx17 

If no patch, there are following errors.

/usr/bin/ld: modules/dnn/CMakeFiles/opencv_dnn.dir/attr_value.pb.cc.o: in function `absl::lts_20230125::log_internal::LogMessage& absl::lts_20230125::log_internal::LogMessage::operator<< <34>(char const (&) [34])':
attr_value.pb.cc:(.text._ZN4absl12lts_2023012512log_internal10LogMessagelsILi34EEERS2_RAT__Kc[_ZN4absl12lts_2023012512log_internal10LogMessagelsILi34EEERS2_RAT__Kc]+0x24): undefined reference to `absl::lts_20230125::log_internal::LogMessage::CopyToEncodedBuffer(std::basic_string_view<char, std::char_traits<char> >, absl::lts_20230125::log_internal::LogMessage::StringType)'
/usr/bin/ld: modules/dnn/CMakeFiles/opencv_dnn.dir/attr_value.pb.cc.o: in function `google::protobuf::MapKey::type() const [clone .part.0]':
...


if(HAVE_ABSL_STRINGS AND HAVE_ABSL_LOG)
list(APPEND CUSTOM_STATUS absl)
list(APPEND CUSTOM_STATUS_absl " abseil-cpp:" "YES (${ABSL_STRINGS_VERSION})" )
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

abseil-cpp is a direct dependency of Protobuf, not OpenCV. Not sure that it has to be reported.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using protobuf v22 or later,
the dnn module directly references abseil-cpp library ( via *.pb.cc converted *.pb with protoc ).

Unfortunally, there is also a possibility that OpenCV Users may use a combination of protobuf and abseil-cpp versions that are not expected.

I think it is better to leave abseil-cpp version information as a cmake log.
It is helpful to investifate compile/runtime errors

kmtr@kmtr-VMware-Virtual-Platform:~/work/build4-main_cxx17/modules/dnn/CMakeFiles/opencv_dnn.dir$ nm attr_value.pb.cc.o | c++filt | grep "absl" | head -10
                 U absl::lts_20230125::log_internal::LogMessage::OstreamView::stream()
                 U absl::lts_20230125::log_internal::LogMessage::OstreamView::OstreamView(absl::lts_20230125::log_internal::LogMessage::LogMessageData&)
                 U absl::lts_20230125::log_internal::LogMessage::OstreamView::~OstreamView()
                 U absl::lts_20230125::log_internal::LogMessage::CopyToEncodedBuffer(std::basic_string_view<char, std::char_traits<char> >, absl::lts_20230125::log_internal::LogMessage::StringType)
0000000000000000 W absl::lts_20230125::log_internal::LogMessage& absl::lts_20230125::log_internal::LogMessage::operator<< <14>(char const (&) [14])
0000000000000000 W absl::lts_20230125::log_internal::LogMessage& absl::lts_20230125::log_internal::LogMessage::operator<< <22>(char const (&) [22])
0000000000000000 W absl::lts_20230125::log_internal::LogMessage& absl::lts_20230125::log_internal::LogMessage::operator<< <23>(char const (&) [23])
0000000000000000 W absl::lts_20230125::log_internal::LogMessage& absl::lts_20230125::log_internal::LogMessage::operator<< <2>(char const (&) [2])
0000000000000000 W absl::lts_20230125::log_internal::LogMessage& absl::lts_20230125::log_internal::LogMessage::operator<< <34>(char const (&) [34])
0000000000000000 t absl::lts_20230125::log_internal::LogMessage& absl::lts_20230125::log_internal::LogMessage::operator<< <48>(char const (&) [48]) [clone .isra.0]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it additionally.

When a user builds a protobuf, it is fully expected that the appropriate abseil-cpp version will be applied in the git submodule.

Therefore, I would like to delete the version log. thank you very much!

e.g. https://github.com/protocolbuffers/protobuf/tree/main/third_party

abseil-cpp @ fb3621f Update to latest absl LTS patch 20230802.1 (#14145)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it, thank you for your comment !

ocv_add_accuracy_tests(${dnn_runtime_libs})

if(NOT BUILD_PROTOBUF)
ocv_target_compile_definitions(opencv_test_dnn PRIVATE "OPENCV_DNN_EXTERNAL_PROTOBUF=1")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need "if(TARGET opencv_test_dnn)" as well? I'm seeing:

CMake Error at cmake/OpenCVUtils.cmake:1517 (message):
  ocv_target_compile_definitions: invalid target: 'opencv_test_dnn'
Call Stack (most recent call first):
  modules/dnn/CMakeLists.txt:258 (ocv_target_compile_definitions)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, thank you for your comment, I agree with you.
When ENABLE_TESTS=OFF, same problem is occured.
I push additional patch to fix it.

@shr-project
Copy link
Copy Markdown

Thanks, tested in OpenEmbedded environment where I've first seen this issue and with these changes it builds fine again. Thank you!

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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does abseil has cmake package? Can we use find_package instead of pkg_config and find_library?

@mshabunin
Copy link
Copy Markdown
Contributor

mshabunin commented Oct 11, 2023

Some time ago I've been able to build OpenCV with latest protobuf: #23791 (comment) And I did not need to explicitly link with abseil. Did something change over time or was my configuration different than yours in some way? Maybe you don't need to build abseil separately then it will be built as part of protobuf and will be linked automatically?

@Kumataro
Copy link
Copy Markdown
Contributor Author

Kumataro commented Oct 11, 2023

Maybe you don't need to build abseil separately then it will be built as part of protobuf and will be linked automatically?

Thank you for your comment.

I found https://protobuf.dev/support/migration/#abseil

There are two ways protobuf references abseil-cpp.

  1. protobuf_ABSL_PROVIDER=module (abseil-cpp will be included in protobuf)
  2. protobuf_ABSL_PROVIDER=package (protobuf refers external abseil-cpp)

If abseil-cpp is installed manually in the system before running cmake for protobuf, option 2, that is using external abseil-cpp package, is selected.

Sorry, I'll need some time to work.
My current idea is ....

  1. Search for abseil-cpp with find_package().
  2. If found, add abseil string and abseil log to the link targets of the dnn module with protobuf
  3. (If not ) Only protobuf will be linked for the dnn module.

---- Add comment
Maybe it is not good idea. I can build protobuf including absl-cpp, but if I try to install protobuf, absl-cpp seems to be installed already...

kmtr@kmtr-VMware-Virtual-Platform:~/work/build_protobuf$ sudo ninja install | egrep "protobuf.pc|absl_strings.pc"
-- Up-to-date: /usr/local/lib/pkgconfig/absl_strings.pc
-- Up-to-date: /usr/local/lib/pkgconfig/protobuf.pc

kmtr@kmtr-VMware-Virtual-Platform:~/work/build_protobuf$ nm libprotobuf.so | c++filt | grep " t absl" | head -10
000000000034be74 t absl::lts_20230125::CordBuffer::ConsumeValue(std::basic_string_view<char, std::char_traits<char> >&)
000000000034c13e t absl::lts_20230125::CordBuffer::available_up_to(unsigned long)
000000000034c256 t absl::lts_20230125::CordBuffer::IncreaseLengthBy(unsigned long)
000000000034bfae t absl::lts_20230125::CordBuffer::CreateWithDefaultLimit(unsigned long)
000000000034bc00 t absl::lts_20230125::CordBuffer::Rep::long_available()
000000000034bb88 t absl::lts_20230125::CordBuffer::Rep::short_available()
000000000034bd46 t absl::lts_20230125::CordBuffer::Rep::add_short_length(unsigned long)
000000000034bd22 t absl::lts_20230125::CordBuffer::Rep::set_short_length(unsigned long)
000000000034be56 t absl::lts_20230125::CordBuffer::Rep::Long::Long(absl::lts_20230125::cord_internal::CordRepFlat*)
000000000034be56 t absl::lts_20230125::CordBuffer::Rep::Long::Long(absl::lts_20230125::cord_internal::CordRepFlat*)

@mshabunin
Copy link
Copy Markdown
Contributor

I tried to build protobuf@24.x and OpenCV. As in previous result only minor patch was needed (replace Protobuf prefix with protobuf in two places, see link in my previous comment). Apparently when using Protobuf name, cmake's own search method will be used which does not set imported targets property correctly, that is why it is necessary to search for protobuf package.

protobuf cmake package already searches for abseil (see protobuf-config.cmake):

if(NOT TARGET absl::strings)
  find_package(absl CONFIG)
endif()

and imported target already provides required interface link libraries (see protobuf-targets.cmake):

# Create imported target protobuf::libprotobuf
add_library(protobuf::libprotobuf SHARED IMPORTED)

set_target_properties(protobuf::libprotobuf PROPERTIES
  INTERFACE_COMPILE_DEFINITIONS "PROTOBUF_USE_DLLS"
  INTERFACE_COMPILE_FEATURES "cxx_std_14"
  INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
  INTERFACE_LINK_LIBRARIES "absl::absl_check;absl::absl_log;absl::algorithm;absl::base;absl::bind_front;absl::bits;absl::btree;absl::cleanup;absl::cord;absl::core_headers;absl::debugging;absl::die_if_null;absl::dynamic_annotations;absl::flags;absl::flat_hash_map;absl::flat_hash_set;absl::function_ref;absl::hash;absl::layout;absl::log_initialize;absl::log_severity;absl::memory;absl::node_hash_map;absl::node_hash_set;absl::optional;absl::span;absl::status;absl::statusor;absl::strings;absl::synchronization;absl::time;absl::type_traits;absl::utility;absl::variant"
)

My script:

PD=/work/protobuf/.build

build_pb() {
    rm -rf ${PD}
    mkdir -p ${PD} && pushd ${PD}
    cmake -GNinja \
        -DCMAKE_BUILD_TYPE=Release \
        -Dprotobuf_BUILD_TESTS=OFF \
        -DCMAKE_POSITION_INDEPENDENT_CODE=ON \ # need to be compatible with dynamic linkage
        -DCMAKE_INSTALL_PREFIX=install \
        -Dprotobuf_BUILD_SHARED_LIBS=ON \ # static works as well
        ..
    ninja install
    popd
}

build_opencv() {
    export LD_LIBRARY_PATH=${PD}/install/lib:${LD_LIBRARY_PATH} # need to set this for protoc to work
    mkdir -p $D
    pushd $D && rm -rf *
    cmake \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_PREFIX_PATH=${PD}/install \
        -DWITH_PROTOBUF=ON \
        -DBUILD_PROTOBUF=OFF \
        -DPROTOBUF_UPDATE_FILES=ON \
        -Dprotobuf_MODULE_COMPATIBLE=ON \ # we still need this for command PROTOBUF_GENERATE_CPP
        -DCMAKE_CXX_STANDARD=17 \
        -DCMAKE_BUILD_TYPE=Release \
        -DWITH_OPENCL=OFF \
        ../opencv
    ninja
    popd
}

build_pb
build_opencv

In previous PR I promised to integrate this patch to OpenCV, but completely forgot about it 🤦

@Kumataro
Copy link
Copy Markdown
Contributor Author

I'm sorry to not finished this fix.
It seems that my trial to remove direct-referencing absl-cpp libraries doesn't work well.
I think my operation maybe has some mistakes. I'll re-think about it calmly tomorrow(today).

I tried with Ubuntu23.04.

  • (1) libs in modules/dnn/CMakefile is "ocv.3rdparty.flatbuffers;protobuf::libprotobuf". It is OK.
  • (2) when linking dnn module, the referenced external library is only libprotobuf.so, not absl-cpp. So there are some undefined symbols.
  • (3) pkg-config protobuf --libs outputs with absl libraries.

(1)

-- opencv_dnn: filter out cuda4dnn source code
-- opencv_dnn: ocv_create_module( ocv.3rdparty.flatbuffers protobuf::libprotobuf )
-- Excluding from source files list: <BUILD>/modules/dnn/layers/layers_common.avx.cpp

(2)

 modules/dnn/CMakeFiles/opencv_dnn.dir/opencl_kernels_dnn.cpp.o
 modules/dnn/CMakeFiles/opencv_dnn.dir/layers/cpu_kernels/fast_gemm_kernels.neon.cpp.o
  -Wl,-rpath,/home/kmtr/work/build4-main_cxx17/lib:/usr/local/lib:  
lib/libopencv_imgproc.so.4.8.0  -ldl  -lm  -lpthread  -lrt  
3rdparty/lib/libtegra_hal.a  
/usr/local/lib/libprotobuf.so  
lib/libopencv_core.so.4.8.0 && :

(3)

kmtr@ubuntu:~/work/build4-main_cxx17$ pkg-config protobuf --libs
-L/usr/local/lib -lprotobuf -labsl_log_internal_check_op -labsl_leak_check -labsl_die_if_null -labsl_log_internal_conditions -labsl_log_internal_message -labsl_examine_stack -labsl_log_internal_format -labsl_log_internal_proto -labsl_log_internal_nullguard -labsl_log_internal_log_sink_set -labsl_log_sink -labsl_log_entry -labsl_flags -labsl_flags_internal -labsl_flags_marshalling -labsl_flags_reflection -labsl_flags_private_handle_accessor -labsl_flags_commandlineflag -labsl_flags_commandlineflag_internal -labsl_flags_config -labsl_flags_program_name -labsl_log_initialize -labsl_log_globals -labsl_log_internal_globals -labsl_hash -labsl_city -labsl_low_level_hash -labsl_raw_hash_set -labsl_hashtablez_sampler -labsl_statusor -labsl_status -labsl_cord -labsl_cordz_info -labsl_cord_internal -labsl_cordz_functions -labsl_exponential_biased -labsl_cordz_handle -labsl_crc_cord_state -labsl_crc32c -labsl_crc_internal -labsl_crc_cpu_detect -labsl_bad_optional_access -labsl_str_format_internal -labsl_strerror -labsl_synchronization -labsl_graphcycles_internal -labsl_stacktrace -labsl_symbolize -labsl_debugging_internal -labsl_demangle_internal -labsl_malloc_internal -labsl_time -labsl_civil_time -labsl_time_zone -labsl_bad_variant_access -lutf8_validity -lutf8_range -labsl_strings -labsl_strings_internal -lrt -labsl_base -labsl_spinlock_wait -labsl_int128 -labsl_throw_delegate -labsl_raw_logging_internal -labsl_log_severity

@Kumataro
Copy link
Copy Markdown
Contributor Author

Kumataro commented Oct 11, 2023

Hello, I made the workaround clearly. ( I'll fix it after my job.. )

I tried to debug following command to check protobuf-targets.cmake is available.

message("${libs}")
include(CMakePrintHelpers)
CMAKE_PRINT_PROPERTIES(TARGETS "protobuf::libprotobuf" PROPERTIES INTERFACE_COMPILE_DEFINITIONS INTERFACE_COMPILE_FEA
TURES INTERFACE_INCLUDE_DIRECTORIES INTERFACE_LINK_LIBRARIES)

find_package(Protobuf QUIET CONFIG) is required.

If there is "CONFIG" in find_package(), we can get nessary properties.

 Properties for TARGET protobuf::libprotobuf:
   protobuf::libprotobuf.INTERFACE_COMPILE_DEFINITIONS = "PROTOBUF_USE_DLLS"
   protobuf::libprotobuf.INTERFACE_COMPILE_FEA = <NOTFOUND>
   protobuf::libprotobuf.TURES = <NOTFOUND>
   protobuf::libprotobuf.INTERFACE_INCLUDE_DIRECTORIES = "/usr/local/include"
   protobuf::libprotobuf.INTERFACE_LINK_LIBRARIES = "absl::absl_check;absl::absl_log;absl::algorithm;absl::base;absl::bind_front;absl::bits;absl::btree;absl::cleanup;absl::cord;absl::core_headers;absl::debugging;absl::die_if_null;absl::dynamic_annotations;absl::flags;absl::flat_hash_map;absl::flat_hash_set;absl::function_ref;absl::hash;absl::layout;absl::log_initialize;absl::log_severity;absl::memory;absl::node_hash_map;absl::node_hash_set;absl::optional;absl::span;absl::status;absl::statusor;absl::strings;absl::synchronization;absl::time;absl::type_traits;absl::utility;absl::variant"

if NOT...

 Properties for TARGET protobuf::libprotobuf:
   protobuf::libprotobuf.INTERFACE_COMPILE_DEFINITIONS = <NOTFOUND>
   protobuf::libprotobuf.INTERFACE_COMPILE_FEA = <NOTFOUND>
   protobuf::libprotobuf.TURES = <NOTFOUND>
   protobuf::libprotobuf.INTERFACE_INCLUDE_DIRECTORIES = "/usr/local/include"
   protobuf::libprotobuf.INTERFACE_LINK_LIBRARIES = "Threads::Threads"

Now my test code doesn't reference absl-cpp libraries directly. It works well.

@mshabunin
Copy link
Copy Markdown
Contributor

If there is "CONFIG" in find_package(), we can get nessary properties.

As I understood this is related to cmake's built-in protobuf search mechanism. It seem to be using pkg-config and can not correctly add abseil dependencies. Adding CONFIG or NO_MODULE or specifying name protobuf instead of Protobuf disables built-in search mechanism and uses actual cmake package provided by protobuf.

I'm working on an integration testing harness for protobuf, I'll try to provide some solution today or tomorrow.

@Kumataro
Copy link
Copy Markdown
Contributor Author

Kumataro commented Oct 12, 2023

I believe it is better to wait your comments/suggestions. Thank you very much !

This problem ,which is related with linking abseil-cpp, is discussed at protocolbuffers/protobuf#12292 (Protobuf Github)

However, it is not resolved clearly, it seems that there are many problems.


I checked what cmake version Ubuntu LTS series uses.

Ubuntu CMake protobuf::libprotobuf
14.04 2.8.12.2 N/A
16.04 3.5.1 N/A
18.04 3.10.2 Supported
20.04 3.16.3 Supported
  • abseil-cpp requires cmake 3.10. and protobuf::libprotobuf is supported after 3.10+. So it is not need to consider about cmake version. ( Before building opencv, to compile protobuf v22+ and abseil-cpp is failed.)

@Kumataro
Copy link
Copy Markdown
Contributor Author

I intended to maintain the old branch and commit the code without dependence on ABSL to the new branch.

However, I made a mistake updating the same branch. The modifications are as expected. I'm sorry...

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.")
Copy link
Copy Markdown
Contributor

@mshabunin mshabunin Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually OpenCV does not throw errors if an optional dependency can not be found or used - the dependecy just get turned off.
@asmorkalov , @opencv-alalek , what do you think? Should we show a warning and disable protobuf in case of configuration incompatibilty?

Here and below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review !
I would like to wait for comments for the direction of correction.

And unfortunately, there are some message(FATAL_ERROR) in this file like this block. If error is occured, they will stop configuration.

if(NOT HAVE_FLATBUFFERS)
message(FATAL_ERROR "DNN: TFLite is not supported without enabled 'flatbuffers'. Check build configuration.")
endif()

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

if(NOT HAVE_OPENVINO AND NOT HAVE_NGRAPH)
message(FATAL_ERROR "DNN: Inference Engine is not supported without enabled 'nGraph'. Check build configuration.")
endif()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not a default configuration.
Need to explicitly specify BUILD_PROTOBUF=OFF to go here.
I believe we could these errors here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review!
Yes, it is clear. This is about protobuf, so I set HAVE_PROTOBUF=OFF if old cxx standard is used. (And I forget C++98 ...)

Copy link
Copy Markdown
Contributor

@mshabunin mshabunin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this PR using several different protobuf versions (https://github.com/mshabunin/protobuf-check), builds are fine, test had not run.

Overall looks good to me 👍 , except one question.

@Kumataro
Copy link
Copy Markdown
Contributor Author

Thank you for testing with various protobuf versions!

I also manually verified that there were no problems using the previous version of protobuf (v21), which does not require Abseil-cpp.

Overall looks good to me 👍 , except one question.

I relieve that there were no major problems with this modification.
Thank you very much.


if(HAVE_PROTOBUF)
if("${Protobuf_VERSION}" MATCHES [[[0-9]+.([0-9]+).[0-9]+]])
string(COMPARE GREATER "${CMAKE_MATCH_1}" "21" REQUEST_AFTER_CXX17) # >=22
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the problem with if'(NOT (Protobuf_VERSION VERSION_LESS 22))?

https://cmake.org/cmake/help/latest/command/if.html#version-less

Any non-integer version component or non-integer trailing part of a version component effectively truncates the string at that point.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, It is better than my script. Thank you very much !

if(REQUEST_AFTER_CXX17)
string(COMPARE GREATER "${CMAKE_CXX_STANDARD}" "16" USED_AFTER_CXX17) # >=17

if(NOT USED_AFTER_CXX17)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use if(GREATER) instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comparing result is used with "NOT", so I use LESS.
And I add to check C++98. Thank you !

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.")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not a default configuration.
Need to explicitly specify BUILD_PROTOBUF=OFF to go here.
I believe we could these errors here.

return()
endif()


Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry I have small mistake that is inserting unnecessary blank line.I had created pull request to fix it.

@asmorkalov
Copy link
Copy Markdown
Contributor

@dkurt @opencv-alalek May I merge the patch?

Copy link
Copy Markdown
Contributor

@opencv-alalek opencv-alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@asmorkalov asmorkalov merged commit 6e4280e into opencv:4.x Oct 19, 2023
@asmorkalov asmorkalov mentioned this pull request Nov 3, 2023
IskXCr pushed a commit to Haosonn/opencv that referenced this pull request Dec 20, 2023
Supporting protobuf v22 and later(with abseil-cpp/C++17) opencv#24372

fix opencv#24369
related opencv#23791

1. This patch supports external protobuf v22 and later, it required abseil-cpp and c++17.
    Even if the built-in protobuf is upgraded to v22 or later, 
    the dependency on abseil-cpp and the requirement for C++17 will continue.
2. Some test for caffe required patched protobuf, so this patch disable them.

This patch is tested by following libraries.
-  Protobuf:                    /usr/local/lib/libprotobuf.so (4.24.4)
-  abseil-cpp:                YES (20230125)

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
thewoz pushed a commit to thewoz/opencv that referenced this pull request Jan 4, 2024
Supporting protobuf v22 and later(with abseil-cpp/C++17) opencv#24372

fix opencv#24369
related opencv#23791

1. This patch supports external protobuf v22 and later, it required abseil-cpp and c++17.
    Even if the built-in protobuf is upgraded to v22 or later, 
    the dependency on abseil-cpp and the requirement for C++17 will continue.
2. Some test for caffe required patched protobuf, so this patch disable them.

This patch is tested by following libraries.
-  Protobuf:                    /usr/local/lib/libprotobuf.so (4.24.4)
-  abseil-cpp:                YES (20230125)

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
thewoz pushed a commit to thewoz/opencv that referenced this pull request May 29, 2024
Supporting protobuf v22 and later(with abseil-cpp/C++17) opencv#24372

fix opencv#24369
related opencv#23791

1. This patch supports external protobuf v22 and later, it required abseil-cpp and c++17.
    Even if the built-in protobuf is upgraded to v22 or later, 
    the dependency on abseil-cpp and the requirement for C++17 will continue.
2. Some test for caffe required patched protobuf, so this patch disable them.

This patch is tested by following libraries.
-  Protobuf:                    /usr/local/lib/libprotobuf.so (4.24.4)
-  abseil-cpp:                YES (20230125)

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

supporting protobuf v22 and later(with abseil-cpp/C++17)

6 participants