Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 69 additions & 32 deletions cmake/opentelemetry-proto.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
# SPDX-License-Identifier: Apache-2.0

#
# The dependency on opentelemetry-proto can be provided by order
# of decreasing priority, options are:
# The dependency on opentelemetry-proto can be provided by order of decreasing
# priority, options are:
#
# 1 - Fetch from local source directory defined by the OTELCPP_PROTO_PATH variable
# 1 - Fetch from local source directory defined by the OTELCPP_PROTO_PATH
# variable
#
# 2 - Fetch from the opentelemetry-proto git submodule (opentelemetry-cpp/third_party/opentelemetry-proto)
# 2 - Fetch from the opentelemetry-proto git submodule
# (opentelemetry-cpp/third_party/opentelemetry-proto)
#
# 3 - Fetch from github using the git tag set in opentelemetry-cpp/third_party_release
# 3 - Fetch from github using the git tag set in
# opentelemetry-cpp/third_party_release
#

set(OPENTELEMETRY_PROTO_SUBMODULE "${opentelemetry-cpp_SOURCE_DIR}/third_party/opentelemetry-proto")
set(OPENTELEMETRY_PROTO_SUBMODULE
"${opentelemetry-cpp_SOURCE_DIR}/third_party/opentelemetry-proto")

if(OTELCPP_PROTO_PATH)
if(NOT EXISTS
Expand All @@ -21,29 +25,32 @@ if(OTELCPP_PROTO_PATH)
FATAL_ERROR
"OTELCPP_PROTO_PATH does not point to a opentelemetry-proto repository")
endif()
message(STATUS "fetching opentelemetry-proto from OTELCPP_PROTO_PATH=${OTELCPP_PROTO_PATH}")
FetchContent_Declare(
opentelemetry-proto
SOURCE_DIR ${OTELCPP_PROTO_PATH}
message(
STATUS
"fetching opentelemetry-proto from OTELCPP_PROTO_PATH=${OTELCPP_PROTO_PATH}"
)
FetchContent_Declare(opentelemetry-proto SOURCE_DIR ${OTELCPP_PROTO_PATH})
set(opentelemetry-proto_PROVIDER "fetch_source")
# If the opentelemetry-proto directory is a general directory then we don't have a good way to determine the version. Set it as unknown.
# If the opentelemetry-proto directory is a general directory then we don't
# have a good way to determine the version. Set it as unknown.
set(opentelemetry-proto_VERSION "unknown")
elseif(EXISTS ${OPENTELEMETRY_PROTO_SUBMODULE}/.git)
message(STATUS "fetching opentelemetry-proto from git submodule")
FetchContent_Declare(
opentelemetry-proto
SOURCE_DIR ${OPENTELEMETRY_PROTO_SUBMODULE}
)
message(STATUS "fetching opentelemetry-proto from git submodule")
FetchContent_Declare(opentelemetry-proto SOURCE_DIR
${OPENTELEMETRY_PROTO_SUBMODULE})
set(opentelemetry-proto_PROVIDER "fetch_source")
string(REGEX REPLACE "^v([0-9]+\\.[0-9]+\\.[0-9]+)$" "\\1" opentelemetry-proto_VERSION "${opentelemetry-proto_GIT_TAG}")
string(REGEX
REPLACE "^v([0-9]+\\.[0-9]+\\.[0-9]+)$" "\\1"
opentelemetry-proto_VERSION "${opentelemetry-proto_GIT_TAG}")
else()
FetchContent_Declare(
opentelemetry-proto
GIT_REPOSITORY https://github.com/open-telemetry/opentelemetry-proto.git
GIT_TAG "${opentelemetry-proto_GIT_TAG}")
opentelemetry-proto
GIT_REPOSITORY https://github.com/open-telemetry/opentelemetry-proto.git
GIT_TAG "${opentelemetry-proto_GIT_TAG}")
set(opentelemetry-proto_PROVIDER "fetch_repository")
string(REGEX REPLACE "^v([0-9]+\\.[0-9]+\\.[0-9]+)$" "\\1" opentelemetry-proto_VERSION "${opentelemetry-proto_GIT_TAG}")
string(REGEX
REPLACE "^v([0-9]+\\.[0-9]+\\.[0-9]+)$" "\\1"
opentelemetry-proto_VERSION "${opentelemetry-proto_GIT_TAG}")
endif()

FetchContent_MakeAvailable(opentelemetry-proto)
Expand Down Expand Up @@ -174,8 +181,10 @@ foreach(IMPORT_DIR ${PROTOBUF_IMPORT_DIRS})
list(APPEND PROTOBUF_INCLUDE_FLAGS "-I${IMPORT_DIR}")
endforeach()

set(PROTOBUF_COMMON_FLAGS "--proto_path=${PROTO_PATH}"
"--cpp_out=${GENERATED_PROTOBUF_PATH}")
set(PROTOBUF_COMMON_FLAGS
"--proto_path=${PROTO_PATH}"
"--cpp_out=dllexport_decl=OPENTELEMETRY_PROTO_API:${GENERATED_PROTOBUF_PATH}"
)
# --experimental_allow_proto3_optional is available from 3.13 and be stable and
# enabled by default from 3.16
if(Protobuf_VERSION AND Protobuf_VERSION VERSION_LESS "3.16")
Expand Down Expand Up @@ -276,13 +285,13 @@ add_custom_command(
DEPENDS ${PROTOBUF_GENERATE_DEPENDS})

unset(OTELCPP_PROTO_TARGET_OPTIONS)
if(CMAKE_SYSTEM_NAME MATCHES "Windows|MinGW|WindowsStore")
list(APPEND OTELCPP_PROTO_TARGET_OPTIONS STATIC)
elseif((NOT protobuf_lib_type STREQUAL "STATIC_LIBRARY")
AND (NOT DEFINED BUILD_SHARED_LIBS OR BUILD_SHARED_LIBS))
if((NOT protobuf_lib_type STREQUAL "STATIC_LIBRARY")
AND (NOT DEFINED BUILD_SHARED_LIBS OR BUILD_SHARED_LIBS))
list(APPEND OTELCPP_PROTO_TARGET_OPTIONS SHARED)
set(OTELCPP_PROTO_LIB_TYPE "SHARED_LIBRARY")
else()
list(APPEND OTELCPP_PROTO_TARGET_OPTIONS STATIC)
set(OTELCPP_PROTO_LIB_TYPE "STATIC_LIBRARY")
endif()

set(OPENTELEMETRY_PROTO_TARGETS opentelemetry_proto)
Expand All @@ -299,13 +308,26 @@ add_library(
${METRICS_SERVICE_PB_CPP_FILE})
set_target_version(opentelemetry_proto)

if(OTELCPP_PROTO_LIB_TYPE STREQUAL "SHARED_LIBRARY")
project_build_tools_set_shared_library_declaration(OPENTELEMETRY_PROTO_API
opentelemetry_proto)
else()
project_build_tools_set_static_library_declaration(OPENTELEMETRY_PROTO_API
opentelemetry_proto)
endif()

target_include_directories(
opentelemetry_proto PUBLIC "$<BUILD_INTERFACE:${GENERATED_PROTOBUF_PATH}>"
"$<INSTALL_INTERFACE:include>")

# Disable include-what-you-use and clang-tidy on generated code.
set_target_properties(opentelemetry_proto PROPERTIES CXX_INCLUDE_WHAT_YOU_USE ""
CXX_CLANG_TIDY "")
set_target_properties(
opentelemetry_proto
PROPERTIES CXX_INCLUDE_WHAT_YOU_USE ""
CXX_CLANG_TIDY ""
C_VISIBILITY_PRESET "hidden"
CXX_VISIBILITY_PRESET "hidden"
VISIBILITY_INLINES_HIDDEN OFF)
if(NOT Protobuf_INCLUDE_DIRS AND TARGET protobuf::libprotobuf)
get_target_property(Protobuf_INCLUDE_DIRS protobuf::libprotobuf
INTERFACE_INCLUDE_DIRECTORIES)
Expand All @@ -323,10 +345,24 @@ if(WITH_OTLP_GRPC)
${LOGS_SERVICE_GRPC_PB_CPP_FILE} ${METRICS_SERVICE_GRPC_PB_CPP_FILE})
set_target_version(opentelemetry_proto_grpc)

if(OTELCPP_PROTO_LIB_TYPE STREQUAL "SHARED_LIBRARY")
if(CMAKE_SYSTEM_NAME MATCHES "Windows|MinGW|WindowsStore")
# The codes generated by gRPC plugin do not support dll export/import
# declarations. To work around this, we enable WINDOWS_EXPORT_ALL_SYMBOLS
# property to export all symbols when building a shared library.
set_target_properties(opentelemetry_proto_grpc
PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()
endif()

# Disable include-what-you-use and clang-tidy on generated code.
set_target_properties(
opentelemetry_proto_grpc PROPERTIES CXX_INCLUDE_WHAT_YOU_USE ""
CXX_CLANG_TIDY "")
opentelemetry_proto_grpc
PROPERTIES CXX_INCLUDE_WHAT_YOU_USE ""
CXX_CLANG_TIDY ""
C_VISIBILITY_PRESET "default"
CXX_VISIBILITY_PRESET "default"
VISIBILITY_INLINES_HIDDEN OFF)

list(APPEND OPENTELEMETRY_PROTO_TARGETS opentelemetry_proto_grpc)
target_link_libraries(opentelemetry_proto_grpc PUBLIC opentelemetry_proto)
Expand All @@ -338,7 +374,8 @@ if(WITH_OTLP_GRPC)
# opentelemetry_exporter_otlp_grpc_client as a static library.
get_target_property(grpc_lib_type gRPC::grpc++ TYPE)

if(grpc_lib_type STREQUAL "SHARED_LIBRARY")
if(grpc_lib_type STREQUAL "SHARED_LIBRARY" OR OTELCPP_PROTO_LIB_TYPE STREQUAL
"SHARED_LIBRARY")
target_link_libraries(opentelemetry_proto_grpc PUBLIC gRPC::grpc++)
endif()
set_target_properties(opentelemetry_proto_grpc PROPERTIES EXPORT_NAME
Expand Down
97 changes: 97 additions & 0 deletions cmake/tools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,100 @@ function(project_build_tools_patch_default_imported_config)
endif()
endforeach()
endfunction()

function(project_build_tools_set_export_declaration OUTPUT_VARNAME)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang|Intel|XL|XLClang")
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(${OUTPUT_VARNAME}
"__attribute__((__dllexport__))"
PARENT_SCOPE)
else()
set(${OUTPUT_VARNAME}
"__attribute__((visibility(\"default\")))"
PARENT_SCOPE)
endif()
elseif(MSVC)
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(${OUTPUT_VARNAME}
"__declspec(dllexport)"
PARENT_SCOPE)
else()
set(${OUTPUT_VARNAME}
""
PARENT_SCOPE)
endif()
elseif(SunPro)
set(${OUTPUT_VARNAME}
"__global"
PARENT_SCOPE)
Comment on lines +247 to +250
Copy link

Copilot AI Oct 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SunPro compiler check uses an undefined variable SunPro instead of checking CMAKE_CXX_COMPILER_ID. This condition will never be true. Change to elseif(CMAKE_CXX_COMPILER_ID STREQUAL \"SunPro\") to properly detect the SunPro compiler.

Copilot uses AI. Check for mistakes.
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(${OUTPUT_VARNAME}
"__declspec(dllexport)"
PARENT_SCOPE)
else()
set(${OUTPUT_VARNAME}
""
PARENT_SCOPE)
endif()
endfunction()

function(project_build_tools_set_import_declaration OUTPUT_VARNAME)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang|Intel|XL|XLClang")
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(${OUTPUT_VARNAME}
"__attribute__((__dllimport__))"
PARENT_SCOPE)
else()
set(${OUTPUT_VARNAME}
""
PARENT_SCOPE)
endif()
elseif(MSVC)
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(${OUTPUT_VARNAME}
"__declspec(dllimport)"
PARENT_SCOPE)
else()
set(${OUTPUT_VARNAME}
""
PARENT_SCOPE)
endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
set(${OUTPUT_VARNAME}
"__global"
PARENT_SCOPE)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(${OUTPUT_VARNAME}
"__declspec(dllimport)"
PARENT_SCOPE)
else()
set(${OUTPUT_VARNAME}
""
PARENT_SCOPE)
endif()
endfunction()

function(project_build_tools_set_shared_library_declaration DEFINITION_VARNAME)
project_build_tools_set_export_declaration(EXPORT_DECLARATION)
project_build_tools_set_import_declaration(IMPORT_DECLARATION)
foreach(TARGET_NAME ${ARGN})
target_compile_definitions(
${TARGET_NAME} INTERFACE "${DEFINITION_VARNAME}=${IMPORT_DECLARATION}")
target_compile_definitions(
${TARGET_NAME} PRIVATE "${DEFINITION_VARNAME}=${EXPORT_DECLARATION}")
endforeach()
endfunction()

function(project_build_tools_set_static_library_declaration DEFINITION_VARNAME)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang|Intel|XL|XLClang")
foreach(TARGET_NAME ${ARGN})
target_compile_definitions(
${TARGET_NAME}
PUBLIC "${DEFINITION_VARNAME}=__attribute__((visibility(\"default\")))")
endforeach()
else()
foreach(TARGET_NAME ${ARGN})
target_compile_definitions(${TARGET_NAME} PUBLIC "${DEFINITION_VARNAME}=")
endforeach()
endif()
endfunction()
14 changes: 9 additions & 5 deletions exporters/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,12 @@ if(WITH_OTLP_HTTP)

target_link_libraries(
opentelemetry_exporter_otlp_http_client
PUBLIC opentelemetry_sdk opentelemetry_ext
# Links flags of opentelemetry_http_client_curl should be public when
# building internal components
PRIVATE opentelemetry_proto
PUBLIC opentelemetry_sdk
opentelemetry_ext
# Links flags of opentelemetry_http_client_curl should be public when
# building internal components
"$<BUILD_INTERFACE:opentelemetry_proto>"
PRIVATE "$<INSTALL_INTERFACE:opentelemetry_proto>"
"$<BUILD_INTERFACE:opentelemetry_http_client_curl>"
"$<BUILD_INTERFACE:nlohmann_json::nlohmann_json>"
"$<INSTALL_INTERFACE:opentelemetry_http_client_curl>")
Expand Down Expand Up @@ -409,7 +411,9 @@ if(WITH_OTLP_FILE)
target_link_libraries(
opentelemetry_exporter_otlp_file_client
PUBLIC opentelemetry_sdk opentelemetry_common
PRIVATE opentelemetry_proto $<BUILD_INTERFACE:nlohmann_json::nlohmann_json>)
"$<BUILD_INTERFACE:opentelemetry_proto>"
PRIVATE "$<INSTALL_INTERFACE:opentelemetry_proto>"
"$<BUILD_INTERFACE:nlohmann_json::nlohmann_json>")

target_include_directories(
opentelemetry_exporter_otlp_file_client
Expand Down
Loading