Skip to content

Commit 546fcd7

Browse files
authored
[INSTALL] Unify cmake install functions and dynamically set component dependencies (#3368)
1 parent 1e33bb5 commit 546fcd7

34 files changed

+1016
-924
lines changed

.github/workflows/ci.yml

+22
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,28 @@ jobs:
5757
run: |
5858
./ci/do_ci.sh cmake.test
5959
60+
cmake_fetch_content_test:
61+
name: CMake FetchContent usage with opentelemetry-cpp
62+
runs-on: ubuntu-24.04
63+
env:
64+
CXX_STANDARD: '17'
65+
steps:
66+
- name: Harden the runner (Audit all outbound calls)
67+
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
68+
with:
69+
egress-policy: audit
70+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
71+
with:
72+
submodules: 'recursive'
73+
- name: setup
74+
run: |
75+
sudo -E ./ci/setup_ci_environment.sh
76+
sudo -E ./ci/setup_cmake.sh
77+
sudo -E ./ci/setup_googletest.sh
78+
- name: run fetch content cmake test
79+
run: |
80+
./ci/do_ci.sh cmake.fetch_content.test
81+
6082
cmake_gcc_maintainer_sync_test:
6183
name: CMake gcc 14 (maintainer mode, sync)
6284
runs-on: ubuntu-24.04

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ Increment the:
4242
* [SDK] Aggregate identical metrics instruments and detect duplicates
4343
[#3358](https://github.com/open-telemetry/opentelemetry-cpp/pull/3358)
4444

45+
* [INSTALL] Add CMake components to the opentelemetry-cpp package
46+
[#3320](https://github.com/open-telemetry/opentelemetry-cpp/pull/3220)
47+
[#3368](https://github.com/open-telemetry/opentelemetry-cpp/pull/3368)
48+
4549
## [1.20 2025-04-01]
4650

4751
* [BUILD] Update opentelemetry-proto version

CMakeLists.txt

+10-33
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,8 @@ if(prometheus-cpp_FOUND)
841841
endif()
842842
message(STATUS "---------------------------------------------")
843843

844+
include("${PROJECT_SOURCE_DIR}/cmake/otel-install-functions.cmake")
845+
844846
include(CMakePackageConfigHelpers)
845847

846848
if(DEFINED OPENTELEMETRY_BUILD_DLL)
@@ -914,39 +916,14 @@ include(cmake/opentelemetry-build-external-component.cmake)
914916
include(cmake/patch-imported-config.cmake)
915917

916918
if(OPENTELEMETRY_INSTALL)
917-
# Export cmake config and support find_packages(opentelemetry-cpp CONFIG)
918-
# Write config file for find_packages(opentelemetry-cpp CONFIG)
919-
set(INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}")
920-
configure_package_config_file(
921-
"${CMAKE_CURRENT_LIST_DIR}/cmake/templates/opentelemetry-cpp-config.cmake.in"
922-
"${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME}/${PROJECT_NAME}-config.cmake"
923-
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
924-
PATH_VARS OPENTELEMETRY_ABI_VERSION_NO OPENTELEMETRY_VERSION PROJECT_NAME
925-
INCLUDE_INSTALL_DIR CMAKE_INSTALL_LIBDIR)
926-
927-
# Write version file for find_packages(opentelemetry-cpp CONFIG)
928-
write_basic_package_version_file(
929-
"${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME}/${PROJECT_NAME}-config-version.cmake"
930-
VERSION ${OPENTELEMETRY_VERSION}
931-
COMPATIBILITY ExactVersion)
932-
933-
# Write the "BUILT_WITH_<dependency" flags for use in the
934-
# opentelemetry-cpp-config.cmake
935-
configure_file(
936-
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/templates/thirdparty-built-with-flags.cmake.in"
937-
"${CMAKE_CURRENT_BINARY_DIR}/thirdparty-built-with-flags.cmake"
938-
@ONLY)
939-
940-
install(
941-
FILES
942-
"${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME}/${PROJECT_NAME}-config.cmake"
943-
"${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME}/${PROJECT_NAME}-config-version.cmake"
944-
"${CMAKE_CURRENT_BINARY_DIR}/thirdparty-built-with-flags.cmake"
945-
"${CMAKE_CURRENT_LIST_DIR}/cmake/component-definitions.cmake"
946-
"${CMAKE_CURRENT_LIST_DIR}/cmake/thirdparty-dependency-definitions.cmake"
947-
"${CMAKE_CURRENT_LIST_DIR}/cmake/find-package-support-functions.cmake"
948-
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
949-
COMPONENT cmake-config)
919+
# Install the cmake config and version files
920+
otel_install_cmake_config()
921+
922+
# Install the components and associated files
923+
otel_install_components()
924+
925+
# Install the thirdparty dependency definition file
926+
otel_install_thirdparty_definitions()
950927

951928
if(BUILD_PACKAGE)
952929
include(cmake/package.cmake)

INSTALL.md

+47-13
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,55 @@ If building and installing Protobuf and gRPC manually with cmake the
151151
$
152152
```
153153

154-
### Incorporating into an existing CMake Project
154+
### Incorporating into an external CMake Project
155155

156-
To use the library from a CMake project, you can locate it directly with
157-
`find_package` and use the imported targets from generated package
158-
configurations. As of now, this will import targets for both API and SDK. In
159-
future, there may be separate packages for API and SDK which can be installed
160-
and imported separately according to need.
156+
There are two approaches to incoporate `opentelemetry-cpp` into
157+
an external CMake project:
161158

162-
```cmake
163-
# CMakeLists.txt
164-
find_package(opentelemetry-cpp CONFIG REQUIRED)
165-
...
166-
target_include_directories(foo PRIVATE ${OPENTELEMETRY_CPP_INCLUDE_DIRS})
167-
target_link_libraries(foo PRIVATE ${OPENTELEMETRY_CPP_LIBRARIES})
168-
```
159+
1. Build and install `opentelemetry-cpp` then use `find_package`
160+
to import its targets
161+
162+
```cmake
163+
# Find all installed components and link all imported targets
164+
find_package(opentelemetry-cpp CONFIG REQUIRED)
165+
...
166+
target_include_directories(foo PRIVATE ${OPENTELEMETRY_CPP_INCLUDE_DIRS})
167+
target_link_libraries(foo PRIVATE ${OPENTELEMETRY_CPP_LIBRARIES})
168+
```
169+
170+
```cmake
171+
# Find a specific component and link its imported target(s)
172+
find_package(opentelemetry-cpp CONFIG REQUIRED COMPONENTS api)
173+
...
174+
target_link_libraries(foo PRIVATE opentelemetry-cpp::api)
175+
```
176+
177+
2. Use CMake's [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html)
178+
module to fetch and build `opentelemetry-cpp` then make its targets available
179+
180+
```cmake
181+
# Fetch from an existing clone and build
182+
include(FetchContent)
183+
FetchContent_Declare(opentelemetry-cpp SOURCE_DIR "<path/to/opentelemetry-cpp>")
184+
FetchContent_MakeAvailable(opentelemetry-cpp)
185+
...
186+
target_link_libraries(foo PRIVATE opentelemetry-cpp::api)
187+
```
188+
189+
```cmake
190+
# Clone and build opentelemetry-cpp from a git tag
191+
include(FetchContent)
192+
FetchContent_Declare(
193+
opentelemetry-cpp
194+
GIT_REPOSITORY https://github.com/open-telemetry/opentelemetry-cpp.git
195+
GIT_TAG v1.20.0)
196+
FetchContent_MakeAvailable(opentelemetry-cpp)
197+
...
198+
target_link_libraries(foo PRIVATE opentelemetry-cpp::api)
199+
```
200+
201+
In both cases the project's built or imported CMake targets will be
202+
available in the `opentelemetry-cpp` namespace (ie: `opentelemetry-cpp::api`)
169203

170204
#### Using opentelemetry-cpp package components
171205

api/CMakeLists.txt

+13-21
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,20 @@ target_include_directories(
99

1010
set_target_properties(opentelemetry_api PROPERTIES EXPORT_NAME api)
1111

12-
if(OPENTELEMETRY_INSTALL)
13-
install(
14-
TARGETS opentelemetry_api
15-
EXPORT "${PROJECT_NAME}-api-target"
16-
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
17-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
18-
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT api)
19-
20-
install(
21-
DIRECTORY include/opentelemetry
22-
DESTINATION include
23-
COMPONENT api
24-
FILES_MATCHING
25-
PATTERN "*.h")
26-
27-
install(
28-
EXPORT "${PROJECT_NAME}-api-target"
29-
FILE "${PROJECT_NAME}-api-target.cmake"
30-
NAMESPACE "${PROJECT_NAME}::"
31-
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
32-
COMPONENT api)
12+
otel_add_component(
13+
COMPONENT
14+
api
15+
TARGETS
16+
opentelemetry_api
17+
FILES_DIRECTORY
18+
"include/opentelemetry"
19+
FILES_DESTINATION
20+
"include"
21+
FILES_MATCHING
22+
PATTERN
23+
"*.h")
3324

25+
if(OPENTELEMETRY_INSTALL)
3426
unset(TARGET_DEPS)
3527
endif()
3628

ci/do_ci.sh

+39
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,45 @@ elif [[ "$1" == "cmake.install.test" ]]; then
514514
-S "${SRC_DIR}/install/test/cmake"
515515
ctest --output-on-failure
516516
exit 0
517+
elif [[ "$1" == "cmake.fetch_content.test" ]]; then
518+
if [[ -n "${BUILD_SHARED_LIBS}" && "${BUILD_SHARED_LIBS}" == "ON" ]]; then
519+
CMAKE_OPTIONS+=("-DBUILD_SHARED_LIBS=ON")
520+
echo "BUILD_SHARED_LIBS is set to: ON"
521+
else
522+
CMAKE_OPTIONS+=("-DBUILD_SHARED_LIBS=OFF")
523+
echo "BUILD_SHARED_LIBS is set to: OFF"
524+
fi
525+
CMAKE_OPTIONS+=("-DCMAKE_POSITION_INDEPENDENT_CODE=ON")
526+
527+
cd "${BUILD_DIR}"
528+
rm -rf *
529+
cmake "${CMAKE_OPTIONS[@]}" \
530+
-DCMAKE_INSTALL_PREFIX=${INSTALL_TEST_DIR} \
531+
-DWITH_ABI_VERSION_1=OFF \
532+
-DWITH_ABI_VERSION_2=ON \
533+
-DWITH_METRICS_EXEMPLAR_PREVIEW=ON \
534+
-DWITH_ASYNC_EXPORT_PREVIEW=ON \
535+
-DWITH_THREAD_INSTRUMENTATION_PREVIEW=ON \
536+
-DWITH_OTLP_GRPC_SSL_MTLS_PREVIEW=ON \
537+
-DWITH_OTLP_RETRY_PREVIEW=ON \
538+
-DWITH_OTLP_GRPC=OFF \
539+
-DWITH_OTLP_HTTP=OFF \
540+
-DWITH_OTLP_FILE=OFF \
541+
-DWITH_OTLP_HTTP_COMPRESSION=OFF \
542+
-DWITH_HTTP_CLIENT_CURL=OFF \
543+
-DWITH_PROMETHEUS=OFF \
544+
-DWITH_ZIPKIN=OFF \
545+
-DWITH_ELASTICSEARCH=OFF \
546+
-DWITH_EXAMPLES=OFF \
547+
-DWITH_EXAMPLES_HTTP=OFF \
548+
-DBUILD_W3CTRACECONTEXT_TEST=OFF \
549+
-DOPENTELEMETRY_INSTALL=OFF \
550+
-DOPENTELEMETRY_CPP_SRC_DIR="${SRC_DIR}" \
551+
"${SRC_DIR}/install/test/cmake/fetch_content_test"
552+
make -j $(nproc)
553+
make test
554+
exit 0
555+
517556
elif [[ "$1" == "cmake.test_example_plugin" ]]; then
518557
# Build the plugin
519558
cd "${BUILD_DIR}"

0 commit comments

Comments
 (0)