Skip to content

Commit cc61532

Browse files
authored
impl: prefer Protobuf config vs. module in CMake (#11517)
CMake ships with a `FindProtobuf` module, when one types: ``` find_package(Protobuf) ``` CMake uses that module to find protobuf and add the necessary compile and link-time flags. The next release of Protobuf adds dependencies that the module does not know about. Fortunately the next release also install the CMake support files, which can be used if one types: ``` find_package(Protobuf CONFIG) ``` Because we need to support older versions of Protobuf, we first try with the `CONFIG` flag, and then fallback to the module instead. We install our own CMake configuration files, which need to use `find_dependency()` to find Protobuf. `find_dependency()` is a thin wrapper around `find_package()`, and it needs to use the same strategy that we know worked for building `google-cloud-cpp`. In the generator's CMake file, we use `protobuf_generate_cpp()` which does not always exist.
1 parent 5f1b412 commit cc61532

7 files changed

Lines changed: 29 additions & 15 deletions

File tree

CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,14 @@ add_custom_target(all-docfx)
182182
find_package(absl CONFIG REQUIRED)
183183
if (${GOOGLE_CLOUD_CPP_ENABLE_GRPC})
184184
find_package(gRPC REQUIRED QUIET)
185-
find_package(Protobuf REQUIRED QUIET)
185+
set(GOOGLE_CLOUD_CPP_FIND_DEPENDENCY_PROTOBUF
186+
"find_dependency(Protobuf CONFIG)")
187+
find_package(Protobuf CONFIG QUIET)
188+
if (NOT Protobuf_FOUND)
189+
set(GOOGLE_CLOUD_CPP_FIND_DEPENDENCY_PROTOBUF
190+
"find_dependency(Protobuf)")
191+
find_package(Protobuf REQUIRED)
192+
endif ()
186193
add_subdirectory(external/googleapis)
187194
endif ()
188195

cmake/FindgRPC.cmake

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ find_package(Threads REQUIRED)
6969
# `find_package()` because we (have to) install this module in non-standard
7070
# locations.
7171
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
72-
find_package(Protobuf)
72+
find_package(Protobuf CONFIG QUIET)
73+
if (NOT Protobuf_FOUND)
74+
find_package(Protobuf)
75+
endif ()
7376

7477
# The gRPC::grpc_cpp_plugin target is sometimes defined, but without a
7578
# IMPORTED_LOCATION

external/googleapis/config.cmake.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# ~~~
1616

1717
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
18-
find_dependency(Protobuf)
18+
@GOOGLE_CLOUD_CPP_FIND_DEPENDENCY_PROTOBUF@
1919
find_dependency(gRPC)
2020

2121
include("${CMAKE_CURRENT_LIST_DIR}/googleapis-targets.cmake")

generator/CMakeLists.txt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ set(DOXYGEN_EXCLUDE_SYMBOLS "generator_internal")
1818
set(DOXYGEN_EXAMPLE_PATH "")
1919

2020
include(GoogleCloudCppCommon)
21-
find_package(Protobuf REQUIRED)
21+
find_package(Protobuf CONFIG QUIET)
22+
if (NOT Protobuf_FOUND)
23+
find_package(Protobuf REQUIRED)
24+
endif ()
2225
include(IncludeNlohmannJson)
2326

2427
add_library(
@@ -149,8 +152,17 @@ target_link_libraries(
149152
protobuf::libprotoc ${Protobuf_LIBRARIES})
150153

151154
# Generate protobuf code and library
152-
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS generator_config.proto)
153-
add_library(google_cloud_cpp_generator_config ${PROTO_HDRS} ${PROTO_SRCS})
155+
if (COMMAND protobuf_generate)
156+
set(protobuf_PROTOC_EXE $<TARGET_FILE:protobuf::protoc>)
157+
add_library(google_cloud_cpp_generator_config)
158+
protobuf_generate(LANGUAGE cpp TARGET google_cloud_cpp_generator_config
159+
PROTOS generator_config.proto)
160+
elseif (COMMAND protobuf_generate_cpp)
161+
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS generator_config.proto)
162+
add_library(google_cloud_cpp_generator_config ${PROTO_HDRS} ${PROTO_SRCS})
163+
else ()
164+
message(FATAL_ERROR "Missing protobuf_generate_cpp and protobuf_generate")
165+
endif ()
154166
target_link_libraries(google_cloud_cpp_generator_config
155167
PUBLIC protobuf::libprotobuf)
156168
set_target_properties(google_cloud_cpp_generator_config

google/cloud/config-rest-protobuf.cmake.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ find_dependency(google_cloud_cpp_grpc_utils)
1717
find_dependency(google_cloud_cpp_rest_internal)
1818
find_dependency(google_cloud_cpp_common)
1919
find_dependency(absl)
20-
find_dependency(protobuf)
20+
@GOOGLE_CLOUD_CPP_FIND_DEPENDENCY_PROTOBUF@
2121

2222
include("${CMAKE_CURRENT_LIST_DIR}/google_cloud_cpp_rest_protobuf_internal-targets.cmake")

google/cloud/dataproc/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ include(GoogleCloudCppDoxygen)
3939
google_cloud_cpp_doxygen_targets("dataproc" DEPENDS cloud-docs
4040
google-cloud-cpp::dataproc_protos)
4141

42-
find_package(gRPC REQUIRED)
43-
find_package(Protobuf REQUIRED)
44-
find_package(absl CONFIG REQUIRED)
45-
4642
include(GoogleCloudCppCommon)
4743

4844
include(CompileProtos)

google/cloud/resourcesettings/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ include(GoogleCloudCppDoxygen)
3939
google_cloud_cpp_doxygen_targets("resourcesettings" DEPENDS cloud-docs
4040
google-cloud-cpp::resourcesettings_protos)
4141

42-
find_package(gRPC REQUIRED)
43-
find_package(Protobuf REQUIRED)
44-
find_package(absl CONFIG REQUIRED)
45-
4642
include(GoogleCloudCppCommon)
4743

4844
include(CompileProtos)

0 commit comments

Comments
 (0)