Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using CMake-codecov with $<TARGET_OBJECTS:xxxxxxxx> #11

Open
Gjacquenot opened this issue Jul 20, 2017 · 4 comments
Open

Using CMake-codecov with $<TARGET_OBJECTS:xxxxxxxx> #11

Gjacquenot opened this issue Jul 20, 2017 · 4 comments

Comments

@Gjacquenot
Copy link

I am facing a Coverage disabled warning while trying to use CMake-codecov.

I am building executable with TARGET_OBJECTS option, that prevent CMake-codecov to discover the language used. The internal variable NUM_COMPILERS is equal to zero in Findcodecov.cmake. Would it be possible to investigate this development?

ADD_EXECUTABLE(my_test_prog $<TARGET_OBJECTS:my_test_prog_obj>)
add_coverage(my_test_prog)

Below is a CMakeLists.txt, that repoduces the problem (one needs to have codecov available to CMake)

CMAKE_MINIMUM_REQUIRED(VERSION 2.8.8)
PROJECT(hello_world)
IF(NOT DEFINED CMAKE_BUILD_TYPE)
    SET(CMAKE_BUILD_TYPE Coverage)
ENDIF()
STRING(TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_UPPER)
IF(${CMAKE_BUILD_TYPE_UPPER} STREQUAL "COVERAGE")
    MESSAGE(STATUS "Adding coverage")
    SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) # ;${CMAKE_MODULE_PATH}
    FIND_PACKAGE(codecov)
ENDIF()
FILE(WRITE "${CMAKE_CURRENT_BINARY_DIR}/my_test_prog.cpp"
"
#include <iostream>
using namespace std;
int main()
{
    cout << \"Hello World!\" << endl;
    return 0;
}
")

ADD_LIBRARY(my_test_prog_obj OBJECT ${CMAKE_CURRENT_BINARY_DIR}/my_test_prog.cpp)
ADD_EXECUTABLE(my_test_prog $<TARGET_OBJECTS:my_test_prog_obj>)
add_coverage(my_test_prog)

The log is here

-- Adding coverage
-- Try  code coverage flag = [-O0 -g -fprofile-arcs -ftest-coverage]
-- Try  code coverage flag = [-O0 -g --coverage]
CMake Warning (dev) at cmake/Findcodecov.cmake:174 (message):
  Coverage disabled for target my_test_prog because there is no sanitizer
  available for target sources.
Call Stack (most recent call first):
  cmake/Findcodecov.cmake:34 (add_coverage_target)
  CMakeLists.txt:25 (add_coverage)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring done
-- Generating done
-- Build files have been written to: buildCoverage

#make
Scanning dependencies of target my_test_prog_obj
[ 50%] Building CXX object CMakeFiles/my_test_prog_obj.dir/my_test_prog.cpp.obj
[ 50%] Built target my_test_prog_obj
Scanning dependencies of target my_test_prog
[100%] Linking CXX executable my_test_prog.exe
[100%] Built target my_test_prog

Guillaume

@alehaa
Copy link
Contributor

alehaa commented Jul 21, 2017

Unfortunately I didn't find a way to find the linker and compiler to be used this early at configuration time, so I needed to get these by the list of source files. The my_test_prog target doesn't have any source files in its sources list, so add_coverage() can't determine the language of this target and doesn't know which flags to set for this target.

However, add_coverage() should have print an error instead of searching flags for an empty compiler. I'll investigate why this didn't happen.

@alehaa
Copy link
Contributor

alehaa commented Jul 21, 2017

@Gjacquenot I can't reproduce your log.

First I needed to change the code to get it working with CMake 3.8.2:

--- ../foo.txt	2017-07-21 17:20:42.000000000 +0200
+++ ../CMakeLists.txt	2017-07-21 17:21:40.000000000 +0200
@@ -1,6 +1,9 @@
 CMAKE_MINIMUM_REQUIRED(VERSION 2.8.8)
 PROJECT(hello_world)
-IF(NOT DEFINED CMAKE_BUILD_TYPE)
+IF(POLICY CMP0051)
+    cmake_policy(SET CMP0051 NEW)
+ENDIF()
+IF(NOT CMAKE_BUILD_TYPE)
     SET(CMAKE_BUILD_TYPE Coverage)
 ENDIF()
 STRING(TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_UPPER)

Then I've run CMake:

$ cmake .. -DCMAKE_BUILD_TYPE=Coverage -DENABLE_COVERAGE=On
-- The C compiler identification is AppleClang 8.1.0.8020042
-- The CXX compiler identification is AppleClang 8.1.0.8020042
-- Check for working C compiler: /usr/local/opt/ccache/libexec/cc
-- Check for working C compiler: /usr/local/opt/ccache/libexec/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/local/opt/ccache/libexec/c++
-- Check for working CXX compiler: /usr/local/opt/ccache/libexec/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Adding coverage
-- Try Clang code coverage flag = [-O0 -g -fprofile-arcs -ftest-coverage]
-- Performing Test COVERAGE_FLAG_DETECTED
-- Performing Test COVERAGE_FLAG_DETECTED - Success
-- Found gcov evaluation for Clang: /usr/bin/gcov
-- Could NOT find lcov (missing:  LCOV_BIN GENINFO_BIN GENHTML_BIN) 
CMake Warning at /Users/alexander/develop/must/PnMPI/externals/CMake-codecov/cmake/Findcodecov.cmake:218 (message):
  Can't use code coverage for target my_test_prog, because it uses an unknown
  compiler.  Target will be compiled without code coverage.
Call Stack (most recent call first):
  /Users/alexander/develop/must/PnMPI/externals/CMake-codecov/cmake/Findcodecov.cmake:38 (add_coverage_target)
  CMakeLists.txt:28 (add_coverage)


-- Configuring done
-- Generating done
-- Build files have been written to: .

As you can see I get the right output for the search of the right compiler flag and add_coverage() will print an appropriate warning to the console.

@Gjacquenot
Copy link
Author

@alehaa, thanks for your quick answer.

Sorry, I did not use the latest version of your repository, this is why we obtain different log messages.

However, you face the same disabling warning on target.

Wouldn't it be possible to obtain compiler information from another method, or use a default value in case of failure?

@alehaa
Copy link
Contributor

alehaa commented Jul 21, 2017

My fault, sorry.

I think some time ago CMake itself didn't know how to handle targets with object libraries as only sources. There are two problems:

  1. You have to add add_coverage() for the object library, otherwise the objects wouldn't be compiled with coverage support. In the configure step CMake doesn't know which sources belong to a target and its related targets, so it can't figure out for which files to add the compile flags. This information becomes available in the generate step first.

  2. Someone has to tell the linker for the my_test_prog to link with coverage support. These flags have to match the compiler used for compilation. One cannot add GCC flags, if you've compiled with Clang, etc. - and if the objects have been compiled by different compilers, e.g. Fortran by Gcc and C by Clang, coverage must not be used et all. As we don't know the full list of sources of all object libraries (see 1.), we can't add flags and therefore can't use coverage.

However, I'm thinking about an optional parameter named LANGUAGES for add_coverage() to override the language detection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants