1# vim: et ts=4 sts=4 sw=4 tw=0 2 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# 15set(JSONCPP_OLDEST_VALIDATED_POLICIES_VERSION "3.8.0") 16set(JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION "3.13.2") 17cmake_minimum_required(VERSION ${JSONCPP_OLDEST_VALIDATED_POLICIES_VERSION}) 18if("${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}") 21else() 22 set(JSONCPP_CMAKE_POLICY_VERSION "${JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION}") 23endif() 24cmake_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# 29foreach(pnew "") # Currently Empty 30 if(POLICY ${pnew}) 31 cmake_policy(SET ${pnew} NEW) 32 endif() 33endforeach() 34foreach(pold "") # Currently Empty 35 if(POLICY ${pold}) 36 cmake_policy(SET ${pold} OLD) 37 endif() 38endforeach() 39 40# Ensure that CMAKE_BUILD_TYPE has a value specified for single configuration generators. 41if(NOT DEFINED CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES) 42 set(CMAKE_BUILD_TYPE Release CACHE STRING 43 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.") 44endif() 45 46# --------------------------------------------------------------------------- 47# use ccache if found, has to be done before project() 48# --------------------------------------------------------------------------- 49find_program(CCACHE_EXECUTABLE "ccache" HINTS /usr/local/bin /opt/local/bin) 50if(CCACHE_EXECUTABLE) 51 message(STATUS "use ccache") 52 set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_EXECUTABLE}" CACHE PATH "ccache" FORCE) 53 set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_EXECUTABLE}" CACHE PATH "ccache" FORCE) 54endif() 55 56# Note: project(VERSION XX) - the VERSION here is number, but VERSION in meson is string. 57# Thus, it is better to be consistent. 58project(JSONCPP 59 LANGUAGES CXX) 60 61# Set variable named ${VAR_NAME} to value ${VALUE} 62function(set_using_dynamic_name VAR_NAME VALUE) 63 set( "${VAR_NAME}" "${VALUE}" PARENT_SCOPE) 64endfunction() 65 66# Extract major, minor, patch from version text 67# Parse a version string "X.Y.Z" and outputs 68# version parts in ${OUPUT_PREFIX}_MAJOR, _MINOR, _PATCH. 69# If parse succeeds then ${OUPUT_PREFIX}_FOUND is TRUE. 70macro(jsoncpp_parse_version VERSION_TEXT OUPUT_PREFIX) 71 set(VERSION_REGEX "[0-9]+\\.[0-9]+\\.[0-9]+(-[a-zA-Z0-9_]+)?") 72 if( ${VERSION_TEXT} MATCHES ${VERSION_REGEX} ) 73 string(REGEX MATCHALL "[0-9]+|-([A-Za-z0-9_]+)" VERSION_PARTS ${VERSION_TEXT}) 74 list(GET VERSION_PARTS 0 ${OUPUT_PREFIX}_MAJOR) 75 list(GET VERSION_PARTS 1 ${OUPUT_PREFIX}_MINOR) 76 list(GET VERSION_PARTS 2 ${OUPUT_PREFIX}_PATCH) 77 set_using_dynamic_name( "${OUPUT_PREFIX}_FOUND" TRUE ) 78 else( ${VERSION_TEXT} MATCHES ${VERSION_REGEX} ) 79 set_using_dynamic_name( "${OUPUT_PREFIX}_FOUND" FALSE ) 80 endif() 81endmacro() 82 83# Note: version must be updated in three places when doing a release. This 84# annoying process ensures that amalgamate, CMake, and meson all report the 85# correct version. 86# 1. ./meson.build 87# 2. ./include/json/version.h 88# 3. ./CMakeLists.txt 89# IMPORTANT: also update the JSONCPP_SOVERSION!! 90set( JSONCPP_VERSION 00.11.0 ) 91set( JSONCPP_SOVERSION 23 ) 92jsoncpp_parse_version( ${JSONCPP_VERSION} JSONCPP_VERSION ) 93message(STATUS "JsonCpp Version: ${JSONCPP_VERSION_MAJOR}.${JSONCPP_VERSION_MINOR}.${JSONCPP_VERSION_PATCH}") 94#if(NOT JSONCPP_VERSION_FOUND) 95# message(FATAL_ERROR "Failed to parse version string properly. Expect X.Y.Z") 96#endif(NOT JSONCPP_VERSION_FOUND) 97 98option(JSONCPP_WITH_TESTS "Compile and (for jsoncpp_check) run JsonCpp test executables" ON) 99option(JSONCPP_WITH_POST_BUILD_UNITTEST "Automatically run unit-tests as a post build step" ON) 100option(JSONCPP_WITH_WARNING_AS_ERROR "Force compilation to fail if a warning occurs" OFF) 101option(JSONCPP_WITH_STRICT_ISO "Issue all the warnings demanded by strict ISO C and ISO C++" ON) 102option(JSONCPP_WITH_PKGCONFIG_SUPPORT "Generate and install .pc files" ON) 103option(JSONCPP_WITH_CMAKE_PACKAGE "Generate and install cmake package files" ON) 104option(JSONCPP_WITH_EXAMPLE "Compile JsonCpp example" OFF) 105option(BUILD_SHARED_LIBS "Build jsoncpp_lib as a shared library." OFF) 106option(BUILD_WITH_CXX_11 "Build jsoncpp_lib with C++11 standard." ON) 107 108## To compatible with C++0x and C++1x 109set(CMAKE_MINIMUN_CXX_STANDARD 98) 110if(CMAKE_COMPILER_IS_GNUCXX) 111 if(NOT DEFINED CMAKE_CXX_COMPILER) 112 set(CMAKE_CXX_COMPILER "/usr/bin/g++") 113 endif() 114 execute_process(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion OUTPUT_VARIABLE CXX_VERSION) 115 if(CXX_VERSION VERSION_GREATER 4.8.0) 116 if(BUILD_WITH_CXX_11) 117 set(CMAKE_CXX_STANDARD 11) 118 message(STATUS "Compiled with C++11(or newer) standard!") 119 else() 120 set(CMAKE_CXX_STANDARD 98) 121 message(STATUS "Compiled with C++0x standard!") 122 endif() 123 else() 124 set(CMAKE_CXX_STANDARD 98) 125 message(STATUS "Compiled with C++0x standard!") 126 endif() 127endif() 128 129if (NOT CMAKE_CXX_STANDARD) 130 if (BUILD_WITH_CXX_11) 131 set(CMAKE_CXX_STANDARD 11) 132 message(STATUS "Compiled with C++1x standard!") 133 else() 134 set(CMAKE_CXX_STANDARD 98) 135 message(STATUS "Compiled with C++0x standard!") 136 endif() 137endif() 138 139# Adhere to GNU filesystem layout conventions 140include(GNUInstallDirs) 141 142set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" CACHE PATH "Archive output dir.") 143set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" CACHE PATH "Library output dir.") 144set(CMAKE_PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" CACHE PATH "PDB (MSVC debug symbol)output dir.") 145set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" CACHE PATH "Executable/dll output dir.") 146 147set(JSONCPP_USE_SECURE_MEMORY "0" CACHE STRING "-D...=1 to use memory-wiping allocator for STL") 148 149configure_file("${PROJECT_SOURCE_DIR}/version.in" 150 "${PROJECT_BINARY_DIR}/version" 151 NEWLINE_STYLE UNIX) 152 153macro(use_compilation_warning_as_error) 154 if(MSVC) 155 # Only enabled in debug because some old versions of VS STL generate 156 # warnings when compiled in release configuration. 157 add_compile_options($<$<CONFIG:Debug>:/WX>) 158 elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 159 add_compile_options(-Werror) 160 if(JSONCPP_WITH_STRICT_ISO) 161 add_compile_options(-pedantic-errors) 162 endif() 163 endif() 164endmacro() 165 166# Include our configuration header 167include_directories(${jsoncpp_SOURCE_DIR}/include) 168 169if(MSVC) 170 # Only enabled in debug because some old versions of VS STL generate 171 # unreachable code warning when compiled in release configuration. 172 add_compile_options($<$<CONFIG:Debug>:/W4>) 173endif() 174 175if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") 176 # using regular Clang or AppleClang 177 add_compile_options(-Wall -Wconversion -Wshadow -Werror=conversion -Werror=sign-compare) 178elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 179 # using GCC 180 add_compile_options(-Wall -Wconversion -Wshadow -Wextra) 181 # not yet ready for -Wsign-conversion 182 183 if(JSONCPP_WITH_STRICT_ISO) 184 add_compile_options(-Wall) 185 endif() 186 if(JSONCPP_WITH_WARNING_AS_ERROR) 187 add_compile_options(-Werror=conversion) 188 endif() 189elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel") 190 # using Intel compiler 191 add_compile_options(-Wall -Wconversion -Wshadow -Wextra -Werror=conversion) 192 193 if(JSONCPP_WITH_STRICT_ISO AND NOT JSONCPP_WITH_WARNING_AS_ERROR) 194 add_compile_options(-Wpedantic) 195 endif() 196endif() 197 198if(JSONCPP_WITH_WARNING_AS_ERROR) 199 use_compilation_warning_as_error() 200endif() 201 202if(JSONCPP_WITH_PKGCONFIG_SUPPORT) 203 configure_file( 204 "pkg-config/jsoncpp.pc.in" 205 "pkg-config/jsoncpp.pc" 206 @ONLY) 207 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/pkg-config/jsoncpp.pc" 208 DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") 209endif() 210 211if(JSONCPP_WITH_CMAKE_PACKAGE) 212 include(CMakePackageConfigHelpers) 213 install(EXPORT jsoncpp 214 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp 215 FILE jsoncppConfig.cmake) 216 write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake" 217 VERSION ${JSONCPP_VERSION} 218 COMPATIBILITY SameMajorVersion) 219 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake 220 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp) 221endif() 222 223if(JSONCPP_WITH_TESTS) 224 enable_testing() 225 include(CTest) 226endif() 227 228# Build the different applications 229add_subdirectory(src) 230 231#install the includes 232add_subdirectory(include) 233 234#install the example 235if(JSONCPP_WITH_EXAMPLE) 236 add_subdirectory(example) 237endif() 238