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