Raef Coles | 8cbeed5 | 2024-03-11 16:24:57 +0000 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | # ----------------------------------------------------------------------------- |
Jackson Cooper-Driver | 2d73606 | 2025-03-05 12:37:09 +0000 | [diff] [blame] | 3 | # Copyright (c) 2024-2025, Arm Limited. All rights reserved. |
Raef Coles | 8cbeed5 | 2024-03-11 16:24:57 +0000 | [diff] [blame] | 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | # ----------------------------------------------------------------------------- |
| 8 | |
| 9 | import argparse |
| 10 | import logging |
| 11 | import json |
| 12 | from os import listdir |
| 13 | from os.path import join, isfile, relpath |
| 14 | import subprocess |
| 15 | |
| 16 | class Source: |
| 17 | def __init__(self, location : str): |
| 18 | self.type = "git" |
| 19 | self.location = location |
| 20 | self.refspec = "" |
| 21 | |
| 22 | with open(join(location, ".git", "config"), "rt") as git_config_file: |
| 23 | url_line = [x for x in git_config_file.readlines() if "url" in x][0] |
| 24 | self.url = url_line.rstrip().replace("\turl = ", "").rstrip() |
| 25 | |
| 26 | with open(join(location, ".git", "HEAD"), "rt") as git_HEAD_file: |
| 27 | self.commit = git_HEAD_file.read().rstrip() |
| 28 | |
| 29 | self.location = relpath(location, args.source_dir) |
| 30 | |
| 31 | def get_tfm_dependencies(build_dir : str) -> [Source]: |
| 32 | dependencies = [] |
| 33 | |
| 34 | with open(join(build_dir, "CMakeCache.txt"), "rt") as cmakecache_file: |
| 35 | cmakecache = cmakecache_file.readlines() |
| 36 | variables = [x.rstrip().split("=") for x in cmakecache if "=" in x] |
| 37 | path_variables = [x for x in variables if "PATH" in x[0]] |
| 38 | |
| 39 | for _,p in path_variables: |
| 40 | try: |
| 41 | dependencies.append(Source(p)) |
| 42 | except (FileNotFoundError, NotADirectoryError): |
| 43 | continue |
| 44 | |
| 45 | return dependencies |
| 46 | |
| 47 | parser = argparse.ArgumentParser() |
| 48 | parser.add_argument("--build_dir", help="TF-M build directory", required=True) |
| 49 | parser.add_argument("--source_dir", help="TF-M source directory", required=True) |
| 50 | parser.add_argument("--tools_binary_dir", help="Binary dir in which objdump etc reside", required=False) |
| 51 | parser.add_argument("--log_level", help="Log level", choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], default="ERROR") |
| 52 | parser.add_argument("--output_config_file", help="output JSON file", required=True) |
| 53 | parser.add_argument("--output_intermediate_file", help="output intermediate file", required=True) |
| 54 | parser.add_argument("trace_file", nargs="+", help="input trace log files") |
Jackson Cooper-Driver | 2d73606 | 2025-03-05 12:37:09 +0000 | [diff] [blame] | 55 | parser.add_argument("--filter_elfs", help="comma-separated list of ELF files to generate report for", required=False) |
Raef Coles | 8cbeed5 | 2024-03-11 16:24:57 +0000 | [diff] [blame] | 56 | args = parser.parse_args() |
| 57 | |
| 58 | # logging setup |
| 59 | logging.basicConfig(level=args.log_level) |
| 60 | |
| 61 | configuration = { |
| 62 | "remove_workspace": True, |
| 63 | "include_assembly": True, |
| 64 | } |
| 65 | |
| 66 | if (args.tools_binary_dir): |
| 67 | tools_prefix = args.tools_binary_dir |
| 68 | else: |
| 69 | tools_prefix = "" |
| 70 | |
| 71 | tfm_source = Source(args.source_dir) |
| 72 | dependencies = get_tfm_dependencies(args.build_dir) |
| 73 | |
| 74 | parameters = { |
| 75 | "objdump" : join(tools_prefix, "arm-none-eabi-objdump"), |
| 76 | "readelf" : join(tools_prefix, "arm-none-eabi-readelf"), |
| 77 | "sources" : [ |
| 78 | { |
| 79 | "type" : x.type, |
| 80 | "URL": x.url, |
| 81 | "COMMIT" : x.commit, |
| 82 | "REFSPEC" : x.refspec, |
| 83 | "LOCATION" : x.location, |
| 84 | } for x in [tfm_source] + dependencies], |
| 85 | "workspace": args.source_dir, |
| 86 | "output_file": args.output_intermediate_file, |
| 87 | } |
| 88 | |
| 89 | bin_dir = join(args.build_dir, "bin") |
| 90 | elf_files = [join(bin_dir, x) for x in listdir(bin_dir) if isfile(join(bin_dir, x)) and "elf" in x] |
| 91 | |
Jackson Cooper-Driver | 2d73606 | 2025-03-05 12:37:09 +0000 | [diff] [blame] | 92 | if args.filter_elfs: |
| 93 | elf_files = [x for x in elf_files for f in args.filter_elfs.split(",") if f in x] |
| 94 | |
Raef Coles | 8cbeed5 | 2024-03-11 16:24:57 +0000 | [diff] [blame] | 95 | elfs = [ |
| 96 | { |
| 97 | "name": x, |
| 98 | "traces": args.trace_file, |
| 99 | } for x in elf_files] |
| 100 | |
| 101 | output = { |
| 102 | "configuration": configuration, |
| 103 | "parameters": parameters, |
| 104 | "elfs": elfs, |
| 105 | } |
| 106 | |
| 107 | with open(args.output_config_file, "w+") as output_file: |
| 108 | json.dump(output, output_file) |