cmake: Support FetchContent projects which include gRPC dependencies
The feature request: Respect externally set gRPC_*_PROVIDER variables by not overriding them in https://github.com/grpc/grpc/blob/master/CMakeLists.txt, or support not building certain dependencies with some other mechanism.
Other open source google libraries support FetchContent by having Build_ vars for each dependency. See here: https://github.com/google/or-tools/issues/2219
Is your feature request related to a problem? Please describe.
The problem: when building via a FetchContent super project which includes re2, even if I set OVERRIDE_FIND_PACKAGE when including re2, and set(gRPC_RE2_PROVIDER "package") gRPC still attempts to configure re2. This happens because of the line set(gRPC_RE2_PROVIDER "module" CACHE STRING "Provider of re2 library") in https://github.com/grpc/grpc/blob/master/CMakeLists.txt#L76
This causes a failure in the configure step the first time cmake .. is run (though it passes for subsequent runs). Full cmake is at the bottom of the post.
A hacky fix for this issue would be forking the repo and inserting set(gRPC_RE2_PROVIDER "package") at the start of https://github.com/grpc/grpc/blob/master/cmake/re2.cmake
Here is the CMakeLists.txt:
cmake_minimum_required(VERSION 3.24)
project(google_example)
set(CMAKE_CXX_STANDARD 17)
cmake_policy(SET CMP0022 NEW)
cmake_policy(SET CMP0042 NEW)
cmake_policy(SET CMP0068 NEW)
cmake_policy(SET CMP0077 NEW)
cmake_policy(SET CMP0079 NEW)
SET(ABSL_PROPAGATE_CXX_STD ON)
SET(ABSL_BUILD_TESTING OFF)
set(CMAKE_INCLUDE_CURRENT_DIR OFF)
set(CMAKE_THREAD_LIBS_INIT "-lpthread")
set(CMAKE_HAVE_THREADS_LIBRARY 1)
set(CMAKE_USE_WIN32_THREADS_INIT 0)
set(CMAKE_USE_PTHREADS_INIT 1)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
set(sublibs ${sublibs} Threads::Threads)
include(FetchContent)
set(FETCHCONTENT_QUIET OFF)
set(ABSL_ENABLE_INSTALL ON)
get_directory_property(old_dir_compile_options COMPILE_OPTIONS)
add_compile_options(-w)
message(${CMAKE_CURRENT_LIST_DIR})
FetchContent_Declare(
absl
GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git
GIT_TAG 20230125.3
# PATCH_COMMAND git apply --ignore-whitespace "${CMAKE_CURRENT_LIST_DIR}/abseil-cpp-20230125.3.patch"
OVERRIDE_FIND_PACKAGE
)
FetchContent_MakeAvailable(absl)
include_directories(${absl_SOURCE_DIR})
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
set(BUILD_GMOCK OFF CACHE BOOL "" FORCE)
set(BUILD_GTEST ON CACHE BOOL "" FORCE)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.14.0
OVERRIDE_FIND_PACKAGE
)
FetchContent_MakeAvailable(googletest)
set(RE2_BUILD_TESTING OFF)
FetchContent_Declare(
re2
GIT_REPOSITORY "https://github.com/google/re2.git"
GIT_TAG "2023-07-01"
OVERRIDE_FIND_PACKAGE
#PATCH_COMMAND git apply --ignore-whitespace "${CMAKE_CURRENT_LIST_DIR}/../../patches/re2-2023-07-01.patch"
)
FetchContent_MakeAvailable(re2)
set(gRPC_RE2_PROVIDER package)
FetchContent_Declare(Protobuf
GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
GIT_TAG v24.4
SOURCE_SUBDIR cmake
OVERRIDE_FIND_PACKAGE
)
FetchContent_MakeAvailable(Protobuf)
include_directories(${protobuf_SOURCE_DIR}/../src)
set(gRPC_SSL_PROVIDER package)
set(gRPC_ZLIB_PROVIDER package)
FetchContent_Declare(
gRPC
GIT_REPOSITORY https://github.com/grpc/grpc
GIT_TAG v1.59.2
OVERRIDE_FIND_PACKAGE
)
FetchContent_MakeAvailable(gRPC)
set(_PROTOBUF_LIBPROTOBUF libprotobuf)
set(_REFLECTION grpc++_reflection)
set(_PROTOBUF_PROTOC $<TARGET_FILE:protoc>)
set(_GRPC_GRPCPP grpc++)
if(CMAKE_CROSSCOMPILING)
find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
else()
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
endif()
set(BUILD_SHARED_LIBS OFF)
set(BUILD_DEPS OFF)
set(BUILD_SAMPLES OFF)
set(BUILD_EXAMPLES OFF)
set(BUILD_Eigen3 ON)
set(BUILD_CoinUtils ON)
set(BUILD_Cbc ON)
set(BUILD_Osi ON)
set(BUILD_Clp ON)
set(BUILD_Cgl ON)
set(BUILD_SCIP ON)
FetchContent_Declare(
ortools
GIT_REPOSITORY https://github.com/google/or-tools.git
GIT_TAG v9.7
)
FetchContent_MakeAvailable(ortools)
add_definitions(-DORTOOLS)
include_directories(${ortools_SOURCE_DIR})
include_directories(${ortools_BINARY_DIR})
set_directory_properties(PROPERTIES COMPILE_OPTIONS "${old_dir_compile_options}")
add_executable(google_example main.cpp)
target_link_libraries(google_example absl::strings grpc ortools::ortools)
Is there a way to resolve this without adding additional options?
Looks like, this is due to CMP0126. Using set(gRPC_RE2_PROVIDER package CACHE INTERNAL "") in super project should avoid the issue.
More than 30 days have passed since label "disposition/requires reporter action" was added. Closing this issue. Please feel free to re-open/create a new issue if this is still relevant.