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_ciadd_custom_target(bytecode_optimizer_coverage DEPENDS bytecodeopt_unit_tests)
15b1994897Sopenharmony_ci
16b1994897Sopenharmony_ciadd_custom_command(TARGET bytecode_optimizer_coverage POST_BUILD
17b1994897Sopenharmony_ci    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
18b1994897Sopenharmony_ci    COMMAND bash ${PANDA_ROOT}/bytecode_optimizer/tools/optimizer_coverage.sh --binary-dir=${PANDA_BINARY_ROOT} --root-dir=${PANDA_ROOT}
19b1994897Sopenharmony_ci)
20b1994897Sopenharmony_ci
21b1994897Sopenharmony_cioption(ENABLE_BYTECODE_OPTIMIZER_COVERAGE "Enable coverage calculation for the bytecode optimizer" false)
22b1994897Sopenharmony_ci
23b1994897Sopenharmony_cifind_program(
24b1994897Sopenharmony_ci    LCOV
25b1994897Sopenharmony_ci    NAMES "lcov"
26b1994897Sopenharmony_ci    DOC "Path to lcov executable")
27b1994897Sopenharmony_ciif(NOT LCOV)
28b1994897Sopenharmony_ci    set(ENABLE_BYTECODE_OPTIMIZER_COVERAGE false)
29b1994897Sopenharmony_ciendif()
30b1994897Sopenharmony_ci
31b1994897Sopenharmony_cifind_program(
32b1994897Sopenharmony_ci    GENHTML
33b1994897Sopenharmony_ci    NAMES "genhtml"
34b1994897Sopenharmony_ci    DOC "Path to genhtml executable")
35b1994897Sopenharmony_ciif(NOT GENHTML)
36b1994897Sopenharmony_ci    set(ENABLE_BYTECODE_OPTIMIZER_COVERAGE false)
37b1994897Sopenharmony_ciendif()
38b1994897Sopenharmony_ci
39b1994897Sopenharmony_ciif(ENABLE_BYTECODE_OPTIMIZER_COVERAGE)
40b1994897Sopenharmony_ci    # Set coverage options
41b1994897Sopenharmony_ci    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
42b1994897Sopenharmony_ci    add_custom_target(bo_coverage DEPENDS bytecodeopt_unit_tests) # Execute tests
43b1994897Sopenharmony_ci    set(ADD_COV_FLAGS --quiet --rc lcov_branch_coverage=1)
44b1994897Sopenharmony_ci    add_custom_command(TARGET bo_coverage POST_BUILD
45b1994897Sopenharmony_ci        WORKING_DIRECTORY ${PANDA_BINARY_ROOT}
46b1994897Sopenharmony_ci        # Update current coverage info
47b1994897Sopenharmony_ci        COMMAND lcov --no-external -b ${PANDA_ROOT}/bytecode_optimizer -d  ${CMAKE_CURRENT_BINARY_DIR} -c -o bo_coverage.info ${ADD_COV_FLAGS}
48b1994897Sopenharmony_ci        # Generating an html report
49b1994897Sopenharmony_ci        COMMAND genhtml -o bo_coverage_report bo_coverage.info --ignore-errors source ${ADD_COV_FLAGS}
50b1994897Sopenharmony_ci        COMMAND echo "Coverage report: ${PANDA_BINARY_ROOT}/bo_coverage_report"
51b1994897Sopenharmony_ci        # Delete temporary files to collect statistics
52b1994897Sopenharmony_ci        COMMAND rm bo_coverage.info
53b1994897Sopenharmony_ci        COMMAND find ${PANDA_BINARY_ROOT}/* -iname "*.gcda" -delete
54b1994897Sopenharmony_ci    )
55b1994897Sopenharmony_cielse()
56b1994897Sopenharmony_ci    message(STATUS "Coverage will not be calculated (may be enabled by -DENABLE_BYTECODE_OPTIMIZER_COVERAGE=true ).")
57b1994897Sopenharmony_ciendif(ENABLE_BYTECODE_OPTIMIZER_COVERAGE)
58