CMakeLists.txt 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. if(ENABLE_CJSON_TEST)
  2. add_library(unity unity/src/unity.c)
  3. # Disable -Werror for Unity
  4. if (FLAG_SUPPORTED_Werror)
  5. if ("${CMAKE_VERSION}" VERSION_LESS "2.8.12")
  6. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error")
  7. else()
  8. target_compile_options(unity PRIVATE "-Wno-error")
  9. endif()
  10. endif()
  11. # Disable -fvisibility=hidden for Unity
  12. if (FLAG_SUPPORTED_fvisibilityhidden)
  13. if ("${CMAKE_VERSION}" VERSION_LESS "2.8.12")
  14. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=default")
  15. else()
  16. target_compile_options(unity PRIVATE "-fvisibility=default")
  17. endif()
  18. endif()
  19. # Disable -fsanitize=float-divide-by-zero for Unity (GCC bug on x86 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80097)
  20. if (FLAG_SUPPORTED_fsanitizefloatdividebyzero AND (CMAKE_C_COMPILER_ID STREQUAL "GNU"))
  21. if ("${CMAKE_VERSION}" VERSION_LESS "2.8.12")
  22. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize=float-divide-by-zero")
  23. else()
  24. target_compile_options(unity PRIVATE "-fno-sanitize=float-divide-by-zero")
  25. endif()
  26. endif()
  27. #copy test files
  28. file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/inputs")
  29. file(GLOB test_files "inputs/*")
  30. file(COPY ${test_files} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/inputs/")
  31. set(unity_tests
  32. parse_examples
  33. parse_number
  34. parse_hex4
  35. parse_string
  36. parse_array
  37. parse_object
  38. parse_value
  39. print_string
  40. print_number
  41. print_array
  42. print_object
  43. print_value
  44. misc_tests
  45. )
  46. add_library(test-common common.c)
  47. option(ENABLE_VALGRIND OFF "Enable the valgrind memory checker for the tests.")
  48. if (ENABLE_VALGRIND)
  49. find_program(MEMORYCHECK_COMMAND valgrind)
  50. if ("${MEMORYCHECK_COMMAND}" MATCHES "MEMORYCHECK_COMMAND-NOTFOUND")
  51. message(WARNING "Valgrind couldn't be found.")
  52. unset(MEMORYCHECK_COMMAND)
  53. else()
  54. set(MEMORYCHECK_COMMAND_OPTIONS --trace-children=yes --leak-check=full --error-exitcode=1)
  55. endif()
  56. endif()
  57. foreach(unity_test ${unity_tests})
  58. add_executable("${unity_test}" "${unity_test}.c")
  59. target_link_libraries("${unity_test}" "${CJSON_LIB}" unity test-common)
  60. if(MEMORYCHECK_COMMAND)
  61. add_test(NAME "${unity_test}"
  62. COMMAND "${MEMORYCHECK_COMMAND}" ${MEMORYCHECK_COMMAND_OPTIONS} "${CMAKE_CURRENT_BINARY_DIR}/${unity_test}")
  63. else()
  64. add_test(NAME "${unity_test}"
  65. COMMAND "./${unity_test}")
  66. endif()
  67. endforeach()
  68. add_dependencies(check ${unity_tests})
  69. endif()