CMakeLists.txt 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 behaivor, 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. #
  25. # Now enumerate specific policies newer than JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION
  26. # that may need to be individually set to NEW/OLD
  27. #
  28. foreach(pnew "") # Currently Empty
  29. if(POLICY ${pnew})
  30. cmake_policy(SET ${pnew} NEW)
  31. endif()
  32. endforeach()
  33. foreach(pold "") # Currently Empty
  34. if(POLICY ${pold})
  35. cmake_policy(SET ${pold} OLD)
  36. endif()
  37. endforeach()
  38. # ==== Define language standard configurations requiring at least c++11 standard
  39. if(CMAKE_CXX_STANDARD EQUAL "98")
  40. message(FATAL_ERROR "CMAKE_CXX_STANDARD:STRING=98 is not supported.")
  41. endif()
  42. #####
  43. ## Set the default target properties
  44. if(NOT CMAKE_CXX_STANDARD)
  45. set(CMAKE_CXX_STANDARD 11) # Supported values are ``11``, ``14``, and ``17``.
  46. endif()
  47. if(NOT CMAKE_CXX_STANDARD_REQUIRED)
  48. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  49. endif()
  50. if(NOT CMAKE_CXX_EXTENSIONS)
  51. set(CMAKE_CXX_EXTENSIONS OFF)
  52. endif()
  53. # ====
  54. # Ensures that CMAKE_BUILD_TYPE has a default value
  55. if(NOT DEFINED CMAKE_BUILD_TYPE)
  56. set(CMAKE_BUILD_TYPE Release CACHE STRING
  57. "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.")
  58. endif()
  59. # ---------------------------------------------------------------------------
  60. # use ccache if found, has to be done before project()
  61. # ---------------------------------------------------------------------------
  62. find_program(CCACHE_EXECUTABLE "ccache" HINTS /usr/local/bin /opt/local/bin)
  63. if(CCACHE_EXECUTABLE)
  64. message(STATUS "use ccache")
  65. set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_EXECUTABLE}" CACHE PATH "ccache" FORCE)
  66. set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_EXECUTABLE}" CACHE PATH "ccache" FORCE)
  67. endif()
  68. project(JSONCPP
  69. # Note: version must be updated in three places when doing a release. This
  70. # annoying process ensures that amalgamate, CMake, and meson all report the
  71. # correct version.
  72. # 1. ./meson.build
  73. # 2. ./include/json/version.h
  74. # 3. ./CMakeLists.txt
  75. # IMPORTANT: also update the JSONCPP_SOVERSION!!
  76. VERSION 1.9.3 # <major>[.<minor>[.<patch>[.<tweak>]]]
  77. LANGUAGES CXX)
  78. message(STATUS "JsonCpp Version: ${JSONCPP_VERSION_MAJOR}.${JSONCPP_VERSION_MINOR}.${JSONCPP_VERSION_PATCH}")
  79. set(JSONCPP_SOVERSION 23)
  80. option(JSONCPP_WITH_TESTS "Compile and (for jsoncpp_check) run JsonCpp test executables" ON)
  81. option(JSONCPP_WITH_POST_BUILD_UNITTEST "Automatically run unit-tests as a post build step" ON)
  82. option(JSONCPP_WITH_WARNING_AS_ERROR "Force compilation to fail if a warning occurs" OFF)
  83. option(JSONCPP_WITH_STRICT_ISO "Issue all the warnings demanded by strict ISO C and ISO C++" ON)
  84. option(JSONCPP_WITH_PKGCONFIG_SUPPORT "Generate and install .pc files" ON)
  85. option(JSONCPP_WITH_CMAKE_PACKAGE "Generate and install cmake package files" ON)
  86. option(JSONCPP_WITH_EXAMPLE "Compile JsonCpp example" OFF)
  87. option(BUILD_SHARED_LIBS "Build jsoncpp_lib as a shared library." OFF)
  88. # Enable runtime search path support for dynamic libraries on OSX
  89. if(APPLE)
  90. set(CMAKE_MACOSX_RPATH 1)
  91. endif()
  92. # Adhere to GNU filesystem layout conventions
  93. include(GNUInstallDirs)
  94. set(DEBUG_LIBNAME_SUFFIX "" CACHE STRING "Optional suffix to append to the library name for a debug build")
  95. set(JSONCPP_USE_SECURE_MEMORY "0" CACHE STRING "-D...=1 to use memory-wiping allocator for STL")
  96. configure_file("${PROJECT_SOURCE_DIR}/version.in"
  97. "${PROJECT_BINARY_DIR}/version"
  98. NEWLINE_STYLE UNIX)
  99. macro(use_compilation_warning_as_error)
  100. if(MSVC)
  101. # Only enabled in debug because some old versions of VS STL generate
  102. # warnings when compiled in release configuration.
  103. if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
  104. add_compile_options($<$<CONFIG:Debug>:/WX>)
  105. else()
  106. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /WX ")
  107. endif()
  108. elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  109. if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
  110. add_compile_options(-Werror)
  111. else()
  112. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
  113. endif()
  114. if(JSONCPP_WITH_STRICT_ISO)
  115. if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
  116. add_compile_options(-pedantic-errors)
  117. else()
  118. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic-errors")
  119. endif()
  120. endif()
  121. endif()
  122. endmacro()
  123. # Include our configuration header
  124. include_directories(${jsoncpp_SOURCE_DIR}/include)
  125. if(MSVC)
  126. # Only enabled in debug because some old versions of VS STL generate
  127. # unreachable code warning when compiled in release configuration.
  128. if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
  129. add_compile_options($<$<CONFIG:Debug>:/W4>)
  130. else()
  131. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /W4 ")
  132. endif()
  133. endif()
  134. if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  135. # using regular Clang or AppleClang
  136. if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
  137. add_compile_options(-Wall -Wconversion -Wshadow -Werror=conversion -Werror=sign-compare)
  138. else()
  139. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wconversion -Wshadow -Werror=conversion -Werror=sign-compare")
  140. endif()
  141. elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  142. # using GCC
  143. if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
  144. add_compile_options(-Wall -Wconversion -Wshadow -Wextra)
  145. else()
  146. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wconversion -Wshadow -Wextra")
  147. endif()
  148. # not yet ready for -Wsign-conversion
  149. if(JSONCPP_WITH_STRICT_ISO)
  150. if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
  151. add_compile_options(-Wpedantic)
  152. else()
  153. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wpedantic")
  154. endif()
  155. endif()
  156. if(JSONCPP_WITH_WARNING_AS_ERROR)
  157. if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
  158. add_compile_options(-Werror=conversion)
  159. else()
  160. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=conversion")
  161. endif()
  162. endif()
  163. elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
  164. # using Intel compiler
  165. if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
  166. add_compile_options(-Wall -Wconversion -Wshadow -Wextra -Werror=conversion)
  167. else()
  168. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wconversion -Wshadow -Wextra -Werror=conversion")
  169. endif()
  170. if(JSONCPP_WITH_STRICT_ISO AND NOT JSONCPP_WITH_WARNING_AS_ERROR)
  171. if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
  172. add_compile_options(-Wpedantic)
  173. else()
  174. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wpedantic")
  175. endif()
  176. endif()
  177. endif()
  178. if(JSONCPP_WITH_WARNING_AS_ERROR)
  179. use_compilation_warning_as_error()
  180. endif()
  181. if(JSONCPP_WITH_PKGCONFIG_SUPPORT)
  182. configure_file(
  183. "pkg-config/jsoncpp.pc.in"
  184. "pkg-config/jsoncpp.pc"
  185. @ONLY)
  186. install(FILES "${CMAKE_CURRENT_BINARY_DIR}/pkg-config/jsoncpp.pc"
  187. DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
  188. endif()
  189. if(JSONCPP_WITH_CMAKE_PACKAGE)
  190. include(CMakePackageConfigHelpers)
  191. install(EXPORT jsoncpp
  192. DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp
  193. FILE jsoncppConfig.cmake)
  194. write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake"
  195. VERSION ${PROJECT_VERSION}
  196. COMPATIBILITY SameMajorVersion)
  197. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake
  198. DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp)
  199. endif()
  200. if(JSONCPP_WITH_TESTS)
  201. enable_testing()
  202. include(CTest)
  203. endif()
  204. # Build the different applications
  205. add_subdirectory(src)
  206. #install the includes
  207. add_subdirectory(include)
  208. #install the example
  209. if(JSONCPP_WITH_EXAMPLE)
  210. add_subdirectory(example)
  211. endif()