#------------------------------------------
# Manage CamiTK Test coverage
#-------------------------------------------

# When using gcc, enable test coverage
# using https://github.com/bilke/cmake-modules/blob/master/CodeCoverage.cmake
# requires lcov to be installed on the machine
if(CMAKE_COMPILER_IS_GNUCXX)
    
    option(CAMITK_TEST_COVERAGE "Check this option to generate the 'camitk-ce-test-coverage' target that will create a coverage analysis report in ${PROJECT_BINARY_DIR}/camitk-ce-test-coverage" OFF)    
    
    if (CAMITK_TEST_COVERAGE)
        # CodeCoverage is an additional CMake modules published on github by Lars Bilke.
        # it declares append_coverage_compiler_flags and setup_target_for_coverage macros
        include(CodeCoverage)
        
        # First append gxx flags, this has to be done before any C++ project configuration takes place
        append_coverage_compiler_flags()
        
        # exclude files from coverage (see also ci-coverage.cmake)
        # - CTEST_CUSTOM_COVERAGE_EXCLUDE is used during CI (see sdk/cmake/ctest/ci-coverage.cmake to edit it)
        # - COVERAGE_EXCLUDES is used during build of target camitk-ce-test-coverage (see below)
        # Note:
        # - during local build (see below) use PROJECT_BINARY_DIR and PROJECT_SOURCE_DIR to to specify absolute path
        # - during ctest use CTEST_BINARY_DIRECTORY and CTEST_SOURCE_DIRECTORY to specify absolute path
        list(APPEND COVERAGE_EXCLUDES 
            # Exclude project binary and build dir
            "${PROJECT_BINARY_DIR}/*"

            # Exclude files from the testing directories
            "${PROJECT_SOURCE_DIR}/*/testing/*"

            # Exclude system files
            "/usr/*"
        )

        # test executable requires bash to pipe to exit 0 unconditionally
        find_program(BASH_PROGRAM bash)
        if(NOT BASH_PROGRAM)
            message(FATAL_ERROR "bash not found! Aborting...")
        endif()

        # parallelize test scripts if possible
        include(ProcessorCount)
        ProcessorCount(NUMBER_OF_PROC)
        message(STATUS "Detected ${NUMBER_OF_PROC} processors")
        # remove some to avoid processor overload
        MATH(EXPR NUMBER_OF_PROC "${NUMBER_OF_PROC}-1")
        if(NUMBER_OF_PROC GREATER 0)
            set(CTEST_PARALLEL_FLAG "--parallel ${NUMBER_OF_PROC}")
        endif()

        # Defines a specific target to run test coverage and reporting.
        # This will create a specific camitk-ce-test-coverage target 
        # that, when run, will launch all the test known by ctest in parallel 
        # and generate a report in ${PROJECT_BINARY_DIR}/camitk-ce-test-coverage
        # 
        # Note: you can run a quick test by bypassing some tests. 
        #       e.g., to run only one test every 50 tests "-I 1,,50" (ctest documenation: -I Start,End,Stride,test#,test#
        # Note: add "-V" or "-VV" for verbose, add "-R pattern" to run only specific test
        setup_target_for_coverage(NAME camitk-ce-test-coverage 
                                  EXECUTABLE bash -c "ctest ${CTEST_PARALLEL_FLAG} || exit 0") 
    endif()
    
endif()
