Home | History | Annotate | Download | only in cmake
      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       set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE $<TARGET_FILE:protoc>)
     43     endif()
     44     set(_gRPC_PROTOBUF_INCLUDE_DIR "${PROTOBUF_ROOT_DIR}")
     45     # For well-known .proto files distributed with protobuf
     46     set(_gRPC_PROTOBUF_WELLKNOWN_INCLUDE_DIR "${PROTOBUF_ROOT_DIR}/src")
     47   else()
     48       message(WARNING "gRPC_PROTOBUF_PROVIDER is \"module\" but PROTOBUF_ROOT_DIR is wrong")
     49   endif()
     50   if(gRPC_INSTALL)
     51     message(WARNING "gRPC_INSTALL will be forced to FALSE because gRPC_PROTOBUF_PROVIDER is \"module\"")
     52     set(gRPC_INSTALL FALSE)
     53   endif()
     54 elseif("${gRPC_PROTOBUF_PROVIDER}" STREQUAL "package")
     55   find_package(Protobuf REQUIRED ${gRPC_PROTOBUF_PACKAGE_TYPE})
     56 
     57   # {Protobuf,PROTOBUF}_FOUND is defined based on find_package type ("MODULE" vs "CONFIG").
     58   # For "MODULE", the case has also changed between cmake 3.5 and 3.6.
     59   # We use the legacy uppercase version for *_LIBRARIES AND *_INCLUDE_DIRS variables
     60   # as newer cmake versions provide them too for backward compatibility.
     61   if(Protobuf_FOUND OR PROTOBUF_FOUND)
     62     if(TARGET protobuf::${_gRPC_PROTOBUF_LIBRARY_NAME})
     63       set(_gRPC_PROTOBUF_LIBRARIES protobuf::${_gRPC_PROTOBUF_LIBRARY_NAME})
     64     else()
     65       set(_gRPC_PROTOBUF_LIBRARIES ${PROTOBUF_LIBRARIES})
     66     endif()
     67     if(TARGET protobuf::libprotoc)
     68       set(_gRPC_PROTOBUF_PROTOC_LIBRARIES protobuf::libprotoc)
     69       # extract the include dir from target's properties
     70       get_target_property(_gRPC_PROTOBUF_WELLKNOWN_INCLUDE_DIR protobuf::libprotoc INTERFACE_INCLUDE_DIRECTORIES)
     71     else()
     72       set(_gRPC_PROTOBUF_PROTOC_LIBRARIES ${PROTOBUF_PROTOC_LIBRARIES})
     73       set(_gRPC_PROTOBUF_WELLKNOWN_INCLUDE_DIR ${PROTOBUF_INCLUDE_DIRS})
     74     endif()
     75     if(TARGET protobuf::protoc)
     76       set(_gRPC_PROTOBUF_PROTOC protobuf::protoc)
     77       set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE $<TARGET_FILE:protobuf::protoc>)
     78     else()
     79       set(_gRPC_PROTOBUF_PROTOC ${PROTOBUF_PROTOC_EXECUTABLE})
     80       set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE ${PROTOBUF_PROTOC_EXECUTABLE})
     81     endif()
     82     set(_gRPC_PROTOBUF_INCLUDE_DIR ${PROTOBUF_INCLUDE_DIRS})
     83     set(_gRPC_FIND_PROTOBUF "if(NOT Protobuf_FOUND AND NOT PROTOBUF_FOUND)\n  find_package(Protobuf ${gRPC_PROTOBUF_PACKAGE_TYPE})\nendif()")
     84   endif()
     85 endif()
     86