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