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 21 kB

9 years ago
5 years ago
9 years ago
5 years ago
9 years ago
build: Add symbol versions to all exported symbols With this version script, newly-linked binaries that depend on the json-c shared library will refer to its symbols in a versioned form, preventing their references from being resolved to a symbol of the same name exported by json-glib or libjansson if those libraries appear in dependency search order before json-c, which will usually result in a crash. This is necessary because ELF symbol resolution normally uses a single flat namespace, not a tree like Windows symbol resolution. At least one symbol (json_object_iter_next()) is exported by all three JSON libraries. Linking with -Bsymbolic is not enough to have this effect in all cases, because -Bsymbolic only affects symbol lookup within a shared object, for example when json_object_set_serializer() calls json_object_set_userdata(). It does not affect calls from external code into json-c, unless json-c was statically linked into the external caller. This change will also not prevent code that depends on json-glib or libjansson from finding json-c's symbols and crashing; to prevent that, a corresponding change in json-glib or libjansson would be needed. Adding a symbol-version is a backwards-compatible change, but once added, removing or changing the symbol-version on a symbol would be an incompatible change that requires a SONAME bump. Resolves: https://github.com/json-c/json-c/issues/621 (when combined with an equivalent change to libjansson). Signed-off-by: Simon McVittie <smcv@collabora.com>
5 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. if(POLICY CMP0048)
  5. cmake_policy(SET CMP0048 NEW)
  6. endif()
  7. # JSON-C library is C only project.
  8. if (CMAKE_VERSION VERSION_LESS 3.0)
  9. project(json-c)
  10. set(PROJECT_VERSION_MAJOR "0")
  11. set(PROJECT_VERSION_MINOR "16")
  12. set(PROJECT_VERSION_PATCH "99")
  13. set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
  14. else()
  15. project(json-c LANGUAGES C VERSION 0.16.99)
  16. endif()
  17. # If we've got 3.0 then it's good, let's provide support. Otherwise, leave it be.
  18. if(POLICY CMP0038)
  19. # Policy CMP0038 introduced was in CMake 3.0
  20. cmake_policy(SET CMP0038 NEW)
  21. endif()
  22. if(POLICY CMP0054)
  23. cmake_policy(SET CMP0054 NEW)
  24. endif()
  25. # set default build type if not specified by user
  26. if(NOT CMAKE_BUILD_TYPE)
  27. set(CMAKE_BUILD_TYPE debug)
  28. endif()
  29. set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2")
  30. # Include file check macros honor CMAKE_REQUIRED_LIBRARIES
  31. # i.e. the check_include_file() calls will include -lm when checking.
  32. if(POLICY CMP0075)
  33. cmake_policy(SET CMP0075 NEW)
  34. endif()
  35. include(CTest)
  36. # Set some packaging variables.
  37. set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
  38. set(CPACK_PACKAGE_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}")
  39. set(CPACK_PACKAGE_VERSION_MINOR "${PROJECT_VERSION_MINOR}")
  40. set(CPACK_PACKAGE_VERSION_PATCH "${PROJECT_VERSION_PATCH}")
  41. set(JSON_C_BUGREPORT "json-c@googlegroups.com")
  42. set(CPACK_SOURCE_IGNORE_FILES
  43. ${PROJECT_SOURCE_DIR}/build
  44. ${PROJECT_SOURCE_DIR}/cmake-build-debug
  45. ${PROJECT_SOURCE_DIR}/pack
  46. ${PROJECT_SOURCE_DIR}/.idea
  47. ${PROJECT_SOURCE_DIR}/.DS_Store
  48. ${PROJECT_SOURCE_DIR}/.git
  49. ${PROJECT_SOURCE_DIR}/.vscode)
  50. include(CheckSymbolExists)
  51. include(CheckIncludeFile)
  52. include(CheckIncludeFiles)
  53. include(CheckCSourceCompiles)
  54. include(CheckTypeSize)
  55. include(CPack)
  56. include(GNUInstallDirs)
  57. include(CMakePackageConfigHelpers)
  58. option(BUILD_SHARED_LIBS "Default to building shared libraries" ON)
  59. option(BUILD_STATIC_LIBS "Default to building static libraries" ON)
  60. if (BUILD_SHARED_LIBS)
  61. add_definitions(-D JSON_C_DLL)
  62. endif()
  63. # Generate a release merge and test it to verify the correctness of republishing the package.
  64. ADD_CUSTOM_TARGET(distcheck
  65. COMMAND make package_source
  66. COMMAND tar -xvf "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source.tar.gz"
  67. COMMAND mkdir "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source/build"
  68. COMMAND cmake "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source/" -B"./${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source/build/"
  69. COMMAND make -C "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source/build"
  70. COMMAND make test -C "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source/build"
  71. COMMAND rm -rf "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source"
  72. )
  73. # Enable or disable features. By default, all features are turned off.
  74. option(DISABLE_BSYMBOLIC "Avoid linking with -Bsymbolic-function." OFF)
  75. option(DISABLE_THREAD_LOCAL_STORAGE "Disable using Thread-Local Storage (HAVE___THREAD)." OFF)
  76. option(DISABLE_WERROR "Avoid treating compiler warnings as fatal errors." OFF)
  77. option(ENABLE_RDRAND "Enable RDRAND Hardware RNG Hash Seed." OFF)
  78. option(ENABLE_THREADING "Enable partial threading support." OFF)
  79. option(OVERRIDE_GET_RANDOM_SEED "Override json_c_get_random_seed() with custom code." OFF)
  80. option(DISABLE_EXTRA_LIBS "Avoid linking against extra libraries, such as libbsd." OFF)
  81. option(DISABLE_JSON_POINTER "Disable JSON pointer (RFC6901) support." OFF)
  82. if (UNIX OR MINGW OR CYGWIN)
  83. list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
  84. endif()
  85. if (UNIX)
  86. list(APPEND CMAKE_REQUIRED_LIBRARIES m)
  87. endif()
  88. if (MSVC)
  89. list(APPEND CMAKE_REQUIRED_DEFINITIONS /D_CRT_SECURE_NO_DEPRECATE)
  90. list(APPEND CMAKE_REQUIRED_FLAGS /wd4996)
  91. endif()
  92. if (NOT DISABLE_STATIC_FPIC)
  93. # Use '-fPIC'/'-fPIE' option.
  94. # This will allow other libraries to statically link in libjson-c.a
  95. # which in turn prevents crashes in downstream apps that may use
  96. # a different JSON library with identical symbol names.
  97. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  98. endif()
  99. check_include_file("fcntl.h" HAVE_FCNTL_H)
  100. check_include_file("inttypes.h" HAVE_INTTYPES_H)
  101. check_include_file(stdarg.h HAVE_STDARG_H)
  102. check_include_file(strings.h HAVE_STRINGS_H)
  103. check_include_file(string.h HAVE_STRING_H)
  104. check_include_file(syslog.h HAVE_SYSLOG_H)
  105. check_include_files("stdlib.h;stdarg.h;string.h;float.h" STDC_HEADERS)
  106. check_include_file(unistd.h HAVE_UNISTD_H)
  107. check_include_file(sys/types.h HAVE_SYS_TYPES_H)
  108. check_include_file(sys/resource.h HAVE_SYS_RESOURCE_H) # for getrusage
  109. check_include_file("dlfcn.h" HAVE_DLFCN_H)
  110. check_include_file("endian.h" HAVE_ENDIAN_H)
  111. check_include_file("limits.h" HAVE_LIMITS_H)
  112. check_include_file("locale.h" HAVE_LOCALE_H)
  113. check_include_file("memory.h" HAVE_MEMORY_H)
  114. check_include_file(stdint.h HAVE_STDINT_H)
  115. check_include_file(stdlib.h HAVE_STDLIB_H)
  116. check_include_file(sys/cdefs.h HAVE_SYS_CDEFS_H)
  117. check_include_file(sys/param.h HAVE_SYS_PARAM_H)
  118. check_include_file(sys/random.h HAVE_SYS_RANDOM_H)
  119. check_include_file(sys/stat.h HAVE_SYS_STAT_H)
  120. check_include_file(xlocale.h HAVE_XLOCALE_H)
  121. if (HAVE_INTTYPES_H)
  122. # Set a json-c specific var to stamp into json_config.h
  123. # in a way that hopefully won't conflict with other
  124. # projects that use json-c.
  125. set(JSON_C_HAVE_INTTYPES_H 1)
  126. endif()
  127. check_symbol_exists(_isnan "float.h" HAVE_DECL__ISNAN)
  128. check_symbol_exists(_finite "float.h" HAVE_DECL__FINITE)
  129. if ((MSVC AND NOT (MSVC_VERSION LESS 1800)) OR MINGW OR CYGWIN OR UNIX)
  130. check_symbol_exists(INFINITY "math.h" HAVE_DECL_INFINITY)
  131. check_symbol_exists(isinf "math.h" HAVE_DECL_ISINF)
  132. check_symbol_exists(isnan "math.h" HAVE_DECL_ISNAN)
  133. check_symbol_exists(nan "math.h" HAVE_DECL_NAN)
  134. endif()
  135. check_symbol_exists(_doprnt "stdio.h" HAVE_DOPRNT)
  136. if (UNIX OR MINGW OR CYGWIN)
  137. check_symbol_exists(snprintf "stdio.h" HAVE_SNPRINTF)
  138. endif()
  139. check_symbol_exists(vasprintf "stdio.h" HAVE_VASPRINTF)
  140. check_symbol_exists(vsnprintf "stdio.h" HAVE_VSNPRINTF)
  141. check_symbol_exists(vprintf "stdio.h" HAVE_VPRINTF)
  142. check_symbol_exists(arc4random "stdlib.h" HAVE_ARC4RANDOM)
  143. if (NOT HAVE_ARC4RANDOM AND DISABLE_EXTRA_LIBS STREQUAL "OFF")
  144. check_include_file(bsd/stdlib.h HAVE_BSD_STDLIB_H)
  145. if (HAVE_BSD_STDLIB_H)
  146. list(APPEND CMAKE_REQUIRED_LIBRARIES "bsd")
  147. unset(HAVE_ARC4RANDOM CACHE)
  148. check_symbol_exists(arc4random "bsd/stdlib.h" HAVE_ARC4RANDOM)
  149. if (NOT HAVE_ARC4RANDOM)
  150. list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "bsd")
  151. endif()
  152. endif()
  153. endif()
  154. if (HAVE_FCNTL_H)
  155. check_symbol_exists(open "fcntl.h" HAVE_OPEN)
  156. endif()
  157. if (HAVE_STDLIB_H)
  158. check_symbol_exists(realloc "stdlib.h" HAVE_REALLOC)
  159. endif()
  160. if (HAVE_LOCALE_H)
  161. check_symbol_exists(setlocale "locale.h" HAVE_SETLOCALE)
  162. check_symbol_exists(uselocale "locale.h" HAVE_USELOCALE)
  163. endif()
  164. # uClibc *intentionally* crashes in duplocale(), at least as of:
  165. # https://github.com/ffainelli/uClibc/blob/266bdc1/libc/misc/locale/locale.c#L1322
  166. # So, if it looks like we're compiling for a system like that just disable
  167. # locale handling entirely.
  168. exec_program(${CMAKE_C_COMPILER} ARGS -dumpmachine OUTPUT_VARIABLE CMAKE_GNU_C_MACHINE)
  169. if (CMAKE_GNU_C_MACHINE MATCHES "uclibc")
  170. message(STATUS "Detected uClibc compiler, disabling locale handling")
  171. set(HAVE_SETLOCALE 0)
  172. set(HAVE_USELOCALE 0)
  173. endif()
  174. if (HAVE_STRINGS_H)
  175. check_symbol_exists(strcasecmp "strings.h" HAVE_STRCASECMP)
  176. check_symbol_exists(strncasecmp "strings.h" HAVE_STRNCASECMP)
  177. endif()
  178. if (HAVE_STRING_H)
  179. check_symbol_exists(strdup "string.h" HAVE_STRDUP)
  180. check_symbol_exists(strerror "string.h" HAVE_STRERROR)
  181. endif()
  182. if (HAVE_SYSLOG_H)
  183. check_symbol_exists(vsyslog "syslog.h" HAVE_VSYSLOG)
  184. endif()
  185. if (HAVE_SYS_RANDOM_H)
  186. check_symbol_exists(getrandom "sys/random.h" HAVE_GETRANDOM)
  187. endif()
  188. if (HAVE_SYS_RESOURCE_H)
  189. check_symbol_exists(getrusage "sys/resource.h" HAVE_GETRUSAGE)
  190. endif()
  191. check_symbol_exists(strtoll "stdlib.h" HAVE_STRTOLL)
  192. check_symbol_exists(strtoull "stdlib.h" HAVE_STRTOULL)
  193. set(json_c_strtoll "strtoll")
  194. if (NOT HAVE_STRTOLL)
  195. # Use _strtoi64 if strtoll is not available.
  196. check_symbol_exists(_strtoi64 "stdlib.h" __have_strtoi64)
  197. if (__have_strtoi64)
  198. #set(HAVE_STRTOLL 1)
  199. set(json_c_strtoll "_strtoi64")
  200. endif()
  201. endif()
  202. set(json_c_strtoull "strtoull")
  203. if (NOT HAVE_STRTOULL)
  204. # Use _strtoui64 if strtoull is not available.
  205. check_symbol_exists(_strtoui64 "stdlib.h" __have_strtoui64)
  206. if (__have_strtoui64)
  207. #set(HAVE_STRTOULL 1)
  208. set(json_c_strtoull "_strtoui64")
  209. endif()
  210. endif()
  211. check_type_size(int SIZEOF_INT)
  212. check_type_size(int64_t SIZEOF_INT64_T)
  213. check_type_size(long SIZEOF_LONG)
  214. check_type_size("long long" SIZEOF_LONG_LONG)
  215. check_type_size("size_t" SIZEOF_SIZE_T)
  216. if (MSVC)
  217. list(APPEND CMAKE_EXTRA_INCLUDE_FILES BaseTsd.h)
  218. check_type_size("SSIZE_T" SIZEOF_SSIZE_T)
  219. else()
  220. check_type_size("ssize_t" SIZEOF_SSIZE_T)
  221. endif()
  222. check_c_source_compiles(
  223. "
  224. extern void json_object_get();
  225. __asm__(\".section .gnu.json_object_get\\n\\t.ascii \\\"Please link against libjson-c instead of libjson\\\"\\n\\t.text\");
  226. int main(int c, char *v) { return 0;}
  227. "
  228. HAS_GNU_WARNING_LONG)
  229. check_c_source_compiles(
  230. "int main() { int i, x = 0; i = __sync_add_and_fetch(&x,1); return x; }"
  231. HAVE_ATOMIC_BUILTINS)
  232. if (NOT DISABLE_THREAD_LOCAL_STORAGE)
  233. check_c_source_compiles(
  234. "__thread int x = 0; int main() { return 0; }"
  235. HAVE___THREAD)
  236. if (HAVE___THREAD)
  237. set(SPEC___THREAD __thread)
  238. elseif (MSVC)
  239. set(SPEC___THREAD __declspec(thread))
  240. endif()
  241. endif()
  242. # Hardware random number is not available on Windows? Says, config.h.win32. Best to preserve compatibility.
  243. if (WIN32)
  244. set(ENABLE_RDRAND 0)
  245. endif()
  246. # Once we've done basic symbol/header searches let's add them in.
  247. configure_file(${PROJECT_SOURCE_DIR}/cmake/config.h.in ${PROJECT_BINARY_DIR}/config.h)
  248. message(STATUS "Wrote ${PROJECT_BINARY_DIR}/config.h")
  249. configure_file(${PROJECT_SOURCE_DIR}/cmake/json_config.h.in ${PROJECT_BINARY_DIR}/json_config.h)
  250. message(STATUS "Wrote ${PROJECT_BINARY_DIR}/json_config.h")
  251. if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
  252. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections")
  253. if ("${DISABLE_WERROR}" STREQUAL "OFF")
  254. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
  255. endif()
  256. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
  257. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wcast-qual")
  258. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=deprecated-declarations")
  259. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra")
  260. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wwrite-strings")
  261. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-parameter")
  262. if (NOT WIN32)
  263. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wstrict-prototypes")
  264. endif()
  265. add_definitions(-D_GNU_SOURCE)
  266. elseif ("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
  267. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /DEBUG")
  268. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4100")
  269. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4996")
  270. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4244")
  271. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4706")
  272. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4702")
  273. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4127")
  274. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4701")
  275. endif()
  276. if (NOT ("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC"))
  277. check_c_source_compiles(
  278. "
  279. /* uClibc toolchains without threading barf when _REENTRANT is defined */
  280. #define _REENTRANT 1
  281. #include <sys/types.h>
  282. int main (void)
  283. {
  284. return 0;
  285. }
  286. "
  287. REENTRANT_WORKS
  288. )
  289. if (REENTRANT_WORKS)
  290. add_compile_options("-D_REENTRANT")
  291. endif()
  292. # OSX Mach-O doesn't support linking with '-Bsymbolic-functions'.
  293. # Others may not support it, too.
  294. list(APPEND CMAKE_REQUIRED_LIBRARIES "-Wl,-Bsymbolic-functions")
  295. check_c_source_compiles(
  296. "
  297. int main (void)
  298. {
  299. return 0;
  300. }
  301. "
  302. BSYMBOLIC_WORKS
  303. )
  304. list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "-Wl,-Bsymbolic-functions")
  305. if (DISABLE_BSYMBOLIC STREQUAL "OFF" AND BSYMBOLIC_WORKS)
  306. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-Bsymbolic-functions")
  307. # XXX need cmake>=3.13 for this:
  308. #add_link_options("-Wl,-Bsymbolic-functions")
  309. endif()
  310. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/check-version-script.sym" "TEST { global: *; };")
  311. list(APPEND CMAKE_REQUIRED_LIBRARIES "-Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/check-version-script.sym")
  312. check_c_source_compiles(
  313. "
  314. int main (void)
  315. {
  316. return 0;
  317. }
  318. "
  319. VERSION_SCRIPT_WORKS
  320. )
  321. list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "-Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/check-version-script.sym")
  322. if (VERSION_SCRIPT_WORKS)
  323. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--version-script,${CMAKE_CURRENT_SOURCE_DIR}/json-c.sym")
  324. endif()
  325. endif()
  326. if ($ENV{VALGRIND})
  327. # Build so that valgrind doesn't complain about linkhash.c
  328. add_definitions(-DVALGRIND=1)
  329. endif()
  330. set(JSON_C_PUBLIC_HEADERS
  331. # Note: config.h is _not_ included here
  332. ${PROJECT_BINARY_DIR}/json_config.h
  333. ${PROJECT_BINARY_DIR}/json.h
  334. ${PROJECT_SOURCE_DIR}/arraylist.h
  335. ${PROJECT_SOURCE_DIR}/debug.h
  336. ${PROJECT_SOURCE_DIR}/json_c_version.h
  337. ${PROJECT_SOURCE_DIR}/json_inttypes.h
  338. ${PROJECT_SOURCE_DIR}/json_object.h
  339. ${PROJECT_SOURCE_DIR}/json_object_iterator.h
  340. ${PROJECT_SOURCE_DIR}/json_tokener.h
  341. ${PROJECT_SOURCE_DIR}/json_types.h
  342. ${PROJECT_SOURCE_DIR}/json_util.h
  343. ${PROJECT_SOURCE_DIR}/json_visit.h
  344. ${PROJECT_SOURCE_DIR}/linkhash.h
  345. ${PROJECT_SOURCE_DIR}/printbuf.h
  346. )
  347. set(JSON_C_HEADERS
  348. ${JSON_C_PUBLIC_HEADERS}
  349. ${PROJECT_SOURCE_DIR}/json_object_private.h
  350. ${PROJECT_SOURCE_DIR}/random_seed.h
  351. ${PROJECT_SOURCE_DIR}/strerror_override.h
  352. ${PROJECT_SOURCE_DIR}/strerror_override_private.h
  353. ${PROJECT_SOURCE_DIR}/math_compat.h
  354. ${PROJECT_SOURCE_DIR}/snprintf_compat.h
  355. ${PROJECT_SOURCE_DIR}/strdup_compat.h
  356. ${PROJECT_SOURCE_DIR}/vasprintf_compat.h
  357. )
  358. set(JSON_C_SOURCES
  359. ${PROJECT_SOURCE_DIR}/arraylist.c
  360. ${PROJECT_SOURCE_DIR}/debug.c
  361. ${PROJECT_SOURCE_DIR}/json_c_version.c
  362. ${PROJECT_SOURCE_DIR}/json_object.c
  363. ${PROJECT_SOURCE_DIR}/json_object_iterator.c
  364. ${PROJECT_SOURCE_DIR}/json_tokener.c
  365. ${PROJECT_SOURCE_DIR}/json_util.c
  366. ${PROJECT_SOURCE_DIR}/json_visit.c
  367. ${PROJECT_SOURCE_DIR}/linkhash.c
  368. ${PROJECT_SOURCE_DIR}/printbuf.c
  369. ${PROJECT_SOURCE_DIR}/random_seed.c
  370. ${PROJECT_SOURCE_DIR}/strerror_override.c
  371. )
  372. if (NOT DISABLE_JSON_POINTER)
  373. set(JSON_C_PUBLIC_HEADERS ${JSON_C_PUBLIC_HEADERS} ${PROJECT_SOURCE_DIR}/json_pointer.h)
  374. set(JSON_C_SOURCES ${JSON_C_SOURCES} ${PROJECT_SOURCE_DIR}/json_pointer.c)
  375. set(JSON_H_JSON_POINTER "#include \"json_pointer.h\"")
  376. else()
  377. set(JSON_H_JSON_POINTER "")
  378. endif()
  379. configure_file(json.h.cmakein ${PROJECT_BINARY_DIR}/json.h @ONLY)
  380. include_directories(${PROJECT_SOURCE_DIR})
  381. include_directories(${PROJECT_BINARY_DIR})
  382. add_subdirectory(doc)
  383. # "uninstall" custom target for make generators in unix like operating systems
  384. # and if that target is not present
  385. if (CMAKE_GENERATOR STREQUAL "Unix Makefiles")
  386. if(NOT TARGET uninstall)
  387. add_custom_target(uninstall
  388. COMMAND cat ${PROJECT_BINARY_DIR}/install_manifest.txt | xargs rm
  389. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  390. )
  391. endif()
  392. endif()
  393. # XXX for a normal full distribution we'll need to figure out
  394. # XXX how to build both shared and static libraries.
  395. # Probably leverage that to build a local VALGRIND=1 library for testing too.
  396. add_library(${PROJECT_NAME}
  397. ${JSON_C_SOURCES}
  398. ${JSON_C_HEADERS}
  399. )
  400. set_target_properties(${PROJECT_NAME} PROPERTIES
  401. VERSION 5.2.0
  402. SOVERSION 5)
  403. list(APPEND CMAKE_TARGETS ${PROJECT_NAME})
  404. # If json-c is used as subroject it set to target correct interface -I flags and allow
  405. # to build external target without extra include_directories(...)
  406. target_include_directories(${PROJECT_NAME}
  407. PUBLIC
  408. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
  409. $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
  410. )
  411. target_link_libraries(${PROJECT_NAME} PUBLIC ${CMAKE_REQUIRED_LIBRARIES})
  412. # Allow to build static and shared libraries at the same time
  413. if (BUILD_STATIC_LIBS AND BUILD_SHARED_LIBS)
  414. set(STATIC_LIB ${PROJECT_NAME}-static)
  415. add_library(${STATIC_LIB} STATIC
  416. ${JSON_C_SOURCES}
  417. ${JSON_C_HEADERS}
  418. )
  419. target_include_directories(${PROJECT_NAME}-static
  420. PUBLIC
  421. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
  422. $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
  423. )
  424. target_link_libraries(${PROJECT_NAME}-static PUBLIC ${CMAKE_REQUIRED_LIBRARIES})
  425. # rename the static library
  426. if (NOT MSVC)
  427. set_target_properties(${STATIC_LIB} PROPERTIES
  428. OUTPUT_NAME ${PROJECT_NAME}
  429. )
  430. endif()
  431. list(APPEND CMAKE_TARGETS ${STATIC_LIB})
  432. endif ()
  433. # Always create new install dirs with 0755 permissions, regardless of umask
  434. set(CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS
  435. OWNER_READ
  436. OWNER_WRITE
  437. OWNER_EXECUTE
  438. GROUP_READ
  439. GROUP_EXECUTE
  440. WORLD_READ
  441. WORLD_EXECUTE
  442. )
  443. install(TARGETS ${CMAKE_TARGETS}
  444. EXPORT ${PROJECT_NAME}-targets
  445. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  446. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  447. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  448. INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ${CMAKE_INSTALL_INCLUDEDIR}/json-c
  449. )
  450. install(EXPORT ${PROJECT_NAME}-targets
  451. FILE ${PROJECT_NAME}-targets.cmake
  452. NAMESPACE ${PROJECT_NAME}::
  453. DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
  454. )
  455. configure_package_config_file(
  456. "cmake/Config.cmake.in"
  457. ${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake
  458. INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
  459. )
  460. install(
  461. FILES ${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake
  462. DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
  463. )
  464. if (UNIX OR MINGW OR CYGWIN)
  465. SET(prefix ${CMAKE_INSTALL_PREFIX})
  466. # exec_prefix is prefix by default and CMake does not have the
  467. # concept.
  468. SET(exec_prefix ${CMAKE_INSTALL_PREFIX})
  469. SET(libdir ${CMAKE_INSTALL_FULL_LIBDIR})
  470. SET(includedir ${CMAKE_INSTALL_FULL_INCLUDEDIR})
  471. SET(VERSION ${PROJECT_VERSION})
  472. # Linking against the static json-c requires
  473. # dependent packages to include additional libs:
  474. SET(LIBS_LIST ${CMAKE_REQUIRED_LIBRARIES})
  475. # Note: We would need cmake >= 3.12 in order to use list(TRANSFORM ...)
  476. function(list_transform_prepend var prefix)
  477. set(temp "")
  478. foreach(f ${${var}})
  479. list(APPEND temp "${prefix}${f}")
  480. endforeach()
  481. set(${var} "${temp}" PARENT_SCOPE)
  482. endfunction()
  483. list_transform_prepend(LIBS_LIST "-l")
  484. string(REPLACE ";" " " LIBS "${LIBS_LIST}")
  485. configure_file(json-c.pc.in json-c.pc @ONLY)
  486. set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_LIBDIR}/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")
  487. install(FILES ${PROJECT_BINARY_DIR}/json-c.pc DESTINATION "${INSTALL_PKGCONFIG_DIR}")
  488. endif ()
  489. install(FILES ${JSON_C_PUBLIC_HEADERS} DESTINATION ${CMAKE_INSTALL_FULL_INCLUDEDIR}/json-c)
  490. if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING AND
  491. (NOT MSVC OR NOT (MSVC_VERSION LESS 1800)) # Tests need at least VS2013
  492. )
  493. add_subdirectory(tests)
  494. endif()
  495. if (NOT MSVC) # cmd line apps don't built on Windows currently.
  496. add_subdirectory(apps)
  497. endif()