|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2018-2019 Uber Technologies, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# To invoke this script, use `bash coverage.sh <source-dir> <binary-dir>`, |
| 18 | +# where `source-dir` corresponds to CMake source directory and `binary-dir` |
| 19 | +# corresponds to the CMake build directory. This script is used to run the |
| 20 | +# `coverage` target. In order to guarantee that `coverage` is only a valid |
| 21 | +# target in debug builds, we must know the build mode. However, CMake tries to |
| 22 | +# delay the selection of the build mode until the build itself. This allows |
| 23 | +# tools like Xcode to choose to build in debug mode or release mode without |
| 24 | +# rerunning CMake. In order to control the `coverage` target based on the build |
| 25 | +# mode, this script is regenerated whenever a new build mode is selected, |
| 26 | +# regardless of whether or not the `cmake` command is invoked again. |
| 27 | +# |
| 28 | +# Example: |
| 29 | +# |
| 30 | +# ``` |
| 31 | +# $ cmake -DCMAKE_BUILD_TYPE=Debug .. |
| 32 | +# $ xcodebuild -configuration Release # coverage.sh generated |
| 33 | +# $ xcodebuild -configuration Release # coverage.sh not regenerated |
| 34 | +# $ xcodebuild -configuration Debug # coverage.sh regenerated |
| 35 | +# ``` |
| 36 | + |
| 37 | +set -e |
| 38 | + |
| 39 | +if [[ $<BOOL:$<CONFIG:Debug>> != 1 ]]; then |
| 40 | + echo "Cannot run coverage for non-debug build" 1>&2 |
| 41 | + exit 1 |
| 42 | +fi |
| 43 | + |
| 44 | +src_dir=${1:-"Missing source directory"} |
| 45 | +binary_dir=${2:-"Missing binary directory"} |
| 46 | + |
| 47 | +# Exclude the usual LCOV exclusion comment, and also |
| 48 | +# do not require branch coverage for assertions. |
| 49 | +br_exclusion='LCOV_EXCL_BR_LINE|assert\(' |
| 50 | + |
| 51 | +cd "${binary_dir}" |
| 52 | +lcov --rc lcov_branch_coverage=1 --rc "lcov_excl_br_line=$br_exclusion" --directory . --capture --output-file coverage.info |
| 53 | +lcov --rc lcov_branch_coverage=1 --rc "lcov_excl_br_line=$br_exclusion" --extract coverage.info "${src_dir}/src/*" --output-file coverage.cleaned.info |
| 54 | +genhtml --branch-coverage -o coverage coverage.cleaned.info --title 'h3 coverage' |
0 commit comments