diff --git a/.github/workflows/cmake_install.yml b/.github/workflows/cmake_install.yml index c62d7def82..0406db5292 100644 --- a/.github/workflows/cmake_install.yml +++ b/.github/workflows/cmake_install.yml @@ -254,6 +254,7 @@ jobs: run: | sudo -E ./ci/setup_cmake.sh conan install install/conan/conanfile_stable.txt --build=missing -of /home/runner/conan -s build_type=${BUILD_TYPE} -s compiler.cppstd=${CXX_STANDARD} + conan cache clean --source --build - name: Run Tests (static libs) env: BUILD_SHARED_LIBS: 'OFF' @@ -297,6 +298,7 @@ jobs: run: | sudo -E ./ci/setup_cmake.sh conan install install/conan/conanfile_latest.txt --build=missing -of /home/runner/conan -s build_type=${BUILD_TYPE} -s compiler.cppstd=${CXX_STANDARD} + conan cache clean --source --build - name: Run Tests (static libs) env: BUILD_SHARED_LIBS: 'OFF' @@ -330,7 +332,9 @@ jobs: ./ci/setup_cmake_macos.sh conan profile detect --force - name: Install or build all dependencies with Conan - run: conan install install/conan/conanfile_stable.txt --build=missing -of /Users/runner/conan -s build_type=${BUILD_TYPE} -s compiler.cppstd=${CXX_STANDARD} + run: | + conan install install/conan/conanfile_stable.txt --build=missing -of /Users/runner/conan -s build_type=${BUILD_TYPE} -s compiler.cppstd=${CXX_STANDARD} + conan cache clean --source --build - name: Run Tests (static libs) env: BUILD_SHARED_LIBS: 'OFF' diff --git a/CHANGELOG.md b/CHANGELOG.md index 30c94679ed..765038b362 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,9 @@ Increment the: ## [Unreleased] +* [TEST] Test examples in CI with CMake Part 1 + [#3449](https://github.com/open-telemetry/opentelemetry-cpp/pull/3449) + ## [1.21 2025-05-28] * [BUILD] Remove WITH_ABSEIL diff --git a/ci/do_ci.ps1 b/ci/do_ci.ps1 index 87aa73b6fa..a5d3b2044f 100644 --- a/ci/do_ci.ps1 +++ b/ci/do_ci.ps1 @@ -77,23 +77,8 @@ switch ($action) { if ($exit -ne 0) { exit $exit } - ctest -C Debug - $exit = $LASTEXITCODE - if ($exit -ne 0) { - exit $exit - } $env:PATH = "$BUILD_DIR\ext\src\dll\Debug;$env:PATH" - examples\simple\Debug\example_simple.exe - $exit = $LASTEXITCODE - if ($exit -ne 0) { - exit $exit - } - examples\metrics_simple\Debug\metrics_ostream_example.exe - $exit = $LASTEXITCODE - if ($exit -ne 0) { - exit $exit - } - examples\logs_simple\Debug\example_logs_simple.exe + ctest -C Debug $exit = $LASTEXITCODE if ($exit -ne 0) { exit $exit @@ -115,23 +100,8 @@ switch ($action) { if ($exit -ne 0) { exit $exit } - ctest -C Debug - $exit = $LASTEXITCODE - if ($exit -ne 0) { - exit $exit - } $env:PATH = "$BUILD_DIR\ext\src\dll\Debug;$env:PATH" - examples\simple\Debug\example_simple.exe - $exit = $LASTEXITCODE - if ($exit -ne 0) { - exit $exit - } - examples\metrics_simple\Debug\metrics_ostream_example.exe - $exit = $LASTEXITCODE - if ($exit -ne 0) { - exit $exit - } - examples\logs_simple\Debug\example_logs_simple.exe + ctest -C Debug $exit = $LASTEXITCODE if ($exit -ne 0) { exit $exit @@ -277,6 +247,7 @@ switch ($action) { if ($exit -ne 0) { exit $exit } + $env:PATH = "$BUILD_DIR\ext\src\dll\Debug;$env:PATH" ctest -C Debug $exit = $LASTEXITCODE if ($exit -ne 0) { @@ -467,13 +438,13 @@ switch ($action) { $CMAKE_OPTIONS = @( "-DCMAKE_CXX_STANDARD=17", "-DVCPKG_TARGET_TRIPLET=x64-windows", - "-DCMAKE_TOOLCHAIN_FILE=$VCPKG_DIR/scripts/buildsystems/vcpkg.cmake" + "-DCMAKE_TOOLCHAIN_FILE=$VCPKG_DIR/scripts/buildsystems/vcpkg.cmake", + "-DOPENTELEMETRY_BUILD_DLL=1" ) cmake $SRC_DIR ` $CMAKE_OPTIONS ` "-DCMAKE_INSTALL_PREFIX=$INSTALL_TEST_DIR" ` - -DOPENTELEMETRY_BUILD_DLL=1 ` -DWITH_ABI_VERSION_1=ON ` -DWITH_ABI_VERSION_2=OFF ` -DWITH_THREAD_INSTRUMENTATION_PREVIEW=ON ` diff --git a/examples/batch/CMakeLists.txt b/examples/batch/CMakeLists.txt index 2d88df76bf..765244d14e 100644 --- a/examples/batch/CMakeLists.txt +++ b/examples/batch/CMakeLists.txt @@ -5,3 +5,8 @@ add_executable(batch_span_processor_example main.cc) target_link_libraries( batch_span_processor_example PRIVATE opentelemetry-cpp::ostream_span_exporter opentelemetry-cpp::trace) + +if(BUILD_TESTING) + add_test(NAME examples.batch + COMMAND "$") +endif() diff --git a/examples/common/foo_library/CMakeLists.txt b/examples/common/foo_library/CMakeLists.txt index 58cb179f81..6577866753 100644 --- a/examples/common/foo_library/CMakeLists.txt +++ b/examples/common/foo_library/CMakeLists.txt @@ -3,8 +3,6 @@ add_library(common_foo_library foo_library.h foo_library.cc) -set_target_version(common_foo_library) - if(DEFINED OPENTELEMETRY_BUILD_DLL) target_compile_definitions(common_foo_library PRIVATE OPENTELEMETRY_BUILD_IMPORT_DLL) diff --git a/examples/common/logs_foo_library/CMakeLists.txt b/examples/common/logs_foo_library/CMakeLists.txt index af6207ca96..b1173098fd 100644 --- a/examples/common/logs_foo_library/CMakeLists.txt +++ b/examples/common/logs_foo_library/CMakeLists.txt @@ -3,8 +3,6 @@ add_library(common_logs_foo_library foo_library.h foo_library.cc) -set_target_version(common_logs_foo_library) - if(DEFINED OPENTELEMETRY_BUILD_DLL) target_compile_definitions(common_logs_foo_library PRIVATE OPENTELEMETRY_BUILD_IMPORT_DLL) diff --git a/examples/common/metrics_foo_library/CMakeLists.txt b/examples/common/metrics_foo_library/CMakeLists.txt index 64a4b53aca..54f034afa1 100644 --- a/examples/common/metrics_foo_library/CMakeLists.txt +++ b/examples/common/metrics_foo_library/CMakeLists.txt @@ -3,8 +3,6 @@ add_library(common_metrics_foo_library foo_library.h foo_library.cc) -set_target_version(common_metrics_foo_library) - if(DEFINED OPENTELEMETRY_BUILD_DLL) target_compile_definitions(common_metrics_foo_library PRIVATE OPENTELEMETRY_BUILD_IMPORT_DLL) diff --git a/examples/etw_threads/CMakeLists.txt b/examples/etw_threads/CMakeLists.txt index bcc88350e9..e4eaeecd28 100644 --- a/examples/etw_threads/CMakeLists.txt +++ b/examples/etw_threads/CMakeLists.txt @@ -8,3 +8,7 @@ add_executable(etw_threadpool main.cc) target_link_libraries( etw_threadpool PRIVATE Threads::Threads opentelemetry-cpp::api opentelemetry-cpp::etw_exporter) + +if(BUILD_TESTING) + add_test(NAME examples.etw_threads COMMAND "$") +endif() diff --git a/examples/grpc/CMakeLists.txt b/examples/grpc/CMakeLists.txt index 20d35f3c51..4f87bc3b89 100644 --- a/examples/grpc/CMakeLists.txt +++ b/examples/grpc/CMakeLists.txt @@ -12,19 +12,35 @@ set(example_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/messages.pb.h") set(example_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/messages.grpc.pb.cc") set(example_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/messages.grpc.pb.h") +if(NOT TARGET gRPC::grpc_cpp_plugin) + message( + FATAL_ERROR + "gRPC::grpc_cpp_plugin target not found. Please ensure that gRPC is installed and found with find_package." + ) +endif() + +if(NOT DEFINED PROTOBUF_PROTOC_EXECUTABLE) + if(NOT TARGET protobuf::protoc) + message( + FATAL_ERROR + "protobuf::protoc target not found. Please ensure that Protobuf is installed and found with find_package." + ) + endif() + set(PROTOBUF_PROTOC_EXECUTABLE protobuf::protoc) +endif() + add_custom_command( OUTPUT "${example_proto_srcs}" "${example_proto_hdrs}" "${example_grpc_srcs}" "${example_grpc_hdrs}" COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} ARGS "--grpc_out=${CMAKE_CURRENT_BINARY_DIR}" "--cpp_out=${CMAKE_CURRENT_BINARY_DIR}" "--proto_path=${proto_file_path}" - "--plugin=protoc-gen-grpc=${gRPC_CPP_PLUGIN_EXECUTABLE}" "${proto_file}") + "--plugin=protoc-gen-grpc=$" + "${proto_file}") add_library(example_grpc_proto ${example_grpc_srcs} ${example_grpc_hdrs} ${example_proto_srcs} ${example_proto_hdrs}) -patch_protobuf_targets(example_grpc_proto) - # Disable include-what-you-use on generated code. set_target_properties(example_grpc_proto PROPERTIES CXX_INCLUDE_WHAT_YOU_USE "") @@ -45,5 +61,4 @@ foreach(_target client server) target_link_libraries( ${_target} PRIVATE example_grpc_proto opentelemetry-cpp::ostream_span_exporter) - patch_protobuf_targets(${_target}) endforeach() diff --git a/examples/grpc/client.cc b/examples/grpc/client.cc index ab11d86045..085fd2eb53 100644 --- a/examples/grpc/client.cc +++ b/examples/grpc/client.cc @@ -127,7 +127,7 @@ int main(int argc, char **argv) uint16_t port; if (argc > 1) { - port = atoi(argv[1]); + port = static_cast(atoi(argv[1])); } else { diff --git a/examples/grpc/server.cc b/examples/grpc/server.cc index 1c23b27a0d..c87db8f1b2 100644 --- a/examples/grpc/server.cc +++ b/examples/grpc/server.cc @@ -127,7 +127,7 @@ int main(int argc, char **argv) uint16_t port; if (argc > 1) { - port = atoi(argv[1]); + port = static_cast(atoi(argv[1])); } else { diff --git a/examples/logs_simple/CMakeLists.txt b/examples/logs_simple/CMakeLists.txt index 91d45e0bf9..f0923d6bfc 100644 --- a/examples/logs_simple/CMakeLists.txt +++ b/examples/logs_simple/CMakeLists.txt @@ -16,3 +16,8 @@ else() opentelemetry-cpp::ostream_span_exporter opentelemetry-cpp::ostream_log_record_exporter) endif() + +if(BUILD_TESTING) + add_test(NAME examples.logs_simple + COMMAND "$") +endif() diff --git a/examples/metrics_simple/CMakeLists.txt b/examples/metrics_simple/CMakeLists.txt index 4b649c199d..29fa8cd4bc 100644 --- a/examples/metrics_simple/CMakeLists.txt +++ b/examples/metrics_simple/CMakeLists.txt @@ -16,3 +16,11 @@ else() metrics_ostream_example PRIVATE opentelemetry-cpp::metrics opentelemetry-cpp::ostream_metrics_exporter) endif() + +if(BUILD_TESTING) + add_test(NAME examples.metrics_simple + COMMAND "$") + # Disable the metrics_simple example test due to sporadic segfaults on Windows + # See https://github.com/open-telemetry/opentelemetry-cpp/issues/3458 + set_tests_properties(examples.metrics_simple PROPERTIES DISABLED TRUE) +endif() diff --git a/examples/multi_processor/CMakeLists.txt b/examples/multi_processor/CMakeLists.txt index 537f64abad..7120ec636f 100644 --- a/examples/multi_processor/CMakeLists.txt +++ b/examples/multi_processor/CMakeLists.txt @@ -7,3 +7,8 @@ target_link_libraries( PRIVATE common_foo_library opentelemetry-cpp::trace opentelemetry-cpp::ostream_span_exporter opentelemetry-cpp::in_memory_span_exporter) + +if(BUILD_TESTING) + add_test(NAME examples.multi_processor + COMMAND "$") +endif() diff --git a/examples/multithreaded/CMakeLists.txt b/examples/multithreaded/CMakeLists.txt index 08615756e1..b05a36a3ad 100644 --- a/examples/multithreaded/CMakeLists.txt +++ b/examples/multithreaded/CMakeLists.txt @@ -5,3 +5,8 @@ add_executable(example_multithreaded main.cc) target_link_libraries( example_multithreaded PRIVATE opentelemetry-cpp::trace opentelemetry-cpp::ostream_span_exporter) + +if(BUILD_TESTING) + add_test(NAME examples.multithreaded + COMMAND "$") +endif() diff --git a/examples/otlp/grpc_metric_main.cc b/examples/otlp/grpc_metric_main.cc index 761eabd4f1..d710cf8350 100644 --- a/examples/otlp/grpc_metric_main.cc +++ b/examples/otlp/grpc_metric_main.cc @@ -1,9 +1,6 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include "grpcpp/grpcpp.h" -#include "opentelemetry/exporters/otlp/otlp_grpc_exporter.h" - #include "opentelemetry/exporters/otlp/otlp_grpc_metric_exporter_factory.h" #include "opentelemetry/metrics/provider.h" #include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" @@ -18,6 +15,7 @@ #include "opentelemetry/sdk/metrics/view/meter_selector_factory.h" #include "opentelemetry/sdk/metrics/view/view_factory.h" +#include #include #include diff --git a/examples/plugin/CMakeLists.txt b/examples/plugin/CMakeLists.txt index 5dfc79963c..ef0c25aeca 100644 --- a/examples/plugin/CMakeLists.txt +++ b/examples/plugin/CMakeLists.txt @@ -6,3 +6,12 @@ add_subdirectory(load) if(NOT OPENTELEMETRY_SKIP_DYNAMIC_LOADING_TESTS) add_subdirectory(plugin) endif() + +if(BUILD_TESTING) + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/load/empty_config.txt" "") + add_test( + NAME examples.plugin + COMMAND + "$" "$" + "${CMAKE_CURRENT_BINARY_DIR}/load/empty_config.txt") +endif() diff --git a/examples/simple/CMakeLists.txt b/examples/simple/CMakeLists.txt index b7eefef8a9..cd784febd7 100644 --- a/examples/simple/CMakeLists.txt +++ b/examples/simple/CMakeLists.txt @@ -14,3 +14,7 @@ else() example_simple PRIVATE opentelemetry-cpp::trace opentelemetry-cpp::ostream_span_exporter) endif() + +if(BUILD_TESTING) + add_test(NAME examples.simple COMMAND "$") +endif() diff --git a/ext/src/dll/CMakeLists.txt b/ext/src/dll/CMakeLists.txt index d5c273c83a..628a03abeb 100644 --- a/ext/src/dll/CMakeLists.txt +++ b/ext/src/dll/CMakeLists.txt @@ -16,8 +16,9 @@ set_target_properties(opentelemetry_cpp PROPERTIES EXPORT_NAME opentelemetry_cpp) target_link_libraries( - opentelemetry_cpp PRIVATE opentelemetry_trace - opentelemetry_exporter_ostream_span) + opentelemetry_cpp + PUBLIC opentelemetry_api + PRIVATE opentelemetry_trace opentelemetry_exporter_ostream_span) target_include_directories( opentelemetry_cpp diff --git a/install/test/cmake/CMakeLists.txt b/install/test/cmake/CMakeLists.txt index fc6227894e..b12720e141 100644 --- a/install/test/cmake/CMakeLists.txt +++ b/install/test/cmake/CMakeLists.txt @@ -139,3 +139,25 @@ foreach(component ${INSTALL_TEST_COMPONENTS}) NAME component-${component}-run-test COMMAND ${CMAKE_BINARY_DIR}/build-${component}-test/${component}_test) endforeach() + +set(OPENTELEMETRY_CPP_EXAMPLES_SRC_DIR "${CMAKE_SOURCE_DIR}/../../../examples") + +# Configure the examples with the installed package +add_test( + NAME examples-config-test + COMMAND + ${CMAKE_COMMAND} --log-level=DEBUG -S ${CMAKE_SOURCE_DIR}/examples_test -B + examples-test "-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}" + ${INSTALL_TEST_CMAKE_OPTIONS} + "-DOPENTELEMETRY_CPP_EXAMPLES_SRC_DIR=${OPENTELEMETRY_CPP_EXAMPLES_SRC_DIR}" +) + +# Build the examples with the installed package +add_test(NAME examples-build-test + COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/examples-test + --parallel) + +# Run the examples +add_test(NAME examples-run-test + COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_BINARY_DIR}/examples-test + ${CMAKE_CTEST_COMMAND} --output-on-failure -C $) diff --git a/install/test/cmake/examples_test/CMakeLists.txt b/install/test/cmake/examples_test/CMakeLists.txt new file mode 100644 index 0000000000..3b94e295a6 --- /dev/null +++ b/install/test/cmake/examples_test/CMakeLists.txt @@ -0,0 +1,59 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.14) + +project(opentelemetry-cpp-examples-test LANGUAGES CXX) + +if(NOT DEFINED OPENTELEMETRY_CPP_EXAMPLES_SRC_DIR) + message( + FATAL_ERROR + "OPENTELEMETRY_CPP_SRC_DIR must be defined when running cmake on this test project" + ) +endif() + +find_package(GTest CONFIG REQUIRED) +find_package(opentelemetry-cpp CONFIG REQUIRED) + +if(NOT OPENTELEMETRY_CPP_COMPONENTS_INSTALLED) + message( + FATAL_ERROR + "OPENTELEMETRY_CPP_COMPONENTS_INSTALLED must be set when running cmake on this test project" + ) +endif() + +if("exporters_otlp_file" IN_LIST OPENTELEMETRY_CPP_COMPONENTS_INSTALLED) + set(WITH_OTLP_FILE ON) +endif() + +if("exporters_otlp_grpc" IN_LIST OPENTELEMETRY_CPP_COMPONENTS_INSTALLED) + set(WITH_OTLP_GRPC ON) +endif() + +if("exporters_otlp_http" IN_LIST OPENTELEMETRY_CPP_COMPONENTS_INSTALLED) + set(WITH_OTLP_HTTP ON) +endif() + +if("exporters_prometheus" IN_LIST OPENTELEMETRY_CPP_COMPONENTS_INSTALLED) + set(WITH_PROMETHEUS ON) +endif() + +if("exporters_zipkin" IN_LIST OPENTELEMETRY_CPP_COMPONENTS_INSTALLED) + set(WITH_ZIPKIN ON) +endif() + +if("exporters_etw" IN_LIST OPENTELEMETRY_CPP_COMPONENTS_INSTALLED) + set(WITH_ETW ON) +endif() + +if("ext_http_curl" IN_LIST OPENTELEMETRY_CPP_COMPONENTS_INSTALLED) + set(WITH_EXAMPLES_HTTP ON) +endif() + +set(BUILD_TESTING ON) + +include(CTest) +include(GoogleTest) + +add_subdirectory(${OPENTELEMETRY_CPP_EXAMPLES_SRC_DIR} + ${CMAKE_CURRENT_BINARY_DIR}/examples)