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