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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. option(BUILD_APPS "Default to building apps" ON)
  80. if (UNIX OR MINGW OR CYGWIN)
  81. list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
  82. endif()
  83. if (UNIX)
  84. list(APPEND CMAKE_REQUIRED_LIBRARIES m)
  85. endif()
  86. if (MSVC)
  87. list(APPEND CMAKE_REQUIRED_DEFINITIONS /D_CRT_SECURE_NO_DEPRECATE)
  88. list(APPEND CMAKE_REQUIRED_FLAGS /wd4996)
  89. endif()
  90. if (NOT DISABLE_STATIC_FPIC)
  91. # Use '-fPIC'/'-fPIE' option.
  92. # This will allow other libraries to statically link in libjson-c.a
  93. # which in turn prevents crashes in downstream apps that may use
  94. # a different JSON library with identical symbol names.
  95. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  96. endif()
  97. check_include_file("fcntl.h" HAVE_FCNTL_H)
  98. check_include_file("inttypes.h" HAVE_INTTYPES_H)
  99. check_include_file(stdarg.h HAVE_STDARG_H)
  100. check_include_file(strings.h HAVE_STRINGS_H)
  101. check_include_file(string.h HAVE_STRING_H)
  102. check_include_file(syslog.h HAVE_SYSLOG_H)
  103. check_include_files("stdlib.h;stdarg.h;string.h;float.h" STDC_HEADERS)
  104. check_include_file(unistd.h HAVE_UNISTD_H)
  105. check_include_file(sys/types.h HAVE_SYS_TYPES_H)
  106. check_include_file(sys/resource.h HAVE_SYS_RESOURCE_H) # for getrusage
  107. check_include_file("dlfcn.h" HAVE_DLFCN_H)
  108. check_include_file("endian.h" HAVE_ENDIAN_H)
  109. check_include_file("limits.h" HAVE_LIMITS_H)
  110. check_include_file("locale.h" HAVE_LOCALE_H)
  111. check_include_file("memory.h" HAVE_MEMORY_H)
  112. check_include_file(stdint.h HAVE_STDINT_H)
  113. check_include_file(stdlib.h HAVE_STDLIB_H)
  114. check_include_file(sys/cdefs.h HAVE_SYS_CDEFS_H)
  115. check_include_file(sys/param.h HAVE_SYS_PARAM_H)
  116. check_include_file(sys/random.h HAVE_SYS_RANDOM_H)
  117. check_include_file(sys/stat.h HAVE_SYS_STAT_H)
  118. check_include_file(xlocale.h HAVE_XLOCALE_H)
  119. # Set json-c specific vars to stamp into json_config.h
  120. # in a way that hopefully won't conflict with other
  121. # projects that use json-c.
  122. if (HAVE_INTTYPES_H)
  123. set(JSON_C_HAVE_INTTYPES_H 1)
  124. endif()
  125. if (HAVE_STDINT_H)
  126. set(JSON_C_HAVE_STDINT_H 1)
  127. endif()
  128. check_symbol_exists(_isnan "float.h" HAVE_DECL__ISNAN)
  129. check_symbol_exists(_finite "float.h" HAVE_DECL__FINITE)
  130. if ((MSVC AND NOT (MSVC_VERSION LESS 1800)) OR MINGW OR CYGWIN OR UNIX)
  131. check_symbol_exists(INFINITY "math.h" HAVE_DECL_INFINITY)
  132. check_symbol_exists(isinf "math.h" HAVE_DECL_ISINF)
  133. check_symbol_exists(isnan "math.h" HAVE_DECL_ISNAN)
  134. check_symbol_exists(nan "math.h" HAVE_DECL_NAN)
  135. endif()
  136. check_symbol_exists(_doprnt "stdio.h" HAVE_DOPRNT)
  137. if (UNIX OR MINGW OR CYGWIN)
  138. check_symbol_exists(snprintf "stdio.h" HAVE_SNPRINTF)
  139. endif()
  140. check_symbol_exists(vasprintf "stdio.h" HAVE_VASPRINTF)
  141. check_symbol_exists(vsnprintf "stdio.h" HAVE_VSNPRINTF)
  142. check_symbol_exists(vprintf "stdio.h" HAVE_VPRINTF)
  143. check_symbol_exists(arc4random "stdlib.h" HAVE_ARC4RANDOM)
  144. if (NOT HAVE_ARC4RANDOM AND DISABLE_EXTRA_LIBS STREQUAL "OFF")
  145. check_include_file(bsd/stdlib.h HAVE_BSD_STDLIB_H)
  146. if (HAVE_BSD_STDLIB_H)
  147. list(APPEND CMAKE_REQUIRED_LIBRARIES "bsd")
  148. unset(HAVE_ARC4RANDOM CACHE)
  149. check_symbol_exists(arc4random "bsd/stdlib.h" HAVE_ARC4RANDOM)
  150. if (NOT HAVE_ARC4RANDOM)
  151. list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "bsd")
  152. endif()
  153. endif()
  154. endif()
  155. if (HAVE_FCNTL_H)
  156. check_symbol_exists(open "fcntl.h" HAVE_OPEN)
  157. endif()
  158. if (HAVE_STDLIB_H)
  159. check_symbol_exists(realloc "stdlib.h" HAVE_REALLOC)
  160. endif()
  161. if (HAVE_LOCALE_H)
  162. check_symbol_exists(setlocale "locale.h" HAVE_SETLOCALE)
  163. check_symbol_exists(uselocale "locale.h" HAVE_USELOCALE)
  164. endif()
  165. # uClibc *intentionally* crashes in duplocale(), at least as of:
  166. # https://github.com/ffainelli/uClibc/blob/266bdc1/libc/misc/locale/locale.c#L1322
  167. # So, if it looks like we're compiling for a system like that just disable
  168. # locale handling entirely.
  169. exec_program(${CMAKE_C_COMPILER} ARGS -dumpmachine OUTPUT_VARIABLE CMAKE_GNU_C_MACHINE)
  170. if (CMAKE_GNU_C_MACHINE MATCHES "uclibc")
  171. message(STATUS "Detected uClibc compiler, disabling locale handling")
  172. set(HAVE_SETLOCALE 0)
  173. set(HAVE_USELOCALE 0)
  174. endif()
  175. if (HAVE_STRINGS_H)
  176. check_symbol_exists(strcasecmp "strings.h" HAVE_STRCASECMP)
  177. check_symbol_exists(strncasecmp "strings.h" HAVE_STRNCASECMP)
  178. endif()
  179. if (HAVE_STRING_H)
  180. check_symbol_exists(strdup "string.h" HAVE_STRDUP)
  181. check_symbol_exists(strerror "string.h" HAVE_STRERROR)
  182. endif()
  183. if (HAVE_SYSLOG_H)
  184. check_symbol_exists(vsyslog "syslog.h" HAVE_VSYSLOG)
  185. endif()
  186. if (HAVE_SYS_RANDOM_H)
  187. check_symbol_exists(getrandom "sys/random.h" HAVE_GETRANDOM)
  188. endif()
  189. if (HAVE_SYS_RESOURCE_H)
  190. check_symbol_exists(getrusage "sys/resource.h" HAVE_GETRUSAGE)
  191. endif()
  192. check_symbol_exists(strtoll "stdlib.h" HAVE_STRTOLL)
  193. check_symbol_exists(strtoull "stdlib.h" HAVE_STRTOULL)
  194. set(json_c_strtoll "strtoll")
  195. if (NOT HAVE_STRTOLL)
  196. # Use _strtoi64 if strtoll is not available.
  197. check_symbol_exists(_strtoi64 "stdlib.h" __have_strtoi64)
  198. if (__have_strtoi64)
  199. #set(HAVE_STRTOLL 1)
  200. set(json_c_strtoll "_strtoi64")
  201. endif()
  202. endif()
  203. set(json_c_strtoull "strtoull")
  204. if (NOT HAVE_STRTOULL)
  205. # Use _strtoui64 if strtoull is not available.
  206. check_symbol_exists(_strtoui64 "stdlib.h" __have_strtoui64)
  207. if (__have_strtoui64)
  208. #set(HAVE_STRTOULL 1)
  209. set(json_c_strtoull "_strtoui64")
  210. endif()
  211. endif()
  212. check_type_size(int SIZEOF_INT)
  213. check_type_size(int64_t SIZEOF_INT64_T)
  214. check_type_size(long SIZEOF_LONG)
  215. check_type_size("long long" SIZEOF_LONG_LONG)
  216. check_type_size("size_t" SIZEOF_SIZE_T)
  217. if (MSVC)
  218. list(APPEND CMAKE_EXTRA_INCLUDE_FILES BaseTsd.h)
  219. check_type_size("SSIZE_T" SIZEOF_SSIZE_T)
  220. else()
  221. check_type_size("ssize_t" SIZEOF_SSIZE_T)
  222. endif()
  223. check_c_source_compiles(
  224. "
  225. extern void json_object_get();
  226. __asm__(\".section .gnu.json_object_get\\n\\t.ascii \\\"Please link against libjson-c instead of libjson\\\"\\n\\t.text\");
  227. int main(int c, char *v) { return 0;}
  228. "
  229. HAS_GNU_WARNING_LONG)
  230. check_c_source_compiles(
  231. "int main() { int i, x = 0; i = __sync_add_and_fetch(&x,1); return x; }"
  232. HAVE_ATOMIC_BUILTINS)
  233. if (NOT DISABLE_THREAD_LOCAL_STORAGE)
  234. check_c_source_compiles(
  235. "__thread int x = 0; int main() { return 0; }"
  236. HAVE___THREAD)
  237. if (HAVE___THREAD)
  238. set(SPEC___THREAD __thread)
  239. elseif (MSVC)
  240. set(SPEC___THREAD __declspec(thread))
  241. endif()
  242. endif()
  243. # Hardware random number is not available on Windows? Says, config.h.win32. Best to preserve compatibility.
  244. if (WIN32)
  245. set(ENABLE_RDRAND 0)
  246. endif()
  247. # Once we've done basic symbol/header searches let's add them in.
  248. configure_file(${PROJECT_SOURCE_DIR}/cmake/config.h.in ${PROJECT_BINARY_DIR}/config.h)
  249. message(STATUS "Wrote ${PROJECT_BINARY_DIR}/config.h")
  250. configure_file(${PROJECT_SOURCE_DIR}/cmake/json_config.h.in ${PROJECT_BINARY_DIR}/json_config.h)
  251. message(STATUS "Wrote ${PROJECT_BINARY_DIR}/json_config.h")
  252. if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
  253. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections")
  254. if ("${DISABLE_WERROR}" STREQUAL "OFF")
  255. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
  256. endif()
  257. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
  258. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wcast-qual")
  259. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=deprecated-declarations")
  260. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra")
  261. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wwrite-strings")
  262. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-parameter")
  263. if (NOT WIN32)
  264. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wstrict-prototypes")
  265. endif()
  266. add_definitions(-D_GNU_SOURCE)
  267. if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
  268. # Remove this for 1.0 when we can bump the ABI and actually fix these warnings.
  269. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-shorten-64-to-32")
  270. endif()
  271. elseif ("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
  272. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /DEBUG")
  273. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4100")
  274. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4996")
  275. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4244")
  276. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4706")
  277. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4702")
  278. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4127")
  279. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4701")
  280. endif()
  281. if (NOT ("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC"))
  282. check_c_source_compiles(
  283. "
  284. /* uClibc toolchains without threading barf when _REENTRANT is defined */
  285. #define _REENTRANT 1
  286. #include <sys/types.h>
  287. int main (void)
  288. {
  289. return 0;
  290. }
  291. "
  292. REENTRANT_WORKS
  293. )
  294. if (REENTRANT_WORKS)
  295. add_compile_options("-D_REENTRANT")
  296. endif()
  297. # OSX Mach-O doesn't support linking with '-Bsymbolic-functions'.
  298. # Others may not support it, too.
  299. list(APPEND CMAKE_REQUIRED_LIBRARIES "-Wl,-Bsymbolic-functions")
  300. check_c_source_compiles(
  301. "
  302. int main (void)
  303. {
  304. return 0;
  305. }
  306. "
  307. BSYMBOLIC_WORKS
  308. )
  309. list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "-Wl,-Bsymbolic-functions")
  310. if (DISABLE_BSYMBOLIC STREQUAL "OFF" AND BSYMBOLIC_WORKS)
  311. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-Bsymbolic-functions")
  312. # XXX need cmake>=3.13 for this:
  313. #add_link_options("-Wl,-Bsymbolic-functions")
  314. endif()
  315. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/check-version-script.sym" "TEST { global: *; };")
  316. list(APPEND CMAKE_REQUIRED_LIBRARIES "-Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/check-version-script.sym")
  317. check_c_source_compiles(
  318. "
  319. int main (void)
  320. {
  321. return 0;
  322. }
  323. "
  324. VERSION_SCRIPT_WORKS
  325. )
  326. list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "-Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/check-version-script.sym")
  327. if (VERSION_SCRIPT_WORKS)
  328. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--version-script,${CMAKE_CURRENT_SOURCE_DIR}/json-c.sym")
  329. endif()
  330. endif()
  331. if ($ENV{VALGRIND})
  332. # Build so that valgrind doesn't complain about linkhash.c
  333. add_definitions(-DVALGRIND=1)
  334. endif()
  335. set(JSON_C_PUBLIC_HEADERS
  336. # Note: config.h is _not_ included here
  337. ${PROJECT_BINARY_DIR}/json_config.h
  338. ${PROJECT_BINARY_DIR}/json.h
  339. ${PROJECT_SOURCE_DIR}/arraylist.h
  340. ${PROJECT_SOURCE_DIR}/debug.h
  341. ${PROJECT_SOURCE_DIR}/json_c_version.h
  342. ${PROJECT_SOURCE_DIR}/json_inttypes.h
  343. ${PROJECT_SOURCE_DIR}/json_object.h
  344. ${PROJECT_SOURCE_DIR}/json_object_iterator.h
  345. ${PROJECT_SOURCE_DIR}/json_tokener.h
  346. ${PROJECT_SOURCE_DIR}/json_types.h
  347. ${PROJECT_SOURCE_DIR}/json_util.h
  348. ${PROJECT_SOURCE_DIR}/json_visit.h
  349. ${PROJECT_SOURCE_DIR}/linkhash.h
  350. ${PROJECT_SOURCE_DIR}/printbuf.h
  351. )
  352. set(JSON_C_HEADERS
  353. ${JSON_C_PUBLIC_HEADERS}
  354. ${PROJECT_SOURCE_DIR}/json_object_private.h
  355. ${PROJECT_SOURCE_DIR}/json_pointer_private.h
  356. ${PROJECT_SOURCE_DIR}/random_seed.h
  357. ${PROJECT_SOURCE_DIR}/strerror_override.h
  358. ${PROJECT_SOURCE_DIR}/math_compat.h
  359. ${PROJECT_SOURCE_DIR}/snprintf_compat.h
  360. ${PROJECT_SOURCE_DIR}/strdup_compat.h
  361. ${PROJECT_SOURCE_DIR}/vasprintf_compat.h
  362. )
  363. set(JSON_C_SOURCES
  364. ${PROJECT_SOURCE_DIR}/arraylist.c
  365. ${PROJECT_SOURCE_DIR}/debug.c
  366. ${PROJECT_SOURCE_DIR}/json_c_version.c
  367. ${PROJECT_SOURCE_DIR}/json_object.c
  368. ${PROJECT_SOURCE_DIR}/json_object_iterator.c
  369. ${PROJECT_SOURCE_DIR}/json_tokener.c
  370. ${PROJECT_SOURCE_DIR}/json_util.c
  371. ${PROJECT_SOURCE_DIR}/json_visit.c
  372. ${PROJECT_SOURCE_DIR}/linkhash.c
  373. ${PROJECT_SOURCE_DIR}/printbuf.c
  374. ${PROJECT_SOURCE_DIR}/random_seed.c
  375. ${PROJECT_SOURCE_DIR}/strerror_override.c
  376. )
  377. if (NOT DISABLE_JSON_POINTER)
  378. set(JSON_C_PUBLIC_HEADERS ${JSON_C_PUBLIC_HEADERS} ${PROJECT_SOURCE_DIR}/json_pointer.h)
  379. set(JSON_C_SOURCES ${JSON_C_SOURCES} ${PROJECT_SOURCE_DIR}/json_pointer.c)
  380. set(JSON_H_JSON_POINTER "#include \"json_pointer.h\"")
  381. if (NOT DISABLE_JSON_PATCH)
  382. set(JSON_C_PUBLIC_HEADERS ${JSON_C_PUBLIC_HEADERS} ${PROJECT_SOURCE_DIR}/json_patch.h)
  383. set(JSON_C_SOURCES ${JSON_C_SOURCES} ${PROJECT_SOURCE_DIR}/json_patch.c)
  384. set(JSON_H_JSON_PATCH "#include \"json_patch.h\"")
  385. endif()
  386. else()
  387. set(JSON_H_JSON_POINTER "")
  388. set(JSON_H_JSON_PATCH "")
  389. endif()
  390. configure_file(json.h.cmakein ${PROJECT_BINARY_DIR}/json.h @ONLY)
  391. include_directories(${PROJECT_SOURCE_DIR})
  392. include_directories(${PROJECT_BINARY_DIR})
  393. add_subdirectory(doc)
  394. # "uninstall" custom target for make generators in unix like operating systems
  395. # and if that target is not present
  396. if (CMAKE_GENERATOR STREQUAL "Unix Makefiles")
  397. if(NOT TARGET uninstall)
  398. add_custom_target(uninstall
  399. COMMAND cat ${PROJECT_BINARY_DIR}/install_manifest.txt | xargs rm
  400. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  401. )
  402. endif()
  403. endif()
  404. # XXX for a normal full distribution we'll need to figure out
  405. # XXX how to build both shared and static libraries.
  406. # Probably leverage that to build a local VALGRIND=1 library for testing too.
  407. add_library(${PROJECT_NAME}
  408. ${JSON_C_SOURCES}
  409. ${JSON_C_HEADERS}
  410. )
  411. set_target_properties(${PROJECT_NAME} PROPERTIES
  412. VERSION 5.2.0
  413. SOVERSION 5)
  414. list(APPEND CMAKE_TARGETS ${PROJECT_NAME})
  415. # If json-c is used as subroject it set to target correct interface -I flags and allow
  416. # to build external target without extra include_directories(...)
  417. target_include_directories(${PROJECT_NAME}
  418. PUBLIC
  419. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
  420. $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
  421. )
  422. target_link_libraries(${PROJECT_NAME} PUBLIC ${CMAKE_REQUIRED_LIBRARIES})
  423. # Allow to build static and shared libraries at the same time
  424. if (BUILD_STATIC_LIBS AND BUILD_SHARED_LIBS)
  425. set(STATIC_LIB ${PROJECT_NAME}-static)
  426. add_library(${STATIC_LIB} STATIC
  427. ${JSON_C_SOURCES}
  428. ${JSON_C_HEADERS}
  429. )
  430. target_include_directories(${PROJECT_NAME}-static
  431. PUBLIC
  432. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
  433. $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
  434. )
  435. target_link_libraries(${PROJECT_NAME}-static PUBLIC ${CMAKE_REQUIRED_LIBRARIES})
  436. # rename the static library
  437. if (NOT MSVC)
  438. set_target_properties(${STATIC_LIB} PROPERTIES
  439. OUTPUT_NAME ${PROJECT_NAME}
  440. )
  441. endif()
  442. list(APPEND CMAKE_TARGETS ${STATIC_LIB})
  443. endif ()
  444. # Always create new install dirs with 0755 permissions, regardless of umask
  445. set(CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS
  446. OWNER_READ
  447. OWNER_WRITE
  448. OWNER_EXECUTE
  449. GROUP_READ
  450. GROUP_EXECUTE
  451. WORLD_READ
  452. WORLD_EXECUTE
  453. )
  454. install(TARGETS ${CMAKE_TARGETS}
  455. EXPORT ${PROJECT_NAME}-targets
  456. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  457. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  458. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  459. INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ${CMAKE_INSTALL_INCLUDEDIR}/json-c
  460. )
  461. install(EXPORT ${PROJECT_NAME}-targets
  462. FILE ${PROJECT_NAME}-targets.cmake
  463. NAMESPACE ${PROJECT_NAME}::
  464. DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
  465. )
  466. configure_package_config_file(
  467. "cmake/Config.cmake.in"
  468. ${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake
  469. INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
  470. )
  471. install(
  472. FILES ${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake
  473. DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
  474. )
  475. if (UNIX OR MINGW OR CYGWIN)
  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. endif ()
  500. install(FILES ${JSON_C_PUBLIC_HEADERS} DESTINATION ${CMAKE_INSTALL_FULL_INCLUDEDIR}/json-c)
  501. if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING AND
  502. (NOT MSVC OR NOT (MSVC_VERSION LESS 1800)) # Tests need at least VS2013
  503. )
  504. add_subdirectory(tests)
  505. endif()
  506. if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_APPS)
  507. # skip apps when we're included in someone else's build
  508. if (NOT MSVC) # cmd line apps don't built on Windows currently.
  509. add_subdirectory(apps)
  510. endif()
  511. endif()