CMakeLists.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. set(CMAKE_LEGACY_CYGWIN_WIN32 0)
  2. cmake_minimum_required(VERSION 2.8)
  3. subdirs(tests fuzzing)
  4. include(GNUInstallDirs)
  5. project(cJSON C)
  6. set(PROJECT_VERSION_MAJOR 1)
  7. set(PROJECT_VERSION_MINOR 3)
  8. set(PROJECT_VERSION_PATCH 0)
  9. set(CJSON_VERSION_SO 1)
  10. set(CJSON_UTILS_VERSION_SO 1)
  11. set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
  12. set(custom_compiler_flags)
  13. include(CheckCCompilerFlag)
  14. option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags for Clang and GCC" ON)
  15. if (ENABLE_CUSTOM_COMPILER_FLAGS)
  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. )
  38. endif()
  39. option(ENABLE_SANITIZERS "Enables AddressSanitizer and UndefinedBehaviorSanitizer." OFF)
  40. if (ENABLE_SANITIZERS)
  41. list(APPEND custom_compiler_flags
  42. -fno-omit-frame-pointer
  43. -fsanitize=address
  44. -fsanitize=undefined
  45. -fsanitize=float-divide-by-zero
  46. -fsanitize=float-cast-overflow
  47. -fsanitize-address-use-after-scope
  48. -fsanitize=integer
  49. -01
  50. -fno-sanitize-recover
  51. )
  52. endif()
  53. # apply custom compiler flags
  54. foreach(compiler_flag ${custom_compiler_flags})
  55. #remove problematic characters
  56. string(REGEX REPLACE "[^a-zA-Z0-9]" "" current_variable ${compiler_flag})
  57. CHECK_C_COMPILER_FLAG(${compiler_flag} "FLAG_SUPPORTED_${current_variable}")
  58. if (FLAG_SUPPORTED_${current_variable})
  59. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${compiler_flag}")
  60. endif()
  61. endforeach()
  62. #variables for pkg-config
  63. set(prefix "${CMAKE_INSTALL_PREFIX}")
  64. set(libdir "${CMAKE_INSTALL_LIBDIR}")
  65. set(version "${PROJECT_VERSION}")
  66. set(includedir "${CMAKE_INSTALL_INCLUDEDIR}")
  67. option(BUILD_SHARED_LIBS "Build shared libraries" ON)
  68. option(ENABLE_TARGET_EXPORT "Enable exporting of CMake targets. Disable when it causes problems!" ON)
  69. #cJSON
  70. set(CJSON_LIB cjson)
  71. file(GLOB HEADERS cJSON.h)
  72. set(SOURCES cJSON.c)
  73. add_library("${CJSON_LIB}" "${HEADERS}" "${SOURCES}")
  74. if (NOT WIN32)
  75. target_link_libraries("${CJSON_LIB}" m)
  76. endif()
  77. configure_file("${CMAKE_CURRENT_SOURCE_DIR}/libcjson.pc.in"
  78. "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY)
  79. install(FILES cJSON.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson")
  80. install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
  81. install(TARGETS "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_LIB}")
  82. if(ENABLE_TARGET_EXPORT)
  83. # export library information for CMake projects
  84. install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON")
  85. endif()
  86. set_target_properties("${CJSON_LIB}"
  87. PROPERTIES
  88. SOVERSION "${CJSON_VERSION_SO}"
  89. VERSION "${PROJECT_VERSION}")
  90. #cJSON_Utils
  91. option(ENABLE_CJSON_UTILS "Enable building the cJSON_Utils library." OFF)
  92. if(ENABLE_CJSON_UTILS)
  93. set(CJSON_UTILS_LIB cjson_utils)
  94. file(GLOB HEADERS_UTILS cJSON_Utils.h)
  95. set(SOURCES_UTILS cJSON_Utils.c)
  96. add_library("${CJSON_UTILS_LIB}" "${HEADERS_UTILS}" "${SOURCES_UTILS}")
  97. target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}")
  98. configure_file("${CMAKE_CURRENT_SOURCE_DIR}/libcjson_utils.pc.in"
  99. "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" @ONLY)
  100. install(TARGETS "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_UTILS_LIB}")
  101. install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson")
  102. install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
  103. if(ENABLE_TARGET_EXPORT)
  104. # export library information for CMake projects
  105. install(EXPORT "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON")
  106. endif()
  107. set_target_properties("${CJSON_UTILS_LIB}"
  108. PROPERTIES
  109. SOVERSION "${CJSON_UTILS_VERSION_SO}"
  110. VERSION "${PROJECT_VERSION}")
  111. endif()
  112. # create the other package config files
  113. configure_file(
  114. cJSONConfig.cmake.in
  115. ${PROJECT_BINARY_DIR}/cJSONConfig.cmake @ONLY)
  116. configure_file(
  117. cJSONConfigVersion.cmake.in
  118. ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake @ONLY)
  119. # Install package config files
  120. install(FILES ${PROJECT_BINARY_DIR}/cJSONConfig.cmake
  121. ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake
  122. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON")
  123. option(ENABLE_CJSON_TEST "Enable building cJSON test" ON)
  124. if(ENABLE_CJSON_TEST)
  125. enable_testing()
  126. set(TEST_CJSON cJSON_test)
  127. add_executable("${TEST_CJSON}" test.c)
  128. target_link_libraries("${TEST_CJSON}" "${CJSON_LIB}")
  129. if(ENABLE_CJSON_UTILS)
  130. set(TEST_CJSON_UTILS cJSON_test_utils)
  131. add_executable("${TEST_CJSON_UTILS}" test_utils.c)
  132. target_link_libraries("${TEST_CJSON_UTILS}" "${CJSON_UTILS_LIB}")
  133. endif()
  134. endif()