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 IMPORTED)
48 set_target_properties(libprotobuf PROPERTIES
49 INTERFACE_LINK_LIBRARIES protobuf::libprotobuf
52 add_library(libprotobuf UNKNOWN IMPORTED)
53 set_target_properties(libprotobuf PROPERTIES
54 IMPORTED_LOCATION "${Protobuf_LIBRARY}"
55 INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}"
56 INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}"
58 get_protobuf_version(Protobuf_VERSION "${Protobuf_INCLUDE_DIR}")
60 set(HAVE_PROTOBUF TRUE)
64 if(HAVE_PROTOBUF AND PROTOBUF_UPDATE_FILES AND NOT COMMAND PROTOBUF_GENERATE_CPP)
65 find_package(Protobuf QUIET)
66 if(NOT COMMAND PROTOBUF_GENERATE_CPP)
67 message(FATAL_ERROR "PROTOBUF_GENERATE_CPP command is not available")
72 list(APPEND CUSTOM_STATUS protobuf)
73 list(APPEND CUSTOM_STATUS_protobuf " Protobuf:"
74 BUILD_PROTOBUF THEN "build (${Protobuf_VERSION})"
75 ELSE "${Protobuf_LIBRARY} (${Protobuf_VERSION})")