blob: 692ddda1f3286ebe91579ede84d9ab92c7e31dd2 [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 Krejcief5f7672021-04-01 17:04:12 +020012configure_file("${PROJECT_SOURCE_DIR}/tests/tests_config.h.in" "${PROJECT_BINARY_DIR}/tests/tests_config.h" ESCAPE_QUOTES @ONLY)
Radek Krejcie84f12f2018-09-18 14:11:50 +020013include_directories(SYSTEM ${CMOCKA_INCLUDE_DIR})
Radek Krejcief5f7672021-04-01 17:04:12 +020014include_directories(${PROJECT_BINARY_DIR}/tests/)
Radek Krejcie84f12f2018-09-18 14:11:50 +020015
Radek Krejci18abde42020-06-13 20:04:39 +020016function(ly_add_utest)
17 cmake_parse_arguments(ADDTEST "" "NAME;WRAP" "SOURCES" ${ARGN})
18 set(TEST_NAME utest_${ADDTEST_NAME})
Radek Krejcie84f12f2018-09-18 14:11:50 +020019
Michal Vasko1b0b9a12021-05-06 08:38:34 +020020 add_executable(${TEST_NAME} ${TEST_SOURCES} $<TARGET_OBJECTS:yangobj>)
Radek Krejci18abde42020-06-13 20:04:39 +020021 foreach(TEST_SOURCE ${ADDTEST_SOURCES})
Radek Krejci18abde42020-06-13 20:04:39 +020022 target_sources(${TEST_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/${TEST_SOURCE})
23 endforeach()
Radek Krejcie84f12f2018-09-18 14:11:50 +020024
Radek Krejci18abde42020-06-13 20:04:39 +020025 # Set common attributes of all tests
26 set_target_properties(${TEST_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")
Radek Krejcibf940f92021-03-24 21:04:13 +010027 target_link_libraries(${TEST_NAME} ${CMOCKA_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${PCRE2_LIBRARIES} ${CMAKE_DL_LIBS} m)
Radek Krejci1b5ef9d2018-11-27 12:49:59 +010028 if (NOT APPLE)
Radek Krejci18abde42020-06-13 20:04:39 +020029 if (ADDTEST_WRAP)
30 set_target_properties(${TEST_NAME} PROPERTIES LINK_FLAGS "${ADDTEST_WRAP}")
31 endif()
Radek Krejci1b5ef9d2018-11-27 12:49:59 +010032 endif()
Radek Krejci18abde42020-06-13 20:04:39 +020033
34 add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
Radek Krejci18abde42020-06-13 20:04:39 +020035 set_property(TEST ${TEST_NAME} APPEND PROPERTY ENVIRONMENT "MALLOC_CHECK_=3")
Radek Krejcie84f12f2018-09-18 14:11:50 +020036
Radek Krejci1fd5ad62020-05-15 15:48:42 +020037 if(ENABLE_VALGRIND_TESTS)
38 if(VALGRIND_FOUND)
Radek Krejci18abde42020-06-13 20:04:39 +020039 add_test(${TEST_NAME}_valgrind valgrind --leak-check=full --show-leak-kinds=all --suppressions=${PROJECT_SOURCE_DIR}/tests/ld.supp --error-exitcode=1
40 ${CMAKE_BINARY_DIR}/tests/${TEST_NAME})
Radek Krejci1fd5ad62020-05-15 15:48:42 +020041 else(VALGRIND_FOUND)
42 message(WARNING "valgrind executable not found! Disabling memory leaks tests.")
43 endif(VALGRIND_FOUND)
44 endif(ENABLE_VALGRIND_TESTS)
Radek Krejci18abde42020-06-13 20:04:39 +020045endfunction(ly_add_utest)
46
Radek Krejci6208cfa2019-03-08 14:43:58 +010047if(ENABLE_COVERAGE)
48 # Destination
49 set(COVERAGE_DIR "${CMAKE_BINARY_DIR}/tests/code_coverage/")
50 set(COVERAGE_FILE_RAW "${CMAKE_BINARY_DIR}/tests/coverage_raw.info")
51 set(COVERAGE_FILE_CLEAN "${CMAKE_BINARY_DIR}/tests/coverage_clean.info")
52
53 # Add coverage target
54 add_custom_target(coverage
55 COMMENT "Generating code coverage..."
Radek Krejci8c1054b2019-05-17 10:18:54 +020056 WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
Radek Krejci6208cfa2019-03-08 14:43:58 +010057 # Cleanup code counters
58 COMMAND "${PATH_LCOV}" --directory . --zerocounters --quiet
59
60 # Run tests
61 COMMAND "${CMAKE_CTEST_COMMAND}" --quiet
62
63 # Capture the counters
64 COMMAND "${PATH_LCOV}"
65 --directory .
66 --rc lcov_branch_coverage=1
67 --rc 'lcov_excl_line=assert'
68 --capture --quiet
69 --output-file "${COVERAGE_FILE_RAW}"
70 # Remove coverage of tests, system headers, etc.
71 COMMAND "${PATH_LCOV}"
Radek Krejci8c1054b2019-05-17 10:18:54 +020072 --remove "${COVERAGE_FILE_RAW}" '${CMAKE_SOURCE_DIR}/tests/*'
Radek Krejci6208cfa2019-03-08 14:43:58 +010073 --rc lcov_branch_coverage=1
74 --quiet --output-file "${COVERAGE_FILE_CLEAN}"
75 # Generate HTML report
76 COMMAND "${PATH_GENHTML}"
77 --branch-coverage --function-coverage --quiet --title "libyang"
78 --legend --show-details --output-directory "${COVERAGE_DIR}"
79 "${COVERAGE_FILE_CLEAN}"
80 # Delete the counters
81 COMMAND "${CMAKE_COMMAND}" -E remove
82 ${COVERAGE_FILE_RAW} ${COVERAGE_FILE_CLEAN}
83 )
84
85 add_custom_command(TARGET coverage POST_BUILD
86 WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/tests"
87 COMMENT "To see the code coverage report, open ${COVERAGE_DIR}index.html"
88 COMMAND ;
89 )
90endif()
Radek Krejcicc3a1b62020-07-20 07:43:08 +020091
92add_subdirectory(style)
Radek Krejci5902be92021-03-25 21:25:14 +010093add_subdirectory(plugins)
Radek Krejcicc3a1b62020-07-20 07:43:08 +020094add_subdirectory(utests)
Juraj Vijtiukd7989422020-09-15 13:54:00 +020095add_subdirectory(fuzz)
Radek Krejcib4ac5a92020-11-23 17:54:33 +010096
97set(format_sources ${format_sources} PARENT_SCOPE)