You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CMakeLists.txt 8.6 kB

9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # Many projects still are stuck using CMake 2.8 is several places so it's good to provide backward support too. This is
  2. # specially true in embedded systems (OpenWRT and friends).
  3. cmake_minimum_required(VERSION 2.8)
  4. # JSON-C library is C only project.
  5. project(json-c C)
  6. # If we've got 3.0 then it's good, let's provide support. Otherwise, leave it be.
  7. if(POLICY CMP0038)
  8. # Policy CMP0038 introduced was in CMake 3.0
  9. cmake_policy(SET CMP0038 NEW)
  10. endif()
  11. # Set some packaging variables.
  12. set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
  13. set(CPACK_PACKAGE_VERSION_MAJOR "0")
  14. set(CPACK_PACKAGE_VERSION_MINOR "13")
  15. set(CPACK_PACKAGE_VERSION_PATCH "99")
  16. set(JSON_C_BUGREPORT "json-c@googlegroups.com")
  17. include(CheckSymbolExists)
  18. include(CheckIncludeFile)
  19. include(CheckIncludeFiles)
  20. include(CheckCSourceCompiles)
  21. include(CheckTypeSize)
  22. include(CPack)
  23. include(GNUInstallDirs)
  24. include(CMakePackageConfigHelpers)
  25. # Enable or disable features. By default, all features are turned off.
  26. option(ENABLE_RDRAND "Enable RDRAND Hardware RNG Hash Seed" OFF)
  27. option(ENABLE_THREADING "Enable partial threading support." OFF)
  28. option(HAS_GNU_WARNING_LONG "Define if .gnu.warning accepts long strings." OFF)
  29. if (UNIX OR MINGW OR CYGWIN)
  30. list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
  31. endif()
  32. if (UNIX)
  33. list(APPEND CMAKE_REQUIRED_LIBRARIES m)
  34. endif()
  35. if (MSVC)
  36. list(APPEND CMAKE_REQUIRED_DEFINITIONS /D_CRT_SECURE_NO_DEPRECATE)
  37. list(APPEND CMAKE_REQUIRED_FLAGS /wd4996)
  38. endif()
  39. check_include_file("fcntl.h" HAVE_FCNTL_H)
  40. check_include_file("inttypes.h" HAVE_INTTYPES_H)
  41. check_include_file(stdarg.h HAVE_STDARG_H)
  42. check_include_file(strings.h HAVE_STRINGS_H)
  43. check_include_file(string.h HAVE_STRING_H)
  44. check_include_file(syslog.h HAVE_SYSLOG_H)
  45. check_include_files("stdlib.h;stdarg.h;string.h;float.h" STDC_HEADERS)
  46. check_include_file(unistd.h HAVE_UNISTD_H)
  47. check_include_file(sys/types.h HAVE_SYS_TYPES_H)
  48. check_include_file("dlfcn.h" HAVE_DLFCN_H)
  49. check_include_file("endian.h" HAVE_ENDIAN_H)
  50. check_include_file("limits.h" HAVE_LIMITS_H)
  51. check_include_file("locale.h" HAVE_LOCALE_H)
  52. check_include_file("memory.h" HAVE_MEMORY_H)
  53. check_include_file(stdint.h HAVE_STDINT_H)
  54. check_include_file(stdlib.h HAVE_STDLIB_H)
  55. check_include_file(sys/cdefs.h HAVE_SYS_CDEFS_H)
  56. check_include_file(sys/param.h HAVE_SYS_PARAM_H)
  57. check_include_file(sys/stat.h HAVE_SYS_STAT_H)
  58. check_include_file(xlocale.h HAVE_XLOCALE_H)
  59. if (HAVE_INTTYPES_H)
  60. set(JSON_C_HAVE_INTTYPES_H 1)
  61. endif()
  62. check_symbol_exists(isnan "math.h" HAVE_DECL_ISNAN)
  63. check_symbol_exists(_isnan "float.h" HAVE_DECL__ISNAN)
  64. check_symbol_exists(_finite "float.h" HAVE_DECL__FINITE)
  65. check_symbol_exists(INFINITY "math.h" HAVE_DECL_INFINITY)
  66. check_symbol_exists(isinf "math.h" HAVE_DECL_ISINF)
  67. check_symbol_exists(nan "math.h" HAVE_DECL_NAN)
  68. check_symbol_exists(_doprnt "stdio.h" HAVE_DOPRNT)
  69. check_symbol_exists(snprintf "stdio.h" HAVE_SNPRINTF)
  70. check_symbol_exists(vasprintf "stdio.h" HAVE_VASPRINTF)
  71. check_symbol_exists(vsnprintf "stdio.h" HAVE_VSNPRINTF)
  72. check_symbol_exists(vprintf "stdio.h" HAVE_VPRINTF)
  73. if (HAVE_FCNTL_H)
  74. check_symbol_exists(open "fcntl.h" HAVE_OPEN)
  75. endif()
  76. if (HAVE_STDLIB_H)
  77. check_symbol_exists(realloc "stdlib.h" HAVE_REALLOC)
  78. endif()
  79. if (HAVE_LOCALE_H)
  80. check_symbol_exists(setlocale "locale.h" HAVE_SETLOCALE)
  81. check_symbol_exists(uselocale "locale.h" HAVE_USELOCALE)
  82. endif()
  83. if (HAVE_STRINGS_H)
  84. check_symbol_exists(strcasecmp "strings.h" HAVE_STRCASECMP)
  85. check_symbol_exists(strncasecmp "strings.h" HAVE_STRNCASECMP)
  86. endif()
  87. if (HAVE_STRING_H)
  88. check_symbol_exists(strdup "string.h" HAVE_STRDUP)
  89. check_symbol_exists(strerror "string.h" HAVE_STRERROR)
  90. endif()
  91. if (HAVE_SYSLOG_H)
  92. check_symbol_exists(vsyslog "syslog.h" HAVE_VSYSLOG)
  93. endif()
  94. if (MSVC)
  95. check_symbol_exists(strtoll "stdlib.h" HAVE_STRTOLL)
  96. set(json_c_strtoll "strtoll")
  97. if (NOT HAVE_STRTOLL)
  98. # Use _strtoi64 if strtoll is not available.
  99. check_symbol_exists(_strtoi64 "stdlib.h" __have_strtoi64)
  100. if (__have_strtoi64)
  101. set(HAVE_STRTOLL 1)
  102. set(json_c_strtoll "_strtoi64")
  103. # could do the same for strtoull, if needed
  104. endif()
  105. endif()
  106. endif()
  107. check_type_size(int SIZEOF_INT)
  108. check_type_size(int64_t SIZEOF_INT64_T)
  109. check_type_size(long SIZEOF_LONG)
  110. check_type_size("long long" SIZEOF_LONG_LONG)
  111. check_type_size("size_t" SIZEOF_SIZE_T)
  112. check_c_source_compiles(
  113. "int main() { int i, x = 0; i = __sync_add_and_fetch(&x,1); return x; }"
  114. HAVE_ATOMIC_BUILTINS)
  115. check_c_source_compiles(
  116. "__thread int x = 0; int main() { return 0; }"
  117. HAVE___THREAD)
  118. if (HAVE___THREAD)
  119. set(SPEC___THREAD __thread)
  120. elseif (MSVC)
  121. set(SPEC___THREAD __declspec(thread))
  122. endif()
  123. # Once we've done basic symbol/header searches let's add them in.
  124. configure_file(${CMAKE_SOURCE_DIR}/cmake/config.h.in ${CMAKE_BINARY_DIR}/config.h)
  125. message(STATUS "Written ${CMAKE_BINARY_DIR}/config.h")
  126. configure_file(${CMAKE_SOURCE_DIR}/cmake/json_config.h.in ${CMAKE_BINARY_DIR}/json_config.h)
  127. message(STATUS "Written ${CMAKE_BINARY_DIR}/json_config.h")
  128. configure_package_config_file(
  129. "cmake/Config.cmake.in"
  130. ${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake
  131. INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
  132. )
  133. if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
  134. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections")
  135. # There's a catch here.
  136. set(CMAKE_C_FLAGS "-Werror")
  137. add_definitions(-D_GNU_SOURCE)
  138. elseif ("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
  139. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /DEBUG")
  140. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4100")
  141. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4996")
  142. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4244")
  143. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4706")
  144. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4702")
  145. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4127")
  146. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4701")
  147. endif()
  148. set(JSON_C_PUBLIC_HEADERS
  149. ${CMAKE_BINARY_DIR}/config.h
  150. ${CMAKE_BINARY_DIR}/json_config.h
  151. ${CMAKE_SOURCE_DIR}/json.h
  152. ${CMAKE_SOURCE_DIR}/arraylist.h
  153. ${CMAKE_SOURCE_DIR}/debug.h
  154. ${CMAKE_SOURCE_DIR}/json_c_version.h
  155. ${CMAKE_SOURCE_DIR}/json_inttypes.h
  156. ${CMAKE_SOURCE_DIR}/json_object.h
  157. ${CMAKE_SOURCE_DIR}/json_object_iterator.h
  158. ${CMAKE_SOURCE_DIR}/json_pointer.h
  159. ${CMAKE_SOURCE_DIR}/json_tokener.h
  160. ${CMAKE_SOURCE_DIR}/json_util.h
  161. ${CMAKE_SOURCE_DIR}/linkhash.h
  162. ${CMAKE_SOURCE_DIR}/printbuf.h
  163. )
  164. set(JSON_C_HEADERS
  165. ${JSON_C_PUBLIC_HEADERS}
  166. ${CMAKE_SOURCE_DIR}/json_object_private.h
  167. ${CMAKE_SOURCE_DIR}/random_seed.h
  168. ${CMAKE_SOURCE_DIR}/strerror_override.h
  169. ${CMAKE_SOURCE_DIR}/strerror_override_private.h
  170. ${CMAKE_SOURCE_DIR}/math_compat.h
  171. ${CMAKE_SOURCE_DIR}/snprintf_compat.h
  172. ${CMAKE_SOURCE_DIR}/strdup_compat.h
  173. ${CMAKE_SOURCE_DIR}/vasprintf_compat.h
  174. )
  175. set(JSON_C_SOURCES
  176. ${CMAKE_SOURCE_DIR}/arraylist.c
  177. ${CMAKE_SOURCE_DIR}/debug.c
  178. ${CMAKE_SOURCE_DIR}/json_c_version.c
  179. ${CMAKE_SOURCE_DIR}/json_object.c
  180. ${CMAKE_SOURCE_DIR}/json_object_iterator.c
  181. ${CMAKE_SOURCE_DIR}/json_pointer.c
  182. ${CMAKE_SOURCE_DIR}/json_tokener.c
  183. ${CMAKE_SOURCE_DIR}/json_util.c
  184. ${CMAKE_SOURCE_DIR}/json_visit.c
  185. ${CMAKE_SOURCE_DIR}/linkhash.c
  186. ${CMAKE_SOURCE_DIR}/printbuf.c
  187. ${CMAKE_SOURCE_DIR}/random_seed.c
  188. ${CMAKE_SOURCE_DIR}/strerror_override.c
  189. )
  190. include_directories(${CMAKE_SOURCE_DIR})
  191. include_directories(${CMAKE_BINARY_DIR})
  192. # If -DBUILD_SHARED_LIBS is set in the CMake command-line, we'll be able to create shared libs, otherwise this would
  193. # generate static libs. Good enough for most use-cases unless there's some serious requirement to create both
  194. # simultaneously.
  195. add_library(${PROJECT_NAME}
  196. ${JSON_C_SOURCES}
  197. ${JSON_C_HEADERS}
  198. )
  199. install(TARGETS ${PROJECT_NAME}
  200. RUNTIME DESTINATION bin
  201. LIBRARY DESTINATION lib
  202. ARCHIVE DESTINATION lib
  203. )
  204. install(FILES ${JSON_C_PUBLIC_HEADERS} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/json-c)