Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1 | # vim: et ts=4 sts=4 sw=4 tw=0 |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 2 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 3 | # ==== Define cmake build policies that affect compilation and linkage default behaviors |
| 4 | # |
| 5 | # Set the JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION string to the newest cmake version |
| 6 | # policies that provide successful builds. By setting JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION |
| 7 | # to a value greater than the oldest policies, all policies between |
| 8 | # JSONCPP_OLDEST_VALIDATED_POLICIES_VERSION and CMAKE_VERSION (used for this build) |
| 9 | # are set to their NEW behaivor, thereby suppressing policy warnings related to policies |
| 10 | # between the JSONCPP_OLDEST_VALIDATED_POLICIES_VERSION and CMAKE_VERSION. |
| 11 | # |
| 12 | # CMake versions greater than the JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION policies will |
| 13 | # continue to generate policy warnings "CMake Warning (dev)...Policy CMP0XXX is not set:" |
| 14 | # |
| 15 | set(JSONCPP_OLDEST_VALIDATED_POLICIES_VERSION "3.8.0") |
| 16 | set(JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION "3.13.2") |
| 17 | cmake_minimum_required(VERSION ${JSONCPP_OLDEST_VALIDATED_POLICIES_VERSION}) |
| 18 | if("${CMAKE_VERSION}" VERSION_LESS "${JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION}") |
| 19 | #Set and use the newest available cmake policies that are validated to work |
| 20 | set(JSONCPP_CMAKE_POLICY_VERSION "${CMAKE_VERSION}") |
| 21 | else() |
| 22 | set(JSONCPP_CMAKE_POLICY_VERSION "${JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION}") |
| 23 | endif() |
| 24 | cmake_policy(VERSION ${JSONCPP_CMAKE_POLICY_VERSION}) |
Elliott Hughes | 1601ea0 | 2021-12-07 09:43:38 -0800 | [diff] [blame] | 25 | if(POLICY CMP0091) |
| 26 | cmake_policy(SET CMP0091 NEW) |
| 27 | endif() |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 28 | # |
| 29 | # Now enumerate specific policies newer than JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION |
| 30 | # that may need to be individually set to NEW/OLD |
| 31 | # |
| 32 | foreach(pnew "") # Currently Empty |
| 33 | if(POLICY ${pnew}) |
| 34 | cmake_policy(SET ${pnew} NEW) |
| 35 | endif() |
| 36 | endforeach() |
| 37 | foreach(pold "") # Currently Empty |
| 38 | if(POLICY ${pold}) |
| 39 | cmake_policy(SET ${pold} OLD) |
| 40 | endif() |
| 41 | endforeach() |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 42 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 43 | # Build the library with C++11 standard support, independent from other including |
| 44 | # software which may use a different CXX_STANDARD or CMAKE_CXX_STANDARD. |
| 45 | set(CMAKE_CXX_STANDARD 11) |
| 46 | set(CMAKE_CXX_EXTENSIONS OFF) |
| 47 | set(CMAKE_CXX_STANDARD_REQUIRED ON) |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 48 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 49 | # Ensure that CMAKE_BUILD_TYPE has a value specified for single configuration generators. |
| 50 | if(NOT DEFINED CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES) |
| 51 | set(CMAKE_BUILD_TYPE Release CACHE STRING |
| 52 | "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.") |
| 53 | endif() |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 54 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 55 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake") |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 56 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 57 | # --------------------------------------------------------------------------- |
| 58 | # use ccache if found, has to be done before project() |
| 59 | # --------------------------------------------------------------------------- |
| 60 | find_program(CCACHE_EXECUTABLE "ccache" HINTS /usr/local/bin /opt/local/bin) |
| 61 | if(CCACHE_EXECUTABLE) |
| 62 | message(STATUS "use ccache") |
| 63 | set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_EXECUTABLE}" CACHE PATH "ccache" FORCE) |
| 64 | set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_EXECUTABLE}" CACHE PATH "ccache" FORCE) |
| 65 | endif() |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 66 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 67 | project(jsoncpp |
| 68 | # Note: version must be updated in three places when doing a release. This |
| 69 | # annoying process ensures that amalgamate, CMake, and meson all report the |
| 70 | # correct version. |
| 71 | # 1. ./meson.build |
| 72 | # 2. ./include/json/version.h |
| 73 | # 3. ./CMakeLists.txt |
| 74 | # IMPORTANT: also update the PROJECT_SOVERSION!! |
Elliott Hughes | 1601ea0 | 2021-12-07 09:43:38 -0800 | [diff] [blame] | 75 | VERSION 1.9.5 # <major>[.<minor>[.<patch>[.<tweak>]]] |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 76 | LANGUAGES CXX) |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 77 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 78 | message(STATUS "JsonCpp Version: ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") |
Elliott Hughes | 1601ea0 | 2021-12-07 09:43:38 -0800 | [diff] [blame] | 79 | set(PROJECT_SOVERSION 25) |
| 80 | |
| 81 | include(${CMAKE_CURRENT_SOURCE_DIR}/include/PreventInSourceBuilds.cmake) |
| 82 | include(${CMAKE_CURRENT_SOURCE_DIR}/include/PreventInBuildInstalls.cmake) |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 83 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 84 | option(JSONCPP_WITH_TESTS "Compile and (for jsoncpp_check) run JsonCpp test executables" ON) |
| 85 | option(JSONCPP_WITH_POST_BUILD_UNITTEST "Automatically run unit-tests as a post build step" ON) |
| 86 | option(JSONCPP_WITH_WARNING_AS_ERROR "Force compilation to fail if a warning occurs" OFF) |
| 87 | option(JSONCPP_WITH_STRICT_ISO "Issue all the warnings demanded by strict ISO C and ISO C++" ON) |
| 88 | option(JSONCPP_WITH_PKGCONFIG_SUPPORT "Generate and install .pc files" ON) |
| 89 | option(JSONCPP_WITH_CMAKE_PACKAGE "Generate and install cmake package files" ON) |
| 90 | option(JSONCPP_WITH_EXAMPLE "Compile JsonCpp example" OFF) |
Elliott Hughes | 1601ea0 | 2021-12-07 09:43:38 -0800 | [diff] [blame] | 91 | option(JSONCPP_STATIC_WINDOWS_RUNTIME "Use static (MT/MTd) Windows runtime" OFF) |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 92 | option(BUILD_SHARED_LIBS "Build jsoncpp_lib as a shared library." ON) |
| 93 | option(BUILD_STATIC_LIBS "Build jsoncpp_lib as a static library." ON) |
| 94 | option(BUILD_OBJECT_LIBS "Build jsoncpp_lib as a object library." ON) |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 95 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 96 | # Adhere to GNU filesystem layout conventions |
| 97 | include(GNUInstallDirs) |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 98 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 99 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" CACHE PATH "Archive output dir.") |
| 100 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" CACHE PATH "Library output dir.") |
| 101 | set(CMAKE_PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" CACHE PATH "PDB (MSVC debug symbol)output dir.") |
| 102 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" CACHE PATH "Executable/dll output dir.") |
| 103 | |
| 104 | set(JSONCPP_USE_SECURE_MEMORY "0" CACHE STRING "-D...=1 to use memory-wiping allocator for STL") |
| 105 | |
| 106 | configure_file("${PROJECT_SOURCE_DIR}/version.in" |
| 107 | "${PROJECT_BINARY_DIR}/version" |
| 108 | NEWLINE_STYLE UNIX) |
| 109 | |
| 110 | macro(use_compilation_warning_as_error) |
| 111 | if(MSVC) |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 112 | # Only enabled in debug because some old versions of VS STL generate |
| 113 | # warnings when compiled in release configuration. |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 114 | add_compile_options($<$<CONFIG:Debug>:/WX>) |
| 115 | elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
| 116 | add_compile_options(-Werror) |
| 117 | if(JSONCPP_WITH_STRICT_ISO) |
| 118 | add_compile_options(-pedantic-errors) |
| 119 | endif() |
| 120 | endif() |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 121 | endmacro() |
| 122 | |
| 123 | # Include our configuration header |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 124 | include_directories(${jsoncpp_SOURCE_DIR}/include) |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 125 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 126 | if(MSVC) |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 127 | # Only enabled in debug because some old versions of VS STL generate |
| 128 | # unreachable code warning when compiled in release configuration. |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 129 | add_compile_options($<$<CONFIG:Debug>:/W4>) |
Elliott Hughes | 1601ea0 | 2021-12-07 09:43:38 -0800 | [diff] [blame] | 130 | if (JSONCPP_STATIC_WINDOWS_RUNTIME) |
| 131 | set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") |
| 132 | endif() |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 133 | endif() |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 134 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 135 | if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") |
| 136 | # using regular Clang or AppleClang |
Elliott Hughes | 1601ea0 | 2021-12-07 09:43:38 -0800 | [diff] [blame] | 137 | add_compile_options(-Wall -Wconversion -Wshadow) |
| 138 | |
| 139 | if(JSONCPP_WITH_WARNING_AS_ERROR) |
| 140 | add_compile_options(-Werror=conversion -Werror=sign-compare) |
| 141 | endif() |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 142 | elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
| 143 | # using GCC |
| 144 | add_compile_options(-Wall -Wconversion -Wshadow -Wextra) |
| 145 | # not yet ready for -Wsign-conversion |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 146 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 147 | if(JSONCPP_WITH_STRICT_ISO) |
| 148 | add_compile_options(-Wpedantic) |
| 149 | endif() |
| 150 | if(JSONCPP_WITH_WARNING_AS_ERROR) |
| 151 | add_compile_options(-Werror=conversion) |
| 152 | endif() |
| 153 | elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel") |
| 154 | # using Intel compiler |
Elliott Hughes | 1601ea0 | 2021-12-07 09:43:38 -0800 | [diff] [blame] | 155 | add_compile_options(-Wall -Wconversion -Wshadow -Wextra) |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 156 | |
Elliott Hughes | 1601ea0 | 2021-12-07 09:43:38 -0800 | [diff] [blame] | 157 | if(JSONCPP_WITH_WARNING_AS_ERROR) |
| 158 | add_compile_options(-Werror=conversion) |
| 159 | elseif(JSONCPP_WITH_STRICT_ISO) |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 160 | add_compile_options(-Wpedantic) |
| 161 | endif() |
| 162 | endif() |
| 163 | |
| 164 | if(JSONCPP_WITH_WARNING_AS_ERROR) |
| 165 | use_compilation_warning_as_error() |
| 166 | endif() |
| 167 | |
| 168 | if(JSONCPP_WITH_PKGCONFIG_SUPPORT) |
| 169 | include(JoinPaths) |
| 170 | |
| 171 | join_paths(libdir_for_pc_file "\${exec_prefix}" "${CMAKE_INSTALL_LIBDIR}") |
| 172 | join_paths(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}") |
| 173 | |
| 174 | configure_file( |
| 175 | "pkg-config/jsoncpp.pc.in" |
| 176 | "pkg-config/jsoncpp.pc" |
| 177 | @ONLY) |
| 178 | install(FILES "${CMAKE_CURRENT_BINARY_DIR}/pkg-config/jsoncpp.pc" |
| 179 | DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") |
| 180 | endif() |
| 181 | |
| 182 | if(JSONCPP_WITH_CMAKE_PACKAGE) |
| 183 | include(CMakePackageConfigHelpers) |
| 184 | install(EXPORT jsoncpp |
| 185 | DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp |
Elliott Hughes | 1601ea0 | 2021-12-07 09:43:38 -0800 | [diff] [blame] | 186 | FILE jsoncpp-targets.cmake) |
| 187 | configure_package_config_file(jsoncppConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfig.cmake |
| 188 | INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp) |
| 189 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 190 | write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake" |
| 191 | VERSION ${PROJECT_VERSION} |
| 192 | COMPATIBILITY SameMajorVersion) |
Elliott Hughes | 1601ea0 | 2021-12-07 09:43:38 -0800 | [diff] [blame] | 193 | install(FILES |
| 194 | ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfig.cmake |
| 195 | ${CMAKE_CURRENT_SOURCE_DIR}/jsoncpp-namespaced-targets.cmake |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 196 | DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp) |
| 197 | endif() |
| 198 | |
| 199 | if(JSONCPP_WITH_TESTS) |
| 200 | enable_testing() |
| 201 | include(CTest) |
| 202 | endif() |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 203 | |
| 204 | # Build the different applications |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 205 | add_subdirectory(src) |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 206 | |
| 207 | #install the includes |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 208 | add_subdirectory(include) |
| 209 | |
| 210 | #install the example |
| 211 | if(JSONCPP_WITH_EXAMPLE) |
| 212 | add_subdirectory(example) |
| 213 | endif() |