blob: 01cb370379ff22d9d411727c650303bd0dca616a [file] [log] [blame]
Radek Krejcie84f12f2018-09-18 14:11:50 +02001cmake_minimum_required(VERSION 2.8.12)
2
3# Correct RPATH usage on OS X
4set(CMAKE_MACOSX_RPATH TRUE)
5
Radek Krejcie84f12f2018-09-18 14:11:50 +02006if(ENABLE_STATIC AND ENABLE_VALGRIND_TESTS)
7 message(WARNING "Can't run C valgrind tests on a static build")
8else()
9 find_program(VALGRIND_FOUND valgrind)
10endif()
11
Radek Krejci1e880032018-09-20 12:17:12 +020012configure_file("${PROJECT_SOURCE_DIR}/tests/config.h.in" "${PROJECT_BINARY_DIR}/tests/config.h" ESCAPE_QUOTES @ONLY)
Radek Krejcie84f12f2018-09-18 14:11:50 +020013include_directories(SYSTEM ${CMOCKA_INCLUDE_DIR})
Radek Krejci1e880032018-09-20 12:17:12 +020014include_directories(${PROJECT_BINARY_DIR})
Radek Krejcie84f12f2018-09-18 14:11:50 +020015
16set(tests)
Radek Krejci36bac2b2018-09-19 11:15:29 +020017set(tests_wraps)
Radek Krejcie84f12f2018-09-18 14:11:50 +020018add_subdirectory(src)
Radek Krejci8c1054b2019-05-17 10:18:54 +020019add_subdirectory(features)
Radek Krejcie84f12f2018-09-18 14:11:50 +020020
21foreach(test_name IN LISTS tests)
Radek Krejciaaf6d402018-09-20 15:14:47 +020022 message(STATUS ${test_name})
23 string(REGEX REPLACE "[a-z]*_(.*)" "\\1" name "${test_name}")
Radek Krejcie84f12f2018-09-18 14:11:50 +020024 string(REGEX REPLACE "([a-z]*)_.*" "\\1" prefix "${test_name}")
Radek Krejci2d7a47b2019-05-16 13:34:10 +020025 add_executable(${test_name} ${prefix}/test_${name}.c $<TARGET_OBJECTS:yangobj>)
Radek Krejcie84f12f2018-09-18 14:11:50 +020026endforeach(test_name)
27
28# Set common attributes of all tests
29foreach(test_name IN LISTS tests)
Radek Krejci54579462019-04-30 12:47:06 +020030 target_link_libraries(${test_name} ${CMOCKA_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${PCRE2_LIBRARIES} m)
Radek Krejci1b5ef9d2018-11-27 12:49:59 +010031 if (NOT APPLE)
32 list(GET tests_wraps 0 test_wrap)
33 set_target_properties(${test_name} PROPERTIES LINK_FLAGS "${test_wrap}")
34 list(REMOVE_AT tests_wraps 0)
35 endif()
Radek Krejcie84f12f2018-09-18 14:11:50 +020036 add_test(NAME ${test_name} COMMAND ${test_name})
37# set_property(TEST ${test_name} PROPERTY ENVIRONMENT "LIBYANG_EXTENSIONS_PLUGINS_DIR=${CMAKE_BINARY_DIR}/src/extensions")
38# set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT "LIBYANG_USER_TYPES_PLUGINS_DIR=${CMAKE_BINARY_DIR}/src/user_types")
39 set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT "MALLOC_CHECK_=3")
40endforeach(test_name)
41
42if(ENABLE_VALGRIND_TESTS)
43 if(VALGRIND_FOUND)
44 foreach(test_name IN LISTS tests)
45 add_test(${test_name}_valgrind valgrind --leak-check=full --show-leak-kinds=all --suppressions=${PROJECT_SOURCE_DIR}/tests/ld.supp --error-exitcode=1
46 ${CMAKE_BINARY_DIR}/tests/${test_name})
47# set_property(TEST ${test_name}_valgrind PROPERTY ENVIRONMENT "LIBYANG_EXTENSIONS_PLUGINS_DIR=${CMAKE_BINARY_DIR}/src/extensions")
48# set_property(TEST ${test_name}_valgrind APPEND PROPERTY ENVIRONMENT "LIBYANG_USER_TYPES_PLUGINS_DIR=${CMAKE_BINARY_DIR}/src/user_types")
49 endforeach(test_name)
50 else(VALGRIND_FOUND)
51 message(WARNING "valgrind executable not found! Disabling memory leaks tests.")
52 endif(VALGRIND_FOUND)
Radek Krejci6208cfa2019-03-08 14:43:58 +010053endif()
54
55if(ENABLE_COVERAGE)
56 # Destination
57 set(COVERAGE_DIR "${CMAKE_BINARY_DIR}/tests/code_coverage/")
58 set(COVERAGE_FILE_RAW "${CMAKE_BINARY_DIR}/tests/coverage_raw.info")
59 set(COVERAGE_FILE_CLEAN "${CMAKE_BINARY_DIR}/tests/coverage_clean.info")
60
61 # Add coverage target
62 add_custom_target(coverage
63 COMMENT "Generating code coverage..."
Radek Krejci8c1054b2019-05-17 10:18:54 +020064 WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
Radek Krejci6208cfa2019-03-08 14:43:58 +010065 # Cleanup code counters
66 COMMAND "${PATH_LCOV}" --directory . --zerocounters --quiet
67
68 # Run tests
69 COMMAND "${CMAKE_CTEST_COMMAND}" --quiet
70
71 # Capture the counters
72 COMMAND "${PATH_LCOV}"
73 --directory .
74 --rc lcov_branch_coverage=1
75 --rc 'lcov_excl_line=assert'
76 --capture --quiet
77 --output-file "${COVERAGE_FILE_RAW}"
78 # Remove coverage of tests, system headers, etc.
79 COMMAND "${PATH_LCOV}"
Radek Krejci8c1054b2019-05-17 10:18:54 +020080 --remove "${COVERAGE_FILE_RAW}" '${CMAKE_SOURCE_DIR}/tests/*'
Radek Krejci6208cfa2019-03-08 14:43:58 +010081 --rc lcov_branch_coverage=1
82 --quiet --output-file "${COVERAGE_FILE_CLEAN}"
83 # Generate HTML report
84 COMMAND "${PATH_GENHTML}"
85 --branch-coverage --function-coverage --quiet --title "libyang"
86 --legend --show-details --output-directory "${COVERAGE_DIR}"
87 "${COVERAGE_FILE_CLEAN}"
88 # Delete the counters
89 COMMAND "${CMAKE_COMMAND}" -E remove
90 ${COVERAGE_FILE_RAW} ${COVERAGE_FILE_CLEAN}
91 )
92
93 add_custom_command(TARGET coverage POST_BUILD
94 WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/tests"
95 COMMENT "To see the code coverage report, open ${COVERAGE_DIR}index.html"
96 COMMAND ;
97 )
98endif()