From b3813bd4503be8f4120672300488b1cad6c7596e Mon Sep 17 00:00:00 2001 From: wangmingrong1 Date: Sun, 22 Dec 2024 20:45:20 +0800 Subject: [PATCH] gcov: Solve the problem of incorrect report generation caused by different compilation and test environments Due to the makefile, the relative paths of the files are different, and it is impossible to use existing tools such as gcov and lcov to specify a base compilation path for them. The solution for Linux is to copy a copy of the source code from the compilation environment to the test environment, and there is a place that is consistent with the absolute path of the compilation environment. This solution is to pass in the "-b" parameter to directly modify the info file generated by lcov. The search path will be executed in the next step of genhtml Signed-off-by: wangmingrong1 --- tools/gcov.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tools/gcov.py b/tools/gcov.py index d888658181..314bde0643 100755 --- a/tools/gcov.py +++ b/tools/gcov.py @@ -73,6 +73,20 @@ def parse_gcda_data(path): output += line.strip() +def correct_content_path(file, newpath): + with open(file, "r", encoding="utf-8") as f: + content = f.read() + + pattern = r"SF:([^\s]*?)/nuttx/include/nuttx" + matches = re.findall(pattern, content) + + if matches: + new_content = content.replace(matches[0], newpath) + + with open(file, "w", encoding="utf-8") as f: + f.write(new_content) + + def copy_file_endswith(endswith, source_dir, target_dir): print(f"Collect {endswith} files {source_dir} -> {target_dir}") @@ -93,6 +107,7 @@ def arg_parser(): ) parser.add_argument("-i", "--input", help="Input dump data") parser.add_argument("-t", dest="gcov_tool", help="Path to gcov tool") + parser.add_argument("-b", dest="base_dir", help="Compile base directory") parser.add_argument("-s", dest="gcno_dir", help="Directory containing gcno files") parser.add_argument("-a", dest="gcda_dir", help="Directory containing gcda files") parser.add_argument("--debug", action="store_true", help="Enable debug mode") @@ -171,6 +186,9 @@ def main(): stderr=sys.stdout, ) + if args.base_dir: + correct_content_path(coverage_file, args.base_dir) + # genhtml generate coverage report subprocess.run( [