Merge pull request #19814 from alalek:pyopencv_to_safe
[platform/upstream/opencv.git] / cmake / OpenCVFindProtobuf.cmake
1 # If protobuf is found - libprotobuf target is available
2
3 set(HAVE_PROTOBUF FALSE)
4
5 if(NOT WITH_PROTOBUF)
6   return()
7 endif()
8
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)
11
12 # BUILD_PROTOBUF=OFF: Custom manual protobuf configuration (see find_package(Protobuf) for details):
13 # - Protobuf_INCLUDE_DIR
14 # - Protobuf_LIBRARY
15 # - Protobuf_PROTOC_EXECUTABLE
16
17
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)
25 endfunction()
26
27 if(BUILD_PROTOBUF)
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)
32 else()
33   unset(Protobuf_VERSION CACHE)
34   find_package(Protobuf QUIET)
35
36   # Backwards compatibility
37   # Define camel case versions of input variables
38   foreach(UPPER
39       PROTOBUF_FOUND
40       PROTOBUF_LIBRARY
41       PROTOBUF_INCLUDE_DIR
42       PROTOBUF_VERSION
43       )
44       if (DEFINED ${UPPER})
45           string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER})
46           if (NOT DEFINED ${Camel})
47               set(${Camel} ${${UPPER}})
48           endif()
49       endif()
50   endforeach()
51   # end of compatibility block
52
53   if(Protobuf_FOUND)
54     if(TARGET protobuf::libprotobuf)
55       set(Protobuf_LIBRARIES "protobuf::libprotobuf")
56     else()
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}"
62       )
63       get_protobuf_version(Protobuf_VERSION "${Protobuf_INCLUDE_DIR}")
64       set(Protobuf_LIBRARIES "libprotobuf")
65     endif()
66     set(HAVE_PROTOBUF TRUE)
67   endif()
68 endif()
69
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})")
72 endif()
73
74 if(HAVE_PROTOBUF)
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)
79       if(NOT __location)
80         get_target_property(__location "${Protobuf_LIBRARIES}" IMPORTED_LOCATION)
81       endif()
82     elseif(Protobuf_LIBRARY)
83       set(__location "${Protobuf_LIBRARY}")
84     else()
85       set(__location "${Protobuf_LIBRARIES}")
86     endif()
87   endif()
88   list(APPEND CUSTOM_STATUS_protobuf "    Protobuf:"
89     BUILD_PROTOBUF THEN "build (${Protobuf_VERSION})"
90     ELSE "${__location} (${Protobuf_VERSION})")
91 endif()