1 # If protobuf is found - libprotobuf target is available
3 set(HAVE_PROTOBUF FALSE)
9 ocv_option(BUILD_PROTOBUF "Force to build libprotobuf runtime from sources" ON)
10 ocv_option(PROTOBUF_UPDATE_FILES "Force rebuilding .proto files (protoc should be available)" OFF)
12 # BUILD_PROTOBUF=OFF: Custom manual protobuf configuration (see find_package(Protobuf) for details):
13 # - Protobuf_INCLUDE_DIR
15 # - Protobuf_PROTOC_EXECUTABLE
18 function(get_protobuf_version version include)
19 file(STRINGS "${include}/google/protobuf/stubs/common.h" ver REGEX "#define GOOGLE_PROTOBUF_VERSION [0-9]+")
20 string(REGEX MATCHALL "[0-9]+" ver ${ver})
21 math(EXPR major "${ver} / 1000000")
22 math(EXPR minor "${ver} / 1000 % 1000")
23 math(EXPR patch "${ver} % 1000")
24 set(${version} "${major}.${minor}.${patch}" PARENT_SCOPE)
28 ocv_assert(NOT PROTOBUF_UPDATE_FILES)
29 add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/protobuf")
30 set(Protobuf_LIBRARIES "libprotobuf")
31 set(HAVE_PROTOBUF TRUE)
33 unset(Protobuf_VERSION CACHE)
34 find_package(Protobuf QUIET)
36 # Backwards compatibility
37 # Define camel case versions of input variables
45 string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER})
46 if (NOT DEFINED ${Camel})
47 set(${Camel} ${${UPPER}})
51 # end of compatibility block
54 if(TARGET protobuf::libprotobuf)
55 set(Protobuf_LIBRARIES "protobuf::libprotobuf")
57 add_library(libprotobuf UNKNOWN IMPORTED)
58 set_target_properties(libprotobuf PROPERTIES
59 IMPORTED_LOCATION "${Protobuf_LIBRARY}"
60 INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}"
61 INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}"
63 get_protobuf_version(Protobuf_VERSION "${Protobuf_INCLUDE_DIR}")
64 set(Protobuf_LIBRARIES "libprotobuf")
66 set(HAVE_PROTOBUF TRUE)
70 if(HAVE_PROTOBUF AND PROTOBUF_UPDATE_FILES AND NOT COMMAND PROTOBUF_GENERATE_CPP)
71 message(FATAL_ERROR "Can't configure protobuf dependency (BUILD_PROTOBUF=${BUILD_PROTOBUF} PROTOBUF_UPDATE_FILES=${PROTOBUF_UPDATE_FILES})")
75 list(APPEND CUSTOM_STATUS protobuf)
76 if(NOT BUILD_PROTOBUF)
77 if(TARGET "${Protobuf_LIBRARIES}")
78 get_target_property(__location "${Protobuf_LIBRARIES}" IMPORTED_LOCATION_RELEASE)
80 get_target_property(__location "${Protobuf_LIBRARIES}" IMPORTED_LOCATION)
82 elseif(Protobuf_LIBRARY)
83 set(__location "${Protobuf_LIBRARY}")
85 set(__location "${Protobuf_LIBRARIES}")
88 list(APPEND CUSTOM_STATUS_protobuf " Protobuf:"
89 BUILD_PROTOBUF THEN "build (${Protobuf_VERSION})"
90 ELSE "${__location} (${Protobuf_VERSION})")