CMakeLists.txt 6.3 KB

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