1 # If protobuf is found - libprotobuf target is available
3 set(HAVE_PROTOBUF FALSE)
9 ocv_option(BUILD_PROTOBUF "Force to build libprotobuf from sources" ON)
10 ocv_option(PROTOBUF_UPDATE_FILES "Force rebuilding .proto files (protoc should be available)" OFF)
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)
22 add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/protobuf")
23 set(HAVE_PROTOBUF TRUE)
25 unset(Protobuf_VERSION CACHE)
26 find_package(Protobuf QUIET)
28 # Backwards compatibility
29 # Define camel case versions of input variables
37 string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER})
38 if (NOT DEFINED ${Camel})
39 set(${Camel} ${${UPPER}})
43 # end of compatibility block
46 if(TARGET protobuf::libprotobuf)
47 add_library(libprotobuf INTERFACE)
48 target_link_libraries(libprotobuf INTERFACE protobuf::libprotobuf)
50 add_library(libprotobuf UNKNOWN IMPORTED)
51 set_target_properties(libprotobuf PROPERTIES
52 IMPORTED_LOCATION "${Protobuf_LIBRARY}"
53 INTERFACE_INCLUDE_SYSTEM_DIRECTORIES "${Protobuf_INCLUDE_DIR}"
55 get_protobuf_version(Protobuf_VERSION "${Protobuf_INCLUDE_DIR}")
57 set(HAVE_PROTOBUF TRUE)
61 if(HAVE_PROTOBUF AND PROTOBUF_UPDATE_FILES AND NOT COMMAND PROTOBUF_GENERATE_CPP)
62 find_package(Protobuf QUIET)
63 if(NOT COMMAND PROTOBUF_GENERATE_CPP)
64 message(FATAL_ERROR "PROTOBUF_GENERATE_CPP command is not available")
69 list(APPEND CUSTOM_STATUS protobuf)
70 list(APPEND CUSTOM_STATUS_protobuf " Protobuf:"
71 BUILD_PROTOBUF THEN "build (${Protobuf_VERSION})"
72 ELSE "${Protobuf_LIBRARY} (${Protobuf_VERSION})")