Skip to content

Commit 82663ac

Browse files
kirklandsignfacebook-github-bot
authored andcommitted
Collect OSS GTest code coverage (#3734)
Summary: Use LLVM coverage tool to collect coverage. We print it to log for now (https://productionresultssa7.blob.core.windows.net/actions-results/36302d2c-4406-4404-88c1-7b3e63331252/workflow-job-run-00b9f9ca-16b3-535c-a3c3-70113f2f2d24/logs/job/job-logs.txt?rsct=text%2Fplain&se=2024-05-28T21%3A48%3A03Z&sig=xgur5uWUJVMNRxkwkm%2BSeqCFFjsoYcnQKtFksKHcLHo%3D&sp=r&spr=https&sr=b&st=2024-05-28T21%3A37%3A58Z&sv=2021-12-02 and search for `Branches Missed Branches Cover`) Later we can process the data in a separate workflow. Pull Request resolved: #3734 Reviewed By: huydhn Differential Revision: D57792685 Pulled By: kirklandsign fbshipit-source-id: 22942cdb012276d2541ff9fbff7f07014b87197b
1 parent 8f65b6d commit 82663ac

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

.github/workflows/_unittest.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ jobs:
3737
# Run pytest with coverage
3838
pytest -n auto --cov=./ --cov-report=xml
3939
# Run gtest
40+
LLVM_PROFDATA=llvm-profdata-12 LLVM_COV=llvm-cov-12 \
4041
test/run_oss_cpp_tests.sh
4142
4243
macos:
@@ -66,4 +67,5 @@ jobs:
6667
# Run pytest with coverage
6768
${CONDA_RUN} pytest -n auto --cov=./ --cov-report=xml
6869
# Run gtest
70+
LLVM_PROFDATA="xcrun llvm-profdata" LLVM_COV="xcrun llvm-cov" \
6971
${CONDA_RUN} test/run_oss_cpp_tests.sh

CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,23 @@ else()
343343
set(CMAKE_TOOLCHAIN_ANDROID OFF)
344344
endif()
345345

346+
# Add code coverage flags to supported compilers
347+
if(EXECUTORCH_USE_CPP_CODE_COVERAGE)
348+
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
349+
string(APPEND CMAKE_C_FLAGS " --coverage -fprofile-abs-path")
350+
string(APPEND CMAKE_CXX_FLAGS " --coverage -fprofile-abs-path")
351+
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
352+
string(APPEND CMAKE_C_FLAGS " -fprofile-instr-generate -fcoverage-mapping")
353+
string(APPEND CMAKE_CXX_FLAGS
354+
" -fprofile-instr-generate -fcoverage-mapping"
355+
)
356+
else()
357+
message(ERROR
358+
"Code coverage for compiler ${CMAKE_CXX_COMPILER_ID} is unsupported"
359+
)
360+
endif()
361+
endif()
362+
346363
# EXECUTORCH_BUILD_HOST_TARGETS: Option to control the building of host-only
347364
# tools like `flatc`, along with example executables like `executor_runner` and
348365
# libraries that it uses, like `gflags`. Disabling this can be helpful when

build/Test.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@
1717
# It should also be cmake-lint clean.
1818
#
1919

20+
# Add code coverage flags to supported compilers
21+
if(EXECUTORCH_USE_CPP_CODE_COVERAGE)
22+
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
23+
string(APPEND CMAKE_C_FLAGS " --coverage -fprofile-abs-path")
24+
string(APPEND CMAKE_CXX_FLAGS " --coverage -fprofile-abs-path")
25+
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
26+
string(APPEND CMAKE_C_FLAGS " -fprofile-instr-generate -fcoverage-mapping")
27+
string(APPEND CMAKE_CXX_FLAGS " -fprofile-instr-generate -fcoverage-mapping")
28+
else()
29+
message(ERROR "Code coverage for compiler ${CMAKE_CXX_COMPILER_ID} is unsupported")
30+
endif()
31+
endif()
32+
2033
# A helper function to generate a gtest cxx executable target
2134
# @param target_name: name for the executable
2235
# @param SOURCES <list_of_sources>: test sources to be compiled. Sometimes

test/run_oss_cpp_tests.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,18 @@
1414

1515
set -ex
1616

17+
if [[ $(uname) == "Darwin" ]]; then
18+
export LLVM_PROFDATA="${LLVM_PROFDATA:-xcrun llvm-profdata}"
19+
export LLVM_COV="${LLVM_COV:-xcrun llvm-cov}"
20+
elif [[ $(uname) == "Linux" ]]; then
21+
export LLVM_PROFDATA="${LLVM_PROFDATA:-llvm-profdata}"
22+
export LLVM_COV="${LLVM_COV:-llvm-cov}"
23+
fi
24+
1725
build_executorch() {
1826
cmake . \
1927
-DCMAKE_INSTALL_PREFIX=cmake-out \
28+
-DEXECUTORCH_USE_CPP_CODE_COVERAGE=ON \
2029
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
2130
-Bcmake-out
2231
cmake --build cmake-out -j9 --target install
@@ -35,17 +44,24 @@ build_and_run_test() {
3544
local test_dir=$1
3645
cmake "${test_dir}" \
3746
-DCMAKE_INSTALL_PREFIX=cmake-out \
47+
-DEXECUTORCH_USE_CPP_CODE_COVERAGE=ON \
3848
-DCMAKE_PREFIX_PATH="$(pwd)/third-party/googletest/build" \
3949
-Bcmake-out/"${test_dir}"
4050
cmake --build cmake-out/"${test_dir}" -j9
4151

4252
for t in cmake-out/"${test_dir}"/*test; do
4353
if [ -e "$t" ]; then
44-
./"$t";
54+
LLVM_PROFILE_FILE="cmake-out/$(basename $t).profraw" ./"$t";
55+
TEST_BINARY_LIST="${TEST_BINARY_LIST} -object $t"
4556
fi
4657
done
4758
}
4859

60+
report_coverage() {
61+
"${LLVM_PROFDATA}" merge -sparse cmake-out/*.profraw -o cmake-out/merged.profdata
62+
"${LLVM_COV}" report -instr-profile=cmake-out/merged.profdata $TEST_BINARY_LIST
63+
}
64+
4965
probe_tests() {
5066
# This function finds the set of directories that contain C++ tests
5167
# CMakeLists.txt rules, that are buildable using build_and_run_test
@@ -79,3 +95,5 @@ if [ -z "$1" ]; then
7995
else
8096
build_and_run_test "$1"
8197
fi
98+
99+
report_coverage || true

test/utils/generate_gtest_cmakelists.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,3 @@ def read_config_json(json_path):
7373
)
7474
else:
7575
print(f"Please run cmake-format -i {path_to_root}/CMakeLists.txt")
76-
print("Note: Please update test/run_oss_cpp_tests.sh")

0 commit comments

Comments
 (0)