-
Notifications
You must be signed in to change notification settings - Fork 501
[CMAKE] Add CMake scripts to find or fetch protobuf and grpc #3533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
marcalff
merged 14 commits into
open-telemetry:main
from
dbarker:cmake_find_or_fetch_protobuf_and_grpc
Jul 22, 2025
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
305a525
add cmake scripts to find or fetch protobuf and grpc
dbarker 7fc964f
update ci FetchContent test to fetch protobuf and grpc and built them…
dbarker e456a20
cleanup and ci fixes
dbarker 29f86c6
log protobuf and grpc versions if built using FetchContent
dbarker dcb931a
Merge remote-tracking branch 'origin/main' into cmake_find_or_fetch_p…
dbarker 8907ce7
set fPIC for the shared libs otlp grpc test
dbarker 74ed5f4
Merge remote-tracking branch 'origin/main' into cmake_find_or_fetch_p…
dbarker 19936ab
Merge remote-tracking branch 'origin/main' into cmake_find_or_fetch_p…
dbarker cc4372d
set gRPC_PROTOBUF_PACKAGE_TYPE=CONFIG when installing and building gr…
dbarker fca2c70
Merge branch 'main' into cmake_find_or_fetch_protobuf_and_grpc
ThomsonTan 8317a45
Merge branch 'main' into cmake_find_or_fetch_protobuf_and_grpc
ThomsonTan 72172a1
use Protobuf_PROTOC_EXECUTABLE
dbarker cfd1cc6
Merge branch 'main' into cmake_find_or_fetch_protobuf_and_grpc
dbarker d6a2641
Merge branch 'main' into cmake_find_or_fetch_protobuf_and_grpc
dbarker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# Import gRPC targets (gRPC::grpc++ and gRPC::grpc_cpp_plugin). | ||
# 1. Find an installed gRPC package | ||
# 2. Use FetchContent to fetch and build gRPC (and its submodules) from GitHub | ||
|
||
# Including the CMakeFindDependencyMacro resolves an error from | ||
# gRPCConfig.cmake on some grpc versions. See | ||
# https://github.com/grpc/grpc/pull/33361 for more details. | ||
include(CMakeFindDependencyMacro) | ||
|
||
find_package(gRPC CONFIG QUIET) | ||
set(gRPC_PROVIDER "find_package") | ||
|
||
if(NOT gRPC_FOUND) | ||
FetchContent_Declare( | ||
"grpc" | ||
GIT_REPOSITORY "https://github.com/grpc/grpc.git" | ||
GIT_TAG "${grpc_GIT_TAG}" | ||
GIT_SUBMODULES | ||
"third_party/re2" | ||
"third_party/abseil-cpp" | ||
"third_party/protobuf" | ||
"third_party/cares/cares" | ||
"third_party/boringssl-with-bazel" | ||
) | ||
set(gRPC_PROVIDER "fetch_repository") | ||
|
||
set(gRPC_INSTALL ${OPENTELEMETRY_INSTALL} CACHE BOOL "" FORCE) | ||
set(gRPC_BUILD_TESTS OFF CACHE BOOL "" FORCE) | ||
set(gRPC_BUILD_GRPC_CPP_PLUGIN ON CACHE BOOL "" FORCE) | ||
set(gRPC_BUILD_GRPC_CSHARP_PLUGIN OFF CACHE BOOL "" FORCE) | ||
set(gRPC_BUILD_GRPC_OBJECTIVE_C_PLUGIN OFF CACHE BOOL "" FORCE) | ||
set(gRPC_BUILD_GRPC_PHP_PLUGIN OFF CACHE BOOL "" FORCE) | ||
set(gRPC_BUILD_GRPC_NODE_PLUGIN OFF CACHE BOOL "" FORCE) | ||
set(gRPC_BUILD_GRPC_PYTHON_PLUGIN OFF CACHE BOOL "" FORCE) | ||
set(gRPC_BUILD_GRPC_RUBY_PLUGIN OFF CACHE BOOL "" FORCE) | ||
set(gRPC_BUILD_GRPCPP_OTEL_PLUGIN OFF CACHE BOOL "" FORCE) | ||
set(gRPC_ZLIB_PROVIDER "package" CACHE STRING "" FORCE) | ||
set(gRPC_RE2_PROVIDER "module" CACHE STRING "" FORCE) | ||
set(RE2_BUILD_TESTING OFF CACHE BOOL "" FORCE) | ||
set(gRPC_PROTOBUF_PROVIDER "module" CACHE STRING "" FORCE) | ||
set(gRPC_PROTOBUF_PACKAGE_TYPE "CONFIG" CACHE STRING "" FORCE) | ||
set(gRPC_ABSL_PROVIDER "module" CACHE STRING "" FORCE) | ||
set(gRPC_CARES_PROVIDER "module" CACHE STRING "" FORCE) | ||
|
||
FetchContent_MakeAvailable(grpc) | ||
|
||
# Set the gRPC_VERSION variable from the git tag. | ||
string(REGEX REPLACE "^v([0-9]+\\.[0-9]+\\.[0-9]+)$" "\\1" gRPC_VERSION "${grpc_GIT_TAG}") | ||
|
||
#Disable iwyu and clang-tidy | ||
foreach(_grpc_target grpc++ grpc_cpp_plugin) | ||
if(TARGET ${_grpc_target}) | ||
set_target_properties(${_grpc_target} PROPERTIES POSITION_INDEPENDENT_CODE ON CXX_INCLUDE_WHAT_YOU_USE "" | ||
CXX_CLANG_TIDY "") | ||
endif() | ||
endforeach() | ||
|
||
if(TARGET grpc++ AND NOT TARGET gRPC::grpc++) | ||
add_library(gRPC::grpc++ ALIAS grpc++) | ||
endif() | ||
|
||
if(TARGET grpc_cpp_plugin AND NOT TARGET gRPC::grpc_cpp_plugin) | ||
add_executable(gRPC::grpc_cpp_plugin ALIAS grpc_cpp_plugin) | ||
endif() | ||
|
||
endif() | ||
|
||
if(NOT TARGET gRPC::grpc++) | ||
message(FATAL_ERROR "A required gRPC target (gRPC::grpc++) was not imported") | ||
endif() | ||
|
||
if(CMAKE_CROSSCOMPILING) | ||
find_program(gRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin) | ||
else() | ||
if(NOT TARGET gRPC::grpc_cpp_plugin) | ||
message(FATAL_ERROR "A required gRPC target (gRPC::grpc_cpp_plugin) was not imported") | ||
endif() | ||
set(gRPC_CPP_PLUGIN_EXECUTABLE "$<TARGET_FILE:gRPC::grpc_cpp_plugin>") | ||
endif() | ||
|
||
message(STATUS "gRPC_CPP_PLUGIN_EXECUTABLE=${gRPC_CPP_PLUGIN_EXECUTABLE}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
|
||
# Import Protobuf targets (protobuf::libprotobuf and protobuf::protoc) and set PROTOBUF_PROTOC_EXECUTABLE. | ||
# 1. If gRPC was fetched from github then use the Protobuf submodule built with gRPC | ||
# 2. Find an installed Protobuf package | ||
# 3. Use FetchContent to fetch and build Protobuf from GitHub | ||
|
||
if(DEFINED gRPC_PROVIDER AND NOT gRPC_PROVIDER STREQUAL "find_package" AND TARGET libprotobuf) | ||
# gRPC was fetched and built Protobuf as a submodule | ||
|
||
set(_Protobuf_VERSION_REGEX "\"cpp\"[ \t]*:[ \t]*\"([0-9]+\\.[0-9]+(\\.[0-9]+)?)\"") | ||
set(_Protobuf_VERSION_FILE "${grpc_SOURCE_DIR}/third_party/protobuf/version.json") | ||
|
||
file(READ "${_Protobuf_VERSION_FILE}" _Protobuf_VERSION_FILE_CONTENTS) | ||
if(_Protobuf_VERSION_FILE_CONTENTS MATCHES ${_Protobuf_VERSION_REGEX}) | ||
set(Protobuf_VERSION "${CMAKE_MATCH_1}") | ||
else() | ||
message(WARNING "Failed to parse Protobuf version from ${_Protobuf_VERSION_FILE} using regex ${_Protobuf_VERSION_REGEX}") | ||
endif() | ||
set(Protobuf_PROVIDER "grpc_submodule") | ||
else() | ||
|
||
# Search for an installed Protobuf package explicitly using the CONFIG search mode first followed by the MODULE search mode. | ||
# Protobuf versions < 3.22.0 may be found using the module mode and some protobuf apt packages do not support the CONFIG search. | ||
|
||
find_package(Protobuf CONFIG QUIET) | ||
set(Protobuf_PROVIDER "find_package") | ||
|
||
if(NOT Protobuf_FOUND) | ||
find_package(Protobuf MODULE QUIET) | ||
endif() | ||
|
||
if(NOT Protobuf_FOUND) | ||
FetchContent_Declare( | ||
"protobuf" | ||
GIT_REPOSITORY "https://github.com/protocolbuffers/protobuf.git" | ||
GIT_TAG "${protobuf_GIT_TAG}" | ||
) | ||
|
||
set(protobuf_INSTALL ${OPENTELEMETRY_INSTALL} CACHE BOOL "" FORCE) | ||
set(protobuf_BUILD_TESTS OFF CACHE BOOL "" FORCE) | ||
set(protobuf_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) | ||
|
||
FetchContent_MakeAvailable(protobuf) | ||
|
||
set(Protobuf_PROVIDER "fetch_repository") | ||
|
||
# Set the Protobuf_VERSION variable from the git tag. | ||
string(REGEX REPLACE "^v([0-9]+\\.[0-9]+\\.[0-9]+)$" "\\1" Protobuf_VERSION "${protobuf_GIT_TAG}") | ||
|
||
if(TARGET libprotobuf) | ||
set_target_properties(libprotobuf PROPERTIES POSITION_INDEPENDENT_CODE ON CXX_CLANG_TIDY "" CXX_INCLUDE_WHAT_YOU_USE "") | ||
endif() | ||
|
||
endif() | ||
endif() | ||
|
||
if(NOT TARGET protobuf::libprotobuf) | ||
message(FATAL_ERROR "A required protobuf target (protobuf::libprotobuf) was not imported") | ||
endif() | ||
|
||
if(CMAKE_CROSSCOMPILING) | ||
find_program(PROTOBUF_PROTOC_EXECUTABLE protoc) | ||
dbarker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
else() | ||
if(NOT TARGET protobuf::protoc) | ||
message(FATAL_ERROR "A required protobuf target (protobuf::protoc) was not imported") | ||
endif() | ||
set(PROTOBUF_PROTOC_EXECUTABLE "$<TARGET_FILE:protobuf::protoc>") | ||
endif() | ||
|
||
message(STATUS "PROTOBUF_PROTOC_EXECUTABLE=${PROTOBUF_PROTOC_EXECUTABLE}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.