Code coverage ( *.cpp.gcda: CANNOT OPEN) is not working - c++11

I want to code coverage with gcov & lcov. But I got a few error.I think My CMakeLists is not providing ".gcda" files or is not finding ".gcda" files.
I tried a lot of solutions. Such as;
- I looked at similar projects and rearranged:
For example: https://jhbell.com/using-cmake-and-gcov
- I changed object output direcory:
set(OBJECT_OUTPUT_DIR ${CMAKE_BINARY_DIR}/test/CMakeFiles/*****rpmsgtobetested.dir)
But I didn't solve this problem.
CMAKE_MINIMUM_REQUIRED(VERSION 3.8)
option(COMPILE_TESTS "Compile the tests" OFF)
if(COMPILE_TESTS)
enable_language(C)
enable_language(CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
if(DEFINED ENV{CPPUTEST_HOME})
message(STATUS "Using CppUTest home: $ENV{CPPUTEST_HOME}")
set(CPPUTEST_INCLUDE_DIRS $ENV{CPPUTEST_HOME}/include)
set(CPPUTEST_LIBRARIES $ENV{CPPUTEST_HOME}/lib)
set(CPPUTEST_LDFLAGS CppUTest CppUTestExt)
else()
find_package(PkgConfig REQUIRED)
pkg_search_module(CPPUTEST REQUIRED cpputest>=3.8)
message(STATUS "Found CppUTest version ${CPPUTEST_VERSION}")
endif()
pkg_check_modules(******rpmsgtest_depends json-c)
add_definitions(-DCPPUTEST_USE_MEM_LEAK_DETECTION=0)
add_definitions("-std=c++11")
# Coverage bilgisi elde etmek için CXX flagları
#
set(CMAKE_CXX_FLAGS "-g -O0 -Wall -fprofile-arcs -ftest-coverage")
set(CMAKE_CXX_OUTPUT_EXTENSION_REPLACE OFF)
set(OBJECT_OUTPUT_DIR ${CMAKE_BINARY_DIR}/test/CMakeFiles/******rpmsgtobetested.dir)
# (1) set sources to be tested
set(TARGET_LIBTOBETESTED ******rpmsgtobetested)
set(libtobetested_sources
../src/rpmsg_device.cpp
../src/rpmsg_sm_messages.cpp
../src/thread_interface.cpp)
add_library(${TARGET_LIBTOBETESTED} ${libtobetested_sources})
# (2) Our unit tests sources
set(TEST_APP_NAME ${TARGET_LIBTOBETESTED}_tests)
set(TEST_SOURCES
test_rpmsg_device.cpp
main.cpp
fake_sdevent.cpp
fake_syscalls.cpp
)
# (3) Take care of include directories
include_directories(${CPPUTEST_INCLUDE_DIRS}
../include
/usr/include/glib-2.0
/usr/include/gio-unix-2.0
${******rpmsgtest_depends_INCLUDE_DIRS})
link_directories(${CPPUTEST_LIBRARIES})
link_libraries(${TARGET_LIBTOBETESTED})
# (4) Build the unit tests objects and link then with the app library
add_executable(${TEST_APP_NAME} ${TEST_SOURCES})
target_compile_options(${TEST_APP_NAME} PUBLIC ${******rpmsgtest_depends_CFLAGS})
target_link_libraries(${TEST_APP_NAME} ${link_libraries}
${CPPUTEST_LDFLAGS}
${******rpmsgtest_depends_LIBRARIES}
)
# (5) Run the test once the build is done
add_custom_command(TARGET ${TEST_APP_NAME} COMMAND ./${TEST_APP_NAME} POST_BUILD)
############################################################################
################################### LCOV ###################################
############################################################################
add_custom_target(lcov
COMMAND mkdir -p coverage
COMMAND mkdir -p coverage/html
COMMAND ${CMAKE_MAKE_PROGRAM}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_custom_command(TARGET lcov
COMMAND echo "################# LCOV #################"
COMMAND lcov -c -d ${COVERAGE_DATA_DIR} -o ${CMAKE_BINARY_DIR}/coverage.info
COMMAND lcov --remove ${CMAKE_BINARY_DIR}/coverage.info '**/include/**' -o ${CMAKE_BINARY_DIR}/coverage_filtered.info
COMMAND genhtml -o ${CMAKE_BINARY_DIR}/coverage/html ${CMAKE_BINARY_DIR}/coverage_filtered.info --ignore-errors source
COMMAND echo "-- Coverage report has been output to ${CMAKE_BINARY_DIR}/coverage/html"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_dependencies(lcov ${TARGET_LIBTOBETESTED})
############################################################################
################################### GCOV ###################################
############################################################################
add_custom_target(gcov
COMMAND mkdir -p coverage
COMMAND ${CMAKE_MAKE_PROGRAM}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_custom_command(TARGET gcov
COMMAND echo "################# GCOV #################"
COMMAND gcov -b ${libtobetested_sources} -o ${OBJECT_OUTPUT_DIR} | grep -A 5 ".cpp$" > CoverageSummary.tmp
COMMAND cat CoverageSummary.tmp
COMMAND echo "-- Coverage files have been output to ${CMAKE_BINARY_DIR}/coverage"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/coverage
)
add_dependencies(gcov ${TARGET_LIBTOBETESTED})
else()
message(STATUS "Cross compile environment set")
endif()
Like below the last of my code output.
TEST(RpmsgDevice_test, create_new_object_with_null_param) - 0 ms
TEST(RpmsgDevice_test, create_new_object_cannot_open_device) - 0 ms
TEST(RpmsgDevice_test, create_new_object_with_thread) - 0 ms
OK (10 tests, 10 ran, 20 checks, 0 ignored, 0 filtered out, 0 ms)
profiling:/home/msergenergen/Desktop/*****/IP/*****-lib-rpmsg/build-test/test/CMakeFiles/*****rpmsgtobetested.dir//src/rpmsg_sm_messages.cpp.gcda:Cannot open
profiling:/home/msergenergen/Desktop/*****/IP/*****-lib-rpmsg/build-test/test/CMakeFiles/*****rpmsgtobetested.dir//src/rpmsg_device.cpp.gcda:Cannot open
profiling:/home/msergenergen/Desktop/*****/IP/*****-lib-rpmsg/build-test/test/CMakeFiles/*****rpmsgtobetested_tests.dir/fake_syscalls.cpp.gcda:Cannot open
profiling:/home/msergenergen/Desktop/*****/IP/*****-lib-rpmsg/build-test/test/CMakeFiles/*****rpmsgtobetested_tests.dir/fake_sdevent.cpp.gcda:Cannot open
profiling:/home/msergenergen/Desktop/*****/IP/******-lib-rpmsg/build-test/test/CMakeFiles/*****rpmsgtobetested_tests.dir/main.cpp.gcda:Cannot open
profiling:/home/msergenergen/Desktop/*****/IP/*****-lib-rpmsg/build-test/test/CMakeFiles/*****rpmsgtobetested_tests.dir/test_rpmsg_device.cpp.gcda:Cannot open
[100%] Built target *****rpmsgtobetested_tests

Related

cannot create hex elf files with cmakelists

I am trying to use cmakelists to create a hex and elf file for my blinkled project, using IAR files.
The compilation is without errors with cmake but I dont see the files in the build folder.
my goal is to create a hex and elf files for flashing pursoses later on.
Can someone please tell me what I should modify to solve this problem?
thanks a lot
cmakelists:
cmake_minimum_required(VERSION 3.7.2)
######################################################################
#
# Project
#
######################################################################
project(blink_cortex_m0plus)
enable_language(ASM)
######################################################################
#
# CPP Definitions
#
######################################################################
set(PROJECT_FLAGS_ARCH
"-mcpu=cortex-m0plus \
-mthumb"
)
set(PROJECT_LINKER_FLAGS
"-Wl,-Map=${PROJECT_NAME}.map \
-L${PROJECT_SOURCE_DIR}/../config/ \
-Tlinker.ld"
)
set(PROJECT_C_FLAGS_WARNINGS "${COMMON_FLAGS_WARNINGS} ${C_FLAGS_WARNINGS_EXTRA}")
######################################################################
#
# Sources
#
######################################################################
set(PROJECT_INCLUDE_DIRS
${PROJECT_CONFIG_DIR}
${BLINK_DIR}/../
${CYPRESS_CORE_LIBRARY}
)
set(PROJECT_SRCS
${PROJECT_SOURCE_DIR}/main_cm0plus.c
${CYPRESS_PERIPHERAL_DRIVERS_LIBRARY}system_tviibe1m_cm0plus.c
${CYPRESS_PERIPHERAL_DRIVERS_LIBRARY}startup_cm0plus.S
)
######################################################################
#
# TARGET
#
######################################################################
set (CMAKE_C_FLAGS "${C_FLAGS} ${C_FLAGS_WARNINGS} ${FLAGS_OPTIMIZATION} ${PROJECT_FLAGS_ARCH}")
set (CMAKE_ASM_FLAGS "${C_FLAGS} ${C_FLAGS_WARNINGS} ${FLAGS_OPTIMIZATION} ${PROJECT_FLAGS_ARCH}")
#set (CMAKE_CXX_FLAGS "${CXX_FLAGS} ${CXX_FLAGS_WARNINGS} ${FLAGS_OPTIMIZATION} ${PROJECT_FLAGS_ARCH}")
set (CMAKE_EXE_LINKER_FLAGS "${LINKER_FLAGS} ${PROJECT_LINKER_FLAGS} ${FLAGS_OPTIMIZATION} ${PROJECT_FLAGS_ARCH}")
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
include_directories(
${PROJECT_INCLUDE_DIRS}
${CYPRESS_PERIPHERAL_DRIVERS_LIBRARY_INCLUDE_DIRS}
# )
add_executable(${PROJECT_NAME}.elf ${PROJECT_SRCS} ${CYPRESS_PERIPHERAL_DRIVERS_LIBRARY_SRCS})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map=${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.map")
set(HEX_FILE ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.bin)
add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
COMMAND ${OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
COMMAND ${OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
COMMENT "Building ${HEX_FILE} \nBuilding ${BIN_FILE}")
I convert to .bin and .hex it with the following command:
add_custom_command(TARGET ${APPLICATION}
POST_BUILD
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMAND ${OBJCOPY} ARGS -O binary ${APPLICATION} ${APPLICATION_BIN}
COMMAND ${OBJCOPY} ARGS -O ihex -R .eeprom -R .fuse -R .lock -R .signature ${APPLICATION} ${APPLICATION_HEX}
)
One notable difference is the ARGS as suggested here: https://stackoverflow.com/a/53573133

how do you escape an nmake command line inside cmake?

I am trying to build zlib on windows as a dependency in my cmake project.
I have come up with the following which works fine:
file(WRITE ${CMAKE_BINARY_DIR}/zlib.nmake
AS=ml64\n
LOC="-DASMV -DASMINF -DNDEBUG -I."\n
OBJA="inffasx64.obj gvmat64.obj inffas8664.obj"\n
-f win32/Makefile.msc\n
zlib.lib\n)
ExternalProject_Add(
zlib
GIT_REPOSITORY https://github.com/kyotov/zlib.git
GIT_TAG mt
PREFIX ${CMAKE_BINARY_DIR}
CONFIGURE_COMMAND ""
BUILD_COMMAND ${CMAKE_COMMAND} -E chdir <SOURCE_DIR> nmake #${CMAKE_BINARY_DIR}/zlib.nmake
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy <SOURCE_DIR>/zlib.h <INSTALL_DIR>/install/include/zlib.h
COMMAND ${CMAKE_COMMAND} -E copy <SOURCE_DIR>/zconf.h <INSTALL_DIR>/install/include/zconf.h
COMMAND ${CMAKE_COMMAND} -E copy <SOURCE_DIR>/zlib.lib <INSTALL_DIR>/install/lib/zlib.lib
COMMAND ${CMAKE_COMMAND} -E copy <SOURCE_DIR>/zlib.pdb <INSTALL_DIR>/install/lib/zlib.pdb
)
I would really prefer if I can inline the call to nmake and not have to resort to passing # parameter.
I spent about 15-30 minutes trying to escape the double quotes in various ways but could not get it done.
Google did not teach me anything either...
Anybody has any smart ideas?

How to extract all file in a .tar.gz to specific folder with cmake add_custom_target?

To decrease library size, used cmake -E tar "zcvf" "lib.tar.gz" out.lib in cmake to automatically create .tar.gz then lib.tar.gz is moved to folder f, ie. f/lib.tar.gz.
However, to use this library in another project, it's needed to extract to specific folder. The command without cmake is clear: tar -xzf f/lib.tar.gz -C target-folder.
The problem is that, in cmake, how to combine cmake -E tar "xzf" f/lib.tar.gz with -C option? Commands such as cmake -E tar "xzf" f/lib.tar.gz -C f/ will result error.
Ideal script would be sth. like
add_custom_target(PrepareLib
COMMAND ${CMAKE_COMMAND} -E tar "xzf" command....
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} ## note, this line must not be modified
)
Tools version: Visual Studio 2017, Windows 10 and CMake 3.17.1.

gcov cannot open graph file

I'm getting the following error when trying to run gcov as part of my Travis CI build:
$ gcov src/bgrep.c
bgrep.gcno:cannot open graph file
My .travis.yml:
language: c
compiler: gcc
sudo: false
before_script:
- uname -a
- printenv
- gcov --version
script:
- scons coverage=1
- ls -l src/
- test/run_test.py
- ls -l src/
- gcov src/bgrep.c
- ls -l src/
- ls -l
after_success:
- bash <(curl -s https://codecov.io/bash)
I can run the exact sequence of commands (scons, test/run_test.py, gcov src/bgrep.c) on my Fedora machine, and gcov produces grep.gcov as expected.
Travis is using gcov (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3. You can see an example build log here.
What am I (or Travis) doing wrong?
I had the same problem with gcov and travis. It seems that gcov indeed requires that the *.gcda and *.gcno files must be in the same directory as the *.c files. And additionally the -o option must be given which points then to the same directory...
So, I have the source files in folder src/ and the binaries in build/coverage/. First, I tried with 'gcov -o build/coverage/ src/*.c' which did not work. Second attempt was with copying the *.gc* files to src/ and executing 'gcov src/*.c' which also did not work. It finally worked with 'gcov -o src/ src/*.c'.

Use shell command as INSTALL_COMMAND to ExternalProject_Add

Is it possible to use any shell command for the INSTALL_COMMAND phase of cmake's ExternalProject_Add? e.g.
ExternalProject_Add(leveldb
GIT_REPOSITORY git#github.com:google/leveldb.git
GIT_TAG v1.18
CONFIGURE_COMMAND ./build_detect_platform build.settings .
BUILD_COMMAND make -j 8
BUILD_IN_SOURCE 1
INSTALL_COMMAND ""
)
# INSTALL_COMMAND "mkdir -p ${CMAKE_BINARY_DIR}/lib/ \
# && find . \( -name \"*${CMAKE_SHARED_LIBRARY_SUFFIX}\" -or -name \"*${CMAKE_STATIC_LIBRARY_SUFFIX}\" \
# -exec cp {} ${CMAKE_BINARY_DIR}/lib/\;\) \
# && cp -r ./include ${CMAKE_BINARY_DIR}")
I commented out the INSTALL_COMMAND I want to use, find and cp don't seem to be allowed, "No such file or directory, error 127" is the result of using this.
Direct Answer:
install(CODE "execute_process(...)")
The SCRIPT and CODE signature:
install([[SCRIPT <file>] [CODE <code>]] [...])
The SCRIPT form will invoke the given CMake script files during installation. If the script file name is a relative path it will be interpreted with respect to the current source directory. The CODE form will invoke the given CMake code during installation. Code is specified as a single argument inside a double-quoted string. For example, the code
install(CODE "MESSAGE(\"Sample install message.\")")
will print a message during installation.
Food for thought:
You might be in a hurry to get something done quickly. Do consider this if you have time or you can get back to it and fix it.
One of the main reasons why people love cmake is because of it's cross platform nature. Any project when properly coded with this aspect in mind will work with either on Linux or Windows or any other operating supported system. The various generators will work happily if the developer had this in mind. My suggestion is to convert the shell commands into cmake in a cross platform way, put them in a separate *.cmake file and execute them using cmake -E option.
Here is an extract from a working project that I had worked on in the past.
project_build_steps.cmake
message(VAR1=${VAR1}) # These variables can be passed from the invocation place
message(VAR2=${VAR2}) # You can use them in the build steps
if("${BUILD_STEP}" STREQUAL "patch")
message("BUILD_STEP: patch")
# Put your patch steps using cmake
endif()
if("${BUILD_STEP}" STREQUAL "configure")
message("BUILD_STEP: configure")
# Put your configure steps using cmake
endif()
if("${BUILD_STEP}" STREQUAL "build")
message("BUILD_STEP: build")
# Put your build steps using cmake
endif()
if("${BUILD_STEP}" STREQUAL "install")
message("BUILD_STEP: install")
# Put your install steps using cmake
endif()
CMakeLists.txt (Option 1)
set(CMAKE_COMMAND /usr/bin/cmake)
set(PROJECT_BUILD_STEPS_FILE project_build_steps.cmake)
ExternalProject_Add(
project_name
SOURCE_DIR /path/to/project/source
PATCH_COMMAND ${CMAKE_COMMAND} -DBUILD_STEP=patch -P ${PROJECT_BUILD_STEPS_FILE}
CONFIGURE_COMMAND ${CMAKE_COMMAND} -DBUILD_STEP=configure -P ${PROJECT_BUILD_STEPS_FILE}
BUILD_COMMAND ${CMAKE_COMMAND} -DBUILD_STEP=build -P ${PROJECT_BUILD_STEPS_FILE}
INSTALL_COMMAND ${CMAKE_COMMAND} -DBUILD_STEP=install -P ${PROJECT_BUILD_STEPS_FILE}
)
If you do not want to use ExternalProject_Add you can use something like following. This will also give you individual build targets like make project_patch, make project_configure, make project_build, make project_install.
CMakeLists.txt (Option 2)
set(CMAKE_COMMAND /usr/bin/cmake)
set(PROJECT_BUILD_STEPS_FILE project_build_steps.cmake)
set(STAMP_FILE_PROJECT_PATCH .project_patch_done)
add_custom_command(
OUTPUT ${STAMP_FILE_PROJECT_PATCH}
COMMAND ${CMAKE_COMMAND} -DVAR1=value1 -DSTEP=patch -P ${PROJECT_BUILD_STEPS_FILE}
COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_FILE_PROJECT_PATCH}
)
add_custom_target(project_patch DEPENDS ${STAMP_FILE_PROJECT_PATCH})
set(STAMP_FILE_PROJECT_CONFIGURE .project_configure_done)
add_custom_command(
OUTPUT ${STAMP_FILE_PROJECT_CONFIGURE}
COMMAND ${CMAKE_COMMAND} -DSTEP=configure -P ${PROJECT_BUILD_STEPS_FILE}
COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_FILE_PROJECT_CONFIGURE}
)
add_custom_target(project_configure DEPENDS project_patch ${STAMP_FILE_PROJECT_CONFIGURE})
set(STAMP_FILE_PROJECT_BUILD .project_build_done)
add_custom_command(
OUTPUT ${STAMP_FILE_PROJECT_BUILD}
COMMAND ${CMAKE_COMMAND} -DSTEP=build -P ${PROJECT_BUILD_STEPS_FILE}
COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_FILE_PROJECT_BUILD}
VERBATIM
)
add_custom_target(project_build DEPENDS project_configure ${STAMP_FILE_PROJECT_BUILD})
set(STAMP_FILE_PROJECT_INSTALL .project_install_done)
add_custom_command(
OUTPUT ${STAMP_FILE_PROJECT_INSTALL}
COMMAND ${CMAKE_COMMAND} -DSTEP=install -P ${PROJECT_INSTALL_STEPS_FILE}
COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_FILE_PROJECT_INSTALL}
VERBATIM
)
add_custom_target(project_install DEPENDS project_build ${STAMP_FILE_PROJECT_INSTALL})

Resources