|
| 1 | +#lcov command options: |
| 2 | +# -a, --add-tracefile (takes glob) |
| 3 | +# -c, --capture; create a trace file from the .da files |
| 4 | +# -e, --extract; a reduced scope for analysis |
| 5 | +# -l, --list; the contents of a tracefile |
| 6 | +# -r, --remove; remove pattern-match from tracefile |
| 7 | +# -z, --zerocounters; run this first, then -c -i |
| 8 | +# --diff |
| 9 | +# --summary |
| 10 | +# other necessary options: |
| 11 | +# --directory; points to the source root. If left off it analyzes the kernel |
| 12 | +# --no-external; only analyze code in --directory |
| 13 | +# --build-directory; where the .no when different from the .da files |
| 14 | +# --branch-coverage; to ensure branch info is saved |
| 15 | +# --demangle-cpp; requires c++filt |
| 16 | + |
| 17 | +if (COVERAGE) |
| 18 | + find_program(LCOV lcov) |
| 19 | + find_program(GENINFO geninfo) |
| 20 | + find_program(GENHTML genhtml) |
| 21 | + find_program(CPPFILT c++filt) |
| 22 | + |
| 23 | + if (NOT LCOV OR NOT GENINFO OR NOT GENHTML OR NOT CPPFILT) |
| 24 | + MESSAGE(WARNING "A required program for presenting coverage information isn't available, disabling coverage") |
| 25 | + set(COVERAGE OFF CACHE INTERNAL "") |
| 26 | + return() |
| 27 | + endif() |
| 28 | +else() |
| 29 | + return() |
| 30 | +endif() |
| 31 | + |
| 32 | +execute_process(COMMAND lcov --version OUTPUT_VARIABLE lcov_version_response) |
| 33 | +string(REGEX MATCH "[-.0-9]+" lcov_version ${lcov_version_response}) |
| 34 | +set(LCOV_VERSION ${lcov_version} CACHE INTERNAL "") |
| 35 | + |
| 36 | +set(excludes_arg "") |
| 37 | +foreach (sys_path ${CMAKE_SYSTEM_PREFIX_PATH}) |
| 38 | + list(APPEND excludes_arg "--exclude" "${sys_path}/*") |
| 39 | +endforeach() |
| 40 | + |
| 41 | +set(ignores_arg "--ignore-errors" "unused,unused" "--ignore-errors" "mismatch,mismatch" |
| 42 | + "--ignore-errors" "empty,empty") |
| 43 | +list(APPEND ignores_arg "--rc" "geninfo_unexecuted_blocks=1") |
| 44 | +set(generate_flags "") |
| 45 | +set(geninfo_flags --quiet ${excludes_arg}) |
| 46 | +if (LCOV_VERSION VERSION_GREATER_EQUAL "2.0") |
| 47 | + list(APPEND geninfo_flags ${ignores_arg}) |
| 48 | + list(APPEND generate_flags "--branch-coverage" "--demangle-cpp" "c++filt") |
| 49 | +else() |
| 50 | + list(APPEND generate_flags "--rc" "lcov_branch_coverage=1" "--rc" "genhtml_demangle_cpp=1") |
| 51 | +endif() |
| 52 | +set(coverage_dir "${CMAKE_BINARY_DIR}/Coverage" CACHE INTERNAL "Directory to accumulate coverage tracefiles") |
| 53 | +set(coverage_html_dir "${CMAKE_BINARY_DIR}/Coverage-HTML" CACHE INTERNAL "Directory to place HTML coverage results pages") |
| 54 | + |
| 55 | +file(MAKE_DIRECTORY ${coverage_dir}) |
| 56 | + |
| 57 | +add_custom_target(lcov-initialize) |
| 58 | +if (LCOV_VERSION VERSION_GREATER_EQUAL "2.0") |
| 59 | + add_custom_target(lcov-collect |
| 60 | + COMMAND lcov ${geninfo_flags} -a ${coverage_dir}/*.info -o ${coverage_dir}/gnucash.info |
| 61 | + COMMAND lcov --summary ${coverage_dir}/gnucash.info |
| 62 | + VERBATIM |
| 63 | + COMMAND_EXPAND_LISTS) |
| 64 | +else() |
| 65 | + file(GENERATE OUTPUT ${CMAKE_BINARY_DIR}/collect.sh |
| 66 | + CONTENT |
| 67 | + "#!/bin/bash |
| 68 | +if [ -e $2 ] |
| 69 | + then rm $2 |
| 70 | +fi |
| 71 | +j=\"\" |
| 72 | +for i in $1/*.info |
| 73 | +do j=\"$j -a $i\" |
| 74 | +done |
| 75 | +lcov $j -o $2 |
| 76 | +" |
| 77 | + FILE_PERMISSIONS OWNER_EXECUTE OWNER_READ OWNER_WRITE WORLD_EXECUTE) |
| 78 | + |
| 79 | + add_custom_target(lcov-collect |
| 80 | + COMMAND ${CMAKE_COMMAND} -E env ${CMAKE_BINARY_DIR}/collect.sh ${coverage_dir} ${coverage_dir}/gnucash.info |
| 81 | + DEPENDS ${CMAKE_BINARY_DIR}/collect.sh |
| 82 | + VERBATIM |
| 83 | + COMMAND_EXPAND_LISTS) |
| 84 | +endif() |
| 85 | +set_target_properties(lcov-collect PROPERTIES ADDITIONAL_CLEAN_FILES "${coverage_dir}/gnucash.info") |
| 86 | + |
| 87 | +add_custom_target(lcov-generate-html |
| 88 | + genhtml --quiet --output-directory "${coverage_html_dir}" --legend --function-coverage ${generate_flags} ${coverage_dir}/gnucash.info |
| 89 | + VERBATIM |
| 90 | + COMMAND_EXPAND_LISTS) |
| 91 | +set_target_properties(lcov-generate-html PROPERTIES ADDITIONAL_CLEAN_FILES "${coverage_html_dir}") |
| 92 | + |
| 93 | +function (add_coverage_target tgt) |
| 94 | + get_target_property(build_dir ${tgt} BINARY_DIR) |
| 95 | + set(target_dir "${build_dir}/CMakeFiles/${tgt}.dir") |
| 96 | + |
| 97 | + add_custom_target(lcov-initialize-${tgt} |
| 98 | + lcov ${geninfo_flags} -z --directory ${target_dir} |
| 99 | + COMMAND lcov ${geninfo_flags} ${generate_flags} -c -i --directory ${target_dir} -o "${coverage_dir}/${tgt}_base.info" |
| 100 | + VERBATIM |
| 101 | + COMMAND_EXPAND_LISTS) |
| 102 | + add_dependencies(lcov-initialize lcov-initialize-${tgt}) |
| 103 | + add_dependencies(lcov-initialize-${tgt} ${tgt}) |
| 104 | + |
| 105 | + add_custom_target(lcov-collect-${tgt} |
| 106 | + COMMAND lcov ${geninfo_flags} ${generate_flags} -c --directory ${target_dir} -o "${coverage_dir}/${tgt}_result.info" |
| 107 | + VERBATIM |
| 108 | + COMMAND_EXPAND_LISTS) |
| 109 | + add_dependencies(lcov-collect lcov-collect-${tgt}) |
| 110 | + set_target_properties(${tgt} PROPERTIES ADDITIONAL_CLEAN_FILES "${coverage_dir}/${tgt}_base.info;${coverage_dir}/${tgt}_result.info") |
| 111 | +endFunction() |
0 commit comments