OpenCVFindProtobuf.cmake 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. unset(Protobuf_VERSION CACHE)
  27. find_package(Protobuf QUIET)
  28. # Backwards compatibility
  29. # Define camel case versions of input variables
  30. foreach(UPPER
  31. PROTOBUF_FOUND
  32. PROTOBUF_LIBRARY
  33. PROTOBUF_INCLUDE_DIR
  34. PROTOBUF_VERSION
  35. )
  36. if (DEFINED ${UPPER})
  37. string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER})
  38. if (NOT DEFINED ${Camel})
  39. set(${Camel} ${${UPPER}})
  40. endif()
  41. endif()
  42. endforeach()
  43. # end of compatibility block
  44. if(Protobuf_FOUND)
  45. if(TARGET protobuf::libprotobuf)
  46. set(Protobuf_LIBRARIES "protobuf::libprotobuf")
  47. else()
  48. add_library(libprotobuf UNKNOWN IMPORTED)
  49. set_target_properties(libprotobuf PROPERTIES
  50. IMPORTED_LOCATION "${Protobuf_LIBRARY}"
  51. INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}"
  52. INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}"
  53. )
  54. get_protobuf_version(Protobuf_VERSION "${Protobuf_INCLUDE_DIR}")
  55. set(Protobuf_LIBRARIES "libprotobuf")
  56. endif()
  57. set(HAVE_PROTOBUF TRUE)
  58. endif()
  59. endif()
  60. if(HAVE_PROTOBUF AND PROTOBUF_UPDATE_FILES AND NOT COMMAND PROTOBUF_GENERATE_CPP)
  61. message(FATAL_ERROR "Can't configure protobuf dependency (BUILD_PROTOBUF=${BUILD_PROTOBUF} PROTOBUF_UPDATE_FILES=${PROTOBUF_UPDATE_FILES})")
  62. endif()
  63. if(HAVE_PROTOBUF)
  64. list(APPEND CUSTOM_STATUS protobuf)
  65. if(NOT BUILD_PROTOBUF)
  66. if(TARGET "${Protobuf_LIBRARIES}")
  67. get_target_property(__location "${Protobuf_LIBRARIES}" IMPORTED_LOCATION_RELEASE)
  68. if(NOT __location)
  69. get_target_property(__location "${Protobuf_LIBRARIES}" IMPORTED_LOCATION)
  70. endif()
  71. elseif(Protobuf_LIBRARY)
  72. set(__location "${Protobuf_LIBRARY}")
  73. else()
  74. set(__location "${Protobuf_LIBRARIES}")
  75. endif()
  76. endif()
  77. list(APPEND CUSTOM_STATUS_protobuf " Protobuf:"
  78. BUILD_PROTOBUF THEN "build (${Protobuf_VERSION})"
  79. ELSE "${__location} (${Protobuf_VERSION})")
  80. endif()