1# Taken from amazon-freertos repository
2cmake_minimum_required(VERSION 3.13)
3set(BINARY_DIR ${CMAKE_BINARY_DIR})
4set(CMOCK_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../tools/CMock )
5
6
7# reset coverage counters
8execute_process(
9            COMMAND lcov --directory ${CMAKE_BINARY_DIR}
10                         --base-directory ${CMAKE_BINARY_DIR}
11                         --zerocounters
12
13            COMMAND mkdir -p  ${CMAKE_BINARY_DIR}/coverage
14        )
15# make the initial/baseline capture a zeroed out files
16execute_process( COMMAND lcov --directory ${CMAKE_BINARY_DIR}
17                         --base-directory ${CMAKE_BINARY_DIR}
18                         --initial
19                         --capture
20                         --rc lcov_branch_coverage=1
21                         --rc genhtml_branch_coverage=1
22                         --output-file=${CMAKE_BINARY_DIR}/base_coverage.info
23        )
24
25file(GLOB files "${CMAKE_BINARY_DIR}/bin/tests/*")
26
27set(REPORT_FILE ${CMAKE_BINARY_DIR}/utest_report.txt)
28file(WRITE ${REPORT_FILE} "")
29
30# execute all files in bin directory, gathering the output to show it in CI
31foreach(testname ${files})
32    get_filename_component(test
33                           ${testname}
34                           NAME_WLE
35            )
36    message("Running ${testname}")
37    execute_process(COMMAND ${testname} OUTPUT_FILE ${CMAKE_BINARY_DIR}/${test}_out.txt)
38
39    file(READ ${CMAKE_BINARY_DIR}/${test}_out.txt CONTENTS)
40    file(APPEND ${REPORT_FILE} "${CONTENTS}")
41endforeach()
42
43# generate Junit style xml output
44execute_process(COMMAND ruby
45    ${CMOCK_DIR}/vendor/unity/auto/parse_output.rb
46                    -xml ${REPORT_FILE}
47                    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
48            )
49
50# capture data after running the tests
51execute_process(
52            COMMAND lcov --capture
53                         --rc lcov_branch_coverage=1
54                         --rc genhtml_branch_coverage=1
55                         --base-directory ${CMAKE_BINARY_DIR}
56                         --directory ${CMAKE_BINARY_DIR}
57                         --output-file ${CMAKE_BINARY_DIR}/second_coverage.info
58        )
59
60# Remove existing coverage file.
61execute_process(
62            COMMAND rm -f ${CMAKE_BINARY_DIR}/coverage.info
63        )
64
65# combile baseline results (zeros) with the one after running the tests
66execute_process(
67            COMMAND lcov --base-directory ${CMAKE_BINARY_DIR}
68                         --directory ${CMAKE_BINARY_DIR}
69                         --add-tracefile ${CMAKE_BINARY_DIR}/base_coverage.info
70                         --add-tracefile ${CMAKE_BINARY_DIR}/second_coverage.info
71                         --output-file ${CMAKE_BINARY_DIR}/coverage.info
72                         --no-external
73                         --rc lcov_branch_coverage=1
74        )
75
76if(1)
77# Remove kernel coverage
78execute_process(
79           COMMAND lcov --rc lcov_branch_coverage=1
80                        --remove ${CMAKE_BINARY_DIR}/coverage.info
81                        *FreeRTOS-Kernel*
82                        --output-file ${CMAKE_BINARY_DIR}/coverage.info
83       )
84
85# Remove portable coverage
86execute_process(
87           COMMAND lcov --rc lcov_branch_coverage=1
88                        --remove ${CMAKE_BINARY_DIR}/coverage.info
89                        *portable*
90                        --output-file ${CMAKE_BINARY_DIR}/coverage.info
91       )
92
93# Remove stubs coverage
94execute_process(
95           COMMAND lcov --rc lcov_branch_coverage=1
96                        --remove ${CMAKE_BINARY_DIR}/coverage.info
97                        *stubs*
98                        --output-file ${CMAKE_BINARY_DIR}/coverage.info
99       )
100endif()
101
102execute_process(
103            COMMAND genhtml --rc lcov_branch_coverage=1
104                            --branch-coverage
105                            --output-directory ${CMAKE_BINARY_DIR}/coverage
106                                ${CMAKE_BINARY_DIR}/coverage.info
107        )
108
109