|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -e -o pipefail |
| 4 | +set +x |
| 5 | +set -u |
| 6 | + |
| 7 | +LLVM_VERSION=${LLVM_VERSION:-"18.1.8"} |
| 8 | +CLANG_VERSION=$(clang-18 --version | grep version | sed -e 's/\ *Ubuntu clang version \([0-9.]*\).*/\1/') |
| 9 | +LLVM_COV_VERSION=$(llvm-cov --version | grep version | sed -e 's/\ *Ubuntu LLVM version \([0-9.]*\).*/\1/') |
| 10 | +LLVM_PROFDATA_VERSION=$(llvm-profdata show --version | grep version | sed -e 's/\ *Ubuntu LLVM version \(.*\)/\1/') |
| 11 | +SRCDIR=${SRCDIR:-"${PWD}"} |
| 12 | + |
| 13 | +#ERROR: clang version Ubuntu18.1.3 does not match expected 18.1.3 |
| 14 | +if [[ "${CLANG_VERSION}" != "${LLVM_VERSION}" ]]; then |
| 15 | + echo "ERROR: clang version ${CLANG_VERSION} does not match expected ${LLVM_VERSION}" >&2 |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +if [[ "${LLVM_COV_VERSION}" != "${LLVM_VERSION}" ]]; then |
| 20 | + echo "ERROR: llvm-cov version ${LLVM_COV_VERSION} does not match expected ${LLVM_VERSION}" >&2 |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +if [[ "${LLVM_PROFDATA_VERSION}" != "${LLVM_VERSION}" ]]; then |
| 25 | + echo "ERROR: llvm-profdata version ${LLVM_PROFDATA_VERSION} does not match expected ${LLVM_VERSION}" >&2 |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +COVERAGE_TARGET="${COVERAGE_TARGET:-}" |
| 30 | +#TBD propogate any important global build options |
| 31 | +read -ra BAZEL_BUILD_OPTIONS <<< "${BAZEL_BUILD_OPTION_LIST:-}" |
| 32 | +read -ra BAZEL_GLOBAL_OPTIONS <<< "${BAZEL_GLOBAL_OPTION_LIST:-}" |
| 33 | + |
| 34 | +# This is the target that will be run to generate coverage data. It can be overridden by consumer |
| 35 | +# projects that want to run coverage on a different/combined target. |
| 36 | +# Command-line arguments take precedence over ${COVERAGE_TARGET}. |
| 37 | +if [[ $# -gt 0 ]]; then |
| 38 | + COVERAGE_TARGETS=("$@") |
| 39 | +elif [[ -n "${COVERAGE_TARGET}" ]]; then |
| 40 | + COVERAGE_TARGETS=("${COVERAGE_TARGET}") |
| 41 | +else |
| 42 | + COVERAGE_TARGETS=(//tests/...) |
| 43 | +fi |
| 44 | + |
| 45 | +BAZEL_COVERAGE_OPTIONS=() |
| 46 | +BAZEL_COVERAGE_OPTIONS+=(--heap_dump_on_oom) |
| 47 | +BAZEL_COVERAGE_OPTIONS+=(--action_env=BAZEL_USE_LLVM_NATIVE_COVERAGE=1) |
| 48 | +BAZEL_COVERAGE_OPTIONS+=(--combined_report=lcov) |
| 49 | +BAZEL_COVERAGE_OPTIONS+=(--coverage_report_generator=//tools/coverage:cilium_report_generator) |
| 50 | +BAZEL_COVERAGE_OPTIONS+=(--experimental_use_llvm_covmap) |
| 51 | +BAZEL_COVERAGE_OPTIONS+=(--experimental_generate_llvm_lcov) |
| 52 | +BAZEL_COVERAGE_OPTIONS+=(--experimental_split_coverage_postprocessing) |
| 53 | +BAZEL_COVERAGE_OPTIONS+=(--experimental_fetch_all_coverage_outputs) |
| 54 | +BAZEL_COVERAGE_OPTIONS+=(--collect_code_coverage) |
| 55 | +BAZEL_COVERAGE_OPTIONS+=(--remote_download_minimal) |
| 56 | +BAZEL_COVERAGE_OPTIONS+=(--copt=-DNDEBUG) |
| 57 | +BAZEL_COVERAGE_OPTIONS+=(--build_tests_only) |
| 58 | +#from envoy |
| 59 | +BAZEL_COVERAGE_OPTIONS+=(--experimental_repository_cache_hardlinks) |
| 60 | +BAZEL_COVERAGE_OPTIONS+=(--verbose_failures) |
| 61 | +BAZEL_COVERAGE_OPTIONS+=(--experimental_generate_json_trace_profile) |
| 62 | +BAZEL_COVERAGE_OPTIONS+=(--action_env=GCOV=llvm-profdata) |
| 63 | +BAZEL_VALIDATE_OPTIONS=() |
| 64 | + |
| 65 | + |
| 66 | +# Output unusually long logs due to trace logging. |
| 67 | +BAZEL_COVERAGE_OPTIONS+=("--experimental_ui_max_stdouterr_bytes=80000000") |
| 68 | +BAZEL_BUILD_OPTIONS+=("--remote_cache=https://storage.googleapis.com/cilium-proxy-bazel-remote-cache") |
| 69 | +BAZEL_BUILD_OPTIONS+=("--google_default_credentials") |
| 70 | + |
| 71 | +COVERAGE_DIR="${SRCDIR}/generated/coverage" |
| 72 | + |
| 73 | +COVERAGE_DATA="${COVERAGE_DIR}/coverage.dat" |
| 74 | + |
| 75 | + |
| 76 | +run_coverage() { |
| 77 | + echo "Running bazel coverage with:" |
| 78 | + echo " Options: ${BAZEL_BUILD_OPTIONS[*]} ${BAZEL_COVERAGE_OPTIONS[*]}" |
| 79 | + echo " Targets: ${COVERAGE_TARGETS[*]}" |
| 80 | + bazel coverage "${COVERAGE_TARGETS[@]}" "${BAZEL_BUILD_OPTIONS[@]}" "${BAZEL_COVERAGE_OPTIONS[@]}" --compiler=clang-18 --verbose_failures --sandbox_writable_path=$(bazel info output_path) --test_timeout=300 --local_test_jobs=1 --flaky_test_attempts=3 --instrument_test_targets --instrumentation_filter='^//' |
| 81 | + |
| 82 | + if [[ ! -e bazel-out/_coverage/_coverage_report.dat ]]; then |
| 83 | + echo "ERROR: No coverage report found (bazel-out/_coverage/_coverage_report.dat)" >&2 |
| 84 | + exit 1 |
| 85 | + elif [[ ! -s bazel-out/_coverage/_coverage_report.dat ]]; then |
| 86 | + echo "ERROR: Coverage report is empty (bazel-out/_coverage/_coverage_report.dat)" >&2 |
| 87 | + exit 1 |
| 88 | + fi |
| 89 | +} |
| 90 | + |
| 91 | +unpack_coverage_results() { |
| 92 | + rm -rf "${COVERAGE_DIR}" |
| 93 | + mkdir -p "${COVERAGE_DIR}" |
| 94 | + rm -f bazel-out/_coverage/_coverage_report.tar.zst |
| 95 | + mv bazel-out/_coverage/_coverage_report.dat bazel-out/_coverage/_coverage_report.tar.zst |
| 96 | + bazel run "${BAZEL_BUILD_OPTIONS[@]}" --nobuild_tests_only @envoy//tools/zstd -- -d -c "${PWD}/bazel-out/_coverage/_coverage_report.tar.zst" \ |
| 97 | + | tar -xf - -C "${COVERAGE_DIR}" |
| 98 | + COVERAGE_JSON="${COVERAGE_DIR}/coverage.json" |
| 99 | +} |
| 100 | + |
| 101 | +validate_coverage() { |
| 102 | + bazel run \ |
| 103 | + "${BAZEL_BUILD_OPTIONS[@]}" \ |
| 104 | + "${BAZEL_VALIDATE_OPTIONS[@]}" \ |
| 105 | + --nobuild_tests_only \ |
| 106 | + //tools/coverage:validate \ |
| 107 | + "$COVERAGE_JSON" |
| 108 | +} |
| 109 | + |
| 110 | +run_coverage |
| 111 | +unpack_coverage_results |
| 112 | +validate_coverage |
0 commit comments