Skip to content

Commit 94875f1

Browse files
committed
add test for WITH_API_ONLY and fix the main cmake file so that option overrides third party package finds/fetches as required
1 parent b5443d4 commit 94875f1

File tree

5 files changed

+182
-63
lines changed

5 files changed

+182
-63
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,12 @@ jobs:
6868
run: |
6969
sudo -E ./ci/setup_ci_environment.sh
7070
sudo -E ./ci/setup_cmake.sh
71-
- name: run fetch content cmake test
71+
- name: run fetch content test
7272
run: |
7373
./ci/do_ci.sh cmake.fetch_content.test
74+
- name: run api only test
75+
run: |
76+
./ci/do_ci.sh cmake.api_only.test
7477
7578
cmake_gcc_maintainer_sync_test:
7679
name: CMake gcc 14 (maintainer mode, sync)

CMakeLists.txt

Lines changed: 72 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -260,84 +260,94 @@ if(MSVC)
260260
endif()
261261
endif()
262262

263-
if(NOT WITH_API_ONLY)
264-
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/threads.cmake")
265-
endif()
263+
#
264+
# API ONLY Dependencies
265+
#
266266

267267
if(WITH_GSL)
268268
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/ms-gsl.cmake")
269269
endif()
270270

271271
#
272-
# Do we need HTTP CLIENT CURL ?
272+
# SDK and Exporter Dependencies
273273
#
274274

275-
if(WITH_OTLP_HTTP
276-
OR WITH_ELASTICSEARCH
277-
OR WITH_ZIPKIN
278-
OR BUILD_W3CTRACECONTEXT_TEST
279-
OR WITH_EXAMPLES_HTTP)
280-
set(WITH_HTTP_CLIENT_CURL ON)
281-
else()
282-
set(WITH_HTTP_CLIENT_CURL OFF)
283-
endif()
275+
if(NOT WITH_API_ONLY)
284276

285-
#
286-
# Do we need ZLIB ?
287-
#
277+
if(WITH_OPENTRACING)
278+
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/opentracing-cpp.cmake")
279+
endif()
288280

289-
if((NOT WITH_API_ONLY)
290-
AND WITH_HTTP_CLIENT_CURL
291-
AND WITH_OTLP_HTTP_COMPRESSION)
292-
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/zlib.cmake")
293-
endif()
281+
# Always include Threads for the SDK
294282

295-
#
296-
# Do we need CURL ?
297-
#
283+
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/threads.cmake")
298284

299-
if((NOT WITH_API_ONLY) AND WITH_HTTP_CLIENT_CURL)
300-
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/curl.cmake")
301-
endif()
285+
#
286+
# Do we need HTTP CLIENT CURL ?
287+
#
302288

303-
if(WITH_OTLP_GRPC
304-
OR WITH_OTLP_HTTP
305-
OR WITH_OTLP_FILE)
289+
set(WITH_HTTP_CLIENT_CURL OFF)
290+
if(WITH_OTLP_HTTP
291+
OR WITH_ELASTICSEARCH
292+
OR WITH_ZIPKIN
293+
OR BUILD_W3CTRACECONTEXT_TEST
294+
OR WITH_EXAMPLES_HTTP)
295+
set(WITH_HTTP_CLIENT_CURL ON)
296+
endif()
306297

307-
if(WITH_OTLP_GRPC)
308-
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/grpc.cmake")
298+
#
299+
# Do we need ZLIB ?
300+
#
301+
302+
if(WITH_HTTP_CLIENT_CURL AND WITH_OTLP_HTTP_COMPRESSION)
303+
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/zlib.cmake")
309304
endif()
310305

311-
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/protobuf.cmake")
312-
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/opentelemetry-proto.cmake")
313-
endif()
306+
#
307+
# Do we need CURL ?
308+
#
314309

315-
#
316-
# Do we need NLOHMANN_JSON ?
317-
#
310+
if(WITH_HTTP_CLIENT_CURL)
311+
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/curl.cmake")
312+
endif()
318313

319-
if(WITH_ELASTICSEARCH
320-
OR WITH_ZIPKIN
321-
OR WITH_OTLP_HTTP
322-
OR WITH_OTLP_FILE
323-
OR BUILD_W3CTRACECONTEXT_TEST
324-
OR WITH_ETW)
325-
set(USE_NLOHMANN_JSON ON)
326-
else()
327-
set(USE_NLOHMANN_JSON OFF)
328-
endif()
314+
#
315+
# Do we need gRPC, Protobuf, and opentelemetry-proto ?
316+
#
329317

330-
if((NOT WITH_API_ONLY) AND USE_NLOHMANN_JSON)
331-
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/nlohmann-json.cmake")
332-
endif()
318+
if(WITH_OTLP_GRPC
319+
OR WITH_OTLP_HTTP
320+
OR WITH_OTLP_FILE)
333321

334-
if(WITH_PROMETHEUS)
335-
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/prometheus-cpp.cmake")
336-
endif()
322+
# Include gRPC before Protobuf We'll use the gRPC submodules for Protobuf
323+
# and Abseil with FetchContent
337324

338-
if(WITH_OPENTRACING)
339-
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/opentracing-cpp.cmake")
340-
endif()
325+
if(WITH_OTLP_GRPC)
326+
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/grpc.cmake")
327+
endif()
328+
329+
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/protobuf.cmake")
330+
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/opentelemetry-proto.cmake")
331+
endif()
332+
333+
#
334+
# Do we need NLOHMANN_JSON ?
335+
#
336+
337+
if(WITH_ELASTICSEARCH
338+
OR WITH_ZIPKIN
339+
OR WITH_OTLP_HTTP
340+
OR WITH_OTLP_FILE
341+
OR BUILD_W3CTRACECONTEXT_TEST
342+
OR WITH_ETW)
343+
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/nlohmann-json.cmake")
344+
endif()
345+
346+
if(WITH_PROMETHEUS)
347+
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/prometheus-cpp.cmake")
348+
endif()
349+
350+
endif(NOT WITH_API_ONLY)
341351

342352
if(OTELCPP_MAINTAINER_MODE)
343353
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
@@ -461,11 +471,12 @@ otel_print_build_config()
461471

462472
add_subdirectory(api)
463473

464-
if(WITH_OPENTRACING)
465-
add_subdirectory(opentracing-shim)
466-
endif()
467-
468474
if(NOT WITH_API_ONLY)
475+
476+
if(WITH_OPENTRACING)
477+
add_subdirectory(opentracing-shim)
478+
endif()
479+
469480
add_subdirectory(sdk)
470481
add_subdirectory(ext)
471482
add_subdirectory(exporters)

ci/do_ci.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,17 @@ elif [[ "$1" == "cmake.fetch_content.test" ]]; then
543543
make test
544544
exit 0
545545

546+
elif [[ "$1" == "cmake.api_only.test" ]]; then
547+
cd "${BUILD_DIR}"
548+
rm -rf *
549+
cmake "${CMAKE_OPTIONS[@]}" \
550+
-DOPENTELEMETRY_INSTALL=OFF \
551+
-DOPENTELEMETRY_CPP_SRC_DIR="${SRC_DIR}" \
552+
"${SRC_DIR}/install/test/cmake/api_only_test"
553+
make -j $(nproc)
554+
make test
555+
exit 0
556+
546557
elif [[ "$1" == "cmake.test_example_plugin" ]]; then
547558
# Build the plugin
548559
cd "${BUILD_DIR}"

cmake/otel-install-functions.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,6 @@ function(otel_print_build_config)
868868
message(STATUS "---------------------------------------------")
869869
message(STATUS "third-party options")
870870
message(STATUS "---------------------------------------------")
871-
message(STATUS "WITH_NLOHMANN_JSON: ${USE_NLOHMANN_JSON}")
872871
message(STATUS "WITH_CURL_LOGGING: ${WITH_CURL_LOGGING}")
873872
message(STATUS "WITH_OTLP_HTTP_COMPRESSION: ${WITH_OTLP_HTTP_COMPRESSION}")
874873
message(STATUS "---------------------------------------------")
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# This test covers the WITH_API_ONLY build option of opentelemetry-cpp. It
5+
# ensures that no third-party libraries are included in the build and that only
6+
# the opentelemetry-cpp::api target is created.
7+
8+
# CMake 3.24+ is required to test all targets imported globally
9+
10+
cmake_minimum_required(VERSION 3.24)
11+
12+
project(opentelemetry-cpp-api-only-test LANGUAGES CXX)
13+
14+
if(NOT OPENTELEMETRY_CPP_SRC_DIR)
15+
message(
16+
FATAL_ERROR
17+
"OPENTELEMETRY_CPP_SRC_DIR must be defined when running cmake on this test project"
18+
)
19+
endif()
20+
21+
# This setting requires CMake 3.24 or later
22+
set(CMAKE_FIND_PACKAGE_TARGETS_GLOBAL ON)
23+
24+
# Turn on the WITH_API_ONLY build option and disable GSL. This must override any
25+
# other options and not import third party targets.
26+
set(WITH_API_ONLY ON)
27+
set(WITH_GSL OFF)
28+
29+
# Turn on all components that should be overridden by WITH_API_ONLY
30+
set(WITH_OTLP_FILE ON)
31+
set(WITH_OTLP_HTTP ON)
32+
set(WITH_OTLP_GRPC ON)
33+
set(WITH_OTLP_HTTP_COMPRESSION ON)
34+
set(WITH_PROMETHEUS ON)
35+
set(WITH_ZIPKIN ON)
36+
set(WITH_ELASTICSEARCH ON)
37+
set(WITH_OPENTRACING ON)
38+
39+
# Build testing off should disable importing Gtest and Benchmark targets
40+
set(BUILD_TESTING OFF)
41+
42+
include(FetchContent)
43+
FetchContent_Declare(opentelemetry-cpp SOURCE_DIR ${OPENTELEMETRY_CPP_SRC_DIR})
44+
FetchContent_MakeAvailable(opentelemetry-cpp)
45+
46+
set(UNEXPECTED_TARGETS
47+
opentelemetry-cpp::sdk
48+
Microsoft.GSL::GSL
49+
Threads::Threads
50+
ZLIB::ZLIB
51+
CURL::libcurl
52+
protobuf::libprotobuf
53+
gRPC::grpc++
54+
nlohmann_json::nlohmann_json
55+
OpenTracing::opentracing
56+
OpenTracing::opentracing_static
57+
GTest::gtest
58+
benchmark::benchmark
59+
prometheus-cpp::core)
60+
61+
foreach(target IN LISTS UNEXPECTED_TARGETS)
62+
if(TARGET ${target})
63+
message(
64+
FATAL_ERROR
65+
"Unexpected target ${target} was created in an WITH_API_ONLY build.")
66+
endif()
67+
endforeach()
68+
69+
if(NOT TARGET opentelemetry-cpp::api)
70+
message(
71+
FATAL_ERROR
72+
"opentelemetry-cpp::api target was not created in an WITH_API_ONLY build."
73+
)
74+
endif()
75+
76+
FetchContent_Declare(
77+
googletest SOURCE_DIR ${OPENTELEMETRY_CPP_SRC_DIR}/third_party/googletest)
78+
79+
FetchContent_MakeAvailable(googletest)
80+
81+
add_executable(api_only_test
82+
${OPENTELEMETRY_CPP_SRC_DIR}/install/test/src/test_api.cc)
83+
84+
target_link_libraries(api_only_test PRIVATE opentelemetry-cpp::api GTest::gtest
85+
GTest::gtest_main)
86+
87+
set(BUILD_TESTING ON)
88+
89+
include(CTest)
90+
include(GoogleTest)
91+
92+
gtest_add_tests(
93+
TARGET api_only_test
94+
TEST_PREFIX api_only.
95+
TEST_LIST api_only_test)

0 commit comments

Comments
 (0)