From 23dfc20ac672efd7f9ce6ff985b29ff889d61bda Mon Sep 17 00:00:00 2001 From: WenTao Ou Date: Thu, 18 May 2023 03:13:07 +0800 Subject: [PATCH] Allow build shared `opentelemetry_proto` on non-Windows platform. (#2097) --- CHANGELOG.md | 13 ++++++ cmake/opentelemetry-proto.cmake | 80 ++++++++++++++++++++++----------- exporters/otlp/CMakeLists.txt | 18 +++++--- 3 files changed, 79 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7566b78304..d7c7f88120 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,9 +19,22 @@ Increment the: [#2123](https://github.com/open-telemetry/opentelemetry-cpp/pull/2123) * [BUILD] Build break with old curl, macro CURL_VERSION_BITS unknown [#2102](https://github.com/open-telemetry/opentelemetry-cpp/pull/2102) +* [BUILD] Add opentelemetry_proto_grpc and allow build shared opentelemetry_proto + and opentelemetry_proto_grpc on non-Windows platform. + [#2097](https://github.com/open-telemetry/opentelemetry-cpp/pull/2097) * [API] Add user facing Logging API and Benchmarks [#2094](https://github.com/open-telemetry/opentelemetry-cpp/pull/2094) +Breaking changes: + +* Add opentelemetry_proto_grpc and move gRPC sources into it. + [#2097](https://github.com/open-telemetry/opentelemetry-cpp/pull/2097) + * There will be no breaking changes for users who only use OTLP exporters and + do not directly use opentelemetry-cpp::proto. However, it is important to + note that `opentelemetry-cpp::proto` no longer contains generated gRPC codes + , and all components that depend on these gRPC codes should also link to + `opentelemetry-cpp::proto_grpc`. + Deprecations: * The Jaeger Exporter is deprecated, see [DEPRECATED](./DEPRECATED.md) for details. diff --git a/cmake/opentelemetry-proto.cmake b/cmake/opentelemetry-proto.cmake index 1aa1ba0349..34b33d3cff 100644 --- a/cmake/opentelemetry-proto.cmake +++ b/cmake/opentelemetry-proto.cmake @@ -229,31 +229,47 @@ endif() include_directories("${GENERATED_PROTOBUF_PATH}") -if(WITH_OTLP_GRPC) - add_library( - opentelemetry_proto STATIC - ${COMMON_PB_CPP_FILE} - ${RESOURCE_PB_CPP_FILE} - ${TRACE_PB_CPP_FILE} - ${LOGS_PB_CPP_FILE} - ${METRICS_PB_CPP_FILE} - ${TRACE_SERVICE_PB_CPP_FILE} - ${TRACE_SERVICE_GRPC_PB_CPP_FILE} - ${LOGS_SERVICE_PB_CPP_FILE} - ${LOGS_SERVICE_GRPC_PB_CPP_FILE} - ${METRICS_SERVICE_PB_CPP_FILE} - ${METRICS_SERVICE_GRPC_PB_CPP_FILE}) +unset(OTELCPP_PROTO_TARGET_OPTIONS) +if(CMAKE_SYSTEM_NAME MATCHES "Windows|MinGW|WindowsStore") + list(APPEND OTELCPP_PROTO_TARGET_OPTIONS STATIC) +elseif(NOT DEFINED BUILD_SHARED_LIBS OR BUILD_SHARED_LIBS) + list(APPEND OTELCPP_PROTO_TARGET_OPTIONS SHARED) else() + list(APPEND OTELCPP_PROTO_TARGET_OPTIONS STATIC) +endif() + +set(OPENTELEMETRY_PROTO_TARGETS opentelemetry_proto) +add_library( + opentelemetry_proto + ${OTELCPP_PROTO_TARGET_OPTIONS} + ${COMMON_PB_CPP_FILE} + ${RESOURCE_PB_CPP_FILE} + ${TRACE_PB_CPP_FILE} + ${LOGS_PB_CPP_FILE} + ${METRICS_PB_CPP_FILE} + ${TRACE_SERVICE_PB_CPP_FILE} + ${LOGS_SERVICE_PB_CPP_FILE} + ${METRICS_SERVICE_PB_CPP_FILE}) + +if(WITH_OTLP_GRPC) add_library( - opentelemetry_proto STATIC - ${COMMON_PB_CPP_FILE} - ${RESOURCE_PB_CPP_FILE} - ${TRACE_PB_CPP_FILE} - ${LOGS_PB_CPP_FILE} - ${METRICS_PB_CPP_FILE} - ${TRACE_SERVICE_PB_CPP_FILE} - ${LOGS_SERVICE_PB_CPP_FILE} - ${METRICS_SERVICE_PB_CPP_FILE}) + opentelemetry_proto_grpc + ${OTELCPP_PROTO_TARGET_OPTIONS} ${TRACE_SERVICE_GRPC_PB_CPP_FILE} + ${LOGS_SERVICE_GRPC_PB_CPP_FILE} ${METRICS_SERVICE_GRPC_PB_CPP_FILE}) + + list(APPEND OPENTELEMETRY_PROTO_TARGETS opentelemetry_proto_grpc) + target_link_libraries(opentelemetry_proto_grpc PUBLIC opentelemetry_proto) + + set_target_properties(opentelemetry_proto_grpc PROPERTIES EXPORT_NAME + proto_grpc) + patch_protobuf_targets(opentelemetry_proto_grpc) + get_target_property(GRPC_INCLUDE_DIRECTORY gRPC::grpc++ + INTERFACE_INCLUDE_DIRECTORIES) + if(GRPC_INCLUDE_DIRECTORY) + target_include_directories( + opentelemetry_proto_grpc + PUBLIC "$") + endif() endif() if(needs_proto_download) @@ -264,7 +280,7 @@ patch_protobuf_targets(opentelemetry_proto) if(OPENTELEMETRY_INSTALL) install( - TARGETS opentelemetry_proto + TARGETS ${OPENTELEMETRY_PROTO_TARGETS} EXPORT "${PROJECT_NAME}-target" RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} @@ -282,9 +298,21 @@ if(TARGET protobuf::libprotobuf) else() # cmake 3.8 or lower target_include_directories(opentelemetry_proto PUBLIC ${Protobuf_INCLUDE_DIRS}) - target_link_libraries(opentelemetry_proto INTERFACE ${Protobuf_LIBRARIES}) + target_link_libraries(opentelemetry_proto PUBLIC ${Protobuf_LIBRARIES}) +endif() + +if(WITH_OTLP_GRPC) + if(WITH_ABSEIL) + find_package(absl CONFIG) + if(TARGET absl::synchronization) + target_link_libraries(opentelemetry_proto_grpc + PRIVATE absl::synchronization) + endif() + endif() endif() if(BUILD_SHARED_LIBS) - set_property(TARGET opentelemetry_proto PROPERTY POSITION_INDEPENDENT_CODE ON) + foreach(proto_target ${OPENTELEMETRY_PROTO_TARGETS}) + set_property(TARGET ${proto_target} PROPERTY POSITION_INDEPENDENT_CODE ON) + endforeach() endif() diff --git a/exporters/otlp/CMakeLists.txt b/exporters/otlp/CMakeLists.txt index 31bf627b0b..386bfcd918 100644 --- a/exporters/otlp/CMakeLists.txt +++ b/exporters/otlp/CMakeLists.txt @@ -16,9 +16,6 @@ target_include_directories( "$") set(OPENTELEMETRY_OTLP_TARGETS opentelemetry_otlp_recordable) -target_link_libraries( - opentelemetry_otlp_recordable - PUBLIC opentelemetry_trace opentelemetry_resources opentelemetry_proto) if(WITH_LOGS_PREVIEW) target_link_libraries(opentelemetry_otlp_recordable PUBLIC opentelemetry_logs) @@ -34,13 +31,18 @@ if(WITH_OTLP_GRPC) PROPERTIES EXPORT_NAME otlp_grpc_client) set_target_version(opentelemetry_exporter_otlp_grpc_client) + # Because opentelemetry_proto_grpc depends on gRPC, and gRPC can only be + # linked in one dynamic library or executable (otherwise, there may be + # conflicts with some global variables in certain versions of gRPC), we have + # switched the link for opentelemetry_exporter_otlp_grpc_client into all + # targets that depend on opentelemetry_proto_grpc. target_link_libraries( opentelemetry_exporter_otlp_grpc_client PUBLIC opentelemetry_sdk opentelemetry_common opentelemetry_ext - opentelemetry_proto) + # gRPC::grpc++ must be linked before opentelemetry_proto_grpc. + opentelemetry_proto_grpc + PRIVATE gRPC::grpc++) - target_link_libraries(opentelemetry_exporter_otlp_grpc_client - PRIVATE gRPC::grpc++) get_target_property(GRPC_INCLUDE_DIRECTORY gRPC::grpc++ INTERFACE_INCLUDE_DIRECTORIES) if(GRPC_INCLUDE_DIRECTORY) @@ -174,6 +176,10 @@ if(WITH_OTLP_HTTP) opentelemetry_exporter_otlp_http_metric) endif() +target_link_libraries( + opentelemetry_otlp_recordable + PUBLIC opentelemetry_trace opentelemetry_resources opentelemetry_proto) + if(OPENTELEMETRY_INSTALL) install( TARGETS ${OPENTELEMETRY_OTLP_TARGETS}