0

I am trying to make a very simple regression model that (among other things), builds and compiles a GCC target for coverage, executes, and then publishes a standard Cobertura coverage report (all within Jenkins). The Jenkins part is somewhat irrelevant here, I'm only concerned with CMake syntax at the moment. This is my CMake file so far:

cmake_minimum_required( VERSION 3.15 )

# Project's name
project( my_project )

# Set the output folder where your program will be created
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/test/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})

set( CMAKE_VERBOSE_MAKEFILE on )

# Generate coverage on GCC.
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
    set(LDFLAGS "${LDFLAGS} -lgcov -fprofile-arcs")
endif()

# Includes and Sources
include_directories(${PROJECT_SOURCE_DIR}/inc)
file(GLOB APP_SRC "./src/*.c")
file(GLOB TEST_DEPENDS_SRC "./test/src/*.c")

# Add executable to list.
add_executable( ${PROJECT_NAME}_Test ${APP_SRC} ${TEST_DEPENDS_SRC} )

This generates my *.gcno and *.gcda files in the directory /test/build/gcc/CMakeFiles/my_project.dir/*, but for ease of post-processing, I think I want these files placed alongside their source. Is that possible? If so, how? Is that best practice? I'm still pretty green when it comes to CMake.

Carlos Cavero
  • 3,011
  • 5
  • 21
  • 41
SeaNick
  • 501
  • 1
  • 4
  • 14
  • I'm not sure about best practices here, but to copy the files, you could use CMake's `add_custom_command()` with `POST_BUILD` to put the files in the desired location after the build completes. It would be similar to [this](https://stackoverflow.com/a/34800230/3987854) answer. – Kevin Sep 27 '19 at 17:06
  • "Is that best practice?" - I don't find generating files in the project's **source tree** to be a good practice. However, I know that there are cases when it is inevitable. As for coverage files, you can easily adjust `lcov` tool to work with coverage files in the build tree, so there is no needs to pollute the source tree. – Tsyvarev Sep 27 '19 at 17:30
  • 1
    So I haven't gotten to the coverage report step yet, but the reason I hesitate to modify lcov/gcovr that is because in my experience, the Jenkins visualization of the coverage report cannot locate the associated c/c++/h file when the coverage files are in a different directory than their associated source. – SeaNick Sep 27 '19 at 17:34
  • OK, it seems to be `there are cases when it is inevitable` situation described in my previous comment. BTW, it means that "Jenkins" part of your question is actually **important**, opposite to what you state in the question post. And the question could be "How to collect coverage report in CMake project with Jenkins". So the answers could be either about creating coverage files in the source tree, ... or configuring Jenkins to work with files in the build tree. – Tsyvarev Sep 27 '19 at 17:54
  • I didn't find Jenkins to be particularly relevant to the actual question, since the root question I'm asking is how to explicitly redirect output files of coverage executables, with less emphasis on where I was directing them to. I was trying to avoid a question with too large of a scope. – SeaNick Sep 27 '19 at 18:46

0 Answers0