11# Copyright The OpenTelemetry Authors
22# SPDX-License-Identifier: Apache-2.0
33
4-
5- # Import Protobuf targets (protobuf::libprotobuf and protobuf::protoc) and set PROTOBUF_PROTOC_EXECUTABLE.
6- # 1. If gRPC was fetched from github then use the Protobuf submodule built with gRPC
7- # 2. Find an installed Protobuf package
8- # 3. Use FetchContent to fetch and build Protobuf from GitHub
9-
10- if (DEFINED gRPC_PROVIDER AND NOT gRPC_PROVIDER STREQUAL "find_package" AND TARGET libprotobuf)
4+ # Import Protobuf targets (protobuf::libprotobuf and protobuf::protoc) and set
5+ # PROTOBUF_PROTOC_EXECUTABLE. 1. If gRPC was fetched from github then use the
6+ # Protobuf submodule built with gRPC 2. Find an installed Protobuf package 3.
7+ # Use FetchContent to fetch and build Protobuf from GitHub
8+
9+ if (DEFINED gRPC_PROVIDER
10+ AND NOT gRPC_PROVIDER STREQUAL "find_package"
11+ AND TARGET libprotobuf)
1112 # gRPC was fetched and built Protobuf as a submodule
1213
13- set (_Protobuf_VERSION_REGEX "\" cpp\" [ \t ]*:[ \t ]*\" ([0-9]+\\ .[0-9]+(\\ .[0-9]+)?)\" " )
14- set (_Protobuf_VERSION_FILE "${grpc_SOURCE_DIR} /third_party/protobuf/version.json" )
14+ set (_Protobuf_VERSION_REGEX
15+ "\" cpp\" [ \t ]*:[ \t ]*\" ([0-9]+\\ .[0-9]+(\\ .[0-9]+)?)\" " )
16+ set (_Protobuf_VERSION_FILE
17+ "${grpc_SOURCE_DIR} /third_party/protobuf/version.json" )
1518
1619 file (READ "${_Protobuf_VERSION_FILE} " _Protobuf_VERSION_FILE_CONTENTS )
1720 if (_Protobuf_VERSION_FILE_CONTENTS MATCHES ${_Protobuf_VERSION_REGEX} )
1821 set (Protobuf_VERSION "${CMAKE_MATCH_1} " )
1922 else ()
20- message (WARNING "Failed to parse Protobuf version from ${_Protobuf_VERSION_FILE} using regex ${_Protobuf_VERSION_REGEX} " )
23+ message (
24+ WARNING
25+ "Failed to parse Protobuf version from ${_Protobuf_VERSION_FILE} using regex ${_Protobuf_VERSION_REGEX} "
26+ )
2127 endif ()
2228 set (Protobuf_PROVIDER "grpc_submodule" )
2329else ()
2430
25- # Search for an installed Protobuf package explicitly using the CONFIG search mode first followed by the MODULE search mode.
26- # Protobuf versions < 3.22.0 may be found using the module mode and some protobuf apt packages do not support the CONFIG search.
31+ # Search for an installed Protobuf package explicitly using the CONFIG search
32+ # mode first followed by the MODULE search mode. Protobuf versions < 3.22.0
33+ # may be found using the module mode and some protobuf apt packages do not
34+ # support the CONFIG search.
2735
2836 find_package (Protobuf CONFIG QUIET )
2937 set (Protobuf_PROVIDER "find_package" )
@@ -36,45 +44,82 @@ else()
3644 FetchContent_Declare (
3745 "protobuf"
3846 GIT_REPOSITORY "https://github.com/protocolbuffers/protobuf.git"
39- GIT_TAG "${protobuf_GIT_TAG} "
40- )
41-
42- set (protobuf_INSTALL ${OPENTELEMETRY_INSTALL} CACHE BOOL "" FORCE )
43- set (protobuf_BUILD_TESTS OFF CACHE BOOL "" FORCE )
44- set (protobuf_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE )
47+ GIT_TAG "${protobuf_GIT_TAG} " )
48+
49+ set (protobuf_INSTALL
50+ ${OPENTELEMETRY_INSTALL}
51+ CACHE BOOL "" FORCE )
52+ set (protobuf_BUILD_TESTS
53+ OFF
54+ CACHE BOOL "" FORCE )
55+ set (protobuf_BUILD_EXAMPLES
56+ OFF
57+ CACHE BOOL "" FORCE )
4558
4659 FetchContent_MakeAvailable (protobuf)
4760
4861 set (Protobuf_PROVIDER "fetch_repository" )
4962
5063 # Set the Protobuf_VERSION variable from the git tag.
51- string (REGEX REPLACE "^v([0-9]+\\ .[0-9]+\\ .[0-9]+)$" "\\ 1" Protobuf_VERSION "${protobuf_GIT_TAG} " )
64+ string (REGEX REPLACE "^v([0-9]+\\ .[0-9]+\\ .[0-9]+)$" "\\ 1" Protobuf_VERSION
65+ "${protobuf_GIT_TAG} " )
5266
5367 if (TARGET libprotobuf)
54- set_target_properties (libprotobuf PROPERTIES POSITION_INDEPENDENT_CODE ON CXX_CLANG_TIDY "" CXX_INCLUDE_WHAT_YOU_USE "" )
68+ set_target_properties (
69+ libprotobuf
70+ PROPERTIES POSITION_INDEPENDENT_CODE ON
71+ CXX_CLANG_TIDY ""
72+ CXX_INCLUDE_WHAT_YOU_USE "" )
5573 endif ()
5674
5775 endif ()
5876endif ()
5977
6078if (NOT TARGET protobuf::libprotobuf)
61- message (FATAL_ERROR "A required protobuf target (protobuf::libprotobuf) was not imported" )
79+ message (
80+ FATAL_ERROR
81+ "A required protobuf target (protobuf::libprotobuf) was not imported" )
6282endif ()
6383
6484if (PROTOBUF_PROTOC_EXECUTABLE AND NOT Protobuf_PROTOC_EXECUTABLE)
65- message (WARNING "Use of PROTOBUF_PROTOC_EXECUTABLE is deprecated. Please use Protobuf_PROTOC_EXECUTABLE instead." )
85+ message (
86+ WARNING
87+ "Use of PROTOBUF_PROTOC_EXECUTABLE is deprecated. Please use Protobuf_PROTOC_EXECUTABLE instead."
88+ )
6689 set (Protobuf_PROTOC_EXECUTABLE "${PROTOBUF_PROTOC_EXECUTABLE} " )
6790endif ()
6891
6992if (CMAKE_CROSSCOMPILING )
7093 find_program (Protobuf_PROTOC_EXECUTABLE protoc )
7194else ()
7295 if (NOT TARGET protobuf::protoc)
73- message (FATAL_ERROR "A required protobuf target (protobuf::protoc) was not imported" )
96+ message (
97+ FATAL_ERROR
98+ "A required protobuf target (protobuf::protoc) was not imported" )
7499 endif ()
75100 set (Protobuf_PROTOC_EXECUTABLE "$<TARGET_FILE :protobuf ::protoc >" )
76101endif ()
77102
78103set (PROTOBUF_PROTOC_EXECUTABLE "${Protobuf_PROTOC_EXECUTABLE} " )
79104
80105message (STATUS "PROTOBUF_PROTOC_EXECUTABLE=${PROTOBUF_PROTOC_EXECUTABLE} " )
106+
107+ # When protobuf is fetched or built as a gRPC submodule utf8_range is compiled
108+ # but may need the alias target created
109+ if (TARGET utf8_validity AND NOT TARGET utf8_range::utf8_validity)
110+ add_library (utf8_range::utf8_validity ALIAS utf8_validity )
111+ endif ()
112+
113+ # For the find_package(Protobuf) path, utf8_range can also be found
114+ if (NOT TARGET utf8_range::utf8_validity)
115+ find_package (utf8_range CONFIG QUIET )
116+ endif ()
117+
118+ # The utf8_range target should now be available for use in otel-cpp
119+ if (NOT TARGET utf8_range::utf8_validity)
120+ message (
121+ WARNING
122+ "Target (utf8_range::utf8_validity) was not found. "
123+ "We will not validate UTF-8 strings in the OTLP exporter."
124+ "Which may lead to record drops when sending non UTF-8 data as string." )
125+ endif ()
0 commit comments