Search code examples
phpcode-coveragephp-extensionlibtoolgcov

Generated libtool is removing *.gcno on linking when compiling PHP extension


I want to have code coverage for my PHP extension. I am using the following setup:

PHP 7.2.2 (cli) (built: Mar 24 2018 20:43:46) ( NTS DEBUG )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609

I can build the extension without any issues and run all tests. I am configuring the extension as follows:

CXX=gcc CFLAGS="--coverage -fprofile-arcs -ftest-coverage $CFLAGS" LDFLAGS="--coverage" ./configure

The compilation step of make all

/bin/bash /home/alex/Projects/php-7.2.2/ext/zookeeper/libtool --mode=compile cc -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -I. -I/home/alex/Projects/php-7.2.2/ext/zookeeper -DPHP_ATOM_INC -I/home/alex/Projects/php-7.2.2/ext/zookeeper/include -I/home/alex/Projects/php-7.2.2/ext/zookeeper/main -I/home/alex/Projects/php-7.2.2/ext/zookeeper -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib  -DHAVE_CONFIG_H  --coverage -fprofile-arcs -ftest-coverage -O0   -c /home/alex/Projects/php-7.2.2/ext/zookeeper/zookeeper.c -o zookeeper.lo

generates the following files:

.libs/zookeeper.gcno
.libs/zookeeper.o

Now the linking part:

/bin/bash /home/alex/Projects/php-7.2.2/ext/zookeeper/libtool --mode=link cc -DPHP_ATOM_INC -I/home/alex/Projects/php-7.2.2/ext/zookeeper/include -I/home/alex/Projects/php-7.2.2/ext/zookeeper/main -I/home/alex/Projects/php-7.2.2/ext/zookeeper -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib  -DHAVE_CONFIG_H  --coverage -fprofile-arcs -ftest-coverage -O0  --coverage -o zookeeper.la -export-dynamic -avoid-version -prefer-pic -module -rpath /home/alex/Projects/php-7.2.2/ext/zookeeper/modules  zookeeper.lo -Wl,-rpath,/home/alex/Projects/php-7.2.2/ext/zookeeper/target/release -L/home/alex/Projects/php-7.2.2/ext/zookeeper/target/release -lzookeeper_bridge

logs the following step:

rm -fr  .libs/zookeeper.gcno

I managed to narrow it down to the following line of ltmain.sh script that is bundled with PHP: https://github.com/php/php-src/blob/d1186b8/ltmain.sh#L3457

My libtool generated during ./configure also has this line:

*.$objext)

Now if I manually change it to:

*.$objext | *.gcno)

the linking process no longer removes the .gcno file and I can get correct coverage report.

I assume there is a way to force the linking process to not to remove the .gcno files and therefore allow me to generate the proper coverage report. Am I missing something obvious here?


Solution

  • I assume there is a way to force the linking process to not to remove the .gcno files and therefore allow me to generate the proper coverage report.

    In this case, probably not. The ltmain.sh you referenced above says it's from libtool 1.5.26. However, the version of libtool that claims to no longer remove .gcno profiler information is libtool 2.2.6. The fix is probably in later libtool versions as well.

    So to fix it, you'll have to get the php-src maintainers to upgrade libtool. Or patch libtool as part of your build process as you've done by hand.