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