Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 1 | # Correct RPATH usage on OS X |
| 2 | set(CMAKE_MACOSX_RPATH TRUE) |
| 3 | |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 4 | if(ENABLE_STATIC AND ENABLE_VALGRIND_TESTS) |
| 5 | message(WARNING "Can't run C valgrind tests on a static build") |
| 6 | else() |
| 7 | find_program(VALGRIND_FOUND valgrind) |
| 8 | endif() |
| 9 | |
Radek Krejci | ef5f767 | 2021-04-01 17:04:12 +0200 | [diff] [blame] | 10 | configure_file("${PROJECT_SOURCE_DIR}/tests/tests_config.h.in" "${PROJECT_BINARY_DIR}/tests/tests_config.h" ESCAPE_QUOTES @ONLY) |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 11 | include_directories(SYSTEM ${CMOCKA_INCLUDE_DIR}) |
Radek Krejci | ef5f767 | 2021-04-01 17:04:12 +0200 | [diff] [blame] | 12 | include_directories(${PROJECT_BINARY_DIR}/tests/) |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 13 | |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 14 | function(ly_add_utest) |
| 15 | cmake_parse_arguments(ADDTEST "" "NAME;WRAP" "SOURCES" ${ARGN}) |
| 16 | set(TEST_NAME utest_${ADDTEST_NAME}) |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 17 | |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 18 | foreach(TEST_SOURCE ${ADDTEST_SOURCES}) |
Michal Vasko | 0666d05 | 2021-05-21 11:12:13 +0200 | [diff] [blame^] | 19 | list(APPEND TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/${TEST_SOURCE}) |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 20 | endforeach() |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 21 | |
Michal Vasko | 0666d05 | 2021-05-21 11:12:13 +0200 | [diff] [blame^] | 22 | add_executable(${TEST_NAME} ${TEST_SOURCES} $<TARGET_OBJECTS:yangobj>) |
| 23 | |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 24 | # Set common attributes of all tests |
| 25 | set_target_properties(${TEST_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") |
Radek Krejci | bf940f9 | 2021-03-24 21:04:13 +0100 | [diff] [blame] | 26 | target_link_libraries(${TEST_NAME} ${CMOCKA_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${PCRE2_LIBRARIES} ${CMAKE_DL_LIBS} m) |
Radek Krejci | 1b5ef9d | 2018-11-27 12:49:59 +0100 | [diff] [blame] | 27 | if (NOT APPLE) |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 28 | if (ADDTEST_WRAP) |
| 29 | set_target_properties(${TEST_NAME} PROPERTIES LINK_FLAGS "${ADDTEST_WRAP}") |
| 30 | endif() |
Radek Krejci | 1b5ef9d | 2018-11-27 12:49:59 +0100 | [diff] [blame] | 31 | endif() |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 32 | |
| 33 | add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME}) |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 34 | set_property(TEST ${TEST_NAME} APPEND PROPERTY ENVIRONMENT "MALLOC_CHECK_=3") |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 35 | |
Radek Krejci | 1fd5ad6 | 2020-05-15 15:48:42 +0200 | [diff] [blame] | 36 | if(ENABLE_VALGRIND_TESTS) |
| 37 | if(VALGRIND_FOUND) |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 38 | add_test(${TEST_NAME}_valgrind valgrind --leak-check=full --show-leak-kinds=all --suppressions=${PROJECT_SOURCE_DIR}/tests/ld.supp --error-exitcode=1 |
| 39 | ${CMAKE_BINARY_DIR}/tests/${TEST_NAME}) |
Radek Krejci | 1fd5ad6 | 2020-05-15 15:48:42 +0200 | [diff] [blame] | 40 | else(VALGRIND_FOUND) |
| 41 | message(WARNING "valgrind executable not found! Disabling memory leaks tests.") |
| 42 | endif(VALGRIND_FOUND) |
| 43 | endif(ENABLE_VALGRIND_TESTS) |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 44 | endfunction(ly_add_utest) |
| 45 | |
Radek Krejci | 6208cfa | 2019-03-08 14:43:58 +0100 | [diff] [blame] | 46 | if(ENABLE_COVERAGE) |
| 47 | # Destination |
| 48 | set(COVERAGE_DIR "${CMAKE_BINARY_DIR}/tests/code_coverage/") |
| 49 | set(COVERAGE_FILE_RAW "${CMAKE_BINARY_DIR}/tests/coverage_raw.info") |
| 50 | set(COVERAGE_FILE_CLEAN "${CMAKE_BINARY_DIR}/tests/coverage_clean.info") |
| 51 | |
| 52 | # Add coverage target |
| 53 | add_custom_target(coverage |
| 54 | COMMENT "Generating code coverage..." |
Radek Krejci | 8c1054b | 2019-05-17 10:18:54 +0200 | [diff] [blame] | 55 | WORKING_DIRECTORY "${CMAKE_BINARY_DIR}" |
Radek Krejci | 6208cfa | 2019-03-08 14:43:58 +0100 | [diff] [blame] | 56 | # Cleanup code counters |
| 57 | COMMAND "${PATH_LCOV}" --directory . --zerocounters --quiet |
| 58 | |
| 59 | # Run tests |
| 60 | COMMAND "${CMAKE_CTEST_COMMAND}" --quiet |
| 61 | |
| 62 | # Capture the counters |
| 63 | COMMAND "${PATH_LCOV}" |
| 64 | --directory . |
| 65 | --rc lcov_branch_coverage=1 |
| 66 | --rc 'lcov_excl_line=assert' |
| 67 | --capture --quiet |
| 68 | --output-file "${COVERAGE_FILE_RAW}" |
| 69 | # Remove coverage of tests, system headers, etc. |
| 70 | COMMAND "${PATH_LCOV}" |
Radek Krejci | 8c1054b | 2019-05-17 10:18:54 +0200 | [diff] [blame] | 71 | --remove "${COVERAGE_FILE_RAW}" '${CMAKE_SOURCE_DIR}/tests/*' |
Radek Krejci | 6208cfa | 2019-03-08 14:43:58 +0100 | [diff] [blame] | 72 | --rc lcov_branch_coverage=1 |
| 73 | --quiet --output-file "${COVERAGE_FILE_CLEAN}" |
| 74 | # Generate HTML report |
| 75 | COMMAND "${PATH_GENHTML}" |
| 76 | --branch-coverage --function-coverage --quiet --title "libyang" |
| 77 | --legend --show-details --output-directory "${COVERAGE_DIR}" |
| 78 | "${COVERAGE_FILE_CLEAN}" |
| 79 | # Delete the counters |
| 80 | COMMAND "${CMAKE_COMMAND}" -E remove |
| 81 | ${COVERAGE_FILE_RAW} ${COVERAGE_FILE_CLEAN} |
| 82 | ) |
| 83 | |
| 84 | add_custom_command(TARGET coverage POST_BUILD |
| 85 | WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/tests" |
| 86 | COMMENT "To see the code coverage report, open ${COVERAGE_DIR}index.html" |
| 87 | COMMAND ; |
| 88 | ) |
| 89 | endif() |
Radek Krejci | cc3a1b6 | 2020-07-20 07:43:08 +0200 | [diff] [blame] | 90 | |
| 91 | add_subdirectory(style) |
Radek Krejci | 5902be9 | 2021-03-25 21:25:14 +0100 | [diff] [blame] | 92 | add_subdirectory(plugins) |
Radek Krejci | cc3a1b6 | 2020-07-20 07:43:08 +0200 | [diff] [blame] | 93 | add_subdirectory(utests) |
Juraj Vijtiuk | d798942 | 2020-09-15 13:54:00 +0200 | [diff] [blame] | 94 | add_subdirectory(fuzz) |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 95 | |
| 96 | set(format_sources ${format_sources} PARENT_SCOPE) |