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 | |
| 16 | set(tests) |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 17 | set(tests_wraps) |
Radek Krejci | 1fd5ad6 | 2020-05-15 15:48:42 +0200 | [diff] [blame^] | 18 | add_subdirectory(utests) |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 19 | |
Radek Krejci | 1fd5ad6 | 2020-05-15 15:48:42 +0200 | [diff] [blame^] | 20 | foreach(test_id IN LISTS tests) |
| 21 | message(STATUS ${test_id}) |
| 22 | string(REGEX REPLACE "([a-z]*):.*" "\\1" category "${test_id}") |
| 23 | string(REGEX REPLACE "[a-z]*:(.*)" "\\1" path "${test_id}") |
| 24 | string(REGEX REPLACE "[a-z:/]*test_(.*)" "\\1" name "${test_id}") |
| 25 | set(test_name ${category}_${name}) |
| 26 | add_executable(${test_name} ${category}s/${path}.c $<TARGET_OBJECTS:yangobj>) |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 27 | |
Radek Krejci | 1fd5ad6 | 2020-05-15 15:48:42 +0200 | [diff] [blame^] | 28 | # Set common attributes of all tests |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 29 | 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] | 30 | if (NOT APPLE) |
| 31 | list(GET tests_wraps 0 test_wrap) |
| 32 | set_target_properties(${test_name} PROPERTIES LINK_FLAGS "${test_wrap}") |
| 33 | list(REMOVE_AT tests_wraps 0) |
| 34 | endif() |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 35 | add_test(NAME ${test_name} COMMAND ${test_name}) |
| 36 | # set_property(TEST ${test_name} PROPERTY ENVIRONMENT "LIBYANG_EXTENSIONS_PLUGINS_DIR=${CMAKE_BINARY_DIR}/src/extensions") |
| 37 | # set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT "LIBYANG_USER_TYPES_PLUGINS_DIR=${CMAKE_BINARY_DIR}/src/user_types") |
| 38 | set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT "MALLOC_CHECK_=3") |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 39 | |
Radek Krejci | 1fd5ad6 | 2020-05-15 15:48:42 +0200 | [diff] [blame^] | 40 | if(ENABLE_VALGRIND_TESTS) |
| 41 | if(VALGRIND_FOUND) |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 42 | add_test(${test_name}_valgrind valgrind --leak-check=full --show-leak-kinds=all --suppressions=${PROJECT_SOURCE_DIR}/tests/ld.supp --error-exitcode=1 |
Radek Krejci | 1fd5ad6 | 2020-05-15 15:48:42 +0200 | [diff] [blame^] | 43 | ${CMAKE_BINARY_DIR}/tests/${test_name}) |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 44 | # set_property(TEST ${test_name}_valgrind PROPERTY ENVIRONMENT "LIBYANG_EXTENSIONS_PLUGINS_DIR=${CMAKE_BINARY_DIR}/src/extensions") |
| 45 | # 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^] | 46 | else(VALGRIND_FOUND) |
| 47 | message(WARNING "valgrind executable not found! Disabling memory leaks tests.") |
| 48 | endif(VALGRIND_FOUND) |
| 49 | endif(ENABLE_VALGRIND_TESTS) |
| 50 | endforeach() |
Radek Krejci | 6208cfa | 2019-03-08 14:43:58 +0100 | [diff] [blame] | 51 | |
| 52 | if(ENABLE_COVERAGE) |
| 53 | # Destination |
| 54 | set(COVERAGE_DIR "${CMAKE_BINARY_DIR}/tests/code_coverage/") |
| 55 | set(COVERAGE_FILE_RAW "${CMAKE_BINARY_DIR}/tests/coverage_raw.info") |
| 56 | set(COVERAGE_FILE_CLEAN "${CMAKE_BINARY_DIR}/tests/coverage_clean.info") |
| 57 | |
| 58 | # Add coverage target |
| 59 | add_custom_target(coverage |
| 60 | COMMENT "Generating code coverage..." |
Radek Krejci | 8c1054b | 2019-05-17 10:18:54 +0200 | [diff] [blame] | 61 | WORKING_DIRECTORY "${CMAKE_BINARY_DIR}" |
Radek Krejci | 6208cfa | 2019-03-08 14:43:58 +0100 | [diff] [blame] | 62 | # Cleanup code counters |
| 63 | COMMAND "${PATH_LCOV}" --directory . --zerocounters --quiet |
| 64 | |
| 65 | # Run tests |
| 66 | COMMAND "${CMAKE_CTEST_COMMAND}" --quiet |
| 67 | |
| 68 | # Capture the counters |
| 69 | COMMAND "${PATH_LCOV}" |
| 70 | --directory . |
| 71 | --rc lcov_branch_coverage=1 |
| 72 | --rc 'lcov_excl_line=assert' |
| 73 | --capture --quiet |
| 74 | --output-file "${COVERAGE_FILE_RAW}" |
| 75 | # Remove coverage of tests, system headers, etc. |
| 76 | COMMAND "${PATH_LCOV}" |
Radek Krejci | 8c1054b | 2019-05-17 10:18:54 +0200 | [diff] [blame] | 77 | --remove "${COVERAGE_FILE_RAW}" '${CMAKE_SOURCE_DIR}/tests/*' |
Radek Krejci | 6208cfa | 2019-03-08 14:43:58 +0100 | [diff] [blame] | 78 | --rc lcov_branch_coverage=1 |
| 79 | --quiet --output-file "${COVERAGE_FILE_CLEAN}" |
| 80 | # Generate HTML report |
| 81 | COMMAND "${PATH_GENHTML}" |
| 82 | --branch-coverage --function-coverage --quiet --title "libyang" |
| 83 | --legend --show-details --output-directory "${COVERAGE_DIR}" |
| 84 | "${COVERAGE_FILE_CLEAN}" |
| 85 | # Delete the counters |
| 86 | COMMAND "${CMAKE_COMMAND}" -E remove |
| 87 | ${COVERAGE_FILE_RAW} ${COVERAGE_FILE_CLEAN} |
| 88 | ) |
| 89 | |
| 90 | add_custom_command(TARGET coverage POST_BUILD |
| 91 | WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/tests" |
| 92 | COMMENT "To see the code coverage report, open ${COVERAGE_DIR}index.html" |
| 93 | COMMAND ; |
| 94 | ) |
| 95 | endif() |