[CMake] Nice handling of code coverage option?

Brad Hards bradh at frogmouth.net
Thu Mar 25 06:12:44 EDT 2010


On Wednesday 24 March 2010 10:58:48 am Brad Hards wrote:
> - it mostly done in a module that I can just "INCLUDE( MagicCoverage )"
> - is controlled by a cmake OPTION()
> - automatically gets the right coverage tool for the CMAKE_CXX_COMPILER (or
> warns about not being able to find an appropriate coverage tool)
> - adds the right compiler / linker flags
> - has a makefile (or equivalent) target to run the coverage tool.
I had a first cut at this, and it looks like it works. I wouldn't really say its "nice"
at this stage, but it probably does what I need for this iteration.

Any comments or suggestions on this?

Brad 

# - Enable Code Coverage
#
#
# USAGE:
# 1. Copy this file into your cmake modules path
# 2. Add the following line to your CMakeLists.txt:
#      include(UseCodeCoverage)
# 3. Select the ENABLE_CODECOVERAGE option when you want to build with code coverage enabled.
# 
# Variables you may define are:
#  CODECOV_OUTPUTFILE - the name of the temporary output file used. Defaults to "cmake_coverage.output"
#  CODECOV_HTMLOUTPUTDIR - the name of the directory where HTML results are placed. Defaults to "coverage_results"
#

#
#  Copyright (C) 2010 Brad Hards <bradh at frogmouth.net>
#
#  Redistribution and use is allowed according to the terms of the New
#  BSD license.
#  For details see the accompanying COPYING-CMAKE-SCRIPTS file.
#

OPTION( ENABLE_CODECOVERAGE "Enable code coverage testing support" )

if ( ENABLE_CODECOVERAGE )

    if ( NOT CMAKE_BUILD_TYPE STREQUAL "Debug" )
        message( WARNING "Code coverage results with an optimised (non-Debug) build may be misleading" )
    endif ( NOT CMAKE_BUILD_TYPE STREQUAL "Debug" )

    if ( NOT DEFINED CODECOV_OUTPUTFILE )
        set( CODECOV_OUTPUTFILE cmake_coverage.output )
    endif ( NOT DEFINED CODECOV_OUTPUTFILE )

    if ( NOT DEFINED CODECOV_HTMLOUTPUTDIR )
        set( CODECOV_HTMLOUTPUTDIR coverage_results )
    endif ( NOT DEFINED CODECOV_HTMLOUTPUTDIR )

    if ( CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_GNUCXX )
        find_program( CODECOV_GCOV gcov )
        find_program( CODECOV_LCOV lcov )
        find_program( CODECOV_GENHTML genhtml )
        add_definitions( -fprofile-arcs -ftest-coverage )
        link_libraries( gcov )
        set( CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} --coverage )
        add_custom_target( coverage_init ALL ${CODECOV_LCOV} --base-directory .  --directory ${CMAKE_BINARY_DIR} --output-file ${CODECOV_OUTPUTFILE} --capture --initial )
        add_custom_target( coverage ${CODECOV_LCOV} --base-directory .  --directory ${CMAKE_BINARY_DIR} --output-file ${CODECOV_OUTPUTFILE} --capture COMMAND genhtml -o ${CODECOV_HTMLOUTPUTDIR} ${CODECOV_OUTPUTFILE} )
    endif ( CMAKE_COMPILER_IS_GNUCXX )

endif (ENABLE_CODECOVERAGE )


More information about the CMake mailing list