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