Skip to content

Commit fa231c7

Browse files
committed
Create a cmake script for each third party dependency. Support FetchContent and find_package with the otel_add_thirdparty_package function. Remove the static third party dependency file. Move the version parsing to a version script and set the project version in the main command. Clean up cmake target usage.
1 parent b1253a7 commit fa231c7

29 files changed

+1025
-896
lines changed

CMakeLists.txt

Lines changed: 83 additions & 503 deletions
Large diffs are not rendered by default.

api/CMakeLists.txt

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,7 @@ target_include_directories(
99

1010
set_target_properties(opentelemetry_api PROPERTIES EXPORT_NAME api)
1111

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")
24-
25-
if(OPENTELEMETRY_INSTALL)
26-
unset(TARGET_DEPS)
27-
endif()
12+
unset(TARGET_DEPS)
2813

2914
if(BUILD_TESTING)
3015
add_subdirectory(test)
@@ -74,18 +59,8 @@ endif()
7459

7560
if(WITH_GSL)
7661
target_compile_definitions(opentelemetry_api INTERFACE HAVE_GSL)
77-
78-
# Guidelines Support Library path. Used if we are not on not get C++20.
79-
#
80-
find_package(Microsoft.GSL QUIET)
81-
if(TARGET Microsoft.GSL::GSL)
82-
target_link_libraries(opentelemetry_api INTERFACE Microsoft.GSL::GSL)
83-
list(APPEND TARGET_DEPS "gsl")
84-
else()
85-
set(GSL_DIR third_party/ms-gsl)
86-
target_include_directories(
87-
opentelemetry_api INTERFACE "$<BUILD_INTERFACE:${GSL_DIR}/include>")
88-
endif()
62+
target_link_libraries(opentelemetry_api INTERFACE Microsoft.GSL::GSL)
63+
list(APPEND TARGET_DEPS "gsl")
8964
endif()
9065

9166
if(WITH_NO_GETENV)
@@ -140,6 +115,19 @@ if(APPLE)
140115
target_link_libraries(opentelemetry_api INTERFACE "-framework CoreFoundation")
141116
endif()
142117

118+
otel_add_component(
119+
COMPONENT
120+
api
121+
TARGETS
122+
opentelemetry_api
123+
FILES_DIRECTORY
124+
"include/opentelemetry"
125+
FILES_DESTINATION
126+
"include"
127+
FILES_MATCHING
128+
PATTERN
129+
"*.h")
130+
143131
include(${PROJECT_SOURCE_DIR}/cmake/pkgconfig.cmake)
144132

145133
if(OPENTELEMETRY_INSTALL)

cmake/benchmark.cmake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
otel_add_thirdparty_package(
5+
PACKAGE_NAME "benchmark"
6+
PACKAGE_SEARCH_MODES "CONFIG"
7+
FETCH_GIT_REPOSITORY "https://github.com/google/benchmark.git"
8+
FETCH_GIT_TAG ${benchmark_GIT_TAG}
9+
FETCH_SOURCE_DIR "${PROJECT_SOURCE_DIR}/third_party/benchmark"
10+
FETCH_CMAKE_ARGS
11+
"-DBENCHMARK_ENABLE_TESTING=OFF"
12+
"-DBENCHMARK_ENABLE_INSTALL=${OPENTELEMETRY_INSTALL}"
13+
REQUIRED_TARGETS "benchmark::benchmark"
14+
VERSION_REGEX "project.*\\([^\\)]*VERSION[ \t]*([0-9]+(\\.[0-9]+)*(\\.[0-9]+)*)"
15+
VERSION_FILE "\${benchmark_SOURCE_DIR}/CMakeLists.txt"
16+
)

cmake/curl.cmake

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
if(OPENTELEMETRY_INSTALL)
5+
set(_CURL_DISABLE_INSTALL OFF)
6+
else()
7+
set(_CURL_DISABLE_INSTALL ON)
8+
endif()
9+
10+
otel_add_thirdparty_package(
11+
PACKAGE_NAME "CURL"
12+
PACKAGE_SEARCH_MODES "CONFIG" "MODULE"
13+
FETCH_NAME "curl"
14+
FETCH_GIT_REPOSITORY "https://github.com/curl/curl.git"
15+
FETCH_GIT_TAG "${curl_GIT_TAG}"
16+
FETCH_CMAKE_ARGS
17+
"-DCURL_DISABLE_INSTALL=${_CURL_DISABLE_INSTALL}"
18+
"-DCURL_USE_LIBPSL=OFF"
19+
"-DBUILD_CURL_EXE=OFF"
20+
"-DBUILD_LIBCURL_DOCS=OFF"
21+
"-DBUILD_MISC_DOCS=OFF"
22+
"-DENABLE_CURL_MANUAL=OFF"
23+
"-DBUILD_SHARED_LIBS=ON"
24+
VERSION_REGEX "#define[ \t]+LIBCURL_VERSION[ \t]+\"([0-9]+\\.[0-9]+\\.[0-9]+(-[A-Za-z0-9]+)?)\""
25+
VERSION_FILE "\${curl_SOURCE_DIR}/include/curl/curlver.h"
26+
)
27+
28+
if(NOT TARGET CURL::libcurl)
29+
if(TARGET libcurl_shared)
30+
add_library(CURL::libcurl ALIAS libcurl_shared)
31+
elseif(TARGET libcurl_static)
32+
add_library(CURL::libcurl ALIAS libcurl_static)
33+
endif()
34+
endif()
35+
36+
if(NOT TARGET CURL::libcurl)
37+
message(FATAL_ERROR "CURL::libcurl imported target not found.")
38+
endif()

cmake/googletest.cmake

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
otel_add_thirdparty_package(
5+
PACKAGE_NAME "GTest"
6+
PACKAGE_SEARCH_MODES "CONFIG"
7+
FETCH_NAME googletest
8+
FETCH_GIT_REPOSITORY "https://github.com/google/googletest.git"
9+
FETCH_GIT_TAG "${googletest_GIT_TAG}"
10+
FETCH_SOURCE_DIR "${PROJECT_SOURCE_DIR}/third_party/googletest"
11+
FETCH_CMAKE_ARGS
12+
"-DINSTALL_GTEST=${OPENTELEMETRY_INSTALL}"
13+
"-DBUILD_GMOCK=ON"
14+
REQUIRED_TARGETS "GTest::gtest;GTest::gtest_main;GTest::gmock"
15+
VERSION_REGEX "set\\s*\\(\\s*GOOGLETEST_VERSION[ \t]+([0-9]+(\\.[0-9]+)*)([ \t]|\\))"
16+
VERSION_FILE "\${googletest_SOURCE_DIR}/CMakeLists.txt"
17+
)
18+
19+
if(NOT GTEST_BOTH_LIBRARIES)
20+
set(GTEST_BOTH_LIBRARIES GTest::gtest GTest::gtest_main)
21+
endif()
22+
23+
if(NOT GMOCK_LIB)
24+
set(GMOCK_LIB GTest::gmock)
25+
endif()

cmake/grpc.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+
# Including the CMakeFindDependencyMacro resolves an error from
5+
# gRPCConfig.cmake on some grpc versions. See
6+
# https://github.com/grpc/grpc/pull/33361 for more details.
7+
include(CMakeFindDependencyMacro)
8+
9+
otel_add_thirdparty_package(
10+
PACKAGE_NAME "gRPC"
11+
PACKAGE_SEARCH_MODES "CONFIG"
12+
FETCH_NAME "grpc"
13+
FETCH_GIT_REPOSITORY "https://github.com/grpc/grpc.git"
14+
FETCH_GIT_TAG "${grpc_GIT_TAG}"
15+
FETCH_GIT_SUBMODULES
16+
"third_party/re2"
17+
"third_party/abseil-cpp"
18+
"third_party/protobuf"
19+
"third_party/cares/cares"
20+
"third_party/boringssl-with-bazel"
21+
FETCH_CMAKE_ARGS
22+
"-DgRPC_INSTALL=${OPENTELEMETRY_INSTALL}"
23+
"-DgRPC_BUILD_TESTS=OFF"
24+
"-DgRPC_BUILD_GRPC_CPP_PLUGIN=ON"
25+
"-DgRPC_BUILD_GRPC_CSHARP_PLUGIN=OFF"
26+
"-DgRPC_BUILD_GRPC_OBJECTIVE_C_PLUGIN=OFF"
27+
"-DgRPC_BUILD_GRPC_PHP_PLUGIN=OFF"
28+
"-DgRPC_BUILD_GRPC_NODE_PLUGIN=OFF"
29+
"-DgRPC_BUILD_GRPC_PYTHON_PLUGIN=OFF"
30+
"-DgRPC_BUILD_GRPC_RUBY_PLUGIN=OFF"
31+
"-DgRPC_BUILD_GRPCPP_OTEL_PLUGIN=OFF"
32+
"-DRE2_BUILD_TESTING=OFF"
33+
"-DgRPC_ZLIB_PROVIDER=package"
34+
"-DgRPC_RE2_PROVIDER=module"
35+
"-DgRPC_PROTOBUF_PROVIDER=module"
36+
"-DgRPC_ABSL_PROVIDER=module"
37+
"-DgRPC_CARES_PROVIDER=module"
38+
VERSION_REGEX "#define[ \t]+GRPC_CPP_VERSION_STRING[ \t]+\"([0-9]+\\.[0-9]+\\.[0-9]+(-[A-Za-z0-9]+)?)\""
39+
VERSION_FILE "\${grpc_SOURCE_DIR}/include/grpcpp/version_info.h"
40+
)
41+
42+
if(TARGET grpc++)
43+
set_target_properties(grpc++ PROPERTIES POSITION_INDEPENDENT_CODE ON
44+
CXX_INCLUDE_WHAT_YOU_USE "" CXX_CLANG_TIDY "")
45+
endif()
46+
47+
if(TARGET grpc++ AND NOT TARGET gRPC::grpc++)
48+
add_library(gRPC::grpc++ ALIAS grpc++)
49+
endif()
50+
51+
if(TARGET grpc_cpp_plugin AND NOT TARGET gRPC::grpc_cpp_plugin)
52+
add_executable(gRPC::grpc_cpp_plugin ALIAS grpc_cpp_plugin)
53+
endif()
54+
55+
if(NOT TARGET gRPC::grpc++)
56+
message(FATAL_ERROR "A required gRPC target (grpc++) was not found")
57+
endif()
58+
59+
if(CMAKE_CROSSCOMPILING)
60+
find_program(gRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
61+
else()
62+
if(NOT TARGET gRPC::grpc_cpp_plugin)
63+
message(FATAL_ERROR "A required gRPC target (grpc_cpp_plugin) was not found")
64+
endif()
65+
set(gRPC_CPP_PLUGIN_EXECUTABLE "$<TARGET_FILE:gRPC::grpc_cpp_plugin>")
66+
endif()
67+
68+
message(STATUS "gRPC_CPP_PLUGIN_EXECUTABLE=${gRPC_CPP_PLUGIN_EXECUTABLE}")

cmake/ms-gsl.cmake

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
otel_add_thirdparty_package(
5+
PACKAGE_NAME "Microsoft.GSL"
6+
PACKAGE_SEARCH_MODES "CONFIG"
7+
FETCH_NAME "gsl"
8+
FETCH_GIT_REPOSITORY "https://github.com/microsoft/GSL.git"
9+
FETCH_GIT_TAG "${ms-gsl_GIT_TAG}"
10+
FETCH_SOURCE_DIR "${PROJECT_SOURCE_DIR}/third_party/ms-gsl"
11+
FETCH_CMAKE_ARGS
12+
"-DGSL_TEST=OFF"
13+
"-DGSL_INSTALL=${OPENTELEMETRY_INSTALL}"
14+
REQUIRED_TARGETS "Microsoft.GSL::GSL"
15+
VERSION_REGEX "project\\([^\\)]*VERSION[ \t]*([0-9]+(\\.[0-9]+)*(\\.[0-9]+)*)"
16+
VERSION_FILE "\${GSL_SOURCE_DIR}/CMakeLists.txt"
17+
)

cmake/nlohmann-json.cmake

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

4-
#
5-
# The dependency on nlohmann_json can be provided different ways. By order of
6-
# decreasing priority, options are:
7-
#
8-
# 1 - Search for a nlohmann_json package
9-
#
10-
# Packages installed on the local machine are used if found.
11-
#
12-
# The nlohmann_json dependency is not installed, as it already is.
13-
#
14-
# 2 - Search for a nlohmann_json git submodule
15-
#
16-
# When git submodule is used, the nlohmann_json code is located in:
17-
# third_party/nlohmann-json
18-
#
19-
# The nlohmann_json dependency is installed, by building the sub directory with
20-
# JSON_Install=ON
21-
#
22-
# 3 - Download nlohmann_json from github
23-
#
24-
# Code from the development branch is used, unless a specific release tag is
25-
# provided in variable ${nlohmann-json}
26-
#
27-
# The nlohmann_json dependency is installed, by building the downloaded code
28-
# with JSON_Install=ON
29-
#
30-
31-
# nlohmann_json package is required for most SDK build configurations
32-
find_package(nlohmann_json QUIET)
33-
set(nlohmann_json_clone FALSE)
34-
if(nlohmann_json_FOUND)
35-
message(STATUS "nlohmann::json dependency satisfied by: package")
36-
elseif(TARGET nlohmann_json)
37-
message(STATUS "nlohmann::json is already added as a CMake target!")
38-
elseif(EXISTS ${PROJECT_SOURCE_DIR}/.git
39-
AND EXISTS
40-
${PROJECT_SOURCE_DIR}/third_party/nlohmann-json/CMakeLists.txt)
41-
message(STATUS "nlohmann::json dependency satisfied by: git submodule")
42-
set(JSON_BuildTests
43-
OFF
44-
CACHE INTERNAL "")
45-
set(JSON_Install
46-
ON
47-
CACHE INTERNAL "")
48-
# This option allows to link nlohmann_json::nlohmann_json target
49-
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/nlohmann-json)
50-
# This option allows to add header to include directories
51-
include_directories(
52-
${PROJECT_SOURCE_DIR}/third_party/nlohmann-json/single_include)
53-
else()
54-
if("${nlohmann-json}" STREQUAL "")
55-
set(nlohmann-json "develop")
56-
endif()
57-
message(STATUS "nlohmann::json dependency satisfied by: github download")
58-
set(nlohmann_json_clone TRUE)
59-
include(ExternalProject)
60-
ExternalProject_Add(
61-
nlohmann_json_download
62-
PREFIX third_party
63-
GIT_REPOSITORY https://github.com/nlohmann/json.git
64-
GIT_TAG "${nlohmann-json}"
65-
UPDATE_COMMAND ""
66-
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
67-
-DJSON_BuildTests=OFF -DJSON_Install=ON
68-
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
69-
TEST_AFTER_INSTALL 0
70-
DOWNLOAD_NO_PROGRESS 1
71-
LOG_CONFIGURE 1
72-
LOG_BUILD 1
73-
LOG_INSTALL 1)
74-
75-
ExternalProject_Get_Property(nlohmann_json_download INSTALL_DIR)
76-
set(NLOHMANN_JSON_INCLUDE_DIR
77-
${INSTALL_DIR}/src/nlohmann_json_download/single_include)
78-
add_library(nlohmann_json_ INTERFACE)
79-
target_include_directories(
80-
nlohmann_json_ INTERFACE "$<BUILD_INTERFACE:${NLOHMANN_JSON_INCLUDE_DIR}>"
81-
"$<INSTALL_INTERFACE:include>")
82-
add_dependencies(nlohmann_json_ nlohmann_json_download)
83-
add_library(nlohmann_json::nlohmann_json ALIAS nlohmann_json_)
84-
85-
if(OPENTELEMETRY_INSTALL)
86-
install(
87-
TARGETS nlohmann_json_
88-
EXPORT "${PROJECT_NAME}-third_party_nlohmann_json_target"
89-
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
90-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
91-
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
92-
COMPONENT third_party_nlohmann_json)
93-
endif()
94-
endif()
4+
otel_add_thirdparty_package(
5+
PACKAGE_NAME "nlohmann_json"
6+
PACKAGE_SEARCH_MODES "CONFIG"
7+
FETCH_GIT_REPOSITORY "https://github.com/nlohmann/json.git"
8+
FETCH_GIT_TAG "${nlohmann-json_GIT_TAG}"
9+
FETCH_SOURCE_DIR "${PROJECT_SOURCE_DIR}/third_party/nlohmann-json"
10+
FETCH_CMAKE_ARGS
11+
"-DJSON_BuildTests=OFF"
12+
"-DJSON_Install=${OPENTELEMETRY_INSTALL}"
13+
"-DJSON_MultipleHeaders=OFF"
14+
REQUIRED_TARGETS "nlohmann_json::nlohmann_json"
15+
VERSION_REGEX "project\\([^\\)]*VERSION[ \t]*([0-9]+(\\.[0-9]+)*(\\.[0-9]+)*)"
16+
VERSION_FILE "\${nlohmann_json_SOURCE_DIR}/CMakeLists.txt"
17+
)

0 commit comments

Comments
 (0)