1# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14option(ENABLE_MM_COVERAGE "Enable coverage-calculation for libpandabase" false)
15
16find_program(
17    LCOV
18    NAMES "lcov"
19    DOC "Path to lcov executable")
20if(NOT LCOV)
21    set(ENABLE_MM_COVERAGE false)
22endif()
23
24find_program(
25    GENHTML
26    NAMES "genhtml"
27    DOC "Path to genhtml executable")
28if(NOT GENHTML)
29    set(ENABLE_MM_COVERAGE false)
30endif()
31
32if(ENABLE_MM_COVERAGE)
33    # Set coverage options
34    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
35    add_custom_target(mm_coverage DEPENDS arkbase_tests arkbase_mem_range_tests)
36    set(ADD_COV_FLAGS --quiet --rc lcov_branch_coverage=1)
37    add_custom_command(TARGET mm_coverage POST_BUILD
38        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
39        # Update current coverage info
40        COMMAND find ${CMAKE_CURRENT_BINARY_DIR}/* -iname "*.gcda" -delete
41        # run tests
42        COMMAND arkbase_tests
43        COMMAND arkbase_mem_range_tests
44        # coverage
45        COMMAND lcov --no-external -b ${PANDA_ROOT}/libpandabase -d ${CMAKE_CURRENT_BINARY_DIR} -c -o mm_coverage.info ${ADD_COV_FLAGS}
46        # html-file generation
47        COMMAND genhtml -o mm_coverage_report mm_coverage.info --ignore-errors source ${ADD_COV_FLAGS}
48        COMMAND echo "Coverage report path: ${CMAKE_CURRENT_BINARY_DIR}/mm_coverage_report"
49        # Delete temporary files to collect statistics
50        COMMAND rm mm_coverage.info
51    )
52else()
53    message(STATUS "Coverage will not be calculated (may be enabled by -DENABLE_MM_COVERAGE=true ).")
54endif(ENABLE_MM_COVERAGE)
55