xref: /aosp_15_r20/external/grpc-grpc/cmake/protobuf.cmake (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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
15if(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}/CMakeLists.txt")
32    set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "Link static runtime libraries")
33    add_subdirectory(${PROTOBUF_ROOT_DIR} 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()
57elseif(gRPC_PROTOBUF_PROVIDER STREQUAL "package")
58  find_package(Protobuf REQUIRED CONFIG)
59
60  if(Protobuf_FOUND OR PROTOBUF_FOUND)
61    if(TARGET protobuf::${_gRPC_PROTOBUF_LIBRARY_NAME})
62      set(_gRPC_PROTOBUF_LIBRARIES protobuf::${_gRPC_PROTOBUF_LIBRARY_NAME})
63    else()
64      set(_gRPC_PROTOBUF_LIBRARIES ${PROTOBUF_LIBRARIES})
65    endif()
66    if(TARGET protobuf::libprotoc)
67      set(_gRPC_PROTOBUF_PROTOC_LIBRARIES protobuf::libprotoc)
68      # extract the include dir from target's properties
69      get_target_property(_gRPC_PROTOBUF_WELLKNOWN_INCLUDE_DIR protobuf::libprotoc INTERFACE_INCLUDE_DIRECTORIES)
70    else()
71      set(_gRPC_PROTOBUF_PROTOC_LIBRARIES ${PROTOBUF_PROTOC_LIBRARIES})
72      set(_gRPC_PROTOBUF_WELLKNOWN_INCLUDE_DIR ${PROTOBUF_INCLUDE_DIRS})
73    endif()
74    if(TARGET protobuf::protoc)
75      set(_gRPC_PROTOBUF_PROTOC protobuf::protoc)
76      if(CMAKE_CROSSCOMPILING)
77        find_program(_gRPC_PROTOBUF_PROTOC_EXECUTABLE protoc)
78      else()
79        set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE $<TARGET_FILE:protobuf::protoc>)
80      endif()
81    else()
82      set(_gRPC_PROTOBUF_PROTOC ${PROTOBUF_PROTOC_EXECUTABLE})
83      if(CMAKE_CROSSCOMPILING)
84        find_program(_gRPC_PROTOBUF_PROTOC_EXECUTABLE protoc)
85      else()
86        set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE ${PROTOBUF_PROTOC_EXECUTABLE})
87      endif()
88    endif()
89    set(_gRPC_FIND_PROTOBUF "include(CMakeFindDependencyMacro)\nfind_dependency(Protobuf CONFIG)")
90  endif()
91endif()
92