Debian Codegraph @

cmake/protobuf.cmake

raw
1 # Copyright 2017 gRPC authors.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 if(gRPC_PROTOBUF_PROVIDER STREQUAL "module")
16 # Building the protobuf tests require gmock what is not part of a standard protobuf checkout.
17 # Disable them unless they are explicitly requested from the cmake command line (when we assume
18 # gmock is downloaded to the right location inside protobuf).
19 if(NOT protobuf_BUILD_TESTS)
20 set(protobuf_BUILD_TESTS OFF CACHE BOOL "Build protobuf tests")
21 endif()
22 # Disable building protobuf with zlib. Building protobuf with zlib breaks
23 # the build if zlib is not installed on the system.
24 if(NOT protobuf_WITH_ZLIB)
25 set(protobuf_WITH_ZLIB OFF CACHE BOOL "Build protobuf with zlib.")
26 endif()
27 if(NOT PROTOBUF_ROOT_DIR)
28 set(PROTOBUF_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/protobuf)
29 endif()
30
31 if(EXISTS "${PROTOBUF_ROOT_DIR}/cmake/CMakeLists.txt")
32 set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "Link static runtime libraries")
33 add_subdirectory(${PROTOBUF_ROOT_DIR}/cmake third_party/protobuf)
34 if(TARGET ${_gRPC_PROTOBUF_LIBRARY_NAME})
35 set(_gRPC_PROTOBUF_LIBRARIES ${_gRPC_PROTOBUF_LIBRARY_NAME})
36 endif()
37 if(TARGET libprotoc)
38 set(_gRPC_PROTOBUF_PROTOC_LIBRARIES libprotoc)
39 endif()
40 if(TARGET protoc)
41 set(_gRPC_PROTOBUF_PROTOC protoc)
42 if(CMAKE_CROSSCOMPILING)
43 find_program(_gRPC_PROTOBUF_PROTOC_EXECUTABLE protoc)
44 else()
45 set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE $<TARGET_FILE:protoc>)
46 endif()
47 endif()
48 # For well-known .proto files distributed with protobuf
49 set(_gRPC_PROTOBUF_WELLKNOWN_INCLUDE_DIR "${PROTOBUF_ROOT_DIR}/src")
50 else()
51 message(WARNING "gRPC_PROTOBUF_PROVIDER is \"module\" but PROTOBUF_ROOT_DIR is wrong")
52 endif()
53 if(gRPC_INSTALL AND NOT _gRPC_INSTALL_SUPPORTED_FROM_MODULE)
54 message(WARNING "gRPC_INSTALL will be forced to FALSE because gRPC_PROTOBUF_PROVIDER is \"module\" and CMake version (${CMAKE_VERSION}) is less than 3.13.")
55 set(gRPC_INSTALL FALSE)
56 endif()
57 elseif(gRPC_PROTOBUF_PROVIDER STREQUAL "package")
58 find_package(Protobuf REQUIRED ${gRPC_PROTOBUF_PACKAGE_TYPE})
59
60 # {Protobuf,PROTOBUF}_FOUND is defined based on find_package type ("MODULE" vs "CONFIG").
61 # For "MODULE", the case has also changed between cmake 3.5 and 3.6.
62 # We use the legacy uppercase version for *_LIBRARIES AND *_INCLUDE_DIRS variables
63 # as newer cmake versions provide them too for backward compatibility.
64 if(Protobuf_FOUND OR PROTOBUF_FOUND)
65 if(TARGET protobuf::${_gRPC_PROTOBUF_LIBRARY_NAME})
66 set(_gRPC_PROTOBUF_LIBRARIES protobuf::${_gRPC_PROTOBUF_LIBRARY_NAME})
67 else()
68 set(_gRPC_PROTOBUF_LIBRARIES ${PROTOBUF_LIBRARIES})
69 endif()
70 if(TARGET protobuf::libprotoc)
71 set(_gRPC_PROTOBUF_PROTOC_LIBRARIES protobuf::libprotoc)
72 # extract the include dir from target's properties
73 get_target_property(_gRPC_PROTOBUF_WELLKNOWN_INCLUDE_DIR protobuf::libprotoc INTERFACE_INCLUDE_DIRECTORIES)
74 else()
75 set(_gRPC_PROTOBUF_PROTOC_LIBRARIES ${PROTOBUF_PROTOC_LIBRARIES})
76 set(_gRPC_PROTOBUF_WELLKNOWN_INCLUDE_DIR ${PROTOBUF_INCLUDE_DIRS})
77 endif()
78 if(TARGET protobuf::protoc)
79 set(_gRPC_PROTOBUF_PROTOC protobuf::protoc)
80 if(CMAKE_CROSSCOMPILING)
81 find_program(_gRPC_PROTOBUF_PROTOC_EXECUTABLE protoc)
82 else()
83 set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE $<TARGET_FILE:protobuf::protoc>)
84 endif()
85 else()
86 set(_gRPC_PROTOBUF_PROTOC ${PROTOBUF_PROTOC_EXECUTABLE})
87 if(CMAKE_CROSSCOMPILING)
88 find_program(_gRPC_PROTOBUF_PROTOC_EXECUTABLE protoc)
89 else()
90 set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE ${PROTOBUF_PROTOC_EXECUTABLE})
91 endif()
92 endif()
93 set(_gRPC_FIND_PROTOBUF "if(NOT Protobuf_FOUND AND NOT PROTOBUF_FOUND)\n find_package(Protobuf ${gRPC_PROTOBUF_PACKAGE_TYPE})\nendif()")
94 endif()
95 endif()