Skip to content

Commit ec63743

Browse files
Copilotachamayou
andauthored
Add CMake code coverage support and coverage aggregation script (#7740)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: achamayou <4016369+achamayou@users.noreply.github.com> Co-authored-by: Amaury Chamayou <amaury@xargs.fr> Co-authored-by: Amaury Chamayou <amchamay@microsoft.com>
1 parent 968818c commit ec63743

8 files changed

Lines changed: 368 additions & 0 deletions

File tree

.github/workflows/ci.yml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,109 @@ jobs:
181181
if-no-files-found: ignore
182182
if: success() || failure()
183183

184+
vmss-virtual-c:
185+
name: "VMSS Virtual C" # Code coverage
186+
runs-on:
187+
[
188+
self-hosted,
189+
1ES.Pool=gha-vmss-d16av5-ci,
190+
"JobId=ci_coverage-${{ github.run_id }}-${{ github.run_number }}-${{ github.run_attempt }}",
191+
]
192+
container:
193+
image: mcr.microsoft.com/azurelinux/base/core:3.0
194+
options: --user root --publish-all --cap-add NET_ADMIN --cap-add NET_RAW --cap-add SYS_PTRACE
195+
196+
steps:
197+
- name: "Checkout dependencies"
198+
shell: bash
199+
run: |
200+
set -ex
201+
gpg --import /etc/pki/rpm-gpg/MICROSOFT-RPM-GPG-KEY
202+
tdnf -y update
203+
tdnf -y install ca-certificates git
204+
205+
- uses: actions/checkout@v6
206+
with:
207+
fetch-depth: 0
208+
209+
- name: "Install dependencies"
210+
shell: bash
211+
run: |
212+
set -ex
213+
./scripts/setup-ci.sh
214+
215+
- name: "Build Debug with coverage"
216+
run: |
217+
set -ex
218+
git config --global --add safe.directory /__w/CCF/CCF
219+
mkdir build
220+
cd build
221+
cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -DCOVERAGE=ON ..
222+
ninja
223+
shell: bash
224+
225+
- name: "Run Unit tests"
226+
run: |
227+
set -ex
228+
cd build
229+
./tests.sh --output-on-failure -L unit -j$(nproc --all)
230+
shell: bash
231+
232+
- name: "End to end tests except partition"
233+
run: |
234+
set -ex
235+
cd build
236+
rm -rf /github/home/.cache
237+
mkdir -p /github/home/.cache
238+
239+
# End to end tests
240+
./tests.sh --timeout 360 --output-on-failure -LE "benchmark|suite|unit"
241+
shell: bash
242+
243+
- name: "Generate coverage reports"
244+
run: |
245+
set -ex
246+
cd build
247+
../scripts/coverage.sh --html coverage_html | tee coverage_report.txt
248+
shell: bash
249+
250+
- name: "Write coverage summary"
251+
if: always()
252+
run: |
253+
set -ex
254+
cd build
255+
if [[ -f coverage_report.txt ]]; then
256+
{
257+
echo '## Code Coverage'
258+
echo ''
259+
echo '```'
260+
cat coverage_report.txt
261+
echo '```'
262+
} >> "$GITHUB_STEP_SUMMARY"
263+
fi
264+
shell: bash
265+
266+
- name: "Upload HTML coverage report"
267+
uses: actions/upload-artifact@v7
268+
with:
269+
name: coverage-report-html
270+
path: build/coverage_html/
271+
if-no-files-found: ignore
272+
if: success() || failure()
273+
274+
- name: "Upload logs for virtual C"
275+
uses: actions/upload-artifact@v7
276+
with:
277+
name: logs-azurelinux-virtual-c
278+
path: |
279+
build/workspace/*/*.config.json
280+
build/workspace/*/out
281+
build/workspace/*/err
282+
build/workspace/*/*.ledger/*
283+
build/workspace/*/stack_trace
284+
if-no-files-found: ignore
285+
if: success() || failure()
286+
184287
aci_snp_milan:
185288
name: "ACI SNP Milan"
186289
runs-on:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1616
- Added `ccf::describe_cose_receipt_v1(receipt)` to obtain COSE receipts with Merkle proof in unprotected header for non-signature TXs, and empty unprotected header for signature TXs (#7700).
1717
- `NetworkIdentitySubsystemInterface` now exposes `get_trusted_keys()`, returning all trusted network identity keys as a `TrustedKeys` map (#7690).
1818
- Added support for self-transparent code update policies (#7681).
19+
- Added `scripts/coverage.sh` to aggregate LLVM coverage data produced by tests built with `-DCOVERAGE=ON`. The script merges all `.profraw` files in the build directory, prints an overall coverage summary, and optionally shows uncovered lines (`--show-uncovered`) or generates an HTML report (`--html <dir>`). CMake now also generates `coverage_binaries.txt` in the build directory listing all instrumented test binaries, and automatically sets `LLVM_PROFILE_FILE` for each unit test so that parallel test runs produce uniquely-named profile files.
1920

2021
### Changed
2122

CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,21 @@ if(BUILD_TESTS)
12701270
PYTHON_SCRIPT ${CMAKE_SOURCE_DIR}/tests/consistency_trace_validation.py
12711271
)
12721272
endif()
1273+
1274+
if(COVERAGE)
1275+
# Generate a list of coverage-instrumented binaries for scripts/coverage.sh
1276+
get_property(_cov_targets GLOBAL PROPERTY CCF_COVERAGE_TARGETS)
1277+
set(_cov_binaries "")
1278+
foreach(_target ${_cov_targets})
1279+
list(APPEND _cov_binaries "$<TARGET_FILE:${_target}>")
1280+
endforeach()
1281+
string(REPLACE ";" "\n" _cov_binaries_nl "${_cov_binaries}")
1282+
file(
1283+
GENERATE
1284+
OUTPUT "${CMAKE_BINARY_DIR}/coverage_binaries.txt"
1285+
CONTENT "${_cov_binaries_nl}\n"
1286+
)
1287+
endif()
12731288
endif()
12741289

12751290
# Generate and install CMake export file for consumers using CMake

cmake/ccf_app.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ function(add_ccf_app name)
4848

4949
add_san(${name})
5050
add_tidy(${name})
51+
enable_coverage(${name})
5152

5253
if(USE_SNMALLOC)
5354
target_link_libraries(${name} INTERFACE snmallocshim-static)

cmake/common.cmake

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ function(add_unit_test name)
4444
PROPERTY LABELS unit
4545
)
4646

47+
if(COVERAGE)
48+
set_property(
49+
TEST ${name}
50+
APPEND
51+
PROPERTY ENVIRONMENT "LLVM_PROFILE_FILE=${name}-%p.profraw"
52+
)
53+
endif()
54+
4755
add_san_test_properties(${name})
4856
endfunction()
4957

@@ -132,6 +140,16 @@ function(add_e2e_test)
132140

133141
add_san_test_properties(${PARSED_ARGS_NAME})
134142

143+
if(COVERAGE)
144+
set_property(
145+
TEST ${PARSED_ARGS_NAME}
146+
APPEND
147+
PROPERTY
148+
ENVIRONMENT
149+
"LLVM_PROFILE_FILE=${CMAKE_BINARY_DIR}/${PARSED_ARGS_NAME}-%p.profraw"
150+
)
151+
endif()
152+
135153
set_property(
136154
TEST ${PARSED_ARGS_NAME}
137155
APPEND

cmake/tools.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@ function(enable_coverage name)
5454
if(COVERAGE)
5555
target_compile_options(${name} PRIVATE ${COVERAGE_FLAGS})
5656
target_link_libraries(${name} PRIVATE ${COVERAGE_LINK})
57+
set_property(GLOBAL APPEND PROPERTY CCF_COVERAGE_TARGETS ${name})
5758
endif()
5859
endfunction()

0 commit comments

Comments
 (0)