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.9 kB

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