create php-src code coverage

Finding a up2date code coverage report for php is not so easy currently.
There is http://gcov.php.net/ but its not even listing one for php8, and the last one which is actually listed as working is php7.1
There is also http://qa.php.net/ but that somehow did not help me much.

Not having experience with C, and where to start searching I took the lame attempt to google for the title of the code coverage report “LCOV – code coverage report” and learnt about the tool LCOV and also found this gist https://gist.github.com/mattparker/90a8e5b4411095e23403

Requirements:
Install: lcov, gcovr
Then I run this modified version, with a separate prefix for the installed executable, so I can run the testsuite with it. (in case I even need it)

./configure  --enable-gcov --prefix=/work/php-build
TEST_PHP_EXECUTABLE=./sapi/cli/php ./run-tests.php 
lcov -directory ./ -c -o tests.info
mkdir /work/php-coverage 
genhtml tests.info -o /work/php-coverage

I did run into this nice error

Processing ext/pdo/pdo_sqlstate.gcda
/work/php-src/ext/pdo/pdo_sqlstate.gcno:version '505', prefer 'A93'
geninfo: ERROR: GCOV failed for /work/php-src/ext/pdo/pdo_sqlstate.gcda!

because apparently I did use gcc 5 for compiling. Probably happened because of something installed via brew with higher priority in PATH. Adding an Alias did sadly not resolve it. So I throw it out of the Path for now.

Next issue I run into on the genhtml step:


Processing file /work/php-src/<stdout>
genhtml: ERROR: cannot read /work/php-src/<stdout>

in https://sourceforge.net/p/ltp/mailman/ltp-coverage/?limit=50&viewmonth=200908 its mentioned this is caused by parts where compiled code is not based on locally existing code or something.

It appears that the object file sdflex.o is compiled from code that is 
not present in any file but only in a file handle (<stdout>). 
Additionally, lcov incorrectly stops processing when it cannot read the 
.gcov file.

This was luckily possible to ignore with ” –ignore-errors=source”.
So finally, I ended up with the following script, and have put the result online at https://flyingmana.github.io/code-coverage-output/php/PHP_HEAD/index.html

<?php

$phpSrcPath = realpath(__DIR__ . '/../../php-src');
$versionIdentifier = 'PHP_HEAD';
$phpBuildPath = realpath(__DIR__ . '/../../php-build');
$reportPath = realpath(__DIR__ . "/../docs/php/$versionIdentifier");

chdir($phpSrcPath);
passthru("./configure  --enable-gcov --prefix=$phpBuildPath");
passthru("make -4j");
passthru('NO_INTERACTION=1 TEST_PHP_EXECUTABLE=./sapi/cli/php ./run-tests.php -j4'); // TEST_PHP_ARGS=-j4
passthru('lcov -directory ./ -c -o tests.info');
$cmd = "genhtml tests.info --ignore-errors=source  -o $reportPath";
echo $cmd.PHP_EOL;
passthru($cmd);

Leave a comment