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 20 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. if (HAVE_STRINGS_H)
  165. check_symbol_exists(strcasecmp "strings.h" HAVE_STRCASECMP)
  166. check_symbol_exists(strncasecmp "strings.h" HAVE_STRNCASECMP)
  167. endif()
  168. if (HAVE_STRING_H)
  169. check_symbol_exists(strdup "string.h" HAVE_STRDUP)
  170. check_symbol_exists(strerror "string.h" HAVE_STRERROR)
  171. endif()
  172. if (HAVE_SYSLOG_H)
  173. check_symbol_exists(vsyslog "syslog.h" HAVE_VSYSLOG)
  174. endif()
  175. if (HAVE_SYS_RANDOM_H)
  176. check_symbol_exists(getrandom "sys/random.h" HAVE_GETRANDOM)
  177. endif()
  178. if (HAVE_SYS_RESOURCE_H)
  179. check_symbol_exists(getrusage "sys/resource.h" HAVE_GETRUSAGE)
  180. endif()
  181. check_symbol_exists(strtoll "stdlib.h" HAVE_STRTOLL)
  182. check_symbol_exists(strtoull "stdlib.h" HAVE_STRTOULL)
  183. set(json_c_strtoll "strtoll")
  184. if (NOT HAVE_STRTOLL)
  185. # Use _strtoi64 if strtoll is not available.
  186. check_symbol_exists(_strtoi64 "stdlib.h" __have_strtoi64)
  187. if (__have_strtoi64)
  188. #set(HAVE_STRTOLL 1)
  189. set(json_c_strtoll "_strtoi64")
  190. endif()
  191. endif()
  192. set(json_c_strtoull "strtoull")
  193. if (NOT HAVE_STRTOULL)
  194. # Use _strtoui64 if strtoull is not available.
  195. check_symbol_exists(_strtoui64 "stdlib.h" __have_strtoui64)
  196. if (__have_strtoui64)
  197. #set(HAVE_STRTOULL 1)
  198. set(json_c_strtoull "_strtoui64")
  199. endif()
  200. endif()
  201. check_type_size(int SIZEOF_INT)
  202. check_type_size(int64_t SIZEOF_INT64_T)
  203. check_type_size(long SIZEOF_LONG)
  204. check_type_size("long long" SIZEOF_LONG_LONG)
  205. check_type_size("size_t" SIZEOF_SIZE_T)
  206. if (MSVC)
  207. list(APPEND CMAKE_EXTRA_INCLUDE_FILES BaseTsd.h)
  208. check_type_size("SSIZE_T" SIZEOF_SSIZE_T)
  209. else()
  210. check_type_size("ssize_t" SIZEOF_SSIZE_T)
  211. endif()
  212. check_c_source_compiles(
  213. "
  214. extern void json_object_get();
  215. __asm__(\".section .gnu.json_object_get\\n\\t.ascii \\\"Please link against libjson-c instead of libjson\\\"\\n\\t.text\");
  216. int main(int c, char *v) { return 0;}
  217. "
  218. HAS_GNU_WARNING_LONG)
  219. check_c_source_compiles(
  220. "int main() { int i, x = 0; i = __sync_add_and_fetch(&x,1); return x; }"
  221. HAVE_ATOMIC_BUILTINS)
  222. if (NOT DISABLE_THREAD_LOCAL_STORAGE)
  223. check_c_source_compiles(
  224. "__thread int x = 0; int main() { return 0; }"
  225. HAVE___THREAD)
  226. if (HAVE___THREAD)
  227. set(SPEC___THREAD __thread)
  228. elseif (MSVC)
  229. set(SPEC___THREAD __declspec(thread))
  230. endif()
  231. endif()
  232. # Hardware random number is not available on Windows? Says, config.h.win32. Best to preserve compatibility.
  233. if (WIN32)
  234. set(ENABLE_RDRAND 0)
  235. endif()
  236. # Once we've done basic symbol/header searches let's add them in.
  237. configure_file(${PROJECT_SOURCE_DIR}/cmake/config.h.in ${PROJECT_BINARY_DIR}/config.h)
  238. message(STATUS "Wrote ${PROJECT_BINARY_DIR}/config.h")
  239. configure_file(${PROJECT_SOURCE_DIR}/cmake/json_config.h.in ${PROJECT_BINARY_DIR}/json_config.h)
  240. message(STATUS "Wrote ${PROJECT_BINARY_DIR}/json_config.h")
  241. if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
  242. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections")
  243. if ("${DISABLE_WERROR}" STREQUAL "OFF")
  244. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
  245. endif()
  246. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
  247. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wcast-qual")
  248. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=deprecated-declarations")
  249. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra")
  250. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wwrite-strings")
  251. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-parameter")
  252. if (NOT WIN32)
  253. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wstrict-prototypes")
  254. endif()
  255. add_definitions(-D_GNU_SOURCE)
  256. elseif ("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
  257. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /DEBUG")
  258. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4100")
  259. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4996")
  260. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4244")
  261. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4706")
  262. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4702")
  263. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4127")
  264. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4701")
  265. endif()
  266. if (NOT ("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC"))
  267. check_c_source_compiles(
  268. "
  269. /* uClibc toolchains without threading barf when _REENTRANT is defined */
  270. #define _REENTRANT 1
  271. #include <sys/types.h>
  272. int main (void)
  273. {
  274. return 0;
  275. }
  276. "
  277. REENTRANT_WORKS
  278. )
  279. if (REENTRANT_WORKS)
  280. add_compile_options("-D_REENTRANT")
  281. endif()
  282. # OSX Mach-O doesn't support linking with '-Bsymbolic-functions'.
  283. # Others may not support it, too.
  284. list(APPEND CMAKE_REQUIRED_LIBRARIES "-Wl,-Bsymbolic-functions")
  285. check_c_source_compiles(
  286. "
  287. int main (void)
  288. {
  289. return 0;
  290. }
  291. "
  292. BSYMBOLIC_WORKS
  293. )
  294. list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "-Wl,-Bsymbolic-functions")
  295. if (DISABLE_BSYMBOLIC STREQUAL "OFF" AND BSYMBOLIC_WORKS)
  296. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-Bsymbolic-functions")
  297. # XXX need cmake>=3.13 for this:
  298. #add_link_options("-Wl,-Bsymbolic-functions")
  299. endif()
  300. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/check-version-script.sym" "TEST { global: *; };")
  301. list(APPEND CMAKE_REQUIRED_LIBRARIES "-Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/check-version-script.sym")
  302. check_c_source_compiles(
  303. "
  304. int main (void)
  305. {
  306. return 0;
  307. }
  308. "
  309. VERSION_SCRIPT_WORKS
  310. )
  311. list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "-Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/check-version-script.sym")
  312. if (VERSION_SCRIPT_WORKS)
  313. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--version-script,${CMAKE_CURRENT_SOURCE_DIR}/json-c.sym")
  314. endif()
  315. endif()
  316. if ($ENV{VALGRIND})
  317. # Build so that valgrind doesn't complain about linkhash.c
  318. add_definitions(-DVALGRIND=1)
  319. endif()
  320. set(JSON_C_PUBLIC_HEADERS
  321. # Note: config.h is _not_ included here
  322. ${PROJECT_BINARY_DIR}/json_config.h
  323. ${PROJECT_BINARY_DIR}/json.h
  324. ${PROJECT_SOURCE_DIR}/arraylist.h
  325. ${PROJECT_SOURCE_DIR}/debug.h
  326. ${PROJECT_SOURCE_DIR}/json_c_version.h
  327. ${PROJECT_SOURCE_DIR}/json_inttypes.h
  328. ${PROJECT_SOURCE_DIR}/json_object.h
  329. ${PROJECT_SOURCE_DIR}/json_object_iterator.h
  330. ${PROJECT_SOURCE_DIR}/json_tokener.h
  331. ${PROJECT_SOURCE_DIR}/json_types.h
  332. ${PROJECT_SOURCE_DIR}/json_util.h
  333. ${PROJECT_SOURCE_DIR}/json_visit.h
  334. ${PROJECT_SOURCE_DIR}/linkhash.h
  335. ${PROJECT_SOURCE_DIR}/printbuf.h
  336. )
  337. set(JSON_C_HEADERS
  338. ${JSON_C_PUBLIC_HEADERS}
  339. ${PROJECT_SOURCE_DIR}/json_object_private.h
  340. ${PROJECT_SOURCE_DIR}/random_seed.h
  341. ${PROJECT_SOURCE_DIR}/strerror_override.h
  342. ${PROJECT_SOURCE_DIR}/strerror_override_private.h
  343. ${PROJECT_SOURCE_DIR}/math_compat.h
  344. ${PROJECT_SOURCE_DIR}/snprintf_compat.h
  345. ${PROJECT_SOURCE_DIR}/strdup_compat.h
  346. ${PROJECT_SOURCE_DIR}/vasprintf_compat.h
  347. )
  348. set(JSON_C_SOURCES
  349. ${PROJECT_SOURCE_DIR}/arraylist.c
  350. ${PROJECT_SOURCE_DIR}/debug.c
  351. ${PROJECT_SOURCE_DIR}/json_c_version.c
  352. ${PROJECT_SOURCE_DIR}/json_object.c
  353. ${PROJECT_SOURCE_DIR}/json_object_iterator.c
  354. ${PROJECT_SOURCE_DIR}/json_tokener.c
  355. ${PROJECT_SOURCE_DIR}/json_util.c
  356. ${PROJECT_SOURCE_DIR}/json_visit.c
  357. ${PROJECT_SOURCE_DIR}/linkhash.c
  358. ${PROJECT_SOURCE_DIR}/printbuf.c
  359. ${PROJECT_SOURCE_DIR}/random_seed.c
  360. ${PROJECT_SOURCE_DIR}/strerror_override.c
  361. )
  362. if (NOT DISABLE_JSON_POINTER)
  363. set(JSON_C_PUBLIC_HEADERS ${JSON_C_PUBLIC_HEADERS} ${PROJECT_SOURCE_DIR}/json_pointer.h)
  364. set(JSON_C_SOURCES ${JSON_C_SOURCES} ${PROJECT_SOURCE_DIR}/json_pointer.c)
  365. set(JSON_H_JSON_POINTER "#include \"json_pointer.h\"")
  366. else()
  367. set(JSON_H_JSON_POINTER "")
  368. endif()
  369. configure_file(json.h.cmakein ${PROJECT_BINARY_DIR}/json.h @ONLY)
  370. include_directories(${PROJECT_SOURCE_DIR})
  371. include_directories(${PROJECT_BINARY_DIR})
  372. add_subdirectory(doc)
  373. # "uninstall" custom target for make generators in unix like operating systems
  374. # and if that target is not present
  375. if (CMAKE_GENERATOR STREQUAL "Unix Makefiles")
  376. if(NOT TARGET uninstall)
  377. add_custom_target(uninstall
  378. COMMAND cat ${PROJECT_BINARY_DIR}/install_manifest.txt | xargs rm
  379. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  380. )
  381. endif()
  382. endif()
  383. # XXX for a normal full distribution we'll need to figure out
  384. # XXX how to build both shared and static libraries.
  385. # Probably leverage that to build a local VALGRIND=1 library for testing too.
  386. add_library(${PROJECT_NAME}
  387. ${JSON_C_SOURCES}
  388. ${JSON_C_HEADERS}
  389. )
  390. set_target_properties(${PROJECT_NAME} PROPERTIES
  391. VERSION 5.2.0
  392. SOVERSION 5)
  393. list(APPEND CMAKE_TARGETS ${PROJECT_NAME})
  394. # If json-c is used as subroject it set to target correct interface -I flags and allow
  395. # to build external target without extra include_directories(...)
  396. target_include_directories(${PROJECT_NAME}
  397. PUBLIC
  398. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
  399. $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
  400. )
  401. target_link_libraries(${PROJECT_NAME} PUBLIC ${CMAKE_REQUIRED_LIBRARIES})
  402. # Allow to build static and shared libraries at the same time
  403. if (BUILD_STATIC_LIBS AND BUILD_SHARED_LIBS)
  404. set(STATIC_LIB ${PROJECT_NAME}-static)
  405. add_library(${STATIC_LIB} STATIC
  406. ${JSON_C_SOURCES}
  407. ${JSON_C_HEADERS}
  408. )
  409. target_include_directories(${PROJECT_NAME}-static
  410. PUBLIC
  411. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
  412. $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
  413. )
  414. target_link_libraries(${PROJECT_NAME}-static PUBLIC ${CMAKE_REQUIRED_LIBRARIES})
  415. # rename the static library
  416. if (NOT MSVC)
  417. set_target_properties(${STATIC_LIB} PROPERTIES
  418. OUTPUT_NAME ${PROJECT_NAME}
  419. )
  420. endif()
  421. list(APPEND CMAKE_TARGETS ${STATIC_LIB})
  422. endif ()
  423. # Always create new install dirs with 0755 permissions, regardless of umask
  424. set(CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS
  425. OWNER_READ
  426. OWNER_WRITE
  427. OWNER_EXECUTE
  428. GROUP_READ
  429. GROUP_EXECUTE
  430. WORLD_READ
  431. WORLD_EXECUTE
  432. )
  433. install(TARGETS ${CMAKE_TARGETS}
  434. EXPORT ${PROJECT_NAME}-targets
  435. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  436. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  437. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  438. INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ${CMAKE_INSTALL_INCLUDEDIR}/json-c
  439. )
  440. install(EXPORT ${PROJECT_NAME}-targets
  441. FILE ${PROJECT_NAME}-targets.cmake
  442. NAMESPACE ${PROJECT_NAME}::
  443. DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
  444. )
  445. configure_package_config_file(
  446. "cmake/Config.cmake.in"
  447. ${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake
  448. INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
  449. )
  450. install(
  451. FILES ${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake
  452. DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
  453. )
  454. if (UNIX OR MINGW OR CYGWIN)
  455. SET(prefix ${CMAKE_INSTALL_PREFIX})
  456. # exec_prefix is prefix by default and CMake does not have the
  457. # concept.
  458. SET(exec_prefix ${CMAKE_INSTALL_PREFIX})
  459. SET(libdir ${CMAKE_INSTALL_FULL_LIBDIR})
  460. SET(includedir ${CMAKE_INSTALL_FULL_INCLUDEDIR})
  461. SET(VERSION ${PROJECT_VERSION})
  462. # Linking against the static json-c requires
  463. # dependent packages to include additional libs:
  464. SET(LIBS_LIST ${CMAKE_REQUIRED_LIBRARIES})
  465. # Note: We would need cmake >= 3.12 in order to use list(TRANSFORM ...)
  466. function(list_transform_prepend var prefix)
  467. set(temp "")
  468. foreach(f ${${var}})
  469. list(APPEND temp "${prefix}${f}")
  470. endforeach()
  471. set(${var} "${temp}" PARENT_SCOPE)
  472. endfunction()
  473. list_transform_prepend(LIBS_LIST "-l")
  474. string(REPLACE ";" " " LIBS "${LIBS_LIST}")
  475. configure_file(json-c.pc.in json-c.pc @ONLY)
  476. set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_LIBDIR}/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")
  477. install(FILES ${PROJECT_BINARY_DIR}/json-c.pc DESTINATION "${INSTALL_PKGCONFIG_DIR}")
  478. endif ()
  479. install(FILES ${JSON_C_PUBLIC_HEADERS} DESTINATION ${CMAKE_INSTALL_FULL_INCLUDEDIR}/json-c)
  480. if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING AND
  481. (NOT MSVC OR NOT (MSVC_VERSION LESS 1800)) # Tests need at least VS2013
  482. )
  483. add_subdirectory(tests)
  484. endif()
  485. if (NOT MSVC) # cmd line apps don't built on Windows currently.
  486. add_subdirectory(apps)
  487. endif()