CMakeLists.txt 9.2 KB

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