Merge pull request #45 from realm/tg/coverage

Add cmake target to generate a code coverage report
This commit is contained in:
Thomas Goyne 2016-05-06 16:15:48 -07:00
commit e4ace9ca20
5 changed files with 55 additions and 0 deletions

49
CMake/CodeCoverage.cmake Normal file
View File

@ -0,0 +1,49 @@
find_program(LCOV_PATH lcov)
find_program(GENHTML_PATH genhtml)
find_program(GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/tests)
set(CMAKE_CXX_FLAGS_COVERAGE "-g -O0 -fprofile-arcs -ftest-coverage"
CACHE STRING "Flags used by the C++ compiler during coverage builds.")
mark_as_advanced(CMAKE_CXX_FLAGS_COVERAGE)
if(CMAKE_BUILD_TYPE STREQUAL "Coverage")
if(NOT (LCOV_PATH AND GENHTML_PATH AND GCOVR_PATH))
message(FATAL_ERROR "Generating a coverage report requires lcov and gcovr")
endif()
function(create_coverage_target targetname testrunner)
add_custom_target(${targetname}
# Clear previous coverage information
COMMAND ${LCOV_PATH} --directory . --zerocounters
# Run the tests
COMMAND ${testrunner}
# Generate new coverage report
COMMAND ${LCOV_PATH} --directory . --capture --output-file coverage.info
COMMAND ${LCOV_PATH} --extract coverage.info '${CMAKE_SOURCE_DIR}/src/*' --output-file coverage.info.cleaned
COMMAND ${GENHTML_PATH} -o coverage coverage.info.cleaned
COMMAND ${CMAKE_COMMAND} -E remove coverage.info coverage.info.cleaned
COMMAND echo Open coverage/index.html in your browser to view the coverage report.
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_custom_target(${targetname}-cobertura
COMMAND ${testrunner}
COMMAND ${GCOVR_PATH} -x -r ${CMAKE_SOURCE_DIR}/src -o coverage.xml
COMMAND echo Code coverage report written to coverage.xml
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
endfunction()
else()
function(create_coverage_target targetname testrunner)
add_custom_target(${targetname}
COMMAND echo "Configure with -DCMAKE_BUILD_TYPE=Coverage to generate coverage reports")
add_custom_target(${targetname}-cobertura
COMMAND echo "Configure with -DCMAKE_BUILD_TYPE=Coverage to generate coverage reports")
endfunction()
endif()

View File

@ -3,6 +3,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED on)
set(CMAKE_CXX_EXTENSIONS off) set(CMAKE_CXX_EXTENSIONS off)
add_compile_options(-Wall -DREALM_HAVE_CONFIG) add_compile_options(-Wall -DREALM_HAVE_CONFIG)
add_compile_options("$<$<CONFIG:DEBUG>:-DREALM_DEBUG>") add_compile_options("$<$<CONFIG:DEBUG>:-DREALM_DEBUG>")
add_compile_options("$<$<CONFIG:COVERAGE>:-DREALM_DEBUG>")
if(${CMAKE_GENERATOR} STREQUAL "Ninja") if(${CMAKE_GENERATOR} STREQUAL "Ninja")
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")

View File

@ -60,6 +60,7 @@ function(download_realm_core core_version)
add_library(realm STATIC IMPORTED) add_library(realm STATIC IMPORTED)
add_dependencies(realm realm-core) add_dependencies(realm realm-core)
set_property(TARGET realm PROPERTY IMPORTED_LOCATION_DEBUG ${core_library_debug}) set_property(TARGET realm PROPERTY IMPORTED_LOCATION_DEBUG ${core_library_debug})
set_property(TARGET realm PROPERTY IMPORTED_LOCATION_COVERAGE ${core_library_debug})
set_property(TARGET realm PROPERTY IMPORTED_LOCATION_RELEASE ${core_library_release}) set_property(TARGET realm PROPERTY IMPORTED_LOCATION_RELEASE ${core_library_release})
set_property(TARGET realm PROPERTY IMPORTED_LOCATION ${core_library_release}) set_property(TARGET realm PROPERTY IMPORTED_LOCATION ${core_library_release})
@ -81,6 +82,7 @@ macro(define_built_realm_core_target core_directory)
add_dependencies(realm realm-core) add_dependencies(realm realm-core)
set_property(TARGET realm PROPERTY IMPORTED_LOCATION_DEBUG ${core_library_debug}) set_property(TARGET realm PROPERTY IMPORTED_LOCATION_DEBUG ${core_library_debug})
set_property(TARGET realm PROPERTY IMPORTED_LOCATION_COVERAGE ${core_library_debug})
set_property(TARGET realm PROPERTY IMPORTED_LOCATION_RELEASE ${core_library_release}) set_property(TARGET realm PROPERTY IMPORTED_LOCATION_RELEASE ${core_library_release})
set_property(TARGET realm PROPERTY IMPORTED_LOCATION ${core_library_release}) set_property(TARGET realm PROPERTY IMPORTED_LOCATION ${core_library_release})

View File

@ -5,6 +5,7 @@ project(realm-object-store)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake") list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake")
include(CodeCoverage)
include(CompilerFlags) include(CompilerFlags)
include(Sanitizers) include(Sanitizers)

View File

@ -15,4 +15,6 @@ set(SOURCES
add_executable(tests ${SOURCES} ${HEADERS}) add_executable(tests ${SOURCES} ${HEADERS})
target_link_libraries(tests realm-object-store) target_link_libraries(tests realm-object-store)
create_coverage_target(generate-coverage tests)
add_custom_target(run-tests USES_TERMINAL DEPENDS tests COMMAND ./tests) add_custom_target(run-tests USES_TERMINAL DEPENDS tests COMMAND ./tests)