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