W-M-R commented on code in PR #15268: URL: https://github.com/apache/nuttx/pull/15268#discussion_r1891135422
########## tools/gcov.py: ########## @@ -0,0 +1,209 @@ +#!/usr/bin/env python3 +# tools/gcov.py + +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import argparse +import os +import re +import shutil +import subprocess +import sys + + +def parse_gcda_data(input_file): + with open(input_file, "r") as file: + lines = file.read().strip().splitlines() + + for line in lines: + if not line.startswith("gcov start"): + continue + + match = re.search(r"filename:(.*?)\s+size:\s*(\d+)Byte", line) + if not match: + continue + + hex_dump = "" + filename = match.group(1) + size = int(match.group(2)) + + # Read the hex dump until the end of the file + next_line_index = lines.index(line) + 1 + while next_line_index < len(lines) and not lines[next_line_index].startswith( + "gcov end" + ): + hex_dump += lines[next_line_index].strip() + next_line_index += 1 + + if size != len(hex_dump) // 2: + print( + f"Size mismatch for {filename}: expected {size} bytes, got {len(hex_dump) // 2} bytes" + ) + + checksum_match = ( + re.search(r"checksum:\s*(0x[0-9a-fA-F]+)", lines[next_line_index]) + if next_line_index < len(lines) + else None + ) + if not checksum_match: + continue + + checksum = int(checksum_match.group(1), 16) + calculated_checksum = sum(bytearray.fromhex(hex_dump)) % 65536 + if calculated_checksum != checksum: + print( + f"Checksum mismatch for {filename}: expected {checksum}, got {calculated_checksum}" + ) + continue + + with open(filename, "wb") as fp: + fp.write(bytes.fromhex(hex_dump)) + print(f"write {filename} success") + + +def collect_match_endswith(endswith, source_dir, target_dir): + print(f"Collect {endswith} files {source_dir} -> {target_dir}") + + if not os.path.exists(target_dir): + os.makedirs(target_dir) + + for _root, _, _files in os.walk(source_dir): + for _file in _files: + if _file.endswith(endswith): + source_file = os.path.join(_root, _file) + target_file = os.path.join(target_dir, _file) + shutil.copy2(source_file, target_file) + + +def arg_parser(): + parser = argparse.ArgumentParser( + description="Code coverage generation tool.", add_help=False + ) + parser.add_argument("-i", "--input", help="Input dump data") + parser.add_argument("-t", dest="gcov_tool", help="Path to gcov tool") + 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") + parser.add_argument( + "-x", + dest="only_copy", + action="store_true", + help="Only copy *.gcno and *.gcda files", + ) + parser.add_argument( + "gcov_dir", + nargs="?", + default=os.getcwd(), + help="Directory to store gcov data and report", + ) + + return parser.parse_args() + + +def main(): + args = arg_parser() + + root_dir = os.getcwd() + gcov_dir = os.path.abspath(args.gcov_dir) + gcno_dir = os.path.abspath(args.gcno_dir) if args.gcno_dir else root_dir + gcda_dir = os.path.abspath(args.gcda_dir) if args.gcda_dir else root_dir + + coverage_file = os.path.join(gcov_dir, "coverage.info") + result_dir = os.path.join(gcov_dir, "result") + gcov_data_dir = os.path.join(gcov_dir, "data") + + if args.debug: + debug_file = os.path.join(gcov_dir, "debug.log") + sys.stdout = open(debug_file, "w+") Review Comment: The autest process cannot get the running script log information. If an error occurs, it may not be found and saved in this file. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
