CMakeLists.txt 5.5 KB

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