Skip to content

Commit a1e6011

Browse files
authored
[CMAKE] Address the vcpkg opentelemetry-cpp port CMake patches (#3518)
1 parent 3eec22c commit a1e6011

File tree

6 files changed

+112
-70
lines changed

6 files changed

+112
-70
lines changed

.github/workflows/cmake_install.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ jobs:
215215
run: ./ci/do_ci.sh cmake.opentracing_shim.install.test
216216

217217
ubuntu_2404_conan_latest:
218-
name: Ubuntu 24.04 conan latest versions cxx17 (static libs)
218+
name: Ubuntu 24.04 conan latest versions cxx17 (static libs - opentracing shim)
219219
runs-on: ubuntu-24.04
220220
env:
221221
INSTALL_TEST_DIR: '/home/runner/install_test'
@@ -248,6 +248,8 @@ jobs:
248248
run: |
249249
export PKG_CONFIG_PATH=$INSTALL_TEST_DIR/lib/pkgconfig:$PKG_CONFIG_PATH
250250
./ci/verify_packages.sh
251+
- name: Run OpenTracing Shim Test
252+
run: ./ci/do_ci.sh cmake.opentracing_shim.install.test
251253

252254
macos_14_conan_stable:
253255
name: macOS 14 conan stable versions cxx17 (static libs)

CMakeLists.txt

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ project(opentelemetry-cpp)
3636
# Mark variables as used so cmake doesn't complain about them
3737
mark_as_advanced(CMAKE_TOOLCHAIN_FILE)
3838

39-
# Note: CMAKE_FIND_PACKAGE_PREFER_CONFIG requires cmake 3.15. Prefer cmake
40-
# CONFIG search mode to find dependencies. This is important to properly find
41-
# protobuf versions 3.22.0 and above due to the abseil-cpp dependency.
42-
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG TRUE)
43-
4439
# Don't use customized cmake modules if vcpkg is used to resolve dependence.
4540
if(NOT DEFINED CMAKE_TOOLCHAIN_FILE)
4641
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules/")
@@ -79,10 +74,6 @@ if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
7974
CACHE STRING "")
8075
endif()
8176

82-
if(VCPKG_CHAINLOAD_TOOLCHAIN_FILE)
83-
include("${VCPKG_CHAINLOAD_TOOLCHAIN_FILE}")
84-
endif()
85-
8677
option(WITH_ABI_VERSION_1 "ABI version 1" ON)
8778
option(WITH_ABI_VERSION_2 "EXPERIMENTAL: ABI version 2 preview" OFF)
8879

@@ -497,6 +488,14 @@ if((NOT WITH_API_ONLY) AND USE_NLOHMANN_JSON)
497488
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/nlohmann-json.cmake")
498489
endif()
499490

491+
#
492+
# Do we need OpenTracing ?
493+
#
494+
495+
if(WITH_OPENTRACING)
496+
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/opentracing-cpp.cmake")
497+
endif()
498+
500499
if(OTELCPP_MAINTAINER_MODE)
501500
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
502501
message(STATUS "Building with gcc in maintainer mode.")
@@ -703,6 +702,10 @@ endif()
703702
if(prometheus-cpp_FOUND)
704703
message(STATUS "prometheus-cpp: ${prometheus-cpp_VERSION}")
705704
endif()
705+
if(WITH_OPENTRACING)
706+
message(
707+
STATUS "opentracing-cpp: ${OpenTracing_VERSION} (${OpenTracing_PROVIDER})")
708+
endif()
706709
message(STATUS "---------------------------------------------")
707710

708711
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/otel-install-functions.cmake")
@@ -726,30 +729,6 @@ endif()
726729
add_subdirectory(api)
727730

728731
if(WITH_OPENTRACING)
729-
find_package(OpenTracing CONFIG QUIET)
730-
if(NOT OpenTracing_FOUND)
731-
set(OPENTRACING_DIR "third_party/opentracing-cpp")
732-
message(STATUS "Trying to use local ${OPENTRACING_DIR} from submodule")
733-
if(EXISTS "${PROJECT_SOURCE_DIR}/${OPENTRACING_DIR}/.git")
734-
set(SAVED_BUILD_TESTING ${BUILD_TESTING})
735-
set(BUILD_TESTING OFF)
736-
set(SAVED_CMAKE_CXX_INCLUDE_WHAT_YOU_USE
737-
${CMAKE_CXX_INCLUDE_WHAT_YOU_USE})
738-
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "")
739-
add_subdirectory(${OPENTRACING_DIR})
740-
set(BUILD_TESTING ${SAVED_BUILD_TESTING})
741-
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE
742-
${SAVED_CMAKE_CXX_INCLUDE_WHAT_YOU_USE})
743-
else()
744-
message(
745-
FATAL_ERROR
746-
"\nopentracing-cpp package was not found. Please either provide it manually or clone with submodules. "
747-
"To initialize, fetch and checkout any nested submodules, you can use the following command:\n"
748-
"git submodule update --init --recursive")
749-
endif()
750-
else()
751-
message(STATUS "Using external opentracing-cpp")
752-
endif()
753732
add_subdirectory(opentracing-shim)
754733
endif()
755734

cmake/opentracing-cpp.cmake

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
find_package(OpenTracing CONFIG QUIET)
5+
set(OpenTracing_PROVIDER "find_package")
6+
7+
if(NOT OpenTracing_FOUND)
8+
set(_OPENTRACING_SUBMODULE_DIR "${opentelemetry-cpp_SOURCE_DIR}/third_party/opentracing-cpp")
9+
if(EXISTS "${_OPENTRACING_SUBMODULE_DIR}/.git")
10+
FetchContent_Declare(
11+
"opentracing"
12+
SOURCE_DIR "${_OPENTRACING_SUBMODULE_DIR}"
13+
)
14+
set(OpenTracing_PROVIDER "fetch_source")
15+
else()
16+
FetchContent_Declare(
17+
"opentracing"
18+
GIT_REPOSITORY "https://github.com/opentracing/opentracing-cpp.git"
19+
GIT_TAG "${opentracing-cpp_GIT_TAG}"
20+
)
21+
set(OpenTracing_PROVIDER "fetch_repository")
22+
endif()
23+
24+
# OpenTracing uses the BUILD_TESTING variable directly and we must force the cached value to OFF.
25+
# save the current state of BUILD_TESTING
26+
if(DEFINED BUILD_TESTING)
27+
set(_SAVED_BUILD_TESTING ${BUILD_TESTING})
28+
endif()
29+
30+
# Set the cache variables for the opentracing build
31+
set(BUILD_TESTING OFF CACHE BOOL "" FORCE)
32+
set(BUILD_MOCKTRACER OFF CACHE BOOL "" FORCE)
33+
34+
FetchContent_MakeAvailable(opentracing)
35+
36+
# Restore the saved state of BUILD_TESTING
37+
if(DEFINED _SAVED_BUILD_TESTING)
38+
set(BUILD_TESTING ${_SAVED_BUILD_TESTING} CACHE BOOL "" FORCE)
39+
else()
40+
unset(BUILD_TESTING CACHE)
41+
endif()
42+
43+
# Patch the opentracing targets to set missing includes, add namespaced alias targets, disable iwyu and clang-tidy.
44+
foreach(_target opentracing opentracing-static)
45+
if(TARGET ${_target})
46+
# Add missing include directories
47+
target_include_directories(${_target} PUBLIC
48+
"$<BUILD_INTERFACE:${opentracing_SOURCE_DIR}/include>"
49+
"$<BUILD_INTERFACE:${opentracing_SOURCE_DIR}/3rd_party/include>"
50+
"$<BUILD_INTERFACE:${opentracing_BINARY_DIR}/include>"
51+
)
52+
# Disable CXX_INCLUDE_WHAT_YOU_USE and CXX_CLANG_TIDY
53+
set_target_properties(${_target}
54+
PROPERTIES CXX_INCLUDE_WHAT_YOU_USE "" CXX_CLANG_TIDY "")
55+
# Create alias targets
56+
if(NOT TARGET OpenTracing::${_target})
57+
add_library(OpenTracing::${_target} ALIAS ${_target})
58+
endif()
59+
endif()
60+
endforeach()
61+
62+
# Set the OpenTracing_VERSION variable from the git tag.
63+
string(REGEX REPLACE "^v([0-9]+\\.[0-9]+\\.[0-9]+)$" "\\1" OpenTracing_VERSION "${opentracing-cpp_GIT_TAG}")
64+
endif(NOT OpenTracing_FOUND)
65+
66+
if(NOT TARGET OpenTracing::opentracing AND NOT TARGET OpenTracing::opentracing-static)
67+
message(FATAL_ERROR "No OpenTracing targets (OpenTracing::opentracing or OpenTracing::opentracing-static) were imported")
68+
endif()

install/conan/conanfile_latest.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protobuf/*:shared=False
2525
abseil/*:fPIC=True
2626
abseil/*:shared=False
2727
opentracing-cpp/*:fPIC=True
28-
opentracing-cpp/*:shared=True
28+
opentracing-cpp/*:shared=False
2929
pcre2/*:with_bzip2=False
3030

3131
[test_requires]

install/test/cmake/component_tests/shims_opentracing/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ project(opentelemetry-cpp-shims_opentracing-install-test LANGUAGES CXX)
66

77
find_package(opentelemetry-cpp REQUIRED COMPONENTS shims_opentracing)
88

9-
if(NOT TARGET OpenTracing::opentracing)
10-
message(FATAL_ERROR "OpenTracing::opentracing target not found")
9+
if(NOT TARGET OpenTracing::opentracing AND NOT TARGET
10+
OpenTracing::opentracing-static)
11+
message(
12+
FATAL_ERROR
13+
"A required OpenTracing target (OpenTracing::opentracing or OpenTracing::opentracing-static) was not imported"
14+
)
1115
endif()
1216

1317
find_package(GTest CONFIG REQUIRED)

opentracing-shim/CMakeLists.txt

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

4-
set(this_target opentelemetry_opentracing_shim)
4+
add_library(
5+
opentelemetry_opentracing_shim src/shim_utils.cc src/span_shim.cc
6+
src/span_context_shim.cc src/tracer_shim.cc)
57

6-
add_library(${this_target} src/shim_utils.cc src/span_shim.cc
7-
src/span_context_shim.cc src/tracer_shim.cc)
8-
9-
set_target_properties(${this_target} PROPERTIES EXPORT_NAME opentracing_shim)
10-
set_target_version(${this_target})
8+
set_target_properties(opentelemetry_opentracing_shim
9+
PROPERTIES EXPORT_NAME opentracing_shim)
10+
set_target_version(opentelemetry_opentracing_shim)
1111

1212
target_include_directories(
13-
${this_target} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
14-
"$<INSTALL_INTERFACE:include>")
13+
opentelemetry_opentracing_shim
14+
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
15+
"$<INSTALL_INTERFACE:include>")
16+
17+
target_link_libraries(opentelemetry_opentracing_shim PUBLIC opentelemetry_api)
1518

16-
if(OPENTRACING_DIR)
17-
target_include_directories(
18-
${this_target}
19-
PUBLIC
20-
"$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/${OPENTRACING_DIR}/include>"
21-
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/${OPENTRACING_DIR}/include>"
22-
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/${OPENTRACING_DIR}/3rd_party/include>"
23-
)
24-
target_link_libraries(${this_target} opentelemetry_api opentracing)
19+
# link to the shared library target (OpenTracing::opentracing) if it exists
20+
# otherwise use the static library target.
21+
if(TARGET OpenTracing::opentracing)
22+
target_link_libraries(opentelemetry_opentracing_shim
23+
PUBLIC OpenTracing::opentracing)
2524
else()
26-
target_link_libraries(${this_target} opentelemetry_api
27-
OpenTracing::opentracing)
25+
target_link_libraries(opentelemetry_opentracing_shim
26+
PUBLIC OpenTracing::opentracing-static)
2827
endif()
2928

3029
otel_add_component(
3130
COMPONENT
3231
shims_opentracing
3332
TARGETS
34-
${this_target}
33+
opentelemetry_opentracing_shim
3534
FILES_DIRECTORY
3635
"include/opentelemetry/opentracingshim"
3736
FILES_DESTINATION
@@ -43,20 +42,10 @@ otel_add_component(
4342
if(BUILD_TESTING)
4443
foreach(testname propagation_test shim_utils_test span_shim_test
4544
span_context_shim_test tracer_shim_test)
46-
4745
add_executable(${testname} "test/${testname}.cc")
48-
49-
if(OPENTRACING_DIR)
50-
target_link_libraries(
51-
${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}
52-
opentelemetry_api opentelemetry_opentracing_shim opentracing)
53-
else()
54-
target_link_libraries(
55-
${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}
56-
opentelemetry_api opentelemetry_opentracing_shim
57-
OpenTracing::opentracing)
58-
endif()
59-
46+
target_link_libraries(
47+
${testname} PRIVATE ${GTEST_BOTH_LIBRARIES} Threads::Threads
48+
opentelemetry_opentracing_shim)
6049
gtest_add_tests(
6150
TARGET ${testname}
6251
TEST_PREFIX opentracing_shim.

0 commit comments

Comments
 (0)