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