gitlab-ci.yml cpp coverage report - c++

I am trying to implement CI using Gitlab for a c++ project. To start with, I added a simple c++ hello world program which compiled and ranfine in both my PC and in Gitlab CI.
When I try to generate coverage report for the same the commands, it works on PC but not in Gitlab CI.
This is my gitlab-ci.yml
# use the official gcc image, based on debian
# can use verions as well, like gcc:5.2
# see https://hub.docker.com/_/gcc/
image: gcc
build:
stage: build
# instead of calling g++ directly you can also use some build toolkit like make
# install the necessary build tools when needed
# before_script:
# - apt update && apt -y install make autoconf
script:
- g++ -Wall --coverage -fprofile-arcs -ftest-coverage helloworld.cpp -o mybinary
- ls
artifacts:
paths:
- mybinary
# depending on your build setup it's most likely a good idea to cache outputs to reduce the build time
# cache:
# paths:
# - "*.o"
# run tests using the binary built before
test:
stage: test
script:
- bash runmytests.sh
coverage:
stage: deploy
before_script:
- apt-get -qq update && apt-get -qq install -y gcovr ggcov lcov
script:
- ls
- g++ -Wall --coverage -fprofile-arcs -ftest-coverage helloworld.cpp -o mybinary
- ./mybinary
- ls
- gcov helloworld.cpp
- lcov --directory . --capture --output-file coverage.info
- lcov --remove coverage.info '/usr/*' --output-file coverage.info
- lcov --list coverage.info
- genhtml -o res coverage.info
This is the generated error output
$ g++ -Wall --coverage -fprofile-arcs -ftest-coverage helloworld.cpp -o mybinary
$ ./mybinary
Hello, World!$ ls
README.md
helloworld.cpp
helloworld.gcda
helloworld.gcno
mybinary
runmytests.sh
$ gcov helloworld.cpp
File 'helloworld.cpp'
Lines executed:100.00% of 3
Creating 'helloworld.cpp.gcov'
File '/usr/local/include/c++/8.1.0/iostream'
No executable lines
Removing 'iostream.gcov'
$ lcov --directory . --capture --output-file coverage.info
Capturing coverage data from .
Found gcov version: 8.1.0
Scanning . for .gcda files ...
geninfo: WARNING: /builds/ganeshredcobra/gshell/helloworld.gcno: Overlong record at end of file!
Found 1 data files in .
Processing helloworld.gcda
geninfo: WARNING: cannot find an entry for helloworld.cpp.gcov in .gcno file, skipping file!
Finished .info-file creation
$ lcov --list coverage.info
Reading tracefile coverage.info
lcov: ERROR: no valid records found in tracefile coverage.info
ERROR: Job failed: exit code 1
How can I fix this?

Solved the issue by changing image name from gcc to ubuntu 16.04, the working yml will look like this
# use the official gcc image, based on debian
# can use verions as well, like gcc:5.2
# see https://hub.docker.com/_/gcc/
image: ubuntu:16.04
build:
stage: build
# instead of calling g++ directly you can also use some build toolkit like make
# install the necessary build tools when needed
before_script:
- apt update && apt -y install make autoconf gcc g++
script:
- g++ -Wall --coverage -fprofile-arcs -ftest-coverage helloworld.cpp -o mybinary
- ls
artifacts:
paths:
- mybinary
# depending on your build setup it's most likely a good idea to cache outputs to reduce the build time
# cache:
# paths:
# - "*.o"
# run tests using the binary built before
test:
stage: test
script:
- bash runmytests.sh
coverage:
stage: deploy
before_script:
- apt-get -qq update && apt-get -qq install -y make autoconf gcc g++ gcovr ggcov lcov
script:
- ls
- g++ -Wall --coverage -fprofile-arcs -ftest-coverage helloworld.cpp -o mybinary
- ./mybinary
- ls
- gcov helloworld.cpp
- lcov --directory . --capture --output-file coverage.info
- lcov --list coverage.info

Related

llvm-link: error : expected top-level entity

I'm trying to compile the libpng 1.2.56 file and use a target.cc(same as fuzzer-test-suite to unite all these file in libpng1.2.56 into one LLVM IR bitcode file, named combined.bc.
Here is my compile process, refering build.sh:
[ ! -e libpng-1.2.56.tar.gz ] && wget https://downloads.sourceforge.net/project/libpng/libpng12/older-releases/1.2.56/libpng-1.2.56.tar.gz
[ ! -e libpng-1.2.56 ] && tar xf libpng-1.2.56.tar.gz
build_lib() {
rm -rf BUILD
cp -rf libpng-1.2.56 BUILD
(cd BUILD && ./configure --disable-shared && make -j $JOBS )
}
build_lib || exit 1
mkdir bitcode
clang++-10 -g -flto -std=c++11 ./target.cc -c -o bitcode/target.o -I .
cd bitcode
ar x ../.libs/libpng12.a
llvm-link-10 *.o -o combined.bc
And error is
enter image description here
The bitcode folder:
enter image description here
For modifications like the above, I succeeded on the project json, but not on libpng.
I am a beginner in LLVM and would like to get answers from you all. Thanks a lot!

How to compile custom cpp files on Google Colab

I'm trying to replicate the result of this github repo using Google Colab since I don't want to install all the requirements on my local machine and to take advantage of the GPU on Google Colab
However, one of the things I need to do (as indicated in the repo's README) is to first compile a cpp makefile. The instruction of the makefile is included below. Obvious I can't follow this instruction since I don't know Google Colab's directories of ncvv, cudalib and tensorflow library
cd latent_3d_points/external
with your editor modify the first three lines of the makefile to point to
your nvcc, cudalib and tensorflow library.
make
Is there a way for me to compile the files included in the makefile (because those functions are needed to run the model) either using the makefile directly or compile each cpp file individually? I included the content of the makefile below to avoid having you to click around in the repo looking for it
nvcc = /usr/local/cuda-8.0/bin/nvcc
cudalib = /usr/local/cuda-8.0/lib64
tensorflow = /orions4-zfs/projects/optas/Virt_Env/tf_1.3/lib/python2.7/site-packages/tensorflow/include
all: tf_approxmatch_so.so tf_approxmatch_g.cu.o tf_nndistance_so.so tf_nndistance_g.cu.o
tf_approxmatch_so.so: tf_approxmatch_g.cu.o tf_approxmatch.cpp
g++ -std=c++11 tf_approxmatch.cpp tf_approxmatch_g.cu.o -o tf_approxmatch_so.so -shared -fPIC -I $(tensorflow) -lcudart -L $(cudalib) -O2 -D_GLIBCXX_USE_CXX11_ABI=0
tf_approxmatch_g.cu.o: tf_approxmatch_g.cu
$(nvcc) -D_GLIBCXX_USE_CXX11_ABI=0 -std=c++11 -c -o tf_approxmatch_g.cu.o tf_approxmatch_g.cu -I $(tensorflow) -DGOOGLE_CUDA=1 -x cu -Xcompiler -fPIC -O2
tf_nndistance_so.so: tf_nndistance_g.cu.o tf_nndistance.cpp
g++ -std=c++11 tf_nndistance.cpp tf_nndistance_g.cu.o -o tf_nndistance_so.so -shared -fPIC -I $(tensorflow) -lcudart -L $(cudalib) -O2 -D_GLIBCXX_USE_CXX11_ABI=0
tf_nndistance_g.cu.o: tf_nndistance_g.cu
$(nvcc) -D_GLIBCXX_USE_CXX11_ABI=0 -std=c++11 -c -o tf_nndistance_g.cu.o tf_nndistance_g.cu -I $(tensorflow) -DGOOGLE_CUDA=1 -x cu -Xcompiler -fPIC -O2
clean:
rm tf_approxmatch_so.so
rm tf_nndistance_so.so
rm *.cu.o
You can use the bash like on your pc by adding %%bash in the colab's cells.
Example:
Cell one: write cpp file
%%writefile welcome.cpp
#include <iostream>
int main()
{
std::cout << "Welcome To AI with Ashok's Blog\n";
return 0;
}
Cell two: compile and run
%%bash
g++ welcome.cpp -o welcome
./welcome
You can also open the cpp file in colab's build-in text editor in order to enjoy correct highlights. It opens when you open a text file from the "Files" tab on the left and can be save with "ctr+s" shortcut.
You can install the required version of Cuda in google colab. For eg.
For Cuda 9.2 you can try
!apt-get --purge remove cuda nvidia* libnvidia-*
!dpkg -l | grep cuda- | awk '{print $2}' | xargs -n1 dpkg --purge
!apt-get remove cuda-*
!apt autoremove
!apt-get update
!wget https://developer.nvidia.com/compute/cuda/9.2/Prod/local_installers/cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64 -O cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64.deb
!dpkg -i cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64.deb
!apt-key add /var/cuda-repo-9-2-local/7fa2af80.pub
!apt-get update
!apt-get install cuda-9.2
Similarly, you can find a way to install Cuda 8.2.
For gcc
!apt-get install -qq gcc-5 g++-5 -y
!ln -s /usr/bin/gcc-5
!ln -s /usr/bin/g++-5
!sudo apt-get update
!sudo apt-get upgrade
Then you can compile it or make it by running make, if your installation has a custom make file.
!make

travis ci make -std=c++14 not recognized

I have the error g++: error: unrecognized command line option ‘-std=c++14’ when building my project with travis ci using premake (https://travis-ci.org/S6066/Teal/builds/171980872)
My premake script: https://github.com/S6066/Teal/blob/master/build/premake5.lua
My travis ci file: https://github.com/S6066/Teal/blob/master/.travis.yml
Thanks for help.
Edit: g++ --version gives me 4.8.5, so the real problem is Travis won't compile with g++ 6
I did these scripts for a github project and c++14 works:
This is an example of a .travis.yml:
sudo: required
dist: trusty
language: cpp
compiler: g++
install: export CXX="g++-5"
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-5
notifications:
email:
on_success: never
on_failure: always
before_install: sudo apt-get update -qq
script: make --directory "Var & ReadOnly C++"
And this is the associated makefile:
all:
$(CXX) -std=c++14 main.cpp -I . -Os -Wall -Wextra -o Example
clean:
rm Example*

Wrong coverage data for unit tests of a library

I want to test a complex shared library (cxsc) with the help of cmake. After a lot of trial and error i managed to create a html coverage report but the lines I test, with the boost testing framework, are still red.
This is the script I use to create the coverage report:
rm -fr build_coverage
mkdir build_coverage
cd build_coverage
cmake \
-DCMAKE_BUILD_TYPE=Coverage \
..
make
cd ../tests
make clean
make
cd ../build_coverage
make cxsc_coverage
The cmake part where the coverage report gets created:
# Cleanup lcov
COMMAND ${LCOV_PATH} --zerocounters --directory ${CMAKE_CURRENT_SOURCE_DIR}/build_coverage/CMakeFiles/cxsc.dir/src
COMMAND ${LCOV_PATH} --capture --initial --no-external --directory ${CMAKE_CURRENT_SOURCE_DIR}/build_coverage/CMakeFiles/cxsc.dir/src --output-file ${_outputname}.before
# Run tests
COMMAND LD_LIBRARY_PATH=${CMAKE_CURRENT_SOURCE_DIR}/build_coverage ${_testrunner} ${ARGV3}
# Capturing lcov counters and generating report
COMMAND ${LCOV_PATH} --capture --no-checksum --no-external --directory ${CMAKE_CURRENT_SOURCE_DIR}/build_coverage/CMakeFiles/cxsc.dir/src --output-file ${_outputname}.after
COMMAND ${LCOV_PATH} --add-tracefile ${_outputname}.before --add-tracefile ${_outputname}.after --output-file ${_outputname}.info
COMMAND ${GENHTML_PATH} -o ${_outputname} ${_outputname}.info
Makefile of the test binary (I think this is where the error is):
g++ \
-o test_runner \
main.cpp \
test_interval.cpp \
-I./../src \
-I./../src/rts \
-I./../src/asm \
-I./../src/fi_lib \
-I./../build_coverage \
-L./../build_coverage \
-lcxsc \
-Wall -Winline \
-lboost_unit_test_framework
This is what happens:
cmake -DCMAKE_BUILD_TYPE=Coverage .. build_coverage directory is created with a target.dir directory and *.gcno files in it
cd tests && make Test executable is created and linked against the shared library in the build_coverage directory (Maybe here is my mistake)
make coverage Coverage data is collected and tests are executed
Edit:
To clarify my problem, there are some lines covered but only global consts or one helper function which is used pretty often. But not the functions/methods I call in my tests.
Files in build_coverage/CMakeFiles/cxsc.dir/src/:
After cmake -DC...: A few *.cmake and *.make files
After make in build_coverage dir: *.gcno files
After lcov -c -i ...: Still *.gcno files
After running tests: *.gcda and *.gcno files
After lcov -c ...: A lot of movement in the directory but still the same filenames
I don't see any gcov flags in your makefile. Try this one for test_runner :
g++ \
-Wall -Winline -fprofile-arcs -ftest-coverage \
main.cpp \
test_interval.cpp \
-I./../src \
-I./../src/rts \
-I./../src/asm \
-I./../src/fi_lib \
-I./../build_coverage \
-L./../build_coverage \
-lcxsc \
-lboost_unit_test_framework \
-lgcov \
-o test_runner
Your shared library need those flags too :
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lgcov")
See this post with a similar problem.
NB : "--coverage" can substitute "-fprofile-arcs -ftest-coverage" and "-lgcov"

lcov, reporting 0% coverage for not tested headers

i will ask using an example. Let's suppose the following files:
root
- yes.h
- not.h
- test.cpp
"test.cpp" includes "yes.h"
when I run lcov shows the percentage covered in yes.h and in test.cpp, but (and here's my question) I want a zero coverage entry for "not.h", this way I can really have a valuable coverage metric. There's any way to achieve this?
Here's my lcov usage:
g++ --coverage test.cpp
lcov --directory . --zerocounters
lcov -c -i -d . -o app_base.info
./a.out
lcov -c -d . -o app_test.info
lcov -a app_base.info -a app_test.info -o app_total.info
geninfo app_total.info
thanks.
You might want to take a look at the --remove and --extract options for lcov (see the man page).
In your case, you might want to change
lcov -a app_base.info -a app_test.info -o app_total.info
geninfo app_total.info
with
lcov -a app_base.info -a app_test.info -o app_tmp.info
lcov --remove app_tmp.info */not.h --output app_total.info
geninfo app_total.info