CMakeLists.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. set(CMAKE_LEGACY_CYGWIN_WIN32 0)
  2. cmake_minimum_required(VERSION 3.0)
  3. project(cJSON
  4. VERSION 1.7.15
  5. LANGUAGES C)
  6. cmake_policy(SET CMP0054 NEW) # set CMP0054 policy
  7. include(GNUInstallDirs)
  8. set(CJSON_VERSION_SO 1)
  9. set(CJSON_UTILS_VERSION_SO 1)
  10. set(custom_compiler_flags)
  11. include(CheckCCompilerFlag)
  12. option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags" ON)
  13. if (ENABLE_CUSTOM_COMPILER_FLAGS)
  14. if (("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") OR ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU"))
  15. list(APPEND custom_compiler_flags
  16. -std=c89
  17. -pedantic
  18. -Wall
  19. -Wextra
  20. -Werror
  21. -Wstrict-prototypes
  22. -Wwrite-strings
  23. -Wshadow
  24. -Winit-self
  25. -Wcast-align
  26. -Wformat=2
  27. -Wmissing-prototypes
  28. -Wstrict-overflow=2
  29. -Wcast-qual
  30. -Wundef
  31. -Wswitch-default
  32. -Wconversion
  33. -Wc++-compat
  34. -fstack-protector-strong
  35. -Wcomma
  36. -Wdouble-promotion
  37. -Wparentheses
  38. -Wformat-overflow
  39. -Wunused-macros
  40. -Wmissing-variable-declarations
  41. -Wused-but-marked-unused
  42. -Wswitch-enum
  43. )
  44. elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
  45. # Disable warning c4001 - nonstandard extension 'single line comment' was used
  46. # Define _CRT_SECURE_NO_WARNINGS to disable deprecation warnings for "insecure" C library functions
  47. list(APPEND custom_compiler_flags
  48. /GS
  49. /Za
  50. /sdl
  51. /W4
  52. /wd4001
  53. /D_CRT_SECURE_NO_WARNINGS
  54. )
  55. endif()
  56. endif()
  57. option(ENABLE_SANITIZERS "Enables AddressSanitizer and UndefinedBehaviorSanitizer." OFF)
  58. if (ENABLE_SANITIZERS)
  59. list(APPEND custom_compiler_flags
  60. -fno-omit-frame-pointer
  61. -fsanitize=address
  62. -fsanitize=undefined
  63. -fsanitize=float-cast-overflow
  64. -fsanitize-address-use-after-scope
  65. -fsanitize=integer
  66. -01
  67. -fno-sanitize-recover
  68. )
  69. endif()
  70. option(ENABLE_SAFE_STACK "Enables the SafeStack instrumentation pass by the Code Pointer Integrity Project" OFF)
  71. if (ENABLE_SAFE_STACK)
  72. if (ENABLE_SANITIZERS)
  73. message(FATAL_ERROR "ENABLE_SAFE_STACK cannot be used in combination with ENABLE_SANITIZERS")
  74. endif()
  75. list(APPEND custom_compiler_flags
  76. -fsanitize=safe-stack
  77. )
  78. endif()
  79. option(ENABLE_PUBLIC_SYMBOLS "Export library symbols." On)
  80. if (ENABLE_PUBLIC_SYMBOLS)
  81. list(APPEND custom_compiler_flags -fvisibility=hidden)
  82. add_definitions(-DCJSON_EXPORT_SYMBOLS -DCJSON_API_VISIBILITY)
  83. endif()
  84. option(ENABLE_HIDDEN_SYMBOLS "Hide library symbols." Off)
  85. if (ENABLE_HIDDEN_SYMBOLS)
  86. add_definitions(-DCJSON_HIDE_SYMBOLS -UCJSON_API_VISIBILITY)
  87. endif()
  88. # apply custom compiler flags
  89. foreach(compiler_flag ${custom_compiler_flags})
  90. #remove problematic characters
  91. string(REGEX REPLACE "[^a-zA-Z0-9]" "" current_variable ${compiler_flag})
  92. CHECK_C_COMPILER_FLAG(${compiler_flag} "FLAG_SUPPORTED_${current_variable}")
  93. if (FLAG_SUPPORTED_${current_variable})
  94. list(APPEND supported_compiler_flags)
  95. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${compiler_flag}")
  96. endif()
  97. endforeach()
  98. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${supported_compiler_flags}")
  99. option(BUILD_SHARED_LIBS "Build shared libraries" ON)
  100. option(ENABLE_TARGET_EXPORT "Enable exporting of CMake targets. Disable when it causes problems!" ON)
  101. #cJSON
  102. set(CJSON_LIB cjson)
  103. file(GLOB HEADERS cJSON.h)
  104. set(SOURCES cJSON.c)
  105. option(BUILD_SHARED_AND_STATIC_LIBS "Build both shared and static libraries" Off)
  106. option(CJSON_OVERRIDE_BUILD_SHARED_LIBS "Override BUILD_SHARED_LIBS with CJSON_BUILD_SHARED_LIBS" OFF)
  107. option(CJSON_BUILD_SHARED_LIBS "Overrides BUILD_SHARED_LIBS if CJSON_OVERRIDE_BUILD_SHARED_LIBS is enabled" ON)
  108. option(ENABLE_CJSON_VERSION_SO "Enables cJSON so version" ON)
  109. if ((CJSON_OVERRIDE_BUILD_SHARED_LIBS AND CJSON_BUILD_SHARED_LIBS) OR ((NOT CJSON_OVERRIDE_BUILD_SHARED_LIBS) AND BUILD_SHARED_LIBS))
  110. set(CJSON_LIBRARY_TYPE SHARED)
  111. else()
  112. set(CJSON_LIBRARY_TYPE STATIC)
  113. endif()
  114. if (NOT BUILD_SHARED_AND_STATIC_LIBS)
  115. add_library("${CJSON_LIB}" "${CJSON_LIBRARY_TYPE}" "${HEADERS}" "${SOURCES}")
  116. else()
  117. # See https://cmake.org/Wiki/CMake_FAQ#How_do_I_make_my_shared_and_static_libraries_have_the_same_root_name.2C_but_different_suffixes.3F
  118. add_library("${CJSON_LIB}" SHARED "${HEADERS}" "${SOURCES}")
  119. add_library("${CJSON_LIB}-static" STATIC "${HEADERS}" "${SOURCES}")
  120. set_target_properties("${CJSON_LIB}-static" PROPERTIES OUTPUT_NAME "${CJSON_LIB}")
  121. set_target_properties("${CJSON_LIB}-static" PROPERTIES PREFIX "lib")
  122. endif()
  123. if (NOT WIN32)
  124. target_link_libraries("${CJSON_LIB}" m)
  125. endif()
  126. configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson.pc.in"
  127. "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY)
  128. install(FILES cJSON.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson")
  129. install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig")
  130. install(TARGETS "${CJSON_LIB}"
  131. EXPORT "${CJSON_LIB}"
  132. ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
  133. LIBRARY DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
  134. RUNTIME DESTINATION "${CMAKE_INSTALL_FULL_BINDIR}"
  135. INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}"
  136. )
  137. if (BUILD_SHARED_AND_STATIC_LIBS)
  138. install(TARGETS "${CJSON_LIB}-static"
  139. EXPORT "${CJSON_LIB}"
  140. ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
  141. INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}"
  142. )
  143. endif()
  144. if(ENABLE_TARGET_EXPORT)
  145. # export library information for CMake projects
  146. install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON")
  147. endif()
  148. if(ENABLE_CJSON_VERSION_SO)
  149. set_target_properties("${CJSON_LIB}"
  150. PROPERTIES
  151. SOVERSION "${CJSON_VERSION_SO}"
  152. VERSION "${PROJECT_VERSION}")
  153. endif()
  154. #cJSON_Utils
  155. option(ENABLE_CJSON_UTILS "Enable building the cJSON_Utils library." OFF)
  156. if(ENABLE_CJSON_UTILS)
  157. set(CJSON_UTILS_LIB cjson_utils)
  158. file(GLOB HEADERS_UTILS cJSON_Utils.h)
  159. set(SOURCES_UTILS cJSON_Utils.c)
  160. if (NOT BUILD_SHARED_AND_STATIC_LIBS)
  161. add_library("${CJSON_UTILS_LIB}" "${CJSON_LIBRARY_TYPE}" "${HEADERS_UTILS}" "${SOURCES_UTILS}")
  162. target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}")
  163. else()
  164. add_library("${CJSON_UTILS_LIB}" SHARED "${HEADERS_UTILS}" "${SOURCES_UTILS}")
  165. target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}")
  166. add_library("${CJSON_UTILS_LIB}-static" STATIC "${HEADERS_UTILS}" "${SOURCES_UTILS}")
  167. target_link_libraries("${CJSON_UTILS_LIB}-static" "${CJSON_LIB}-static")
  168. set_target_properties("${CJSON_UTILS_LIB}-static" PROPERTIES OUTPUT_NAME "${CJSON_UTILS_LIB}")
  169. set_target_properties("${CJSON_UTILS_LIB}-static" PROPERTIES PREFIX "lib")
  170. endif()
  171. configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson_utils.pc.in"
  172. "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" @ONLY)
  173. install(TARGETS "${CJSON_UTILS_LIB}"
  174. EXPORT "${CJSON_UTILS_LIB}"
  175. ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
  176. LIBRARY DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
  177. RUNTIME DESTINATION "${CMAKE_INSTALL_FULL_BINDIR}"
  178. INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}"
  179. )
  180. if (BUILD_SHARED_AND_STATIC_LIBS)
  181. install(TARGETS "${CJSON_UTILS_LIB}-static"
  182. EXPORT "${CJSON_UTILS_LIB}"
  183. ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
  184. INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}"
  185. )
  186. endif()
  187. install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson")
  188. install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig")
  189. if(ENABLE_TARGET_EXPORT)
  190. # export library information for CMake projects
  191. install(EXPORT "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON")
  192. endif()
  193. if(ENABLE_CJSON_VERSION_SO)
  194. set_target_properties("${CJSON_UTILS_LIB}"
  195. PROPERTIES
  196. SOVERSION "${CJSON_UTILS_VERSION_SO}"
  197. VERSION "${PROJECT_VERSION}")
  198. endif()
  199. endif()
  200. # create the other package config files
  201. configure_file(
  202. "${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfig.cmake.in"
  203. ${PROJECT_BINARY_DIR}/cJSONConfig.cmake @ONLY)
  204. configure_file(
  205. "${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfigVersion.cmake.in"
  206. ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake @ONLY)
  207. if(ENABLE_TARGET_EXPORT)
  208. # Install package config files
  209. install(FILES ${PROJECT_BINARY_DIR}/cJSONConfig.cmake
  210. ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake
  211. DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON")
  212. endif()
  213. option(ENABLE_CJSON_TEST "Enable building cJSON test" ON)
  214. if(ENABLE_CJSON_TEST)
  215. enable_testing()
  216. set(TEST_CJSON cJSON_test)
  217. add_executable("${TEST_CJSON}" test.c)
  218. target_link_libraries("${TEST_CJSON}" "${CJSON_LIB}")
  219. add_test(NAME ${TEST_CJSON} COMMAND "${CMAKE_CURRENT_BINARY_DIR}/${TEST_CJSON}")
  220. # Disable -fsanitize=float-divide-by-zero for cJSON_test
  221. if (FLAG_SUPPORTED_fsanitizefloatdividebyzero)
  222. if ("${CMAKE_VERSION}" VERSION_LESS "2.8.12")
  223. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize=float-divide-by-zero")
  224. else()
  225. target_compile_options(${TEST_CJSON} PRIVATE "-fno-sanitize=float-divide-by-zero")
  226. endif()
  227. endif()
  228. #"check" target that automatically builds everything and runs the tests
  229. add_custom_target(check
  230. COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
  231. DEPENDS ${TEST_CJSON})
  232. endif()
  233. #Create the uninstall target
  234. option(ENABLE_CJSON_UNINSTALL "Enable creating uninstall target" ON)
  235. if(ENABLE_CJSON_UNINSTALL)
  236. add_custom_target(uninstall "${CMAKE_COMMAND}" -P
  237. "${PROJECT_SOURCE_DIR}/library_config/uninstall.cmake")
  238. endif()
  239. # Enable the use of locales
  240. option(ENABLE_LOCALES "Enable the use of locales" ON)
  241. if(ENABLE_LOCALES)
  242. add_definitions(-DENABLE_LOCALES)
  243. endif()
  244. add_subdirectory(tests)
  245. add_subdirectory(fuzzing)