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