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