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