Skip to content

Commit 65c22e9

Browse files
authored
Cleanup CMake makefiles for nlohmann_json. (open-telemetry#1912)
1 parent 8a9d085 commit 65c22e9

File tree

3 files changed

+102
-65
lines changed

3 files changed

+102
-65
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Increment the:
2626
[1871](https://github.com/open-telemetry/opentelemetry-cpp/pull/1871)
2727
* [BUILD] Migrate from @bazel_tools//platforms to [Bazel Platforms](https://github.com/bazelbuild/platforms)
2828
to enable Bazel 6.0.0 compatibility [#1873](https://github.com/open-telemetry/opentelemetry-cpp/pull/1873)
29+
* [BUILD] Cleanup CMake makefiles for nlohmann_json
30+
[#1912](https://github.com/open-telemetry/opentelemetry-cpp/pull/1912)
2931

3032
## [1.8.1] 2022-12-04
3133

CMakeLists.txt

+2-25
Original file line numberDiff line numberDiff line change
@@ -340,32 +340,9 @@ if(WITH_ELASTICSEARCH
340340
else()
341341
set(USE_NLOHMANN_JSON OFF)
342342
endif()
343+
343344
if((NOT WITH_API_ONLY) AND USE_NLOHMANN_JSON)
344-
# nlohmann_json package is required for most SDK build configurations
345-
find_package(nlohmann_json QUIET)
346-
set(nlohmann_json_clone FALSE)
347-
if(nlohmann_json_FOUND)
348-
message("Using external nlohmann::json")
349-
elseif(EXISTS ${PROJECT_SOURCE_DIR}/.git
350-
AND EXISTS
351-
${PROJECT_SOURCE_DIR}/third_party/nlohmann-json/CMakeLists.txt)
352-
message("Trying to use local nlohmann::json from submodule")
353-
set(JSON_BuildTests
354-
OFF
355-
CACHE INTERNAL "")
356-
set(JSON_Install
357-
ON
358-
CACHE INTERNAL "")
359-
# This option allows to link nlohmann_json::nlohmann_json target
360-
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/nlohmann-json)
361-
# This option allows to add header to include directories
362-
include_directories(
363-
${PROJECT_SOURCE_DIR}/third_party/nlohmann-json/single_include)
364-
else()
365-
set(nlohmann_json_clone TRUE)
366-
include(cmake/nlohmann-json.cmake)
367-
message("\nnlohmann_json package was not found. Cloning from github")
368-
endif()
345+
include(cmake/nlohmann-json.cmake)
369346
endif()
370347

371348
if(OTELCPP_MAINTAINER_MODE)

cmake/nlohmann-json.cmake

+98-40
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,100 @@
1-
if("${nlohmann-json}" STREQUAL "")
2-
set(nlohmann-json "develop")
3-
endif()
4-
include(ExternalProject)
5-
ExternalProject_Add(nlohmann_json_download
6-
PREFIX third_party
7-
GIT_REPOSITORY https://github.com/nlohmann/json.git
8-
GIT_TAG
9-
"${nlohmann-json}"
10-
UPDATE_COMMAND ""
11-
CMAKE_ARGS
12-
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
13-
-DJSON_BuildTests=OFF
14-
-DJSON_Install=ON
15-
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
16-
TEST_AFTER_INSTALL
17-
0
18-
DOWNLOAD_NO_PROGRESS
19-
1
20-
LOG_CONFIGURE
21-
1
22-
LOG_BUILD
23-
1
24-
LOG_INSTALL
25-
1
26-
)
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
#
5+
# The dependency on nlohmann_json can be provided different ways.
6+
# By order of 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,
13+
# as it already is.
14+
#
15+
# 2 - Search for a nlohmann_json git submodule
16+
#
17+
# When git submodule is used,
18+
# the nlohmann_json code is located in:
19+
# third_party/nlohmann-json
20+
#
21+
# The nlohmann_json dependency is installed,
22+
# by building the sub directory with JSON_Install=ON
23+
#
24+
# 3 - Download nlohmann_json from github
25+
#
26+
# Code from the development branch is used,
27+
# unless a specific release tag is provided
28+
# in variable ${nlohmann-json}
29+
#
30+
# The nlohmann_json dependency is installed,
31+
# by building the downloaded code with JSON_Install=ON
32+
#
33+
2734

28-
ExternalProject_Get_Property(nlohmann_json_download INSTALL_DIR)
29-
SET(NLOHMANN_JSON_INCLUDE_DIR ${INSTALL_DIR}/src/nlohmann_json_download/single_include)
30-
add_library(nlohmann_json_ INTERFACE)
31-
target_include_directories(nlohmann_json_ INTERFACE
32-
"$<BUILD_INTERFACE:${NLOHMANN_JSON_INCLUDE_DIR}>"
33-
"$<INSTALL_INTERFACE:include>")
34-
add_dependencies(nlohmann_json_ nlohmann_json_download)
35-
add_library(nlohmann_json::nlohmann_json ALIAS nlohmann_json_)
35+
# nlohmann_json package is required for most SDK build configurations
36+
find_package(nlohmann_json QUIET)
37+
set(nlohmann_json_clone FALSE)
38+
if(nlohmann_json_FOUND)
39+
message(STATUS "nlohmann::json dependency satisfied by: package")
40+
elseif(EXISTS ${PROJECT_SOURCE_DIR}/.git
41+
AND EXISTS
42+
${PROJECT_SOURCE_DIR}/third_party/nlohmann-json/CMakeLists.txt)
43+
message(STATUS "nlohmann::json dependency satisfied by: git submodule")
44+
set(JSON_BuildTests
45+
OFF
46+
CACHE INTERNAL "")
47+
set(JSON_Install
48+
ON
49+
CACHE INTERNAL "")
50+
# This option allows to link nlohmann_json::nlohmann_json target
51+
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/nlohmann-json)
52+
# This option allows to add header to include directories
53+
include_directories(
54+
${PROJECT_SOURCE_DIR}/third_party/nlohmann-json/single_include)
55+
else()
56+
if("${nlohmann-json}" STREQUAL "")
57+
set(nlohmann-json "develop")
58+
endif()
59+
message(STATUS "nlohmann::json dependency satisfied by: github download")
60+
set(nlohmann_json_clone TRUE)
61+
include(ExternalProject)
62+
ExternalProject_Add(nlohmann_json_download
63+
PREFIX third_party
64+
GIT_REPOSITORY https://github.com/nlohmann/json.git
65+
GIT_TAG
66+
"${nlohmann-json}"
67+
UPDATE_COMMAND ""
68+
CMAKE_ARGS
69+
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
70+
-DJSON_BuildTests=OFF
71+
-DJSON_Install=ON
72+
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
73+
TEST_AFTER_INSTALL
74+
0
75+
DOWNLOAD_NO_PROGRESS
76+
1
77+
LOG_CONFIGURE
78+
1
79+
LOG_BUILD
80+
1
81+
LOG_INSTALL
82+
1
83+
)
3684

37-
install(
38-
TARGETS nlohmann_json_
39-
EXPORT "${PROJECT_NAME}-target"
40-
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
41-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
42-
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
85+
ExternalProject_Get_Property(nlohmann_json_download INSTALL_DIR)
86+
SET(NLOHMANN_JSON_INCLUDE_DIR ${INSTALL_DIR}/src/nlohmann_json_download/single_include)
87+
add_library(nlohmann_json_ INTERFACE)
88+
target_include_directories(nlohmann_json_ INTERFACE
89+
"$<BUILD_INTERFACE:${NLOHMANN_JSON_INCLUDE_DIR}>"
90+
"$<INSTALL_INTERFACE:include>")
91+
add_dependencies(nlohmann_json_ nlohmann_json_download)
92+
add_library(nlohmann_json::nlohmann_json ALIAS nlohmann_json_)
93+
94+
install(
95+
TARGETS nlohmann_json_
96+
EXPORT "${PROJECT_NAME}-target"
97+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
98+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
99+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
100+
endif()

0 commit comments

Comments
 (0)