CMakeLists.txt 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
  2. if(POLICY CMP0025)
  3. # detect Apple's Clang
  4. cmake_policy(SET CMP0025 NEW)
  5. endif()
  6. if(POLICY CMP0054)
  7. cmake_policy(SET CMP0054 NEW)
  8. endif()
  9. SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules)
  10. PROJECT(RapidJSON CXX)
  11. set(LIB_MAJOR_VERSION "1")
  12. set(LIB_MINOR_VERSION "1")
  13. set(LIB_PATCH_VERSION "0")
  14. set(LIB_VERSION_STRING "${LIB_MAJOR_VERSION}.${LIB_MINOR_VERSION}.${LIB_PATCH_VERSION}")
  15. # compile in release with debug info mode by default
  16. if(NOT CMAKE_BUILD_TYPE)
  17. set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
  18. endif()
  19. # Build all binaries in a separate directory
  20. SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  21. option(RAPIDJSON_BUILD_DOC "Build rapidjson documentation." ON)
  22. option(RAPIDJSON_BUILD_EXAMPLES "Build rapidjson examples." ON)
  23. option(RAPIDJSON_BUILD_TESTS "Build rapidjson perftests and unittests." ON)
  24. option(RAPIDJSON_BUILD_THIRDPARTY_GTEST
  25. "Use gtest installation in `thirdparty/gtest` by default if available" OFF)
  26. option(RAPIDJSON_BUILD_CXX11 "Build rapidjson with C++11 (gcc/clang)" ON)
  27. option(RAPIDJSON_BUILD_ASAN "Build rapidjson with address sanitizer (gcc/clang)" OFF)
  28. option(RAPIDJSON_BUILD_UBSAN "Build rapidjson with undefined behavior sanitizer (gcc/clang)" OFF)
  29. option(RAPIDJSON_HAS_STDSTRING "" OFF)
  30. if(RAPIDJSON_HAS_STDSTRING)
  31. add_definitions(-DRAPIDJSON_HAS_STDSTRING)
  32. endif()
  33. find_program(CCACHE_FOUND ccache)
  34. if(CCACHE_FOUND)
  35. set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
  36. set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
  37. if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  38. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Qunused-arguments -fcolor-diagnostics")
  39. endif()
  40. endif(CCACHE_FOUND)
  41. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  42. if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "powerpc" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64le")
  43. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcpu=native")
  44. else()
  45. #FIXME: x86 is -march=native, but doesn't mean every arch is this option. To keep original project's compatibility, I leave this except POWER.
  46. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
  47. endif()
  48. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror")
  49. set(EXTRA_CXX_FLAGS -Weffc++ -Wswitch-default -Wfloat-equal -Wconversion -Wsign-conversion)
  50. if (RAPIDJSON_BUILD_CXX11)
  51. if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.7.0")
  52. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
  53. else()
  54. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  55. endif()
  56. endif()
  57. if (RAPIDJSON_BUILD_ASAN)
  58. if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.8.0")
  59. message(FATAL_ERROR "GCC < 4.8 doesn't support the address sanitizer")
  60. else()
  61. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
  62. endif()
  63. endif()
  64. if (RAPIDJSON_BUILD_UBSAN)
  65. if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9.0")
  66. message(FATAL_ERROR "GCC < 4.9 doesn't support the undefined behavior sanitizer")
  67. else()
  68. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
  69. endif()
  70. endif()
  71. elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  72. if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "powerpc" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64le")
  73. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcpu=native")
  74. else()
  75. #FIXME: x86 is -march=native, but doesn't mean every arch is this option. To keep original project's compatibility, I leave this except POWER.
  76. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
  77. endif()
  78. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror -Wno-missing-field-initializers")
  79. set(EXTRA_CXX_FLAGS -Weffc++ -Wswitch-default -Wfloat-equal -Wconversion -Wimplicit-fallthrough -Weverything)
  80. if (RAPIDJSON_BUILD_CXX11)
  81. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  82. endif()
  83. if (RAPIDJSON_BUILD_ASAN)
  84. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
  85. endif()
  86. if (RAPIDJSON_BUILD_UBSAN)
  87. if (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
  88. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined-trap -fsanitize-undefined-trap-on-error")
  89. else()
  90. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
  91. endif()
  92. endif()
  93. elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
  94. add_definitions(-D_CRT_SECURE_NO_WARNINGS=1)
  95. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc")
  96. elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL")
  97. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -qarch=auto")
  98. endif()
  99. #add extra search paths for libraries and includes
  100. SET(INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "The directory the headers are installed in")
  101. SET(LIB_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE STRING "Directory where lib will install")
  102. SET(DOC_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/share/doc/${PROJECT_NAME}" CACHE PATH "Path to the documentation")
  103. IF(UNIX OR CYGWIN)
  104. SET(_CMAKE_INSTALL_DIR "${LIB_INSTALL_DIR}/cmake/${PROJECT_NAME}")
  105. ELSEIF(WIN32)
  106. SET(_CMAKE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/cmake")
  107. ENDIF()
  108. SET(CMAKE_INSTALL_DIR "${_CMAKE_INSTALL_DIR}" CACHE PATH "The directory cmake fiels are installed in")
  109. include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
  110. if(RAPIDJSON_BUILD_DOC)
  111. add_subdirectory(doc)
  112. endif()
  113. add_custom_target(travis_doc)
  114. add_custom_command(TARGET travis_doc
  115. COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/travis-doxygen.sh)
  116. if(RAPIDJSON_BUILD_EXAMPLES)
  117. add_subdirectory(example)
  118. endif()
  119. if(RAPIDJSON_BUILD_TESTS)
  120. if(MSVC11)
  121. # required for VS2012 due to missing support for variadic templates
  122. add_definitions(-D_VARIADIC_MAX=10)
  123. endif(MSVC11)
  124. add_subdirectory(test)
  125. include(CTest)
  126. endif()
  127. # pkg-config
  128. IF (UNIX OR CYGWIN)
  129. CONFIGURE_FILE (${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pc.in
  130. ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc
  131. @ONLY)
  132. INSTALL (FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc
  133. DESTINATION "${LIB_INSTALL_DIR}/pkgconfig"
  134. COMPONENT pkgconfig)
  135. ENDIF()
  136. install(FILES readme.md
  137. DESTINATION "${DOC_INSTALL_DIR}"
  138. COMPONENT doc)
  139. install(DIRECTORY include/rapidjson
  140. DESTINATION "${INCLUDE_INSTALL_DIR}"
  141. COMPONENT dev)
  142. install(DIRECTORY example/
  143. DESTINATION "${DOC_INSTALL_DIR}/examples"
  144. COMPONENT examples
  145. # Following patterns are for excluding the intermediate/object files
  146. # from an install of in-source CMake build.
  147. PATTERN "CMakeFiles" EXCLUDE
  148. PATTERN "Makefile" EXCLUDE
  149. PATTERN "cmake_install.cmake" EXCLUDE)
  150. # Provide config and version files to be used by other applications
  151. # ===============================
  152. ################################################################################
  153. # Export package for use from the build tree
  154. EXPORT( PACKAGE ${PROJECT_NAME} )
  155. # Create the RapidJSONConfig.cmake file for other cmake projects.
  156. # ... for the build tree
  157. SET( CONFIG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  158. SET( CONFIG_DIR ${CMAKE_CURRENT_BINARY_DIR})
  159. CONFIGURE_FILE( ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in
  160. ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake @ONLY )
  161. CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}ConfigVersion.cmake.in
  162. ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake @ONLY)
  163. # ... for the install tree
  164. SET( CMAKECONFIG_INSTALL_DIR lib/cmake/${PROJECT_NAME} )
  165. FILE( RELATIVE_PATH REL_INCLUDE_DIR
  166. "${CMAKE_INSTALL_PREFIX}/${CMAKECONFIG_INSTALL_DIR}"
  167. "${CMAKE_INSTALL_PREFIX}/include" )
  168. SET( ${PROJECT_NAME}_INCLUDE_DIR "\${${PROJECT_NAME}_CMAKE_DIR}/${REL_INCLUDE_DIR}" )
  169. SET( CONFIG_SOURCE_DIR )
  170. SET( CONFIG_DIR )
  171. CONFIGURE_FILE( ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in
  172. ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${PROJECT_NAME}Config.cmake @ONLY )
  173. INSTALL(FILES "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${PROJECT_NAME}Config.cmake"
  174. DESTINATION ${CMAKECONFIG_INSTALL_DIR} )
  175. # Install files
  176. INSTALL(FILES
  177. ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
  178. ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
  179. DESTINATION "${CMAKE_INSTALL_DIR}"
  180. COMPONENT dev)