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 | 6208cfa | 2019-03-08 14:43:58 +0100 | [diff] [blame] | 12 | if(ENABLE_COVERAGE) |
| 13 | find_program(PATH_GCOV NAMES gcov) |
| 14 | if(NOT PATH_GCOV) |
| 15 | message(WARNING "'gcov' executable not found! Disabling building code coverage report.") |
| 16 | set(ENABLE_COVERAGE OFF) |
| 17 | endif() |
| 18 | |
| 19 | find_program(PATH_LCOV NAMES lcov) |
| 20 | if(NOT PATH_GCOV) |
| 21 | message(WARNING "'lcov' executable not found! Disabling building code coverage report.") |
| 22 | set(ENABLE_COVERAGE OFF) |
| 23 | endif() |
| 24 | |
| 25 | find_program(PATH_GENHTML NAMES genhtml) |
| 26 | if(NOT PATH_GCOV) |
| 27 | message(WARNING "'genhtml' executable not found! Disabling building code coverage report.") |
| 28 | set(ENABLE_COVERAGE OFF) |
| 29 | endif() |
| 30 | |
| 31 | if(NOT CMAKE_COMPILER_IS_GNUCC) |
Radek Krejci | e8fb7dd | 2019-03-08 15:14:38 +0100 | [diff] [blame] | 32 | message(WARNING "Compiler is not gcc! Coverage may break the tests!") |
Radek Krejci | 6208cfa | 2019-03-08 14:43:58 +0100 | [diff] [blame] | 33 | endif() |
| 34 | |
| 35 | if(ENABLE_COVERAGE) |
| 36 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage -fprofile-arcs -ftest-coverage") |
| 37 | endif() |
| 38 | endif() |
| 39 | |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 40 | 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] | 41 | include_directories(SYSTEM ${CMOCKA_INCLUDE_DIR}) |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 42 | include_directories(${PROJECT_BINARY_DIR}) |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 43 | |
| 44 | set(tests) |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 45 | set(tests_wraps) |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 46 | add_subdirectory(src) |
| 47 | |
| 48 | foreach(test_name IN LISTS tests) |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 49 | message(STATUS ${test_name}) |
| 50 | string(REGEX REPLACE "[a-z]*_(.*)" "\\1" name "${test_name}") |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 51 | string(REGEX REPLACE "([a-z]*)_.*" "\\1" prefix "${test_name}") |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame^] | 52 | add_executable(${test_name} ${prefix}/test_${name}.c $<TARGET_OBJECTS:yangobj>) |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 53 | endforeach(test_name) |
| 54 | |
| 55 | # Set common attributes of all tests |
| 56 | foreach(test_name IN LISTS tests) |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 57 | 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] | 58 | if (NOT APPLE) |
| 59 | list(GET tests_wraps 0 test_wrap) |
| 60 | set_target_properties(${test_name} PROPERTIES LINK_FLAGS "${test_wrap}") |
| 61 | list(REMOVE_AT tests_wraps 0) |
| 62 | endif() |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 63 | add_test(NAME ${test_name} COMMAND ${test_name}) |
| 64 | # set_property(TEST ${test_name} PROPERTY ENVIRONMENT "LIBYANG_EXTENSIONS_PLUGINS_DIR=${CMAKE_BINARY_DIR}/src/extensions") |
| 65 | # set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT "LIBYANG_USER_TYPES_PLUGINS_DIR=${CMAKE_BINARY_DIR}/src/user_types") |
| 66 | set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT "MALLOC_CHECK_=3") |
| 67 | endforeach(test_name) |
| 68 | |
| 69 | if(ENABLE_VALGRIND_TESTS) |
| 70 | if(VALGRIND_FOUND) |
| 71 | foreach(test_name IN LISTS tests) |
| 72 | add_test(${test_name}_valgrind valgrind --leak-check=full --show-leak-kinds=all --suppressions=${PROJECT_SOURCE_DIR}/tests/ld.supp --error-exitcode=1 |
| 73 | ${CMAKE_BINARY_DIR}/tests/${test_name}) |
| 74 | # set_property(TEST ${test_name}_valgrind PROPERTY ENVIRONMENT "LIBYANG_EXTENSIONS_PLUGINS_DIR=${CMAKE_BINARY_DIR}/src/extensions") |
| 75 | # set_property(TEST ${test_name}_valgrind APPEND PROPERTY ENVIRONMENT "LIBYANG_USER_TYPES_PLUGINS_DIR=${CMAKE_BINARY_DIR}/src/user_types") |
| 76 | endforeach(test_name) |
| 77 | else(VALGRIND_FOUND) |
| 78 | message(WARNING "valgrind executable not found! Disabling memory leaks tests.") |
| 79 | endif(VALGRIND_FOUND) |
Radek Krejci | 6208cfa | 2019-03-08 14:43:58 +0100 | [diff] [blame] | 80 | endif() |
| 81 | |
| 82 | if(ENABLE_COVERAGE) |
| 83 | # Destination |
| 84 | set(COVERAGE_DIR "${CMAKE_BINARY_DIR}/tests/code_coverage/") |
| 85 | set(COVERAGE_FILE_RAW "${CMAKE_BINARY_DIR}/tests/coverage_raw.info") |
| 86 | set(COVERAGE_FILE_CLEAN "${CMAKE_BINARY_DIR}/tests/coverage_clean.info") |
| 87 | |
| 88 | # Add coverage target |
| 89 | add_custom_target(coverage |
| 90 | COMMENT "Generating code coverage..." |
| 91 | WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/tests" |
| 92 | # Cleanup code counters |
| 93 | COMMAND "${PATH_LCOV}" --directory . --zerocounters --quiet |
| 94 | |
| 95 | # Run tests |
| 96 | COMMAND "${CMAKE_CTEST_COMMAND}" --quiet |
| 97 | |
| 98 | # Capture the counters |
| 99 | COMMAND "${PATH_LCOV}" |
| 100 | --directory . |
| 101 | --rc lcov_branch_coverage=1 |
| 102 | --rc 'lcov_excl_line=assert' |
| 103 | --capture --quiet |
| 104 | --output-file "${COVERAGE_FILE_RAW}" |
| 105 | # Remove coverage of tests, system headers, etc. |
| 106 | COMMAND "${PATH_LCOV}" |
| 107 | --remove "${COVERAGE_FILE_RAW}" '${CMAKE_BINARY_DIR}/*' '${CMAKE_BINARY_DIR}/tests/*' '/usr/*' |
| 108 | --rc lcov_branch_coverage=1 |
| 109 | --quiet --output-file "${COVERAGE_FILE_CLEAN}" |
| 110 | # Generate HTML report |
| 111 | COMMAND "${PATH_GENHTML}" |
| 112 | --branch-coverage --function-coverage --quiet --title "libyang" |
| 113 | --legend --show-details --output-directory "${COVERAGE_DIR}" |
| 114 | "${COVERAGE_FILE_CLEAN}" |
| 115 | # Delete the counters |
| 116 | COMMAND "${CMAKE_COMMAND}" -E remove |
| 117 | ${COVERAGE_FILE_RAW} ${COVERAGE_FILE_CLEAN} |
| 118 | ) |
| 119 | |
| 120 | add_custom_command(TARGET coverage POST_BUILD |
| 121 | WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/tests" |
| 122 | COMMENT "To see the code coverage report, open ${COVERAGE_DIR}index.html" |
| 123 | COMMAND ; |
| 124 | ) |
| 125 | endif() |