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+
410add_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)
919set_target_properties (opentelemetry_otlp_recordable PROPERTIES EXPORT_NAME
1020 otlp_recordable)
@@ -23,10 +33,34 @@ target_link_libraries(opentelemetry_otlp_recordable
2333
2434if (WITH_OTLP_GRPC)
2535 find_package (gRPC REQUIRED)
36+ # gRPC uses numerous global variables, which can lead to conflicts when a
37+ # user's dynamic libraries, executables, and otel-cpp are all built with
38+ # -fvisibility=hidden and linked against a statically built gRPC library. This
39+ # may result in crashes. To prevent such conflicts, we also need to build
40+ # opentelemetry_exporter_otlp_grpc_client as a static library.
41+ get_target_property (grpc_lib_type gRPC::grpc++ TYPE )
42+ # grpc_lib_type may be "INTERFACE_LIBRARY" in some build systems, such as
43+ # conan.
44+ if (grpc_lib_type STREQUAL "INTERFACE_LIBRARY" )
45+ project_build_tools_recursive_scan_imported_locations(
46+ grpc_lib_files TARGET_NAME gRPC::grpc++ TARGET_MATCH ".*(grpc|gRPC).*" )
47+ foreach (grpc_lib_file ${grpc_lib_files} )
48+ if (grpc_lib_file MATCHES "(^|[\\\\\\ /])[^\\\\\\ /]*grpc[^\\\\\\ /]*.a$" )
49+ set (grpc_lib_type "STATIC_LIBRARY" )
50+ break ()
51+ endif ()
52+ endforeach ()
53+ endif ()
54+ if (grpc_lib_type STREQUAL "STATIC_LIBRARY" OR protobuf_lib_type STREQUAL
55+ "STATIC_LIBRARY" )
56+ set (OPENTELEMETRY_OTLP_GRPC_CLIENT_LIB_TYPE STATIC )
57+ else ()
58+ set (OPENTELEMETRY_OTLP_GRPC_CLIENT_LIB_TYPE)
59+ endif ()
2660 add_library (
2761 opentelemetry_exporter_otlp_grpc_client
28- src/otlp_grpc_client.cc src/otlp_grpc_client_factory .cc
29- src/otlp_grpc_utils.cc)
62+ ${OPENTELEMETRY_OTLP_GRPC_CLIENT_LIB_TYPE} src/otlp_grpc_client .cc
63+ src/otlp_grpc_client_factory.cc src/ otlp_grpc_utils.cc)
3064 set_target_properties (opentelemetry_exporter_otlp_grpc_client
3165 PROPERTIES EXPORT_NAME otlp_grpc_client)
3266 set_target_version(opentelemetry_exporter_otlp_grpc_client)
@@ -60,8 +94,8 @@ if(WITH_OTLP_GRPC)
6094
6195 add_library (
6296 opentelemetry_exporter_otlp_grpc
63- src/otlp_grpc_exporter.cc src/otlp_grpc_exporter_factory .cc
64- src/otlp_grpc_exporter_options.cc)
97+ ${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_grpc_exporter .cc
98+ src/otlp_grpc_exporter_factory.cc src/ otlp_grpc_exporter_options.cc)
6599
66100 set_target_properties (opentelemetry_exporter_otlp_grpc
67101 PROPERTIES EXPORT_NAME otlp_grpc_exporter)
@@ -76,7 +110,7 @@ if(WITH_OTLP_GRPC)
76110
77111 add_library (
78112 opentelemetry_exporter_otlp_grpc_log
79- src/otlp_grpc_log_record_exporter.cc
113+ ${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_grpc_log_record_exporter.cc
80114 src/otlp_grpc_log_record_exporter_factory.cc
81115 src/otlp_grpc_log_record_exporter_options.cc)
82116
@@ -94,7 +128,8 @@ if(WITH_OTLP_GRPC)
94128
95129 add_library (
96130 opentelemetry_exporter_otlp_grpc_metrics
97- src/otlp_grpc_metric_exporter.cc src/otlp_grpc_metric_exporter_factory.cc
131+ ${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_grpc_metric_exporter.cc
132+ src/otlp_grpc_metric_exporter_factory.cc
98133 src/otlp_grpc_metric_exporter_options.cc)
99134
100135 set_target_properties (opentelemetry_exporter_otlp_grpc_metrics
@@ -111,8 +146,10 @@ if(WITH_OTLP_GRPC)
111146endif ()
112147
113148if (WITH_OTLP_HTTP)
114- add_library (opentelemetry_exporter_otlp_http_client src/otlp_http.cc
115- src/otlp_http_client.cc)
149+ add_library (
150+ opentelemetry_exporter_otlp_http_client
151+ ${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_http.cc
152+ src/otlp_http_client.cc)
116153 set_target_properties (opentelemetry_exporter_otlp_http_client
117154 PROPERTIES EXPORT_NAME otlp_http_client)
118155 set_target_version(opentelemetry_exporter_otlp_http_client)
@@ -141,8 +178,8 @@ if(WITH_OTLP_HTTP)
141178
142179 add_library (
143180 opentelemetry_exporter_otlp_http
144- src/otlp_http_exporter.cc src/otlp_http_exporter_factory .cc
145- src/otlp_http_exporter_options.cc)
181+ ${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_http_exporter .cc
182+ src/otlp_http_exporter_factory.cc src/ otlp_http_exporter_options.cc)
146183
147184 set_target_properties (opentelemetry_exporter_otlp_http
148185 PROPERTIES EXPORT_NAME otlp_http_exporter)
@@ -157,7 +194,7 @@ if(WITH_OTLP_HTTP)
157194
158195 add_library (
159196 opentelemetry_exporter_otlp_http_log
160- src/otlp_http_log_record_exporter.cc
197+ ${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_http_log_record_exporter.cc
161198 src/otlp_http_log_record_exporter_factory.cc
162199 src/otlp_http_log_record_exporter_options.cc)
163200
@@ -175,7 +212,8 @@ if(WITH_OTLP_HTTP)
175212
176213 add_library (
177214 opentelemetry_exporter_otlp_http_metric
178- src/otlp_http_metric_exporter.cc src/otlp_http_metric_exporter_factory.cc
215+ ${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_http_metric_exporter.cc
216+ src/otlp_http_metric_exporter_factory.cc
179217 src/otlp_http_metric_exporter_options.cc)
180218
181219 set_target_properties (opentelemetry_exporter_otlp_http_metric
@@ -192,7 +230,8 @@ if(WITH_OTLP_HTTP)
192230endif ()
193231
194232if (WITH_OTLP_FILE)
195- add_library (opentelemetry_exporter_otlp_file_client src/otlp_file_client.cc)
233+ add_library (opentelemetry_exporter_otlp_file_client
234+ ${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_file_client.cc)
196235 set_target_properties (opentelemetry_exporter_otlp_file_client
197236 PROPERTIES EXPORT_NAME otlp_file_client)
198237 set_target_version(opentelemetry_exporter_otlp_file_client)
@@ -216,8 +255,8 @@ if(WITH_OTLP_FILE)
216255
217256 add_library (
218257 opentelemetry_exporter_otlp_file
219- src/otlp_file_exporter.cc src/otlp_file_exporter_factory .cc
220- src/otlp_file_exporter_options.cc)
258+ ${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_file_exporter .cc
259+ src/otlp_file_exporter_factory.cc src/ otlp_file_exporter_options.cc)
221260
222261 set_target_properties (opentelemetry_exporter_otlp_file
223262 PROPERTIES EXPORT_NAME otlp_file_exporter)
@@ -232,7 +271,7 @@ if(WITH_OTLP_FILE)
232271
233272 add_library (
234273 opentelemetry_exporter_otlp_file_log
235- src/otlp_file_log_record_exporter.cc
274+ ${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_file_log_record_exporter.cc
236275 src/otlp_file_log_record_exporter_factory.cc
237276 src/otlp_file_log_record_exporter_options.cc)
238277
@@ -250,7 +289,8 @@ if(WITH_OTLP_FILE)
250289
251290 add_library (
252291 opentelemetry_exporter_otlp_file_metric
253- src/otlp_file_metric_exporter.cc src/otlp_file_metric_exporter_factory.cc
292+ ${OPENTELEMETRY_OTLP_TARGETS_LIB_TYPE} src/otlp_file_metric_exporter.cc
293+ src/otlp_file_metric_exporter_factory.cc
254294 src/otlp_file_metric_exporter_options.cc)
255295
256296 set_target_properties (opentelemetry_exporter_otlp_file_metric
0 commit comments