Skip to content

Instantly share code, notes, and snippets.

@pankajgangwar
Last active March 6, 2024 22:09
Show Gist options
  • Save pankajgangwar/f070b8b54e83543f8e3638dcd2cae1b8 to your computer and use it in GitHub Desktop.
Save pankajgangwar/f070b8b54e83543f8e3638dcd2cae1b8 to your computer and use it in GitHub Desktop.
How to generate native coverage on Android with soong build system.
#!/usr/bin/env bash
###### Module specific parameters starts ######
MODULE_PATH='hardware/interfaces/automotive/vehicle/2.0/default/'
MODULE_INSTALL_PATH='data/nativetest64/vehicle-tests'
INSTRUMENTED_BINARY_NAME='vehicle-tests'
REMOTE_COVERAGE_OUTPUT_DIR='/data/local/tmp/nativetest64/'
SOONG_INTERMEDIATES="out/soong/.intermediates"
GCNO_DIR="$SOONG_INTERMEDIATES/$MODULE_PATH"
###### Module specific parameters ends ########
if [ -z $ANDROID_BUILD_TOP ]; then
echo "You need to source and lunch before you can use this script"
exit 1
fi
source $ANDROID_BUILD_TOP/build/envsetup.sh
## check if gcc-4.6 is installed
if ! [ -x "$(command -v gcc-4.6)" ]; then
echo "gcc-4.6 not found. sudo apt-get install gcc-4.6 to use this script"
exit 1
fi
## check if lcov-1.12 is installed
if ! [ -x "$(command -v lcov)" ]; then
echo "lcov not found. sudo apt-get install lcov to use this script"
exit 1
fi
## Clean previous gcno files from out dir
find "$ANDROID_BUILD_TOP/$GCNO_DIR" -iname "*.gcno" -exec rm -rf "{}" \;
OUTPUT_DIR="$OUT/coverage/$MODULE_INSTALL_PATH"
## Clean previous report if any
rm -rf $OUTPUT_DIR
REPORT_TYPE=${1:-HTML}
echo "Running native tests and generating coverage report"
echo "Output dir: $OUT/coverage/$MODULE_INSTALL_PATH/ "
echo "Report type: $REPORT_TYPE"
set -e # fail early
(cd "$ANDROID_BUILD_TOP/$MODULE_PATH" && ALLOW_MISSING_DEPENDENCIES=true NATIVE_COVERAGE=true mma -j32)
set -x # print commands
adb root
adb wait-for-device
adb shell mkdir -p $REMOTE_COVERAGE_OUTPUT_DIR
#clean old gcda files from device
adb shell find $REMOTE_COVERAGE_OUTPUT_DIR -iname "*.gcda" -exec rm -rf "{}" "\;"
adb push $OUT/$MODULE_INSTALL_PATH/$INSTRUMENTED_BINARY_NAME /data/nativetest64/
## Make this module binary exectuable
adb shell chmod +x /data/nativetest64/$INSTRUMENTED_BINARY_NAME
adb shell \
GCOV_PREFIX=$REMOTE_COVERAGE_OUTPUT_DIR \
GCOV_PREFIX_STRIP=`echo $ANDROID_BUILD_TOP | grep -o / | wc -l` \
/data/nativetest64/$INSTRUMENTED_BINARY_NAME
OUT_COVERAGE=$OUT/coverage
## Pull coverage data from device
adb pull $REMOTE_COVERAGE_OUTPUT_DIR $OUT_COVERAGE/$MODULE_INSTALL_PATH/
cd "$OUT_COVERAGE/$MODULE_INSTALL_PATH"
## Move gcda files from out/soong/.intermediates to current dir
find . -iname "*.gcda" -exec mv "{}" . \;
## Extract gcno from gcnodir archive
ar x $INSTRUMENTED_BINARY_NAME.gcnodir
## Read gcda files with llvm-cov
llvm-cov gcov -f -b *.gcda
## Collect the code coverage results
lcov --directory . --base-directory . --gcov-tool /usr/bin/gcov-4.6 --capture -o cov.info
## Generate HTML files
genhtml cov.info -o report
#cleaning all intermediates coverage files
rm -rf *.gcov cov.info *.gcno *.gcda
set +x
# Echo the file as URI to quickly open the result using ctrl-click in terminal
echo "file://$OUT_COVERAGE/$MODULE_INSTALL_PATH/report/index.html"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment