CMakeLists.txt 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. # Notes:
  2. #
  3. # Author: Paul Harris, June 2012
  4. # Additions: Joakim Soderberg, Febuary 2013
  5. #
  6. # Supports: building static/shared, release/debug/etc, can also build html docs
  7. # and some of the tests.
  8. # Note that its designed for out-of-tree builds, so it will not pollute your
  9. # source tree.
  10. #
  11. # TODO 1: Finish implementing tests. api tests are working, but the valgrind
  12. # variants are not flagging problems.
  13. #
  14. # TODO 2: There is a check_exports script that would try and incorporate.
  15. #
  16. # TODO 3: Consolidate version numbers, currently the version number is written
  17. # into: * cmake (here) * autotools (the configure) * source code header files.
  18. # Should not be written directly into header files, autotools/cmake can do
  19. # that job.
  20. #
  21. # Brief intro on how to use cmake:
  22. # > mkdir build (somewhere - we do out-of-tree builds)
  23. # > use cmake, ccmake, or cmake-gui to configure the project. for linux, you
  24. # can only choose one variant: release,debug,etc... and static or shared.
  25. # >> example:
  26. # >> cd build
  27. # >> ccmake -i ../path_to_jansson_dir
  28. # >> inside, configure your options. press C until there are no lines
  29. # with * next to them.
  30. # >> note, I like to configure the 'install' path to ../install, so I get
  31. # self-contained clean installs I can point other projects to.
  32. # >> press G to 'generate' the project files.
  33. # >> make (to build the project)
  34. # >> make install
  35. # >> make test (to run the tests, if you enabled them)
  36. #
  37. # Brief description on how it works:
  38. # There is a small heirachy of CMakeLists.txt files which define how the
  39. # project is built.
  40. # Header file detection etc is done, and the results are written into config.h
  41. # and jansson_config.h, which are generated from the corresponding
  42. # config.h.cmake and jansson_config.h.cmake template files.
  43. # The generated header files end up in the build directory - not in
  44. # the source directory.
  45. # The rest is down to the usual make process.
  46. cmake_minimum_required (VERSION 2.8)
  47. # required for exports? cmake_minimum_required (VERSION 2.8.6)
  48. project (jansson C)
  49. # Options
  50. option(JANSSON_BUILD_SHARED_LIBS "Build shared libraries." OFF)
  51. option(USE_URANDOM "Use /dev/urandom to seed the hash function." ON)
  52. option(USE_WINDOWS_CRYPTOAPI "Use CryptGenRandom to seed the hash function." ON)
  53. if (MSVC)
  54. # This option must match the settings used in your program, in particular if you
  55. # are linking statically
  56. option(JANSSON_STATIC_CRT "Link the static CRT libraries" OFF )
  57. endif ()
  58. option(JANSSON_EXAMPLES "Compile example applications" ON)
  59. if (UNIX)
  60. option(JANSSON_COVERAGE "(GCC Only! Requires gcov/lcov to be installed). Include target for doing coverage analysis for the test suite. Note that -DCMAKE_BUILD_TYPE=Debug must be set" OFF)
  61. option(JANSSON_COVERALLS "Generate coverage info for Coveralls" OFF)
  62. option(JANSSON_COVERALLS_UPLOAD "Upload coverage info to Coveralls (Only works via Travis)" ON)
  63. endif ()
  64. # Set some nicer output dirs.
  65. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
  66. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
  67. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
  68. set(JANSSON_TEMP_DIR ${PROJECT_BINARY_DIR}/tmp)
  69. # Give the debug version a different postfix for windows,
  70. # so both the debug and release version can be built in the
  71. # same build-tree on Windows (MSVC).
  72. if (WIN32)
  73. set(CMAKE_DEBUG_POSTFIX "_d")
  74. endif (WIN32)
  75. # This is how I thought it should go
  76. # set (JANSSON_VERSION "2.3.1")
  77. # set (JANSSON_SOVERSION 2)
  78. set(JANSSON_DISPLAY_VERSION "2.9")
  79. # This is what is required to match the same numbers as automake's
  80. set(JANSSON_VERSION "4.9.0")
  81. set(JANSSON_SOVERSION 4)
  82. # for CheckFunctionKeywords
  83. set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
  84. include (CheckCSourceCompiles)
  85. include (CheckFunctionExists)
  86. include (CheckFunctionKeywords)
  87. include (CheckIncludeFiles)
  88. include (CheckTypeSize)
  89. if (MSVC)
  90. # Turn off Microsofts "security" warnings.
  91. add_definitions( "/W3 /D_CRT_SECURE_NO_WARNINGS /wd4005 /wd4996 /nologo" )
  92. if (JANSSON_STATIC_CRT)
  93. set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
  94. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
  95. endif()
  96. endif()
  97. if (NOT WIN32 AND (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX))
  98. add_definitions("-fPIC")
  99. endif()
  100. message("C compiler: ${CMAKE_C_COMPILER_ID}")
  101. # Coverage only works with GCC for a debug build.
  102. if (JANSSON_COVERALLS)
  103. set(JANSSON_COVERAGE ON)
  104. endif()
  105. if (JANSSON_COVERAGE)
  106. include(CodeCoverage)
  107. include(Coveralls)
  108. # This adds coverage arguments to gcc/clang.
  109. coveralls_turn_on_coverage()
  110. endif()
  111. check_include_files (endian.h HAVE_ENDIAN_H)
  112. check_include_files (fcntl.h HAVE_FCNTL_H)
  113. check_include_files (sched.h HAVE_SCHED_H)
  114. check_include_files (unistd.h HAVE_UNISTD_H)
  115. check_include_files (sys/param.h HAVE_SYS_PARAM_H)
  116. check_include_files (sys/stat.h HAVE_SYS_STAT_H)
  117. check_include_files (sys/time.h HAVE_SYS_TIME_H)
  118. check_include_files (sys/time.h HAVE_SYS_TYPES_H)
  119. check_function_exists (close HAVE_CLOSE)
  120. check_function_exists (getpid HAVE_GETPID)
  121. check_function_exists (gettimeofday HAVE_GETTIMEOFDAY)
  122. check_function_exists (open HAVE_OPEN)
  123. check_function_exists (read HAVE_READ)
  124. check_function_exists (sched_yield HAVE_SCHED_YIELD)
  125. # Check for the int-type includes
  126. check_include_files (stdint.h HAVE_STDINT_H)
  127. # Check our 64 bit integer sizes
  128. check_type_size (__int64 __INT64)
  129. check_type_size (int64_t INT64_T)
  130. check_type_size ("long long" LONG_LONG_INT)
  131. # Check our 32 bit integer sizes
  132. check_type_size (int32_t INT32_T)
  133. check_type_size (__int32 __INT32)
  134. check_type_size ("long" LONG_INT)
  135. check_type_size ("int" INT)
  136. if (HAVE_INT32_T)
  137. set (JSON_INT32 int32_t)
  138. elseif (HAVE___INT32)
  139. set (JSON_INT32 __int32)
  140. elseif (HAVE_LONG_INT AND (${LONG_INT} EQUAL 4))
  141. set (JSON_INT32 long)
  142. elseif (HAVE_INT AND (${INT} EQUAL 4))
  143. set (JSON_INT32 int)
  144. else ()
  145. message (FATAL_ERROR "Could not detect a valid 32-bit integer type")
  146. endif ()
  147. check_type_size ("unsigned long" UNSIGNED_LONG_INT)
  148. check_type_size ("unsigned int" UNSIGNED_INT)
  149. check_type_size ("unsigned short" UNSIGNED_SHORT)
  150. check_type_size (uint32_t UINT32_T)
  151. check_type_size (__uint32 __UINT32)
  152. if (HAVE_UINT32_T)
  153. set (JSON_UINT32 uint32_t)
  154. elseif (HAVE___UINT32)
  155. set (JSON_UINT32 __uint32)
  156. elseif (HAVE_UNSIGNED_LONG_INT AND (${UNSIGNED_LONG_INT} EQUAL 4))
  157. set (JSON_UINT32 "unsigned long")
  158. elseif (HAVE_UNSIGNED_INT AND (${UNSIGNED_INT} EQUAL 4))
  159. set (JSON_UINT32 "unsigned int")
  160. else ()
  161. message (FATAL_ERROR "Could not detect a valid unsigned 32-bit integer type")
  162. endif ()
  163. check_type_size (uint16_t UINT16_T)
  164. check_type_size (__uint16 __UINT16)
  165. if (HAVE_UINT16_T)
  166. set (JSON_UINT16 uint16_t)
  167. elseif (HAVE___UINT16)
  168. set (JSON_UINT16 __uint16)
  169. elseif (HAVE_UNSIGNED_INT AND (${UNSIGNED_INT} EQUAL 2))
  170. set (JSON_UINT16 "unsigned int")
  171. elseif (HAVE_UNSIGNED_SHORT AND (${UNSIGNED_SHORT} EQUAL 2))
  172. set (JSON_UINT16 "unsigned short")
  173. else ()
  174. message (FATAL_ERROR "Could not detect a valid unsigned 16-bit integer type")
  175. endif ()
  176. check_type_size (uint8_t UINT8_T)
  177. check_type_size (__uint8 __UINT8)
  178. if (HAVE_UINT8_T)
  179. set (JSON_UINT8 uint8_t)
  180. elseif (HAVE___UINT8)
  181. set (JSON_UINT8 __uint8)
  182. else ()
  183. set (JSON_UINT8 "unsigned char")
  184. endif ()
  185. # Check for ssize_t and SSIZE_T existance.
  186. check_type_size(ssize_t SSIZE_T)
  187. check_type_size(SSIZE_T UPPERCASE_SSIZE_T)
  188. if(NOT HAVE_SSIZE_T)
  189. if(HAVE_UPPERCASE_SSIZE_T)
  190. set(JSON_SSIZE SSIZE_T)
  191. else()
  192. set(JSON_SSIZE int)
  193. endif()
  194. endif()
  195. set(CMAKE_EXTRA_INCLUDE_FILES "")
  196. # Check for all the variants of strtoll
  197. check_function_exists (strtoll HAVE_STRTOLL)
  198. check_function_exists (strtoq HAVE_STRTOQ)
  199. check_function_exists (_strtoi64 HAVE__STRTOI64)
  200. # Figure out what variant we should use
  201. if (HAVE_STRTOLL)
  202. set (JSON_STRTOINT strtoll)
  203. elseif (HAVE_STRTOQ)
  204. set (JSON_STRTOINT strtoq)
  205. elseif (HAVE__STRTOI64)
  206. set (JSON_STRTOINT _strtoi64)
  207. else ()
  208. # fallback to strtol (32 bit)
  209. # this will set all the required variables
  210. set (JSON_STRTOINT strtol)
  211. set (JSON_INT_T long)
  212. set (JSON_INTEGER_FORMAT "\"ld\"")
  213. endif ()
  214. # if we haven't defined JSON_INT_T, then we have a 64 bit conversion function.
  215. # detect what to use for the 64 bit type.
  216. # Note: I will prefer long long if I can get it, as that is what the automake system aimed for.
  217. if (NOT DEFINED JSON_INT_T)
  218. if (HAVE_LONG_LONG_INT AND (${LONG_LONG_INT} EQUAL 8))
  219. set (JSON_INT_T "long long")
  220. elseif (HAVE_INT64_T)
  221. set (JSON_INT_T int64_t)
  222. elseif (HAVE___INT64)
  223. set (JSON_INT_T __int64)
  224. else ()
  225. message (FATAL_ERROR "Could not detect 64 bit type, although I detected the strtoll equivalent")
  226. endif ()
  227. # Apparently, Borland BCC and MSVC wants I64d,
  228. # Borland BCC could also accept LD
  229. # and gcc wants ldd,
  230. # I am not sure what cygwin will want, so I will assume I64d
  231. if (WIN32) # matches both msvc and cygwin
  232. set (JSON_INTEGER_FORMAT "\"I64d\"")
  233. else ()
  234. set (JSON_INTEGER_FORMAT "\"lld\"")
  235. endif ()
  236. endif ()
  237. # If locale.h and localeconv() are available, define to 1, otherwise to 0.
  238. check_include_files (locale.h HAVE_LOCALE_H)
  239. check_function_exists (localeconv HAVE_LOCALECONV)
  240. if (HAVE_LOCALECONV AND HAVE_LOCALE_H)
  241. set (JSON_HAVE_LOCALECONV 1)
  242. else ()
  243. set (JSON_HAVE_LOCALECONV 0)
  244. endif()
  245. # check if we have setlocale
  246. check_function_exists(setlocale HAVE_SETLOCALE)
  247. # Check what the inline keyword is.
  248. # Note that the original JSON_INLINE was always set to just 'inline', so this goes further.
  249. check_function_keywords("inline")
  250. check_function_keywords("__inline")
  251. check_function_keywords("__inline__")
  252. if (HAVE_INLINE)
  253. set(JSON_INLINE inline)
  254. elseif (HAVE___INLINE)
  255. set(JSON_INLINE __inline)
  256. elseif (HAVE___INLINE__)
  257. set(JSON_INLINE __inline__)
  258. else()
  259. # no inline on this platform
  260. set (JSON_INLINE)
  261. endif()
  262. check_c_source_compiles ("int main() { unsigned long val; __sync_bool_compare_and_swap(&val, 0, 1); return 0; } " HAVE_SYNC_BUILTINS)
  263. check_c_source_compiles ("int main() { char l; unsigned long v; __atomic_test_and_set(&l, __ATOMIC_RELAXED); __atomic_store_n(&v, 1, __ATOMIC_RELEASE); __atomic_load_n(&v, __ATOMIC_ACQUIRE); return 0; }" HAVE_ATOMIC_BUILTINS)
  264. set (JANSSON_INITIAL_HASHTABLE_ORDER 3 CACHE STRING "Number of buckets new object hashtables contain is 2 raised to this power. The default is 3, so empty hashtables contain 2^3 = 8 buckets.")
  265. # configure the public config file
  266. configure_file (${CMAKE_CURRENT_SOURCE_DIR}/cmake/jansson_config.h.cmake
  267. ${CMAKE_CURRENT_BINARY_DIR}/include/jansson_config.h)
  268. # Copy the jansson.h file to the public include folder
  269. file (COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/jansson.h
  270. DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/include/)
  271. add_definitions(-DJANSSON_USING_CMAKE)
  272. # configure the private config file
  273. configure_file (${CMAKE_CURRENT_SOURCE_DIR}/cmake/jansson_private_config.h.cmake
  274. ${CMAKE_CURRENT_BINARY_DIR}/private_include/jansson_private_config.h)
  275. # and tell the source code to include it
  276. add_definitions(-DHAVE_CONFIG_H)
  277. include_directories (${CMAKE_CURRENT_BINARY_DIR}/include)
  278. include_directories (${CMAKE_CURRENT_BINARY_DIR}/private_include)
  279. # Add the lib sources.
  280. file(GLOB JANSSON_SRC src/*.c)
  281. set(JANSSON_HDR_PRIVATE
  282. ${CMAKE_CURRENT_SOURCE_DIR}/src/hashtable.h
  283. ${CMAKE_CURRENT_SOURCE_DIR}/src/jansson_private.h
  284. ${CMAKE_CURRENT_SOURCE_DIR}/src/strbuffer.h
  285. ${CMAKE_CURRENT_SOURCE_DIR}/src/utf.h
  286. ${CMAKE_CURRENT_BINARY_DIR}/private_include/jansson_private_config.h)
  287. set(JANSSON_HDR_PUBLIC
  288. ${CMAKE_CURRENT_BINARY_DIR}/include/jansson_config.h
  289. ${CMAKE_CURRENT_SOURCE_DIR}/src/jansson.h)
  290. source_group("Library Sources" FILES ${JANSSON_SRC})
  291. source_group("Library Private Headers" FILES ${JANSSON_HDR_PRIVATE})
  292. source_group("Library Public Headers" FILES ${JANSSON_HDR_PUBLIC})
  293. if(JANSSON_BUILD_SHARED_LIBS)
  294. add_library(jansson SHARED
  295. ${JANSSON_SRC}
  296. ${JANSSON_HDR_PRIVATE}
  297. ${JANSSON_HDR_PUBLIC}
  298. src/jansson.def)
  299. set_target_properties(jansson PROPERTIES
  300. VERSION ${JANSSON_VERSION}
  301. SOVERSION ${JANSSON_SOVERSION})
  302. else()
  303. add_library(jansson
  304. ${JANSSON_SRC}
  305. ${JANSSON_HDR_PRIVATE}
  306. ${JANSSON_HDR_PUBLIC})
  307. endif()
  308. if (JANSSON_EXAMPLES)
  309. add_executable(simple_parse "${PROJECT_SOURCE_DIR}/examples/simple_parse.c")
  310. target_link_libraries(simple_parse jansson)
  311. endif()
  312. # For building Documentation (uses Sphinx)
  313. option(JANSSON_BUILD_DOCS "Build documentation (uses python-sphinx)." ON)
  314. if (JANSSON_BUILD_DOCS)
  315. find_package(Sphinx)
  316. if (NOT SPHINX_FOUND)
  317. message(WARNING "Sphinx not found. Cannot generate documentation!
  318. Set -DJANSSON_BUILD_DOCS=OFF to get rid of this message.")
  319. else()
  320. if (Sphinx_VERSION_STRING VERSION_LESS 1.0)
  321. message(WARNING "Your Sphinx version is too old!
  322. This project requires Sphinx v1.0 or above to produce
  323. proper documentation (you have v${Sphinx_VERSION_STRING}).
  324. You will get output but it will have errors.")
  325. endif()
  326. # configured documentation tools and intermediate build results
  327. set(BINARY_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/_build")
  328. # Sphinx cache with pickled ReST documents
  329. set(SPHINX_CACHE_DIR "${CMAKE_CURRENT_BINARY_DIR}/_doctrees")
  330. # CMake could be used to build the conf.py file too,
  331. # eg it could automatically write the version of the program or change the theme.
  332. # if(NOT DEFINED SPHINX_THEME)
  333. # set(SPHINX_THEME default)
  334. # endif()
  335. #
  336. # if(NOT DEFINED SPHINX_THEME_DIR)
  337. # set(SPHINX_THEME_DIR)
  338. # endif()
  339. #
  340. # configure_file(
  341. # "${CMAKE_CURRENT_SOURCE_DIR}/conf.py.in"
  342. # "${BINARY_BUILD_DIR}/conf.py"
  343. # @ONLY)
  344. # TODO: Add support for all sphinx builders: http://sphinx-doc.org/builders.html
  345. # Add documentation targets.
  346. set(DOC_TARGETS html)
  347. option(JANSSON_BUILD_MAN "Create a target for building man pages." ON)
  348. if (JANSSON_BUILD_MAN)
  349. if (Sphinx_VERSION_STRING VERSION_LESS 1.0)
  350. message(WARNING "Sphinx version 1.0 > is required to build man pages. You have v${Sphinx_VERSION_STRING}.")
  351. else()
  352. list(APPEND DOC_TARGETS man)
  353. endif()
  354. endif()
  355. option(JANSSON_BUILD_LATEX "Create a target for building latex docs (to create PDF)." OFF)
  356. if (JANSSON_BUILD_LATEX)
  357. find_package(LATEX)
  358. if (NOT LATEX_COMPILER)
  359. message("Couldn't find Latex, can't build latex docs using Sphinx")
  360. else()
  361. message("Latex found! If you have problems building, see Sphinx documentation for required Latex packages.")
  362. list(APPEND DOC_TARGETS latex)
  363. endif()
  364. endif()
  365. # The doc target will build all documentation targets.
  366. add_custom_target(doc)
  367. foreach (DOC_TARGET ${DOC_TARGETS})
  368. add_custom_target(${DOC_TARGET}
  369. ${SPHINX_EXECUTABLE}
  370. # -q # Enable for quiet mode
  371. -b ${DOC_TARGET}
  372. -d "${SPHINX_CACHE_DIR}"
  373. # -c "${BINARY_BUILD_DIR}" # enable if using cmake-generated conf.py
  374. "${CMAKE_CURRENT_SOURCE_DIR}/doc"
  375. "${CMAKE_CURRENT_BINARY_DIR}/doc/${DOC_TARGET}"
  376. COMMENT "Building ${DOC_TARGET} documentation with Sphinx")
  377. add_dependencies(doc ${DOC_TARGET})
  378. endforeach()
  379. message("Building documentation enabled for: ${DOC_TARGETS}")
  380. endif()
  381. endif ()
  382. option(JANSSON_WITHOUT_TESTS "Don't build tests ('make test' to execute tests)" OFF)
  383. if (NOT JANSSON_WITHOUT_TESTS)
  384. option(JANSSON_TEST_WITH_VALGRIND "Enable valgrind tests." OFF)
  385. ENABLE_TESTING()
  386. if (JANSSON_TEST_WITH_VALGRIND)
  387. # TODO: Add FindValgrind.cmake instead of having a hardcoded path.
  388. add_definitions(-DVALGRIND)
  389. # enable valgrind
  390. set(CMAKE_MEMORYCHECK_COMMAND valgrind)
  391. set(CMAKE_MEMORYCHECK_COMMAND_OPTIONS
  392. "--error-exitcode=1 --leak-check=full --show-reachable=yes --track-origins=yes -q")
  393. set(MEMCHECK_COMMAND
  394. "${CMAKE_MEMORYCHECK_COMMAND} ${CMAKE_MEMORYCHECK_COMMAND_OPTIONS}")
  395. separate_arguments(MEMCHECK_COMMAND)
  396. endif ()
  397. #
  398. # Test suites.
  399. #
  400. if (CMAKE_COMPILER_IS_GNUCC)
  401. add_definitions(-Wall -Wextra -Wdeclaration-after-statement)
  402. endif ()
  403. set(api_tests
  404. test_array
  405. test_copy
  406. test_dump
  407. test_dump_callback
  408. test_equal
  409. test_load
  410. test_loadb
  411. test_number
  412. test_object
  413. test_pack
  414. test_simple
  415. test_unpack)
  416. # Doing arithmetic on void pointers is not allowed by Microsofts compiler
  417. # such as secure_malloc and secure_free is doing, so exclude it for now.
  418. if (NOT MSVC)
  419. list(APPEND api_tests test_memory_funcs)
  420. endif()
  421. # Helper macro for building and linking a test program.
  422. macro(build_testprog name dir)
  423. add_executable(${name} ${dir}/${name}.c)
  424. add_dependencies(${name} jansson)
  425. target_link_libraries(${name} jansson)
  426. endmacro(build_testprog)
  427. # Create executables and tests/valgrind tests for API tests.
  428. foreach (test ${api_tests})
  429. build_testprog(${test} ${PROJECT_SOURCE_DIR}/test/suites/api)
  430. if (JANSSON_TEST_WITH_VALGRIND)
  431. add_test(memcheck__${test}
  432. ${MEMCHECK_COMMAND} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${test}
  433. WORKING_DIRECTORY ${JANSSON_TEMP_DIR})
  434. else()
  435. add_test(${test}
  436. ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${test}
  437. WORKING_DIRECTORY ${JANSSON_TEMP_DIR})
  438. endif ()
  439. endforeach ()
  440. # Test harness for the suites tests.
  441. build_testprog(json_process ${PROJECT_SOURCE_DIR}/test/bin)
  442. set(SUITE_TEST_CMD ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/json_process)
  443. set(SUITES encoding-flags valid invalid invalid-unicode)
  444. foreach (SUITE ${SUITES})
  445. file(GLOB TESTDIRS ${jansson_SOURCE_DIR}/test/suites/${SUITE}/*)
  446. foreach (TESTDIR ${TESTDIRS})
  447. if (IS_DIRECTORY ${TESTDIR})
  448. get_filename_component(TNAME ${TESTDIR} NAME)
  449. if (JANSSON_TEST_WITH_VALGRIND)
  450. add_test(memcheck__${SUITE}__${TNAME}
  451. ${MEMCHECK_COMMAND} ${SUITE_TEST_CMD} ${TESTDIR})
  452. else()
  453. add_test(${SUITE}__${TNAME}
  454. ${SUITE_TEST_CMD} ${TESTDIR})
  455. endif()
  456. if ((${SUITE} STREQUAL "valid" OR ${SUITE} STREQUAL "invalid") AND NOT EXISTS ${TESTDIR}/nostrip)
  457. if (JANSSON_TEST_WITH_VALGRIND)
  458. add_test(memcheck__${SUITE}__${TNAME}__strip
  459. ${MEMCHECK_COMMAND} ${SUITE_TEST_CMD} --strip ${TESTDIR})
  460. else()
  461. add_test(${SUITE}__${TNAME}__strip
  462. ${SUITE_TEST_CMD} --strip ${TESTDIR})
  463. endif()
  464. endif ()
  465. endif ()
  466. endforeach ()
  467. endforeach ()
  468. if (JANSSON_COVERAGE)
  469. setup_target_for_coverage(
  470. coverage # Coverage make target "make coverage".
  471. coverage # Name of output directory.
  472. make # Name of test runner executable.
  473. test) # Arguments to the test runner above (make test).
  474. if (JANSSON_COVERALLS)
  475. set(COVERAGE_SRCS ${JANSSON_SRC})
  476. coveralls_setup("${COVERAGE_SRCS}" ${JANSSON_COVERALLS_UPLOAD})
  477. endif ()
  478. endif ()
  479. # Enable using "make check" just like the autotools project.
  480. # By default cmake creates a target "make test"
  481. add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}
  482. DEPENDS json_process ${api_tests})
  483. endif ()
  484. #
  485. # Installation preparation.
  486. #
  487. # Allow the user to override installation directories.
  488. set(JANSSON_INSTALL_LIB_DIR lib CACHE PATH "Installation directory for libraries")
  489. set(JANSSON_INSTALL_BIN_DIR bin CACHE PATH "Installation directory for executables")
  490. set(JANSSON_INSTALL_INCLUDE_DIR include CACHE PATH "Installation directory for header files")
  491. if(WIN32 AND NOT CYGWIN)
  492. set(DEF_INSTALL_CMAKE_DIR cmake)
  493. else()
  494. set(DEF_INSTALL_CMAKE_DIR lib/cmake/jansson)
  495. endif()
  496. set(JANSSON_INSTALL_CMAKE_DIR ${DEF_INSTALL_CMAKE_DIR} CACHE PATH "Installation directory for CMake files")
  497. # Create pkg-conf file.
  498. # (We use the same files as ./configure does, so we
  499. # have to defined the same variables used there).
  500. set(prefix ${CMAKE_INSTALL_PREFIX})
  501. set(exec_prefix ${CMAKE_INSTALL_PREFIX})
  502. set(libdir ${CMAKE_INSTALL_PREFIX}/${JANSSON_INSTALL_LIB_DIR})
  503. set(VERSION ${JANSSON_DISPLAY_VERSION})
  504. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/jansson.pc.in
  505. ${CMAKE_CURRENT_BINARY_DIR}/jansson.pc @ONLY)
  506. # Make sure the paths are absolute.
  507. foreach(p LIB BIN INCLUDE CMAKE)
  508. set(var JANSSON_INSTALL_${p}_DIR)
  509. if(NOT IS_ABSOLUTE "${${var}}")
  510. set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
  511. endif()
  512. endforeach()
  513. # Export targets (This is used for other CMake projects to easily find the libraries and include files).
  514. export(TARGETS jansson
  515. FILE "${PROJECT_BINARY_DIR}/JanssonTargets.cmake")
  516. export(PACKAGE jansson)
  517. # Generate the config file for the build-tree.
  518. set(JANSSON__INCLUDE_DIRS
  519. "${PROJECT_SOURCE_DIR}/include"
  520. "${PROJECT_BINARY_DIR}/include")
  521. set(JANSSON_INCLUDE_DIRS ${JANSSON__INCLUDE_DIRS} CACHE PATH "Jansson include directories")
  522. configure_file(${PROJECT_SOURCE_DIR}/cmake/JanssonConfig.cmake.in
  523. ${PROJECT_BINARY_DIR}/JanssonConfig.cmake
  524. @ONLY)
  525. # Generate the config file for the installation tree.
  526. file(RELATIVE_PATH
  527. REL_INCLUDE_DIR
  528. "${JANSSON_INSTALL_CMAKE_DIR}"
  529. "${JANSSON_INSTALL_INCLUDE_DIR}") # Calculate the relative directory from the Cmake dir.
  530. # Note the EVENT_CMAKE_DIR is defined in JanssonConfig.cmake.in,
  531. # we escape it here so it's evaluated when it is included instead
  532. # so that the include dirs are given relative to where the
  533. # config file is located.
  534. set(JANSSON__INCLUDE_DIRS
  535. "\${JANSSON_CMAKE_DIR}/${REL_INCLUDE_DIR}")
  536. configure_file(${PROJECT_SOURCE_DIR}/cmake/JanssonConfig.cmake.in
  537. ${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/JanssonConfig.cmake
  538. @ONLY)
  539. # Generate version info for both build-tree and install-tree.
  540. configure_file(${PROJECT_SOURCE_DIR}/cmake/JanssonConfigVersion.cmake.in
  541. ${PROJECT_BINARY_DIR}/JanssonConfigVersion.cmake
  542. @ONLY)
  543. # Define the public headers.
  544. set_target_properties(jansson PROPERTIES PUBLIC_HEADER "${JANSSON_HDR_PUBLIC}")
  545. #TODO: fix this.
  546. #
  547. # Install targets.
  548. #
  549. option(JANSSON_INSTALL "Generate installation target" ON)
  550. if (JANSSON_INSTALL)
  551. install(TARGETS jansson
  552. EXPORT JanssonTargets
  553. LIBRARY DESTINATION "${JANSSON_INSTALL_LIB_DIR}" COMPONENT lib
  554. ARCHIVE DESTINATION "${JANSSON_INSTALL_LIB_DIR}" COMPONENT lib
  555. RUNTIME DESTINATION "${JANSSON_INSTALL_BIN_DIR}" COMPONENT lib # Windows DLLs
  556. PUBLIC_HEADER DESTINATION "${JANSSON_INSTALL_INCLUDE_DIR}" COMPONENT dev)
  557. # Install the pkg-config.
  558. install (FILES
  559. ${CMAKE_CURRENT_BINARY_DIR}/jansson.pc
  560. DESTINATION ${JANSSON_INSTALL_LIB_DIR}/pkgconfig COMPONENT dev)
  561. # Install the configs.
  562. install(FILES
  563. ${PROJECT_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/JanssonConfig.cmake
  564. ${PROJECT_BINARY_DIR}/JanssonConfigVersion.cmake
  565. DESTINATION "${JANSSON_INSTALL_CMAKE_DIR}" COMPONENT dev)
  566. # Install exports for the install-tree.
  567. install(EXPORT JanssonTargets
  568. DESTINATION "${JANSSON_INSTALL_CMAKE_DIR}" COMPONENT dev)
  569. endif()
  570. # For use when simply using add_library from a parent project to build jansson.
  571. set(JANSSON_LIBRARIES jansson CACHE STRING "Jansson libraries")