2
0

nlohmann-json.cmake 3.3 KB

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