CMakeLists.txt 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. cmake_minimum_required(VERSION 3.1)
  2. ##
  3. ## PROJECT
  4. ## name and version
  5. ##
  6. project(nlohmann_json VERSION 3.11.2 LANGUAGES CXX)
  7. ##
  8. ## MAIN_PROJECT CHECK
  9. ## determine if nlohmann_json is built as a subproject (using add_subdirectory) or if it is the main project
  10. ##
  11. set(MAIN_PROJECT OFF)
  12. if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  13. set(MAIN_PROJECT ON)
  14. endif()
  15. ##
  16. ## INCLUDE
  17. ##
  18. ##
  19. set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
  20. include(ExternalProject)
  21. ##
  22. ## OPTIONS
  23. ##
  24. if (POLICY CMP0077)
  25. # Allow CMake 3.13+ to override options when using FetchContent / add_subdirectory.
  26. cmake_policy(SET CMP0077 NEW)
  27. endif ()
  28. # VERSION_GREATER_EQUAL is not available in CMake 3.1
  29. if(${MAIN_PROJECT} AND (${CMAKE_VERSION} VERSION_EQUAL 3.13 OR ${CMAKE_VERSION} VERSION_GREATER 3.13))
  30. set(JSON_BuildTests_INIT ON)
  31. else()
  32. set(JSON_BuildTests_INIT OFF)
  33. endif()
  34. option(JSON_BuildTests "Build the unit tests when BUILD_TESTING is enabled." ${JSON_BuildTests_INIT})
  35. option(JSON_CI "Enable CI build targets." OFF)
  36. option(JSON_Diagnostics "Use extended diagnostic messages." OFF)
  37. option(JSON_GlobalUDLs "Place use-defined string literals in the global namespace." ON)
  38. option(JSON_ImplicitConversions "Enable implicit conversions." ON)
  39. option(JSON_DisableEnumSerialization "Disable default integer enum serialization." OFF)
  40. option(JSON_LegacyDiscardedValueComparison "Enable legacy discarded value comparison." OFF)
  41. option(JSON_Install "Install CMake targets during install step." ${MAIN_PROJECT})
  42. option(JSON_MultipleHeaders "Use non-amalgamated version of the library." ON)
  43. option(JSON_SystemInclude "Include as system headers (skip for clang-tidy)." OFF)
  44. if (JSON_CI)
  45. include(ci)
  46. endif ()
  47. ##
  48. ## CONFIGURATION
  49. ##
  50. include(GNUInstallDirs)
  51. set(NLOHMANN_JSON_TARGET_NAME ${PROJECT_NAME})
  52. set(NLOHMANN_JSON_CONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}" CACHE INTERNAL "")
  53. set(NLOHMANN_JSON_INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}")
  54. set(NLOHMANN_JSON_TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets")
  55. set(NLOHMANN_JSON_CMAKE_CONFIG_TEMPLATE "cmake/config.cmake.in")
  56. set(NLOHMANN_JSON_CMAKE_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}")
  57. set(NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}ConfigVersion.cmake")
  58. set(NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Config.cmake")
  59. set(NLOHMANN_JSON_CMAKE_PROJECT_TARGETS_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Targets.cmake")
  60. set(NLOHMANN_JSON_PKGCONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/pkgconfig")
  61. if (JSON_MultipleHeaders)
  62. set(NLOHMANN_JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/include/")
  63. message(STATUS "Using the multi-header code from ${NLOHMANN_JSON_INCLUDE_BUILD_DIR}")
  64. else()
  65. set(NLOHMANN_JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/single_include/")
  66. message(STATUS "Using the single-header code from ${NLOHMANN_JSON_INCLUDE_BUILD_DIR}")
  67. endif()
  68. if (NOT JSON_ImplicitConversions)
  69. message(STATUS "Implicit conversions are disabled")
  70. endif()
  71. if (JSON_DisableEnumSerialization)
  72. message(STATUS "Enum integer serialization is disabled")
  73. endif()
  74. if (JSON_LegacyDiscardedValueComparison)
  75. message(STATUS "Legacy discarded value comparison enabled")
  76. endif()
  77. if (JSON_Diagnostics)
  78. message(STATUS "Diagnostics enabled")
  79. endif()
  80. if (JSON_SystemInclude)
  81. set(NLOHMANN_JSON_SYSTEM_INCLUDE "SYSTEM")
  82. endif()
  83. ##
  84. ## TARGET
  85. ## create target and add include path
  86. ##
  87. add_library(${NLOHMANN_JSON_TARGET_NAME} INTERFACE)
  88. add_library(${PROJECT_NAME}::${NLOHMANN_JSON_TARGET_NAME} ALIAS ${NLOHMANN_JSON_TARGET_NAME})
  89. if (${CMAKE_VERSION} VERSION_LESS "3.8.0")
  90. target_compile_features(${NLOHMANN_JSON_TARGET_NAME} INTERFACE cxx_range_for)
  91. else()
  92. target_compile_features(${NLOHMANN_JSON_TARGET_NAME} INTERFACE cxx_std_11)
  93. endif()
  94. target_compile_definitions(
  95. ${NLOHMANN_JSON_TARGET_NAME}
  96. INTERFACE
  97. $<$<NOT:$<BOOL:${JSON_GlobalUDLs}>>:JSON_USE_GLOBAL_UDLS=0>
  98. $<$<NOT:$<BOOL:${JSON_ImplicitConversions}>>:JSON_USE_IMPLICIT_CONVERSIONS=0>
  99. $<$<BOOL:${JSON_DisableEnumSerialization}>:JSON_DISABLE_ENUM_SERIALIZATION=1>
  100. $<$<BOOL:${JSON_Diagnostics}>:JSON_DIAGNOSTICS=1>
  101. $<$<BOOL:${JSON_LegacyDiscardedValueComparison}>:JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON=1>
  102. )
  103. target_include_directories(
  104. ${NLOHMANN_JSON_TARGET_NAME}
  105. ${NLOHMANN_JSON_SYSTEM_INCLUDE} INTERFACE
  106. $<BUILD_INTERFACE:${NLOHMANN_JSON_INCLUDE_BUILD_DIR}>
  107. $<INSTALL_INTERFACE:include>
  108. )
  109. ## add debug view definition file for msvc (natvis)
  110. if (MSVC)
  111. set(NLOHMANN_ADD_NATVIS TRUE)
  112. set(NLOHMANN_NATVIS_FILE "nlohmann_json.natvis")
  113. target_sources(
  114. ${NLOHMANN_JSON_TARGET_NAME}
  115. INTERFACE
  116. $<INSTALL_INTERFACE:${NLOHMANN_NATVIS_FILE}>
  117. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${NLOHMANN_NATVIS_FILE}>
  118. )
  119. endif()
  120. # Install a pkg-config file, so other tools can find this.
  121. CONFIGURE_FILE(
  122. "${CMAKE_CURRENT_SOURCE_DIR}/cmake/pkg-config.pc.in"
  123. "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
  124. )
  125. ##
  126. ## TESTS
  127. ## create and configure the unit test target
  128. ##
  129. if (JSON_BuildTests)
  130. include(CTest)
  131. enable_testing()
  132. add_subdirectory(tests)
  133. endif()
  134. ##
  135. ## INSTALL
  136. ## install header files, generate and install cmake config files for find_package()
  137. ##
  138. include(CMakePackageConfigHelpers)
  139. # use a custom package version config file instead of
  140. # write_basic_package_version_file to ensure that it's architecture-independent
  141. # https://github.com/nlohmann/json/issues/1697
  142. configure_file(
  143. "cmake/nlohmann_jsonConfigVersion.cmake.in"
  144. ${NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE}
  145. @ONLY
  146. )
  147. configure_file(
  148. ${NLOHMANN_JSON_CMAKE_CONFIG_TEMPLATE}
  149. ${NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE}
  150. @ONLY
  151. )
  152. if(JSON_Install)
  153. install(
  154. DIRECTORY ${NLOHMANN_JSON_INCLUDE_BUILD_DIR}
  155. DESTINATION ${NLOHMANN_JSON_INCLUDE_INSTALL_DIR}
  156. )
  157. install(
  158. FILES ${NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE} ${NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE}
  159. DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR}
  160. )
  161. if (NLOHMANN_ADD_NATVIS)
  162. install(
  163. FILES ${NLOHMANN_NATVIS_FILE}
  164. DESTINATION .
  165. )
  166. endif()
  167. export(
  168. TARGETS ${NLOHMANN_JSON_TARGET_NAME}
  169. NAMESPACE ${PROJECT_NAME}::
  170. FILE ${NLOHMANN_JSON_CMAKE_PROJECT_TARGETS_FILE}
  171. )
  172. install(
  173. TARGETS ${NLOHMANN_JSON_TARGET_NAME}
  174. EXPORT ${NLOHMANN_JSON_TARGETS_EXPORT_NAME}
  175. INCLUDES DESTINATION ${NLOHMANN_JSON_INCLUDE_INSTALL_DIR}
  176. )
  177. install(
  178. EXPORT ${NLOHMANN_JSON_TARGETS_EXPORT_NAME}
  179. NAMESPACE ${PROJECT_NAME}::
  180. DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR}
  181. )
  182. install(
  183. FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
  184. DESTINATION ${NLOHMANN_JSON_PKGCONFIG_INSTALL_DIR}
  185. )
  186. endif()