Skip to content

Commit 7dfc312

Browse files
authored
[BUILD] Fixes grpc linking for OTLP exporter's tests (#3435)
1 parent 82bddef commit 7dfc312

File tree

4 files changed

+89
-37
lines changed

4 files changed

+89
-37
lines changed

.github/workflows/cmake_install.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ jobs:
178178
run: ./ci/do_ci.sh cmake.install.test
179179

180180
ubuntu_2404_conan_stable:
181-
name: Ubuntu 24.04 conan stable versions cxx17 (static libs - shared libs - opentracing shim)
181+
name: Ubuntu 24.04 conan stable versions cxx17 (static libs - opentracing shim)
182182
runs-on: ubuntu-24.04
183183
env:
184184
INSTALL_TEST_DIR: '/home/runner/install_test'
@@ -207,10 +207,6 @@ jobs:
207207
env:
208208
BUILD_SHARED_LIBS: 'OFF'
209209
run: ./ci/do_ci.sh cmake.install.test
210-
- name: Run Tests (shared libs)
211-
env:
212-
BUILD_SHARED_LIBS: 'ON'
213-
run: ./ci/do_ci.sh cmake.install.test
214210
- name: verify pkgconfig packages
215211
run: |
216212
export PKG_CONFIG_PATH=$INSTALL_TEST_DIR/lib/pkgconfig:$PKG_CONFIG_PATH

cmake/opentelemetry-proto.cmake

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,25 @@ elseif(PROTOBUF_VERSION AND PROTOBUF_VERSION VERSION_LESS "3.16")
221221
list(APPEND PROTOBUF_COMMON_FLAGS "--experimental_allow_proto3_optional")
222222
endif()
223223

224+
# protobuf uses numerous global variables, which can lead to conflicts when a
225+
# user's dynamic libraries, executables, and otel-cpp are all built as dynamic
226+
# libraries and linked against a statically built protobuf library. This may
227+
# result in crashes. To prevent such conflicts, we also need to build
228+
# opentelemetry_exporter_otlp_grpc_client as a static library.
229+
if(TARGET protobuf::libprotobuf)
230+
get_target_property(protobuf_lib_type protobuf::libprotobuf TYPE)
231+
else()
232+
set(protobuf_lib_type "SHARED_LIBRARY")
233+
target_link_libraries(opentelemetry_proto PUBLIC ${Protobuf_LIBRARIES})
234+
foreach(protobuf_lib_file ${Protobuf_LIBRARIES})
235+
if(protobuf_lib_file MATCHES
236+
"(^|[\\\\\\/])[^\\\\\\/]*protobuf[^\\\\\\/]*\\.(a|lib)$")
237+
set(protobuf_lib_type "STATIC_LIBRARY")
238+
break()
239+
endif()
240+
endforeach()
241+
endif()
242+
224243
set(PROTOBUF_GENERATED_FILES
225244
${COMMON_PB_H_FILE}
226245
${COMMON_PB_CPP_FILE}
@@ -293,7 +312,8 @@ add_custom_command(
293312
unset(OTELCPP_PROTO_TARGET_OPTIONS)
294313
if(CMAKE_SYSTEM_NAME MATCHES "Windows|MinGW|WindowsStore")
295314
list(APPEND OTELCPP_PROTO_TARGET_OPTIONS STATIC)
296-
elseif(NOT DEFINED BUILD_SHARED_LIBS OR BUILD_SHARED_LIBS)
315+
elseif((NOT protobuf_lib_type STREQUAL "STATIC_LIBRARY")
316+
AND (NOT DEFINED BUILD_SHARED_LIBS OR BUILD_SHARED_LIBS))
297317
list(APPEND OTELCPP_PROTO_TARGET_OPTIONS SHARED)
298318
else()
299319
list(APPEND OTELCPP_PROTO_TARGET_OPTIONS STATIC)
@@ -314,13 +334,12 @@ add_library(
314334
set_target_version(opentelemetry_proto)
315335

316336
target_include_directories(
317-
opentelemetry_proto
318-
PUBLIC "$<BUILD_INTERFACE:${GENERATED_PROTOBUF_PATH}>"
319-
"$<INSTALL_INTERFACE:include>")
337+
opentelemetry_proto PUBLIC "$<BUILD_INTERFACE:${GENERATED_PROTOBUF_PATH}>"
338+
"$<INSTALL_INTERFACE:include>")
320339

321340
# Disable include-what-you-use and clang-tidy on generated code.
322-
set_target_properties(opentelemetry_proto PROPERTIES CXX_INCLUDE_WHAT_YOU_USE
323-
"" CXX_CLANG_TIDY "")
341+
set_target_properties(opentelemetry_proto PROPERTIES CXX_INCLUDE_WHAT_YOU_USE ""
342+
CXX_CLANG_TIDY "")
324343
if(NOT Protobuf_INCLUDE_DIRS AND TARGET protobuf::libprotobuf)
325344
get_target_property(Protobuf_INCLUDE_DIRS protobuf::libprotobuf
326345
INTERFACE_INCLUDE_DIRECTORIES)
@@ -339,13 +358,20 @@ if(WITH_OTLP_GRPC)
339358
set_target_version(opentelemetry_proto_grpc)
340359

341360
# Disable include-what-you-use and clang-tidy on generated code.
342-
set_target_properties(opentelemetry_proto_grpc
343-
PROPERTIES CXX_INCLUDE_WHAT_YOU_USE "" CXX_CLANG_TIDY "")
361+
set_target_properties(
362+
opentelemetry_proto_grpc PROPERTIES CXX_INCLUDE_WHAT_YOU_USE ""
363+
CXX_CLANG_TIDY "")
344364

345365
list(APPEND OPENTELEMETRY_PROTO_TARGETS opentelemetry_proto_grpc)
346366
target_link_libraries(opentelemetry_proto_grpc PUBLIC opentelemetry_proto)
347367

368+
# gRPC uses numerous global variables, which can lead to conflicts when a
369+
# user's dynamic libraries, executables, and otel-cpp are all built as dynamic
370+
# libraries and linked against a statically built gRPC library. This may
371+
# result in crashes. To prevent such conflicts, we also need to build
372+
# opentelemetry_exporter_otlp_grpc_client as a static library.
348373
get_target_property(grpc_lib_type gRPC::grpc++ TYPE)
374+
349375
if(grpc_lib_type STREQUAL "SHARED_LIBRARY")
350376
target_link_libraries(opentelemetry_proto_grpc PUBLIC gRPC::grpc++)
351377
endif()
@@ -382,7 +408,8 @@ else() # cmake 3.8 or lower
382408
target_link_libraries(opentelemetry_proto PUBLIC ${Protobuf_LIBRARIES})
383409
endif()
384410

385-
# this is needed on some older grcp versions specifically conan recipe for grpc/1.54.3
411+
# this is needed on some older grcp versions specifically conan recipe for
412+
# grpc/1.54.3
386413
if(WITH_OTLP_GRPC)
387414
if(TARGET absl::synchronization)
388415
target_link_libraries(opentelemetry_proto_grpc

cmake/tools.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ function(project_build_tools_get_imported_location OUTPUT_VAR_NAME TARGET_NAME)
9999
get_target_property(TARGET_TYPE ${TARGET_NAME} TYPE)
100100
if(TARGET_TYPE STREQUAL "INTERFACE_LIBRARY")
101101
# For interface libraries, do not attempt to retrieve imported location.
102-
set(${OUTPUT_VAR_NAME} "" PARENT_SCOPE)
102+
set(${OUTPUT_VAR_NAME}
103+
""
104+
PARENT_SCOPE)
103105
return()
104106
endif()
105107

exporters/otlp/CMakeLists.txt

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
# Copyright The OpenTelemetry Authors
22
# SPDX-License-Identifier: Apache-2.0
33

4+
if(protobuf_lib_type STREQUAL "STATIC_LIBRARY")
5+
set(OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE STATIC)
6+
else()
7+
set(OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE)
8+
endif()
9+
410
add_library(
511
opentelemetry_otlp_recordable
6-
src/otlp_environment.cc src/otlp_log_recordable.cc src/otlp_recordable.cc
7-
src/otlp_populate_attribute_utils.cc src/otlp_recordable_utils.cc
12+
${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE}
13+
src/otlp_environment.cc
14+
src/otlp_log_recordable.cc
15+
src/otlp_recordable.cc
16+
src/otlp_populate_attribute_utils.cc
17+
src/otlp_recordable_utils.cc
818
src/otlp_metric_utils.cc)
919
set_target_properties(opentelemetry_otlp_recordable PROPERTIES EXPORT_NAME
1020
otlp_recordable)
@@ -23,10 +33,20 @@ target_link_libraries(opentelemetry_otlp_recordable
2333

2434
if(WITH_OTLP_GRPC)
2535
find_package(gRPC REQUIRED)
36+
if(NOT DEFINED grpc_lib_type)
37+
message(
38+
FATAL_ERROR "cmake/opentelemetry-proto.cmake should be included first")
39+
endif()
40+
if(grpc_lib_type STREQUAL "STATIC_LIBRARY" OR protobuf_lib_type STREQUAL
41+
"STATIC_LIBRARY")
42+
set(OPENTELEMETRY_OTLP_GRPC_CLIENT_LIB_TYPE STATIC)
43+
else()
44+
set(OPENTELEMETRY_OTLP_GRPC_CLIENT_LIB_TYPE)
45+
endif()
2646
add_library(
2747
opentelemetry_exporter_otlp_grpc_client
28-
src/otlp_grpc_client.cc src/otlp_grpc_client_factory.cc
29-
src/otlp_grpc_utils.cc)
48+
${OPENTELEMETRY_OTLP_GRPC_CLIENT_LIB_TYPE} src/otlp_grpc_client.cc
49+
src/otlp_grpc_client_factory.cc src/otlp_grpc_utils.cc)
3050
set_target_properties(opentelemetry_exporter_otlp_grpc_client
3151
PROPERTIES EXPORT_NAME otlp_grpc_client)
3252
set_target_version(opentelemetry_exporter_otlp_grpc_client)
@@ -40,8 +60,9 @@ if(WITH_OTLP_GRPC)
4060
opentelemetry_exporter_otlp_grpc_client
4161
PUBLIC opentelemetry_sdk opentelemetry_common
4262
# gRPC::grpc++ must be linked before opentelemetry_proto_grpc.
43-
opentelemetry_proto_grpc
44-
PRIVATE gRPC::grpc++ opentelemetry_ext)
63+
"$<BUILD_INTERFACE:opentelemetry_proto_grpc>"
64+
PRIVATE "$<INSTALL_INTERFACE:opentelemetry_proto_grpc>" gRPC::grpc++
65+
opentelemetry_ext)
4566

4667
get_target_property(GRPC_INCLUDE_DIRECTORY gRPC::grpc++
4768
INTERFACE_INCLUDE_DIRECTORIES)
@@ -60,8 +81,8 @@ if(WITH_OTLP_GRPC)
6081

6182
add_library(
6283
opentelemetry_exporter_otlp_grpc
63-
src/otlp_grpc_exporter.cc src/otlp_grpc_exporter_factory.cc
64-
src/otlp_grpc_exporter_options.cc)
84+
${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_grpc_exporter.cc
85+
src/otlp_grpc_exporter_factory.cc src/otlp_grpc_exporter_options.cc)
6586

6687
set_target_properties(opentelemetry_exporter_otlp_grpc
6788
PROPERTIES EXPORT_NAME otlp_grpc_exporter)
@@ -76,7 +97,7 @@ if(WITH_OTLP_GRPC)
7697

7798
add_library(
7899
opentelemetry_exporter_otlp_grpc_log
79-
src/otlp_grpc_log_record_exporter.cc
100+
${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_grpc_log_record_exporter.cc
80101
src/otlp_grpc_log_record_exporter_factory.cc
81102
src/otlp_grpc_log_record_exporter_options.cc)
82103

@@ -94,7 +115,8 @@ if(WITH_OTLP_GRPC)
94115

95116
add_library(
96117
opentelemetry_exporter_otlp_grpc_metrics
97-
src/otlp_grpc_metric_exporter.cc src/otlp_grpc_metric_exporter_factory.cc
118+
${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_grpc_metric_exporter.cc
119+
src/otlp_grpc_metric_exporter_factory.cc
98120
src/otlp_grpc_metric_exporter_options.cc)
99121

100122
set_target_properties(opentelemetry_exporter_otlp_grpc_metrics
@@ -111,8 +133,10 @@ if(WITH_OTLP_GRPC)
111133
endif()
112134

113135
if(WITH_OTLP_HTTP)
114-
add_library(opentelemetry_exporter_otlp_http_client src/otlp_http.cc
115-
src/otlp_http_client.cc)
136+
add_library(
137+
opentelemetry_exporter_otlp_http_client
138+
${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_http.cc
139+
src/otlp_http_client.cc)
116140
set_target_properties(opentelemetry_exporter_otlp_http_client
117141
PROPERTIES EXPORT_NAME otlp_http_client)
118142
set_target_version(opentelemetry_exporter_otlp_http_client)
@@ -141,8 +165,8 @@ if(WITH_OTLP_HTTP)
141165

142166
add_library(
143167
opentelemetry_exporter_otlp_http
144-
src/otlp_http_exporter.cc src/otlp_http_exporter_factory.cc
145-
src/otlp_http_exporter_options.cc)
168+
${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_http_exporter.cc
169+
src/otlp_http_exporter_factory.cc src/otlp_http_exporter_options.cc)
146170

147171
set_target_properties(opentelemetry_exporter_otlp_http
148172
PROPERTIES EXPORT_NAME otlp_http_exporter)
@@ -157,7 +181,7 @@ if(WITH_OTLP_HTTP)
157181

158182
add_library(
159183
opentelemetry_exporter_otlp_http_log
160-
src/otlp_http_log_record_exporter.cc
184+
${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_http_log_record_exporter.cc
161185
src/otlp_http_log_record_exporter_factory.cc
162186
src/otlp_http_log_record_exporter_options.cc)
163187

@@ -175,7 +199,8 @@ if(WITH_OTLP_HTTP)
175199

176200
add_library(
177201
opentelemetry_exporter_otlp_http_metric
178-
src/otlp_http_metric_exporter.cc src/otlp_http_metric_exporter_factory.cc
202+
${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_http_metric_exporter.cc
203+
src/otlp_http_metric_exporter_factory.cc
179204
src/otlp_http_metric_exporter_options.cc)
180205

181206
set_target_properties(opentelemetry_exporter_otlp_http_metric
@@ -192,7 +217,8 @@ if(WITH_OTLP_HTTP)
192217
endif()
193218

194219
if(WITH_OTLP_FILE)
195-
add_library(opentelemetry_exporter_otlp_file_client src/otlp_file_client.cc)
220+
add_library(opentelemetry_exporter_otlp_file_client
221+
${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_file_client.cc)
196222
set_target_properties(opentelemetry_exporter_otlp_file_client
197223
PROPERTIES EXPORT_NAME otlp_file_client)
198224
set_target_version(opentelemetry_exporter_otlp_file_client)
@@ -216,8 +242,8 @@ if(WITH_OTLP_FILE)
216242

217243
add_library(
218244
opentelemetry_exporter_otlp_file
219-
src/otlp_file_exporter.cc src/otlp_file_exporter_factory.cc
220-
src/otlp_file_exporter_options.cc)
245+
${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_file_exporter.cc
246+
src/otlp_file_exporter_factory.cc src/otlp_file_exporter_options.cc)
221247

222248
set_target_properties(opentelemetry_exporter_otlp_file
223249
PROPERTIES EXPORT_NAME otlp_file_exporter)
@@ -232,7 +258,7 @@ if(WITH_OTLP_FILE)
232258

233259
add_library(
234260
opentelemetry_exporter_otlp_file_log
235-
src/otlp_file_log_record_exporter.cc
261+
${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_file_log_record_exporter.cc
236262
src/otlp_file_log_record_exporter_factory.cc
237263
src/otlp_file_log_record_exporter_options.cc)
238264

@@ -250,7 +276,8 @@ if(WITH_OTLP_FILE)
250276

251277
add_library(
252278
opentelemetry_exporter_otlp_file_metric
253-
src/otlp_file_metric_exporter.cc src/otlp_file_metric_exporter_factory.cc
279+
${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_file_metric_exporter.cc
280+
src/otlp_file_metric_exporter_factory.cc
254281
src/otlp_file_metric_exporter_options.cc)
255282

256283
set_target_properties(opentelemetry_exporter_otlp_file_metric
@@ -371,7 +398,7 @@ if(BUILD_TESTING)
371398
add_executable(otlp_grpc_exporter_test test/otlp_grpc_exporter_test.cc)
372399
target_link_libraries(
373400
otlp_grpc_exporter_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}
374-
${GMOCK_LIB} opentelemetry_exporter_otlp_grpc gRPC::grpc++)
401+
${GMOCK_LIB} opentelemetry_exporter_otlp_grpc)
375402
gtest_add_tests(
376403
TARGET otlp_grpc_exporter_test
377404
TEST_PREFIX exporter.otlp.

0 commit comments

Comments
 (0)