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