CMakeLists.txt 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. set(CMAKE_LEGACY_CYGWIN_WIN32 0)
  2. cmake_minimum_required(VERSION 2.8.5)
  3. include(GNUInstallDirs)
  4. project(cJSON C)
  5. set(PROJECT_VERSION_MAJOR 1)
  6. set(PROJECT_VERSION_MINOR 5)
  7. set(PROJECT_VERSION_PATCH 6)
  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. list(APPEND custom_compiler_flags
  47. /GS
  48. /Za
  49. /sdl
  50. /W4
  51. )
  52. endif()
  53. endif()
  54. option(ENABLE_SANITIZERS "Enables AddressSanitizer and UndefinedBehaviorSanitizer." OFF)
  55. if (ENABLE_SANITIZERS)
  56. list(APPEND custom_compiler_flags
  57. -fno-omit-frame-pointer
  58. -fsanitize=address
  59. -fsanitize=undefined
  60. -fsanitize=float-divide-by-zero
  61. -fsanitize=float-cast-overflow
  62. -fsanitize-address-use-after-scope
  63. -fsanitize=integer
  64. -01
  65. -fno-sanitize-recover
  66. )
  67. endif()
  68. option(ENABLE_PUBLIC_SYMBOLS "Export library symbols." On)
  69. if (ENABLE_PUBLIC_SYMBOLS)
  70. list(APPEND custom_compiler_flags -fvisibility=hidden)
  71. add_definitions(-DCJSON_EXPORT_SYMBOLS -DCJSON_API_VISIBILITY)
  72. endif()
  73. option(ENABLE_HIDDEN_SYMBOLS "Hide library symbols." Off)
  74. if (ENABLE_HIDDEN_SYMBOLS)
  75. add_definitions(-DCJSON_HIDE_SYMBOLS -UCJSON_API_VISIBILITY)
  76. endif()
  77. # apply custom compiler flags
  78. foreach(compiler_flag ${custom_compiler_flags})
  79. #remove problematic characters
  80. string(REGEX REPLACE "[^a-zA-Z0-9]" "" current_variable ${compiler_flag})
  81. CHECK_C_COMPILER_FLAG(${compiler_flag} "FLAG_SUPPORTED_${current_variable}")
  82. if (FLAG_SUPPORTED_${current_variable})
  83. list(APPEND supported_compiler_flags)
  84. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${compiler_flag}")
  85. endif()
  86. endforeach()
  87. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${supported_compiler_flags}")
  88. #variables for pkg-config
  89. set(prefix "${CMAKE_INSTALL_PREFIX}")
  90. set(libdir "${CMAKE_INSTALL_LIBDIR}")
  91. set(version "${PROJECT_VERSION}")
  92. set(includedir "${CMAKE_INSTALL_INCLUDEDIR}")
  93. option(BUILD_SHARED_LIBS "Build shared libraries" ON)
  94. option(ENABLE_TARGET_EXPORT "Enable exporting of CMake targets. Disable when it causes problems!" ON)
  95. #cJSON
  96. set(CJSON_LIB cjson)
  97. file(GLOB HEADERS cJSON.h)
  98. set(SOURCES cJSON.c)
  99. option(BUILD_SHARED_AND_STATIC_LIBS "Build both shared and static libraries" Off)
  100. if (NOT BUILD_SHARED_AND_STATIC_LIBS)
  101. add_library("${CJSON_LIB}" "${HEADERS}" "${SOURCES}")
  102. else()
  103. # 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
  104. add_library("${CJSON_LIB}" SHARED "${HEADERS}" "${SOURCES}")
  105. add_library("${CJSON_LIB}-static" STATIC "${HEADERS}" "${SOURCES}")
  106. set_target_properties("${CJSON_LIB}-static" PROPERTIES OUTPUT_NAME "${CJSON_LIB}")
  107. set_target_properties("${CJSON_LIB}-static" PROPERTIES PREFIX "lib")
  108. endif()
  109. if (NOT WIN32)
  110. target_link_libraries("${CJSON_LIB}" m)
  111. endif()
  112. configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson.pc.in"
  113. "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY)
  114. install(FILES cJSON.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson")
  115. install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
  116. install(TARGETS "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_LIB}")
  117. if (BUILD_SHARED_AND_STATIC_LIBS)
  118. install(TARGETS "${CJSON_LIB}-static" DESTINATION "${CMAKE_INSTALL_LIBDIR}")
  119. endif()
  120. if(ENABLE_TARGET_EXPORT)
  121. # export library information for CMake projects
  122. install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON")
  123. endif()
  124. set_target_properties("${CJSON_LIB}"
  125. PROPERTIES
  126. SOVERSION "${CJSON_VERSION_SO}"
  127. VERSION "${PROJECT_VERSION}")
  128. #cJSON_Utils
  129. option(ENABLE_CJSON_UTILS "Enable building the cJSON_Utils library." OFF)
  130. if(ENABLE_CJSON_UTILS)
  131. set(CJSON_UTILS_LIB cjson_utils)
  132. file(GLOB HEADERS_UTILS cJSON_Utils.h)
  133. set(SOURCES_UTILS cJSON_Utils.c)
  134. if (NOT BUILD_SHARED_AND_STATIC_LIBS)
  135. add_library("${CJSON_UTILS_LIB}" "${HEADERS_UTILS}" "${SOURCES_UTILS}")
  136. target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}")
  137. else()
  138. add_library("${CJSON_UTILS_LIB}" SHARED "${HEADERS_UTILS}" "${SOURCES_UTILS}")
  139. target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}")
  140. add_library("${CJSON_UTILS_LIB}-static" STATIC "${HEADERS_UTILS}" "${SOURCES_UTILS}")
  141. target_link_libraries("${CJSON_UTILS_LIB}-static" "${CJSON_LIB}-static")
  142. set_target_properties("${CJSON_UTILS_LIB}-static" PROPERTIES OUTPUT_NAME "${CJSON_UTILS_LIB}")
  143. set_target_properties("${CJSON_UTILS_LIB}-static" PROPERTIES PREFIX "lib")
  144. endif()
  145. configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson_utils.pc.in"
  146. "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" @ONLY)
  147. install(TARGETS "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_UTILS_LIB}")
  148. if (BUILD_SHARED_AND_STATIC_LIBS)
  149. install(TARGETS "${CJSON_UTILS_LIB}-static" DESTINATION "${CMAKE_INSTALL_LIBDIR}")
  150. endif()
  151. install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson")
  152. install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
  153. if(ENABLE_TARGET_EXPORT)
  154. # export library information for CMake projects
  155. install(EXPORT "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON")
  156. endif()
  157. set_target_properties("${CJSON_UTILS_LIB}"
  158. PROPERTIES
  159. SOVERSION "${CJSON_UTILS_VERSION_SO}"
  160. VERSION "${PROJECT_VERSION}")
  161. endif()
  162. # create the other package config files
  163. configure_file(
  164. "${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfig.cmake.in"
  165. ${PROJECT_BINARY_DIR}/cJSONConfig.cmake @ONLY)
  166. configure_file(
  167. "${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfigVersion.cmake.in"
  168. ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake @ONLY)
  169. # Install package config files
  170. install(FILES ${PROJECT_BINARY_DIR}/cJSONConfig.cmake
  171. ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake
  172. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON")
  173. option(ENABLE_CJSON_TEST "Enable building cJSON test" ON)
  174. if(ENABLE_CJSON_TEST)
  175. enable_testing()
  176. set(TEST_CJSON cJSON_test)
  177. add_executable("${TEST_CJSON}" test.c)
  178. target_link_libraries("${TEST_CJSON}" "${CJSON_LIB}")
  179. add_test(NAME ${TEST_CJSON} COMMAND "${CMAKE_CURRENT_BINARY_DIR}/${TEST_CJSON}")
  180. # Disable -fsanitize=float-divide-by-zero for cJSON_test
  181. if (FLAG_SUPPORTED_fsanitizefloatdividebyzero)
  182. if ("${CMAKE_VERSION}" VERSION_LESS "2.8.12")
  183. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize=float-divide-by-zero")
  184. else()
  185. target_compile_options(${TEST_CJSON} PRIVATE "-fno-sanitize=float-divide-by-zero")
  186. endif()
  187. endif()
  188. #"check" target that automatically builds everything and runs the tests
  189. add_custom_target(check
  190. COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
  191. DEPENDS ${TEST_CJSON})
  192. endif()
  193. add_subdirectory(tests)
  194. add_subdirectory(fuzzing)