CMakeLists.txt 8.5 KB

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