OpenCVFindProtobuf.cmake 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # If protobuf is found - libprotobuf target is available
  2. set(HAVE_PROTOBUF FALSE)
  3. if(NOT WITH_PROTOBUF)
  4. return()
  5. endif()
  6. ocv_option(BUILD_PROTOBUF "Force to build libprotobuf runtime from sources" ON)
  7. ocv_option(PROTOBUF_UPDATE_FILES "Force rebuilding .proto files (protoc should be available)" OFF)
  8. # BUILD_PROTOBUF=OFF: Custom manual protobuf configuration (see find_package(Protobuf) for details):
  9. # - Protobuf_INCLUDE_DIR
  10. # - Protobuf_LIBRARY
  11. # - Protobuf_PROTOC_EXECUTABLE
  12. function(get_protobuf_version version include)
  13. file(STRINGS "${include}/google/protobuf/stubs/common.h" ver REGEX "#define GOOGLE_PROTOBUF_VERSION [0-9]+")
  14. string(REGEX MATCHALL "[0-9]+" ver ${ver})
  15. math(EXPR major "${ver} / 1000000")
  16. math(EXPR minor "${ver} / 1000 % 1000")
  17. math(EXPR patch "${ver} % 1000")
  18. set(${version} "${major}.${minor}.${patch}" PARENT_SCOPE)
  19. endfunction()
  20. if(BUILD_PROTOBUF)
  21. ocv_assert(NOT PROTOBUF_UPDATE_FILES)
  22. add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/protobuf")
  23. set(Protobuf_LIBRARIES "libprotobuf")
  24. set(HAVE_PROTOBUF TRUE)
  25. else()
  26. # we still need this for command PROTOBUF_GENERATE_CPP.
  27. set(protobuf_MODULE_COMPATIBLE ON)
  28. unset(Protobuf_VERSION CACHE)
  29. find_package(Protobuf QUIET CONFIG)
  30. if(NOT Protobuf_FOUND)
  31. find_package(Protobuf QUIET)
  32. endif()
  33. # Backwards compatibility
  34. # Define camel case versions of input variables
  35. foreach(UPPER
  36. PROTOBUF_FOUND
  37. PROTOBUF_LIBRARY
  38. PROTOBUF_INCLUDE_DIR
  39. PROTOBUF_VERSION
  40. )
  41. if (DEFINED ${UPPER})
  42. string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER})
  43. if (NOT DEFINED ${Camel})
  44. set(${Camel} ${${UPPER}})
  45. endif()
  46. endif()
  47. endforeach()
  48. # end of compatibility block
  49. if(Protobuf_FOUND)
  50. if(TARGET protobuf::libprotobuf)
  51. set(Protobuf_LIBRARIES "protobuf::libprotobuf")
  52. else()
  53. add_library(libprotobuf UNKNOWN IMPORTED)
  54. set_target_properties(libprotobuf PROPERTIES
  55. IMPORTED_LOCATION "${Protobuf_LIBRARY}"
  56. INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}"
  57. INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}"
  58. )
  59. get_protobuf_version(Protobuf_VERSION "${Protobuf_INCLUDE_DIR}")
  60. set(Protobuf_LIBRARIES "libprotobuf")
  61. endif()
  62. set(HAVE_PROTOBUF TRUE)
  63. endif()
  64. endif()
  65. # See https://github.com/opencv/opencv/issues/24369
  66. # In Protocol Buffers v22.0 and later drops C++11 support and depends abseil-cpp.
  67. # Details: https://protobuf.dev/news/2022-08-03/
  68. # And if std::text_view is in abseil-cpp requests C++17 and later.
  69. if(HAVE_PROTOBUF)
  70. if(NOT (Protobuf_VERSION VERSION_LESS 22))
  71. if((CMAKE_CXX_STANDARD EQUAL 98) OR (CMAKE_CXX_STANDARD LESS 17))
  72. message(STATUS "CMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD} is too old to support protobuf(${Protobuf_VERSION}) and/or abseil-cpp. Use C++17 or later. Turning HAVE_PROTOBUF off")
  73. set(HAVE_PROTOBUF FALSE)
  74. endif()
  75. endif()
  76. endif()
  77. if(HAVE_PROTOBUF AND PROTOBUF_UPDATE_FILES AND NOT COMMAND PROTOBUF_GENERATE_CPP)
  78. message(FATAL_ERROR "Can't configure protobuf dependency (BUILD_PROTOBUF=${BUILD_PROTOBUF} PROTOBUF_UPDATE_FILES=${PROTOBUF_UPDATE_FILES})")
  79. endif()
  80. if(HAVE_PROTOBUF)
  81. list(APPEND CUSTOM_STATUS protobuf)
  82. if(NOT BUILD_PROTOBUF)
  83. unset( __location)
  84. if(TARGET "${Protobuf_LIBRARIES}")
  85. get_target_property(__location "${Protobuf_LIBRARIES}" IMPORTED_LOCATION_RELEASE)
  86. if(NOT __location)
  87. get_target_property(__location "${Protobuf_LIBRARIES}" IMPORTED_LOCATION)
  88. endif()
  89. endif()
  90. if(NOT __location)
  91. if(Protobuf_LIBRARY)
  92. set(__location "${Protobuf_LIBRARY}")
  93. else()
  94. set(__location "${Protobuf_LIBRARIES}")
  95. endif()
  96. endif()
  97. endif()
  98. list(APPEND CUSTOM_STATUS_protobuf " Protobuf:"
  99. BUILD_PROTOBUF THEN "build (${Protobuf_VERSION})"
  100. ELSE "${__location} (${Protobuf_VERSION})")
  101. endif()